blob: 4f0c969e910ab799b86b1ef800f1115f6835b4ec [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_EXIT_TRANSACTION,
70 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
71 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070072 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080073};
74
Martijn Coenen45b07b42017-08-09 12:07:45 +020075pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080076{
77 int ret;
78 pid_t pid;
79 status_t status;
80 int pipefd[2];
81 char stri[16];
82 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020083 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080084 char *childargv[] = {
85 binderservername,
86 binderserverarg,
87 stri,
88 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020089 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070090 binderserversuffix,
Yi Kongfdd8da92018-06-07 17:52:27 -070091 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080092 };
93
94 ret = pipe(pipefd);
95 if (ret < 0)
96 return ret;
97
98 snprintf(stri, sizeof(stri), "%d", arg2);
99 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200100 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800101
102 pid = fork();
103 if (pid == -1)
104 return pid;
105 if (pid == 0) {
106 close(pipefd[0]);
107 execv(binderservername, childargv);
108 status = -errno;
109 write(pipefd[1], &status, sizeof(status));
110 fprintf(stderr, "execv failed, %s\n", strerror(errno));
111 _exit(EXIT_FAILURE);
112 }
113 close(pipefd[1]);
114 ret = read(pipefd[0], &status, sizeof(status));
115 //printf("pipe read returned %d, status %d\n", ret, status);
116 close(pipefd[0]);
117 if (ret == sizeof(status)) {
118 ret = status;
119 } else {
120 kill(pid, SIGKILL);
121 if (ret >= 0) {
122 ret = NO_INIT;
123 }
124 }
125 if (ret < 0) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700126 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800127 return ret;
128 }
129 return pid;
130}
131
132class BinderLibTestEnv : public ::testing::Environment {
133 public:
134 BinderLibTestEnv() {}
135 sp<IBinder> getServer(void) {
136 return m_server;
137 }
138
139 private:
140 virtual void SetUp() {
141 m_serverpid = start_server_process(0);
142 //printf("m_serverpid %d\n", m_serverpid);
143 ASSERT_GT(m_serverpid, 0);
144
145 sp<IServiceManager> sm = defaultServiceManager();
146 //printf("%s: pid %d, get service\n", __func__, m_pid);
147 m_server = sm->getService(binderLibTestServiceName);
Yi Kongfdd8da92018-06-07 17:52:27 -0700148 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800149 //printf("%s: pid %d, get service done\n", __func__, m_pid);
150 }
151 virtual void TearDown() {
152 status_t ret;
153 Parcel data, reply;
154 int exitStatus;
155 pid_t pid;
156
157 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700158 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800159 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
160 EXPECT_EQ(0, ret);
161 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
162 EXPECT_EQ(0, ret);
163 }
164 if (m_serverpid > 0) {
165 //printf("wait for %d\n", m_pids[i]);
166 pid = wait(&exitStatus);
167 EXPECT_EQ(m_serverpid, pid);
168 EXPECT_TRUE(WIFEXITED(exitStatus));
169 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
170 }
171 }
172
173 pid_t m_serverpid;
174 sp<IBinder> m_server;
175};
176
177class BinderLibTest : public ::testing::Test {
178 public:
179 virtual void SetUp() {
180 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
181 }
182 virtual void TearDown() {
183 }
184 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200185 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800186 {
187 int ret;
188 int32_t id;
189 Parcel data, reply;
190 sp<IBinder> binder;
191
Martijn Coenen45b07b42017-08-09 12:07:45 +0200192 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800193 EXPECT_EQ(NO_ERROR, ret);
194
Yi Kongfdd8da92018-06-07 17:52:27 -0700195 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800196 binder = reply.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700197 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800198 ret = reply.readInt32(&id);
199 EXPECT_EQ(NO_ERROR, ret);
200 if (idPtr)
201 *idPtr = id;
202 return binder;
203 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200204
Yi Kongfdd8da92018-06-07 17:52:27 -0700205 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200206 {
207 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
208 }
209
Yi Kongfdd8da92018-06-07 17:52:27 -0700210 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200211 {
212 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
213 }
214
Riley Andrews06b01ad2014-12-18 12:10:08 -0800215 void waitForReadData(int fd, int timeout_ms) {
216 int ret;
217 pollfd pfd = pollfd();
218
219 pfd.fd = fd;
220 pfd.events = POLLIN;
221 ret = poll(&pfd, 1, timeout_ms);
222 EXPECT_EQ(1, ret);
223 }
224
225 sp<IBinder> m_server;
226};
227
228class BinderLibTestBundle : public Parcel
229{
230 public:
231 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800232 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800233 int32_t mark;
234 int32_t bundleLen;
235 size_t pos;
236
237 if (source->readInt32(&mark))
238 return;
239 if (mark != MARK_START)
240 return;
241 if (source->readInt32(&bundleLen))
242 return;
243 pos = source->dataPosition();
244 if (Parcel::appendFrom(source, pos, bundleLen))
245 return;
246 source->setDataPosition(pos + bundleLen);
247 if (source->readInt32(&mark))
248 return;
249 if (mark != MARK_END)
250 return;
251 m_isValid = true;
252 setDataPosition(0);
253 }
254 void appendTo(Parcel *dest) {
255 dest->writeInt32(MARK_START);
256 dest->writeInt32(dataSize());
257 dest->appendFrom(this, 0, dataSize());
258 dest->writeInt32(MARK_END);
259 };
260 bool isValid(void) {
261 return m_isValid;
262 }
263 private:
264 enum {
265 MARK_START = B_PACK_CHARS('B','T','B','S'),
266 MARK_END = B_PACK_CHARS('B','T','B','E'),
267 };
268 bool m_isValid;
269};
270
271class BinderLibTestEvent
272{
273 public:
274 BinderLibTestEvent(void)
275 : m_eventTriggered(false)
276 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700277 pthread_mutex_init(&m_waitMutex, nullptr);
278 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800279 }
280 int waitEvent(int timeout_s)
281 {
282 int ret;
283 pthread_mutex_lock(&m_waitMutex);
284 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800285 struct timespec ts;
286 clock_gettime(CLOCK_REALTIME, &ts);
287 ts.tv_sec += timeout_s;
288 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800289 }
290 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
291 pthread_mutex_unlock(&m_waitMutex);
292 return ret;
293 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200294 pthread_t getTriggeringThread()
295 {
296 return m_triggeringThread;
297 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800298 protected:
299 void triggerEvent(void) {
300 pthread_mutex_lock(&m_waitMutex);
301 pthread_cond_signal(&m_waitCond);
302 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200303 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800304 pthread_mutex_unlock(&m_waitMutex);
305 };
306 private:
307 pthread_mutex_t m_waitMutex;
308 pthread_cond_t m_waitCond;
309 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200310 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800311};
312
313class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
314{
315 public:
316 BinderLibTestCallBack()
317 : m_result(NOT_ENOUGH_DATA)
Yi Kongfdd8da92018-06-07 17:52:27 -0700318 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800319 {
320 }
321 status_t getResult(void)
322 {
323 return m_result;
324 }
325
326 private:
327 virtual status_t onTransact(uint32_t code,
328 const Parcel& data, Parcel* reply,
329 uint32_t flags = 0)
330 {
331 (void)reply;
332 (void)flags;
333 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200334 case BINDER_LIB_TEST_CALL_BACK: {
335 status_t status = data.readInt32(&m_result);
336 if (status != NO_ERROR) {
337 m_result = status;
338 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800339 triggerEvent();
340 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200341 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700342 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
343 sp<IBinder> server;
344 int ret;
345 const uint8_t *buf = data.data();
346 size_t size = data.dataSize();
347 if (m_prev_end) {
348 /* 64-bit kernel needs at most 8 bytes to align buffer end */
349 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
350 } else {
351 EXPECT_TRUE(IsPageAligned((void *)buf));
352 }
353
354 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
355
356 if (size > 0) {
357 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
358 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
359 data, reply);
360 EXPECT_EQ(NO_ERROR, ret);
361 }
362 return NO_ERROR;
363 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800364 default:
365 return UNKNOWN_TRANSACTION;
366 }
367 }
368
369 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700370 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800371};
372
373class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
374{
375 private:
376 virtual void binderDied(const wp<IBinder>& who) {
377 (void)who;
378 triggerEvent();
379 };
380};
381
382TEST_F(BinderLibTest, NopTransaction) {
383 status_t ret;
384 Parcel data, reply;
385 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
386 EXPECT_EQ(NO_ERROR, ret);
387}
388
389TEST_F(BinderLibTest, SetError) {
390 int32_t testValue[] = { 0, -123, 123 };
391 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
392 status_t ret;
393 Parcel data, reply;
394 data.writeInt32(testValue[i]);
395 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
396 EXPECT_EQ(testValue[i], ret);
397 }
398}
399
400TEST_F(BinderLibTest, GetId) {
401 status_t ret;
402 int32_t id;
403 Parcel data, reply;
404 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
405 EXPECT_EQ(NO_ERROR, ret);
406 ret = reply.readInt32(&id);
407 EXPECT_EQ(NO_ERROR, ret);
408 EXPECT_EQ(0, id);
409}
410
411TEST_F(BinderLibTest, PtrSize) {
412 status_t ret;
413 int32_t ptrsize;
414 Parcel data, reply;
415 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700416 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800417 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
418 EXPECT_EQ(NO_ERROR, ret);
419 ret = reply.readInt32(&ptrsize);
420 EXPECT_EQ(NO_ERROR, ret);
421 RecordProperty("TestPtrSize", sizeof(void *));
422 RecordProperty("ServerPtrSize", sizeof(void *));
423}
424
425TEST_F(BinderLibTest, IndirectGetId2)
426{
427 status_t ret;
428 int32_t id;
429 int32_t count;
430 Parcel data, reply;
431 int32_t serverId[3];
432
433 data.writeInt32(ARRAY_SIZE(serverId));
434 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
435 sp<IBinder> server;
436 BinderLibTestBundle datai;
437
438 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700439 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800440 data.writeStrongBinder(server);
441 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
442 datai.appendTo(&data);
443 }
444
445 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
446 ASSERT_EQ(NO_ERROR, ret);
447
448 ret = reply.readInt32(&id);
449 ASSERT_EQ(NO_ERROR, ret);
450 EXPECT_EQ(0, id);
451
452 ret = reply.readInt32(&count);
453 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700454 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800455
456 for (size_t i = 0; i < (size_t)count; i++) {
457 BinderLibTestBundle replyi(&reply);
458 EXPECT_TRUE(replyi.isValid());
459 ret = replyi.readInt32(&id);
460 EXPECT_EQ(NO_ERROR, ret);
461 EXPECT_EQ(serverId[i], id);
462 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
463 }
464
465 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
466}
467
468TEST_F(BinderLibTest, IndirectGetId3)
469{
470 status_t ret;
471 int32_t id;
472 int32_t count;
473 Parcel data, reply;
474 int32_t serverId[3];
475
476 data.writeInt32(ARRAY_SIZE(serverId));
477 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
478 sp<IBinder> server;
479 BinderLibTestBundle datai;
480 BinderLibTestBundle datai2;
481
482 server = addServer(&serverId[i]);
Yi Kongfdd8da92018-06-07 17:52:27 -0700483 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800484 data.writeStrongBinder(server);
485 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
486
487 datai.writeInt32(1);
488 datai.writeStrongBinder(m_server);
489 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
490 datai2.appendTo(&datai);
491
492 datai.appendTo(&data);
493 }
494
495 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
496 ASSERT_EQ(NO_ERROR, ret);
497
498 ret = reply.readInt32(&id);
499 ASSERT_EQ(NO_ERROR, ret);
500 EXPECT_EQ(0, id);
501
502 ret = reply.readInt32(&count);
503 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700504 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800505
506 for (size_t i = 0; i < (size_t)count; i++) {
507 int32_t counti;
508
509 BinderLibTestBundle replyi(&reply);
510 EXPECT_TRUE(replyi.isValid());
511 ret = replyi.readInt32(&id);
512 EXPECT_EQ(NO_ERROR, ret);
513 EXPECT_EQ(serverId[i], id);
514
515 ret = replyi.readInt32(&counti);
516 ASSERT_EQ(NO_ERROR, ret);
517 EXPECT_EQ(1, counti);
518
519 BinderLibTestBundle replyi2(&replyi);
520 EXPECT_TRUE(replyi2.isValid());
521 ret = replyi2.readInt32(&id);
522 EXPECT_EQ(NO_ERROR, ret);
523 EXPECT_EQ(0, id);
524 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
525
526 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
527 }
528
529 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
530}
531
532TEST_F(BinderLibTest, CallBack)
533{
534 status_t ret;
535 Parcel data, reply;
536 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
537 data.writeStrongBinder(callBack);
538 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
539 EXPECT_EQ(NO_ERROR, ret);
540 ret = callBack->waitEvent(5);
541 EXPECT_EQ(NO_ERROR, ret);
542 ret = callBack->getResult();
543 EXPECT_EQ(NO_ERROR, ret);
544}
545
546TEST_F(BinderLibTest, AddServer)
547{
548 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700549 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800550}
551
Riley Andrews06b01ad2014-12-18 12:10:08 -0800552TEST_F(BinderLibTest, DeathNotificationStrongRef)
553{
554 status_t ret;
555 sp<IBinder> sbinder;
556
557 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
558
559 {
560 sp<IBinder> binder = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700561 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800562 ret = binder->linkToDeath(testDeathRecipient);
563 EXPECT_EQ(NO_ERROR, ret);
564 sbinder = binder;
565 }
566 {
567 Parcel data, reply;
568 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
569 EXPECT_EQ(0, ret);
570 }
571 IPCThreadState::self()->flushCommands();
572 ret = testDeathRecipient->waitEvent(5);
573 EXPECT_EQ(NO_ERROR, ret);
574 ret = sbinder->unlinkToDeath(testDeathRecipient);
575 EXPECT_EQ(DEAD_OBJECT, ret);
576}
577
578TEST_F(BinderLibTest, DeathNotificationMultiple)
579{
580 status_t ret;
581 const int clientcount = 2;
582 sp<IBinder> target;
583 sp<IBinder> linkedclient[clientcount];
584 sp<BinderLibTestCallBack> callBack[clientcount];
585 sp<IBinder> passiveclient[clientcount];
586
587 target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700588 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800589 for (int i = 0; i < clientcount; i++) {
590 {
591 Parcel data, reply;
592
593 linkedclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700594 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800595 callBack[i] = new BinderLibTestCallBack();
596 data.writeStrongBinder(target);
597 data.writeStrongBinder(callBack[i]);
598 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
599 EXPECT_EQ(NO_ERROR, ret);
600 }
601 {
602 Parcel data, reply;
603
604 passiveclient[i] = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700605 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800606 data.writeStrongBinder(target);
607 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
608 EXPECT_EQ(NO_ERROR, ret);
609 }
610 }
611 {
612 Parcel data, reply;
613 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
614 EXPECT_EQ(0, ret);
615 }
616
617 for (int i = 0; i < clientcount; i++) {
618 ret = callBack[i]->waitEvent(5);
619 EXPECT_EQ(NO_ERROR, ret);
620 ret = callBack[i]->getResult();
621 EXPECT_EQ(NO_ERROR, ret);
622 }
623}
624
Martijn Coenenf7100e42017-07-31 12:14:09 +0200625TEST_F(BinderLibTest, DeathNotificationThread)
626{
627 status_t ret;
628 sp<BinderLibTestCallBack> callback;
629 sp<IBinder> target = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700630 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200631 sp<IBinder> client = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700632 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200633
634 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
635
636 ret = target->linkToDeath(testDeathRecipient);
637 EXPECT_EQ(NO_ERROR, ret);
638
639 {
640 Parcel data, reply;
641 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
642 EXPECT_EQ(0, ret);
643 }
644
645 /* Make sure it's dead */
646 testDeathRecipient->waitEvent(5);
647
648 /* Now, pass the ref to another process and ask that process to
649 * call linkToDeath() on it, and wait for a response. This tests
650 * two things:
651 * 1) You still get death notifications when calling linkToDeath()
652 * on a ref that is already dead when it was passed to you.
653 * 2) That death notifications are not directly pushed to the thread
654 * registering them, but to the threadpool (proc workqueue) instead.
655 *
656 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
657 * is blocked on a condition variable waiting for the death notification to be
658 * called; therefore, that thread is not available for handling proc work.
659 * So, if the death notification was pushed to the thread workqueue, the callback
660 * would never be called, and the test would timeout and fail.
661 *
662 * Note that we can't do this part of the test from this thread itself, because
663 * the binder driver would only push death notifications to the thread if
664 * it is a looper thread, which this thread is not.
665 *
666 * See b/23525545 for details.
667 */
668 {
669 Parcel data, reply;
670
671 callback = new BinderLibTestCallBack();
672 data.writeStrongBinder(target);
673 data.writeStrongBinder(callback);
674 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
675 EXPECT_EQ(NO_ERROR, ret);
676 }
677
678 ret = callback->waitEvent(5);
679 EXPECT_EQ(NO_ERROR, ret);
680 ret = callback->getResult();
681 EXPECT_EQ(NO_ERROR, ret);
682}
683
Riley Andrews06b01ad2014-12-18 12:10:08 -0800684TEST_F(BinderLibTest, PassFile) {
685 int ret;
686 int pipefd[2];
687 uint8_t buf[1] = { 0 };
688 uint8_t write_value = 123;
689
690 ret = pipe2(pipefd, O_NONBLOCK);
691 ASSERT_EQ(0, ret);
692
693 {
694 Parcel data, reply;
695 uint8_t writebuf[1] = { write_value };
696
697 ret = data.writeFileDescriptor(pipefd[1], true);
698 EXPECT_EQ(NO_ERROR, ret);
699
700 ret = data.writeInt32(sizeof(writebuf));
701 EXPECT_EQ(NO_ERROR, ret);
702
703 ret = data.write(writebuf, sizeof(writebuf));
704 EXPECT_EQ(NO_ERROR, ret);
705
706 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
707 EXPECT_EQ(NO_ERROR, ret);
708 }
709
710 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700711 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800712 EXPECT_EQ(write_value, buf[0]);
713
714 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
715
716 ret = read(pipefd[0], buf, sizeof(buf));
717 EXPECT_EQ(0, ret);
718
719 close(pipefd[0]);
720}
721
Ryo Hashimotobf551892018-05-31 16:58:35 +0900722TEST_F(BinderLibTest, PassParcelFileDescriptor) {
723 const int datasize = 123;
724 std::vector<uint8_t> writebuf(datasize);
725 for (size_t i = 0; i < writebuf.size(); ++i) {
726 writebuf[i] = i;
727 }
728
729 android::base::unique_fd read_end, write_end;
730 {
731 int pipefd[2];
732 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
733 read_end.reset(pipefd[0]);
734 write_end.reset(pipefd[1]);
735 }
736 {
737 Parcel data;
738 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
739 write_end.reset();
740 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
741 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
742
743 Parcel reply;
744 EXPECT_EQ(NO_ERROR,
745 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
746 &reply));
747 }
748 std::vector<uint8_t> readbuf(datasize);
749 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
750 EXPECT_EQ(writebuf, readbuf);
751
752 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
753
754 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
755}
756
Riley Andrews06b01ad2014-12-18 12:10:08 -0800757TEST_F(BinderLibTest, PromoteLocal) {
758 sp<IBinder> strong = new BBinder();
759 wp<IBinder> weak = strong;
760 sp<IBinder> strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700761 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800762 EXPECT_EQ(strong, strong_from_weak);
Yi Kongfdd8da92018-06-07 17:52:27 -0700763 strong = nullptr;
764 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800765 strong_from_weak = weak.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700766 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800767}
768
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700769TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
770 status_t ret;
771 Parcel data, reply;
772
773 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
774 EXPECT_EQ(NO_ERROR, ret);
775
776 const flat_binder_object *fb = reply.readObject(false);
Yi Kongfdd8da92018-06-07 17:52:27 -0700777 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800778 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
779 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
780 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
781 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700782}
783
Connor O'Brien52be2c92016-09-20 14:18:08 -0700784TEST_F(BinderLibTest, FreedBinder) {
785 status_t ret;
786
787 sp<IBinder> server = addServer();
Yi Kongfdd8da92018-06-07 17:52:27 -0700788 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700789
790 __u32 freedHandle;
791 wp<IBinder> keepFreedBinder;
792 {
793 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700794 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
795 ASSERT_EQ(NO_ERROR, ret);
796 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
797 freedHandle = freed->handle;
798 /* Add a weak ref to the freed binder so the driver does not
799 * delete its reference to it - otherwise the transaction
800 * fails regardless of whether the driver is fixed.
801 */
Steven Morelande171d622019-07-17 16:06:01 -0700802 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700803 }
Steven Morelande171d622019-07-17 16:06:01 -0700804 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700805 {
806 Parcel data, reply;
807 data.writeStrongBinder(server);
808 /* Replace original handle with handle to the freed binder */
809 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
810 __u32 oldHandle = strong->handle;
811 strong->handle = freedHandle;
812 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
813 /* Returns DEAD_OBJECT (-32) if target crashes and
814 * FAILED_TRANSACTION if the driver rejects the invalid
815 * object.
816 */
817 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
818 /* Restore original handle so parcel destructor does not use
819 * the wrong handle.
820 */
821 strong->handle = oldHandle;
822 }
823}
824
Sherry Yang336cdd32017-07-24 14:12:27 -0700825TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
826 status_t ret;
827 Parcel data, reply;
828 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
829 for (int i = 0; i < 2; i++) {
830 BinderLibTestBundle datai;
831 datai.appendFrom(&data, 0, data.dataSize());
832
833 data.freeData();
834 data.writeInt32(1);
835 data.writeStrongBinder(callBack);
836 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
837
838 datai.appendTo(&data);
839 }
840 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
841 EXPECT_EQ(NO_ERROR, ret);
842}
843
Martijn Coenen45b07b42017-08-09 12:07:45 +0200844TEST_F(BinderLibTest, OnewayQueueing)
845{
846 status_t ret;
847 Parcel data, data2;
848
849 sp<IBinder> pollServer = addPollServer();
850
851 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
852 data.writeStrongBinder(callBack);
853 data.writeInt32(500000); // delay in us before calling back
854
855 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
856 data2.writeStrongBinder(callBack2);
857 data2.writeInt32(0); // delay in us
858
Yi Kongfdd8da92018-06-07 17:52:27 -0700859 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200860 EXPECT_EQ(NO_ERROR, ret);
861
862 // The delay ensures that this second transaction will end up on the async_todo list
863 // (for a single-threaded server)
Yi Kongfdd8da92018-06-07 17:52:27 -0700864 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200865 EXPECT_EQ(NO_ERROR, ret);
866
867 // The server will ensure that the two transactions are handled in the expected order;
868 // If the ordering is not as expected, an error will be returned through the callbacks.
869 ret = callBack->waitEvent(2);
870 EXPECT_EQ(NO_ERROR, ret);
871 ret = callBack->getResult();
872 EXPECT_EQ(NO_ERROR, ret);
873
874 ret = callBack2->waitEvent(2);
875 EXPECT_EQ(NO_ERROR, ret);
876 ret = callBack2->getResult();
877 EXPECT_EQ(NO_ERROR, ret);
878}
879
Riley Andrews06b01ad2014-12-18 12:10:08 -0800880class BinderLibTestService : public BBinder
881{
882 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800883 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800884 : m_id(id)
885 , m_nextServerId(id + 1)
886 , m_serverStartRequested(false)
Yi Kongfdd8da92018-06-07 17:52:27 -0700887 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800888 {
Yi Kongfdd8da92018-06-07 17:52:27 -0700889 pthread_mutex_init(&m_serverWaitMutex, nullptr);
890 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800891 }
892 ~BinderLibTestService()
893 {
894 exit(EXIT_SUCCESS);
895 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200896
897 void processPendingCall() {
Yi Kongfdd8da92018-06-07 17:52:27 -0700898 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +0200899 Parcel data;
900 data.writeInt32(NO_ERROR);
901 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kongfdd8da92018-06-07 17:52:27 -0700902 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200903 }
904 }
905
Riley Andrews06b01ad2014-12-18 12:10:08 -0800906 virtual status_t onTransact(uint32_t code,
907 const Parcel& data, Parcel* reply,
908 uint32_t flags = 0) {
909 //printf("%s: code %d\n", __func__, code);
910 (void)flags;
911
912 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
913 return PERMISSION_DENIED;
914 }
915 switch (code) {
916 case BINDER_LIB_TEST_REGISTER_SERVER: {
917 int32_t id;
918 sp<IBinder> binder;
919 id = data.readInt32();
920 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700921 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800922 return BAD_VALUE;
923 }
924
925 if (m_id != 0)
926 return INVALID_OPERATION;
927
928 pthread_mutex_lock(&m_serverWaitMutex);
929 if (m_serverStartRequested) {
930 m_serverStartRequested = false;
931 m_serverStarted = binder;
932 pthread_cond_signal(&m_serverWaitCond);
933 }
934 pthread_mutex_unlock(&m_serverWaitMutex);
935 return NO_ERROR;
936 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200937 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -0800938 case BINDER_LIB_TEST_ADD_SERVER: {
939 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800940 int serverid;
941
942 if (m_id != 0) {
943 return INVALID_OPERATION;
944 }
945 pthread_mutex_lock(&m_serverWaitMutex);
946 if (m_serverStartRequested) {
947 ret = -EBUSY;
948 } else {
949 serverid = m_nextServerId++;
950 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200951 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800952
953 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200954 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800955 pthread_mutex_lock(&m_serverWaitMutex);
956 }
957 if (ret > 0) {
958 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800959 struct timespec ts;
960 clock_gettime(CLOCK_REALTIME, &ts);
961 ts.tv_sec += 5;
962 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800963 }
964 if (m_serverStartRequested) {
965 m_serverStartRequested = false;
966 ret = -ETIMEDOUT;
967 } else {
968 reply->writeStrongBinder(m_serverStarted);
969 reply->writeInt32(serverid);
Yi Kongfdd8da92018-06-07 17:52:27 -0700970 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800971 ret = NO_ERROR;
972 }
973 } else if (ret >= 0) {
974 m_serverStartRequested = false;
975 ret = UNKNOWN_ERROR;
976 }
977 pthread_mutex_unlock(&m_serverWaitMutex);
978 return ret;
979 }
980 case BINDER_LIB_TEST_NOP_TRANSACTION:
981 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +0200982 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
983 // Note: this transaction is only designed for use with a
984 // poll() server. See comments around epoll_wait().
Yi Kongfdd8da92018-06-07 17:52:27 -0700985 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +0200986 // A callback was already pending; this means that
987 // we received a second call while still processing
988 // the first one. Fail the test.
989 sp<IBinder> callback = data.readStrongBinder();
990 Parcel data2;
991 data2.writeInt32(UNKNOWN_ERROR);
992
Yi Kongfdd8da92018-06-07 17:52:27 -0700993 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200994 } else {
995 m_callback = data.readStrongBinder();
996 int32_t delayUs = data.readInt32();
997 /*
998 * It's necessary that we sleep here, so the next
999 * transaction the caller makes will be queued to
1000 * the async queue.
1001 */
1002 usleep(delayUs);
1003
1004 /*
1005 * Now when we return, libbinder will tell the kernel
1006 * we are done with this transaction, and the kernel
1007 * can move the queued transaction to either the
1008 * thread todo worklist (for kernels without the fix),
1009 * or the proc todo worklist. In case of the former,
1010 * the next outbound call will pick up the pending
1011 * transaction, which leads to undesired reentrant
1012 * behavior. This is caught in the if() branch above.
1013 */
1014 }
1015
1016 return NO_ERROR;
1017 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001018 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1019 Parcel data2, reply2;
1020 sp<IBinder> binder;
1021 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001022 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001023 return BAD_VALUE;
1024 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001025 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001026 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1027 return NO_ERROR;
1028 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001029 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1030 reply->writeStrongBinder(this);
1031 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001032 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1033 reply->writeInt32(m_id);
1034 return NO_ERROR;
1035 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1036 int32_t count;
1037 uint32_t indirect_code;
1038 sp<IBinder> binder;
1039
1040 count = data.readInt32();
1041 reply->writeInt32(m_id);
1042 reply->writeInt32(count);
1043 for (int i = 0; i < count; i++) {
1044 binder = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001045 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001046 return BAD_VALUE;
1047 }
1048 indirect_code = data.readInt32();
1049 BinderLibTestBundle data2(&data);
1050 if (!data2.isValid()) {
1051 return BAD_VALUE;
1052 }
1053 BinderLibTestBundle reply2;
1054 binder->transact(indirect_code, data2, &reply2);
1055 reply2.appendTo(reply);
1056 }
1057 return NO_ERROR;
1058 }
1059 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1060 reply->setError(data.readInt32());
1061 return NO_ERROR;
1062 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1063 reply->writeInt32(sizeof(void *));
1064 return NO_ERROR;
1065 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1066 return NO_ERROR;
1067 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1068 m_strongRef = data.readStrongBinder();
1069 return NO_ERROR;
1070 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1071 int ret;
1072 Parcel data2, reply2;
1073 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1074 sp<IBinder> target;
1075 sp<IBinder> callback;
1076
1077 target = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001078 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001079 return BAD_VALUE;
1080 }
1081 callback = data.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -07001082 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001083 return BAD_VALUE;
1084 }
1085 ret = target->linkToDeath(testDeathRecipient);
1086 if (ret == NO_ERROR)
1087 ret = testDeathRecipient->waitEvent(5);
1088 data2.writeInt32(ret);
1089 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1090 return NO_ERROR;
1091 }
1092 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1093 int ret;
1094 int32_t size;
1095 const void *buf;
1096 int fd;
1097
1098 fd = data.readFileDescriptor();
1099 if (fd < 0) {
1100 return BAD_VALUE;
1101 }
1102 ret = data.readInt32(&size);
1103 if (ret != NO_ERROR) {
1104 return ret;
1105 }
1106 buf = data.readInplace(size);
Yi Kongfdd8da92018-06-07 17:52:27 -07001107 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001108 return BAD_VALUE;
1109 }
1110 ret = write(fd, buf, size);
1111 if (ret != size)
1112 return UNKNOWN_ERROR;
1113 return NO_ERROR;
1114 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001115 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1116 int ret;
1117 int32_t size;
1118 const void *buf;
1119 android::base::unique_fd fd;
1120
1121 ret = data.readUniqueParcelFileDescriptor(&fd);
1122 if (ret != NO_ERROR) {
1123 return ret;
1124 }
1125 ret = data.readInt32(&size);
1126 if (ret != NO_ERROR) {
1127 return ret;
1128 }
1129 buf = data.readInplace(size);
Yi Kong87d465c2018-07-24 01:14:06 -07001130 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001131 return BAD_VALUE;
1132 }
1133 ret = write(fd.get(), buf, size);
1134 if (ret != size) return UNKNOWN_ERROR;
1135 return NO_ERROR;
1136 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001137 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1138 alarm(10);
1139 return NO_ERROR;
1140 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kongfdd8da92018-06-07 17:52:27 -07001141 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001142 ;
1143 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001144 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001145 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001146 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001147 return NO_ERROR;
1148 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001149 default:
1150 return UNKNOWN_TRANSACTION;
1151 };
1152 }
1153 private:
1154 int32_t m_id;
1155 int32_t m_nextServerId;
1156 pthread_mutex_t m_serverWaitMutex;
1157 pthread_cond_t m_serverWaitCond;
1158 bool m_serverStartRequested;
1159 sp<IBinder> m_serverStarted;
1160 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001161 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001162};
1163
Martijn Coenen45b07b42017-08-09 12:07:45 +02001164int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001165{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001166 binderLibTestServiceName += String16(binderserversuffix);
1167
Riley Andrews06b01ad2014-12-18 12:10:08 -08001168 status_t ret;
1169 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001170 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001171 {
1172 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001173 /*
1174 * We need this below, but can't hold a sp<> because it prevents the
1175 * node from being cleaned up automatically. It's safe in this case
1176 * because of how the tests are written.
1177 */
1178 testServicePtr = testService.get();
1179
Riley Andrews06b01ad2014-12-18 12:10:08 -08001180 if (index == 0) {
1181 ret = sm->addService(binderLibTestServiceName, testService);
1182 } else {
1183 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1184 Parcel data, reply;
1185 data.writeInt32(index);
1186 data.writeStrongBinder(testService);
1187
1188 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1189 }
1190 }
1191 write(readypipefd, &ret, sizeof(ret));
1192 close(readypipefd);
1193 //printf("%s: ret %d\n", __func__, ret);
1194 if (ret)
1195 return 1;
1196 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001197 if (usePoll) {
1198 int fd;
1199 struct epoll_event ev;
1200 int epoll_fd;
1201 IPCThreadState::self()->setupPolling(&fd);
1202 if (fd < 0) {
1203 return 1;
1204 }
1205 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1206
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001207 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001208 if (epoll_fd == -1) {
1209 return 1;
1210 }
1211
1212 ev.events = EPOLLIN;
1213 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1214 return 1;
1215 }
1216
1217 while (1) {
1218 /*
1219 * We simulate a single-threaded process using the binder poll
1220 * interface; besides handling binder commands, it can also
1221 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001222 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001223 *
1224 * processPendingCall() will then issue that transaction.
1225 */
1226 struct epoll_event events[1];
1227 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1228 if (numEvents < 0) {
1229 if (errno == EINTR) {
1230 continue;
1231 }
1232 return 1;
1233 }
1234 if (numEvents > 0) {
1235 IPCThreadState::self()->handlePolledCommands();
1236 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1237 testServicePtr->processPendingCall();
1238 }
1239 }
1240 } else {
1241 ProcessState::self()->startThreadPool();
1242 IPCThreadState::self()->joinThreadPool();
1243 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001244 //printf("%s: joinThreadPool returned\n", __func__);
1245 return 1; /* joinThreadPool should not return */
1246}
1247
1248int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001249 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001250 binderservername = argv[2];
1251 } else {
1252 binderservername = argv[0];
1253 }
1254
Martijn Coenen45b07b42017-08-09 12:07:45 +02001255 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1256 binderserversuffix = argv[5];
1257 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001258 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001259 binderserversuffix = new char[16];
1260 snprintf(binderserversuffix, 16, "%d", getpid());
1261 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001262
1263 ::testing::InitGoogleTest(&argc, argv);
1264 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1265 ProcessState::self()->startThreadPool();
1266 return RUN_ALL_TESTS();
1267}