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> |
| 24 | #include <log/log.h> |
| 25 | |
| 26 | #include <VtsHalHidlTargetTestBase.h> |
Zhuoyao Zhang | bdca6e2 | 2018-02-08 20:51:09 -0800 | [diff] [blame] | 27 | #include <VtsHalHidlTargetTestEnvBase.h> |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 28 | |
| 29 | using ::android::hardware::dumpstate::V1_0::IDumpstateDevice; |
| 30 | using ::android::hardware::Return; |
| 31 | using ::android::sp; |
| 32 | |
Zhuoyao Zhang | bdca6e2 | 2018-02-08 20:51:09 -0800 | [diff] [blame] | 33 | // Test environment for Dumpstate HIDL HAL. |
| 34 | class DumpstateHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { |
| 35 | public: |
| 36 | // get the test environment singleton |
| 37 | static DumpstateHidlEnvironment* Instance() { |
| 38 | static DumpstateHidlEnvironment* instance = new DumpstateHidlEnvironment; |
| 39 | return instance; |
| 40 | } |
| 41 | |
| 42 | virtual void registerTestServices() override { registerTestService<IDumpstateDevice>(); } |
| 43 | }; |
| 44 | |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 45 | class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 46 | public: |
| 47 | virtual void SetUp() override { |
Zhuoyao Zhang | bdca6e2 | 2018-02-08 20:51:09 -0800 | [diff] [blame] | 48 | dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>( |
| 49 | DumpstateHidlEnvironment::Instance()->getServiceName<IDumpstateDevice>()); |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 50 | ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance"; |
| 51 | } |
| 52 | |
| 53 | sp<IDumpstateDevice> dumpstate; |
| 54 | }; |
| 55 | |
| 56 | // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer. |
| 57 | TEST_F(DumpstateHidlTest, TestNullHandle) { |
| 58 | Return<void> status = dumpstate->dumpstateBoard(nullptr); |
| 59 | |
| 60 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 61 | } |
| 62 | |
| 63 | // Negative test: make sure dumpstateBoard() ignores a handle with no FD. |
| 64 | TEST_F(DumpstateHidlTest, TestHandleWithNoFd) { |
| 65 | native_handle_t* handle = native_handle_create(0, 0); |
| 66 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 67 | |
| 68 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 69 | |
| 70 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 71 | |
| 72 | native_handle_close(handle); |
| 73 | native_handle_delete(handle); |
| 74 | } |
| 75 | |
| 76 | // Positive test: make sure dumpstateBoard() writes something to the FD. |
| 77 | TEST_F(DumpstateHidlTest, TestOk) { |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 78 | // Index 0 corresponds to the read end of the pipe; 1 to the write end. |
| 79 | int fds[2]; |
| 80 | ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 81 | |
| 82 | native_handle_t* handle = native_handle_create(1, 0); |
| 83 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 84 | handle->data[0] = fds[1]; |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 85 | |
| 86 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 87 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 88 | |
| 89 | // Check that at least one byte was written |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 90 | char buff; |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 91 | ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing"; |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 92 | |
| 93 | native_handle_close(handle); |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 96 | // Positive test: make sure dumpstateBoard() doesn't crash with two FDs. |
| 97 | TEST_F(DumpstateHidlTest, TestHandleWithTwoFds) { |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 98 | int fds1[2]; |
| 99 | int fds2[2]; |
| 100 | ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno; |
| 101 | ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno; |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 102 | |
| 103 | native_handle_t* handle = native_handle_create(2, 0); |
| 104 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
Tri Vo | 8e1e0c4 | 2017-10-12 16:35:56 -0700 | [diff] [blame] | 105 | handle->data[0] = fds1[1]; |
| 106 | handle->data[1] = fds2[1]; |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 107 | |
| 108 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 109 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 110 | |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 111 | native_handle_close(handle); |
Jie Song | cc4ddff | 2017-06-20 09:58:13 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 114 | int main(int argc, char** argv) { |
Zhuoyao Zhang | bdca6e2 | 2018-02-08 20:51:09 -0800 | [diff] [blame] | 115 | ::testing::AddGlobalTestEnvironment(DumpstateHidlEnvironment::Instance()); |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 116 | ::testing::InitGoogleTest(&argc, argv); |
Zhuoyao Zhang | bdca6e2 | 2018-02-08 20:51:09 -0800 | [diff] [blame] | 117 | DumpstateHidlEnvironment::Instance()->init(&argc, argv); |
Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 118 | int status = RUN_ALL_TESTS(); |
| 119 | ALOGI("Test result = %d", status); |
| 120 | return status; |
| 121 | } |