blob: 917751ef34daee53a9d278eb8e70c5905e034366 [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
Steven Morelandd63ed9d2019-09-04 18:15:25 -070031#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020032#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080033#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020034
Steven Morelandf9f3de22020-05-06 17:14:39 -070035#include "binderAbiHelper.h"
36
Riley Andrews06b01ad2014-12-18 12:10:08 -080037#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
38
39using namespace android;
40
Sherry Yang336cdd32017-07-24 14:12:27 -070041static ::testing::AssertionResult IsPageAligned(void *buf) {
42 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
43 return ::testing::AssertionSuccess();
44 else
45 return ::testing::AssertionFailure() << buf << " is not page aligned";
46}
47
Riley Andrews06b01ad2014-12-18 12:10:08 -080048static testing::Environment* binder_env;
49static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070050static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080051static char binderserverarg[] = "--binderserver";
52
Steven Morelandb96ea772020-07-16 22:43:02 +000053static constexpr int kSchedPolicy = SCHED_RR;
54static constexpr int kSchedPriority = 7;
55
Riley Andrews06b01ad2014-12-18 12:10:08 -080056static String16 binderLibTestServiceName = String16("test.binderLib");
57
58enum BinderLibTestTranscationCode {
59 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
60 BINDER_LIB_TEST_REGISTER_SERVER,
61 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020062 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080063 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070064 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020065 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080066 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070067 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080068 BINDER_LIB_TEST_GET_ID_TRANSACTION,
69 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
70 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
71 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
72 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
73 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
74 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090075 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080076 BINDER_LIB_TEST_EXIT_TRANSACTION,
77 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
78 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070079 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010080 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandb96ea772020-07-16 22:43:02 +000081 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080082 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020083 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080084};
85
Martijn Coenen45b07b42017-08-09 12:07:45 +020086pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080087{
88 int ret;
89 pid_t pid;
90 status_t status;
91 int pipefd[2];
92 char stri[16];
93 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020094 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080095 char *childargv[] = {
96 binderservername,
97 binderserverarg,
98 stri,
99 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200100 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700101 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700102 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800103 };
104
105 ret = pipe(pipefd);
106 if (ret < 0)
107 return ret;
108
109 snprintf(stri, sizeof(stri), "%d", arg2);
110 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200111 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800112
113 pid = fork();
114 if (pid == -1)
115 return pid;
116 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800117 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800118 close(pipefd[0]);
119 execv(binderservername, childargv);
120 status = -errno;
121 write(pipefd[1], &status, sizeof(status));
122 fprintf(stderr, "execv failed, %s\n", strerror(errno));
123 _exit(EXIT_FAILURE);
124 }
125 close(pipefd[1]);
126 ret = read(pipefd[0], &status, sizeof(status));
127 //printf("pipe read returned %d, status %d\n", ret, status);
128 close(pipefd[0]);
129 if (ret == sizeof(status)) {
130 ret = status;
131 } else {
132 kill(pid, SIGKILL);
133 if (ret >= 0) {
134 ret = NO_INIT;
135 }
136 }
137 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700138 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800139 return ret;
140 }
141 return pid;
142}
143
144class BinderLibTestEnv : public ::testing::Environment {
145 public:
146 BinderLibTestEnv() {}
147 sp<IBinder> getServer(void) {
148 return m_server;
149 }
150
151 private:
152 virtual void SetUp() {
153 m_serverpid = start_server_process(0);
154 //printf("m_serverpid %d\n", m_serverpid);
155 ASSERT_GT(m_serverpid, 0);
156
157 sp<IServiceManager> sm = defaultServiceManager();
158 //printf("%s: pid %d, get service\n", __func__, m_pid);
159 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700160 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800161 //printf("%s: pid %d, get service done\n", __func__, m_pid);
162 }
163 virtual void TearDown() {
164 status_t ret;
165 Parcel data, reply;
166 int exitStatus;
167 pid_t pid;
168
169 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700170 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800171 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
172 EXPECT_EQ(0, ret);
173 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
174 EXPECT_EQ(0, ret);
175 }
176 if (m_serverpid > 0) {
177 //printf("wait for %d\n", m_pids[i]);
178 pid = wait(&exitStatus);
179 EXPECT_EQ(m_serverpid, pid);
180 EXPECT_TRUE(WIFEXITED(exitStatus));
181 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
182 }
183 }
184
185 pid_t m_serverpid;
186 sp<IBinder> m_server;
187};
188
189class BinderLibTest : public ::testing::Test {
190 public:
191 virtual void SetUp() {
192 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000193 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800194 }
195 virtual void TearDown() {
196 }
197 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200198 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800199 {
200 int ret;
201 int32_t id;
202 Parcel data, reply;
203 sp<IBinder> binder;
204
Martijn Coenen45b07b42017-08-09 12:07:45 +0200205 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800206 EXPECT_EQ(NO_ERROR, ret);
207
Yi Kong91635562018-06-07 14:38:36 -0700208 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700210 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800211 ret = reply.readInt32(&id);
212 EXPECT_EQ(NO_ERROR, ret);
213 if (idPtr)
214 *idPtr = id;
215 return binder;
216 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200217
Yi Kong91635562018-06-07 14:38:36 -0700218 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200219 {
220 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
221 }
222
Yi Kong91635562018-06-07 14:38:36 -0700223 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200224 {
225 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
226 }
227
Riley Andrews06b01ad2014-12-18 12:10:08 -0800228 void waitForReadData(int fd, int timeout_ms) {
229 int ret;
230 pollfd pfd = pollfd();
231
232 pfd.fd = fd;
233 pfd.events = POLLIN;
234 ret = poll(&pfd, 1, timeout_ms);
235 EXPECT_EQ(1, ret);
236 }
237
238 sp<IBinder> m_server;
239};
240
241class BinderLibTestBundle : public Parcel
242{
243 public:
244 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800245 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800246 int32_t mark;
247 int32_t bundleLen;
248 size_t pos;
249
250 if (source->readInt32(&mark))
251 return;
252 if (mark != MARK_START)
253 return;
254 if (source->readInt32(&bundleLen))
255 return;
256 pos = source->dataPosition();
257 if (Parcel::appendFrom(source, pos, bundleLen))
258 return;
259 source->setDataPosition(pos + bundleLen);
260 if (source->readInt32(&mark))
261 return;
262 if (mark != MARK_END)
263 return;
264 m_isValid = true;
265 setDataPosition(0);
266 }
267 void appendTo(Parcel *dest) {
268 dest->writeInt32(MARK_START);
269 dest->writeInt32(dataSize());
270 dest->appendFrom(this, 0, dataSize());
271 dest->writeInt32(MARK_END);
272 };
273 bool isValid(void) {
274 return m_isValid;
275 }
276 private:
277 enum {
278 MARK_START = B_PACK_CHARS('B','T','B','S'),
279 MARK_END = B_PACK_CHARS('B','T','B','E'),
280 };
281 bool m_isValid;
282};
283
284class BinderLibTestEvent
285{
286 public:
287 BinderLibTestEvent(void)
288 : m_eventTriggered(false)
289 {
Yi Kong91635562018-06-07 14:38:36 -0700290 pthread_mutex_init(&m_waitMutex, nullptr);
291 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800292 }
293 int waitEvent(int timeout_s)
294 {
295 int ret;
296 pthread_mutex_lock(&m_waitMutex);
297 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800298 struct timespec ts;
299 clock_gettime(CLOCK_REALTIME, &ts);
300 ts.tv_sec += timeout_s;
301 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800302 }
303 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
304 pthread_mutex_unlock(&m_waitMutex);
305 return ret;
306 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200307 pthread_t getTriggeringThread()
308 {
309 return m_triggeringThread;
310 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800311 protected:
312 void triggerEvent(void) {
313 pthread_mutex_lock(&m_waitMutex);
314 pthread_cond_signal(&m_waitCond);
315 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200316 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800317 pthread_mutex_unlock(&m_waitMutex);
318 };
319 private:
320 pthread_mutex_t m_waitMutex;
321 pthread_cond_t m_waitCond;
322 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200323 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800324};
325
326class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
327{
328 public:
329 BinderLibTestCallBack()
330 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700331 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800332 {
333 }
334 status_t getResult(void)
335 {
336 return m_result;
337 }
338
339 private:
340 virtual status_t onTransact(uint32_t code,
341 const Parcel& data, Parcel* reply,
342 uint32_t flags = 0)
343 {
344 (void)reply;
345 (void)flags;
346 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200347 case BINDER_LIB_TEST_CALL_BACK: {
348 status_t status = data.readInt32(&m_result);
349 if (status != NO_ERROR) {
350 m_result = status;
351 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800352 triggerEvent();
353 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200354 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700355 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
356 sp<IBinder> server;
357 int ret;
358 const uint8_t *buf = data.data();
359 size_t size = data.dataSize();
360 if (m_prev_end) {
361 /* 64-bit kernel needs at most 8 bytes to align buffer end */
362 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
363 } else {
364 EXPECT_TRUE(IsPageAligned((void *)buf));
365 }
366
367 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
368
369 if (size > 0) {
370 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
371 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
372 data, reply);
373 EXPECT_EQ(NO_ERROR, ret);
374 }
375 return NO_ERROR;
376 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800377 default:
378 return UNKNOWN_TRANSACTION;
379 }
380 }
381
382 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700383 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800384};
385
386class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
387{
388 private:
389 virtual void binderDied(const wp<IBinder>& who) {
390 (void)who;
391 triggerEvent();
392 };
393};
394
395TEST_F(BinderLibTest, NopTransaction) {
396 status_t ret;
397 Parcel data, reply;
398 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
399 EXPECT_EQ(NO_ERROR, ret);
400}
401
402TEST_F(BinderLibTest, SetError) {
403 int32_t testValue[] = { 0, -123, 123 };
404 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
405 status_t ret;
406 Parcel data, reply;
407 data.writeInt32(testValue[i]);
408 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
409 EXPECT_EQ(testValue[i], ret);
410 }
411}
412
413TEST_F(BinderLibTest, GetId) {
414 status_t ret;
415 int32_t id;
416 Parcel data, reply;
417 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
418 EXPECT_EQ(NO_ERROR, ret);
419 ret = reply.readInt32(&id);
420 EXPECT_EQ(NO_ERROR, ret);
421 EXPECT_EQ(0, id);
422}
423
424TEST_F(BinderLibTest, PtrSize) {
425 status_t ret;
426 int32_t ptrsize;
427 Parcel data, reply;
428 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700429 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800430 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
431 EXPECT_EQ(NO_ERROR, ret);
432 ret = reply.readInt32(&ptrsize);
433 EXPECT_EQ(NO_ERROR, ret);
434 RecordProperty("TestPtrSize", sizeof(void *));
435 RecordProperty("ServerPtrSize", sizeof(void *));
436}
437
438TEST_F(BinderLibTest, IndirectGetId2)
439{
440 status_t ret;
441 int32_t id;
442 int32_t count;
443 Parcel data, reply;
444 int32_t serverId[3];
445
446 data.writeInt32(ARRAY_SIZE(serverId));
447 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
448 sp<IBinder> server;
449 BinderLibTestBundle datai;
450
451 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700452 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800453 data.writeStrongBinder(server);
454 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
455 datai.appendTo(&data);
456 }
457
458 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
459 ASSERT_EQ(NO_ERROR, ret);
460
461 ret = reply.readInt32(&id);
462 ASSERT_EQ(NO_ERROR, ret);
463 EXPECT_EQ(0, id);
464
465 ret = reply.readInt32(&count);
466 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700467 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800468
469 for (size_t i = 0; i < (size_t)count; i++) {
470 BinderLibTestBundle replyi(&reply);
471 EXPECT_TRUE(replyi.isValid());
472 ret = replyi.readInt32(&id);
473 EXPECT_EQ(NO_ERROR, ret);
474 EXPECT_EQ(serverId[i], id);
475 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
476 }
477
478 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
479}
480
481TEST_F(BinderLibTest, IndirectGetId3)
482{
483 status_t ret;
484 int32_t id;
485 int32_t count;
486 Parcel data, reply;
487 int32_t serverId[3];
488
489 data.writeInt32(ARRAY_SIZE(serverId));
490 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
491 sp<IBinder> server;
492 BinderLibTestBundle datai;
493 BinderLibTestBundle datai2;
494
495 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700496 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800497 data.writeStrongBinder(server);
498 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
499
500 datai.writeInt32(1);
501 datai.writeStrongBinder(m_server);
502 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
503 datai2.appendTo(&datai);
504
505 datai.appendTo(&data);
506 }
507
508 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
509 ASSERT_EQ(NO_ERROR, ret);
510
511 ret = reply.readInt32(&id);
512 ASSERT_EQ(NO_ERROR, ret);
513 EXPECT_EQ(0, id);
514
515 ret = reply.readInt32(&count);
516 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700517 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800518
519 for (size_t i = 0; i < (size_t)count; i++) {
520 int32_t counti;
521
522 BinderLibTestBundle replyi(&reply);
523 EXPECT_TRUE(replyi.isValid());
524 ret = replyi.readInt32(&id);
525 EXPECT_EQ(NO_ERROR, ret);
526 EXPECT_EQ(serverId[i], id);
527
528 ret = replyi.readInt32(&counti);
529 ASSERT_EQ(NO_ERROR, ret);
530 EXPECT_EQ(1, counti);
531
532 BinderLibTestBundle replyi2(&replyi);
533 EXPECT_TRUE(replyi2.isValid());
534 ret = replyi2.readInt32(&id);
535 EXPECT_EQ(NO_ERROR, ret);
536 EXPECT_EQ(0, id);
537 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
538
539 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
540 }
541
542 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
543}
544
545TEST_F(BinderLibTest, CallBack)
546{
547 status_t ret;
548 Parcel data, reply;
549 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
550 data.writeStrongBinder(callBack);
551 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
552 EXPECT_EQ(NO_ERROR, ret);
553 ret = callBack->waitEvent(5);
554 EXPECT_EQ(NO_ERROR, ret);
555 ret = callBack->getResult();
556 EXPECT_EQ(NO_ERROR, ret);
557}
558
559TEST_F(BinderLibTest, AddServer)
560{
561 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700562 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800563}
564
Riley Andrews06b01ad2014-12-18 12:10:08 -0800565TEST_F(BinderLibTest, DeathNotificationStrongRef)
566{
567 status_t ret;
568 sp<IBinder> sbinder;
569
570 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
571
572 {
573 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700574 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800575 ret = binder->linkToDeath(testDeathRecipient);
576 EXPECT_EQ(NO_ERROR, ret);
577 sbinder = binder;
578 }
579 {
580 Parcel data, reply;
581 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
582 EXPECT_EQ(0, ret);
583 }
584 IPCThreadState::self()->flushCommands();
585 ret = testDeathRecipient->waitEvent(5);
586 EXPECT_EQ(NO_ERROR, ret);
587 ret = sbinder->unlinkToDeath(testDeathRecipient);
588 EXPECT_EQ(DEAD_OBJECT, ret);
589}
590
591TEST_F(BinderLibTest, DeathNotificationMultiple)
592{
593 status_t ret;
594 const int clientcount = 2;
595 sp<IBinder> target;
596 sp<IBinder> linkedclient[clientcount];
597 sp<BinderLibTestCallBack> callBack[clientcount];
598 sp<IBinder> passiveclient[clientcount];
599
600 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700601 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800602 for (int i = 0; i < clientcount; i++) {
603 {
604 Parcel data, reply;
605
606 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700607 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800608 callBack[i] = new BinderLibTestCallBack();
609 data.writeStrongBinder(target);
610 data.writeStrongBinder(callBack[i]);
611 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
612 EXPECT_EQ(NO_ERROR, ret);
613 }
614 {
615 Parcel data, reply;
616
617 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700618 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800619 data.writeStrongBinder(target);
620 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
621 EXPECT_EQ(NO_ERROR, ret);
622 }
623 }
624 {
625 Parcel data, reply;
626 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
627 EXPECT_EQ(0, ret);
628 }
629
630 for (int i = 0; i < clientcount; i++) {
631 ret = callBack[i]->waitEvent(5);
632 EXPECT_EQ(NO_ERROR, ret);
633 ret = callBack[i]->getResult();
634 EXPECT_EQ(NO_ERROR, ret);
635 }
636}
637
Martijn Coenenf7100e42017-07-31 12:14:09 +0200638TEST_F(BinderLibTest, DeathNotificationThread)
639{
640 status_t ret;
641 sp<BinderLibTestCallBack> callback;
642 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700643 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200644 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700645 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200646
647 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
648
649 ret = target->linkToDeath(testDeathRecipient);
650 EXPECT_EQ(NO_ERROR, ret);
651
652 {
653 Parcel data, reply;
654 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
655 EXPECT_EQ(0, ret);
656 }
657
658 /* Make sure it's dead */
659 testDeathRecipient->waitEvent(5);
660
661 /* Now, pass the ref to another process and ask that process to
662 * call linkToDeath() on it, and wait for a response. This tests
663 * two things:
664 * 1) You still get death notifications when calling linkToDeath()
665 * on a ref that is already dead when it was passed to you.
666 * 2) That death notifications are not directly pushed to the thread
667 * registering them, but to the threadpool (proc workqueue) instead.
668 *
669 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
670 * is blocked on a condition variable waiting for the death notification to be
671 * called; therefore, that thread is not available for handling proc work.
672 * So, if the death notification was pushed to the thread workqueue, the callback
673 * would never be called, and the test would timeout and fail.
674 *
675 * Note that we can't do this part of the test from this thread itself, because
676 * the binder driver would only push death notifications to the thread if
677 * it is a looper thread, which this thread is not.
678 *
679 * See b/23525545 for details.
680 */
681 {
682 Parcel data, reply;
683
684 callback = new BinderLibTestCallBack();
685 data.writeStrongBinder(target);
686 data.writeStrongBinder(callback);
687 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
688 EXPECT_EQ(NO_ERROR, ret);
689 }
690
691 ret = callback->waitEvent(5);
692 EXPECT_EQ(NO_ERROR, ret);
693 ret = callback->getResult();
694 EXPECT_EQ(NO_ERROR, ret);
695}
696
Riley Andrews06b01ad2014-12-18 12:10:08 -0800697TEST_F(BinderLibTest, PassFile) {
698 int ret;
699 int pipefd[2];
700 uint8_t buf[1] = { 0 };
701 uint8_t write_value = 123;
702
703 ret = pipe2(pipefd, O_NONBLOCK);
704 ASSERT_EQ(0, ret);
705
706 {
707 Parcel data, reply;
708 uint8_t writebuf[1] = { write_value };
709
710 ret = data.writeFileDescriptor(pipefd[1], true);
711 EXPECT_EQ(NO_ERROR, ret);
712
713 ret = data.writeInt32(sizeof(writebuf));
714 EXPECT_EQ(NO_ERROR, ret);
715
716 ret = data.write(writebuf, sizeof(writebuf));
717 EXPECT_EQ(NO_ERROR, ret);
718
719 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
720 EXPECT_EQ(NO_ERROR, ret);
721 }
722
723 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700724 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800725 EXPECT_EQ(write_value, buf[0]);
726
727 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
728
729 ret = read(pipefd[0], buf, sizeof(buf));
730 EXPECT_EQ(0, ret);
731
732 close(pipefd[0]);
733}
734
Ryo Hashimotobf551892018-05-31 16:58:35 +0900735TEST_F(BinderLibTest, PassParcelFileDescriptor) {
736 const int datasize = 123;
737 std::vector<uint8_t> writebuf(datasize);
738 for (size_t i = 0; i < writebuf.size(); ++i) {
739 writebuf[i] = i;
740 }
741
742 android::base::unique_fd read_end, write_end;
743 {
744 int pipefd[2];
745 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
746 read_end.reset(pipefd[0]);
747 write_end.reset(pipefd[1]);
748 }
749 {
750 Parcel data;
751 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
752 write_end.reset();
753 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
754 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
755
756 Parcel reply;
757 EXPECT_EQ(NO_ERROR,
758 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
759 &reply));
760 }
761 std::vector<uint8_t> readbuf(datasize);
762 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
763 EXPECT_EQ(writebuf, readbuf);
764
765 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
766
767 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
768}
769
Riley Andrews06b01ad2014-12-18 12:10:08 -0800770TEST_F(BinderLibTest, PromoteLocal) {
771 sp<IBinder> strong = new BBinder();
772 wp<IBinder> weak = strong;
773 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700774 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800775 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700776 strong = nullptr;
777 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800778 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700779 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800780}
781
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700782TEST_F(BinderLibTest, LocalGetExtension) {
783 sp<BBinder> binder = new BBinder();
784 sp<IBinder> ext = new BBinder();
785 binder->setExtension(ext);
786 EXPECT_EQ(ext, binder->getExtension());
787}
788
789TEST_F(BinderLibTest, RemoteGetExtension) {
790 sp<IBinder> server = addServer();
791 ASSERT_TRUE(server != nullptr);
792
793 sp<IBinder> extension;
794 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
795 ASSERT_NE(nullptr, extension.get());
796
797 EXPECT_EQ(NO_ERROR, extension->pingBinder());
798}
799
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700800TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
801 status_t ret;
802 Parcel data, reply;
803
804 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
805 EXPECT_EQ(NO_ERROR, ret);
806
807 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700808 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800809 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
810 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
811 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
812 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700813}
814
Connor O'Brien52be2c92016-09-20 14:18:08 -0700815TEST_F(BinderLibTest, FreedBinder) {
816 status_t ret;
817
818 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700819 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700820
821 __u32 freedHandle;
822 wp<IBinder> keepFreedBinder;
823 {
824 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700825 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
826 ASSERT_EQ(NO_ERROR, ret);
827 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
828 freedHandle = freed->handle;
829 /* Add a weak ref to the freed binder so the driver does not
830 * delete its reference to it - otherwise the transaction
831 * fails regardless of whether the driver is fixed.
832 */
Steven Morelande171d622019-07-17 16:06:01 -0700833 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700834 }
Steven Morelande171d622019-07-17 16:06:01 -0700835 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700836 {
837 Parcel data, reply;
838 data.writeStrongBinder(server);
839 /* Replace original handle with handle to the freed binder */
840 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
841 __u32 oldHandle = strong->handle;
842 strong->handle = freedHandle;
843 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
844 /* Returns DEAD_OBJECT (-32) if target crashes and
845 * FAILED_TRANSACTION if the driver rejects the invalid
846 * object.
847 */
848 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
849 /* Restore original handle so parcel destructor does not use
850 * the wrong handle.
851 */
852 strong->handle = oldHandle;
853 }
854}
855
Sherry Yang336cdd32017-07-24 14:12:27 -0700856TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
857 status_t ret;
858 Parcel data, reply;
859 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
860 for (int i = 0; i < 2; i++) {
861 BinderLibTestBundle datai;
862 datai.appendFrom(&data, 0, data.dataSize());
863
864 data.freeData();
865 data.writeInt32(1);
866 data.writeStrongBinder(callBack);
867 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
868
869 datai.appendTo(&data);
870 }
871 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
872 EXPECT_EQ(NO_ERROR, ret);
873}
874
Martijn Coenen45b07b42017-08-09 12:07:45 +0200875TEST_F(BinderLibTest, OnewayQueueing)
876{
877 status_t ret;
878 Parcel data, data2;
879
880 sp<IBinder> pollServer = addPollServer();
881
882 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
883 data.writeStrongBinder(callBack);
884 data.writeInt32(500000); // delay in us before calling back
885
886 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
887 data2.writeStrongBinder(callBack2);
888 data2.writeInt32(0); // delay in us
889
Yi Kong91635562018-06-07 14:38:36 -0700890 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200891 EXPECT_EQ(NO_ERROR, ret);
892
893 // The delay ensures that this second transaction will end up on the async_todo list
894 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700895 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200896 EXPECT_EQ(NO_ERROR, ret);
897
898 // The server will ensure that the two transactions are handled in the expected order;
899 // If the ordering is not as expected, an error will be returned through the callbacks.
900 ret = callBack->waitEvent(2);
901 EXPECT_EQ(NO_ERROR, ret);
902 ret = callBack->getResult();
903 EXPECT_EQ(NO_ERROR, ret);
904
905 ret = callBack2->waitEvent(2);
906 EXPECT_EQ(NO_ERROR, ret);
907 ret = callBack2->getResult();
908 EXPECT_EQ(NO_ERROR, ret);
909}
910
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100911TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
912{
913 status_t ret;
914 Parcel data, reply;
915 data.writeInterfaceToken(binderLibTestServiceName);
916 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
917 EXPECT_EQ(-1, reply.readInt32());
918 EXPECT_EQ(NO_ERROR, ret);
919}
920
921TEST_F(BinderLibTest, WorkSourceSet)
922{
923 status_t ret;
924 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000925 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000926 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100927 data.writeInterfaceToken(binderLibTestServiceName);
928 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
929 EXPECT_EQ(100, reply.readInt32());
930 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000931 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
932 EXPECT_EQ(NO_ERROR, ret);
933}
934
935TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
936{
937 status_t ret;
938 Parcel data, reply;
939
940 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
941 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
942
943 data.writeInterfaceToken(binderLibTestServiceName);
944 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
945 EXPECT_EQ(-1, reply.readInt32());
946 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100947 EXPECT_EQ(NO_ERROR, ret);
948}
949
950TEST_F(BinderLibTest, WorkSourceCleared)
951{
952 status_t ret;
953 Parcel data, reply;
954
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000955 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000956 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
957 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100958 data.writeInterfaceToken(binderLibTestServiceName);
959 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
960
961 EXPECT_EQ(-1, reply.readInt32());
962 EXPECT_EQ(100, previousWorkSource);
963 EXPECT_EQ(NO_ERROR, ret);
964}
965
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000966TEST_F(BinderLibTest, WorkSourceRestored)
967{
968 status_t ret;
969 Parcel data, reply;
970
971 IPCThreadState::self()->setCallingWorkSourceUid(100);
972 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
973 IPCThreadState::self()->restoreCallingWorkSource(token);
974
975 data.writeInterfaceToken(binderLibTestServiceName);
976 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
977
978 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000979 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000980 EXPECT_EQ(NO_ERROR, ret);
981}
982
Olivier Gaillard91a04802018-11-14 17:32:41 +0000983TEST_F(BinderLibTest, PropagateFlagSet)
984{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000985 IPCThreadState::self()->clearPropagateWorkSource();
986 IPCThreadState::self()->setCallingWorkSourceUid(100);
987 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
988}
989
990TEST_F(BinderLibTest, PropagateFlagCleared)
991{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000992 IPCThreadState::self()->setCallingWorkSourceUid(100);
993 IPCThreadState::self()->clearPropagateWorkSource();
994 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
995}
996
997TEST_F(BinderLibTest, PropagateFlagRestored)
998{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000999 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1000 IPCThreadState::self()->restoreCallingWorkSource(token);
1001
1002 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1003}
1004
1005TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1006{
1007 IPCThreadState::self()->setCallingWorkSourceUid(100);
1008
1009 Parcel data, reply;
1010 status_t ret;
1011 data.writeInterfaceToken(binderLibTestServiceName);
1012 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1013
1014 Parcel data2, reply2;
1015 status_t ret2;
1016 data2.writeInterfaceToken(binderLibTestServiceName);
1017 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1018 EXPECT_EQ(100, reply2.readInt32());
1019 EXPECT_EQ(NO_ERROR, ret2);
1020}
1021
Steven Morelandb96ea772020-07-16 22:43:02 +00001022TEST_F(BinderLibTest, SchedPolicySet) {
1023 sp<IBinder> server = addServer();
1024 ASSERT_TRUE(server != nullptr);
1025
1026 Parcel data, reply;
1027 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1028 EXPECT_EQ(NO_ERROR, ret);
1029
1030 int policy = reply.readInt32();
1031 int priority = reply.readInt32();
1032
1033 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1034 EXPECT_EQ(kSchedPriority, priority);
1035}
1036
1037
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001038TEST_F(BinderLibTest, VectorSent) {
1039 Parcel data, reply;
1040 sp<IBinder> server = addServer();
1041 ASSERT_TRUE(server != nullptr);
1042
1043 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1044 data.writeUint64Vector(testValue);
1045
1046 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1047 EXPECT_EQ(NO_ERROR, ret);
1048 std::vector<uint64_t> readValue;
1049 ret = reply.readUint64Vector(&readValue);
1050 EXPECT_EQ(readValue, testValue);
1051}
1052
Martijn Coenen82c75312019-07-24 15:18:30 +02001053TEST_F(BinderLibTest, BufRejected) {
1054 Parcel data, reply;
1055 uint32_t buf;
1056 sp<IBinder> server = addServer();
1057 ASSERT_TRUE(server != nullptr);
1058
1059 binder_buffer_object obj {
1060 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001061 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001062 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1063 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001064 };
1065 data.setDataCapacity(1024);
1066 // Write a bogus object at offset 0 to get an entry in the offset table
1067 data.writeFileDescriptor(0);
1068 EXPECT_EQ(data.objectsCount(), 1);
1069 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1070 // And now, overwrite it with the buffer object
1071 memcpy(parcelData, &obj, sizeof(obj));
1072 data.setDataSize(sizeof(obj));
1073
1074 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1075 // Either the kernel should reject this transaction (if it's correct), but
1076 // if it's not, the server implementation should return an error if it
1077 // finds an object in the received Parcel.
1078 EXPECT_NE(NO_ERROR, ret);
1079}
1080
Riley Andrews06b01ad2014-12-18 12:10:08 -08001081class BinderLibTestService : public BBinder
1082{
1083 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001084 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001085 : m_id(id)
1086 , m_nextServerId(id + 1)
1087 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001088 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001089 {
Yi Kong91635562018-06-07 14:38:36 -07001090 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1091 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001092 }
1093 ~BinderLibTestService()
1094 {
1095 exit(EXIT_SUCCESS);
1096 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001097
1098 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001099 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001100 Parcel data;
1101 data.writeInt32(NO_ERROR);
1102 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001103 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001104 }
1105 }
1106
Riley Andrews06b01ad2014-12-18 12:10:08 -08001107 virtual status_t onTransact(uint32_t code,
1108 const Parcel& data, Parcel* reply,
1109 uint32_t flags = 0) {
1110 //printf("%s: code %d\n", __func__, code);
1111 (void)flags;
1112
1113 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1114 return PERMISSION_DENIED;
1115 }
1116 switch (code) {
1117 case BINDER_LIB_TEST_REGISTER_SERVER: {
1118 int32_t id;
1119 sp<IBinder> binder;
1120 id = data.readInt32();
1121 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001122 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001123 return BAD_VALUE;
1124 }
1125
1126 if (m_id != 0)
1127 return INVALID_OPERATION;
1128
1129 pthread_mutex_lock(&m_serverWaitMutex);
1130 if (m_serverStartRequested) {
1131 m_serverStartRequested = false;
1132 m_serverStarted = binder;
1133 pthread_cond_signal(&m_serverWaitCond);
1134 }
1135 pthread_mutex_unlock(&m_serverWaitMutex);
1136 return NO_ERROR;
1137 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001138 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001139 case BINDER_LIB_TEST_ADD_SERVER: {
1140 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001141 int serverid;
1142
1143 if (m_id != 0) {
1144 return INVALID_OPERATION;
1145 }
1146 pthread_mutex_lock(&m_serverWaitMutex);
1147 if (m_serverStartRequested) {
1148 ret = -EBUSY;
1149 } else {
1150 serverid = m_nextServerId++;
1151 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001152 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001153
1154 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001155 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001156 pthread_mutex_lock(&m_serverWaitMutex);
1157 }
1158 if (ret > 0) {
1159 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001160 struct timespec ts;
1161 clock_gettime(CLOCK_REALTIME, &ts);
1162 ts.tv_sec += 5;
1163 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001164 }
1165 if (m_serverStartRequested) {
1166 m_serverStartRequested = false;
1167 ret = -ETIMEDOUT;
1168 } else {
1169 reply->writeStrongBinder(m_serverStarted);
1170 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001171 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001172 ret = NO_ERROR;
1173 }
1174 } else if (ret >= 0) {
1175 m_serverStartRequested = false;
1176 ret = UNKNOWN_ERROR;
1177 }
1178 pthread_mutex_unlock(&m_serverWaitMutex);
1179 return ret;
1180 }
1181 case BINDER_LIB_TEST_NOP_TRANSACTION:
1182 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001183 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1184 // Note: this transaction is only designed for use with a
1185 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001186 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001187 // A callback was already pending; this means that
1188 // we received a second call while still processing
1189 // the first one. Fail the test.
1190 sp<IBinder> callback = data.readStrongBinder();
1191 Parcel data2;
1192 data2.writeInt32(UNKNOWN_ERROR);
1193
Yi Kong91635562018-06-07 14:38:36 -07001194 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001195 } else {
1196 m_callback = data.readStrongBinder();
1197 int32_t delayUs = data.readInt32();
1198 /*
1199 * It's necessary that we sleep here, so the next
1200 * transaction the caller makes will be queued to
1201 * the async queue.
1202 */
1203 usleep(delayUs);
1204
1205 /*
1206 * Now when we return, libbinder will tell the kernel
1207 * we are done with this transaction, and the kernel
1208 * can move the queued transaction to either the
1209 * thread todo worklist (for kernels without the fix),
1210 * or the proc todo worklist. In case of the former,
1211 * the next outbound call will pick up the pending
1212 * transaction, which leads to undesired reentrant
1213 * behavior. This is caught in the if() branch above.
1214 */
1215 }
1216
1217 return NO_ERROR;
1218 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001219 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1220 Parcel data2, reply2;
1221 sp<IBinder> binder;
1222 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001223 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001224 return BAD_VALUE;
1225 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001226 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001227 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1228 return NO_ERROR;
1229 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001230 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1231 reply->writeStrongBinder(this);
1232 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001233 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1234 reply->writeInt32(m_id);
1235 return NO_ERROR;
1236 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1237 int32_t count;
1238 uint32_t indirect_code;
1239 sp<IBinder> binder;
1240
1241 count = data.readInt32();
1242 reply->writeInt32(m_id);
1243 reply->writeInt32(count);
1244 for (int i = 0; i < count; i++) {
1245 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001246 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001247 return BAD_VALUE;
1248 }
1249 indirect_code = data.readInt32();
1250 BinderLibTestBundle data2(&data);
1251 if (!data2.isValid()) {
1252 return BAD_VALUE;
1253 }
1254 BinderLibTestBundle reply2;
1255 binder->transact(indirect_code, data2, &reply2);
1256 reply2.appendTo(reply);
1257 }
1258 return NO_ERROR;
1259 }
1260 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1261 reply->setError(data.readInt32());
1262 return NO_ERROR;
1263 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1264 reply->writeInt32(sizeof(void *));
1265 return NO_ERROR;
1266 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1267 return NO_ERROR;
1268 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1269 m_strongRef = data.readStrongBinder();
1270 return NO_ERROR;
1271 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1272 int ret;
1273 Parcel data2, reply2;
1274 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1275 sp<IBinder> target;
1276 sp<IBinder> callback;
1277
1278 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001279 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001280 return BAD_VALUE;
1281 }
1282 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001283 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001284 return BAD_VALUE;
1285 }
1286 ret = target->linkToDeath(testDeathRecipient);
1287 if (ret == NO_ERROR)
1288 ret = testDeathRecipient->waitEvent(5);
1289 data2.writeInt32(ret);
1290 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1291 return NO_ERROR;
1292 }
1293 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1294 int ret;
1295 int32_t size;
1296 const void *buf;
1297 int fd;
1298
1299 fd = data.readFileDescriptor();
1300 if (fd < 0) {
1301 return BAD_VALUE;
1302 }
1303 ret = data.readInt32(&size);
1304 if (ret != NO_ERROR) {
1305 return ret;
1306 }
1307 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001308 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001309 return BAD_VALUE;
1310 }
1311 ret = write(fd, buf, size);
1312 if (ret != size)
1313 return UNKNOWN_ERROR;
1314 return NO_ERROR;
1315 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001316 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1317 int ret;
1318 int32_t size;
1319 const void *buf;
1320 android::base::unique_fd fd;
1321
1322 ret = data.readUniqueParcelFileDescriptor(&fd);
1323 if (ret != NO_ERROR) {
1324 return ret;
1325 }
1326 ret = data.readInt32(&size);
1327 if (ret != NO_ERROR) {
1328 return ret;
1329 }
1330 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001331 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001332 return BAD_VALUE;
1333 }
1334 ret = write(fd.get(), buf, size);
1335 if (ret != size) return UNKNOWN_ERROR;
1336 return NO_ERROR;
1337 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001338 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1339 alarm(10);
1340 return NO_ERROR;
1341 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001342 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001343 ;
1344 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001345 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001346 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001347 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001348 return NO_ERROR;
1349 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001350 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1351 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001352 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001353 return NO_ERROR;
1354 }
Steven Morelandb96ea772020-07-16 22:43:02 +00001355 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1356 int policy = 0;
1357 sched_param param;
1358 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1359 return UNKNOWN_ERROR;
1360 }
1361 reply->writeInt32(policy);
1362 reply->writeInt32(param.sched_priority);
1363 return NO_ERROR;
1364 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001365 case BINDER_LIB_TEST_ECHO_VECTOR: {
1366 std::vector<uint64_t> vector;
1367 auto err = data.readUint64Vector(&vector);
1368 if (err != NO_ERROR)
1369 return err;
1370 reply->writeUint64Vector(vector);
1371 return NO_ERROR;
1372 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001373 case BINDER_LIB_TEST_REJECT_BUF: {
1374 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1375 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001376 default:
1377 return UNKNOWN_TRANSACTION;
1378 };
1379 }
1380 private:
1381 int32_t m_id;
1382 int32_t m_nextServerId;
1383 pthread_mutex_t m_serverWaitMutex;
1384 pthread_cond_t m_serverWaitCond;
1385 bool m_serverStartRequested;
1386 sp<IBinder> m_serverStarted;
1387 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001388 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001389};
1390
Martijn Coenen45b07b42017-08-09 12:07:45 +02001391int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001392{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001393 binderLibTestServiceName += String16(binderserversuffix);
1394
Riley Andrews06b01ad2014-12-18 12:10:08 -08001395 status_t ret;
1396 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001397 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001398 {
1399 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001400
Steven Morelandb96ea772020-07-16 22:43:02 +00001401 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1402
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001403 /*
1404 * Normally would also contain functionality as well, but we are only
1405 * testing the extension mechanism.
1406 */
1407 testService->setExtension(new BBinder());
1408
Martijn Coenen82c75312019-07-24 15:18:30 +02001409 // Required for test "BufRejected'
1410 testService->setRequestingSid(true);
1411
Martijn Coenen45b07b42017-08-09 12:07:45 +02001412 /*
1413 * We need this below, but can't hold a sp<> because it prevents the
1414 * node from being cleaned up automatically. It's safe in this case
1415 * because of how the tests are written.
1416 */
1417 testServicePtr = testService.get();
1418
Riley Andrews06b01ad2014-12-18 12:10:08 -08001419 if (index == 0) {
1420 ret = sm->addService(binderLibTestServiceName, testService);
1421 } else {
1422 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1423 Parcel data, reply;
1424 data.writeInt32(index);
1425 data.writeStrongBinder(testService);
1426
1427 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1428 }
1429 }
1430 write(readypipefd, &ret, sizeof(ret));
1431 close(readypipefd);
1432 //printf("%s: ret %d\n", __func__, ret);
1433 if (ret)
1434 return 1;
1435 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001436 if (usePoll) {
1437 int fd;
1438 struct epoll_event ev;
1439 int epoll_fd;
1440 IPCThreadState::self()->setupPolling(&fd);
1441 if (fd < 0) {
1442 return 1;
1443 }
1444 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1445
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001446 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001447 if (epoll_fd == -1) {
1448 return 1;
1449 }
1450
1451 ev.events = EPOLLIN;
1452 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1453 return 1;
1454 }
1455
1456 while (1) {
1457 /*
1458 * We simulate a single-threaded process using the binder poll
1459 * interface; besides handling binder commands, it can also
1460 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001461 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001462 *
1463 * processPendingCall() will then issue that transaction.
1464 */
1465 struct epoll_event events[1];
1466 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1467 if (numEvents < 0) {
1468 if (errno == EINTR) {
1469 continue;
1470 }
1471 return 1;
1472 }
1473 if (numEvents > 0) {
1474 IPCThreadState::self()->handlePolledCommands();
1475 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1476 testServicePtr->processPendingCall();
1477 }
1478 }
1479 } else {
1480 ProcessState::self()->startThreadPool();
1481 IPCThreadState::self()->joinThreadPool();
1482 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001483 //printf("%s: joinThreadPool returned\n", __func__);
1484 return 1; /* joinThreadPool should not return */
1485}
1486
1487int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001488 ExitIfWrongAbi();
1489
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001490 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001491 binderservername = argv[2];
1492 } else {
1493 binderservername = argv[0];
1494 }
1495
Martijn Coenen45b07b42017-08-09 12:07:45 +02001496 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1497 binderserversuffix = argv[5];
1498 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001499 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001500 binderserversuffix = new char[16];
1501 snprintf(binderserversuffix, 16, "%d", getpid());
1502 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001503
1504 ::testing::InitGoogleTest(&argc, argv);
1505 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1506 ProcessState::self()->startThreadPool();
1507 return RUN_ALL_TESTS();
1508}