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 | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include <android/hardware/dumpstate/1.1/IDumpstateDevice.h> |
| 25 | #include <android/hardware/dumpstate/1.1/types.h> |
| 26 | #include <cutils/native_handle.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | #include <hidl/GtestPrinter.h> |
| 29 | #include <hidl/ServiceManagement.h> |
| 30 | #include <log/log.h> |
| 31 | |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 32 | namespace { |
| 33 | |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 34 | using ::android::sp; |
| 35 | using ::android::hardware::Return; |
| 36 | using ::android::hardware::dumpstate::V1_1::DumpstateMode; |
| 37 | using ::android::hardware::dumpstate::V1_1::IDumpstateDevice; |
| 38 | |
| 39 | class DumpstateHidl1_1Test : public ::testing::TestWithParam<std::string> { |
| 40 | public: |
| 41 | virtual void SetUp() override { |
| 42 | dumpstate = IDumpstateDevice::getService(GetParam()); |
| 43 | ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance"; |
| 44 | } |
| 45 | |
| 46 | sp<IDumpstateDevice> dumpstate; |
| 47 | }; |
| 48 | |
| 49 | #define TEST_FOR_DUMPSTATE_MODE(name, body, mode) \ |
| 50 | TEST_P(DumpstateHidl1_1Test, name##_##mode) { body(DumpstateMode::mode); } |
| 51 | |
| 52 | #define TEST_FOR_ALL_DUMPSTATE_MODES(name, body) \ |
| 53 | TEST_FOR_DUMPSTATE_MODE(name, body, FULL); \ |
| 54 | TEST_FOR_DUMPSTATE_MODE(name, body, INTERACTIVE); \ |
| 55 | TEST_FOR_DUMPSTATE_MODE(name, body, REMOTE); \ |
| 56 | TEST_FOR_DUMPSTATE_MODE(name, body, WEAR); \ |
| 57 | TEST_FOR_DUMPSTATE_MODE(name, body, CONNECTIVITY); \ |
| 58 | TEST_FOR_DUMPSTATE_MODE(name, body, WIFI); \ |
| 59 | TEST_FOR_DUMPSTATE_MODE(name, body, DEFAULT); |
| 60 | |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 61 | constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds |
Hunter Knepshield | 1b92d26 | 2020-01-15 17:48:01 -0800 | [diff] [blame] | 62 | |
| 63 | // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer. |
| 64 | TEST_FOR_ALL_DUMPSTATE_MODES(TestNullHandle, [this](DumpstateMode mode) { |
| 65 | Return<void> status = dumpstate->dumpstateBoard_1_1(nullptr, mode, kDefaultTimeoutMillis); |
| 66 | |
| 67 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 68 | }); |
| 69 | |
| 70 | // Negative test: make sure dumpstateBoard() ignores a handle with no FD. |
| 71 | TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithNoFd, [this](DumpstateMode mode) { |
| 72 | native_handle_t* handle = native_handle_create(0, 0); |
| 73 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 74 | |
| 75 | Return<void> status = dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis); |
| 76 | |
| 77 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 78 | |
| 79 | native_handle_close(handle); |
| 80 | native_handle_delete(handle); |
| 81 | }); |
| 82 | |
| 83 | // Positive test: make sure dumpstateBoard() writes something to the FD. |
| 84 | TEST_FOR_ALL_DUMPSTATE_MODES(TestOk, [this](DumpstateMode mode) { |
| 85 | // Index 0 corresponds to the read end of the pipe; 1 to the write end. |
| 86 | int fds[2]; |
| 87 | ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; |
| 88 | |
| 89 | native_handle_t* handle = native_handle_create(1, 0); |
| 90 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 91 | handle->data[0] = fds[1]; |
| 92 | |
| 93 | Return<void> status = dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis); |
| 94 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 95 | |
| 96 | // Check that at least one byte was written |
| 97 | char buff; |
| 98 | ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing"; |
| 99 | |
| 100 | native_handle_close(handle); |
| 101 | native_handle_delete(handle); |
| 102 | }); |
| 103 | |
| 104 | // Positive test: make sure dumpstateBoard() doesn't crash with two FDs. |
| 105 | TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithTwoFds, [this](DumpstateMode mode) { |
| 106 | int fds1[2]; |
| 107 | int fds2[2]; |
| 108 | ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno; |
| 109 | ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno; |
| 110 | |
| 111 | native_handle_t* handle = native_handle_create(2, 0); |
| 112 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 113 | handle->data[0] = fds1[1]; |
| 114 | handle->data[1] = fds2[1]; |
| 115 | |
| 116 | Return<void> status = dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis); |
| 117 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 118 | |
| 119 | native_handle_close(handle); |
| 120 | native_handle_delete(handle); |
| 121 | }); |
| 122 | |
| 123 | // Make sure dumpstateBoard_1_1 actually validates its arguments. |
| 124 | TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Negative) { |
| 125 | int fds[2]; |
| 126 | ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; |
| 127 | |
| 128 | native_handle_t* handle = native_handle_create(1, 0); |
| 129 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 130 | handle->data[0] = fds[1]; |
| 131 | |
| 132 | Return<void> status = dumpstate->dumpstateBoard_1_1(handle, static_cast<DumpstateMode>(-100), |
| 133 | kDefaultTimeoutMillis); |
| 134 | ASSERT_FALSE(status.isOk()) << "Status should not be ok with invalid mode param: " |
| 135 | << status.description(); |
| 136 | |
| 137 | native_handle_close(handle); |
| 138 | native_handle_delete(handle); |
| 139 | } |
| 140 | |
| 141 | TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Undefined) { |
| 142 | int fds[2]; |
| 143 | ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; |
| 144 | |
| 145 | native_handle_t* handle = native_handle_create(1, 0); |
| 146 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 147 | handle->data[0] = fds[1]; |
| 148 | |
| 149 | Return<void> status = dumpstate->dumpstateBoard_1_1(handle, static_cast<DumpstateMode>(9001), |
| 150 | kDefaultTimeoutMillis); |
| 151 | ASSERT_FALSE(status.isOk()) << "Status should not be ok with invalid mode param: " |
| 152 | << status.description(); |
| 153 | |
| 154 | native_handle_close(handle); |
| 155 | native_handle_delete(handle); |
| 156 | } |
| 157 | |
| 158 | // Make sure toggling device logging doesn't crash. |
| 159 | TEST_P(DumpstateHidl1_1Test, TestEnableDeviceLogging) { |
| 160 | Return<bool> status = dumpstate->setDeviceLoggingEnabled(true); |
| 161 | |
| 162 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 163 | } |
| 164 | |
| 165 | TEST_P(DumpstateHidl1_1Test, TestDisableDeviceLogging) { |
| 166 | Return<bool> status = dumpstate->setDeviceLoggingEnabled(false); |
| 167 | |
| 168 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 169 | } |
| 170 | |
| 171 | INSTANTIATE_TEST_SUITE_P( |
| 172 | PerInstance, DumpstateHidl1_1Test, |
| 173 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)), |
| 174 | android::hardware::PrintInstanceNameToString); |
Hunter Knepshield | 7b20bd7 | 2020-01-22 18:25:14 -0800 | [diff] [blame] | 175 | |
| 176 | } // namespace |