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 | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 23 | #include <tuple> |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 24 | #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 Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 31 | #include <hidl/HidlSupport.h> |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 32 | #include <hidl/ServiceManagement.h> |
| 33 | #include <log/log.h> |
| 34 | |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 35 | namespace { |
| 36 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 37 | using ::android::sp; |
| 38 | using ::android::hardware::Return; |
| 39 | using ::android::hardware::dumpstate::V1_1::DumpstateMode; |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 40 | using ::android::hardware::dumpstate::V1_1::DumpstateStatus; |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 41 | using ::android::hardware::dumpstate::V1_1::IDumpstateDevice; |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 42 | using ::android::hardware::dumpstate::V1_1::toString; |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 43 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 44 | // Base class common to all dumpstate HAL v1.1 tests. |
| 45 | template <typename T> |
| 46 | class DumpstateHidl1_1TestBase : public ::testing::TestWithParam<T> { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 47 | protected: |
| 48 | virtual void SetUp() override { GetService(); } |
| 49 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 50 | virtual std::string GetInstanceName() = 0; |
| 51 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 52 | void GetService() { |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 53 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 58 | void ToggleVerboseLogging(bool enable) { |
| 59 | Return<void> status = dumpstate->setVerboseLoggingEnabled(enable); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 60 | 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 Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 68 | Return<bool> logging_enabled = dumpstate->getVerboseLoggingEnabled(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 69 | ASSERT_TRUE(logging_enabled.isOk()) |
| 70 | << "Status should be ok: " << logging_enabled.description(); |
| 71 | ASSERT_EQ(logging_enabled, enable) |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 72 | << "Verbose logging should now be " << (enable ? "enabled" : "disabled"); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 73 | |
| 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 Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 81 | void EnableVerboseLogging() { ToggleVerboseLogging(true); } |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 82 | |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 83 | void DisableVerboseLogging() { ToggleVerboseLogging(false); } |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 84 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 85 | sp<IDumpstateDevice> dumpstate; |
| 86 | }; |
| 87 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 88 | // Tests that don't need to iterate every single DumpstateMode value for dumpstateBoard_1_1. |
| 89 | class DumpstateHidl1_1GeneralTest : public DumpstateHidl1_1TestBase<std::string> { |
| 90 | protected: |
| 91 | virtual std::string GetInstanceName() override { return GetParam(); } |
| 92 | }; |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 93 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 94 | // Tests that iterate every single DumpstateMode value for dumpstateBoard_1_1. |
| 95 | class 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 127 | |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 128 | constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 129 | |
| 130 | // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 131 | TEST_P(DumpstateHidl1_1PerModeTest, TestNullHandle) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 132 | EnableVerboseLogging(); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 133 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 134 | Return<DumpstateStatus> status = |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 135 | dumpstate->dumpstateBoard_1_1(nullptr, GetMode(), kDefaultTimeoutMillis); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 136 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 137 | AssertStatusForMode(status, DumpstateStatus::ILLEGAL_ARGUMENT); |
| 138 | } |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 139 | |
| 140 | // Negative test: make sure dumpstateBoard() ignores a handle with no FD. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 141 | TEST_P(DumpstateHidl1_1PerModeTest, TestHandleWithNoFd) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 142 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 143 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 144 | native_handle_t* handle = native_handle_create(0, 0); |
| 145 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 146 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 147 | Return<DumpstateStatus> status = |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 148 | dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 149 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 150 | AssertStatusForMode(status, DumpstateStatus::ILLEGAL_ARGUMENT); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 151 | |
| 152 | native_handle_close(handle); |
| 153 | native_handle_delete(handle); |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 154 | } |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 155 | |
| 156 | // Positive test: make sure dumpstateBoard() writes something to the FD. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 157 | TEST_P(DumpstateHidl1_1PerModeTest, TestOk) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 158 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 159 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 160 | // 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 168 | Return<DumpstateStatus> status = |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 169 | dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 170 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 171 | AssertStatusForMode(status, DumpstateStatus::OK, [&fds]() { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 172 | // Check that at least one byte was written. |
| 173 | char buff; |
| 174 | ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing"; |
| 175 | }); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 176 | |
| 177 | native_handle_close(handle); |
| 178 | native_handle_delete(handle); |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 179 | } |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 180 | |
| 181 | // Positive test: make sure dumpstateBoard() doesn't crash with two FDs. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 182 | TEST_P(DumpstateHidl1_1PerModeTest, TestHandleWithTwoFds) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 183 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 184 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 185 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 195 | Return<DumpstateStatus> status = |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 196 | dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 197 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 198 | AssertStatusForMode(status, DumpstateStatus::OK, [&fds1, &fds2]() { |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 199 | // 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 206 | |
| 207 | native_handle_close(handle); |
| 208 | native_handle_delete(handle); |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 209 | } |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 210 | |
| 211 | // Make sure dumpstateBoard_1_1 actually validates its arguments. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 212 | TEST_P(DumpstateHidl1_1GeneralTest, TestInvalidModeArgument_Negative) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 213 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 214 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 215 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 222 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 229 | |
| 230 | native_handle_close(handle); |
| 231 | native_handle_delete(handle); |
| 232 | } |
| 233 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 234 | TEST_P(DumpstateHidl1_1GeneralTest, TestInvalidModeArgument_Undefined) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 235 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 236 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 237 | 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 Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 244 | 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 251 | |
| 252 | native_handle_close(handle); |
| 253 | native_handle_delete(handle); |
| 254 | } |
| 255 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 256 | // Positive test: make sure dumpstateBoard() from 1.0 doesn't fail. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 257 | TEST_P(DumpstateHidl1_1GeneralTest, Test1_0MethodOk) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 258 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 259 | |
| 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 268 | |
| 269 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 270 | |
| 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 Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 279 | // 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 Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 281 | TEST_P(DumpstateHidl1_1PerModeTest, TestDeviceLoggingDisabled) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 282 | DisableVerboseLogging(); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 283 | |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 284 | // 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 Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 293 | dumpstate->dumpstateBoard_1_1(handle, GetMode(), kDefaultTimeoutMillis); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 294 | |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 295 | // 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 Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 298 | AssertStatusForMode(status, DumpstateStatus::OK); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 299 | |
| 300 | native_handle_close(handle); |
| 301 | native_handle_delete(handle); |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 302 | } |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 303 | |
| 304 | // Double-enable is perfectly valid, but the second call shouldn't do anything. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 305 | TEST_P(DumpstateHidl1_1GeneralTest, TestRepeatedEnable) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 306 | EnableVerboseLogging(); |
| 307 | EnableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Double-disable is perfectly valid, but the second call shouldn't do anything. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 311 | TEST_P(DumpstateHidl1_1GeneralTest, TestRepeatedDisable) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 312 | DisableVerboseLogging(); |
| 313 | DisableVerboseLogging(); |
Hunter Knepshield | 256f77a | 2020-02-03 16:25:57 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Toggling in short order is perfectly valid. |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 317 | TEST_P(DumpstateHidl1_1GeneralTest, TestRepeatedToggle) { |
Hunter Knepshield | 84dbf58 | 2020-02-12 18:55:28 -0800 | [diff] [blame] | 318 | EnableVerboseLogging(); |
| 319 | DisableVerboseLogging(); |
| 320 | EnableVerboseLogging(); |
| 321 | DisableVerboseLogging(); |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | INSTANTIATE_TEST_SUITE_P( |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 325 | PerInstance, DumpstateHidl1_1GeneralTest, |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 326 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)), |
| 327 | android::hardware::PrintInstanceNameToString); |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 328 | |
Hunter Knepshield | ac0680b | 2020-02-20 13:04:17 -0800 | [diff] [blame] | 329 | // Includes the mode's name as part of the description string. |
| 330 | static 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 | |
| 337 | INSTANTIATE_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 Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 344 | } // namespace |