blob: 96b13c5c387a137624a62423823f7a62d2b32030 [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>
nelsonliba31c172019-10-18 16:00:01 +080024#include <gtest/gtest.h>
25#include <hidl/GtestPrinter.h>
26#include <hidl/ServiceManagement.h>
Felipe Leme4b89d222017-05-26 14:55:14 -070027#include <log/log.h>
28
Felipe Leme4b89d222017-05-26 14:55:14 -070029using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
30using ::android::hardware::Return;
31using ::android::sp;
32
nelsonliba31c172019-10-18 16:00:01 +080033class DumpstateHidlTest : public ::testing::TestWithParam<std::string> {
34 public:
Felipe Leme4b89d222017-05-26 14:55:14 -070035 virtual void SetUp() override {
nelsonliba31c172019-10-18 16:00:01 +080036 dumpstate = IDumpstateDevice::getService(GetParam());
Felipe Leme4b89d222017-05-26 14:55:14 -070037 ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance";
38 }
39
40 sp<IDumpstateDevice> dumpstate;
41};
42
43// Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
nelsonliba31c172019-10-18 16:00:01 +080044TEST_P(DumpstateHidlTest, TestNullHandle) {
Felipe Leme4b89d222017-05-26 14:55:14 -070045 Return<void> status = dumpstate->dumpstateBoard(nullptr);
46
47 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
48}
49
50// Negative test: make sure dumpstateBoard() ignores a handle with no FD.
nelsonliba31c172019-10-18 16:00:01 +080051TEST_P(DumpstateHidlTest, TestHandleWithNoFd) {
Felipe Leme4b89d222017-05-26 14:55:14 -070052 native_handle_t* handle = native_handle_create(0, 0);
53 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
54
55 Return<void> status = dumpstate->dumpstateBoard(handle);
56
57 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
58
59 native_handle_close(handle);
60 native_handle_delete(handle);
61}
62
63// Positive test: make sure dumpstateBoard() writes something to the FD.
nelsonliba31c172019-10-18 16:00:01 +080064TEST_P(DumpstateHidlTest, TestOk) {
Tri Vo8e1e0c42017-10-12 16:35:56 -070065 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
66 int fds[2];
67 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
Felipe Leme4b89d222017-05-26 14:55:14 -070068
69 native_handle_t* handle = native_handle_create(1, 0);
70 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
Tri Vo8e1e0c42017-10-12 16:35:56 -070071 handle->data[0] = fds[1];
Felipe Leme4b89d222017-05-26 14:55:14 -070072
73 Return<void> status = dumpstate->dumpstateBoard(handle);
74 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
75
76 // Check that at least one byte was written
Felipe Leme4b89d222017-05-26 14:55:14 -070077 char buff;
Tri Vo8e1e0c42017-10-12 16:35:56 -070078 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "dumped nothing";
Felipe Leme4b89d222017-05-26 14:55:14 -070079
80 native_handle_close(handle);
Felipe Leme4b89d222017-05-26 14:55:14 -070081}
82
Jie Songcc4ddff2017-06-20 09:58:13 -070083// Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
nelsonliba31c172019-10-18 16:00:01 +080084TEST_P(DumpstateHidlTest, TestHandleWithTwoFds) {
Tri Vo8e1e0c42017-10-12 16:35:56 -070085 int fds1[2];
86 int fds2[2];
87 ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
88 ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
Jie Songcc4ddff2017-06-20 09:58:13 -070089
90 native_handle_t* handle = native_handle_create(2, 0);
91 ASSERT_NE(handle, nullptr) << "Could not create native_handle";
Tri Vo8e1e0c42017-10-12 16:35:56 -070092 handle->data[0] = fds1[1];
93 handle->data[1] = fds2[1];
Jie Songcc4ddff2017-06-20 09:58:13 -070094
95 Return<void> status = dumpstate->dumpstateBoard(handle);
96 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
97
Jie Songcc4ddff2017-06-20 09:58:13 -070098 native_handle_close(handle);
Jie Songcc4ddff2017-06-20 09:58:13 -070099}
100
nelsonliba31c172019-10-18 16:00:01 +0800101INSTANTIATE_TEST_SUITE_P(
102 PerInstance, DumpstateHidlTest,
103 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)),
104 android::hardware::PrintInstanceNameToString);