Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 1 | /* |
| 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 Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 21 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 22 | #include <functional> |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 23 | #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 Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 33 | namespace { |
| 34 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 35 | using ::android::sp; |
| 36 | using ::android::hardware::Return; |
| 37 | using ::android::hardware::dumpstate::V1_1::DumpstateMode; |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 38 | using ::android::hardware::dumpstate::V1_1::DumpstateStatus; |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 39 | using ::android::hardware::dumpstate::V1_1::IDumpstateDevice; |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 40 | using ::android::hardware::dumpstate::V1_1::toString; |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 41 | |
| 42 | class DumpstateHidl1_1Test : public ::testing::TestWithParam<std::string> { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 43 | protected: |
| 44 | virtual void SetUp() override { GetService(); } |
| 45 | |
| 46 | void GetService() { |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 47 | dumpstate = IDumpstateDevice::getService(GetParam()); |
| 48 | ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance"; |
| 49 | } |
| 50 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 51 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 78 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 84 | // 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 88 | #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 Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 97 | constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 98 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 99 | // Will only execute additional_assertions when status == expected. |
| 100 | void 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 123 | // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer. |
| 124 | TEST_FOR_ALL_DUMPSTATE_MODES(TestNullHandle, [this](DumpstateMode mode) { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 125 | EnableDeviceLogging(); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 126 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 127 | Return<DumpstateStatus> status = |
| 128 | dumpstate->dumpstateBoard_1_1(nullptr, mode, kDefaultTimeoutMillis); |
| 129 | |
| 130 | AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 131 | }); |
| 132 | |
| 133 | // Negative test: make sure dumpstateBoard() ignores a handle with no FD. |
| 134 | TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithNoFd, [this](DumpstateMode mode) { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 135 | EnableDeviceLogging(); |
| 136 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 137 | native_handle_t* handle = native_handle_create(0, 0); |
| 138 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 139 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 140 | Return<DumpstateStatus> status = |
| 141 | dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 142 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 143 | AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 144 | |
| 145 | native_handle_close(handle); |
| 146 | native_handle_delete(handle); |
| 147 | }); |
| 148 | |
| 149 | // Positive test: make sure dumpstateBoard() writes something to the FD. |
| 150 | TEST_FOR_ALL_DUMPSTATE_MODES(TestOk, [this](DumpstateMode mode) { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 151 | EnableDeviceLogging(); |
| 152 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 153 | // 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 161 | Return<DumpstateStatus> status = |
| 162 | dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 163 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 164 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 169 | |
| 170 | native_handle_close(handle); |
| 171 | native_handle_delete(handle); |
| 172 | }); |
| 173 | |
| 174 | // Positive test: make sure dumpstateBoard() doesn't crash with two FDs. |
| 175 | TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithTwoFds, [this](DumpstateMode mode) { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 176 | EnableDeviceLogging(); |
| 177 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 178 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 188 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 199 | |
| 200 | native_handle_close(handle); |
| 201 | native_handle_delete(handle); |
| 202 | }); |
| 203 | |
| 204 | // Make sure dumpstateBoard_1_1 actually validates its arguments. |
| 205 | TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Negative) { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 206 | EnableDeviceLogging(); |
| 207 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 208 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 215 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 222 | |
| 223 | native_handle_close(handle); |
| 224 | native_handle_delete(handle); |
| 225 | } |
| 226 | |
| 227 | TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Undefined) { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 228 | EnableDeviceLogging(); |
| 229 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 230 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 237 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 244 | |
| 245 | native_handle_close(handle); |
| 246 | native_handle_delete(handle); |
| 247 | } |
| 248 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 249 | // Positive test: make sure dumpstateBoard() from 1.0 doesn't fail. |
| 250 | TEST_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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 261 | |
| 262 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 263 | |
| 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 272 | // Make sure disabling device logging behaves correctly. |
| 273 | TEST_FOR_ALL_DUMPSTATE_MODES(TestDeviceLoggingDisabled, [this](DumpstateMode mode) { |
| 274 | DisableDeviceLogging(); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 275 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame^] | 276 | // 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. |
| 298 | TEST_P(DumpstateHidl1_1Test, TestRepeatedEnable) { |
| 299 | EnableDeviceLogging(); |
| 300 | EnableDeviceLogging(); |
| 301 | } |
| 302 | |
| 303 | // Double-disable is perfectly valid, but the second call shouldn't do anything. |
| 304 | TEST_P(DumpstateHidl1_1Test, TestRepeatedDisable) { |
| 305 | DisableDeviceLogging(); |
| 306 | DisableDeviceLogging(); |
| 307 | } |
| 308 | |
| 309 | // Toggling in short order is perfectly valid. |
| 310 | TEST_P(DumpstateHidl1_1Test, TestRepeatedToggle) { |
| 311 | EnableDeviceLogging(); |
| 312 | DisableDeviceLogging(); |
| 313 | EnableDeviceLogging(); |
| 314 | DisableDeviceLogging(); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | INSTANTIATE_TEST_SUITE_P( |
| 318 | PerInstance, DumpstateHidl1_1Test, |
| 319 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)), |
| 320 | android::hardware::PrintInstanceNameToString); |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 321 | |
| 322 | } // namespace |