blob: 9e866e7bde576dc17957370d2cc921d3183a663e [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>
27
28using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
29using ::android::hardware::Return;
30using ::android::sp;
31
32class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase {
33 public:
34 virtual void SetUp() override {
35 dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>();
36 ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance";
37 }
38
39 sp<IDumpstateDevice> dumpstate;
40};
41
42// Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
43TEST_F(DumpstateHidlTest, TestNullHandle) {
44 Return<void> status = dumpstate->dumpstateBoard(nullptr);
45
46 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
47}
48
49// Negative test: make sure dumpstateBoard() ignores a handle with no FD.
50TEST_F(DumpstateHidlTest, TestHandleWithNoFd) {
51 native_handle_t* handle = native_handle_create(0, 0);
52 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
53
54 Return<void> status = dumpstate->dumpstateBoard(handle);
55
56 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
57
58 native_handle_close(handle);
59 native_handle_delete(handle);
60}
61
62// Positive test: make sure dumpstateBoard() writes something to the FD.
63TEST_F(DumpstateHidlTest, TestOk) {
Tri Vo8e1e0c42017-10-12 16:35:56 -070064 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
65 int fds[2];
66 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
Felipe Leme4b89d222017-05-26 14:55:14 -070067
68 native_handle_t* handle = native_handle_create(1, 0);
69 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
Tri Vo8e1e0c42017-10-12 16:35:56 -070070 handle->data[0] = fds[1];
Felipe Leme4b89d222017-05-26 14:55:14 -070071
72 Return<void> status = dumpstate->dumpstateBoard(handle);
73 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
74
75 // Check that at least one byte was written
Felipe Leme4b89d222017-05-26 14:55:14 -070076 char buff;
Tri Vo8e1e0c42017-10-12 16:35:56 -070077 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing";
Felipe Leme4b89d222017-05-26 14:55:14 -070078
79 native_handle_close(handle);
Felipe Leme4b89d222017-05-26 14:55:14 -070080}
81
Jie Songcc4ddff2017-06-20 09:58:13 -070082// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
83TEST_F(DumpstateHidlTest, TestHandleWithTwoFds) {
Tri Vo8e1e0c42017-10-12 16:35:56 -070084 int fds1[2];
85 int fds2[2];
86 ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
87 ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
Jie Songcc4ddff2017-06-20 09:58:13 -070088
89 native_handle_t* handle = native_handle_create(2, 0);
90 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
Tri Vo8e1e0c42017-10-12 16:35:56 -070091 handle->data[0] = fds1[1];
92 handle->data[1] = fds2[1];
Jie Songcc4ddff2017-06-20 09:58:13 -070093
94 Return<void> status = dumpstate->dumpstateBoard(handle);
95 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
96
Jie Songcc4ddff2017-06-20 09:58:13 -070097 native_handle_close(handle);
Jie Songcc4ddff2017-06-20 09:58:13 -070098}
99
Felipe Leme4b89d222017-05-26 14:55:14 -0700100int main(int argc, char** argv) {
101 ::testing::InitGoogleTest(&argc, argv);
102 int status = RUN_ALL_TESTS();
103 ALOGI("Test result = %d", status);
104 return status;
105}