Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_hidl_hal_test" |
| 18 | |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 22 | #include <android/hardware/dumpstate/1.0/IDumpstateDevice.h> |
| 23 | #include <cutils/native_handle.h> |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | #include <hidl/GtestPrinter.h> |
| 26 | #include <hidl/ServiceManagement.h> |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 27 | #include <log/log.h> |
| 28 | |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 29 | using ::android::hardware::dumpstate::V1_0::IDumpstateDevice; |
| 30 | using ::android::hardware::Return; |
| 31 | using ::android::sp; |
| 32 | |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 33 | class DumpstateHidlTest : public ::testing::TestWithParam<std::string> { |
| 34 | public: |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 35 | virtual void SetUp() override { |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 36 | dumpstate = IDumpstateDevice::getService(GetParam()); |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 37 | ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance"; |
| 38 | } |
| 39 | |
| 40 | sp<IDumpstateDevice> dumpstate; |
| 41 | }; |
| 42 | |
| 43 | // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer. |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 44 | TEST_P(DumpstateHidlTest, TestNullHandle) { |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 45 | Return<void> status = dumpstate->dumpstateBoard(nullptr); |
| 46 | |
| 47 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 48 | } |
| 49 | |
| 50 | // Negative test: make sure dumpstateBoard() ignores a handle with no FD. |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 51 | TEST_P(DumpstateHidlTest, TestHandleWithNoFd) { |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 52 | native_handle_t* handle = native_handle_create(0, 0); |
| 53 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 54 | |
| 55 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 56 | |
| 57 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 58 | |
| 59 | native_handle_close(handle); |
| 60 | native_handle_delete(handle); |
| 61 | } |
| 62 | |
| 63 | // Positive test: make sure dumpstateBoard() writes something to the FD. |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 64 | TEST_P(DumpstateHidlTest, TestOk) { |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 65 | // Index 0 corresponds to the read end of the pipe; 1 to the write end. |
| 66 | int fds[2]; |
| 67 | ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 68 | |
| 69 | native_handle_t* handle = native_handle_create(1, 0); |
| 70 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 71 | handle->data[0] = fds[1]; |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 72 | |
| 73 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 74 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 75 | |
| 76 | // Check that at least one byte was written |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 77 | char buff; |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 78 | ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing"; |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 79 | |
| 80 | native_handle_close(handle); |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 83 | // Positive test: make sure dumpstateBoard() doesn't crash with two FDs. |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 84 | TEST_P(DumpstateHidlTest, TestHandleWithTwoFds) { |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 85 | int fds1[2]; |
| 86 | int fds2[2]; |
| 87 | ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno; |
| 88 | ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno; |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 89 | |
| 90 | native_handle_t* handle = native_handle_create(2, 0); |
| 91 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 92 | handle->data[0] = fds1[1]; |
| 93 | handle->data[1] = fds2[1]; |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 94 | |
| 95 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 96 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 97 | |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 98 | native_handle_close(handle); |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 99 | } |
| 100 | |
nelsonli | ba31c17 | 2019-10-18 16:00:01 +0800 | [diff] [blame] | 101 | INSTANTIATE_TEST_SUITE_P( |
| 102 | PerInstance, DumpstateHidlTest, |
| 103 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)), |
| 104 | android::hardware::PrintInstanceNameToString); |