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 
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #include <android/hardware/dumpstate/1.0/IDumpstateDevice.h>
23 #include <cutils/native_handle.h>
24 #include <gtest/gtest.h>
25 #include <hidl/GtestPrinter.h>
26 #include <hidl/ServiceManagement.h>
27 #include <log/log.h>
28 
29 using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
30 using ::android::hardware::Return;
31 using ::android::sp;
32 
33 class DumpstateHidlTest : public ::testing::TestWithParam<std::string> {
34   public:
SetUp()35     virtual void SetUp() override {
36         dumpstate = IDumpstateDevice::getService(GetParam());
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.
TEST_P(DumpstateHidlTest,TestNullHandle)44 TEST_P(DumpstateHidlTest, TestNullHandle) {
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.
TEST_P(DumpstateHidlTest,TestHandleWithNoFd)51 TEST_P(DumpstateHidlTest, TestHandleWithNoFd) {
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.
TEST_P(DumpstateHidlTest,TestOk)64 TEST_P(DumpstateHidlTest, TestOk) {
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;
68 
69     native_handle_t* handle = native_handle_create(1, 0);
70     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
71     handle->data[0] = fds[1];
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
77     char buff;
78     ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing";
79 
80     native_handle_close(handle);
81     native_handle_delete(handle);
82 }
83 
84 // Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
TEST_P(DumpstateHidlTest,TestHandleWithTwoFds)85 TEST_P(DumpstateHidlTest, TestHandleWithTwoFds) {
86     int fds1[2];
87     int fds2[2];
88     ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
89     ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
90 
91     native_handle_t* handle = native_handle_create(2, 0);
92     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
93     handle->data[0] = fds1[1];
94     handle->data[1] = fds2[1];
95 
96     Return<void> status = dumpstate->dumpstateBoard(handle);
97     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
98 
99     native_handle_close(handle);
100     native_handle_delete(handle);
101 }
102 
103 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DumpstateHidlTest);
104 INSTANTIATE_TEST_SUITE_P(
105         PerInstance, DumpstateHidlTest,
106         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)),
107         android::hardware::PrintInstanceNameToString);
108