blob: 1811b7312b76ea7279daa3b669a2385398553cfa [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 Knepshield1b92d262020-01-15 17:48:01 -080023#include <vector>
24
25#include <android/hardware/dumpstate/1.1/IDumpstateDevice.h>
26#include <android/hardware/dumpstate/1.1/types.h>
27#include <cutils/native_handle.h>
28#include <gtest/gtest.h>
29#include <hidl/GtestPrinter.h>
30#include <hidl/ServiceManagement.h>
31#include <log/log.h>
32
Hunter Knepshield7b20bd72020-01-22 18:25:14 -080033namespace {
34
Hunter Knepshield1b92d262020-01-15 17:48:01 -080035using ::android::sp;
36using ::android::hardware::Return;
37using ::android::hardware::dumpstate::V1_1::DumpstateMode;
Hunter Knepshield256f77a2020-02-03 16:25:57 -080038using ::android::hardware::dumpstate::V1_1::DumpstateStatus;
Hunter Knepshield1b92d262020-01-15 17:48:01 -080039using ::android::hardware::dumpstate::V1_1::IDumpstateDevice;
Hunter Knepshield256f77a2020-02-03 16:25:57 -080040using ::android::hardware::dumpstate::V1_1::toString;
Hunter Knepshield1b92d262020-01-15 17:48:01 -080041
42class DumpstateHidl1_1Test : public ::testing::TestWithParam<std::string> {
Hunter Knepshield256f77a2020-02-03 16:25:57 -080043 protected:
44 virtual void SetUp() override { GetService(); }
45
46 void GetService() {
Hunter Knepshield1b92d262020-01-15 17:48:01 -080047 dumpstate = IDumpstateDevice::getService(GetParam());
48 ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance";
49 }
50
Hunter Knepshield256f77a2020-02-03 16:25:57 -080051 void ToggleDeviceLogging(bool enable) {
52 Return<void> status = dumpstate->setDeviceLoggingEnabled(enable);
53 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
54
55 if (!dumpstate->ping().isOk()) {
56 ALOGW("IDumpstateDevice service appears to have exited lazily, attempting to get "
57 "again");
58 GetService();
59 }
60
61 Return<bool> logging_enabled = dumpstate->getDeviceLoggingEnabled();
62 ASSERT_TRUE(logging_enabled.isOk())
63 << "Status should be ok: " << logging_enabled.description();
64 ASSERT_EQ(logging_enabled, enable)
65 << "Device logging should now be " << (enable ? "enabled" : "disabled");
66
67 if (!dumpstate->ping().isOk()) {
68 ALOGW("IDumpstateDevice service appears to have exited lazily, attempting to get "
69 "again");
70 GetService();
71 }
72 }
73
74 void EnableDeviceLogging() { ToggleDeviceLogging(true); }
75
76 void DisableDeviceLogging() { ToggleDeviceLogging(false); }
77
Hunter Knepshield1b92d262020-01-15 17:48:01 -080078 sp<IDumpstateDevice> dumpstate;
79};
80
81#define TEST_FOR_DUMPSTATE_MODE(name, body, mode) \
82 TEST_P(DumpstateHidl1_1Test, name##_##mode) { body(DumpstateMode::mode); }
83
Hunter Knepshield256f77a2020-02-03 16:25:57 -080084// We use a macro to define individual test cases instead of hidl_enum_range<> because some HAL
85// implementations are lazy and may call exit() at the end of dumpstateBoard(), which would cause
86// DEAD_OBJECT errors after the first iteration. Separate cases re-get the service each time as part
87// of SetUp(), and also provide better separation of concerns when specific modes are problematic.
Hunter Knepshield1b92d262020-01-15 17:48:01 -080088#define TEST_FOR_ALL_DUMPSTATE_MODES(name, body) \
89 TEST_FOR_DUMPSTATE_MODE(name, body, FULL); \
90 TEST_FOR_DUMPSTATE_MODE(name, body, INTERACTIVE); \
91 TEST_FOR_DUMPSTATE_MODE(name, body, REMOTE); \
92 TEST_FOR_DUMPSTATE_MODE(name, body, WEAR); \
93 TEST_FOR_DUMPSTATE_MODE(name, body, CONNECTIVITY); \
94 TEST_FOR_DUMPSTATE_MODE(name, body, WIFI); \
Mike Ma6165e2d2020-02-13 13:11:00 -080095 TEST_FOR_DUMPSTATE_MODE(name, body, DEFAULT); \
96 TEST_FOR_DUMPSTATE_MODE(name, body, PROTO);
Hunter Knepshield1b92d262020-01-15 17:48:01 -080097
Hunter Knepshield7b20bd72020-01-22 18:25:14 -080098constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds
Hunter Knepshield1b92d262020-01-15 17:48:01 -080099
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800100// Will only execute additional_assertions when status == expected.
101void AssertStatusForMode(const DumpstateMode mode, const Return<DumpstateStatus>& status,
102 const DumpstateStatus expected,
103 std::function<void()> additional_assertions = nullptr) {
104 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
105 << status.description();
106 if (mode == DumpstateMode::DEFAULT) {
107 ASSERT_EQ(expected, status) << "Required mode (DumpstateMode::" << toString(mode)
108 << "): status should be DumpstateStatus::" << toString(expected)
109 << ", but got DumpstateStatus::" << toString(status);
110 } else {
111 // The rest of the modes are optional to support, but they MUST return either the expected
112 // value or UNSUPPORTED_MODE.
113 ASSERT_TRUE(status == expected || status == DumpstateStatus::UNSUPPORTED_MODE)
114 << "Optional mode (DumpstateMode::" << toString(mode)
115 << "): status should be DumpstateStatus::" << toString(expected)
116 << " or DumpstateStatus::UNSUPPORTED_MODE, but got DumpstateStatus::"
117 << toString(status);
118 }
119 if (status == expected && additional_assertions != nullptr) {
120 additional_assertions();
121 }
122}
123
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800124// Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
125TEST_FOR_ALL_DUMPSTATE_MODES(TestNullHandle, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800126 EnableDeviceLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800127
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800128 Return<DumpstateStatus> status =
129 dumpstate->dumpstateBoard_1_1(nullptr, mode, kDefaultTimeoutMillis);
130
131 AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800132});
133
134// Negative test: make sure dumpstateBoard() ignores a handle with no FD.
135TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithNoFd, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800136 EnableDeviceLogging();
137
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800138 native_handle_t* handle = native_handle_create(0, 0);
139 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
140
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800141 Return<DumpstateStatus> status =
142 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800143
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800144 AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800145
146 native_handle_close(handle);
147 native_handle_delete(handle);
148});
149
150// Positive test: make sure dumpstateBoard() writes something to the FD.
151TEST_FOR_ALL_DUMPSTATE_MODES(TestOk, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800152 EnableDeviceLogging();
153
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800154 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
155 int fds[2];
156 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
157
158 native_handle_t* handle = native_handle_create(1, 0);
159 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
160 handle->data[0] = fds[1];
161
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800162 Return<DumpstateStatus> status =
163 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800164
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800165 AssertStatusForMode(mode, status, DumpstateStatus::OK, [&fds]() {
166 // Check that at least one byte was written.
167 char buff;
168 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
169 });
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800170
171 native_handle_close(handle);
172 native_handle_delete(handle);
173});
174
175// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
176TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithTwoFds, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800177 EnableDeviceLogging();
178
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800179 int fds1[2];
180 int fds2[2];
181 ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
182 ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
183
184 native_handle_t* handle = native_handle_create(2, 0);
185 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
186 handle->data[0] = fds1[1];
187 handle->data[1] = fds2[1];
188
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800189 Return<DumpstateStatus> status =
190 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
191
192 AssertStatusForMode(mode, status, DumpstateStatus::OK, [&fds1, &fds2]() {
193 // Check that at least one byte was written to one of the FDs.
194 char buff;
195 size_t read1 = read(fds1[0], &buff, 1);
196 size_t read2 = read(fds2[0], &buff, 1);
197 // Sometimes read returns -1, so we can't just add them together and expect >= 1.
198 ASSERT_TRUE(read1 == 1 || read2 == 1) << "Dumped nothing";
199 });
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800200
201 native_handle_close(handle);
202 native_handle_delete(handle);
203});
204
205// Make sure dumpstateBoard_1_1 actually validates its arguments.
206TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Negative) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800207 EnableDeviceLogging();
208
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800209 int fds[2];
210 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
211
212 native_handle_t* handle = native_handle_create(1, 0);
213 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
214 handle->data[0] = fds[1];
215
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800216 Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
217 handle, static_cast<DumpstateMode>(-100), kDefaultTimeoutMillis);
218
219 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
220 << status.description();
221 ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
222 << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800223
224 native_handle_close(handle);
225 native_handle_delete(handle);
226}
227
228TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Undefined) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800229 EnableDeviceLogging();
230
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800231 int fds[2];
232 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
233
234 native_handle_t* handle = native_handle_create(1, 0);
235 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
236 handle->data[0] = fds[1];
237
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800238 Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
239 handle, static_cast<DumpstateMode>(9001), kDefaultTimeoutMillis);
240
241 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
242 << status.description();
243 ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
244 << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800245
246 native_handle_close(handle);
247 native_handle_delete(handle);
248}
249
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800250// Positive test: make sure dumpstateBoard() from 1.0 doesn't fail.
251TEST_P(DumpstateHidl1_1Test, Test1_0MethodOk) {
252 EnableDeviceLogging();
253
254 int fds[2];
255 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
256
257 native_handle_t* handle = native_handle_create(1, 0);
258 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
259 handle->data[0] = fds[1];
260
261 Return<void> status = dumpstate->dumpstateBoard(handle);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800262
263 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800264
265 // Check that at least one byte was written.
266 char buff;
267 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
268
269 native_handle_close(handle);
270 native_handle_delete(handle);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800271}
272
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800273// Make sure disabling device logging behaves correctly.
274TEST_FOR_ALL_DUMPSTATE_MODES(TestDeviceLoggingDisabled, [this](DumpstateMode mode) {
275 DisableDeviceLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800276
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800277 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
278 int fds[2];
279 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
280
281 native_handle_t* handle = native_handle_create(1, 0);
282 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
283 handle->data[0] = fds[1];
284
285 Return<DumpstateStatus> status =
286 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
287
288 AssertStatusForMode(mode, status, DumpstateStatus::DEVICE_LOGGING_NOT_ENABLED, [&fds]() {
289 // Check that nothing was written. Could return 0 or -1.
290 char buff;
291 ASSERT_NE(1, read(fds[0], &buff, 1)) << "Dumped something when device logging is disabled";
292 });
293
294 native_handle_close(handle);
295 native_handle_delete(handle);
296});
297
298// Double-enable is perfectly valid, but the second call shouldn't do anything.
299TEST_P(DumpstateHidl1_1Test, TestRepeatedEnable) {
300 EnableDeviceLogging();
301 EnableDeviceLogging();
302}
303
304// Double-disable is perfectly valid, but the second call shouldn't do anything.
305TEST_P(DumpstateHidl1_1Test, TestRepeatedDisable) {
306 DisableDeviceLogging();
307 DisableDeviceLogging();
308}
309
310// Toggling in short order is perfectly valid.
311TEST_P(DumpstateHidl1_1Test, TestRepeatedToggle) {
312 EnableDeviceLogging();
313 DisableDeviceLogging();
314 EnableDeviceLogging();
315 DisableDeviceLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800316}
317
318INSTANTIATE_TEST_SUITE_P(
319 PerInstance, DumpstateHidl1_1Test,
320 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)),
321 android::hardware::PrintInstanceNameToString);
Hunter Knepshield7b20bd72020-01-22 18:25:14 -0800322
323} // namespace