1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "Cts-NdkBinderTest"
17
18 #include <android/binder_ibinder.h>
19 #include <gtest/gtest.h>
20
21 #include "utilities.h"
22
23 class NdkBinderTest_AIBinder : public NdkBinderTest {};
24
TEST_F(NdkBinderTest_AIBinder,Destruction)25 TEST_F(NdkBinderTest_AIBinder, Destruction) {
26 bool destroyed = false;
27 AIBinder* binder =
28 SampleData::newBinder(nullptr, [&](SampleData*) { destroyed = true; });
29 EXPECT_FALSE(destroyed);
30 AIBinder_incStrong(binder); // 1 -> 2
31 EXPECT_FALSE(destroyed);
32 AIBinder_decStrong(binder); // 2 -> 1
33 EXPECT_FALSE(destroyed);
34 AIBinder_decStrong(binder); // 1 -> 0
35 EXPECT_TRUE(destroyed);
36 }
37
TEST_F(NdkBinderTest_AIBinder,GetClass)38 TEST_F(NdkBinderTest_AIBinder, GetClass) {
39 AIBinder* binder = SampleData::newBinder();
40 // class is already set since this local binder is contructed with it
41 EXPECT_EQ(SampleData::kClass, AIBinder_getClass(binder));
42 AIBinder_decStrong(binder);
43 }
44
TEST_F(NdkBinderTest_AIBinder,AssociateClass)45 TEST_F(NdkBinderTest_AIBinder, AssociateClass) {
46 AIBinder* binder = SampleData::newBinder();
47 EXPECT_TRUE(AIBinder_associateClass(binder, SampleData::kClass));
48 AIBinder_decStrong(binder);
49 }
50
TEST_F(NdkBinderTest_AIBinder,AssociateUnrelatedClassWithSameDescriptorFails)51 TEST_F(NdkBinderTest_AIBinder, AssociateUnrelatedClassWithSameDescriptorFails) {
52 AIBinder* binder = SampleData::newBinder();
53 EXPECT_FALSE(AIBinder_associateClass(binder, SampleData::kAnotherClassWithSameDescriptor));
54 AIBinder_decStrong(binder);
55 }
56
TEST_F(NdkBinderTest_AIBinder,AssociateWrongClassFails)57 TEST_F(NdkBinderTest_AIBinder, AssociateWrongClassFails) {
58 AIBinder* binder = SampleData::newBinder();
59 EXPECT_FALSE(AIBinder_associateClass(binder, SampleData::kAnotherClass));
60 AIBinder_decStrong(binder);
61 }
62
TEST_F(NdkBinderTest_AIBinder,GetUserData)63 TEST_F(NdkBinderTest_AIBinder, GetUserData) {
64 // This test can't use the helper utility since SampleData isn't exposed
65 SampleData* data = new SampleData;
66 // Takes ownership of data
67 AIBinder* binder = AIBinder_new(SampleData::kClass, static_cast<void*>(data));
68 EXPECT_EQ(data, AIBinder_getUserData(binder));
69 AIBinder_decStrong(binder);
70 }
71
TEST_F(NdkBinderTest_AIBinder,DestructionGivesUserData)72 TEST_F(NdkBinderTest_AIBinder, DestructionGivesUserData) {
73 // This test can't use the helper utility since SampleData isn't exposed
74 SampleData* destroyedPointer = nullptr;
75 SampleData* data = new SampleData(
76 nullptr, [&](SampleData* data) { destroyedPointer = data; });
77 // Takes ownership of data
78 AIBinder* binder = AIBinder_new(SampleData::kClass, static_cast<void*>(data));
79 EXPECT_EQ(nullptr, destroyedPointer);
80 AIBinder_decStrong(binder);
81
82 // These pointers no longer reference valid memory locations, but the pointers
83 // themselves are valid
84 EXPECT_EQ(data, destroyedPointer);
85 }
86
onBinderDied(void *)87 void onBinderDied(void* /*cookie*/) {}
88
TEST_F(NdkBinderTest_AIBinder,LinkInProcess)89 TEST_F(NdkBinderTest_AIBinder, LinkInProcess) {
90 AIBinder* binder = SampleData::newBinder();
91 AIBinder_DeathRecipient* recipient =
92 AIBinder_DeathRecipient_new(onBinderDied);
93
94 EXPECT_EQ(STATUS_INVALID_OPERATION,
95 AIBinder_linkToDeath(binder, recipient, nullptr /*cookie*/));
96
97 AIBinder_DeathRecipient_delete(recipient);
98 AIBinder_decStrong(binder);
99 }
100
TEST_F(NdkBinderTest_AIBinder,GetCallingWhenNoCalling)101 TEST_F(NdkBinderTest_AIBinder, GetCallingWhenNoCalling) {
102 // Calling UID/PID are current calling UID/PID if there isn't an incoming
103 // call.
104 EXPECT_EQ(getuid(), AIBinder_getCallingUid());
105 EXPECT_EQ(getpid(), AIBinder_getCallingPid());
106 }
107
TEST_F(NdkBinderTest_AIBinder,DebugRefCount)108 TEST_F(NdkBinderTest_AIBinder, DebugRefCount) {
109 AIBinder* binder = SampleData::newBinder();
110 EXPECT_EQ(1, AIBinder_debugGetRefCount(binder));
111 AIBinder_decStrong(binder);
112 }
113
TEST_F(NdkBinderTest_AIBinder,WeakPointerCanPromote)114 TEST_F(NdkBinderTest_AIBinder, WeakPointerCanPromote) {
115 AIBinder* binder = SampleData::newBinder();
116 AIBinder_Weak* weak = AIBinder_Weak_new(binder);
117 AIBinder* promoted = AIBinder_Weak_promote(weak);
118 EXPECT_EQ(binder, promoted);
119 AIBinder_Weak_delete(weak);
120 AIBinder_decStrong(binder);
121 AIBinder_decStrong(promoted);
122 }
123
TEST_F(NdkBinderTest_AIBinder,WeakPointerCanNotPromote)124 TEST_F(NdkBinderTest_AIBinder, WeakPointerCanNotPromote) {
125 AIBinder* binder = SampleData::newBinder();
126 AIBinder_Weak* weak = AIBinder_Weak_new(binder);
127 AIBinder_decStrong(binder);
128
129 AIBinder* promoted = AIBinder_Weak_promote(weak);
130 EXPECT_EQ(nullptr, promoted);
131 }
132
TEST_F(NdkBinderTest_AIBinder,LocalIsLocal)133 TEST_F(NdkBinderTest_AIBinder, LocalIsLocal) {
134 AIBinder* binder = SampleData::newBinder();
135 EXPECT_FALSE(AIBinder_isRemote(binder));
136 AIBinder_decStrong(binder);
137 }
138
TEST_F(NdkBinderTest_AIBinder,IsAlive)139 TEST_F(NdkBinderTest_AIBinder, IsAlive) {
140 AIBinder* binder = SampleData::newBinder();
141 EXPECT_TRUE(AIBinder_isAlive(binder));
142 AIBinder_decStrong(binder);
143 }
144
TEST_F(NdkBinderTest_AIBinder,CanPing)145 TEST_F(NdkBinderTest_AIBinder, CanPing) {
146 AIBinder* binder = SampleData::newBinder();
147 EXPECT_OK(AIBinder_ping(binder));
148 AIBinder_decStrong(binder);
149 }
150
TEST_F(NdkBinderTest_AIBinder,GetExtensionImmediatelyReturnsNull)151 TEST_F(NdkBinderTest_AIBinder, GetExtensionImmediatelyReturnsNull) {
152 AIBinder* binder = SampleData::newBinder();
153 AIBinder* ext;
154 EXPECT_OK(AIBinder_getExtension(binder, &ext));
155 EXPECT_EQ(ext, nullptr);
156 AIBinder_decStrong(binder);
157 }
158
TEST_F(NdkBinderTest_AIBinder,GetSetExtensionLocally)159 TEST_F(NdkBinderTest_AIBinder, GetSetExtensionLocally) {
160 AIBinder* binder = SampleData::newBinder();
161 AIBinder* ext = SampleData::newBinder();
162 EXPECT_OK(AIBinder_setExtension(binder, ext));
163
164 AIBinder* getExt;
165 EXPECT_OK(AIBinder_getExtension(binder, &getExt));
166 ASSERT_EQ(ext, getExt);
167
168 AIBinder_decStrong(ext);
169 AIBinder_decStrong(getExt);
170 AIBinder_decStrong(binder);
171 }
172
TEST_F(NdkBinderTest_AIBinder,GetSetExtensionRepeatedly)173 TEST_F(NdkBinderTest_AIBinder, GetSetExtensionRepeatedly) {
174 AIBinder* binder = SampleData::newBinder();
175 AIBinder* ext1 = SampleData::newBinder();
176 AIBinder* ext2 = SampleData::newBinder();
177 EXPECT_OK(AIBinder_setExtension(binder, ext1));
178 EXPECT_OK(AIBinder_setExtension(binder, ext2));
179
180 AIBinder* getExt;
181 EXPECT_OK(AIBinder_getExtension(binder, &getExt));
182 ASSERT_EQ(ext2, getExt);
183
184 AIBinder_decStrong(ext1);
185 AIBinder_decStrong(ext2);
186 AIBinder_decStrong(getExt);
187 AIBinder_decStrong(binder);
188 }
189
TEST_F(NdkBinderTest_AIBinder,TransactionHappens)190 TEST_F(NdkBinderTest_AIBinder, TransactionHappens) {
191 AIBinder* binder = SampleData::newBinder(TransactionsReturn(STATUS_OK),
192 ExpectLifetimeTransactions(1));
193 EXPECT_OK(SampleData::transact(binder, kCode));
194 AIBinder_decStrong(binder);
195 }
196
TEST_F(NdkBinderTest_AIBinder,OnewayTransactionHappens)197 TEST_F(NdkBinderTest_AIBinder, OnewayTransactionHappens) {
198 AIBinder* binder = SampleData::newBinder(TransactionsReturn(STATUS_OK),
199 ExpectLifetimeTransactions(1));
200 EXPECT_OK(SampleData::transact(binder, kCode, WriteNothingToParcel,
201 ReadNothingFromParcel, FLAG_ONEWAY));
202 AIBinder_decStrong(binder);
203 }
204
TEST_F(NdkBinderTest_AIBinder,TransactionCodeMaintained)205 TEST_F(NdkBinderTest_AIBinder, TransactionCodeMaintained) {
206 AIBinder* binder = SampleData::newBinder(
207 [&](transaction_code_t code, const AParcel*, AParcel*) {
208 EXPECT_EQ(code, kCode);
209 return STATUS_OK;
210 },
211 ExpectLifetimeTransactions(1));
212 EXPECT_OK(SampleData::transact(binder, kCode));
213 AIBinder_decStrong(binder);
214 }
215
TEST_F(NdkBinderTest_AIBinder,TransactionCodeRangeRespected)216 TEST_F(NdkBinderTest_AIBinder, TransactionCodeRangeRespected) {
217 AIBinder* binder = SampleData::newBinder(TransactionsReturn(STATUS_OK));
218 EXPECT_OK(SampleData::transact(binder, FIRST_CALL_TRANSACTION));
219 EXPECT_OK(SampleData::transact(binder, FIRST_CALL_TRANSACTION + 1));
220 EXPECT_OK(SampleData::transact(binder, LAST_CALL_TRANSACTION - 1));
221 EXPECT_OK(SampleData::transact(binder, LAST_CALL_TRANSACTION));
222
223 EXPECT_EQ(STATUS_UNKNOWN_TRANSACTION,
224 SampleData::transact(binder, FIRST_CALL_TRANSACTION - 1));
225 EXPECT_EQ(STATUS_UNKNOWN_TRANSACTION,
226 SampleData::transact(binder, LAST_CALL_TRANSACTION + 1));
227 AIBinder_decStrong(binder);
228 }
229
TEST_F(NdkBinderTest_AIBinder,UnknownFlagsRejected)230 TEST_F(NdkBinderTest_AIBinder, UnknownFlagsRejected) {
231 AIBinder* binder =
232 SampleData::newBinder(nullptr, ExpectLifetimeTransactions(0));
233 EXPECT_EQ(STATUS_BAD_VALUE,
234 SampleData::transact(binder, kCode, WriteNothingToParcel,
235 ReadNothingFromParcel, +1 + 415));
236 EXPECT_EQ(STATUS_BAD_VALUE,
237 SampleData::transact(binder, kCode, WriteNothingToParcel,
238 ReadNothingFromParcel, FLAG_ONEWAY + 1));
239 EXPECT_EQ(STATUS_BAD_VALUE,
240 SampleData::transact(binder, kCode, WriteNothingToParcel,
241 ReadNothingFromParcel, ~0));
242 AIBinder_decStrong(binder);
243 }
244
EmptyOnCreate(void * args)245 void* EmptyOnCreate(void* args) { return args; }
EmptyOnDestroy(void *)246 void EmptyOnDestroy(void* /*userData*/) {}
EmptyOnTransact(AIBinder *,transaction_code_t,const AParcel *,AParcel *)247 binder_status_t EmptyOnTransact(AIBinder* /*binder*/,
248 transaction_code_t /*code*/,
249 const AParcel* /*in*/, AParcel* /*out*/) {
250 return STATUS_OK;
251 }
252
TEST_F(NdkBinderTest_AIBinder,NullArguments)253 TEST_F(NdkBinderTest_AIBinder, NullArguments) {
254 void* const kVoidStar = reinterpret_cast<void*>(0xDEADBEEF);
255 const char* const kStr = "asdf";
256 AIBinder* binder = SampleData::newBinder();
257 AIBinder_DeathRecipient* recipient =
258 AIBinder_DeathRecipient_new(onBinderDied);
259 EXPECT_NE(nullptr, recipient);
260
261 EXPECT_EQ(nullptr, AIBinder_Class_define(nullptr, EmptyOnCreate,
262 EmptyOnDestroy, EmptyOnTransact));
263 EXPECT_EQ(nullptr, AIBinder_Class_define(kStr, nullptr, EmptyOnDestroy,
264 EmptyOnTransact));
265 EXPECT_EQ(nullptr, AIBinder_Class_define(kStr, EmptyOnCreate, nullptr,
266 EmptyOnTransact));
267 EXPECT_EQ(nullptr, AIBinder_Class_define(kStr, EmptyOnCreate, EmptyOnDestroy,
268 nullptr));
269
270 EXPECT_EQ(nullptr, AIBinder_new(nullptr /*clazz*/, kVoidStar /*args*/));
271 EXPECT_EQ(false, AIBinder_isRemote(nullptr));
272 EXPECT_EQ(false, AIBinder_isAlive(nullptr));
273 EXPECT_EQ(STATUS_UNEXPECTED_NULL, AIBinder_ping(nullptr));
274
275 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
276 AIBinder_linkToDeath(nullptr, recipient, kVoidStar /*cookie*/));
277 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
278 AIBinder_linkToDeath(binder, nullptr, kVoidStar /*cookie*/));
279 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
280 AIBinder_unlinkToDeath(nullptr, recipient, kVoidStar /*cookie*/));
281 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
282 AIBinder_unlinkToDeath(binder, nullptr, kVoidStar /*cookie*/));
283
284 // Does not crash
285 AIBinder_incStrong(nullptr);
286 AIBinder_decStrong(nullptr);
287
288 EXPECT_EQ(-1, AIBinder_debugGetRefCount(nullptr));
289 EXPECT_EQ(false, AIBinder_associateClass(binder, nullptr));
290 EXPECT_EQ(false, AIBinder_associateClass(nullptr, SampleData::kClass));
291 EXPECT_EQ(nullptr, AIBinder_getClass(nullptr));
292 EXPECT_EQ(nullptr, AIBinder_getUserData(nullptr));
293
294 AParcel* parcel = nullptr;
295 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
296 AIBinder_prepareTransaction(binder, nullptr));
297 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
298 AIBinder_prepareTransaction(nullptr, &parcel));
299 EXPECT_EQ(nullptr, parcel); // not modified
300
301 {
302 auto newParcel = [&] {
303 AParcel* parcel = nullptr;
304 EXPECT_OK(AIBinder_prepareTransaction(binder, &parcel));
305 return parcel;
306 };
307
308 AParcel* inParcel = nullptr;
309 AParcel* outParcel = nullptr;
310
311 inParcel = newParcel();
312 EXPECT_NE(nullptr, inParcel);
313 EXPECT_EQ(
314 STATUS_UNEXPECTED_NULL,
315 AIBinder_transact(nullptr, kCode, &inParcel, &outParcel, 0 /*flags*/));
316 EXPECT_EQ(nullptr, inParcel); // ownership taken
317 EXPECT_EQ(nullptr, outParcel); // not modified
318
319 EXPECT_EQ(
320 STATUS_UNEXPECTED_NULL,
321 AIBinder_transact(binder, kCode, nullptr, &outParcel, 0 /*flags*/));
322 EXPECT_EQ(nullptr, outParcel); // not modified
323
324 inParcel = newParcel();
325 EXPECT_NE(nullptr, inParcel);
326 EXPECT_EQ(
327 STATUS_UNEXPECTED_NULL,
328 AIBinder_transact(binder, kCode, &inParcel, nullptr, 0 /*flags*/));
329 EXPECT_EQ(nullptr, inParcel); // ownership taken
330 EXPECT_EQ(nullptr, outParcel); // not modified
331 }
332
333 EXPECT_EQ(nullptr, AIBinder_Weak_new(nullptr));
334
335 // Does not crash
336 AIBinder_Weak_delete(nullptr);
337
338 EXPECT_EQ(nullptr, AIBinder_Weak_promote(nullptr));
339
340 EXPECT_EQ(nullptr, AIBinder_DeathRecipient_new(nullptr));
341
342 // Does not crash
343 AIBinder_DeathRecipient_delete(nullptr);
344
345 AIBinder_DeathRecipient_delete(recipient);
346 AIBinder_decStrong(binder);
347
348 EXPECT_EQ(STATUS_UNEXPECTED_NULL, AIBinder_getExtension(nullptr, nullptr));
349 EXPECT_EQ(STATUS_UNEXPECTED_NULL, AIBinder_setExtension(nullptr, nullptr));
350 }
351