blob: 565338bac3237c382e2e670c607e786ea6cde994 [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>
Marco Ballesio7ee17572020-09-08 10:30:03 -070019#include <fstream>
Riley Andrews06b01ad2014-12-18 12:10:08 -080020#include <poll.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <gtest/gtest.h>
26
27#include <binder/Binder.h>
28#include <binder/IBinder.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31
Steven Morelandd63ed9d2019-09-04 18:15:25 -070032#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020033#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080034#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020035
Riley Andrews06b01ad2014-12-18 12:10:08 -080036#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
37
38using namespace android;
39
Sherry Yang336cdd32017-07-24 14:12:27 -070040static ::testing::AssertionResult IsPageAligned(void *buf) {
41 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
42 return ::testing::AssertionSuccess();
43 else
44 return ::testing::AssertionFailure() << buf << " is not page aligned";
45}
46
Riley Andrews06b01ad2014-12-18 12:10:08 -080047static testing::Environment* binder_env;
48static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070049static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080050static char binderserverarg[] = "--binderserver";
51
Steven Morelandbf1915b2020-07-16 22:43:02 +000052static constexpr int kSchedPolicy = SCHED_RR;
53static constexpr int kSchedPriority = 7;
54
Riley Andrews06b01ad2014-12-18 12:10:08 -080055static String16 binderLibTestServiceName = String16("test.binderLib");
56
57enum BinderLibTestTranscationCode {
58 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
59 BINDER_LIB_TEST_REGISTER_SERVER,
60 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020061 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080062 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070063 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020064 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080065 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070066 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080067 BINDER_LIB_TEST_GET_ID_TRANSACTION,
68 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
69 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
70 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
71 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
72 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
73 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090074 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080075 BINDER_LIB_TEST_EXIT_TRANSACTION,
76 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
77 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070078 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010079 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000080 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070081 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
82 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080083 BINDER_LIB_TEST_ECHO_VECTOR,
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
Marco Ballesio7ee17572020-09-08 10:30:03 -0700402TEST_F(BinderLibTest, Freeze) {
403 status_t ret;
404 Parcel data, reply, replypid;
405 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
406
407 //Pass test on devices where the freezer is not supported
408 if (freezer_file.fail()) {
409 GTEST_SKIP();
410 return;
411 }
412
413 std::string freezer_enabled;
414 std::getline(freezer_file, freezer_enabled);
415
416 //Pass test on devices where the freezer is disabled
417 if (freezer_enabled != "1") {
418 GTEST_SKIP();
419 return;
420 }
421
422 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
423 int32_t pid = replypid.readInt32();
424 EXPECT_EQ(NO_ERROR, ret);
425 for (int i = 0; i < 10; i++) {
426 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
427 }
428 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
429 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
430 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
431 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
432 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
433 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
434}
435
Riley Andrews06b01ad2014-12-18 12:10:08 -0800436TEST_F(BinderLibTest, SetError) {
437 int32_t testValue[] = { 0, -123, 123 };
438 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
439 status_t ret;
440 Parcel data, reply;
441 data.writeInt32(testValue[i]);
442 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
443 EXPECT_EQ(testValue[i], ret);
444 }
445}
446
447TEST_F(BinderLibTest, GetId) {
448 status_t ret;
449 int32_t id;
450 Parcel data, reply;
451 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
452 EXPECT_EQ(NO_ERROR, ret);
453 ret = reply.readInt32(&id);
454 EXPECT_EQ(NO_ERROR, ret);
455 EXPECT_EQ(0, id);
456}
457
458TEST_F(BinderLibTest, PtrSize) {
459 status_t ret;
460 int32_t ptrsize;
461 Parcel data, reply;
462 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700463 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800464 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
465 EXPECT_EQ(NO_ERROR, ret);
466 ret = reply.readInt32(&ptrsize);
467 EXPECT_EQ(NO_ERROR, ret);
468 RecordProperty("TestPtrSize", sizeof(void *));
469 RecordProperty("ServerPtrSize", sizeof(void *));
470}
471
472TEST_F(BinderLibTest, IndirectGetId2)
473{
474 status_t ret;
475 int32_t id;
476 int32_t count;
477 Parcel data, reply;
478 int32_t serverId[3];
479
480 data.writeInt32(ARRAY_SIZE(serverId));
481 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
482 sp<IBinder> server;
483 BinderLibTestBundle datai;
484
485 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700486 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800487 data.writeStrongBinder(server);
488 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
489 datai.appendTo(&data);
490 }
491
492 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
493 ASSERT_EQ(NO_ERROR, ret);
494
495 ret = reply.readInt32(&id);
496 ASSERT_EQ(NO_ERROR, ret);
497 EXPECT_EQ(0, id);
498
499 ret = reply.readInt32(&count);
500 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700501 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800502
503 for (size_t i = 0; i < (size_t)count; i++) {
504 BinderLibTestBundle replyi(&reply);
505 EXPECT_TRUE(replyi.isValid());
506 ret = replyi.readInt32(&id);
507 EXPECT_EQ(NO_ERROR, ret);
508 EXPECT_EQ(serverId[i], id);
509 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
510 }
511
512 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
513}
514
515TEST_F(BinderLibTest, IndirectGetId3)
516{
517 status_t ret;
518 int32_t id;
519 int32_t count;
520 Parcel data, reply;
521 int32_t serverId[3];
522
523 data.writeInt32(ARRAY_SIZE(serverId));
524 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
525 sp<IBinder> server;
526 BinderLibTestBundle datai;
527 BinderLibTestBundle datai2;
528
529 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700530 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800531 data.writeStrongBinder(server);
532 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
533
534 datai.writeInt32(1);
535 datai.writeStrongBinder(m_server);
536 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
537 datai2.appendTo(&datai);
538
539 datai.appendTo(&data);
540 }
541
542 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
543 ASSERT_EQ(NO_ERROR, ret);
544
545 ret = reply.readInt32(&id);
546 ASSERT_EQ(NO_ERROR, ret);
547 EXPECT_EQ(0, id);
548
549 ret = reply.readInt32(&count);
550 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700551 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800552
553 for (size_t i = 0; i < (size_t)count; i++) {
554 int32_t counti;
555
556 BinderLibTestBundle replyi(&reply);
557 EXPECT_TRUE(replyi.isValid());
558 ret = replyi.readInt32(&id);
559 EXPECT_EQ(NO_ERROR, ret);
560 EXPECT_EQ(serverId[i], id);
561
562 ret = replyi.readInt32(&counti);
563 ASSERT_EQ(NO_ERROR, ret);
564 EXPECT_EQ(1, counti);
565
566 BinderLibTestBundle replyi2(&replyi);
567 EXPECT_TRUE(replyi2.isValid());
568 ret = replyi2.readInt32(&id);
569 EXPECT_EQ(NO_ERROR, ret);
570 EXPECT_EQ(0, id);
571 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
572
573 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
574 }
575
576 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
577}
578
579TEST_F(BinderLibTest, CallBack)
580{
581 status_t ret;
582 Parcel data, reply;
583 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
584 data.writeStrongBinder(callBack);
585 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
586 EXPECT_EQ(NO_ERROR, ret);
587 ret = callBack->waitEvent(5);
588 EXPECT_EQ(NO_ERROR, ret);
589 ret = callBack->getResult();
590 EXPECT_EQ(NO_ERROR, ret);
591}
592
593TEST_F(BinderLibTest, AddServer)
594{
595 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700596 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800597}
598
Riley Andrews06b01ad2014-12-18 12:10:08 -0800599TEST_F(BinderLibTest, DeathNotificationStrongRef)
600{
601 status_t ret;
602 sp<IBinder> sbinder;
603
604 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
605
606 {
607 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700608 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800609 ret = binder->linkToDeath(testDeathRecipient);
610 EXPECT_EQ(NO_ERROR, ret);
611 sbinder = binder;
612 }
613 {
614 Parcel data, reply;
615 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
616 EXPECT_EQ(0, ret);
617 }
618 IPCThreadState::self()->flushCommands();
619 ret = testDeathRecipient->waitEvent(5);
620 EXPECT_EQ(NO_ERROR, ret);
621 ret = sbinder->unlinkToDeath(testDeathRecipient);
622 EXPECT_EQ(DEAD_OBJECT, ret);
623}
624
625TEST_F(BinderLibTest, DeathNotificationMultiple)
626{
627 status_t ret;
628 const int clientcount = 2;
629 sp<IBinder> target;
630 sp<IBinder> linkedclient[clientcount];
631 sp<BinderLibTestCallBack> callBack[clientcount];
632 sp<IBinder> passiveclient[clientcount];
633
634 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700635 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800636 for (int i = 0; i < clientcount; i++) {
637 {
638 Parcel data, reply;
639
640 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700641 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800642 callBack[i] = new BinderLibTestCallBack();
643 data.writeStrongBinder(target);
644 data.writeStrongBinder(callBack[i]);
645 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
646 EXPECT_EQ(NO_ERROR, ret);
647 }
648 {
649 Parcel data, reply;
650
651 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700652 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800653 data.writeStrongBinder(target);
654 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
655 EXPECT_EQ(NO_ERROR, ret);
656 }
657 }
658 {
659 Parcel data, reply;
660 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
661 EXPECT_EQ(0, ret);
662 }
663
664 for (int i = 0; i < clientcount; i++) {
665 ret = callBack[i]->waitEvent(5);
666 EXPECT_EQ(NO_ERROR, ret);
667 ret = callBack[i]->getResult();
668 EXPECT_EQ(NO_ERROR, ret);
669 }
670}
671
Martijn Coenenf7100e42017-07-31 12:14:09 +0200672TEST_F(BinderLibTest, DeathNotificationThread)
673{
674 status_t ret;
675 sp<BinderLibTestCallBack> callback;
676 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700677 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200678 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700679 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200680
681 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
682
683 ret = target->linkToDeath(testDeathRecipient);
684 EXPECT_EQ(NO_ERROR, ret);
685
686 {
687 Parcel data, reply;
688 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
689 EXPECT_EQ(0, ret);
690 }
691
692 /* Make sure it's dead */
693 testDeathRecipient->waitEvent(5);
694
695 /* Now, pass the ref to another process and ask that process to
696 * call linkToDeath() on it, and wait for a response. This tests
697 * two things:
698 * 1) You still get death notifications when calling linkToDeath()
699 * on a ref that is already dead when it was passed to you.
700 * 2) That death notifications are not directly pushed to the thread
701 * registering them, but to the threadpool (proc workqueue) instead.
702 *
703 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
704 * is blocked on a condition variable waiting for the death notification to be
705 * called; therefore, that thread is not available for handling proc work.
706 * So, if the death notification was pushed to the thread workqueue, the callback
707 * would never be called, and the test would timeout and fail.
708 *
709 * Note that we can't do this part of the test from this thread itself, because
710 * the binder driver would only push death notifications to the thread if
711 * it is a looper thread, which this thread is not.
712 *
713 * See b/23525545 for details.
714 */
715 {
716 Parcel data, reply;
717
718 callback = new BinderLibTestCallBack();
719 data.writeStrongBinder(target);
720 data.writeStrongBinder(callback);
721 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
722 EXPECT_EQ(NO_ERROR, ret);
723 }
724
725 ret = callback->waitEvent(5);
726 EXPECT_EQ(NO_ERROR, ret);
727 ret = callback->getResult();
728 EXPECT_EQ(NO_ERROR, ret);
729}
730
Riley Andrews06b01ad2014-12-18 12:10:08 -0800731TEST_F(BinderLibTest, PassFile) {
732 int ret;
733 int pipefd[2];
734 uint8_t buf[1] = { 0 };
735 uint8_t write_value = 123;
736
737 ret = pipe2(pipefd, O_NONBLOCK);
738 ASSERT_EQ(0, ret);
739
740 {
741 Parcel data, reply;
742 uint8_t writebuf[1] = { write_value };
743
744 ret = data.writeFileDescriptor(pipefd[1], true);
745 EXPECT_EQ(NO_ERROR, ret);
746
747 ret = data.writeInt32(sizeof(writebuf));
748 EXPECT_EQ(NO_ERROR, ret);
749
750 ret = data.write(writebuf, sizeof(writebuf));
751 EXPECT_EQ(NO_ERROR, ret);
752
753 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
754 EXPECT_EQ(NO_ERROR, ret);
755 }
756
757 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700758 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800759 EXPECT_EQ(write_value, buf[0]);
760
761 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
762
763 ret = read(pipefd[0], buf, sizeof(buf));
764 EXPECT_EQ(0, ret);
765
766 close(pipefd[0]);
767}
768
Ryo Hashimotobf551892018-05-31 16:58:35 +0900769TEST_F(BinderLibTest, PassParcelFileDescriptor) {
770 const int datasize = 123;
771 std::vector<uint8_t> writebuf(datasize);
772 for (size_t i = 0; i < writebuf.size(); ++i) {
773 writebuf[i] = i;
774 }
775
776 android::base::unique_fd read_end, write_end;
777 {
778 int pipefd[2];
779 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
780 read_end.reset(pipefd[0]);
781 write_end.reset(pipefd[1]);
782 }
783 {
784 Parcel data;
785 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
786 write_end.reset();
787 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
788 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
789
790 Parcel reply;
791 EXPECT_EQ(NO_ERROR,
792 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
793 &reply));
794 }
795 std::vector<uint8_t> readbuf(datasize);
796 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
797 EXPECT_EQ(writebuf, readbuf);
798
799 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
800
801 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
802}
803
Riley Andrews06b01ad2014-12-18 12:10:08 -0800804TEST_F(BinderLibTest, PromoteLocal) {
805 sp<IBinder> strong = new BBinder();
806 wp<IBinder> weak = strong;
807 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700808 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800809 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700810 strong = nullptr;
811 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800812 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700813 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800814}
815
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700816TEST_F(BinderLibTest, LocalGetExtension) {
817 sp<BBinder> binder = new BBinder();
818 sp<IBinder> ext = new BBinder();
819 binder->setExtension(ext);
820 EXPECT_EQ(ext, binder->getExtension());
821}
822
823TEST_F(BinderLibTest, RemoteGetExtension) {
824 sp<IBinder> server = addServer();
825 ASSERT_TRUE(server != nullptr);
826
827 sp<IBinder> extension;
828 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
829 ASSERT_NE(nullptr, extension.get());
830
831 EXPECT_EQ(NO_ERROR, extension->pingBinder());
832}
833
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700834TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
835 status_t ret;
836 Parcel data, reply;
837
838 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
839 EXPECT_EQ(NO_ERROR, ret);
840
841 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700842 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800843 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
844 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
845 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
846 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700847}
848
Connor O'Brien52be2c92016-09-20 14:18:08 -0700849TEST_F(BinderLibTest, FreedBinder) {
850 status_t ret;
851
852 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700853 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700854
855 __u32 freedHandle;
856 wp<IBinder> keepFreedBinder;
857 {
858 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700859 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
860 ASSERT_EQ(NO_ERROR, ret);
861 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
862 freedHandle = freed->handle;
863 /* Add a weak ref to the freed binder so the driver does not
864 * delete its reference to it - otherwise the transaction
865 * fails regardless of whether the driver is fixed.
866 */
Steven Morelande171d622019-07-17 16:06:01 -0700867 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700868 }
Steven Morelande171d622019-07-17 16:06:01 -0700869 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700870 {
871 Parcel data, reply;
872 data.writeStrongBinder(server);
873 /* Replace original handle with handle to the freed binder */
874 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
875 __u32 oldHandle = strong->handle;
876 strong->handle = freedHandle;
877 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
878 /* Returns DEAD_OBJECT (-32) if target crashes and
879 * FAILED_TRANSACTION if the driver rejects the invalid
880 * object.
881 */
882 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
883 /* Restore original handle so parcel destructor does not use
884 * the wrong handle.
885 */
886 strong->handle = oldHandle;
887 }
888}
889
Sherry Yang336cdd32017-07-24 14:12:27 -0700890TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
891 status_t ret;
892 Parcel data, reply;
893 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
894 for (int i = 0; i < 2; i++) {
895 BinderLibTestBundle datai;
896 datai.appendFrom(&data, 0, data.dataSize());
897
898 data.freeData();
899 data.writeInt32(1);
900 data.writeStrongBinder(callBack);
901 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
902
903 datai.appendTo(&data);
904 }
905 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
906 EXPECT_EQ(NO_ERROR, ret);
907}
908
Martijn Coenen45b07b42017-08-09 12:07:45 +0200909TEST_F(BinderLibTest, OnewayQueueing)
910{
911 status_t ret;
912 Parcel data, data2;
913
914 sp<IBinder> pollServer = addPollServer();
915
916 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
917 data.writeStrongBinder(callBack);
918 data.writeInt32(500000); // delay in us before calling back
919
920 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
921 data2.writeStrongBinder(callBack2);
922 data2.writeInt32(0); // delay in us
923
Yi Kong91635562018-06-07 14:38:36 -0700924 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200925 EXPECT_EQ(NO_ERROR, ret);
926
927 // The delay ensures that this second transaction will end up on the async_todo list
928 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700929 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200930 EXPECT_EQ(NO_ERROR, ret);
931
932 // The server will ensure that the two transactions are handled in the expected order;
933 // If the ordering is not as expected, an error will be returned through the callbacks.
934 ret = callBack->waitEvent(2);
935 EXPECT_EQ(NO_ERROR, ret);
936 ret = callBack->getResult();
937 EXPECT_EQ(NO_ERROR, ret);
938
939 ret = callBack2->waitEvent(2);
940 EXPECT_EQ(NO_ERROR, ret);
941 ret = callBack2->getResult();
942 EXPECT_EQ(NO_ERROR, ret);
943}
944
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100945TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
946{
947 status_t ret;
948 Parcel data, reply;
949 data.writeInterfaceToken(binderLibTestServiceName);
950 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
951 EXPECT_EQ(-1, reply.readInt32());
952 EXPECT_EQ(NO_ERROR, ret);
953}
954
955TEST_F(BinderLibTest, WorkSourceSet)
956{
957 status_t ret;
958 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000959 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000960 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100961 data.writeInterfaceToken(binderLibTestServiceName);
962 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
963 EXPECT_EQ(100, reply.readInt32());
964 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000965 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
966 EXPECT_EQ(NO_ERROR, ret);
967}
968
969TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
970{
971 status_t ret;
972 Parcel data, reply;
973
974 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
975 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
976
977 data.writeInterfaceToken(binderLibTestServiceName);
978 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
979 EXPECT_EQ(-1, reply.readInt32());
980 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100981 EXPECT_EQ(NO_ERROR, ret);
982}
983
984TEST_F(BinderLibTest, WorkSourceCleared)
985{
986 status_t ret;
987 Parcel data, reply;
988
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000989 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000990 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
991 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100992 data.writeInterfaceToken(binderLibTestServiceName);
993 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
994
995 EXPECT_EQ(-1, reply.readInt32());
996 EXPECT_EQ(100, previousWorkSource);
997 EXPECT_EQ(NO_ERROR, ret);
998}
999
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001000TEST_F(BinderLibTest, WorkSourceRestored)
1001{
1002 status_t ret;
1003 Parcel data, reply;
1004
1005 IPCThreadState::self()->setCallingWorkSourceUid(100);
1006 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1007 IPCThreadState::self()->restoreCallingWorkSource(token);
1008
1009 data.writeInterfaceToken(binderLibTestServiceName);
1010 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1011
1012 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001013 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001014 EXPECT_EQ(NO_ERROR, ret);
1015}
1016
Olivier Gaillard91a04802018-11-14 17:32:41 +00001017TEST_F(BinderLibTest, PropagateFlagSet)
1018{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001019 IPCThreadState::self()->clearPropagateWorkSource();
1020 IPCThreadState::self()->setCallingWorkSourceUid(100);
1021 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1022}
1023
1024TEST_F(BinderLibTest, PropagateFlagCleared)
1025{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001026 IPCThreadState::self()->setCallingWorkSourceUid(100);
1027 IPCThreadState::self()->clearPropagateWorkSource();
1028 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1029}
1030
1031TEST_F(BinderLibTest, PropagateFlagRestored)
1032{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001033 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1034 IPCThreadState::self()->restoreCallingWorkSource(token);
1035
1036 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1037}
1038
1039TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1040{
1041 IPCThreadState::self()->setCallingWorkSourceUid(100);
1042
1043 Parcel data, reply;
1044 status_t ret;
1045 data.writeInterfaceToken(binderLibTestServiceName);
1046 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1047
1048 Parcel data2, reply2;
1049 status_t ret2;
1050 data2.writeInterfaceToken(binderLibTestServiceName);
1051 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1052 EXPECT_EQ(100, reply2.readInt32());
1053 EXPECT_EQ(NO_ERROR, ret2);
1054}
1055
Steven Morelandbf1915b2020-07-16 22:43:02 +00001056TEST_F(BinderLibTest, SchedPolicySet) {
1057 sp<IBinder> server = addServer();
1058 ASSERT_TRUE(server != nullptr);
1059
1060 Parcel data, reply;
1061 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1062 EXPECT_EQ(NO_ERROR, ret);
1063
1064 int policy = reply.readInt32();
1065 int priority = reply.readInt32();
1066
1067 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1068 EXPECT_EQ(kSchedPriority, priority);
1069}
1070
1071
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001072TEST_F(BinderLibTest, VectorSent) {
1073 Parcel data, reply;
1074 sp<IBinder> server = addServer();
1075 ASSERT_TRUE(server != nullptr);
1076
1077 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1078 data.writeUint64Vector(testValue);
1079
1080 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1081 EXPECT_EQ(NO_ERROR, ret);
1082 std::vector<uint64_t> readValue;
1083 ret = reply.readUint64Vector(&readValue);
1084 EXPECT_EQ(readValue, testValue);
1085}
1086
Riley Andrews06b01ad2014-12-18 12:10:08 -08001087class BinderLibTestService : public BBinder
1088{
1089 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001090 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001091 : m_id(id)
1092 , m_nextServerId(id + 1)
1093 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001094 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001095 {
Yi Kong91635562018-06-07 14:38:36 -07001096 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1097 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001098 }
1099 ~BinderLibTestService()
1100 {
1101 exit(EXIT_SUCCESS);
1102 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001103
1104 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001105 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001106 Parcel data;
1107 data.writeInt32(NO_ERROR);
1108 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001109 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001110 }
1111 }
1112
Riley Andrews06b01ad2014-12-18 12:10:08 -08001113 virtual status_t onTransact(uint32_t code,
1114 const Parcel& data, Parcel* reply,
1115 uint32_t flags = 0) {
1116 //printf("%s: code %d\n", __func__, code);
1117 (void)flags;
1118
1119 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1120 return PERMISSION_DENIED;
1121 }
1122 switch (code) {
1123 case BINDER_LIB_TEST_REGISTER_SERVER: {
1124 int32_t id;
1125 sp<IBinder> binder;
1126 id = data.readInt32();
1127 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001128 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001129 return BAD_VALUE;
1130 }
1131
1132 if (m_id != 0)
1133 return INVALID_OPERATION;
1134
1135 pthread_mutex_lock(&m_serverWaitMutex);
1136 if (m_serverStartRequested) {
1137 m_serverStartRequested = false;
1138 m_serverStarted = binder;
1139 pthread_cond_signal(&m_serverWaitCond);
1140 }
1141 pthread_mutex_unlock(&m_serverWaitMutex);
1142 return NO_ERROR;
1143 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001144 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001145 case BINDER_LIB_TEST_ADD_SERVER: {
1146 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001147 int serverid;
1148
1149 if (m_id != 0) {
1150 return INVALID_OPERATION;
1151 }
1152 pthread_mutex_lock(&m_serverWaitMutex);
1153 if (m_serverStartRequested) {
1154 ret = -EBUSY;
1155 } else {
1156 serverid = m_nextServerId++;
1157 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001158 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001159
1160 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001161 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001162 pthread_mutex_lock(&m_serverWaitMutex);
1163 }
1164 if (ret > 0) {
1165 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001166 struct timespec ts;
1167 clock_gettime(CLOCK_REALTIME, &ts);
1168 ts.tv_sec += 5;
1169 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001170 }
1171 if (m_serverStartRequested) {
1172 m_serverStartRequested = false;
1173 ret = -ETIMEDOUT;
1174 } else {
1175 reply->writeStrongBinder(m_serverStarted);
1176 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001177 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001178 ret = NO_ERROR;
1179 }
1180 } else if (ret >= 0) {
1181 m_serverStartRequested = false;
1182 ret = UNKNOWN_ERROR;
1183 }
1184 pthread_mutex_unlock(&m_serverWaitMutex);
1185 return ret;
1186 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001187 case BINDER_LIB_TEST_GETPID:
1188 reply->writeInt32(getpid());
1189 return NO_ERROR;
1190 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1191 usleep(5000);
1192 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001193 case BINDER_LIB_TEST_NOP_TRANSACTION:
1194 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001195 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1196 // Note: this transaction is only designed for use with a
1197 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001198 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001199 // A callback was already pending; this means that
1200 // we received a second call while still processing
1201 // the first one. Fail the test.
1202 sp<IBinder> callback = data.readStrongBinder();
1203 Parcel data2;
1204 data2.writeInt32(UNKNOWN_ERROR);
1205
Yi Kong91635562018-06-07 14:38:36 -07001206 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001207 } else {
1208 m_callback = data.readStrongBinder();
1209 int32_t delayUs = data.readInt32();
1210 /*
1211 * It's necessary that we sleep here, so the next
1212 * transaction the caller makes will be queued to
1213 * the async queue.
1214 */
1215 usleep(delayUs);
1216
1217 /*
1218 * Now when we return, libbinder will tell the kernel
1219 * we are done with this transaction, and the kernel
1220 * can move the queued transaction to either the
1221 * thread todo worklist (for kernels without the fix),
1222 * or the proc todo worklist. In case of the former,
1223 * the next outbound call will pick up the pending
1224 * transaction, which leads to undesired reentrant
1225 * behavior. This is caught in the if() branch above.
1226 */
1227 }
1228
1229 return NO_ERROR;
1230 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001231 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1232 Parcel data2, reply2;
1233 sp<IBinder> binder;
1234 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001235 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001236 return BAD_VALUE;
1237 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001238 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001239 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1240 return NO_ERROR;
1241 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001242 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1243 reply->writeStrongBinder(this);
1244 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001245 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1246 reply->writeInt32(m_id);
1247 return NO_ERROR;
1248 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1249 int32_t count;
1250 uint32_t indirect_code;
1251 sp<IBinder> binder;
1252
1253 count = data.readInt32();
1254 reply->writeInt32(m_id);
1255 reply->writeInt32(count);
1256 for (int i = 0; i < count; i++) {
1257 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001258 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001259 return BAD_VALUE;
1260 }
1261 indirect_code = data.readInt32();
1262 BinderLibTestBundle data2(&data);
1263 if (!data2.isValid()) {
1264 return BAD_VALUE;
1265 }
1266 BinderLibTestBundle reply2;
1267 binder->transact(indirect_code, data2, &reply2);
1268 reply2.appendTo(reply);
1269 }
1270 return NO_ERROR;
1271 }
1272 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1273 reply->setError(data.readInt32());
1274 return NO_ERROR;
1275 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1276 reply->writeInt32(sizeof(void *));
1277 return NO_ERROR;
1278 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1279 return NO_ERROR;
1280 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1281 m_strongRef = data.readStrongBinder();
1282 return NO_ERROR;
1283 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1284 int ret;
1285 Parcel data2, reply2;
1286 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1287 sp<IBinder> target;
1288 sp<IBinder> callback;
1289
1290 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001291 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001292 return BAD_VALUE;
1293 }
1294 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001295 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001296 return BAD_VALUE;
1297 }
1298 ret = target->linkToDeath(testDeathRecipient);
1299 if (ret == NO_ERROR)
1300 ret = testDeathRecipient->waitEvent(5);
1301 data2.writeInt32(ret);
1302 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1303 return NO_ERROR;
1304 }
1305 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1306 int ret;
1307 int32_t size;
1308 const void *buf;
1309 int fd;
1310
1311 fd = data.readFileDescriptor();
1312 if (fd < 0) {
1313 return BAD_VALUE;
1314 }
1315 ret = data.readInt32(&size);
1316 if (ret != NO_ERROR) {
1317 return ret;
1318 }
1319 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001320 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001321 return BAD_VALUE;
1322 }
1323 ret = write(fd, buf, size);
1324 if (ret != size)
1325 return UNKNOWN_ERROR;
1326 return NO_ERROR;
1327 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001328 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1329 int ret;
1330 int32_t size;
1331 const void *buf;
1332 android::base::unique_fd fd;
1333
1334 ret = data.readUniqueParcelFileDescriptor(&fd);
1335 if (ret != NO_ERROR) {
1336 return ret;
1337 }
1338 ret = data.readInt32(&size);
1339 if (ret != NO_ERROR) {
1340 return ret;
1341 }
1342 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001343 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001344 return BAD_VALUE;
1345 }
1346 ret = write(fd.get(), buf, size);
1347 if (ret != size) return UNKNOWN_ERROR;
1348 return NO_ERROR;
1349 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001350 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1351 alarm(10);
1352 return NO_ERROR;
1353 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001354 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001355 ;
1356 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001357 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001358 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001359 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001360 return NO_ERROR;
1361 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001362 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1363 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001364 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001365 return NO_ERROR;
1366 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001367 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1368 int policy = 0;
1369 sched_param param;
1370 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1371 return UNKNOWN_ERROR;
1372 }
1373 reply->writeInt32(policy);
1374 reply->writeInt32(param.sched_priority);
1375 return NO_ERROR;
1376 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001377 case BINDER_LIB_TEST_ECHO_VECTOR: {
1378 std::vector<uint64_t> vector;
1379 auto err = data.readUint64Vector(&vector);
1380 if (err != NO_ERROR)
1381 return err;
1382 reply->writeUint64Vector(vector);
1383 return NO_ERROR;
1384 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001385 default:
1386 return UNKNOWN_TRANSACTION;
1387 };
1388 }
1389 private:
1390 int32_t m_id;
1391 int32_t m_nextServerId;
1392 pthread_mutex_t m_serverWaitMutex;
1393 pthread_cond_t m_serverWaitCond;
1394 bool m_serverStartRequested;
1395 sp<IBinder> m_serverStarted;
1396 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001397 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001398};
1399
Martijn Coenen45b07b42017-08-09 12:07:45 +02001400int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001401{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001402 binderLibTestServiceName += String16(binderserversuffix);
1403
Riley Andrews06b01ad2014-12-18 12:10:08 -08001404 status_t ret;
1405 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001406 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001407 {
1408 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001409
Steven Morelandbf1915b2020-07-16 22:43:02 +00001410 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1411
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001412 /*
1413 * Normally would also contain functionality as well, but we are only
1414 * testing the extension mechanism.
1415 */
1416 testService->setExtension(new BBinder());
1417
Martijn Coenen45b07b42017-08-09 12:07:45 +02001418 /*
1419 * We need this below, but can't hold a sp<> because it prevents the
1420 * node from being cleaned up automatically. It's safe in this case
1421 * because of how the tests are written.
1422 */
1423 testServicePtr = testService.get();
1424
Riley Andrews06b01ad2014-12-18 12:10:08 -08001425 if (index == 0) {
1426 ret = sm->addService(binderLibTestServiceName, testService);
1427 } else {
1428 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1429 Parcel data, reply;
1430 data.writeInt32(index);
1431 data.writeStrongBinder(testService);
1432
1433 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1434 }
1435 }
1436 write(readypipefd, &ret, sizeof(ret));
1437 close(readypipefd);
1438 //printf("%s: ret %d\n", __func__, ret);
1439 if (ret)
1440 return 1;
1441 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001442 if (usePoll) {
1443 int fd;
1444 struct epoll_event ev;
1445 int epoll_fd;
1446 IPCThreadState::self()->setupPolling(&fd);
1447 if (fd < 0) {
1448 return 1;
1449 }
1450 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1451
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001452 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001453 if (epoll_fd == -1) {
1454 return 1;
1455 }
1456
1457 ev.events = EPOLLIN;
1458 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1459 return 1;
1460 }
1461
1462 while (1) {
1463 /*
1464 * We simulate a single-threaded process using the binder poll
1465 * interface; besides handling binder commands, it can also
1466 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001467 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001468 *
1469 * processPendingCall() will then issue that transaction.
1470 */
1471 struct epoll_event events[1];
1472 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1473 if (numEvents < 0) {
1474 if (errno == EINTR) {
1475 continue;
1476 }
1477 return 1;
1478 }
1479 if (numEvents > 0) {
1480 IPCThreadState::self()->handlePolledCommands();
1481 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1482 testServicePtr->processPendingCall();
1483 }
1484 }
1485 } else {
1486 ProcessState::self()->startThreadPool();
1487 IPCThreadState::self()->joinThreadPool();
1488 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001489 //printf("%s: joinThreadPool returned\n", __func__);
1490 return 1; /* joinThreadPool should not return */
1491}
1492
1493int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001494 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001495 binderservername = argv[2];
1496 } else {
1497 binderservername = argv[0];
1498 }
1499
Martijn Coenen45b07b42017-08-09 12:07:45 +02001500 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1501 binderserversuffix = argv[5];
1502 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001503 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001504 binderserversuffix = new char[16];
1505 snprintf(binderserversuffix, 16, "%d", getpid());
1506 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001507
1508 ::testing::InitGoogleTest(&argc, argv);
1509 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1510 ProcessState::self()->startThreadPool();
1511 return RUN_ALL_TESTS();
1512}