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     native_handle_t* to = makeFromAidl(handle);
66     checkEq(handle, to, false /*exceptFds*/);
67     // no native_handle_close b/c fds are owned by NativeHandle
68     EXPECT_EQ(0, native_handle_delete(to));
69 }
70 
TEST(ConvertNativeHandle,MakeFromAidl)71 TEST(ConvertNativeHandle, MakeFromAidl) {
72     NativeHandle handle = makeTestAidlHandle();
73     native_handle_t* to = makeFromAidl(handle);
74     checkEq(handle, to, false /*exceptFds*/);
75     // no native_handle_close b/c fds are owned by NativeHandle
76     EXPECT_EQ(0, native_handle_delete(to));
77 }
78 
TEST(ConvertNativeHandle,DupFromAidlEmpty)79 TEST(ConvertNativeHandle, DupFromAidlEmpty) {
80     NativeHandle handle;
81     native_handle_t* to = dupFromAidl(handle);
82     checkEq(handle, to, true /*exceptFds*/);
83     EXPECT_EQ(0, native_handle_close(to));
84     EXPECT_EQ(0, native_handle_delete(to));
85 }
86 
TEST(ConvertNativeHandle,DupFromAidl)87 TEST(ConvertNativeHandle, DupFromAidl) {
88     NativeHandle handle = makeTestAidlHandle();
89     native_handle_t* to = dupFromAidl(handle);
90     checkEq(handle, to, true /*exceptFds*/);
91     EXPECT_EQ(0, native_handle_close(to));
92     EXPECT_EQ(0, native_handle_delete(to));
93 }
94 
makeTestLibcutilsHandle()95 static native_handle_t* makeTestLibcutilsHandle() {
96     native_handle_t* handle = native_handle_create(2, 4);
97     handle->data[0] = dup(0);
98     handle->data[1] = dup(0);
99     handle->data[2] = 1;
100     handle->data[3] = 2;
101     handle->data[4] = 3;
102     handle->data[5] = 4;
103     return handle;
104 }
105 
TEST(ConvertNativeHandle,MakeToAidlEmpty)106 TEST(ConvertNativeHandle, MakeToAidlEmpty) {
107     native_handle_t* handle = native_handle_create(0, 0);
108     NativeHandle to = makeToAidl(handle);
109     checkEq(to, handle, false /*exceptFds*/);
110     // no native_handle_close b/c fds are owned by NativeHandle now
111     EXPECT_EQ(0, native_handle_delete(handle));
112 }
113 
TEST(ConvertNativeHandle,MakeToAidl)114 TEST(ConvertNativeHandle, MakeToAidl) {
115     native_handle_t* handle = makeTestLibcutilsHandle();
116     NativeHandle to = makeToAidl(handle);
117     checkEq(to, handle, false /*exceptFds*/);
118     // no native_handle_close b/c fds are owned by NativeHandle now
119     EXPECT_EQ(0, native_handle_delete(handle));
120 }
121 
TEST(ConvertNativeHandle,DupToAidlEmpty)122 TEST(ConvertNativeHandle, DupToAidlEmpty) {
123     native_handle_t* handle = native_handle_create(0, 0);
124     NativeHandle to = dupToAidl(handle);
125     checkEq(to, handle, true /*exceptFds*/);
126     EXPECT_EQ(0, native_handle_close(handle));
127     EXPECT_EQ(0, native_handle_delete(handle));
128 }
129 
TEST(ConvertNativeHandle,DupToAidl)130 TEST(ConvertNativeHandle, DupToAidl) {
131     native_handle_t* handle = makeTestLibcutilsHandle();
132     NativeHandle to = dupToAidl(handle);
133     checkEq(to, handle, true /*exceptFds*/);
134     EXPECT_EQ(0, native_handle_close(handle));
135     EXPECT_EQ(0, native_handle_delete(handle));
136 }
137 
138 }  // namespace android
139