• 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 <android/hardware/dumpstate/1.0/IDumpstateDevice.h>
20 #include <cutils/native_handle.h>
21 #include <log/log.h>
22 
23 #include <VtsHalHidlTargetTestBase.h>
24 
25 using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
26 using ::android::hardware::Return;
27 using ::android::sp;
28 
29 class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase {
30    public:
SetUp()31     virtual void SetUp() override {
32         dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>();
33         ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance";
34     }
35 
36     sp<IDumpstateDevice> dumpstate;
37 };
38 
39 // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
TEST_F(DumpstateHidlTest,TestNullHandle)40 TEST_F(DumpstateHidlTest, TestNullHandle) {
41     Return<void> status = dumpstate->dumpstateBoard(nullptr);
42 
43     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
44 }
45 
46 // Negative test: make sure dumpstateBoard() ignores a handle with no FD.
TEST_F(DumpstateHidlTest,TestHandleWithNoFd)47 TEST_F(DumpstateHidlTest, TestHandleWithNoFd) {
48     native_handle_t* handle = native_handle_create(0, 0);
49     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
50 
51     Return<void> status = dumpstate->dumpstateBoard(handle);
52 
53     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
54 
55     native_handle_close(handle);
56     native_handle_delete(handle);
57 }
58 
59 // Positive test: make sure dumpstateBoard() writes something to the FD.
TEST_F(DumpstateHidlTest,TestOk)60 TEST_F(DumpstateHidlTest, TestOk) {
61     FILE* file = tmpfile();
62 
63     ASSERT_NE(nullptr, file) << "Could not create temp file: " << strerror(errno);
64 
65     native_handle_t* handle = native_handle_create(1, 0);
66     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
67     handle->data[0] = fileno(file);
68 
69     Return<void> status = dumpstate->dumpstateBoard(handle);
70     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
71 
72     // Check that at least one byte was written
73     rewind(file);  // can not fail
74     char buff;
75     int read = fread(&buff, sizeof(buff), 1, file);
76     ASSERT_EQ(1, read) << "dumped nothing";
77 
78     EXPECT_EQ(0, fclose(file)) << errno;
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_F(DumpstateHidlTest,TestHandleWithTwoFds)85 TEST_F(DumpstateHidlTest, TestHandleWithTwoFds) {
86     FILE* file1 = tmpfile();
87     FILE* file2 = tmpfile();
88 
89     ASSERT_NE(nullptr, file1) << "Could not create temp file #1: " << strerror(errno);
90     ASSERT_NE(nullptr, file2) << "Could not create temp file #2: " << strerror(errno);
91 
92     native_handle_t* handle = native_handle_create(2, 0);
93     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
94     handle->data[0] = fileno(file1);
95     handle->data[1] = fileno(file2);
96 
97     Return<void> status = dumpstate->dumpstateBoard(handle);
98     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
99 
100     EXPECT_EQ(0, fclose(file1)) << errno;
101     EXPECT_EQ(0, fclose(file2)) << errno;
102 
103     native_handle_close(handle);
104     native_handle_delete(handle);
105 }
106 
main(int argc,char ** argv)107 int main(int argc, char** argv) {
108     ::testing::InitGoogleTest(&argc, argv);
109     int status = RUN_ALL_TESTS();
110     ALOGI("Test result = %d", status);
111     return status;
112 }
113