1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <aidlcommonsupport/NativeHandle.h>
#include <gtest/gtest.h>
namespace android {
using aidl::android::hardware::common::NativeHandle;
using ndk::ScopedFileDescriptor;
static void checkEq(const NativeHandle& aidl, native_handle_t* libcutils, bool exceptFds) {
ASSERT_NE(libcutils, nullptr);
ASSERT_EQ(libcutils->numFds, aidl.fds.size());
for (size_t i = 0; i < libcutils->numFds; i++) {
int afd = aidl.fds.at(i).get();
int lfd = libcutils->data[i];
EXPECT_GE(afd, 0) << "Invalid fd at index " << i;
EXPECT_GE(lfd, 0) << "Invalid fd at index " << i;
if (exceptFds) {
EXPECT_NE(afd, lfd) << "Index matched at " << i << " but should be dup'd fd";
} else {
EXPECT_EQ(afd, lfd) << "Index mismatched at " << i << " but should be same fd";
}
}
ASSERT_EQ(libcutils->numInts, aidl.ints.size());
for (size_t i = 0; i < libcutils->numInts; i++) {
int afd = aidl.ints.at(i);
int lfd = libcutils->data[libcutils->numFds + i];
EXPECT_EQ(afd, lfd) << "Index mismatch at " << i;
}
}
static NativeHandle makeTestAidlHandle() {
NativeHandle handle = {
.fds = std::vector<ScopedFileDescriptor>(2),
.ints = {1, 2, 3, 4},
};
handle.fds[0].set(dup(0));
handle.fds[1].set(dup(0));
return handle;
}
TEST(ConvertNativeHandle, MakeFromAidlEmpty) {
NativeHandle handle;
native_handle_t* to = makeFromAidl(handle);
checkEq(handle, to, false /*exceptFds*/);
// no native_handle_close b/c fds are owned by NativeHandle
EXPECT_EQ(0, native_handle_delete(to));
}
TEST(ConvertNativeHandle, MakeFromAidl) {
NativeHandle handle = makeTestAidlHandle();
native_handle_t* to = makeFromAidl(handle);
checkEq(handle, to, false /*exceptFds*/);
// no native_handle_close b/c fds are owned by NativeHandle
EXPECT_EQ(0, native_handle_delete(to));
}
TEST(ConvertNativeHandle, DupFromAidlEmpty) {
NativeHandle handle;
native_handle_t* to = dupFromAidl(handle);
checkEq(handle, to, true /*exceptFds*/);
EXPECT_EQ(0, native_handle_close(to));
EXPECT_EQ(0, native_handle_delete(to));
}
TEST(ConvertNativeHandle, DupFromAidl) {
NativeHandle handle = makeTestAidlHandle();
native_handle_t* to = dupFromAidl(handle);
checkEq(handle, to, true /*exceptFds*/);
EXPECT_EQ(0, native_handle_close(to));
EXPECT_EQ(0, native_handle_delete(to));
}
static native_handle_t* makeTestLibcutilsHandle() {
native_handle_t* handle = native_handle_create(2, 4);
handle->data[0] = dup(0);
handle->data[1] = dup(0);
handle->data[2] = 1;
handle->data[3] = 2;
handle->data[4] = 3;
handle->data[5] = 4;
return handle;
}
TEST(ConvertNativeHandle, MakeToAidlEmpty) {
native_handle_t* handle = native_handle_create(0, 0);
NativeHandle to = makeToAidl(handle);
checkEq(to, handle, false /*exceptFds*/);
// no native_handle_close b/c fds are owned by NativeHandle now
EXPECT_EQ(0, native_handle_delete(handle));
}
TEST(ConvertNativeHandle, MakeToAidl) {
native_handle_t* handle = makeTestLibcutilsHandle();
NativeHandle to = makeToAidl(handle);
checkEq(to, handle, false /*exceptFds*/);
// no native_handle_close b/c fds are owned by NativeHandle now
EXPECT_EQ(0, native_handle_delete(handle));
}
TEST(ConvertNativeHandle, DupToAidlEmpty) {
native_handle_t* handle = native_handle_create(0, 0);
NativeHandle to = dupToAidl(handle);
checkEq(to, handle, true /*exceptFds*/);
EXPECT_EQ(0, native_handle_close(handle));
EXPECT_EQ(0, native_handle_delete(handle));
}
TEST(ConvertNativeHandle, DupToAidl) {
native_handle_t* handle = makeTestLibcutilsHandle();
NativeHandle to = dupToAidl(handle);
checkEq(to, handle, true /*exceptFds*/);
EXPECT_EQ(0, native_handle_close(handle));
EXPECT_EQ(0, native_handle_delete(handle));
}
} // namespace android
|