blob: 089b039c2ce2a9501a8c50362313ddc6229af296 [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); \
95 TEST_FOR_DUMPSTATE_MODE(name, body, DEFAULT);
96
Hunter Knepshield7b20bd72020-01-22 18:25:14 -080097constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds
Hunter Knepshield1b92d262020-01-15 17:48:01 -080098
Hunter Knepshield256f77a2020-02-03 16:25:57 -080099// Will only execute additional_assertions when status == expected.
100void AssertStatusForMode(const DumpstateMode mode, const Return<DumpstateStatus>& status,
101 const DumpstateStatus expected,
102 std::function<void()> additional_assertions = nullptr) {
103 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
104 << status.description();
105 if (mode == DumpstateMode::DEFAULT) {
106 ASSERT_EQ(expected, status) << "Required mode (DumpstateMode::" << toString(mode)
107 << "): status should be DumpstateStatus::" << toString(expected)
108 << ", but got DumpstateStatus::" << toString(status);
109 } else {
110 // The rest of the modes are optional to support, but they MUST return either the expected
111 // value or UNSUPPORTED_MODE.
112 ASSERT_TRUE(status == expected || status == DumpstateStatus::UNSUPPORTED_MODE)
113 << "Optional mode (DumpstateMode::" << toString(mode)
114 << "): status should be DumpstateStatus::" << toString(expected)
115 << " or DumpstateStatus::UNSUPPORTED_MODE, but got DumpstateStatus::"
116 << toString(status);
117 }
118 if (status == expected && additional_assertions != nullptr) {
119 additional_assertions();
120 }
121}
122
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800123// Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
124TEST_FOR_ALL_DUMPSTATE_MODES(TestNullHandle, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800125 EnableDeviceLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800126
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800127 Return<DumpstateStatus> status =
128 dumpstate->dumpstateBoard_1_1(nullptr, mode, kDefaultTimeoutMillis);
129
130 AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800131});
132
133// Negative test: make sure dumpstateBoard() ignores a handle with no FD.
134TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithNoFd, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800135 EnableDeviceLogging();
136
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800137 native_handle_t* handle = native_handle_create(0, 0);
138 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
139
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800140 Return<DumpstateStatus> status =
141 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800142
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800143 AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800144
145 native_handle_close(handle);
146 native_handle_delete(handle);
147});
148
149// Positive test: make sure dumpstateBoard() writes something to the FD.
150TEST_FOR_ALL_DUMPSTATE_MODES(TestOk, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800151 EnableDeviceLogging();
152
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800153 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
154 int fds[2];
155 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
156
157 native_handle_t* handle = native_handle_create(1, 0);
158 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
159 handle->data[0] = fds[1];
160
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800161 Return<DumpstateStatus> status =
162 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800163
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800164 AssertStatusForMode(mode, status, DumpstateStatus::OK, [&fds]() {
165 // Check that at least one byte was written.
166 char buff;
167 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
168 });
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800169
170 native_handle_close(handle);
171 native_handle_delete(handle);
172});
173
174// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
175TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithTwoFds, [this](DumpstateMode mode) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800176 EnableDeviceLogging();
177
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800178 int fds1[2];
179 int fds2[2];
180 ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
181 ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
182
183 native_handle_t* handle = native_handle_create(2, 0);
184 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
185 handle->data[0] = fds1[1];
186 handle->data[1] = fds2[1];
187
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800188 Return<DumpstateStatus> status =
189 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
190
191 AssertStatusForMode(mode, status, DumpstateStatus::OK, [&fds1, &fds2]() {
192 // Check that at least one byte was written to one of the FDs.
193 char buff;
194 size_t read1 = read(fds1[0], &buff, 1);
195 size_t read2 = read(fds2[0], &buff, 1);
196 // Sometimes read returns -1, so we can't just add them together and expect >= 1.
197 ASSERT_TRUE(read1 == 1 || read2 == 1) << "Dumped nothing";
198 });
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800199
200 native_handle_close(handle);
201 native_handle_delete(handle);
202});
203
204// Make sure dumpstateBoard_1_1 actually validates its arguments.
205TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Negative) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800206 EnableDeviceLogging();
207
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800208 int fds[2];
209 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
210
211 native_handle_t* handle = native_handle_create(1, 0);
212 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
213 handle->data[0] = fds[1];
214
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800215 Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
216 handle, static_cast<DumpstateMode>(-100), kDefaultTimeoutMillis);
217
218 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
219 << status.description();
220 ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
221 << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800222
223 native_handle_close(handle);
224 native_handle_delete(handle);
225}
226
227TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Undefined) {
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800228 EnableDeviceLogging();
229
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800230 int fds[2];
231 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
232
233 native_handle_t* handle = native_handle_create(1, 0);
234 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
235 handle->data[0] = fds[1];
236
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800237 Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
238 handle, static_cast<DumpstateMode>(9001), kDefaultTimeoutMillis);
239
240 ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
241 << status.description();
242 ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
243 << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800244
245 native_handle_close(handle);
246 native_handle_delete(handle);
247}
248
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800249// Positive test: make sure dumpstateBoard() from 1.0 doesn't fail.
250TEST_P(DumpstateHidl1_1Test, Test1_0MethodOk) {
251 EnableDeviceLogging();
252
253 int fds[2];
254 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
255
256 native_handle_t* handle = native_handle_create(1, 0);
257 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
258 handle->data[0] = fds[1];
259
260 Return<void> status = dumpstate->dumpstateBoard(handle);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800261
262 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800263
264 // Check that at least one byte was written.
265 char buff;
266 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
267
268 native_handle_close(handle);
269 native_handle_delete(handle);
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800270}
271
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800272// Make sure disabling device logging behaves correctly.
273TEST_FOR_ALL_DUMPSTATE_MODES(TestDeviceLoggingDisabled, [this](DumpstateMode mode) {
274 DisableDeviceLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800275
Hunter Knepshield256f77a2020-02-03 16:25:57 -0800276 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
277 int fds[2];
278 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
279
280 native_handle_t* handle = native_handle_create(1, 0);
281 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
282 handle->data[0] = fds[1];
283
284 Return<DumpstateStatus> status =
285 dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
286
287 AssertStatusForMode(mode, status, DumpstateStatus::DEVICE_LOGGING_NOT_ENABLED, [&fds]() {
288 // Check that nothing was written. Could return 0 or -1.
289 char buff;
290 ASSERT_NE(1, read(fds[0], &buff, 1)) << "Dumped something when device logging is disabled";
291 });
292
293 native_handle_close(handle);
294 native_handle_delete(handle);
295});
296
297// Double-enable is perfectly valid, but the second call shouldn't do anything.
298TEST_P(DumpstateHidl1_1Test, TestRepeatedEnable) {
299 EnableDeviceLogging();
300 EnableDeviceLogging();
301}
302
303// Double-disable is perfectly valid, but the second call shouldn't do anything.
304TEST_P(DumpstateHidl1_1Test, TestRepeatedDisable) {
305 DisableDeviceLogging();
306 DisableDeviceLogging();
307}
308
309// Toggling in short order is perfectly valid.
310TEST_P(DumpstateHidl1_1Test, TestRepeatedToggle) {
311 EnableDeviceLogging();
312 DisableDeviceLogging();
313 EnableDeviceLogging();
314 DisableDeviceLogging();
Hunter Knepshield1b92d262020-01-15 17:48:01 -0800315}
316
317INSTANTIATE_TEST_SUITE_P(
318 PerInstance, DumpstateHidl1_1Test,
319 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)),
320 android::hardware::PrintInstanceNameToString);
Hunter Knepshield7b20bd72020-01-22 18:25:14 -0800321
322} // namespace