1 /*
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 
20 namespace android {
21 
22 using aidl::android::hardware::common::NativeHandle;
23 using ndk::ScopedFileDescriptor;
24 
checkEq(const NativeHandle & aidl,native_handle_t * libcutils,bool exceptFds)25 static 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 
makeTestAidlHandle()53 static 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 
TEST(ConvertNativeHandle,MakeFromAidlEmpty)63 TEST(ConvertNativeHandle, MakeFromAidlEmpty) {
64     NativeHandle handle;
65     EXPECT_TRUE(isAidlNativeHandleEmpty(handle));
66     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 
TEST(ConvertNativeHandle,MakeFromAidl)72 TEST(ConvertNativeHandle, MakeFromAidl) {
73     NativeHandle handle = makeTestAidlHandle();
74     EXPECT_FALSE(isAidlNativeHandleEmpty(handle));
75     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 
TEST(ConvertNativeHandle,DupFromAidlEmpty)81 TEST(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 
TEST(ConvertNativeHandle,DupFromAidl)89 TEST(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 
makeTestLibcutilsHandle()97 static 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 
TEST(ConvertNativeHandle,MakeToAidlEmpty)108 TEST(ConvertNativeHandle, MakeToAidlEmpty) {
109     native_handle_t* handle = native_handle_create(0, 0);
110     NativeHandle to = makeToAidl(handle);
111     EXPECT_TRUE(isAidlNativeHandleEmpty(to));
112     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 
TEST(ConvertNativeHandle,MakeToAidl)117 TEST(ConvertNativeHandle, MakeToAidl) {
118     native_handle_t* handle = makeTestLibcutilsHandle();
119     NativeHandle to = makeToAidl(handle);
120     EXPECT_FALSE(isAidlNativeHandleEmpty(to));
121     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 
TEST(ConvertNativeHandle,DupToAidlEmpty)126 TEST(ConvertNativeHandle, DupToAidlEmpty) {
127     native_handle_t* handle = native_handle_create(0, 0);
128     NativeHandle to = dupToAidl(handle);
129     EXPECT_TRUE(isAidlNativeHandleEmpty(to));
130     checkEq(to, handle, true /*exceptFds*/);
131     EXPECT_EQ(0, native_handle_close(handle));
132     EXPECT_EQ(0, native_handle_delete(handle));
133 }
134 
TEST(ConvertNativeHandle,DupToAidl)135 TEST(ConvertNativeHandle, DupToAidl) {
136     native_handle_t* handle = makeTestLibcutilsHandle();
137     NativeHandle to = dupToAidl(handle);
138     EXPECT_FALSE(isAidlNativeHandleEmpty(to));
139     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
145