blob: 57ebf2a88885c5a6ece5d6d11c6c5a758d278bbf [file] [log] [blame]
Felipe Leme4b89d222017-05-26 14:55:14 -07001/*
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 Vo8e1e0c42017-10-12 16:35:56 -070019#include <fcntl.h>
20#include <unistd.h>
21
Felipe Leme4b89d222017-05-26 14:55:14 -070022#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 Zhangbdca6e22018-02-08 20:51:09 -080027#include <VtsHalHidlTargetTestEnvBase.h>
Felipe Leme4b89d222017-05-26 14:55:14 -070028
29using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
30using ::android::hardware::Return;
31using ::android::sp;
32
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -080033// Test environment for Dumpstate HIDL HAL.
34class 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 Leme4b89d222017-05-26 14:55:14 -070045class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase {
46 public:
47 virtual void SetUp() override {
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -080048 dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>(
49 DumpstateHidlEnvironment::Instance()->getServiceName<IDumpstateDevice>());
Felipe Leme4b89d222017-05-26 14:55:14 -070050 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.
57TEST_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.
64TEST_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.
77TEST_F(DumpstateHidlTest, TestOk) {
Tri Vo8e1e0c42017-10-12 16:35:56 -070078 // 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 Leme4b89d222017-05-26 14:55:14 -070081
82 native_handle_t* handle = native_handle_create(1, 0);
83 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
Tri Vo8e1e0c42017-10-12 16:35:56 -070084 handle->data[0] = fds[1];
Felipe Leme4b89d222017-05-26 14:55:14 -070085
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 Leme4b89d222017-05-26 14:55:14 -070090 char buff;
Tri Vo8e1e0c42017-10-12 16:35:56 -070091 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing";
Felipe Leme4b89d222017-05-26 14:55:14 -070092
93 native_handle_close(handle);
Felipe Leme4b89d222017-05-26 14:55:14 -070094}
95
Jie Songcc4ddff2017-06-20 09:58:13 -070096// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
97TEST_F(DumpstateHidlTest, TestHandleWithTwoFds) {
Tri Vo8e1e0c42017-10-12 16:35:56 -070098 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 Songcc4ddff2017-06-20 09:58:13 -0700102
103 native_handle_t* handle = native_handle_create(2, 0);
104 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
Tri Vo8e1e0c42017-10-12 16:35:56 -0700105 handle->data[0] = fds1[1];
106 handle->data[1] = fds2[1];
Jie Songcc4ddff2017-06-20 09:58:13 -0700107
108 Return<void> status = dumpstate->dumpstateBoard(handle);
109 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
110
Jie Songcc4ddff2017-06-20 09:58:13 -0700111 native_handle_close(handle);
Jie Songcc4ddff2017-06-20 09:58:13 -0700112}
113
Felipe Leme4b89d222017-05-26 14:55:14 -0700114int main(int argc, char** argv) {
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -0800115 ::testing::AddGlobalTestEnvironment(DumpstateHidlEnvironment::Instance());
Felipe Leme4b89d222017-05-26 14:55:14 -0700116 ::testing::InitGoogleTest(&argc, argv);
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -0800117 DumpstateHidlEnvironment::Instance()->init(&argc, argv);
Felipe Leme4b89d222017-05-26 14:55:14 -0700118 int status = RUN_ALL_TESTS();
119 ALOGI("Test result = %d", status);
120 return status;
121}