blob: 54e12b6f370a287e1770ea7126cafd1fbf120eec [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
31#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
32
33using namespace android;
34
35static testing::Environment* binder_env;
36static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070037static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080038static char binderserverarg[] = "--binderserver";
39
40static String16 binderLibTestServiceName = String16("test.binderLib");
41
42enum BinderLibTestTranscationCode {
43 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
44 BINDER_LIB_TEST_REGISTER_SERVER,
45 BINDER_LIB_TEST_ADD_SERVER,
46 BINDER_LIB_TEST_CALL_BACK,
47 BINDER_LIB_TEST_NOP_CALL_BACK,
48 BINDER_LIB_TEST_GET_ID_TRANSACTION,
49 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
50 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
51 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
52 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
53 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
54 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
55 BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION,
56 BINDER_LIB_TEST_EXIT_TRANSACTION,
57 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
58 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
59};
60
61pid_t start_server_process(int arg2)
62{
63 int ret;
64 pid_t pid;
65 status_t status;
66 int pipefd[2];
67 char stri[16];
68 char strpipefd1[16];
69 char *childargv[] = {
70 binderservername,
71 binderserverarg,
72 stri,
73 strpipefd1,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070074 binderserversuffix,
Riley Andrews06b01ad2014-12-18 12:10:08 -080075 NULL
76 };
77
78 ret = pipe(pipefd);
79 if (ret < 0)
80 return ret;
81
82 snprintf(stri, sizeof(stri), "%d", arg2);
83 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
84
85 pid = fork();
86 if (pid == -1)
87 return pid;
88 if (pid == 0) {
89 close(pipefd[0]);
90 execv(binderservername, childargv);
91 status = -errno;
92 write(pipefd[1], &status, sizeof(status));
93 fprintf(stderr, "execv failed, %s\n", strerror(errno));
94 _exit(EXIT_FAILURE);
95 }
96 close(pipefd[1]);
97 ret = read(pipefd[0], &status, sizeof(status));
98 //printf("pipe read returned %d, status %d\n", ret, status);
99 close(pipefd[0]);
100 if (ret == sizeof(status)) {
101 ret = status;
102 } else {
103 kill(pid, SIGKILL);
104 if (ret >= 0) {
105 ret = NO_INIT;
106 }
107 }
108 if (ret < 0) {
109 wait(NULL);
110 return ret;
111 }
112 return pid;
113}
114
115class BinderLibTestEnv : public ::testing::Environment {
116 public:
117 BinderLibTestEnv() {}
118 sp<IBinder> getServer(void) {
119 return m_server;
120 }
121
122 private:
123 virtual void SetUp() {
124 m_serverpid = start_server_process(0);
125 //printf("m_serverpid %d\n", m_serverpid);
126 ASSERT_GT(m_serverpid, 0);
127
128 sp<IServiceManager> sm = defaultServiceManager();
129 //printf("%s: pid %d, get service\n", __func__, m_pid);
130 m_server = sm->getService(binderLibTestServiceName);
131 ASSERT_TRUE(m_server != NULL);
132 //printf("%s: pid %d, get service done\n", __func__, m_pid);
133 }
134 virtual void TearDown() {
135 status_t ret;
136 Parcel data, reply;
137 int exitStatus;
138 pid_t pid;
139
140 //printf("%s: pid %d\n", __func__, m_pid);
141 if (m_server != NULL) {
142 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
143 EXPECT_EQ(0, ret);
144 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
145 EXPECT_EQ(0, ret);
146 }
147 if (m_serverpid > 0) {
148 //printf("wait for %d\n", m_pids[i]);
149 pid = wait(&exitStatus);
150 EXPECT_EQ(m_serverpid, pid);
151 EXPECT_TRUE(WIFEXITED(exitStatus));
152 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
153 }
154 }
155
156 pid_t m_serverpid;
157 sp<IBinder> m_server;
158};
159
160class BinderLibTest : public ::testing::Test {
161 public:
162 virtual void SetUp() {
163 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
164 }
165 virtual void TearDown() {
166 }
167 protected:
168 sp<IBinder> addServer(int32_t *idPtr = NULL)
169 {
170 int ret;
171 int32_t id;
172 Parcel data, reply;
173 sp<IBinder> binder;
174
175 ret = m_server->transact(BINDER_LIB_TEST_ADD_SERVER, data, &reply);
176 EXPECT_EQ(NO_ERROR, ret);
177
178 EXPECT_FALSE(binder != NULL);
179 binder = reply.readStrongBinder();
180 EXPECT_TRUE(binder != NULL);
181 ret = reply.readInt32(&id);
182 EXPECT_EQ(NO_ERROR, ret);
183 if (idPtr)
184 *idPtr = id;
185 return binder;
186 }
187 void waitForReadData(int fd, int timeout_ms) {
188 int ret;
189 pollfd pfd = pollfd();
190
191 pfd.fd = fd;
192 pfd.events = POLLIN;
193 ret = poll(&pfd, 1, timeout_ms);
194 EXPECT_EQ(1, ret);
195 }
196
197 sp<IBinder> m_server;
198};
199
200class BinderLibTestBundle : public Parcel
201{
202 public:
203 BinderLibTestBundle(void) {}
204 BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
205 int32_t mark;
206 int32_t bundleLen;
207 size_t pos;
208
209 if (source->readInt32(&mark))
210 return;
211 if (mark != MARK_START)
212 return;
213 if (source->readInt32(&bundleLen))
214 return;
215 pos = source->dataPosition();
216 if (Parcel::appendFrom(source, pos, bundleLen))
217 return;
218 source->setDataPosition(pos + bundleLen);
219 if (source->readInt32(&mark))
220 return;
221 if (mark != MARK_END)
222 return;
223 m_isValid = true;
224 setDataPosition(0);
225 }
226 void appendTo(Parcel *dest) {
227 dest->writeInt32(MARK_START);
228 dest->writeInt32(dataSize());
229 dest->appendFrom(this, 0, dataSize());
230 dest->writeInt32(MARK_END);
231 };
232 bool isValid(void) {
233 return m_isValid;
234 }
235 private:
236 enum {
237 MARK_START = B_PACK_CHARS('B','T','B','S'),
238 MARK_END = B_PACK_CHARS('B','T','B','E'),
239 };
240 bool m_isValid;
241};
242
243class BinderLibTestEvent
244{
245 public:
246 BinderLibTestEvent(void)
247 : m_eventTriggered(false)
248 {
249 pthread_mutex_init(&m_waitMutex, NULL);
250 pthread_cond_init(&m_waitCond, NULL);
251 }
252 int waitEvent(int timeout_s)
253 {
254 int ret;
255 pthread_mutex_lock(&m_waitMutex);
256 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800257 struct timespec ts;
258 clock_gettime(CLOCK_REALTIME, &ts);
259 ts.tv_sec += timeout_s;
260 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800261 }
262 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
263 pthread_mutex_unlock(&m_waitMutex);
264 return ret;
265 }
266 protected:
267 void triggerEvent(void) {
268 pthread_mutex_lock(&m_waitMutex);
269 pthread_cond_signal(&m_waitCond);
270 m_eventTriggered = true;
271 pthread_mutex_unlock(&m_waitMutex);
272 };
273 private:
274 pthread_mutex_t m_waitMutex;
275 pthread_cond_t m_waitCond;
276 bool m_eventTriggered;
277};
278
279class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
280{
281 public:
282 BinderLibTestCallBack()
283 : m_result(NOT_ENOUGH_DATA)
284 {
285 }
286 status_t getResult(void)
287 {
288 return m_result;
289 }
290
291 private:
292 virtual status_t onTransact(uint32_t code,
293 const Parcel& data, Parcel* reply,
294 uint32_t flags = 0)
295 {
296 (void)reply;
297 (void)flags;
298 switch(code) {
299 case BINDER_LIB_TEST_CALL_BACK:
300 m_result = data.readInt32();
301 triggerEvent();
302 return NO_ERROR;
303 default:
304 return UNKNOWN_TRANSACTION;
305 }
306 }
307
308 status_t m_result;
309};
310
311class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
312{
313 private:
314 virtual void binderDied(const wp<IBinder>& who) {
315 (void)who;
316 triggerEvent();
317 };
318};
319
320TEST_F(BinderLibTest, NopTransaction) {
321 status_t ret;
322 Parcel data, reply;
323 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
324 EXPECT_EQ(NO_ERROR, ret);
325}
326
327TEST_F(BinderLibTest, SetError) {
328 int32_t testValue[] = { 0, -123, 123 };
329 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
330 status_t ret;
331 Parcel data, reply;
332 data.writeInt32(testValue[i]);
333 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
334 EXPECT_EQ(testValue[i], ret);
335 }
336}
337
338TEST_F(BinderLibTest, GetId) {
339 status_t ret;
340 int32_t id;
341 Parcel data, reply;
342 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
343 EXPECT_EQ(NO_ERROR, ret);
344 ret = reply.readInt32(&id);
345 EXPECT_EQ(NO_ERROR, ret);
346 EXPECT_EQ(0, id);
347}
348
349TEST_F(BinderLibTest, PtrSize) {
350 status_t ret;
351 int32_t ptrsize;
352 Parcel data, reply;
353 sp<IBinder> server = addServer();
354 ASSERT_TRUE(server != NULL);
355 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
356 EXPECT_EQ(NO_ERROR, ret);
357 ret = reply.readInt32(&ptrsize);
358 EXPECT_EQ(NO_ERROR, ret);
359 RecordProperty("TestPtrSize", sizeof(void *));
360 RecordProperty("ServerPtrSize", sizeof(void *));
361}
362
363TEST_F(BinderLibTest, IndirectGetId2)
364{
365 status_t ret;
366 int32_t id;
367 int32_t count;
368 Parcel data, reply;
369 int32_t serverId[3];
370
371 data.writeInt32(ARRAY_SIZE(serverId));
372 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
373 sp<IBinder> server;
374 BinderLibTestBundle datai;
375
376 server = addServer(&serverId[i]);
377 ASSERT_TRUE(server != NULL);
378 data.writeStrongBinder(server);
379 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
380 datai.appendTo(&data);
381 }
382
383 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
384 ASSERT_EQ(NO_ERROR, ret);
385
386 ret = reply.readInt32(&id);
387 ASSERT_EQ(NO_ERROR, ret);
388 EXPECT_EQ(0, id);
389
390 ret = reply.readInt32(&count);
391 ASSERT_EQ(NO_ERROR, ret);
392 EXPECT_EQ(ARRAY_SIZE(serverId), count);
393
394 for (size_t i = 0; i < (size_t)count; i++) {
395 BinderLibTestBundle replyi(&reply);
396 EXPECT_TRUE(replyi.isValid());
397 ret = replyi.readInt32(&id);
398 EXPECT_EQ(NO_ERROR, ret);
399 EXPECT_EQ(serverId[i], id);
400 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
401 }
402
403 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
404}
405
406TEST_F(BinderLibTest, IndirectGetId3)
407{
408 status_t ret;
409 int32_t id;
410 int32_t count;
411 Parcel data, reply;
412 int32_t serverId[3];
413
414 data.writeInt32(ARRAY_SIZE(serverId));
415 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
416 sp<IBinder> server;
417 BinderLibTestBundle datai;
418 BinderLibTestBundle datai2;
419
420 server = addServer(&serverId[i]);
421 ASSERT_TRUE(server != NULL);
422 data.writeStrongBinder(server);
423 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
424
425 datai.writeInt32(1);
426 datai.writeStrongBinder(m_server);
427 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
428 datai2.appendTo(&datai);
429
430 datai.appendTo(&data);
431 }
432
433 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
434 ASSERT_EQ(NO_ERROR, ret);
435
436 ret = reply.readInt32(&id);
437 ASSERT_EQ(NO_ERROR, ret);
438 EXPECT_EQ(0, id);
439
440 ret = reply.readInt32(&count);
441 ASSERT_EQ(NO_ERROR, ret);
442 EXPECT_EQ(ARRAY_SIZE(serverId), count);
443
444 for (size_t i = 0; i < (size_t)count; i++) {
445 int32_t counti;
446
447 BinderLibTestBundle replyi(&reply);
448 EXPECT_TRUE(replyi.isValid());
449 ret = replyi.readInt32(&id);
450 EXPECT_EQ(NO_ERROR, ret);
451 EXPECT_EQ(serverId[i], id);
452
453 ret = replyi.readInt32(&counti);
454 ASSERT_EQ(NO_ERROR, ret);
455 EXPECT_EQ(1, counti);
456
457 BinderLibTestBundle replyi2(&replyi);
458 EXPECT_TRUE(replyi2.isValid());
459 ret = replyi2.readInt32(&id);
460 EXPECT_EQ(NO_ERROR, ret);
461 EXPECT_EQ(0, id);
462 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
463
464 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
465 }
466
467 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
468}
469
470TEST_F(BinderLibTest, CallBack)
471{
472 status_t ret;
473 Parcel data, reply;
474 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
475 data.writeStrongBinder(callBack);
476 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
477 EXPECT_EQ(NO_ERROR, ret);
478 ret = callBack->waitEvent(5);
479 EXPECT_EQ(NO_ERROR, ret);
480 ret = callBack->getResult();
481 EXPECT_EQ(NO_ERROR, ret);
482}
483
484TEST_F(BinderLibTest, AddServer)
485{
486 sp<IBinder> server = addServer();
487 ASSERT_TRUE(server != NULL);
488}
489
490TEST_F(BinderLibTest, DeathNotificationNoRefs)
491{
492 status_t ret;
493
494 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
495
496 {
497 sp<IBinder> binder = addServer();
498 ASSERT_TRUE(binder != NULL);
499 ret = binder->linkToDeath(testDeathRecipient);
500 EXPECT_EQ(NO_ERROR, ret);
501 }
502 IPCThreadState::self()->flushCommands();
503 ret = testDeathRecipient->waitEvent(5);
504 EXPECT_EQ(NO_ERROR, ret);
505#if 0 /* Is there an unlink api that does not require a strong reference? */
506 ret = binder->unlinkToDeath(testDeathRecipient);
507 EXPECT_EQ(NO_ERROR, ret);
508#endif
509}
510
511TEST_F(BinderLibTest, DeathNotificationWeakRef)
512{
513 status_t ret;
514 wp<IBinder> wbinder;
515
516 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
517
518 {
519 sp<IBinder> binder = addServer();
520 ASSERT_TRUE(binder != NULL);
521 ret = binder->linkToDeath(testDeathRecipient);
522 EXPECT_EQ(NO_ERROR, ret);
523 wbinder = binder;
524 }
525 IPCThreadState::self()->flushCommands();
526 ret = testDeathRecipient->waitEvent(5);
527 EXPECT_EQ(NO_ERROR, ret);
528#if 0 /* Is there an unlink api that does not require a strong reference? */
529 ret = binder->unlinkToDeath(testDeathRecipient);
530 EXPECT_EQ(NO_ERROR, ret);
531#endif
532}
533
534TEST_F(BinderLibTest, DeathNotificationStrongRef)
535{
536 status_t ret;
537 sp<IBinder> sbinder;
538
539 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
540
541 {
542 sp<IBinder> binder = addServer();
543 ASSERT_TRUE(binder != NULL);
544 ret = binder->linkToDeath(testDeathRecipient);
545 EXPECT_EQ(NO_ERROR, ret);
546 sbinder = binder;
547 }
548 {
549 Parcel data, reply;
550 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
551 EXPECT_EQ(0, ret);
552 }
553 IPCThreadState::self()->flushCommands();
554 ret = testDeathRecipient->waitEvent(5);
555 EXPECT_EQ(NO_ERROR, ret);
556 ret = sbinder->unlinkToDeath(testDeathRecipient);
557 EXPECT_EQ(DEAD_OBJECT, ret);
558}
559
560TEST_F(BinderLibTest, DeathNotificationMultiple)
561{
562 status_t ret;
563 const int clientcount = 2;
564 sp<IBinder> target;
565 sp<IBinder> linkedclient[clientcount];
566 sp<BinderLibTestCallBack> callBack[clientcount];
567 sp<IBinder> passiveclient[clientcount];
568
569 target = addServer();
570 ASSERT_TRUE(target != NULL);
571 for (int i = 0; i < clientcount; i++) {
572 {
573 Parcel data, reply;
574
575 linkedclient[i] = addServer();
576 ASSERT_TRUE(linkedclient[i] != NULL);
577 callBack[i] = new BinderLibTestCallBack();
578 data.writeStrongBinder(target);
579 data.writeStrongBinder(callBack[i]);
580 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
581 EXPECT_EQ(NO_ERROR, ret);
582 }
583 {
584 Parcel data, reply;
585
586 passiveclient[i] = addServer();
587 ASSERT_TRUE(passiveclient[i] != NULL);
588 data.writeStrongBinder(target);
589 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
590 EXPECT_EQ(NO_ERROR, ret);
591 }
592 }
593 {
594 Parcel data, reply;
595 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
596 EXPECT_EQ(0, ret);
597 }
598
599 for (int i = 0; i < clientcount; i++) {
600 ret = callBack[i]->waitEvent(5);
601 EXPECT_EQ(NO_ERROR, ret);
602 ret = callBack[i]->getResult();
603 EXPECT_EQ(NO_ERROR, ret);
604 }
605}
606
607TEST_F(BinderLibTest, PassFile) {
608 int ret;
609 int pipefd[2];
610 uint8_t buf[1] = { 0 };
611 uint8_t write_value = 123;
612
613 ret = pipe2(pipefd, O_NONBLOCK);
614 ASSERT_EQ(0, ret);
615
616 {
617 Parcel data, reply;
618 uint8_t writebuf[1] = { write_value };
619
620 ret = data.writeFileDescriptor(pipefd[1], true);
621 EXPECT_EQ(NO_ERROR, ret);
622
623 ret = data.writeInt32(sizeof(writebuf));
624 EXPECT_EQ(NO_ERROR, ret);
625
626 ret = data.write(writebuf, sizeof(writebuf));
627 EXPECT_EQ(NO_ERROR, ret);
628
629 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
630 EXPECT_EQ(NO_ERROR, ret);
631 }
632
633 ret = read(pipefd[0], buf, sizeof(buf));
634 EXPECT_EQ(sizeof(buf), ret);
635 EXPECT_EQ(write_value, buf[0]);
636
637 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
638
639 ret = read(pipefd[0], buf, sizeof(buf));
640 EXPECT_EQ(0, ret);
641
642 close(pipefd[0]);
643}
644
645TEST_F(BinderLibTest, PromoteLocal) {
646 sp<IBinder> strong = new BBinder();
647 wp<IBinder> weak = strong;
648 sp<IBinder> strong_from_weak = weak.promote();
649 EXPECT_TRUE(strong != NULL);
650 EXPECT_EQ(strong, strong_from_weak);
651 strong = NULL;
652 strong_from_weak = NULL;
653 strong_from_weak = weak.promote();
654 EXPECT_TRUE(strong_from_weak == NULL);
655}
656
657TEST_F(BinderLibTest, PromoteRemote) {
658 int ret;
659 Parcel data, reply;
660 sp<IBinder> strong = new BBinder();
661 sp<IBinder> server = addServer();
662
663 ASSERT_TRUE(server != NULL);
664 ASSERT_TRUE(strong != NULL);
665
666 ret = data.writeWeakBinder(strong);
667 EXPECT_EQ(NO_ERROR, ret);
668
669 ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
670 EXPECT_GE(ret, 0);
671}
672
673class BinderLibTestService : public BBinder
674{
675 public:
676 BinderLibTestService(int32_t id)
677 : m_id(id)
678 , m_nextServerId(id + 1)
679 , m_serverStartRequested(false)
680 {
681 pthread_mutex_init(&m_serverWaitMutex, NULL);
682 pthread_cond_init(&m_serverWaitCond, NULL);
683 }
684 ~BinderLibTestService()
685 {
686 exit(EXIT_SUCCESS);
687 }
688 virtual status_t onTransact(uint32_t code,
689 const Parcel& data, Parcel* reply,
690 uint32_t flags = 0) {
691 //printf("%s: code %d\n", __func__, code);
692 (void)flags;
693
694 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
695 return PERMISSION_DENIED;
696 }
697 switch (code) {
698 case BINDER_LIB_TEST_REGISTER_SERVER: {
699 int32_t id;
700 sp<IBinder> binder;
701 id = data.readInt32();
702 binder = data.readStrongBinder();
703 if (binder == NULL) {
704 return BAD_VALUE;
705 }
706
707 if (m_id != 0)
708 return INVALID_OPERATION;
709
710 pthread_mutex_lock(&m_serverWaitMutex);
711 if (m_serverStartRequested) {
712 m_serverStartRequested = false;
713 m_serverStarted = binder;
714 pthread_cond_signal(&m_serverWaitCond);
715 }
716 pthread_mutex_unlock(&m_serverWaitMutex);
717 return NO_ERROR;
718 }
719 case BINDER_LIB_TEST_ADD_SERVER: {
720 int ret;
721 uint8_t buf[1] = { 0 };
722 int serverid;
723
724 if (m_id != 0) {
725 return INVALID_OPERATION;
726 }
727 pthread_mutex_lock(&m_serverWaitMutex);
728 if (m_serverStartRequested) {
729 ret = -EBUSY;
730 } else {
731 serverid = m_nextServerId++;
732 m_serverStartRequested = true;
733
734 pthread_mutex_unlock(&m_serverWaitMutex);
735 ret = start_server_process(serverid);
736 pthread_mutex_lock(&m_serverWaitMutex);
737 }
738 if (ret > 0) {
739 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800740 struct timespec ts;
741 clock_gettime(CLOCK_REALTIME, &ts);
742 ts.tv_sec += 5;
743 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800744 }
745 if (m_serverStartRequested) {
746 m_serverStartRequested = false;
747 ret = -ETIMEDOUT;
748 } else {
749 reply->writeStrongBinder(m_serverStarted);
750 reply->writeInt32(serverid);
751 m_serverStarted = NULL;
752 ret = NO_ERROR;
753 }
754 } else if (ret >= 0) {
755 m_serverStartRequested = false;
756 ret = UNKNOWN_ERROR;
757 }
758 pthread_mutex_unlock(&m_serverWaitMutex);
759 return ret;
760 }
761 case BINDER_LIB_TEST_NOP_TRANSACTION:
762 return NO_ERROR;
763 case BINDER_LIB_TEST_NOP_CALL_BACK: {
764 Parcel data2, reply2;
765 sp<IBinder> binder;
766 binder = data.readStrongBinder();
767 if (binder == NULL) {
768 return BAD_VALUE;
769 }
770 reply2.writeInt32(NO_ERROR);
771 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
772 return NO_ERROR;
773 }
774 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
775 reply->writeInt32(m_id);
776 return NO_ERROR;
777 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
778 int32_t count;
779 uint32_t indirect_code;
780 sp<IBinder> binder;
781
782 count = data.readInt32();
783 reply->writeInt32(m_id);
784 reply->writeInt32(count);
785 for (int i = 0; i < count; i++) {
786 binder = data.readStrongBinder();
787 if (binder == NULL) {
788 return BAD_VALUE;
789 }
790 indirect_code = data.readInt32();
791 BinderLibTestBundle data2(&data);
792 if (!data2.isValid()) {
793 return BAD_VALUE;
794 }
795 BinderLibTestBundle reply2;
796 binder->transact(indirect_code, data2, &reply2);
797 reply2.appendTo(reply);
798 }
799 return NO_ERROR;
800 }
801 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
802 reply->setError(data.readInt32());
803 return NO_ERROR;
804 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
805 reply->writeInt32(sizeof(void *));
806 return NO_ERROR;
807 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
808 return NO_ERROR;
809 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
810 m_strongRef = data.readStrongBinder();
811 return NO_ERROR;
812 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
813 int ret;
814 Parcel data2, reply2;
815 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
816 sp<IBinder> target;
817 sp<IBinder> callback;
818
819 target = data.readStrongBinder();
820 if (target == NULL) {
821 return BAD_VALUE;
822 }
823 callback = data.readStrongBinder();
824 if (callback == NULL) {
825 return BAD_VALUE;
826 }
827 ret = target->linkToDeath(testDeathRecipient);
828 if (ret == NO_ERROR)
829 ret = testDeathRecipient->waitEvent(5);
830 data2.writeInt32(ret);
831 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
832 return NO_ERROR;
833 }
834 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
835 int ret;
836 int32_t size;
837 const void *buf;
838 int fd;
839
840 fd = data.readFileDescriptor();
841 if (fd < 0) {
842 return BAD_VALUE;
843 }
844 ret = data.readInt32(&size);
845 if (ret != NO_ERROR) {
846 return ret;
847 }
848 buf = data.readInplace(size);
849 if (buf == NULL) {
850 return BAD_VALUE;
851 }
852 ret = write(fd, buf, size);
853 if (ret != size)
854 return UNKNOWN_ERROR;
855 return NO_ERROR;
856 }
857 case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
858 int ret;
859 wp<IBinder> weak;
860 sp<IBinder> strong;
861 Parcel data2, reply2;
862 sp<IServiceManager> sm = defaultServiceManager();
863 sp<IBinder> server = sm->getService(binderLibTestServiceName);
864
865 weak = data.readWeakBinder();
866 if (weak == NULL) {
867 return BAD_VALUE;
868 }
869 strong = weak.promote();
870
871 ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
872 if (ret != NO_ERROR)
873 exit(EXIT_FAILURE);
874
875 if (strong == NULL) {
876 reply->setError(1);
877 }
878 return NO_ERROR;
879 }
880 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
881 alarm(10);
882 return NO_ERROR;
883 case BINDER_LIB_TEST_EXIT_TRANSACTION:
884 while (wait(NULL) != -1 || errno != ECHILD)
885 ;
886 exit(EXIT_SUCCESS);
887 default:
888 return UNKNOWN_TRANSACTION;
889 };
890 }
891 private:
892 int32_t m_id;
893 int32_t m_nextServerId;
894 pthread_mutex_t m_serverWaitMutex;
895 pthread_cond_t m_serverWaitCond;
896 bool m_serverStartRequested;
897 sp<IBinder> m_serverStarted;
898 sp<IBinder> m_strongRef;
899};
900
901int run_server(int index, int readypipefd)
902{
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700903 binderLibTestServiceName += String16(binderserversuffix);
904
Riley Andrews06b01ad2014-12-18 12:10:08 -0800905 status_t ret;
906 sp<IServiceManager> sm = defaultServiceManager();
907 {
908 sp<BinderLibTestService> testService = new BinderLibTestService(index);
909 if (index == 0) {
910 ret = sm->addService(binderLibTestServiceName, testService);
911 } else {
912 sp<IBinder> server = sm->getService(binderLibTestServiceName);
913 Parcel data, reply;
914 data.writeInt32(index);
915 data.writeStrongBinder(testService);
916
917 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
918 }
919 }
920 write(readypipefd, &ret, sizeof(ret));
921 close(readypipefd);
922 //printf("%s: ret %d\n", __func__, ret);
923 if (ret)
924 return 1;
925 //printf("%s: joinThreadPool\n", __func__);
926 ProcessState::self()->startThreadPool();
927 IPCThreadState::self()->joinThreadPool();
928 //printf("%s: joinThreadPool returned\n", __func__);
929 return 1; /* joinThreadPool should not return */
930}
931
932int main(int argc, char **argv) {
933 int ret;
934
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700935 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800936 binderservername = argv[2];
937 } else {
938 binderservername = argv[0];
939 }
940
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700941 if (argc == 5 && !strcmp(argv[1], binderserverarg)) {
942 binderserversuffix = argv[4];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800943 return run_server(atoi(argv[2]), atoi(argv[3]));
944 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700945 binderserversuffix = new char[16];
946 snprintf(binderserversuffix, 16, "%d", getpid());
947 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800948
949 ::testing::InitGoogleTest(&argc, argv);
950 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
951 ProcessState::self()->startThreadPool();
952 return RUN_ALL_TESTS();
953}
954