blob: 20fa6d25b2df2eacd34ab30fdb967ee9b1055c3e [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
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>
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -080024#include <VtsHalHidlTargetTestEnvBase.h>
Felipe Leme4b89d222017-05-26 14:55:14 -070025
26using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
27using ::android::hardware::Return;
28using ::android::sp;
29
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -080030// Test environment for Dumpstate HIDL HAL.
31class DumpstateHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
32 public:
33 // get the test environment singleton
34 static DumpstateHidlEnvironment* Instance() {
35 static DumpstateHidlEnvironment* instance = new DumpstateHidlEnvironment;
36 return instance;
37 }
38
39 virtual void registerTestServices() override { registerTestService<IDumpstateDevice>(); }
40};
41
Felipe Leme4b89d222017-05-26 14:55:14 -070042class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase {
43 public:
44 virtual void SetUp() override {
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -080045 dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>(
46 DumpstateHidlEnvironment::Instance()->getServiceName<IDumpstateDevice>());
Felipe Leme4b89d222017-05-26 14:55:14 -070047 ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance";
48 }
49
50 sp<IDumpstateDevice> dumpstate;
51};
52
53// Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
54TEST_F(DumpstateHidlTest, TestNullHandle) {
55 Return<void> status = dumpstate->dumpstateBoard(nullptr);
56
57 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
58}
59
60// Negative test: make sure dumpstateBoard() ignores a handle with no FD.
61TEST_F(DumpstateHidlTest, TestHandleWithNoFd) {
62 native_handle_t* handle = native_handle_create(0, 0);
63 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
64
65 Return<void> status = dumpstate->dumpstateBoard(handle);
66
67 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
68
69 native_handle_close(handle);
70 native_handle_delete(handle);
71}
72
73// Positive test: make sure dumpstateBoard() writes something to the FD.
74TEST_F(DumpstateHidlTest, TestOk) {
75 FILE* file = tmpfile();
Jie Songcc4ddff2017-06-20 09:58:13 -070076
Felipe Leme4b89d222017-05-26 14:55:14 -070077 ASSERT_NE(nullptr, file) << "Could not create temp file: " << strerror(errno);
78
79 native_handle_t* handle = native_handle_create(1, 0);
80 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
81 handle->data[0] = fileno(file);
82
83 Return<void> status = dumpstate->dumpstateBoard(handle);
84 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
85
86 // Check that at least one byte was written
87 rewind(file); // can not fail
88 char buff;
89 int read = fread(&buff, sizeof(buff), 1, file);
90 ASSERT_EQ(1, read) << "dumped nothing";
91
92 EXPECT_EQ(0, fclose(file)) << errno;
93
94 native_handle_close(handle);
95 native_handle_delete(handle);
96}
97
Jie Songcc4ddff2017-06-20 09:58:13 -070098// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
99TEST_F(DumpstateHidlTest, TestHandleWithTwoFds) {
100 FILE* file1 = tmpfile();
101 FILE* file2 = tmpfile();
102
103 ASSERT_NE(nullptr, file1) << "Could not create temp file #1: " << strerror(errno);
104 ASSERT_NE(nullptr, file2) << "Could not create temp file #2: " << strerror(errno);
105
106 native_handle_t* handle = native_handle_create(2, 0);
107 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
108 handle->data[0] = fileno(file1);
109 handle->data[1] = fileno(file2);
110
111 Return<void> status = dumpstate->dumpstateBoard(handle);
112 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
113
114 EXPECT_EQ(0, fclose(file1)) << errno;
115 EXPECT_EQ(0, fclose(file2)) << errno;
116
117 native_handle_close(handle);
118 native_handle_delete(handle);
119}
120
Felipe Leme4b89d222017-05-26 14:55:14 -0700121int main(int argc, char** argv) {
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -0800122 ::testing::AddGlobalTestEnvironment(DumpstateHidlEnvironment::Instance());
Felipe Leme4b89d222017-05-26 14:55:14 -0700123 ::testing::InitGoogleTest(&argc, argv);
Zhuoyao Zhangbdca6e22018-02-08 20:51:09 -0800124 DumpstateHidlEnvironment::Instance()->init(&argc, argv);
Felipe Leme4b89d222017-05-26 14:55:14 -0700125 int status = RUN_ALL_TESTS();
126 ALOGI("Test result = %d", status);
127 return status;
128}