blob: 1bef66358a1148a7797ccb0ddc95bddd869e8cf5 [file] [log] [blame]
Hunter Knepshield1b92d262020-01-15 17:48:01 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "dumpstate_1_1_hidl_hal_test"
18
19#include <fcntl.h>
20#include <unistd.h>
Hunter Knepshield7b20bd72020-01-22 18:25:14 -080021
Hunter Knepshield256f77a2020-02-03 16:25:57 -080022#include <functional>
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080023#include <tuple>
Hunter Knepshield1b92d262020-01-15 17:48:01 -080024#include <vector>
25
26#include <android/hardware/dumpstate/1.1/IDumpstateDevice.h>
27#include <android/hardware/dumpstate/1.1/types.h>
28#include <cutils/native_handle.h>
29#include <gtest/gtest.h>
30#include <hidl/GtestPrinter.h>
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080031#include <hidl/HidlSupport.h>
Hunter Knepshield1b92d262020-01-15 17:48:01 -080032#include <hidl/ServiceManagement.h>
33#include <log/log.h>
34
Hunter Knepshield7b20bd72020-01-22 18:25:14 -080035namespace {
36
Hunter Knepshield1b92d262020-01-15 17:48:01 -080037using ::android::sp;
38using ::android::hardware::Return;
39using ::android::hardware::dumpstate::V1_1::DumpstateMode;
Hunter Knepshield256f77a2020-02-03 16:25:57 -080040using ::android::hardware::dumpstate::V1_1::DumpstateStatus;
Hunter Knepshield1b92d262020-01-15 17:48:01 -080041using ::android::hardware::dumpstate::V1_1::IDumpstateDevice;
Hunter Knepshield256f77a2020-02-03 16:25:57 -080042using ::android::hardware::dumpstate::V1_1::toString;
Hunter Knepshield1b92d262020-01-15 17:48:01 -080043
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080044// Base class common to all dumpstate HAL v1.1 tests.
45template <typename T>
46class DumpstateHidl1_1TestBase : public ::testing::TestWithParam<T> {
Hunter Knepshield256f77a2020-02-03 16:25:57 -080047 protected:
48 virtual void SetUp() override { GetService(); }
49
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080050 virtual std::string GetInstanceName() = 0;
51
Hunter Knepshield256f77a2020-02-03 16:25:57 -080052 void GetService() {
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080053 const std::string instance_name = GetInstanceName();
54 dumpstate = IDumpstateDevice::getService(instance_name);
55 ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance " << instance_name;
Hunter Knepshield1b92d262020-01-15 17:48:01 -080056 }
57
Hunter Knepshield84dbf582020-02-12 18:55:28 -080058 void ToggleVerboseLogging(bool enable) {
59 Return<void> status = dumpstate->setVerboseLoggingEnabled(enable);
Hunter Knepshield256f77a2020-02-03 16:25:57 -080060 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
61
62 if (!dumpstate->ping().isOk()) {
63 ALOGW("IDumpstateDevice service appears to have exited lazily, attempting to get "
64 "again");
65 GetService();
66 }
67
Hunter Knepshield84dbf582020-02-12 18:55:28 -080068 Return<bool> logging_enabled = dumpstate->getVerboseLoggingEnabled();
Hunter Knepshield256f77a2020-02-03 16:25:57 -080069 ASSERT_TRUE(logging_enabled.isOk())
70 << "Status should be ok: " << logging_enabled.description();
71 ASSERT_EQ(logging_enabled, enable)
Hunter Knepshield84dbf582020-02-12 18:55:28 -080072 << "Verbose logging should now be " << (enable ? "enabled" : "disabled");
Hunter Knepshield256f77a2020-02-03 16:25:57 -080073
74 if (!dumpstate->ping().isOk()) {
75 ALOGW("IDumpstateDevice service appears to have exited lazily, attempting to get "
76 "again");
77 GetService();
78 }
79 }
80
Hunter Knepshield84dbf582020-02-12 18:55:28 -080081 void EnableVerboseLogging() { ToggleVerboseLogging(true); }
Hunter Knepshield256f77a2020-02-03 16:25:57 -080082
Hunter Knepshield84dbf582020-02-12 18:55:28 -080083 void DisableVerboseLogging() { ToggleVerboseLogging(false); }
Hunter Knepshield256f77a2020-02-03 16:25:57 -080084
Hunter Knepshield1b92d262020-01-15 17:48:01 -080085 sp<IDumpstateDevice> dumpstate;
86};
87
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080088// Tests that don't need to iterate every single DumpstateMode value for dumpstateBoard_1_1.
89class DumpstateHidl1_1GeneralTest : public DumpstateHidl1_1TestBase<std::string> {
90 protected:
91 virtual std::string GetInstanceName() override { return GetParam(); }
92};
Hunter Knepshield1b92d262020-01-15 17:48:01 -080093
Hunter Knepshieldac0680b2020-02-20 13:04:17 -080094// Tests that iterate every single DumpstateMode value for dumpstateBoard_1_1.
95class DumpstateHidl1_1PerModeTest
96 : public DumpstateHidl1_1TestBase<std::tuple<std::string, DumpstateMode>> {
97 protected:
98 virtual std::string GetInstanceName() override { return std::get<0>(GetParam()); }
99
100 DumpstateMode GetMode() { return std::get<1>(GetParam()); }
101
102 // Will only execute additional_assertions when status == expected.
103 void AssertStatusForMode(const Return<DumpstateStatus>& status, const DumpstateStatus expected,
104 std::function<void()> additional_assertions = nullptr) {
105 ASSERT_TRUE(status.isOk())
106 << "Status should be ok and return a more specific DumpstateStatus: "
107 << status.description();
108 if (GetMode() == DumpstateMode::DEFAULT) {
109 ASSERT_EQ(expected, status)
110 << "Required mode (DumpstateMode::" << toString(GetMode())
111 << "): status should be DumpstateStatus::" << toString(expected)
112 << ", but got DumpstateStatus::" << toString(status);
113 } else {
114 // The rest of the modes are optional to support, but they MUST return either the
115 // expected value or UNSUPPORTED_MODE.
116 ASSERT_TRUE(status == expected || status == DumpstateStatus::UNSUPPORTED_MODE)
117 << "Optional mode (DumpstateMode::" << toString(GetMode())
118 << "): status should be DumpstateStatus::" << toString(expected)
119 << " or DumpstateStatus::UNSUPPORTED_MODE, but got DumpstateStatus::"
120 << toString(status);
121 }
122 if (status == expected && additional_assertions != nullptr) {
123 additional_assertions();
124 }
125 }
126};
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800127
Hunter Knepshield7b20bd72020-01-22 18:25:14 -0800128constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800129
130// Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800131TEST_P(DumpstateHidl1_1PerModeTest, TestNullHandle) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800132 EnableVerboseLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800133
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800134 Return<DumpstateStatus> status =
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800135 dumpstate->dumpstateBoard_1_1(nullptr, GetMode(), kDefaultTimeoutMillis);
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800136
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800137 AssertStatusForMode(status, DumpstateStatus::ILLEGAL_ARGUMENT);
138}
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800139
140// Negative test: make sure dumpstateBoard() ignores a handle with no FD.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800141TEST_P(DumpstateHidl1_1PerModeTest, TestHandleWithNoFd) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800142 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800143
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800144 native_handle_t* handle = native_handle_create(0, 0);
145 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
146
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800147 Return<DumpstateStatus> status =
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800148 dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800149
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800150 AssertStatusForMode(status, DumpstateStatus::ILLEGAL_ARGUMENT);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800151
152 native_handle_close(handle);
153 native_handle_delete(handle);
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800154}
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800155
156// Positive test: make sure dumpstateBoard() writes something to the FD.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800157TEST_P(DumpstateHidl1_1PerModeTest, TestOk) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800158 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800159
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800160 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
161 int fds[2];
162 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
163
164 native_handle_t* handle = native_handle_create(1, 0);
165 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
166 handle->data[0] = fds[1];
167
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800168 Return<DumpstateStatus> status =
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800169 dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800170
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800171 AssertStatusForMode(status, DumpstateStatus::OK, [&fds]() {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800172 // Check that at least one byte was written.
173 char buff;
174 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
175 });
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800176
177 native_handle_close(handle);
178 native_handle_delete(handle);
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800179}
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800180
181// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800182TEST_P(DumpstateHidl1_1PerModeTest, TestHandleWithTwoFds) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800183 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800184
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800185 int fds1[2];
186 int fds2[2];
187 ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
188 ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
189
190 native_handle_t* handle = native_handle_create(2, 0);
191 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
192 handle->data[0] = fds1[1];
193 handle->data[1] = fds2[1];
194
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800195 Return<DumpstateStatus> status =
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800196 dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis);
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800197
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800198 AssertStatusForMode(status, DumpstateStatus::OK, [&fds1, &fds2]() {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800199 // Check that at least one byte was written to one of the FDs.
200 char buff;
201 size_t read1 = read(fds1[0], &buff, 1);
202 size_t read2 = read(fds2[0], &buff, 1);
203 // Sometimes read returns -1, so we can't just add them together and expect >= 1.
204 ASSERT_TRUE(read1 == 1 || read2 == 1) << "Dumped nothing";
205 });
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800206
207 native_handle_close(handle);
208 native_handle_delete(handle);
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800209}
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800210
211// Make sure dumpstateBoard_1_1 actually validates its arguments.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800212TEST_P(DumpstateHidl1_1GeneralTest, TestInvalidModeArgument_Negative) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800213 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800214
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800215 int fds[2];
216 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
217
218 native_handle_t* handle = native_handle_create(1, 0);
219 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
220 handle->data[0] = fds[1];
221
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800222 Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
223 handle, static_cast<DumpstateMode>(-100), kDefaultTimeoutMillis);
224
225 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
226 << status.description();
227 ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
228 << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800229
230 native_handle_close(handle);
231 native_handle_delete(handle);
232}
233
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800234TEST_P(DumpstateHidl1_1GeneralTest, TestInvalidModeArgument_Undefined) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800235 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800236
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800237 int fds[2];
238 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
239
240 native_handle_t* handle = native_handle_create(1, 0);
241 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
242 handle->data[0] = fds[1];
243
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800244 Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
245 handle, static_cast<DumpstateMode>(9001), kDefaultTimeoutMillis);
246
247 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
248 << status.description();
249 ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
250 << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800251
252 native_handle_close(handle);
253 native_handle_delete(handle);
254}
255
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800256// Positive test: make sure dumpstateBoard() from 1.0 doesn't fail.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800257TEST_P(DumpstateHidl1_1GeneralTest, Test1_0MethodOk) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800258 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800259
260 int fds[2];
261 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
262
263 native_handle_t* handle = native_handle_create(1, 0);
264 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
265 handle->data[0] = fds[1];
266
267 Return<void> status = dumpstate->dumpstateBoard(handle);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800268
269 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800270
271 // Check that at least one byte was written.
272 char buff;
273 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
274
275 native_handle_close(handle);
276 native_handle_delete(handle);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800277}
278
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800279// Make sure disabling verbose logging behaves correctly. Some info is still allowed to be emitted,
280// but it can't have privacy/storage/battery impacts.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800281TEST_P(DumpstateHidl1_1PerModeTest, TestDeviceLoggingDisabled) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800282 DisableVerboseLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800283
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800284 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
285 int fds[2];
286 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
287
288 native_handle_t* handle = native_handle_create(1, 0);
289 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
290 handle->data[0] = fds[1];
291
292 Return<DumpstateStatus> status =
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800293 dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis);
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800294
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800295 // We don't include additional assertions here about the file passed in. If verbose logging is
296 // disabled, the OEM may choose to include nothing at all, but it is allowed to include some
297 // essential information based on the mode as long as it isn't private user information.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800298 AssertStatusForMode(status, DumpstateStatus::OK);
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800299
300 native_handle_close(handle);
301 native_handle_delete(handle);
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800302}
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800303
304// Double-enable is perfectly valid, but the second call shouldn't do anything.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800305TEST_P(DumpstateHidl1_1GeneralTest, TestRepeatedEnable) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800306 EnableVerboseLogging();
307 EnableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800308}
309
310// Double-disable is perfectly valid, but the second call shouldn't do anything.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800311TEST_P(DumpstateHidl1_1GeneralTest, TestRepeatedDisable) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800312 DisableVerboseLogging();
313 DisableVerboseLogging();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800314}
315
316// Toggling in short order is perfectly valid.
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800317TEST_P(DumpstateHidl1_1GeneralTest, TestRepeatedToggle) {
Hunter Knepshield84dbf582020-02-12 18:55:28 -0800318 EnableVerboseLogging();
319 DisableVerboseLogging();
320 EnableVerboseLogging();
321 DisableVerboseLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800322}
323
324INSTANTIATE_TEST_SUITE_P(
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800325 PerInstance, DumpstateHidl1_1GeneralTest,
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800326 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)),
327 android::hardware::PrintInstanceNameToString);
Hunter Knepshield7b20bd72020-01-22 18:25:14 -0800328
Hunter Knepshieldac0680b2020-02-20 13:04:17 -0800329// Includes the mode's name as part of the description string.
330static inline std::string PrintInstanceNameToStringWithMode(
331 const testing::TestParamInfo<std::tuple<std::string, DumpstateMode>>& info) {
332 return android::hardware::PrintInstanceNameToString(
333 testing::TestParamInfo(std::get<0>(info.param), info.index)) +
334 "_" + toString(std::get<1>(info.param));
335}
336
337INSTANTIATE_TEST_SUITE_P(
338 PerInstanceAndMode, DumpstateHidl1_1PerModeTest,
339 testing::Combine(testing::ValuesIn(android::hardware::getAllHalInstanceNames(
340 IDumpstateDevice::descriptor)),
341 testing::ValuesIn(android::hardware::hidl_enum_range<DumpstateMode>())),
342 PrintInstanceNameToStringWithMode);
343
Hunter Knepshield7b20bd72020-01-22 18:25:14 -0800344} // namespace