blob: 9b79581379de4d079761f96fdd5b6348d477a286 [file] [log] [blame]
Steven Moreland74e043b2020-12-15 00:13:40 +00001/*
2 * Copyright (C) 2020 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#include <aidlcommonsupport/NativeHandle.h>
18#include <gtest/gtest.h>
19
20namespace android {
21
22using aidl::android::hardware::common::NativeHandle;
23using ndk::ScopedFileDescriptor;
24
25static void checkEq(const NativeHandle& aidl, native_handle_t* libcutils, bool exceptFds) {
26 ASSERT_NE(libcutils, nullptr);
27 ASSERT_EQ(libcutils->numFds, aidl.fds.size());
28
29 for (size_t i = 0; i < libcutils->numFds; i++) {
30 int afd = aidl.fds.at(i).get();
31 int lfd = libcutils->data[i];
32
33 EXPECT_GE(afd, 0) << "Invalid fd at index " << i;
34 EXPECT_GE(lfd, 0) << "Invalid fd at index " << i;
35
36 if (exceptFds) {
37 EXPECT_NE(afd, lfd) << "Index matched at " << i << " but should be dup'd fd";
38 } else {
39 EXPECT_EQ(afd, lfd) << "Index mismatched at " << i << " but should be same fd";
40 }
41 }
42
43 ASSERT_EQ(libcutils->numInts, aidl.ints.size());
44
45 for (size_t i = 0; i < libcutils->numInts; i++) {
46 int afd = aidl.ints.at(i);
47 int lfd = libcutils->data[libcutils->numFds + i];
48
49 EXPECT_EQ(afd, lfd) << "Index mismatch at " << i;
50 }
51}
52
53static NativeHandle makeTestAidlHandle() {
54 NativeHandle handle = {
55 .fds = std::vector<ScopedFileDescriptor>(2),
56 .ints = {1, 2, 3, 4},
57 };
58 handle.fds[0].set(dup(0));
59 handle.fds[1].set(dup(0));
60 return handle;
61}
62
63TEST(ConvertNativeHandle, MakeFromAidlEmpty) {
64 NativeHandle handle;
Roman Kiryanov86338db2022-10-11 15:32:55 -070065 EXPECT_TRUE(isAidlNativeHandleEmpty(handle));
Steven Moreland74e043b2020-12-15 00:13:40 +000066 native_handle_t* to = makeFromAidl(handle);
67 checkEq(handle, to, false /*exceptFds*/);
68 // no native_handle_close b/c fds are owned by NativeHandle
69 EXPECT_EQ(0, native_handle_delete(to));
70}
71
72TEST(ConvertNativeHandle, MakeFromAidl) {
73 NativeHandle handle = makeTestAidlHandle();
Roman Kiryanov86338db2022-10-11 15:32:55 -070074 EXPECT_FALSE(isAidlNativeHandleEmpty(handle));
Steven Moreland74e043b2020-12-15 00:13:40 +000075 native_handle_t* to = makeFromAidl(handle);
76 checkEq(handle, to, false /*exceptFds*/);
77 // no native_handle_close b/c fds are owned by NativeHandle
78 EXPECT_EQ(0, native_handle_delete(to));
79}
80
81TEST(ConvertNativeHandle, DupFromAidlEmpty) {
82 NativeHandle handle;
83 native_handle_t* to = dupFromAidl(handle);
84 checkEq(handle, to, true /*exceptFds*/);
85 EXPECT_EQ(0, native_handle_close(to));
86 EXPECT_EQ(0, native_handle_delete(to));
87}
88
89TEST(ConvertNativeHandle, DupFromAidl) {
90 NativeHandle handle = makeTestAidlHandle();
91 native_handle_t* to = dupFromAidl(handle);
92 checkEq(handle, to, true /*exceptFds*/);
93 EXPECT_EQ(0, native_handle_close(to));
94 EXPECT_EQ(0, native_handle_delete(to));
95}
96
97static native_handle_t* makeTestLibcutilsHandle() {
98 native_handle_t* handle = native_handle_create(2, 4);
99 handle->data[0] = dup(0);
100 handle->data[1] = dup(0);
101 handle->data[2] = 1;
102 handle->data[3] = 2;
103 handle->data[4] = 3;
104 handle->data[5] = 4;
105 return handle;
106}
107
108TEST(ConvertNativeHandle, MakeToAidlEmpty) {
109 native_handle_t* handle = native_handle_create(0, 0);
110 NativeHandle to = makeToAidl(handle);
Roman Kiryanov86338db2022-10-11 15:32:55 -0700111 EXPECT_TRUE(isAidlNativeHandleEmpty(to));
Steven Moreland74e043b2020-12-15 00:13:40 +0000112 checkEq(to, handle, false /*exceptFds*/);
113 // no native_handle_close b/c fds are owned by NativeHandle now
114 EXPECT_EQ(0, native_handle_delete(handle));
115}
116
117TEST(ConvertNativeHandle, MakeToAidl) {
118 native_handle_t* handle = makeTestLibcutilsHandle();
119 NativeHandle to = makeToAidl(handle);
Roman Kiryanov86338db2022-10-11 15:32:55 -0700120 EXPECT_FALSE(isAidlNativeHandleEmpty(to));
Steven Moreland74e043b2020-12-15 00:13:40 +0000121 checkEq(to, handle, false /*exceptFds*/);
122 // no native_handle_close b/c fds are owned by NativeHandle now
123 EXPECT_EQ(0, native_handle_delete(handle));
124}
125
126TEST(ConvertNativeHandle, DupToAidlEmpty) {
127 native_handle_t* handle = native_handle_create(0, 0);
128 NativeHandle to = dupToAidl(handle);
Roman Kiryanov86338db2022-10-11 15:32:55 -0700129 EXPECT_TRUE(isAidlNativeHandleEmpty(to));
Steven Moreland74e043b2020-12-15 00:13:40 +0000130 checkEq(to, handle, true /*exceptFds*/);
131 EXPECT_EQ(0, native_handle_close(handle));
132 EXPECT_EQ(0, native_handle_delete(handle));
133}
134
135TEST(ConvertNativeHandle, DupToAidl) {
136 native_handle_t* handle = makeTestLibcutilsHandle();
137 NativeHandle to = dupToAidl(handle);
Roman Kiryanov86338db2022-10-11 15:32:55 -0700138 EXPECT_FALSE(isAidlNativeHandleEmpty(to));
Steven Moreland74e043b2020-12-15 00:13:40 +0000139 checkEq(to, handle, true /*exceptFds*/);
140 EXPECT_EQ(0, native_handle_close(handle));
141 EXPECT_EQ(0, native_handle_delete(handle));
142}
143
144} // namespace android