• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <log/log.h>
25 
26 #include <VtsHalHidlTargetTestBase.h>
27 #include <VtsHalHidlTargetTestEnvBase.h>
28 
29 using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
30 using ::android::hardware::Return;
31 using ::android::sp;
32 
33 // Test environment for Dumpstate HIDL HAL.
34 class DumpstateHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
35    public:
36     // get the test environment singleton
Instance()37     static DumpstateHidlEnvironment* Instance() {
38         static DumpstateHidlEnvironment* instance = new DumpstateHidlEnvironment;
39         return instance;
40     }
41 
registerTestServices()42     virtual void registerTestServices() override { registerTestService<IDumpstateDevice>(); }
43 };
44 
45 class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase {
46    public:
SetUp()47     virtual void SetUp() override {
48         dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>(
49             DumpstateHidlEnvironment::Instance()->getServiceName<IDumpstateDevice>());
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.
TEST_F(DumpstateHidlTest,TestNullHandle)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.
TEST_F(DumpstateHidlTest,TestHandleWithNoFd)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.
TEST_F(DumpstateHidlTest,TestOk)77 TEST_F(DumpstateHidlTest, TestOk) {
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;
81 
82     native_handle_t* handle = native_handle_create(1, 0);
83     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
84     handle->data[0] = fds[1];
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
90     char buff;
91     ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing";
92 
93     native_handle_close(handle);
94 }
95 
96 // Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
TEST_F(DumpstateHidlTest,TestHandleWithTwoFds)97 TEST_F(DumpstateHidlTest, TestHandleWithTwoFds) {
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;
102 
103     native_handle_t* handle = native_handle_create(2, 0);
104     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
105     handle->data[0] = fds1[1];
106     handle->data[1] = fds2[1];
107 
108     Return<void> status = dumpstate->dumpstateBoard(handle);
109     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
110 
111     native_handle_close(handle);
112 }
113 
main(int argc,char ** argv)114 int main(int argc, char** argv) {
115     ::testing::AddGlobalTestEnvironment(DumpstateHidlEnvironment::Instance());
116     ::testing::InitGoogleTest(&argc, argv);
117     DumpstateHidlEnvironment::Instance()->init(&argc, argv);
118     int status = RUN_ALL_TESTS();
119     ALOGI("Test result = %d", status);
120     return status;
121 }
122