blob: cf83faa8cf2ba260886805a5c990d008e0e28bb5 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 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 <errno.h>
18#include <fcntl.h>
19#include <poll.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <gtest/gtest.h>
25
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30
Martijn Coenen45b07b42017-08-09 12:07:45 +020031#include <sys/epoll.h>
32
Riley Andrews06b01ad2014-12-18 12:10:08 -080033#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
34
35using namespace android;
36
Sherry Yang336cdd32017-07-24 14:12:27 -070037static ::testing::AssertionResult IsPageAligned(void *buf) {
38 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
39 return ::testing::AssertionSuccess();
40 else
41 return ::testing::AssertionFailure() << buf << " is not page aligned";
42}
43
Riley Andrews06b01ad2014-12-18 12:10:08 -080044static testing::Environment* binder_env;
45static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070046static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080047static char binderserverarg[] = "--binderserver";
48
49static String16 binderLibTestServiceName = String16("test.binderLib");
50
51enum BinderLibTestTranscationCode {
52 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
53 BINDER_LIB_TEST_REGISTER_SERVER,
54 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020055 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080056 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070057 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020058 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080059 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070060 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_GET_ID_TRANSACTION,
62 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
63 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
64 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
65 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
66 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
67 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090068 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION,
70 BINDER_LIB_TEST_EXIT_TRANSACTION,
71 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
72 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070073 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080074};
75
Martijn Coenen45b07b42017-08-09 12:07:45 +020076pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080077{
78 int ret;
79 pid_t pid;
80 status_t status;
81 int pipefd[2];
82 char stri[16];
83 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020084 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080085 char *childargv[] = {
86 binderservername,
87 binderserverarg,
88 stri,
89 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020090 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070091 binderserversuffix,
Yi Kongfdd8da92018-06-07 17:52:27 -070092 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080093 };
94
95 ret = pipe(pipefd);
96 if (ret < 0)
97 return ret;
98
99 snprintf(stri, sizeof(stri), "%d", arg2);
100 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200101 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800102
103 pid = fork();
104 if (pid == -1)
105 return pid;
106 if (pid == 0) {
107 close(pipefd[0]);
108 execv(binderservername, childargv);
109 status = -errno;
110 write(pipefd[1], &status, sizeof(status));
111 fprintf(stderr, "execv failed, %s\n", strerror(errno));
112 _exit(EXIT_FAILURE);
113 }
114 close(pipefd[1]);
115 ret = read(pipefd[0], &status, sizeof(status));
116 //printf("pipe read returned %d, status %d\n", ret, status);
117 close(pipefd[0]);
118 if (ret == sizeof(status)) {
119 ret = status;
120 } else {
121 kill(pid, SIGKILL);
122 if (ret >= 0) {
123 ret = NO_INIT;
124 }
125 }
126 if (ret < 0) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700127 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800128 return ret;
129 }
130 return pid;
131}
132
133class BinderLibTestEnv : public ::testing::Environment {
134 public:
135 BinderLibTestEnv() {}
136 sp<IBinder> getServer(void) {
137 return m_server;
138 }
139
140 private:
141 virtual void SetUp() {
142 m_serverpid = start_server_process(0);
143 //printf("m_serverpid %d\n", m_serverpid);
144 ASSERT_GT(m_serverpid, 0);
145
146 sp<IServiceManager> sm = defaultServiceManager();
147 //printf("%s: pid %d, get service\n", __func__, m_pid);
148 m_server = sm->getService(binderLibTestServiceName);
Yi Kongfdd8da92018-06-07 17:52:27 -0700149 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800150 //printf("%s: pid %d, get service done\n", __func__, m_pid);
151 }
152 virtual void TearDown() {
153 status_t ret;
154 Parcel data, reply;
155 int exitStatus;
156 pid_t pid;
157
158 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700159 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800160 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
161 EXPECT_EQ(0, ret);
162 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
163 EXPECT_EQ(0, ret);
164 }
165 if (m_serverpid > 0) {
166 //printf("wait for %d\n", m_pids[i]);
167 pid = wait(&exitStatus);
168 EXPECT_EQ(m_serverpid, pid);
169 EXPECT_TRUE(WIFEXITED(exitStatus));
170 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
171 }
172 }
173
174 pid_t m_serverpid;
175 sp<IBinder> m_server;
176};
177
178class BinderLibTest : public ::testing::Test {
179 public:
180 virtual void SetUp() {
181 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
182 }
183 virtual void TearDown() {
184 }
185 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200186 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800187 {
188 int ret;
189 int32_t id;
190 Parcel data, reply;
191 sp<IBinder> binder;
192
Martijn Coenen45b07b42017-08-09 12:07:45 +0200193 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800194 EXPECT_EQ(NO_ERROR, ret);
195
Yi Kongfdd8da92018-06-07 17:52:27 -0700196 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800197 binder = reply.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700198 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800199 ret = reply.readInt32(&id);
200 EXPECT_EQ(NO_ERROR, ret);
201 if (idPtr)
202 *idPtr = id;
203 return binder;
204 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200205
Yi Kongfdd8da92018-06-07 17:52:27 -0700206 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200207 {
208 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
209 }
210
Yi Kongfdd8da92018-06-07 17:52:27 -0700211 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200212 {
213 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
214 }
215
Riley Andrews06b01ad2014-12-18 12:10:08 -0800216 void waitForReadData(int fd, int timeout_ms) {
217 int ret;
218 pollfd pfd = pollfd();
219
220 pfd.fd = fd;
221 pfd.events = POLLIN;
222 ret = poll(&pfd, 1, timeout_ms);
223 EXPECT_EQ(1, ret);
224 }
225
226 sp<IBinder> m_server;
227};
228
229class BinderLibTestBundle : public Parcel
230{
231 public:
232 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800233 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800234 int32_t mark;
235 int32_t bundleLen;
236 size_t pos;
237
238 if (source->readInt32(&mark))
239 return;
240 if (mark != MARK_START)
241 return;
242 if (source->readInt32(&bundleLen))
243 return;
244 pos = source->dataPosition();
245 if (Parcel::appendFrom(source, pos, bundleLen))
246 return;
247 source->setDataPosition(pos + bundleLen);
248 if (source->readInt32(&mark))
249 return;
250 if (mark != MARK_END)
251 return;
252 m_isValid = true;
253 setDataPosition(0);
254 }
255 void appendTo(Parcel *dest) {
256 dest->writeInt32(MARK_START);
257 dest->writeInt32(dataSize());
258 dest->appendFrom(this, 0, dataSize());
259 dest->writeInt32(MARK_END);
260 };
261 bool isValid(void) {
262 return m_isValid;
263 }
264 private:
265 enum {
266 MARK_START = B_PACK_CHARS('B','T','B','S'),
267 MARK_END = B_PACK_CHARS('B','T','B','E'),
268 };
269 bool m_isValid;
270};
271
272class BinderLibTestEvent
273{
274 public:
275 BinderLibTestEvent(void)
276 : m_eventTriggered(false)
277 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700278 pthread_mutex_init(&m_waitMutex, nullptr);
279 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800280 }
281 int waitEvent(int timeout_s)
282 {
283 int ret;
284 pthread_mutex_lock(&m_waitMutex);
285 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800286 struct timespec ts;
287 clock_gettime(CLOCK_REALTIME, &ts);
288 ts.tv_sec += timeout_s;
289 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800290 }
291 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
292 pthread_mutex_unlock(&m_waitMutex);
293 return ret;
294 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200295 pthread_t getTriggeringThread()
296 {
297 return m_triggeringThread;
298 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800299 protected:
300 void triggerEvent(void) {
301 pthread_mutex_lock(&m_waitMutex);
302 pthread_cond_signal(&m_waitCond);
303 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200304 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 pthread_mutex_unlock(&m_waitMutex);
306 };
307 private:
308 pthread_mutex_t m_waitMutex;
309 pthread_cond_t m_waitCond;
310 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200311 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800312};
313
314class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
315{
316 public:
317 BinderLibTestCallBack()
318 : m_result(NOT_ENOUGH_DATA)
Yi Kongfdd8da92018-06-07 17:52:27 -0700319 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800320 {
321 }
322 status_t getResult(void)
323 {
324 return m_result;
325 }
326
327 private:
328 virtual status_t onTransact(uint32_t code,
329 const Parcel& data, Parcel* reply,
330 uint32_t flags = 0)
331 {
332 (void)reply;
333 (void)flags;
334 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200335 case BINDER_LIB_TEST_CALL_BACK: {
336 status_t status = data.readInt32(&m_result);
337 if (status != NO_ERROR) {
338 m_result = status;
339 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800340 triggerEvent();
341 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200342 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700343 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
344 sp<IBinder> server;
345 int ret;
346 const uint8_t *buf = data.data();
347 size_t size = data.dataSize();
348 if (m_prev_end) {
349 /* 64-bit kernel needs at most 8 bytes to align buffer end */
350 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
351 } else {
352 EXPECT_TRUE(IsPageAligned((void *)buf));
353 }
354
355 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
356
357 if (size > 0) {
358 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
359 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
360 data, reply);
361 EXPECT_EQ(NO_ERROR, ret);
362 }
363 return NO_ERROR;
364 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800365 default:
366 return UNKNOWN_TRANSACTION;
367 }
368 }
369
370 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700371 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800372};
373
374class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
375{
376 private:
377 virtual void binderDied(const wp<IBinder>& who) {
378 (void)who;
379 triggerEvent();
380 };
381};
382
383TEST_F(BinderLibTest, NopTransaction) {
384 status_t ret;
385 Parcel data, reply;
386 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
387 EXPECT_EQ(NO_ERROR, ret);
388}
389
390TEST_F(BinderLibTest, SetError) {
391 int32_t testValue[] = { 0, -123, 123 };
392 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
393 status_t ret;
394 Parcel data, reply;
395 data.writeInt32(testValue[i]);
396 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
397 EXPECT_EQ(testValue[i], ret);
398 }
399}
400
401TEST_F(BinderLibTest, GetId) {
402 status_t ret;
403 int32_t id;
404 Parcel data, reply;
405 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
406 EXPECT_EQ(NO_ERROR, ret);
407 ret = reply.readInt32(&id);
408 EXPECT_EQ(NO_ERROR, ret);
409 EXPECT_EQ(0, id);
410}
411
412TEST_F(BinderLibTest, PtrSize) {
413 status_t ret;
414 int32_t ptrsize;
415 Parcel data, reply;
416 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700417 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800418 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
419 EXPECT_EQ(NO_ERROR, ret);
420 ret = reply.readInt32(&ptrsize);
421 EXPECT_EQ(NO_ERROR, ret);
422 RecordProperty("TestPtrSize", sizeof(void *));
423 RecordProperty("ServerPtrSize", sizeof(void *));
424}
425
426TEST_F(BinderLibTest, IndirectGetId2)
427{
428 status_t ret;
429 int32_t id;
430 int32_t count;
431 Parcel data, reply;
432 int32_t serverId[3];
433
434 data.writeInt32(ARRAY_SIZE(serverId));
435 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
436 sp<IBinder> server;
437 BinderLibTestBundle datai;
438
439 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700440 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800441 data.writeStrongBinder(server);
442 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
443 datai.appendTo(&data);
444 }
445
446 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
447 ASSERT_EQ(NO_ERROR, ret);
448
449 ret = reply.readInt32(&id);
450 ASSERT_EQ(NO_ERROR, ret);
451 EXPECT_EQ(0, id);
452
453 ret = reply.readInt32(&count);
454 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700455 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800456
457 for (size_t i = 0; i < (size_t)count; i++) {
458 BinderLibTestBundle replyi(&reply);
459 EXPECT_TRUE(replyi.isValid());
460 ret = replyi.readInt32(&id);
461 EXPECT_EQ(NO_ERROR, ret);
462 EXPECT_EQ(serverId[i], id);
463 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
464 }
465
466 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
467}
468
469TEST_F(BinderLibTest, IndirectGetId3)
470{
471 status_t ret;
472 int32_t id;
473 int32_t count;
474 Parcel data, reply;
475 int32_t serverId[3];
476
477 data.writeInt32(ARRAY_SIZE(serverId));
478 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
479 sp<IBinder> server;
480 BinderLibTestBundle datai;
481 BinderLibTestBundle datai2;
482
483 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700484 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800485 data.writeStrongBinder(server);
486 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
487
488 datai.writeInt32(1);
489 datai.writeStrongBinder(m_server);
490 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
491 datai2.appendTo(&datai);
492
493 datai.appendTo(&data);
494 }
495
496 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
497 ASSERT_EQ(NO_ERROR, ret);
498
499 ret = reply.readInt32(&id);
500 ASSERT_EQ(NO_ERROR, ret);
501 EXPECT_EQ(0, id);
502
503 ret = reply.readInt32(&count);
504 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700505 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800506
507 for (size_t i = 0; i < (size_t)count; i++) {
508 int32_t counti;
509
510 BinderLibTestBundle replyi(&reply);
511 EXPECT_TRUE(replyi.isValid());
512 ret = replyi.readInt32(&id);
513 EXPECT_EQ(NO_ERROR, ret);
514 EXPECT_EQ(serverId[i], id);
515
516 ret = replyi.readInt32(&counti);
517 ASSERT_EQ(NO_ERROR, ret);
518 EXPECT_EQ(1, counti);
519
520 BinderLibTestBundle replyi2(&replyi);
521 EXPECT_TRUE(replyi2.isValid());
522 ret = replyi2.readInt32(&id);
523 EXPECT_EQ(NO_ERROR, ret);
524 EXPECT_EQ(0, id);
525 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
526
527 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
528 }
529
530 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
531}
532
533TEST_F(BinderLibTest, CallBack)
534{
535 status_t ret;
536 Parcel data, reply;
537 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
538 data.writeStrongBinder(callBack);
539 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
540 EXPECT_EQ(NO_ERROR, ret);
541 ret = callBack->waitEvent(5);
542 EXPECT_EQ(NO_ERROR, ret);
543 ret = callBack->getResult();
544 EXPECT_EQ(NO_ERROR, ret);
545}
546
547TEST_F(BinderLibTest, AddServer)
548{
549 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700550 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800551}
552
Riley Andrews06b01ad2014-12-18 12:10:08 -0800553TEST_F(BinderLibTest, DeathNotificationStrongRef)
554{
555 status_t ret;
556 sp<IBinder> sbinder;
557
558 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
559
560 {
561 sp<IBinder> binder = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700562 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800563 ret = binder->linkToDeath(testDeathRecipient);
564 EXPECT_EQ(NO_ERROR, ret);
565 sbinder = binder;
566 }
567 {
568 Parcel data, reply;
569 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
570 EXPECT_EQ(0, ret);
571 }
572 IPCThreadState::self()->flushCommands();
573 ret = testDeathRecipient->waitEvent(5);
574 EXPECT_EQ(NO_ERROR, ret);
575 ret = sbinder->unlinkToDeath(testDeathRecipient);
576 EXPECT_EQ(DEAD_OBJECT, ret);
577}
578
579TEST_F(BinderLibTest, DeathNotificationMultiple)
580{
581 status_t ret;
582 const int clientcount = 2;
583 sp<IBinder> target;
584 sp<IBinder> linkedclient[clientcount];
585 sp<BinderLibTestCallBack> callBack[clientcount];
586 sp<IBinder> passiveclient[clientcount];
587
588 target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700589 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800590 for (int i = 0; i < clientcount; i++) {
591 {
592 Parcel data, reply;
593
594 linkedclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700595 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800596 callBack[i] = new BinderLibTestCallBack();
597 data.writeStrongBinder(target);
598 data.writeStrongBinder(callBack[i]);
599 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
600 EXPECT_EQ(NO_ERROR, ret);
601 }
602 {
603 Parcel data, reply;
604
605 passiveclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700606 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800607 data.writeStrongBinder(target);
608 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
609 EXPECT_EQ(NO_ERROR, ret);
610 }
611 }
612 {
613 Parcel data, reply;
614 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
615 EXPECT_EQ(0, ret);
616 }
617
618 for (int i = 0; i < clientcount; i++) {
619 ret = callBack[i]->waitEvent(5);
620 EXPECT_EQ(NO_ERROR, ret);
621 ret = callBack[i]->getResult();
622 EXPECT_EQ(NO_ERROR, ret);
623 }
624}
625
Martijn Coenenf7100e42017-07-31 12:14:09 +0200626TEST_F(BinderLibTest, DeathNotificationThread)
627{
628 status_t ret;
629 sp<BinderLibTestCallBack> callback;
630 sp<IBinder> target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700631 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200632 sp<IBinder> client = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700633 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200634
635 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
636
637 ret = target->linkToDeath(testDeathRecipient);
638 EXPECT_EQ(NO_ERROR, ret);
639
640 {
641 Parcel data, reply;
642 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
643 EXPECT_EQ(0, ret);
644 }
645
646 /* Make sure it's dead */
647 testDeathRecipient->waitEvent(5);
648
649 /* Now, pass the ref to another process and ask that process to
650 * call linkToDeath() on it, and wait for a response. This tests
651 * two things:
652 * 1) You still get death notifications when calling linkToDeath()
653 * on a ref that is already dead when it was passed to you.
654 * 2) That death notifications are not directly pushed to the thread
655 * registering them, but to the threadpool (proc workqueue) instead.
656 *
657 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
658 * is blocked on a condition variable waiting for the death notification to be
659 * called; therefore, that thread is not available for handling proc work.
660 * So, if the death notification was pushed to the thread workqueue, the callback
661 * would never be called, and the test would timeout and fail.
662 *
663 * Note that we can't do this part of the test from this thread itself, because
664 * the binder driver would only push death notifications to the thread if
665 * it is a looper thread, which this thread is not.
666 *
667 * See b/23525545 for details.
668 */
669 {
670 Parcel data, reply;
671
672 callback = new BinderLibTestCallBack();
673 data.writeStrongBinder(target);
674 data.writeStrongBinder(callback);
675 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
676 EXPECT_EQ(NO_ERROR, ret);
677 }
678
679 ret = callback->waitEvent(5);
680 EXPECT_EQ(NO_ERROR, ret);
681 ret = callback->getResult();
682 EXPECT_EQ(NO_ERROR, ret);
683}
684
Riley Andrews06b01ad2014-12-18 12:10:08 -0800685TEST_F(BinderLibTest, PassFile) {
686 int ret;
687 int pipefd[2];
688 uint8_t buf[1] = { 0 };
689 uint8_t write_value = 123;
690
691 ret = pipe2(pipefd, O_NONBLOCK);
692 ASSERT_EQ(0, ret);
693
694 {
695 Parcel data, reply;
696 uint8_t writebuf[1] = { write_value };
697
698 ret = data.writeFileDescriptor(pipefd[1], true);
699 EXPECT_EQ(NO_ERROR, ret);
700
701 ret = data.writeInt32(sizeof(writebuf));
702 EXPECT_EQ(NO_ERROR, ret);
703
704 ret = data.write(writebuf, sizeof(writebuf));
705 EXPECT_EQ(NO_ERROR, ret);
706
707 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
708 EXPECT_EQ(NO_ERROR, ret);
709 }
710
711 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700712 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800713 EXPECT_EQ(write_value, buf[0]);
714
715 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
716
717 ret = read(pipefd[0], buf, sizeof(buf));
718 EXPECT_EQ(0, ret);
719
720 close(pipefd[0]);
721}
722
Ryo Hashimotobf551892018-05-31 16:58:35 +0900723TEST_F(BinderLibTest, PassParcelFileDescriptor) {
724 const int datasize = 123;
725 std::vector<uint8_t> writebuf(datasize);
726 for (size_t i = 0; i < writebuf.size(); ++i) {
727 writebuf[i] = i;
728 }
729
730 android::base::unique_fd read_end, write_end;
731 {
732 int pipefd[2];
733 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
734 read_end.reset(pipefd[0]);
735 write_end.reset(pipefd[1]);
736 }
737 {
738 Parcel data;
739 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
740 write_end.reset();
741 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
742 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
743
744 Parcel reply;
745 EXPECT_EQ(NO_ERROR,
746 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
747 &reply));
748 }
749 std::vector<uint8_t> readbuf(datasize);
750 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
751 EXPECT_EQ(writebuf, readbuf);
752
753 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
754
755 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
756}
757
Riley Andrews06b01ad2014-12-18 12:10:08 -0800758TEST_F(BinderLibTest, PromoteLocal) {
759 sp<IBinder> strong = new BBinder();
760 wp<IBinder> weak = strong;
761 sp<IBinder> strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700762 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800763 EXPECT_EQ(strong, strong_from_weak);
Yi Kongfdd8da92018-06-07 17:52:27 -0700764 strong = nullptr;
765 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800766 strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700767 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800768}
769
770TEST_F(BinderLibTest, PromoteRemote) {
771 int ret;
772 Parcel data, reply;
773 sp<IBinder> strong = new BBinder();
774 sp<IBinder> server = addServer();
775
Yi Kongfdd8da92018-06-07 17:52:27 -0700776 ASSERT_TRUE(server != nullptr);
777 ASSERT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800778
779 ret = data.writeWeakBinder(strong);
780 EXPECT_EQ(NO_ERROR, ret);
781
782 ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
783 EXPECT_GE(ret, 0);
784}
785
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700786TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
787 status_t ret;
788 Parcel data, reply;
789
790 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
791 EXPECT_EQ(NO_ERROR, ret);
792
793 const flat_binder_object *fb = reply.readObject(false);
Yi Kongfdd8da92018-06-07 17:52:27 -0700794 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800795 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
796 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
797 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
798 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700799}
800
Connor O'Brien52be2c92016-09-20 14:18:08 -0700801TEST_F(BinderLibTest, FreedBinder) {
802 status_t ret;
803
804 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700805 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700806
807 __u32 freedHandle;
808 wp<IBinder> keepFreedBinder;
809 {
810 Parcel data, reply;
811 data.writeBool(false); /* request weak reference */
812 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
813 ASSERT_EQ(NO_ERROR, ret);
814 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
815 freedHandle = freed->handle;
816 /* Add a weak ref to the freed binder so the driver does not
817 * delete its reference to it - otherwise the transaction
818 * fails regardless of whether the driver is fixed.
819 */
820 keepFreedBinder = reply.readWeakBinder();
821 }
822 {
823 Parcel data, reply;
824 data.writeStrongBinder(server);
825 /* Replace original handle with handle to the freed binder */
826 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
827 __u32 oldHandle = strong->handle;
828 strong->handle = freedHandle;
829 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
830 /* Returns DEAD_OBJECT (-32) if target crashes and
831 * FAILED_TRANSACTION if the driver rejects the invalid
832 * object.
833 */
834 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
835 /* Restore original handle so parcel destructor does not use
836 * the wrong handle.
837 */
838 strong->handle = oldHandle;
839 }
840}
841
Sherry Yang336cdd32017-07-24 14:12:27 -0700842TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
843 status_t ret;
844 Parcel data, reply;
845 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
846 for (int i = 0; i < 2; i++) {
847 BinderLibTestBundle datai;
848 datai.appendFrom(&data, 0, data.dataSize());
849
850 data.freeData();
851 data.writeInt32(1);
852 data.writeStrongBinder(callBack);
853 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
854
855 datai.appendTo(&data);
856 }
857 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
858 EXPECT_EQ(NO_ERROR, ret);
859}
860
Martijn Coenen45b07b42017-08-09 12:07:45 +0200861TEST_F(BinderLibTest, OnewayQueueing)
862{
863 status_t ret;
864 Parcel data, data2;
865
866 sp<IBinder> pollServer = addPollServer();
867
868 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
869 data.writeStrongBinder(callBack);
870 data.writeInt32(500000); // delay in us before calling back
871
872 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
873 data2.writeStrongBinder(callBack2);
874 data2.writeInt32(0); // delay in us
875
Yi Kongfdd8da92018-06-07 17:52:27 -0700876 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200877 EXPECT_EQ(NO_ERROR, ret);
878
879 // The delay ensures that this second transaction will end up on the async_todo list
880 // (for a single-threaded server)
Yi Kongfdd8da92018-06-07 17:52:27 -0700881 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200882 EXPECT_EQ(NO_ERROR, ret);
883
884 // The server will ensure that the two transactions are handled in the expected order;
885 // If the ordering is not as expected, an error will be returned through the callbacks.
886 ret = callBack->waitEvent(2);
887 EXPECT_EQ(NO_ERROR, ret);
888 ret = callBack->getResult();
889 EXPECT_EQ(NO_ERROR, ret);
890
891 ret = callBack2->waitEvent(2);
892 EXPECT_EQ(NO_ERROR, ret);
893 ret = callBack2->getResult();
894 EXPECT_EQ(NO_ERROR, ret);
895}
896
Riley Andrews06b01ad2014-12-18 12:10:08 -0800897class BinderLibTestService : public BBinder
898{
899 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800900 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800901 : m_id(id)
902 , m_nextServerId(id + 1)
903 , m_serverStartRequested(false)
Yi Kongfdd8da92018-06-07 17:52:27 -0700904 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800905 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700906 pthread_mutex_init(&m_serverWaitMutex, nullptr);
907 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800908 }
909 ~BinderLibTestService()
910 {
911 exit(EXIT_SUCCESS);
912 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200913
914 void processPendingCall() {
Yi Kongfdd8da92018-06-07 17:52:27 -0700915 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +0200916 Parcel data;
917 data.writeInt32(NO_ERROR);
918 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kongfdd8da92018-06-07 17:52:27 -0700919 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200920 }
921 }
922
Riley Andrews06b01ad2014-12-18 12:10:08 -0800923 virtual status_t onTransact(uint32_t code,
924 const Parcel& data, Parcel* reply,
925 uint32_t flags = 0) {
926 //printf("%s: code %d\n", __func__, code);
927 (void)flags;
928
929 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
930 return PERMISSION_DENIED;
931 }
932 switch (code) {
933 case BINDER_LIB_TEST_REGISTER_SERVER: {
934 int32_t id;
935 sp<IBinder> binder;
936 id = data.readInt32();
937 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700938 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800939 return BAD_VALUE;
940 }
941
942 if (m_id != 0)
943 return INVALID_OPERATION;
944
945 pthread_mutex_lock(&m_serverWaitMutex);
946 if (m_serverStartRequested) {
947 m_serverStartRequested = false;
948 m_serverStarted = binder;
949 pthread_cond_signal(&m_serverWaitCond);
950 }
951 pthread_mutex_unlock(&m_serverWaitMutex);
952 return NO_ERROR;
953 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200954 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -0800955 case BINDER_LIB_TEST_ADD_SERVER: {
956 int ret;
957 uint8_t buf[1] = { 0 };
958 int serverid;
959
960 if (m_id != 0) {
961 return INVALID_OPERATION;
962 }
963 pthread_mutex_lock(&m_serverWaitMutex);
964 if (m_serverStartRequested) {
965 ret = -EBUSY;
966 } else {
967 serverid = m_nextServerId++;
968 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200969 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800970
971 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200972 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800973 pthread_mutex_lock(&m_serverWaitMutex);
974 }
975 if (ret > 0) {
976 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800977 struct timespec ts;
978 clock_gettime(CLOCK_REALTIME, &ts);
979 ts.tv_sec += 5;
980 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800981 }
982 if (m_serverStartRequested) {
983 m_serverStartRequested = false;
984 ret = -ETIMEDOUT;
985 } else {
986 reply->writeStrongBinder(m_serverStarted);
987 reply->writeInt32(serverid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700988 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800989 ret = NO_ERROR;
990 }
991 } else if (ret >= 0) {
992 m_serverStartRequested = false;
993 ret = UNKNOWN_ERROR;
994 }
995 pthread_mutex_unlock(&m_serverWaitMutex);
996 return ret;
997 }
998 case BINDER_LIB_TEST_NOP_TRANSACTION:
999 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001000 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1001 // Note: this transaction is only designed for use with a
1002 // poll() server. See comments around epoll_wait().
Yi Kongfdd8da92018-06-07 17:52:27 -07001003 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001004 // A callback was already pending; this means that
1005 // we received a second call while still processing
1006 // the first one. Fail the test.
1007 sp<IBinder> callback = data.readStrongBinder();
1008 Parcel data2;
1009 data2.writeInt32(UNKNOWN_ERROR);
1010
Yi Kongfdd8da92018-06-07 17:52:27 -07001011 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001012 } else {
1013 m_callback = data.readStrongBinder();
1014 int32_t delayUs = data.readInt32();
1015 /*
1016 * It's necessary that we sleep here, so the next
1017 * transaction the caller makes will be queued to
1018 * the async queue.
1019 */
1020 usleep(delayUs);
1021
1022 /*
1023 * Now when we return, libbinder will tell the kernel
1024 * we are done with this transaction, and the kernel
1025 * can move the queued transaction to either the
1026 * thread todo worklist (for kernels without the fix),
1027 * or the proc todo worklist. In case of the former,
1028 * the next outbound call will pick up the pending
1029 * transaction, which leads to undesired reentrant
1030 * behavior. This is caught in the if() branch above.
1031 */
1032 }
1033
1034 return NO_ERROR;
1035 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001036 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1037 Parcel data2, reply2;
1038 sp<IBinder> binder;
1039 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001040 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001041 return BAD_VALUE;
1042 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001043 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001044 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1045 return NO_ERROR;
1046 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001047 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1048 reply->writeStrongBinder(this);
1049 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001050 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1051 reply->writeInt32(m_id);
1052 return NO_ERROR;
1053 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1054 int32_t count;
1055 uint32_t indirect_code;
1056 sp<IBinder> binder;
1057
1058 count = data.readInt32();
1059 reply->writeInt32(m_id);
1060 reply->writeInt32(count);
1061 for (int i = 0; i < count; i++) {
1062 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001063 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001064 return BAD_VALUE;
1065 }
1066 indirect_code = data.readInt32();
1067 BinderLibTestBundle data2(&data);
1068 if (!data2.isValid()) {
1069 return BAD_VALUE;
1070 }
1071 BinderLibTestBundle reply2;
1072 binder->transact(indirect_code, data2, &reply2);
1073 reply2.appendTo(reply);
1074 }
1075 return NO_ERROR;
1076 }
1077 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1078 reply->setError(data.readInt32());
1079 return NO_ERROR;
1080 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1081 reply->writeInt32(sizeof(void *));
1082 return NO_ERROR;
1083 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1084 return NO_ERROR;
1085 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1086 m_strongRef = data.readStrongBinder();
1087 return NO_ERROR;
1088 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1089 int ret;
1090 Parcel data2, reply2;
1091 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1092 sp<IBinder> target;
1093 sp<IBinder> callback;
1094
1095 target = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001096 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001097 return BAD_VALUE;
1098 }
1099 callback = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001100 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001101 return BAD_VALUE;
1102 }
1103 ret = target->linkToDeath(testDeathRecipient);
1104 if (ret == NO_ERROR)
1105 ret = testDeathRecipient->waitEvent(5);
1106 data2.writeInt32(ret);
1107 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1108 return NO_ERROR;
1109 }
1110 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1111 int ret;
1112 int32_t size;
1113 const void *buf;
1114 int fd;
1115
1116 fd = data.readFileDescriptor();
1117 if (fd < 0) {
1118 return BAD_VALUE;
1119 }
1120 ret = data.readInt32(&size);
1121 if (ret != NO_ERROR) {
1122 return ret;
1123 }
1124 buf = data.readInplace(size);
Yi Kongfdd8da92018-06-07 17:52:27 -07001125 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001126 return BAD_VALUE;
1127 }
1128 ret = write(fd, buf, size);
1129 if (ret != size)
1130 return UNKNOWN_ERROR;
1131 return NO_ERROR;
1132 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001133 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1134 int ret;
1135 int32_t size;
1136 const void *buf;
1137 android::base::unique_fd fd;
1138
1139 ret = data.readUniqueParcelFileDescriptor(&fd);
1140 if (ret != NO_ERROR) {
1141 return ret;
1142 }
1143 ret = data.readInt32(&size);
1144 if (ret != NO_ERROR) {
1145 return ret;
1146 }
1147 buf = data.readInplace(size);
Yi Kong87d465c2018-07-24 01:14:06 -07001148 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001149 return BAD_VALUE;
1150 }
1151 ret = write(fd.get(), buf, size);
1152 if (ret != size) return UNKNOWN_ERROR;
1153 return NO_ERROR;
1154 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001155 case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
1156 int ret;
1157 wp<IBinder> weak;
1158 sp<IBinder> strong;
1159 Parcel data2, reply2;
1160 sp<IServiceManager> sm = defaultServiceManager();
1161 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1162
1163 weak = data.readWeakBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001164 if (weak == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001165 return BAD_VALUE;
1166 }
1167 strong = weak.promote();
1168
1169 ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
1170 if (ret != NO_ERROR)
1171 exit(EXIT_FAILURE);
1172
Yi Kongfdd8da92018-06-07 17:52:27 -07001173 if (strong == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001174 reply->setError(1);
1175 }
1176 return NO_ERROR;
1177 }
1178 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1179 alarm(10);
1180 return NO_ERROR;
1181 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kongfdd8da92018-06-07 17:52:27 -07001182 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001183 ;
1184 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001185 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
1186 bool strongRef = data.readBool();
1187 sp<IBinder> binder = new BBinder();
1188 if (strongRef) {
1189 reply->writeStrongBinder(binder);
1190 } else {
1191 reply->writeWeakBinder(binder);
1192 }
1193 return NO_ERROR;
1194 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001195 default:
1196 return UNKNOWN_TRANSACTION;
1197 };
1198 }
1199 private:
1200 int32_t m_id;
1201 int32_t m_nextServerId;
1202 pthread_mutex_t m_serverWaitMutex;
1203 pthread_cond_t m_serverWaitCond;
1204 bool m_serverStartRequested;
1205 sp<IBinder> m_serverStarted;
1206 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001207 bool m_callbackPending;
1208 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001209};
1210
Martijn Coenen45b07b42017-08-09 12:07:45 +02001211int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001212{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001213 binderLibTestServiceName += String16(binderserversuffix);
1214
Riley Andrews06b01ad2014-12-18 12:10:08 -08001215 status_t ret;
1216 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001217 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001218 {
1219 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001220 /*
1221 * We need this below, but can't hold a sp<> because it prevents the
1222 * node from being cleaned up automatically. It's safe in this case
1223 * because of how the tests are written.
1224 */
1225 testServicePtr = testService.get();
1226
Riley Andrews06b01ad2014-12-18 12:10:08 -08001227 if (index == 0) {
1228 ret = sm->addService(binderLibTestServiceName, testService);
1229 } else {
1230 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1231 Parcel data, reply;
1232 data.writeInt32(index);
1233 data.writeStrongBinder(testService);
1234
1235 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1236 }
1237 }
1238 write(readypipefd, &ret, sizeof(ret));
1239 close(readypipefd);
1240 //printf("%s: ret %d\n", __func__, ret);
1241 if (ret)
1242 return 1;
1243 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001244 if (usePoll) {
1245 int fd;
1246 struct epoll_event ev;
1247 int epoll_fd;
1248 IPCThreadState::self()->setupPolling(&fd);
1249 if (fd < 0) {
1250 return 1;
1251 }
1252 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1253
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001254 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001255 if (epoll_fd == -1) {
1256 return 1;
1257 }
1258
1259 ev.events = EPOLLIN;
1260 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1261 return 1;
1262 }
1263
1264 while (1) {
1265 /*
1266 * We simulate a single-threaded process using the binder poll
1267 * interface; besides handling binder commands, it can also
1268 * issue outgoing transactions, by storing a callback in
1269 * m_callback and setting m_callbackPending.
1270 *
1271 * processPendingCall() will then issue that transaction.
1272 */
1273 struct epoll_event events[1];
1274 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1275 if (numEvents < 0) {
1276 if (errno == EINTR) {
1277 continue;
1278 }
1279 return 1;
1280 }
1281 if (numEvents > 0) {
1282 IPCThreadState::self()->handlePolledCommands();
1283 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1284 testServicePtr->processPendingCall();
1285 }
1286 }
1287 } else {
1288 ProcessState::self()->startThreadPool();
1289 IPCThreadState::self()->joinThreadPool();
1290 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001291 //printf("%s: joinThreadPool returned\n", __func__);
1292 return 1; /* joinThreadPool should not return */
1293}
1294
1295int main(int argc, char **argv) {
1296 int ret;
1297
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001298 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001299 binderservername = argv[2];
1300 } else {
1301 binderservername = argv[0];
1302 }
1303
Martijn Coenen45b07b42017-08-09 12:07:45 +02001304 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1305 binderserversuffix = argv[5];
1306 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001308 binderserversuffix = new char[16];
1309 snprintf(binderserversuffix, 16, "%d", getpid());
1310 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001311
1312 ::testing::InitGoogleTest(&argc, argv);
1313 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1314 ProcessState::self()->startThreadPool();
1315 return RUN_ALL_TESTS();
1316}