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,AssociateWrongClassFails)51 TEST_F(NdkBinderTest_AIBinder, AssociateWrongClassFails) {
52 AIBinder* binder = SampleData::newBinder();
53 EXPECT_FALSE(AIBinder_associateClass(binder, SampleData::kAnotherClass));
54 AIBinder_decStrong(binder);
55 }
56
TEST_F(NdkBinderTest_AIBinder,GetUserData)57 TEST_F(NdkBinderTest_AIBinder, GetUserData) {
58 // This test can't use the helper utility since SampleData isn't exposed
59 SampleData* data = new SampleData;
60 // Takes ownership of data
61 AIBinder* binder = AIBinder_new(SampleData::kClass, static_cast<void*>(data));
62 EXPECT_EQ(data, AIBinder_getUserData(binder));
63 AIBinder_decStrong(binder);
64 }
65
TEST_F(NdkBinderTest_AIBinder,DestructionGivesUserData)66 TEST_F(NdkBinderTest_AIBinder, DestructionGivesUserData) {
67 // This test can't use the helper utility since SampleData isn't exposed
68 SampleData* destroyedPointer = nullptr;
69 SampleData* data = new SampleData(
70 nullptr, [&](SampleData* data) { destroyedPointer = data; });
71 // Takes ownership of data
72 AIBinder* binder = AIBinder_new(SampleData::kClass, static_cast<void*>(data));
73 EXPECT_EQ(nullptr, destroyedPointer);
74 AIBinder_decStrong(binder);
75
76 // These pointers no longer reference valid memory locations, but the pointers
77 // themselves are valid
78 EXPECT_EQ(data, destroyedPointer);
79 }
80
onBinderDied(void *)81 void onBinderDied(void* /*cookie*/) {}
82
TEST_F(NdkBinderTest_AIBinder,LinkInProcess)83 TEST_F(NdkBinderTest_AIBinder, LinkInProcess) {
84 AIBinder* binder = SampleData::newBinder();
85 AIBinder_DeathRecipient* recipient =
86 AIBinder_DeathRecipient_new(onBinderDied);
87
88 EXPECT_EQ(STATUS_INVALID_OPERATION,
89 AIBinder_linkToDeath(binder, recipient, nullptr /*cookie*/));
90
91 AIBinder_DeathRecipient_delete(recipient);
92 AIBinder_decStrong(binder);
93 }
94
TEST_F(NdkBinderTest_AIBinder,GetCallingWhenNoCalling)95 TEST_F(NdkBinderTest_AIBinder, GetCallingWhenNoCalling) {
96 // Calling UID/PID are current calling UID/PID if there isn't an incoming
97 // call.
98 EXPECT_EQ(getuid(), AIBinder_getCallingUid());
99 EXPECT_EQ(getpid(), AIBinder_getCallingPid());
100 }
101
TEST_F(NdkBinderTest_AIBinder,DebugRefCount)102 TEST_F(NdkBinderTest_AIBinder, DebugRefCount) {
103 AIBinder* binder = SampleData::newBinder();
104 EXPECT_EQ(1, AIBinder_debugGetRefCount(binder));
105 AIBinder_decStrong(binder);
106 }
107
TEST_F(NdkBinderTest_AIBinder,WeakPointerCanPromote)108 TEST_F(NdkBinderTest_AIBinder, WeakPointerCanPromote) {
109 AIBinder* binder = SampleData::newBinder();
110 AIBinder_Weak* weak = AIBinder_Weak_new(binder);
111 AIBinder* promoted = AIBinder_Weak_promote(weak);
112 EXPECT_EQ(binder, promoted);
113 AIBinder_Weak_delete(weak);
114 AIBinder_decStrong(binder);
115 AIBinder_decStrong(promoted);
116 }
117
TEST_F(NdkBinderTest_AIBinder,WeakPointerCanNotPromote)118 TEST_F(NdkBinderTest_AIBinder, WeakPointerCanNotPromote) {
119 AIBinder* binder = SampleData::newBinder();
120 AIBinder_Weak* weak = AIBinder_Weak_new(binder);
121 AIBinder_decStrong(binder);
122
123 AIBinder* promoted = AIBinder_Weak_promote(weak);
124 EXPECT_EQ(nullptr, promoted);
125 }
126
TEST_F(NdkBinderTest_AIBinder,LocalIsLocal)127 TEST_F(NdkBinderTest_AIBinder, LocalIsLocal) {
128 AIBinder* binder = SampleData::newBinder();
129 EXPECT_FALSE(AIBinder_isRemote(binder));
130 AIBinder_decStrong(binder);
131 }
132
TEST_F(NdkBinderTest_AIBinder,IsAlive)133 TEST_F(NdkBinderTest_AIBinder, IsAlive) {
134 AIBinder* binder = SampleData::newBinder();
135 EXPECT_TRUE(AIBinder_isAlive(binder));
136 AIBinder_decStrong(binder);
137 }
138
TEST_F(NdkBinderTest_AIBinder,CanPing)139 TEST_F(NdkBinderTest_AIBinder, CanPing) {
140 AIBinder* binder = SampleData::newBinder();
141 EXPECT_OK(AIBinder_ping(binder));
142 AIBinder_decStrong(binder);
143 }
144
TEST_F(NdkBinderTest_AIBinder,TransactionHappens)145 TEST_F(NdkBinderTest_AIBinder, TransactionHappens) {
146 AIBinder* binder = SampleData::newBinder(TransactionsReturn(STATUS_OK),
147 ExpectLifetimeTransactions(1));
148 EXPECT_OK(SampleData::transact(binder, kCode));
149 AIBinder_decStrong(binder);
150 }
151
TEST_F(NdkBinderTest_AIBinder,OnewayTransactionHappens)152 TEST_F(NdkBinderTest_AIBinder, OnewayTransactionHappens) {
153 AIBinder* binder = SampleData::newBinder(TransactionsReturn(STATUS_OK),
154 ExpectLifetimeTransactions(1));
155 EXPECT_OK(SampleData::transact(binder, kCode, WriteNothingToParcel,
156 ReadNothingFromParcel, FLAG_ONEWAY));
157 AIBinder_decStrong(binder);
158 }
159
TEST_F(NdkBinderTest_AIBinder,TransactionCodeMaintained)160 TEST_F(NdkBinderTest_AIBinder, TransactionCodeMaintained) {
161 AIBinder* binder = SampleData::newBinder(
162 [&](transaction_code_t code, const AParcel*, AParcel*) {
163 EXPECT_EQ(code, kCode);
164 return STATUS_OK;
165 },
166 ExpectLifetimeTransactions(1));
167 EXPECT_OK(SampleData::transact(binder, kCode));
168 AIBinder_decStrong(binder);
169 }
170
TEST_F(NdkBinderTest_AIBinder,TransactionCodeRangeRespected)171 TEST_F(NdkBinderTest_AIBinder, TransactionCodeRangeRespected) {
172 AIBinder* binder = SampleData::newBinder(TransactionsReturn(STATUS_OK));
173 EXPECT_OK(SampleData::transact(binder, FIRST_CALL_TRANSACTION));
174 EXPECT_OK(SampleData::transact(binder, FIRST_CALL_TRANSACTION + 1));
175 EXPECT_OK(SampleData::transact(binder, LAST_CALL_TRANSACTION - 1));
176 EXPECT_OK(SampleData::transact(binder, LAST_CALL_TRANSACTION));
177
178 EXPECT_EQ(STATUS_UNKNOWN_TRANSACTION,
179 SampleData::transact(binder, FIRST_CALL_TRANSACTION - 1));
180 EXPECT_EQ(STATUS_UNKNOWN_TRANSACTION,
181 SampleData::transact(binder, LAST_CALL_TRANSACTION + 1));
182 AIBinder_decStrong(binder);
183 }
184
TEST_F(NdkBinderTest_AIBinder,UnknownFlagsRejected)185 TEST_F(NdkBinderTest_AIBinder, UnknownFlagsRejected) {
186 AIBinder* binder =
187 SampleData::newBinder(nullptr, ExpectLifetimeTransactions(0));
188 EXPECT_EQ(STATUS_BAD_VALUE,
189 SampleData::transact(binder, kCode, WriteNothingToParcel,
190 ReadNothingFromParcel, +1 + 415));
191 EXPECT_EQ(STATUS_BAD_VALUE,
192 SampleData::transact(binder, kCode, WriteNothingToParcel,
193 ReadNothingFromParcel, FLAG_ONEWAY + 1));
194 EXPECT_EQ(STATUS_BAD_VALUE,
195 SampleData::transact(binder, kCode, WriteNothingToParcel,
196 ReadNothingFromParcel, ~0));
197 AIBinder_decStrong(binder);
198 }
199
EmptyOnCreate(void * args)200 void* EmptyOnCreate(void* args) { return args; }
EmptyOnDestroy(void *)201 void EmptyOnDestroy(void* /*userData*/) {}
EmptyOnTransact(AIBinder *,transaction_code_t,const AParcel *,AParcel *)202 binder_status_t EmptyOnTransact(AIBinder* /*binder*/,
203 transaction_code_t /*code*/,
204 const AParcel* /*in*/, AParcel* /*out*/) {
205 return STATUS_OK;
206 }
207
TEST_F(NdkBinderTest_AIBinder,NullArguments)208 TEST_F(NdkBinderTest_AIBinder, NullArguments) {
209 void* const kVoidStar = reinterpret_cast<void*>(0xDEADBEEF);
210 const char* const kStr = "asdf";
211 AIBinder* binder = SampleData::newBinder();
212 AIBinder_DeathRecipient* recipient =
213 AIBinder_DeathRecipient_new(onBinderDied);
214 EXPECT_NE(nullptr, recipient);
215
216 EXPECT_EQ(nullptr, AIBinder_Class_define(nullptr, EmptyOnCreate,
217 EmptyOnDestroy, EmptyOnTransact));
218 EXPECT_EQ(nullptr, AIBinder_Class_define(kStr, nullptr, EmptyOnDestroy,
219 EmptyOnTransact));
220 EXPECT_EQ(nullptr, AIBinder_Class_define(kStr, EmptyOnCreate, nullptr,
221 EmptyOnTransact));
222 EXPECT_EQ(nullptr, AIBinder_Class_define(kStr, EmptyOnCreate, EmptyOnDestroy,
223 nullptr));
224
225 EXPECT_EQ(nullptr, AIBinder_new(nullptr /*clazz*/, kVoidStar /*args*/));
226 EXPECT_EQ(false, AIBinder_isRemote(nullptr));
227 EXPECT_EQ(false, AIBinder_isAlive(nullptr));
228 EXPECT_EQ(STATUS_UNEXPECTED_NULL, AIBinder_ping(nullptr));
229
230 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
231 AIBinder_linkToDeath(nullptr, recipient, kVoidStar /*cookie*/));
232 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
233 AIBinder_linkToDeath(binder, nullptr, kVoidStar /*cookie*/));
234 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
235 AIBinder_unlinkToDeath(nullptr, recipient, kVoidStar /*cookie*/));
236 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
237 AIBinder_unlinkToDeath(binder, nullptr, kVoidStar /*cookie*/));
238
239 // Does not crash
240 AIBinder_incStrong(nullptr);
241 AIBinder_decStrong(nullptr);
242
243 EXPECT_EQ(-1, AIBinder_debugGetRefCount(nullptr));
244 EXPECT_EQ(false, AIBinder_associateClass(binder, nullptr));
245 EXPECT_EQ(false, AIBinder_associateClass(nullptr, SampleData::kClass));
246 EXPECT_EQ(nullptr, AIBinder_getClass(nullptr));
247 EXPECT_EQ(nullptr, AIBinder_getUserData(nullptr));
248
249 AParcel* parcel = nullptr;
250 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
251 AIBinder_prepareTransaction(binder, nullptr));
252 EXPECT_EQ(STATUS_UNEXPECTED_NULL,
253 AIBinder_prepareTransaction(nullptr, &parcel));
254 EXPECT_EQ(nullptr, parcel); // not modified
255
256 {
257 auto newParcel = [&] {
258 AParcel* parcel = nullptr;
259 EXPECT_OK(AIBinder_prepareTransaction(binder, &parcel));
260 return parcel;
261 };
262
263 AParcel* inParcel = nullptr;
264 AParcel* outParcel = nullptr;
265
266 inParcel = newParcel();
267 EXPECT_NE(nullptr, inParcel);
268 EXPECT_EQ(
269 STATUS_UNEXPECTED_NULL,
270 AIBinder_transact(nullptr, kCode, &inParcel, &outParcel, 0 /*flags*/));
271 EXPECT_EQ(nullptr, inParcel); // ownership taken
272 EXPECT_EQ(nullptr, outParcel); // not modified
273
274 EXPECT_EQ(
275 STATUS_UNEXPECTED_NULL,
276 AIBinder_transact(binder, kCode, nullptr, &outParcel, 0 /*flags*/));
277 EXPECT_EQ(nullptr, outParcel); // not modified
278
279 inParcel = newParcel();
280 EXPECT_NE(nullptr, inParcel);
281 EXPECT_EQ(
282 STATUS_UNEXPECTED_NULL,
283 AIBinder_transact(binder, kCode, &inParcel, nullptr, 0 /*flags*/));
284 EXPECT_EQ(nullptr, inParcel); // ownership taken
285 EXPECT_EQ(nullptr, outParcel); // not modified
286 }
287
288 EXPECT_EQ(nullptr, AIBinder_Weak_new(nullptr));
289
290 // Does not crash
291 AIBinder_Weak_delete(nullptr);
292
293 EXPECT_EQ(nullptr, AIBinder_Weak_promote(nullptr));
294
295 EXPECT_EQ(nullptr, AIBinder_DeathRecipient_new(nullptr));
296
297 // Does not crash
298 AIBinder_DeathRecipient_delete(nullptr);
299
300 AIBinder_DeathRecipient_delete(recipient);
301 AIBinder_decStrong(binder);
302 }
303