blob: e51bcebba2b53afe6692ff1222228efad08b5730 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <poll.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <gtest/gtest.h>
25
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30
Martijn Coenen45b07b42017-08-09 12:07:45 +020031#include <sys/epoll.h>
32
Riley Andrews06b01ad2014-12-18 12:10:08 -080033#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
34
35using namespace android;
36
Sherry Yang336cdd32017-07-24 14:12:27 -070037static ::testing::AssertionResult IsPageAligned(void *buf) {
38 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
39 return ::testing::AssertionSuccess();
40 else
41 return ::testing::AssertionFailure() << buf << " is not page aligned";
42}
43
Riley Andrews06b01ad2014-12-18 12:10:08 -080044static testing::Environment* binder_env;
45static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070046static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080047static char binderserverarg[] = "--binderserver";
48
49static String16 binderLibTestServiceName = String16("test.binderLib");
50
51enum BinderLibTestTranscationCode {
52 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
53 BINDER_LIB_TEST_REGISTER_SERVER,
54 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020055 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080056 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070057 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020058 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080059 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070060 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_GET_ID_TRANSACTION,
62 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
63 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
64 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
65 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
66 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
67 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090068 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION,
70 BINDER_LIB_TEST_EXIT_TRANSACTION,
71 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
72 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070073 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010074 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080075 BINDER_LIB_TEST_ECHO_VECTOR,
Riley Andrews06b01ad2014-12-18 12:10:08 -080076};
77
Martijn Coenen45b07b42017-08-09 12:07:45 +020078pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080079{
80 int ret;
81 pid_t pid;
82 status_t status;
83 int pipefd[2];
84 char stri[16];
85 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020086 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080087 char *childargv[] = {
88 binderservername,
89 binderserverarg,
90 stri,
91 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020092 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070093 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -070094 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080095 };
96
97 ret = pipe(pipefd);
98 if (ret < 0)
99 return ret;
100
101 snprintf(stri, sizeof(stri), "%d", arg2);
102 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200103 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800104
105 pid = fork();
106 if (pid == -1)
107 return pid;
108 if (pid == 0) {
109 close(pipefd[0]);
110 execv(binderservername, childargv);
111 status = -errno;
112 write(pipefd[1], &status, sizeof(status));
113 fprintf(stderr, "execv failed, %s\n", strerror(errno));
114 _exit(EXIT_FAILURE);
115 }
116 close(pipefd[1]);
117 ret = read(pipefd[0], &status, sizeof(status));
118 //printf("pipe read returned %d, status %d\n", ret, status);
119 close(pipefd[0]);
120 if (ret == sizeof(status)) {
121 ret = status;
122 } else {
123 kill(pid, SIGKILL);
124 if (ret >= 0) {
125 ret = NO_INIT;
126 }
127 }
128 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700129 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800130 return ret;
131 }
132 return pid;
133}
134
135class BinderLibTestEnv : public ::testing::Environment {
136 public:
137 BinderLibTestEnv() {}
138 sp<IBinder> getServer(void) {
139 return m_server;
140 }
141
142 private:
143 virtual void SetUp() {
144 m_serverpid = start_server_process(0);
145 //printf("m_serverpid %d\n", m_serverpid);
146 ASSERT_GT(m_serverpid, 0);
147
148 sp<IServiceManager> sm = defaultServiceManager();
149 //printf("%s: pid %d, get service\n", __func__, m_pid);
150 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700151 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800152 //printf("%s: pid %d, get service done\n", __func__, m_pid);
153 }
154 virtual void TearDown() {
155 status_t ret;
156 Parcel data, reply;
157 int exitStatus;
158 pid_t pid;
159
160 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700161 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800162 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
163 EXPECT_EQ(0, ret);
164 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
165 EXPECT_EQ(0, ret);
166 }
167 if (m_serverpid > 0) {
168 //printf("wait for %d\n", m_pids[i]);
169 pid = wait(&exitStatus);
170 EXPECT_EQ(m_serverpid, pid);
171 EXPECT_TRUE(WIFEXITED(exitStatus));
172 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
173 }
174 }
175
176 pid_t m_serverpid;
177 sp<IBinder> m_server;
178};
179
180class BinderLibTest : public ::testing::Test {
181 public:
182 virtual void SetUp() {
183 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000184 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800185 }
186 virtual void TearDown() {
187 }
188 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200189 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800190 {
191 int ret;
192 int32_t id;
193 Parcel data, reply;
194 sp<IBinder> binder;
195
Martijn Coenen45b07b42017-08-09 12:07:45 +0200196 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800197 EXPECT_EQ(NO_ERROR, ret);
198
Yi Kong91635562018-06-07 14:38:36 -0700199 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800200 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700201 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 ret = reply.readInt32(&id);
203 EXPECT_EQ(NO_ERROR, ret);
204 if (idPtr)
205 *idPtr = id;
206 return binder;
207 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200208
Yi Kong91635562018-06-07 14:38:36 -0700209 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200210 {
211 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
212 }
213
Yi Kong91635562018-06-07 14:38:36 -0700214 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200215 {
216 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
217 }
218
Riley Andrews06b01ad2014-12-18 12:10:08 -0800219 void waitForReadData(int fd, int timeout_ms) {
220 int ret;
221 pollfd pfd = pollfd();
222
223 pfd.fd = fd;
224 pfd.events = POLLIN;
225 ret = poll(&pfd, 1, timeout_ms);
226 EXPECT_EQ(1, ret);
227 }
228
229 sp<IBinder> m_server;
230};
231
232class BinderLibTestBundle : public Parcel
233{
234 public:
235 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800236 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800237 int32_t mark;
238 int32_t bundleLen;
239 size_t pos;
240
241 if (source->readInt32(&mark))
242 return;
243 if (mark != MARK_START)
244 return;
245 if (source->readInt32(&bundleLen))
246 return;
247 pos = source->dataPosition();
248 if (Parcel::appendFrom(source, pos, bundleLen))
249 return;
250 source->setDataPosition(pos + bundleLen);
251 if (source->readInt32(&mark))
252 return;
253 if (mark != MARK_END)
254 return;
255 m_isValid = true;
256 setDataPosition(0);
257 }
258 void appendTo(Parcel *dest) {
259 dest->writeInt32(MARK_START);
260 dest->writeInt32(dataSize());
261 dest->appendFrom(this, 0, dataSize());
262 dest->writeInt32(MARK_END);
263 };
264 bool isValid(void) {
265 return m_isValid;
266 }
267 private:
268 enum {
269 MARK_START = B_PACK_CHARS('B','T','B','S'),
270 MARK_END = B_PACK_CHARS('B','T','B','E'),
271 };
272 bool m_isValid;
273};
274
275class BinderLibTestEvent
276{
277 public:
278 BinderLibTestEvent(void)
279 : m_eventTriggered(false)
280 {
Yi Kong91635562018-06-07 14:38:36 -0700281 pthread_mutex_init(&m_waitMutex, nullptr);
282 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800283 }
284 int waitEvent(int timeout_s)
285 {
286 int ret;
287 pthread_mutex_lock(&m_waitMutex);
288 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800289 struct timespec ts;
290 clock_gettime(CLOCK_REALTIME, &ts);
291 ts.tv_sec += timeout_s;
292 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800293 }
294 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
295 pthread_mutex_unlock(&m_waitMutex);
296 return ret;
297 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200298 pthread_t getTriggeringThread()
299 {
300 return m_triggeringThread;
301 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800302 protected:
303 void triggerEvent(void) {
304 pthread_mutex_lock(&m_waitMutex);
305 pthread_cond_signal(&m_waitCond);
306 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200307 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800308 pthread_mutex_unlock(&m_waitMutex);
309 };
310 private:
311 pthread_mutex_t m_waitMutex;
312 pthread_cond_t m_waitCond;
313 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200314 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800315};
316
317class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
318{
319 public:
320 BinderLibTestCallBack()
321 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700322 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800323 {
324 }
325 status_t getResult(void)
326 {
327 return m_result;
328 }
329
330 private:
331 virtual status_t onTransact(uint32_t code,
332 const Parcel& data, Parcel* reply,
333 uint32_t flags = 0)
334 {
335 (void)reply;
336 (void)flags;
337 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200338 case BINDER_LIB_TEST_CALL_BACK: {
339 status_t status = data.readInt32(&m_result);
340 if (status != NO_ERROR) {
341 m_result = status;
342 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800343 triggerEvent();
344 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200345 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700346 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
347 sp<IBinder> server;
348 int ret;
349 const uint8_t *buf = data.data();
350 size_t size = data.dataSize();
351 if (m_prev_end) {
352 /* 64-bit kernel needs at most 8 bytes to align buffer end */
353 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
354 } else {
355 EXPECT_TRUE(IsPageAligned((void *)buf));
356 }
357
358 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
359
360 if (size > 0) {
361 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
362 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
363 data, reply);
364 EXPECT_EQ(NO_ERROR, ret);
365 }
366 return NO_ERROR;
367 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800368 default:
369 return UNKNOWN_TRANSACTION;
370 }
371 }
372
373 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700374 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800375};
376
377class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
378{
379 private:
380 virtual void binderDied(const wp<IBinder>& who) {
381 (void)who;
382 triggerEvent();
383 };
384};
385
386TEST_F(BinderLibTest, NopTransaction) {
387 status_t ret;
388 Parcel data, reply;
389 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
390 EXPECT_EQ(NO_ERROR, ret);
391}
392
393TEST_F(BinderLibTest, SetError) {
394 int32_t testValue[] = { 0, -123, 123 };
395 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
396 status_t ret;
397 Parcel data, reply;
398 data.writeInt32(testValue[i]);
399 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
400 EXPECT_EQ(testValue[i], ret);
401 }
402}
403
404TEST_F(BinderLibTest, GetId) {
405 status_t ret;
406 int32_t id;
407 Parcel data, reply;
408 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
409 EXPECT_EQ(NO_ERROR, ret);
410 ret = reply.readInt32(&id);
411 EXPECT_EQ(NO_ERROR, ret);
412 EXPECT_EQ(0, id);
413}
414
415TEST_F(BinderLibTest, PtrSize) {
416 status_t ret;
417 int32_t ptrsize;
418 Parcel data, reply;
419 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700420 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800421 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
422 EXPECT_EQ(NO_ERROR, ret);
423 ret = reply.readInt32(&ptrsize);
424 EXPECT_EQ(NO_ERROR, ret);
425 RecordProperty("TestPtrSize", sizeof(void *));
426 RecordProperty("ServerPtrSize", sizeof(void *));
427}
428
429TEST_F(BinderLibTest, IndirectGetId2)
430{
431 status_t ret;
432 int32_t id;
433 int32_t count;
434 Parcel data, reply;
435 int32_t serverId[3];
436
437 data.writeInt32(ARRAY_SIZE(serverId));
438 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
439 sp<IBinder> server;
440 BinderLibTestBundle datai;
441
442 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700443 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800444 data.writeStrongBinder(server);
445 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
446 datai.appendTo(&data);
447 }
448
449 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
450 ASSERT_EQ(NO_ERROR, ret);
451
452 ret = reply.readInt32(&id);
453 ASSERT_EQ(NO_ERROR, ret);
454 EXPECT_EQ(0, id);
455
456 ret = reply.readInt32(&count);
457 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700458 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800459
460 for (size_t i = 0; i < (size_t)count; i++) {
461 BinderLibTestBundle replyi(&reply);
462 EXPECT_TRUE(replyi.isValid());
463 ret = replyi.readInt32(&id);
464 EXPECT_EQ(NO_ERROR, ret);
465 EXPECT_EQ(serverId[i], id);
466 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
467 }
468
469 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
470}
471
472TEST_F(BinderLibTest, IndirectGetId3)
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 BinderLibTestBundle datai2;
485
486 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700487 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800488 data.writeStrongBinder(server);
489 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
490
491 datai.writeInt32(1);
492 datai.writeStrongBinder(m_server);
493 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
494 datai2.appendTo(&datai);
495
496 datai.appendTo(&data);
497 }
498
499 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
500 ASSERT_EQ(NO_ERROR, ret);
501
502 ret = reply.readInt32(&id);
503 ASSERT_EQ(NO_ERROR, ret);
504 EXPECT_EQ(0, id);
505
506 ret = reply.readInt32(&count);
507 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700508 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800509
510 for (size_t i = 0; i < (size_t)count; i++) {
511 int32_t counti;
512
513 BinderLibTestBundle replyi(&reply);
514 EXPECT_TRUE(replyi.isValid());
515 ret = replyi.readInt32(&id);
516 EXPECT_EQ(NO_ERROR, ret);
517 EXPECT_EQ(serverId[i], id);
518
519 ret = replyi.readInt32(&counti);
520 ASSERT_EQ(NO_ERROR, ret);
521 EXPECT_EQ(1, counti);
522
523 BinderLibTestBundle replyi2(&replyi);
524 EXPECT_TRUE(replyi2.isValid());
525 ret = replyi2.readInt32(&id);
526 EXPECT_EQ(NO_ERROR, ret);
527 EXPECT_EQ(0, id);
528 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
529
530 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
531 }
532
533 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
534}
535
536TEST_F(BinderLibTest, CallBack)
537{
538 status_t ret;
539 Parcel data, reply;
540 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
541 data.writeStrongBinder(callBack);
542 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
543 EXPECT_EQ(NO_ERROR, ret);
544 ret = callBack->waitEvent(5);
545 EXPECT_EQ(NO_ERROR, ret);
546 ret = callBack->getResult();
547 EXPECT_EQ(NO_ERROR, ret);
548}
549
550TEST_F(BinderLibTest, AddServer)
551{
552 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700553 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800554}
555
Riley Andrews06b01ad2014-12-18 12:10:08 -0800556TEST_F(BinderLibTest, DeathNotificationStrongRef)
557{
558 status_t ret;
559 sp<IBinder> sbinder;
560
561 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
562
563 {
564 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700565 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800566 ret = binder->linkToDeath(testDeathRecipient);
567 EXPECT_EQ(NO_ERROR, ret);
568 sbinder = binder;
569 }
570 {
571 Parcel data, reply;
572 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
573 EXPECT_EQ(0, ret);
574 }
575 IPCThreadState::self()->flushCommands();
576 ret = testDeathRecipient->waitEvent(5);
577 EXPECT_EQ(NO_ERROR, ret);
578 ret = sbinder->unlinkToDeath(testDeathRecipient);
579 EXPECT_EQ(DEAD_OBJECT, ret);
580}
581
582TEST_F(BinderLibTest, DeathNotificationMultiple)
583{
584 status_t ret;
585 const int clientcount = 2;
586 sp<IBinder> target;
587 sp<IBinder> linkedclient[clientcount];
588 sp<BinderLibTestCallBack> callBack[clientcount];
589 sp<IBinder> passiveclient[clientcount];
590
591 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700592 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800593 for (int i = 0; i < clientcount; i++) {
594 {
595 Parcel data, reply;
596
597 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700598 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800599 callBack[i] = new BinderLibTestCallBack();
600 data.writeStrongBinder(target);
601 data.writeStrongBinder(callBack[i]);
602 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
603 EXPECT_EQ(NO_ERROR, ret);
604 }
605 {
606 Parcel data, reply;
607
608 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700609 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800610 data.writeStrongBinder(target);
611 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
612 EXPECT_EQ(NO_ERROR, ret);
613 }
614 }
615 {
616 Parcel data, reply;
617 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
618 EXPECT_EQ(0, ret);
619 }
620
621 for (int i = 0; i < clientcount; i++) {
622 ret = callBack[i]->waitEvent(5);
623 EXPECT_EQ(NO_ERROR, ret);
624 ret = callBack[i]->getResult();
625 EXPECT_EQ(NO_ERROR, ret);
626 }
627}
628
Martijn Coenenf7100e42017-07-31 12:14:09 +0200629TEST_F(BinderLibTest, DeathNotificationThread)
630{
631 status_t ret;
632 sp<BinderLibTestCallBack> callback;
633 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700634 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200635 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700636 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200637
638 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
639
640 ret = target->linkToDeath(testDeathRecipient);
641 EXPECT_EQ(NO_ERROR, ret);
642
643 {
644 Parcel data, reply;
645 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
646 EXPECT_EQ(0, ret);
647 }
648
649 /* Make sure it's dead */
650 testDeathRecipient->waitEvent(5);
651
652 /* Now, pass the ref to another process and ask that process to
653 * call linkToDeath() on it, and wait for a response. This tests
654 * two things:
655 * 1) You still get death notifications when calling linkToDeath()
656 * on a ref that is already dead when it was passed to you.
657 * 2) That death notifications are not directly pushed to the thread
658 * registering them, but to the threadpool (proc workqueue) instead.
659 *
660 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
661 * is blocked on a condition variable waiting for the death notification to be
662 * called; therefore, that thread is not available for handling proc work.
663 * So, if the death notification was pushed to the thread workqueue, the callback
664 * would never be called, and the test would timeout and fail.
665 *
666 * Note that we can't do this part of the test from this thread itself, because
667 * the binder driver would only push death notifications to the thread if
668 * it is a looper thread, which this thread is not.
669 *
670 * See b/23525545 for details.
671 */
672 {
673 Parcel data, reply;
674
675 callback = new BinderLibTestCallBack();
676 data.writeStrongBinder(target);
677 data.writeStrongBinder(callback);
678 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
679 EXPECT_EQ(NO_ERROR, ret);
680 }
681
682 ret = callback->waitEvent(5);
683 EXPECT_EQ(NO_ERROR, ret);
684 ret = callback->getResult();
685 EXPECT_EQ(NO_ERROR, ret);
686}
687
Riley Andrews06b01ad2014-12-18 12:10:08 -0800688TEST_F(BinderLibTest, PassFile) {
689 int ret;
690 int pipefd[2];
691 uint8_t buf[1] = { 0 };
692 uint8_t write_value = 123;
693
694 ret = pipe2(pipefd, O_NONBLOCK);
695 ASSERT_EQ(0, ret);
696
697 {
698 Parcel data, reply;
699 uint8_t writebuf[1] = { write_value };
700
701 ret = data.writeFileDescriptor(pipefd[1], true);
702 EXPECT_EQ(NO_ERROR, ret);
703
704 ret = data.writeInt32(sizeof(writebuf));
705 EXPECT_EQ(NO_ERROR, ret);
706
707 ret = data.write(writebuf, sizeof(writebuf));
708 EXPECT_EQ(NO_ERROR, ret);
709
710 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
711 EXPECT_EQ(NO_ERROR, ret);
712 }
713
714 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700715 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800716 EXPECT_EQ(write_value, buf[0]);
717
718 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
719
720 ret = read(pipefd[0], buf, sizeof(buf));
721 EXPECT_EQ(0, ret);
722
723 close(pipefd[0]);
724}
725
Ryo Hashimotobf551892018-05-31 16:58:35 +0900726TEST_F(BinderLibTest, PassParcelFileDescriptor) {
727 const int datasize = 123;
728 std::vector<uint8_t> writebuf(datasize);
729 for (size_t i = 0; i < writebuf.size(); ++i) {
730 writebuf[i] = i;
731 }
732
733 android::base::unique_fd read_end, write_end;
734 {
735 int pipefd[2];
736 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
737 read_end.reset(pipefd[0]);
738 write_end.reset(pipefd[1]);
739 }
740 {
741 Parcel data;
742 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
743 write_end.reset();
744 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
745 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
746
747 Parcel reply;
748 EXPECT_EQ(NO_ERROR,
749 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
750 &reply));
751 }
752 std::vector<uint8_t> readbuf(datasize);
753 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
754 EXPECT_EQ(writebuf, readbuf);
755
756 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
757
758 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
759}
760
Riley Andrews06b01ad2014-12-18 12:10:08 -0800761TEST_F(BinderLibTest, PromoteLocal) {
762 sp<IBinder> strong = new BBinder();
763 wp<IBinder> weak = strong;
764 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700765 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800766 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700767 strong = nullptr;
768 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800769 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700770 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800771}
772
773TEST_F(BinderLibTest, PromoteRemote) {
774 int ret;
775 Parcel data, reply;
776 sp<IBinder> strong = new BBinder();
777 sp<IBinder> server = addServer();
778
Yi Kong91635562018-06-07 14:38:36 -0700779 ASSERT_TRUE(server != nullptr);
780 ASSERT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800781
782 ret = data.writeWeakBinder(strong);
783 EXPECT_EQ(NO_ERROR, ret);
784
785 ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
786 EXPECT_GE(ret, 0);
787}
788
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700789TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
790 status_t ret;
791 Parcel data, reply;
792
793 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
794 EXPECT_EQ(NO_ERROR, ret);
795
796 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700797 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800798 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
799 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
800 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
801 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700802}
803
Connor O'Brien52be2c92016-09-20 14:18:08 -0700804TEST_F(BinderLibTest, FreedBinder) {
805 status_t ret;
806
807 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700808 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700809
810 __u32 freedHandle;
811 wp<IBinder> keepFreedBinder;
812 {
813 Parcel data, reply;
814 data.writeBool(false); /* request weak reference */
815 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
816 ASSERT_EQ(NO_ERROR, ret);
817 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
818 freedHandle = freed->handle;
819 /* Add a weak ref to the freed binder so the driver does not
820 * delete its reference to it - otherwise the transaction
821 * fails regardless of whether the driver is fixed.
822 */
823 keepFreedBinder = reply.readWeakBinder();
824 }
825 {
826 Parcel data, reply;
827 data.writeStrongBinder(server);
828 /* Replace original handle with handle to the freed binder */
829 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
830 __u32 oldHandle = strong->handle;
831 strong->handle = freedHandle;
832 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
833 /* Returns DEAD_OBJECT (-32) if target crashes and
834 * FAILED_TRANSACTION if the driver rejects the invalid
835 * object.
836 */
837 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
838 /* Restore original handle so parcel destructor does not use
839 * the wrong handle.
840 */
841 strong->handle = oldHandle;
842 }
843}
844
Sherry Yang336cdd32017-07-24 14:12:27 -0700845TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
846 status_t ret;
847 Parcel data, reply;
848 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
849 for (int i = 0; i < 2; i++) {
850 BinderLibTestBundle datai;
851 datai.appendFrom(&data, 0, data.dataSize());
852
853 data.freeData();
854 data.writeInt32(1);
855 data.writeStrongBinder(callBack);
856 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
857
858 datai.appendTo(&data);
859 }
860 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
861 EXPECT_EQ(NO_ERROR, ret);
862}
863
Martijn Coenen45b07b42017-08-09 12:07:45 +0200864TEST_F(BinderLibTest, OnewayQueueing)
865{
866 status_t ret;
867 Parcel data, data2;
868
869 sp<IBinder> pollServer = addPollServer();
870
871 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
872 data.writeStrongBinder(callBack);
873 data.writeInt32(500000); // delay in us before calling back
874
875 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
876 data2.writeStrongBinder(callBack2);
877 data2.writeInt32(0); // delay in us
878
Yi Kong91635562018-06-07 14:38:36 -0700879 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200880 EXPECT_EQ(NO_ERROR, ret);
881
882 // The delay ensures that this second transaction will end up on the async_todo list
883 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700884 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200885 EXPECT_EQ(NO_ERROR, ret);
886
887 // The server will ensure that the two transactions are handled in the expected order;
888 // If the ordering is not as expected, an error will be returned through the callbacks.
889 ret = callBack->waitEvent(2);
890 EXPECT_EQ(NO_ERROR, ret);
891 ret = callBack->getResult();
892 EXPECT_EQ(NO_ERROR, ret);
893
894 ret = callBack2->waitEvent(2);
895 EXPECT_EQ(NO_ERROR, ret);
896 ret = callBack2->getResult();
897 EXPECT_EQ(NO_ERROR, ret);
898}
899
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100900TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
901{
902 status_t ret;
903 Parcel data, reply;
904 data.writeInterfaceToken(binderLibTestServiceName);
905 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
906 EXPECT_EQ(-1, reply.readInt32());
907 EXPECT_EQ(NO_ERROR, ret);
908}
909
910TEST_F(BinderLibTest, WorkSourceSet)
911{
912 status_t ret;
913 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000914 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000915 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100916 data.writeInterfaceToken(binderLibTestServiceName);
917 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
918 EXPECT_EQ(100, reply.readInt32());
919 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000920 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
921 EXPECT_EQ(NO_ERROR, ret);
922}
923
924TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
925{
926 status_t ret;
927 Parcel data, reply;
928
929 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
930 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
931
932 data.writeInterfaceToken(binderLibTestServiceName);
933 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
934 EXPECT_EQ(-1, reply.readInt32());
935 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100936 EXPECT_EQ(NO_ERROR, ret);
937}
938
939TEST_F(BinderLibTest, WorkSourceCleared)
940{
941 status_t ret;
942 Parcel data, reply;
943
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000944 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000945 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
946 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100947 data.writeInterfaceToken(binderLibTestServiceName);
948 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
949
950 EXPECT_EQ(-1, reply.readInt32());
951 EXPECT_EQ(100, previousWorkSource);
952 EXPECT_EQ(NO_ERROR, ret);
953}
954
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000955TEST_F(BinderLibTest, WorkSourceRestored)
956{
957 status_t ret;
958 Parcel data, reply;
959
960 IPCThreadState::self()->setCallingWorkSourceUid(100);
961 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
962 IPCThreadState::self()->restoreCallingWorkSource(token);
963
964 data.writeInterfaceToken(binderLibTestServiceName);
965 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
966
967 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000968 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000969 EXPECT_EQ(NO_ERROR, ret);
970}
971
Olivier Gaillard91a04802018-11-14 17:32:41 +0000972TEST_F(BinderLibTest, PropagateFlagSet)
973{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000974 IPCThreadState::self()->clearPropagateWorkSource();
975 IPCThreadState::self()->setCallingWorkSourceUid(100);
976 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
977}
978
979TEST_F(BinderLibTest, PropagateFlagCleared)
980{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000981 IPCThreadState::self()->setCallingWorkSourceUid(100);
982 IPCThreadState::self()->clearPropagateWorkSource();
983 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
984}
985
986TEST_F(BinderLibTest, PropagateFlagRestored)
987{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000988 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
989 IPCThreadState::self()->restoreCallingWorkSource(token);
990
991 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
992}
993
994TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
995{
996 IPCThreadState::self()->setCallingWorkSourceUid(100);
997
998 Parcel data, reply;
999 status_t ret;
1000 data.writeInterfaceToken(binderLibTestServiceName);
1001 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1002
1003 Parcel data2, reply2;
1004 status_t ret2;
1005 data2.writeInterfaceToken(binderLibTestServiceName);
1006 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1007 EXPECT_EQ(100, reply2.readInt32());
1008 EXPECT_EQ(NO_ERROR, ret2);
1009}
1010
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001011TEST_F(BinderLibTest, VectorSent) {
1012 Parcel data, reply;
1013 sp<IBinder> server = addServer();
1014 ASSERT_TRUE(server != nullptr);
1015
1016 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1017 data.writeUint64Vector(testValue);
1018
1019 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1020 EXPECT_EQ(NO_ERROR, ret);
1021 std::vector<uint64_t> readValue;
1022 ret = reply.readUint64Vector(&readValue);
1023 EXPECT_EQ(readValue, testValue);
1024}
1025
Riley Andrews06b01ad2014-12-18 12:10:08 -08001026class BinderLibTestService : public BBinder
1027{
1028 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001029 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001030 : m_id(id)
1031 , m_nextServerId(id + 1)
1032 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001033 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001034 {
Yi Kong91635562018-06-07 14:38:36 -07001035 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1036 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001037 }
1038 ~BinderLibTestService()
1039 {
1040 exit(EXIT_SUCCESS);
1041 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001042
1043 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001044 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001045 Parcel data;
1046 data.writeInt32(NO_ERROR);
1047 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001048 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001049 }
1050 }
1051
Riley Andrews06b01ad2014-12-18 12:10:08 -08001052 virtual status_t onTransact(uint32_t code,
1053 const Parcel& data, Parcel* reply,
1054 uint32_t flags = 0) {
1055 //printf("%s: code %d\n", __func__, code);
1056 (void)flags;
1057
1058 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1059 return PERMISSION_DENIED;
1060 }
1061 switch (code) {
1062 case BINDER_LIB_TEST_REGISTER_SERVER: {
1063 int32_t id;
1064 sp<IBinder> binder;
1065 id = data.readInt32();
1066 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001067 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001068 return BAD_VALUE;
1069 }
1070
1071 if (m_id != 0)
1072 return INVALID_OPERATION;
1073
1074 pthread_mutex_lock(&m_serverWaitMutex);
1075 if (m_serverStartRequested) {
1076 m_serverStartRequested = false;
1077 m_serverStarted = binder;
1078 pthread_cond_signal(&m_serverWaitCond);
1079 }
1080 pthread_mutex_unlock(&m_serverWaitMutex);
1081 return NO_ERROR;
1082 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001083 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001084 case BINDER_LIB_TEST_ADD_SERVER: {
1085 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001086 int serverid;
1087
1088 if (m_id != 0) {
1089 return INVALID_OPERATION;
1090 }
1091 pthread_mutex_lock(&m_serverWaitMutex);
1092 if (m_serverStartRequested) {
1093 ret = -EBUSY;
1094 } else {
1095 serverid = m_nextServerId++;
1096 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001097 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001098
1099 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001100 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001101 pthread_mutex_lock(&m_serverWaitMutex);
1102 }
1103 if (ret > 0) {
1104 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001105 struct timespec ts;
1106 clock_gettime(CLOCK_REALTIME, &ts);
1107 ts.tv_sec += 5;
1108 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001109 }
1110 if (m_serverStartRequested) {
1111 m_serverStartRequested = false;
1112 ret = -ETIMEDOUT;
1113 } else {
1114 reply->writeStrongBinder(m_serverStarted);
1115 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001116 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001117 ret = NO_ERROR;
1118 }
1119 } else if (ret >= 0) {
1120 m_serverStartRequested = false;
1121 ret = UNKNOWN_ERROR;
1122 }
1123 pthread_mutex_unlock(&m_serverWaitMutex);
1124 return ret;
1125 }
1126 case BINDER_LIB_TEST_NOP_TRANSACTION:
1127 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001128 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1129 // Note: this transaction is only designed for use with a
1130 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001131 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001132 // A callback was already pending; this means that
1133 // we received a second call while still processing
1134 // the first one. Fail the test.
1135 sp<IBinder> callback = data.readStrongBinder();
1136 Parcel data2;
1137 data2.writeInt32(UNKNOWN_ERROR);
1138
Yi Kong91635562018-06-07 14:38:36 -07001139 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001140 } else {
1141 m_callback = data.readStrongBinder();
1142 int32_t delayUs = data.readInt32();
1143 /*
1144 * It's necessary that we sleep here, so the next
1145 * transaction the caller makes will be queued to
1146 * the async queue.
1147 */
1148 usleep(delayUs);
1149
1150 /*
1151 * Now when we return, libbinder will tell the kernel
1152 * we are done with this transaction, and the kernel
1153 * can move the queued transaction to either the
1154 * thread todo worklist (for kernels without the fix),
1155 * or the proc todo worklist. In case of the former,
1156 * the next outbound call will pick up the pending
1157 * transaction, which leads to undesired reentrant
1158 * behavior. This is caught in the if() branch above.
1159 */
1160 }
1161
1162 return NO_ERROR;
1163 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001164 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1165 Parcel data2, reply2;
1166 sp<IBinder> binder;
1167 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001168 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001169 return BAD_VALUE;
1170 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001171 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001172 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1173 return NO_ERROR;
1174 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001175 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1176 reply->writeStrongBinder(this);
1177 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001178 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1179 reply->writeInt32(m_id);
1180 return NO_ERROR;
1181 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1182 int32_t count;
1183 uint32_t indirect_code;
1184 sp<IBinder> binder;
1185
1186 count = data.readInt32();
1187 reply->writeInt32(m_id);
1188 reply->writeInt32(count);
1189 for (int i = 0; i < count; i++) {
1190 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001191 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001192 return BAD_VALUE;
1193 }
1194 indirect_code = data.readInt32();
1195 BinderLibTestBundle data2(&data);
1196 if (!data2.isValid()) {
1197 return BAD_VALUE;
1198 }
1199 BinderLibTestBundle reply2;
1200 binder->transact(indirect_code, data2, &reply2);
1201 reply2.appendTo(reply);
1202 }
1203 return NO_ERROR;
1204 }
1205 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1206 reply->setError(data.readInt32());
1207 return NO_ERROR;
1208 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1209 reply->writeInt32(sizeof(void *));
1210 return NO_ERROR;
1211 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1212 return NO_ERROR;
1213 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1214 m_strongRef = data.readStrongBinder();
1215 return NO_ERROR;
1216 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1217 int ret;
1218 Parcel data2, reply2;
1219 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1220 sp<IBinder> target;
1221 sp<IBinder> callback;
1222
1223 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001224 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001225 return BAD_VALUE;
1226 }
1227 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001228 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001229 return BAD_VALUE;
1230 }
1231 ret = target->linkToDeath(testDeathRecipient);
1232 if (ret == NO_ERROR)
1233 ret = testDeathRecipient->waitEvent(5);
1234 data2.writeInt32(ret);
1235 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1236 return NO_ERROR;
1237 }
1238 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1239 int ret;
1240 int32_t size;
1241 const void *buf;
1242 int fd;
1243
1244 fd = data.readFileDescriptor();
1245 if (fd < 0) {
1246 return BAD_VALUE;
1247 }
1248 ret = data.readInt32(&size);
1249 if (ret != NO_ERROR) {
1250 return ret;
1251 }
1252 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001253 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001254 return BAD_VALUE;
1255 }
1256 ret = write(fd, buf, size);
1257 if (ret != size)
1258 return UNKNOWN_ERROR;
1259 return NO_ERROR;
1260 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001261 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1262 int ret;
1263 int32_t size;
1264 const void *buf;
1265 android::base::unique_fd fd;
1266
1267 ret = data.readUniqueParcelFileDescriptor(&fd);
1268 if (ret != NO_ERROR) {
1269 return ret;
1270 }
1271 ret = data.readInt32(&size);
1272 if (ret != NO_ERROR) {
1273 return ret;
1274 }
1275 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001276 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001277 return BAD_VALUE;
1278 }
1279 ret = write(fd.get(), buf, size);
1280 if (ret != size) return UNKNOWN_ERROR;
1281 return NO_ERROR;
1282 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001283 case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
1284 int ret;
1285 wp<IBinder> weak;
1286 sp<IBinder> strong;
1287 Parcel data2, reply2;
1288 sp<IServiceManager> sm = defaultServiceManager();
1289 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1290
1291 weak = data.readWeakBinder();
Yi Kong91635562018-06-07 14:38:36 -07001292 if (weak == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001293 return BAD_VALUE;
1294 }
1295 strong = weak.promote();
1296
1297 ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
1298 if (ret != NO_ERROR)
1299 exit(EXIT_FAILURE);
1300
Yi Kong91635562018-06-07 14:38:36 -07001301 if (strong == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001302 reply->setError(1);
1303 }
1304 return NO_ERROR;
1305 }
1306 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1307 alarm(10);
1308 return NO_ERROR;
1309 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001310 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001311 ;
1312 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001313 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
1314 bool strongRef = data.readBool();
1315 sp<IBinder> binder = new BBinder();
1316 if (strongRef) {
1317 reply->writeStrongBinder(binder);
1318 } else {
1319 reply->writeWeakBinder(binder);
1320 }
1321 return NO_ERROR;
1322 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001323 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1324 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001325 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001326 return NO_ERROR;
1327 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001328 case BINDER_LIB_TEST_ECHO_VECTOR: {
1329 std::vector<uint64_t> vector;
1330 auto err = data.readUint64Vector(&vector);
1331 if (err != NO_ERROR)
1332 return err;
1333 reply->writeUint64Vector(vector);
1334 return NO_ERROR;
1335 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001336 default:
1337 return UNKNOWN_TRANSACTION;
1338 };
1339 }
1340 private:
1341 int32_t m_id;
1342 int32_t m_nextServerId;
1343 pthread_mutex_t m_serverWaitMutex;
1344 pthread_cond_t m_serverWaitCond;
1345 bool m_serverStartRequested;
1346 sp<IBinder> m_serverStarted;
1347 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001348 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001349};
1350
Martijn Coenen45b07b42017-08-09 12:07:45 +02001351int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001352{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001353 binderLibTestServiceName += String16(binderserversuffix);
1354
Riley Andrews06b01ad2014-12-18 12:10:08 -08001355 status_t ret;
1356 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001357 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358 {
1359 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001360 /*
1361 * We need this below, but can't hold a sp<> because it prevents the
1362 * node from being cleaned up automatically. It's safe in this case
1363 * because of how the tests are written.
1364 */
1365 testServicePtr = testService.get();
1366
Riley Andrews06b01ad2014-12-18 12:10:08 -08001367 if (index == 0) {
1368 ret = sm->addService(binderLibTestServiceName, testService);
1369 } else {
1370 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1371 Parcel data, reply;
1372 data.writeInt32(index);
1373 data.writeStrongBinder(testService);
1374
1375 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1376 }
1377 }
1378 write(readypipefd, &ret, sizeof(ret));
1379 close(readypipefd);
1380 //printf("%s: ret %d\n", __func__, ret);
1381 if (ret)
1382 return 1;
1383 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001384 if (usePoll) {
1385 int fd;
1386 struct epoll_event ev;
1387 int epoll_fd;
1388 IPCThreadState::self()->setupPolling(&fd);
1389 if (fd < 0) {
1390 return 1;
1391 }
1392 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1393
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001394 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001395 if (epoll_fd == -1) {
1396 return 1;
1397 }
1398
1399 ev.events = EPOLLIN;
1400 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1401 return 1;
1402 }
1403
1404 while (1) {
1405 /*
1406 * We simulate a single-threaded process using the binder poll
1407 * interface; besides handling binder commands, it can also
1408 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001409 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001410 *
1411 * processPendingCall() will then issue that transaction.
1412 */
1413 struct epoll_event events[1];
1414 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1415 if (numEvents < 0) {
1416 if (errno == EINTR) {
1417 continue;
1418 }
1419 return 1;
1420 }
1421 if (numEvents > 0) {
1422 IPCThreadState::self()->handlePolledCommands();
1423 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1424 testServicePtr->processPendingCall();
1425 }
1426 }
1427 } else {
1428 ProcessState::self()->startThreadPool();
1429 IPCThreadState::self()->joinThreadPool();
1430 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001431 //printf("%s: joinThreadPool returned\n", __func__);
1432 return 1; /* joinThreadPool should not return */
1433}
1434
1435int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001436 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001437 binderservername = argv[2];
1438 } else {
1439 binderservername = argv[0];
1440 }
1441
Martijn Coenen45b07b42017-08-09 12:07:45 +02001442 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1443 binderserversuffix = argv[5];
1444 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001445 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001446 binderserversuffix = new char[16];
1447 snprintf(binderserversuffix, 16, "%d", getpid());
1448 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001449
1450 ::testing::InitGoogleTest(&argc, argv);
1451 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1452 ProcessState::self()->startThreadPool();
1453 return RUN_ALL_TESTS();
1454}