blob: 8cb06e17e9b0d4b21a0323d7efdb246c3e9ebc24 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <poll.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <gtest/gtest.h>
25
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30
Steven Morelandd63ed9d2019-09-04 18:15:25 -070031#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020032#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080033#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020034
Riley Andrews06b01ad2014-12-18 12:10:08 -080035#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
36
37using namespace android;
38
Sherry Yang336cdd32017-07-24 14:12:27 -070039static ::testing::AssertionResult IsPageAligned(void *buf) {
40 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
41 return ::testing::AssertionSuccess();
42 else
43 return ::testing::AssertionFailure() << buf << " is not page aligned";
44}
45
Riley Andrews06b01ad2014-12-18 12:10:08 -080046static testing::Environment* binder_env;
47static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070048static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080049static char binderserverarg[] = "--binderserver";
50
51static String16 binderLibTestServiceName = String16("test.binderLib");
52
53enum BinderLibTestTranscationCode {
54 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
55 BINDER_LIB_TEST_REGISTER_SERVER,
56 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020057 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080058 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070059 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020060 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070062 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080063 BINDER_LIB_TEST_GET_ID_TRANSACTION,
64 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
65 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
66 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
67 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
68 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
69 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090070 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080071 BINDER_LIB_TEST_EXIT_TRANSACTION,
72 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
73 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070074 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010075 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080076 BINDER_LIB_TEST_ECHO_VECTOR,
Riley Andrews06b01ad2014-12-18 12:10:08 -080077};
78
Martijn Coenen45b07b42017-08-09 12:07:45 +020079pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080080{
81 int ret;
82 pid_t pid;
83 status_t status;
84 int pipefd[2];
85 char stri[16];
86 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020087 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080088 char *childargv[] = {
89 binderservername,
90 binderserverarg,
91 stri,
92 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +020093 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -070094 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -070095 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -080096 };
97
98 ret = pipe(pipefd);
99 if (ret < 0)
100 return ret;
101
102 snprintf(stri, sizeof(stri), "%d", arg2);
103 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200104 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800105
106 pid = fork();
107 if (pid == -1)
108 return pid;
109 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800110 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800111 close(pipefd[0]);
112 execv(binderservername, childargv);
113 status = -errno;
114 write(pipefd[1], &status, sizeof(status));
115 fprintf(stderr, "execv failed, %s\n", strerror(errno));
116 _exit(EXIT_FAILURE);
117 }
118 close(pipefd[1]);
119 ret = read(pipefd[0], &status, sizeof(status));
120 //printf("pipe read returned %d, status %d\n", ret, status);
121 close(pipefd[0]);
122 if (ret == sizeof(status)) {
123 ret = status;
124 } else {
125 kill(pid, SIGKILL);
126 if (ret >= 0) {
127 ret = NO_INIT;
128 }
129 }
130 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700131 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800132 return ret;
133 }
134 return pid;
135}
136
137class BinderLibTestEnv : public ::testing::Environment {
138 public:
139 BinderLibTestEnv() {}
140 sp<IBinder> getServer(void) {
141 return m_server;
142 }
143
144 private:
145 virtual void SetUp() {
146 m_serverpid = start_server_process(0);
147 //printf("m_serverpid %d\n", m_serverpid);
148 ASSERT_GT(m_serverpid, 0);
149
150 sp<IServiceManager> sm = defaultServiceManager();
151 //printf("%s: pid %d, get service\n", __func__, m_pid);
152 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700153 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800154 //printf("%s: pid %d, get service done\n", __func__, m_pid);
155 }
156 virtual void TearDown() {
157 status_t ret;
158 Parcel data, reply;
159 int exitStatus;
160 pid_t pid;
161
162 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700163 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800164 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
165 EXPECT_EQ(0, ret);
166 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
167 EXPECT_EQ(0, ret);
168 }
169 if (m_serverpid > 0) {
170 //printf("wait for %d\n", m_pids[i]);
171 pid = wait(&exitStatus);
172 EXPECT_EQ(m_serverpid, pid);
173 EXPECT_TRUE(WIFEXITED(exitStatus));
174 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
175 }
176 }
177
178 pid_t m_serverpid;
179 sp<IBinder> m_server;
180};
181
182class BinderLibTest : public ::testing::Test {
183 public:
184 virtual void SetUp() {
185 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000186 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800187 }
188 virtual void TearDown() {
189 }
190 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200191 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800192 {
193 int ret;
194 int32_t id;
195 Parcel data, reply;
196 sp<IBinder> binder;
197
Martijn Coenen45b07b42017-08-09 12:07:45 +0200198 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800199 EXPECT_EQ(NO_ERROR, ret);
200
Yi Kong91635562018-06-07 14:38:36 -0700201 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700203 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800204 ret = reply.readInt32(&id);
205 EXPECT_EQ(NO_ERROR, ret);
206 if (idPtr)
207 *idPtr = id;
208 return binder;
209 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200210
Yi Kong91635562018-06-07 14:38:36 -0700211 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200212 {
213 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
214 }
215
Yi Kong91635562018-06-07 14:38:36 -0700216 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200217 {
218 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
219 }
220
Riley Andrews06b01ad2014-12-18 12:10:08 -0800221 void waitForReadData(int fd, int timeout_ms) {
222 int ret;
223 pollfd pfd = pollfd();
224
225 pfd.fd = fd;
226 pfd.events = POLLIN;
227 ret = poll(&pfd, 1, timeout_ms);
228 EXPECT_EQ(1, ret);
229 }
230
231 sp<IBinder> m_server;
232};
233
234class BinderLibTestBundle : public Parcel
235{
236 public:
237 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800238 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800239 int32_t mark;
240 int32_t bundleLen;
241 size_t pos;
242
243 if (source->readInt32(&mark))
244 return;
245 if (mark != MARK_START)
246 return;
247 if (source->readInt32(&bundleLen))
248 return;
249 pos = source->dataPosition();
250 if (Parcel::appendFrom(source, pos, bundleLen))
251 return;
252 source->setDataPosition(pos + bundleLen);
253 if (source->readInt32(&mark))
254 return;
255 if (mark != MARK_END)
256 return;
257 m_isValid = true;
258 setDataPosition(0);
259 }
260 void appendTo(Parcel *dest) {
261 dest->writeInt32(MARK_START);
262 dest->writeInt32(dataSize());
263 dest->appendFrom(this, 0, dataSize());
264 dest->writeInt32(MARK_END);
265 };
266 bool isValid(void) {
267 return m_isValid;
268 }
269 private:
270 enum {
271 MARK_START = B_PACK_CHARS('B','T','B','S'),
272 MARK_END = B_PACK_CHARS('B','T','B','E'),
273 };
274 bool m_isValid;
275};
276
277class BinderLibTestEvent
278{
279 public:
280 BinderLibTestEvent(void)
281 : m_eventTriggered(false)
282 {
Yi Kong91635562018-06-07 14:38:36 -0700283 pthread_mutex_init(&m_waitMutex, nullptr);
284 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800285 }
286 int waitEvent(int timeout_s)
287 {
288 int ret;
289 pthread_mutex_lock(&m_waitMutex);
290 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800291 struct timespec ts;
292 clock_gettime(CLOCK_REALTIME, &ts);
293 ts.tv_sec += timeout_s;
294 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800295 }
296 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
297 pthread_mutex_unlock(&m_waitMutex);
298 return ret;
299 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200300 pthread_t getTriggeringThread()
301 {
302 return m_triggeringThread;
303 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800304 protected:
305 void triggerEvent(void) {
306 pthread_mutex_lock(&m_waitMutex);
307 pthread_cond_signal(&m_waitCond);
308 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200309 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800310 pthread_mutex_unlock(&m_waitMutex);
311 };
312 private:
313 pthread_mutex_t m_waitMutex;
314 pthread_cond_t m_waitCond;
315 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200316 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800317};
318
319class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
320{
321 public:
322 BinderLibTestCallBack()
323 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700324 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800325 {
326 }
327 status_t getResult(void)
328 {
329 return m_result;
330 }
331
332 private:
333 virtual status_t onTransact(uint32_t code,
334 const Parcel& data, Parcel* reply,
335 uint32_t flags = 0)
336 {
337 (void)reply;
338 (void)flags;
339 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200340 case BINDER_LIB_TEST_CALL_BACK: {
341 status_t status = data.readInt32(&m_result);
342 if (status != NO_ERROR) {
343 m_result = status;
344 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800345 triggerEvent();
346 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200347 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700348 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
349 sp<IBinder> server;
350 int ret;
351 const uint8_t *buf = data.data();
352 size_t size = data.dataSize();
353 if (m_prev_end) {
354 /* 64-bit kernel needs at most 8 bytes to align buffer end */
355 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
356 } else {
357 EXPECT_TRUE(IsPageAligned((void *)buf));
358 }
359
360 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
361
362 if (size > 0) {
363 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
364 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
365 data, reply);
366 EXPECT_EQ(NO_ERROR, ret);
367 }
368 return NO_ERROR;
369 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800370 default:
371 return UNKNOWN_TRANSACTION;
372 }
373 }
374
375 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700376 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800377};
378
379class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
380{
381 private:
382 virtual void binderDied(const wp<IBinder>& who) {
383 (void)who;
384 triggerEvent();
385 };
386};
387
388TEST_F(BinderLibTest, NopTransaction) {
389 status_t ret;
390 Parcel data, reply;
391 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
392 EXPECT_EQ(NO_ERROR, ret);
393}
394
395TEST_F(BinderLibTest, SetError) {
396 int32_t testValue[] = { 0, -123, 123 };
397 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
398 status_t ret;
399 Parcel data, reply;
400 data.writeInt32(testValue[i]);
401 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
402 EXPECT_EQ(testValue[i], ret);
403 }
404}
405
406TEST_F(BinderLibTest, GetId) {
407 status_t ret;
408 int32_t id;
409 Parcel data, reply;
410 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
411 EXPECT_EQ(NO_ERROR, ret);
412 ret = reply.readInt32(&id);
413 EXPECT_EQ(NO_ERROR, ret);
414 EXPECT_EQ(0, id);
415}
416
417TEST_F(BinderLibTest, PtrSize) {
418 status_t ret;
419 int32_t ptrsize;
420 Parcel data, reply;
421 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700422 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800423 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
424 EXPECT_EQ(NO_ERROR, ret);
425 ret = reply.readInt32(&ptrsize);
426 EXPECT_EQ(NO_ERROR, ret);
427 RecordProperty("TestPtrSize", sizeof(void *));
428 RecordProperty("ServerPtrSize", sizeof(void *));
429}
430
431TEST_F(BinderLibTest, IndirectGetId2)
432{
433 status_t ret;
434 int32_t id;
435 int32_t count;
436 Parcel data, reply;
437 int32_t serverId[3];
438
439 data.writeInt32(ARRAY_SIZE(serverId));
440 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
441 sp<IBinder> server;
442 BinderLibTestBundle datai;
443
444 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700445 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800446 data.writeStrongBinder(server);
447 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
448 datai.appendTo(&data);
449 }
450
451 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
452 ASSERT_EQ(NO_ERROR, ret);
453
454 ret = reply.readInt32(&id);
455 ASSERT_EQ(NO_ERROR, ret);
456 EXPECT_EQ(0, id);
457
458 ret = reply.readInt32(&count);
459 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700460 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800461
462 for (size_t i = 0; i < (size_t)count; i++) {
463 BinderLibTestBundle replyi(&reply);
464 EXPECT_TRUE(replyi.isValid());
465 ret = replyi.readInt32(&id);
466 EXPECT_EQ(NO_ERROR, ret);
467 EXPECT_EQ(serverId[i], id);
468 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
469 }
470
471 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
472}
473
474TEST_F(BinderLibTest, IndirectGetId3)
475{
476 status_t ret;
477 int32_t id;
478 int32_t count;
479 Parcel data, reply;
480 int32_t serverId[3];
481
482 data.writeInt32(ARRAY_SIZE(serverId));
483 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
484 sp<IBinder> server;
485 BinderLibTestBundle datai;
486 BinderLibTestBundle datai2;
487
488 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700489 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800490 data.writeStrongBinder(server);
491 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
492
493 datai.writeInt32(1);
494 datai.writeStrongBinder(m_server);
495 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
496 datai2.appendTo(&datai);
497
498 datai.appendTo(&data);
499 }
500
501 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
502 ASSERT_EQ(NO_ERROR, ret);
503
504 ret = reply.readInt32(&id);
505 ASSERT_EQ(NO_ERROR, ret);
506 EXPECT_EQ(0, id);
507
508 ret = reply.readInt32(&count);
509 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700510 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800511
512 for (size_t i = 0; i < (size_t)count; i++) {
513 int32_t counti;
514
515 BinderLibTestBundle replyi(&reply);
516 EXPECT_TRUE(replyi.isValid());
517 ret = replyi.readInt32(&id);
518 EXPECT_EQ(NO_ERROR, ret);
519 EXPECT_EQ(serverId[i], id);
520
521 ret = replyi.readInt32(&counti);
522 ASSERT_EQ(NO_ERROR, ret);
523 EXPECT_EQ(1, counti);
524
525 BinderLibTestBundle replyi2(&replyi);
526 EXPECT_TRUE(replyi2.isValid());
527 ret = replyi2.readInt32(&id);
528 EXPECT_EQ(NO_ERROR, ret);
529 EXPECT_EQ(0, id);
530 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
531
532 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
533 }
534
535 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
536}
537
538TEST_F(BinderLibTest, CallBack)
539{
540 status_t ret;
541 Parcel data, reply;
542 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
543 data.writeStrongBinder(callBack);
544 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
545 EXPECT_EQ(NO_ERROR, ret);
546 ret = callBack->waitEvent(5);
547 EXPECT_EQ(NO_ERROR, ret);
548 ret = callBack->getResult();
549 EXPECT_EQ(NO_ERROR, ret);
550}
551
552TEST_F(BinderLibTest, AddServer)
553{
554 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700555 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800556}
557
Riley Andrews06b01ad2014-12-18 12:10:08 -0800558TEST_F(BinderLibTest, DeathNotificationStrongRef)
559{
560 status_t ret;
561 sp<IBinder> sbinder;
562
563 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
564
565 {
566 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700567 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800568 ret = binder->linkToDeath(testDeathRecipient);
569 EXPECT_EQ(NO_ERROR, ret);
570 sbinder = binder;
571 }
572 {
573 Parcel data, reply;
574 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
575 EXPECT_EQ(0, ret);
576 }
577 IPCThreadState::self()->flushCommands();
578 ret = testDeathRecipient->waitEvent(5);
579 EXPECT_EQ(NO_ERROR, ret);
580 ret = sbinder->unlinkToDeath(testDeathRecipient);
581 EXPECT_EQ(DEAD_OBJECT, ret);
582}
583
584TEST_F(BinderLibTest, DeathNotificationMultiple)
585{
586 status_t ret;
587 const int clientcount = 2;
588 sp<IBinder> target;
589 sp<IBinder> linkedclient[clientcount];
590 sp<BinderLibTestCallBack> callBack[clientcount];
591 sp<IBinder> passiveclient[clientcount];
592
593 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700594 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800595 for (int i = 0; i < clientcount; i++) {
596 {
597 Parcel data, reply;
598
599 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700600 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800601 callBack[i] = new BinderLibTestCallBack();
602 data.writeStrongBinder(target);
603 data.writeStrongBinder(callBack[i]);
604 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
605 EXPECT_EQ(NO_ERROR, ret);
606 }
607 {
608 Parcel data, reply;
609
610 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700611 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800612 data.writeStrongBinder(target);
613 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
614 EXPECT_EQ(NO_ERROR, ret);
615 }
616 }
617 {
618 Parcel data, reply;
619 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
620 EXPECT_EQ(0, ret);
621 }
622
623 for (int i = 0; i < clientcount; i++) {
624 ret = callBack[i]->waitEvent(5);
625 EXPECT_EQ(NO_ERROR, ret);
626 ret = callBack[i]->getResult();
627 EXPECT_EQ(NO_ERROR, ret);
628 }
629}
630
Martijn Coenenf7100e42017-07-31 12:14:09 +0200631TEST_F(BinderLibTest, DeathNotificationThread)
632{
633 status_t ret;
634 sp<BinderLibTestCallBack> callback;
635 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700636 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200637 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700638 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200639
640 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
641
642 ret = target->linkToDeath(testDeathRecipient);
643 EXPECT_EQ(NO_ERROR, ret);
644
645 {
646 Parcel data, reply;
647 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
648 EXPECT_EQ(0, ret);
649 }
650
651 /* Make sure it's dead */
652 testDeathRecipient->waitEvent(5);
653
654 /* Now, pass the ref to another process and ask that process to
655 * call linkToDeath() on it, and wait for a response. This tests
656 * two things:
657 * 1) You still get death notifications when calling linkToDeath()
658 * on a ref that is already dead when it was passed to you.
659 * 2) That death notifications are not directly pushed to the thread
660 * registering them, but to the threadpool (proc workqueue) instead.
661 *
662 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
663 * is blocked on a condition variable waiting for the death notification to be
664 * called; therefore, that thread is not available for handling proc work.
665 * So, if the death notification was pushed to the thread workqueue, the callback
666 * would never be called, and the test would timeout and fail.
667 *
668 * Note that we can't do this part of the test from this thread itself, because
669 * the binder driver would only push death notifications to the thread if
670 * it is a looper thread, which this thread is not.
671 *
672 * See b/23525545 for details.
673 */
674 {
675 Parcel data, reply;
676
677 callback = new BinderLibTestCallBack();
678 data.writeStrongBinder(target);
679 data.writeStrongBinder(callback);
680 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
681 EXPECT_EQ(NO_ERROR, ret);
682 }
683
684 ret = callback->waitEvent(5);
685 EXPECT_EQ(NO_ERROR, ret);
686 ret = callback->getResult();
687 EXPECT_EQ(NO_ERROR, ret);
688}
689
Riley Andrews06b01ad2014-12-18 12:10:08 -0800690TEST_F(BinderLibTest, PassFile) {
691 int ret;
692 int pipefd[2];
693 uint8_t buf[1] = { 0 };
694 uint8_t write_value = 123;
695
696 ret = pipe2(pipefd, O_NONBLOCK);
697 ASSERT_EQ(0, ret);
698
699 {
700 Parcel data, reply;
701 uint8_t writebuf[1] = { write_value };
702
703 ret = data.writeFileDescriptor(pipefd[1], true);
704 EXPECT_EQ(NO_ERROR, ret);
705
706 ret = data.writeInt32(sizeof(writebuf));
707 EXPECT_EQ(NO_ERROR, ret);
708
709 ret = data.write(writebuf, sizeof(writebuf));
710 EXPECT_EQ(NO_ERROR, ret);
711
712 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
713 EXPECT_EQ(NO_ERROR, ret);
714 }
715
716 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700717 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800718 EXPECT_EQ(write_value, buf[0]);
719
720 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
721
722 ret = read(pipefd[0], buf, sizeof(buf));
723 EXPECT_EQ(0, ret);
724
725 close(pipefd[0]);
726}
727
Ryo Hashimotobf551892018-05-31 16:58:35 +0900728TEST_F(BinderLibTest, PassParcelFileDescriptor) {
729 const int datasize = 123;
730 std::vector<uint8_t> writebuf(datasize);
731 for (size_t i = 0; i < writebuf.size(); ++i) {
732 writebuf[i] = i;
733 }
734
735 android::base::unique_fd read_end, write_end;
736 {
737 int pipefd[2];
738 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
739 read_end.reset(pipefd[0]);
740 write_end.reset(pipefd[1]);
741 }
742 {
743 Parcel data;
744 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
745 write_end.reset();
746 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
747 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
748
749 Parcel reply;
750 EXPECT_EQ(NO_ERROR,
751 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
752 &reply));
753 }
754 std::vector<uint8_t> readbuf(datasize);
755 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
756 EXPECT_EQ(writebuf, readbuf);
757
758 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
759
760 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
761}
762
Riley Andrews06b01ad2014-12-18 12:10:08 -0800763TEST_F(BinderLibTest, PromoteLocal) {
764 sp<IBinder> strong = new BBinder();
765 wp<IBinder> weak = strong;
766 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700767 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800768 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700769 strong = nullptr;
770 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800771 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700772 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800773}
774
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700775TEST_F(BinderLibTest, LocalGetExtension) {
776 sp<BBinder> binder = new BBinder();
777 sp<IBinder> ext = new BBinder();
778 binder->setExtension(ext);
779 EXPECT_EQ(ext, binder->getExtension());
780}
781
782TEST_F(BinderLibTest, RemoteGetExtension) {
783 sp<IBinder> server = addServer();
784 ASSERT_TRUE(server != nullptr);
785
786 sp<IBinder> extension;
787 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
788 ASSERT_NE(nullptr, extension.get());
789
790 EXPECT_EQ(NO_ERROR, extension->pingBinder());
791}
792
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700793TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
794 status_t ret;
795 Parcel data, reply;
796
797 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
798 EXPECT_EQ(NO_ERROR, ret);
799
800 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700801 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800802 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
803 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
804 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
805 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700806}
807
Connor O'Brien52be2c92016-09-20 14:18:08 -0700808TEST_F(BinderLibTest, FreedBinder) {
809 status_t ret;
810
811 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700812 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700813
814 __u32 freedHandle;
815 wp<IBinder> keepFreedBinder;
816 {
817 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700818 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
819 ASSERT_EQ(NO_ERROR, ret);
820 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
821 freedHandle = freed->handle;
822 /* Add a weak ref to the freed binder so the driver does not
823 * delete its reference to it - otherwise the transaction
824 * fails regardless of whether the driver is fixed.
825 */
Steven Morelande171d622019-07-17 16:06:01 -0700826 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700827 }
Steven Morelande171d622019-07-17 16:06:01 -0700828 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700829 {
830 Parcel data, reply;
831 data.writeStrongBinder(server);
832 /* Replace original handle with handle to the freed binder */
833 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
834 __u32 oldHandle = strong->handle;
835 strong->handle = freedHandle;
836 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
837 /* Returns DEAD_OBJECT (-32) if target crashes and
838 * FAILED_TRANSACTION if the driver rejects the invalid
839 * object.
840 */
841 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
842 /* Restore original handle so parcel destructor does not use
843 * the wrong handle.
844 */
845 strong->handle = oldHandle;
846 }
847}
848
Sherry Yang336cdd32017-07-24 14:12:27 -0700849TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
850 status_t ret;
851 Parcel data, reply;
852 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
853 for (int i = 0; i < 2; i++) {
854 BinderLibTestBundle datai;
855 datai.appendFrom(&data, 0, data.dataSize());
856
857 data.freeData();
858 data.writeInt32(1);
859 data.writeStrongBinder(callBack);
860 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
861
862 datai.appendTo(&data);
863 }
864 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
865 EXPECT_EQ(NO_ERROR, ret);
866}
867
Martijn Coenen45b07b42017-08-09 12:07:45 +0200868TEST_F(BinderLibTest, OnewayQueueing)
869{
870 status_t ret;
871 Parcel data, data2;
872
873 sp<IBinder> pollServer = addPollServer();
874
875 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
876 data.writeStrongBinder(callBack);
877 data.writeInt32(500000); // delay in us before calling back
878
879 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
880 data2.writeStrongBinder(callBack2);
881 data2.writeInt32(0); // delay in us
882
Yi Kong91635562018-06-07 14:38:36 -0700883 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200884 EXPECT_EQ(NO_ERROR, ret);
885
886 // The delay ensures that this second transaction will end up on the async_todo list
887 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700888 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200889 EXPECT_EQ(NO_ERROR, ret);
890
891 // The server will ensure that the two transactions are handled in the expected order;
892 // If the ordering is not as expected, an error will be returned through the callbacks.
893 ret = callBack->waitEvent(2);
894 EXPECT_EQ(NO_ERROR, ret);
895 ret = callBack->getResult();
896 EXPECT_EQ(NO_ERROR, ret);
897
898 ret = callBack2->waitEvent(2);
899 EXPECT_EQ(NO_ERROR, ret);
900 ret = callBack2->getResult();
901 EXPECT_EQ(NO_ERROR, ret);
902}
903
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100904TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
905{
906 status_t ret;
907 Parcel data, reply;
908 data.writeInterfaceToken(binderLibTestServiceName);
909 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
910 EXPECT_EQ(-1, reply.readInt32());
911 EXPECT_EQ(NO_ERROR, ret);
912}
913
914TEST_F(BinderLibTest, WorkSourceSet)
915{
916 status_t ret;
917 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000918 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000919 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100920 data.writeInterfaceToken(binderLibTestServiceName);
921 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
922 EXPECT_EQ(100, reply.readInt32());
923 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000924 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
925 EXPECT_EQ(NO_ERROR, ret);
926}
927
928TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
929{
930 status_t ret;
931 Parcel data, reply;
932
933 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
934 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
935
936 data.writeInterfaceToken(binderLibTestServiceName);
937 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
938 EXPECT_EQ(-1, reply.readInt32());
939 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100940 EXPECT_EQ(NO_ERROR, ret);
941}
942
943TEST_F(BinderLibTest, WorkSourceCleared)
944{
945 status_t ret;
946 Parcel data, reply;
947
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000948 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000949 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
950 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100951 data.writeInterfaceToken(binderLibTestServiceName);
952 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
953
954 EXPECT_EQ(-1, reply.readInt32());
955 EXPECT_EQ(100, previousWorkSource);
956 EXPECT_EQ(NO_ERROR, ret);
957}
958
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000959TEST_F(BinderLibTest, WorkSourceRestored)
960{
961 status_t ret;
962 Parcel data, reply;
963
964 IPCThreadState::self()->setCallingWorkSourceUid(100);
965 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
966 IPCThreadState::self()->restoreCallingWorkSource(token);
967
968 data.writeInterfaceToken(binderLibTestServiceName);
969 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
970
971 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000972 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000973 EXPECT_EQ(NO_ERROR, ret);
974}
975
Olivier Gaillard91a04802018-11-14 17:32:41 +0000976TEST_F(BinderLibTest, PropagateFlagSet)
977{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000978 IPCThreadState::self()->clearPropagateWorkSource();
979 IPCThreadState::self()->setCallingWorkSourceUid(100);
980 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
981}
982
983TEST_F(BinderLibTest, PropagateFlagCleared)
984{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000985 IPCThreadState::self()->setCallingWorkSourceUid(100);
986 IPCThreadState::self()->clearPropagateWorkSource();
987 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
988}
989
990TEST_F(BinderLibTest, PropagateFlagRestored)
991{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000992 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
993 IPCThreadState::self()->restoreCallingWorkSource(token);
994
995 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
996}
997
998TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
999{
1000 IPCThreadState::self()->setCallingWorkSourceUid(100);
1001
1002 Parcel data, reply;
1003 status_t ret;
1004 data.writeInterfaceToken(binderLibTestServiceName);
1005 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1006
1007 Parcel data2, reply2;
1008 status_t ret2;
1009 data2.writeInterfaceToken(binderLibTestServiceName);
1010 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1011 EXPECT_EQ(100, reply2.readInt32());
1012 EXPECT_EQ(NO_ERROR, ret2);
1013}
1014
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001015TEST_F(BinderLibTest, VectorSent) {
1016 Parcel data, reply;
1017 sp<IBinder> server = addServer();
1018 ASSERT_TRUE(server != nullptr);
1019
1020 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1021 data.writeUint64Vector(testValue);
1022
1023 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1024 EXPECT_EQ(NO_ERROR, ret);
1025 std::vector<uint64_t> readValue;
1026 ret = reply.readUint64Vector(&readValue);
1027 EXPECT_EQ(readValue, testValue);
1028}
1029
Riley Andrews06b01ad2014-12-18 12:10:08 -08001030class BinderLibTestService : public BBinder
1031{
1032 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001033 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001034 : m_id(id)
1035 , m_nextServerId(id + 1)
1036 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001037 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001038 {
Yi Kong91635562018-06-07 14:38:36 -07001039 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1040 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001041 }
1042 ~BinderLibTestService()
1043 {
1044 exit(EXIT_SUCCESS);
1045 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001046
1047 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001048 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001049 Parcel data;
1050 data.writeInt32(NO_ERROR);
1051 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001052 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001053 }
1054 }
1055
Riley Andrews06b01ad2014-12-18 12:10:08 -08001056 virtual status_t onTransact(uint32_t code,
1057 const Parcel& data, Parcel* reply,
1058 uint32_t flags = 0) {
1059 //printf("%s: code %d\n", __func__, code);
1060 (void)flags;
1061
1062 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1063 return PERMISSION_DENIED;
1064 }
1065 switch (code) {
1066 case BINDER_LIB_TEST_REGISTER_SERVER: {
1067 int32_t id;
1068 sp<IBinder> binder;
1069 id = data.readInt32();
1070 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001071 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001072 return BAD_VALUE;
1073 }
1074
1075 if (m_id != 0)
1076 return INVALID_OPERATION;
1077
1078 pthread_mutex_lock(&m_serverWaitMutex);
1079 if (m_serverStartRequested) {
1080 m_serverStartRequested = false;
1081 m_serverStarted = binder;
1082 pthread_cond_signal(&m_serverWaitCond);
1083 }
1084 pthread_mutex_unlock(&m_serverWaitMutex);
1085 return NO_ERROR;
1086 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001087 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001088 case BINDER_LIB_TEST_ADD_SERVER: {
1089 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001090 int serverid;
1091
1092 if (m_id != 0) {
1093 return INVALID_OPERATION;
1094 }
1095 pthread_mutex_lock(&m_serverWaitMutex);
1096 if (m_serverStartRequested) {
1097 ret = -EBUSY;
1098 } else {
1099 serverid = m_nextServerId++;
1100 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001101 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001102
1103 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001104 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001105 pthread_mutex_lock(&m_serverWaitMutex);
1106 }
1107 if (ret > 0) {
1108 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001109 struct timespec ts;
1110 clock_gettime(CLOCK_REALTIME, &ts);
1111 ts.tv_sec += 5;
1112 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001113 }
1114 if (m_serverStartRequested) {
1115 m_serverStartRequested = false;
1116 ret = -ETIMEDOUT;
1117 } else {
1118 reply->writeStrongBinder(m_serverStarted);
1119 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001120 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001121 ret = NO_ERROR;
1122 }
1123 } else if (ret >= 0) {
1124 m_serverStartRequested = false;
1125 ret = UNKNOWN_ERROR;
1126 }
1127 pthread_mutex_unlock(&m_serverWaitMutex);
1128 return ret;
1129 }
1130 case BINDER_LIB_TEST_NOP_TRANSACTION:
1131 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001132 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1133 // Note: this transaction is only designed for use with a
1134 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001135 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001136 // A callback was already pending; this means that
1137 // we received a second call while still processing
1138 // the first one. Fail the test.
1139 sp<IBinder> callback = data.readStrongBinder();
1140 Parcel data2;
1141 data2.writeInt32(UNKNOWN_ERROR);
1142
Yi Kong91635562018-06-07 14:38:36 -07001143 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001144 } else {
1145 m_callback = data.readStrongBinder();
1146 int32_t delayUs = data.readInt32();
1147 /*
1148 * It's necessary that we sleep here, so the next
1149 * transaction the caller makes will be queued to
1150 * the async queue.
1151 */
1152 usleep(delayUs);
1153
1154 /*
1155 * Now when we return, libbinder will tell the kernel
1156 * we are done with this transaction, and the kernel
1157 * can move the queued transaction to either the
1158 * thread todo worklist (for kernels without the fix),
1159 * or the proc todo worklist. In case of the former,
1160 * the next outbound call will pick up the pending
1161 * transaction, which leads to undesired reentrant
1162 * behavior. This is caught in the if() branch above.
1163 */
1164 }
1165
1166 return NO_ERROR;
1167 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001168 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1169 Parcel data2, reply2;
1170 sp<IBinder> binder;
1171 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001172 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001173 return BAD_VALUE;
1174 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001175 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001176 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1177 return NO_ERROR;
1178 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001179 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1180 reply->writeStrongBinder(this);
1181 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001182 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1183 reply->writeInt32(m_id);
1184 return NO_ERROR;
1185 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1186 int32_t count;
1187 uint32_t indirect_code;
1188 sp<IBinder> binder;
1189
1190 count = data.readInt32();
1191 reply->writeInt32(m_id);
1192 reply->writeInt32(count);
1193 for (int i = 0; i < count; i++) {
1194 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001195 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001196 return BAD_VALUE;
1197 }
1198 indirect_code = data.readInt32();
1199 BinderLibTestBundle data2(&data);
1200 if (!data2.isValid()) {
1201 return BAD_VALUE;
1202 }
1203 BinderLibTestBundle reply2;
1204 binder->transact(indirect_code, data2, &reply2);
1205 reply2.appendTo(reply);
1206 }
1207 return NO_ERROR;
1208 }
1209 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1210 reply->setError(data.readInt32());
1211 return NO_ERROR;
1212 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1213 reply->writeInt32(sizeof(void *));
1214 return NO_ERROR;
1215 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1216 return NO_ERROR;
1217 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1218 m_strongRef = data.readStrongBinder();
1219 return NO_ERROR;
1220 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1221 int ret;
1222 Parcel data2, reply2;
1223 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1224 sp<IBinder> target;
1225 sp<IBinder> callback;
1226
1227 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001228 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001229 return BAD_VALUE;
1230 }
1231 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001232 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001233 return BAD_VALUE;
1234 }
1235 ret = target->linkToDeath(testDeathRecipient);
1236 if (ret == NO_ERROR)
1237 ret = testDeathRecipient->waitEvent(5);
1238 data2.writeInt32(ret);
1239 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1240 return NO_ERROR;
1241 }
1242 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1243 int ret;
1244 int32_t size;
1245 const void *buf;
1246 int fd;
1247
1248 fd = data.readFileDescriptor();
1249 if (fd < 0) {
1250 return BAD_VALUE;
1251 }
1252 ret = data.readInt32(&size);
1253 if (ret != NO_ERROR) {
1254 return ret;
1255 }
1256 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001257 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001258 return BAD_VALUE;
1259 }
1260 ret = write(fd, buf, size);
1261 if (ret != size)
1262 return UNKNOWN_ERROR;
1263 return NO_ERROR;
1264 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001265 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1266 int ret;
1267 int32_t size;
1268 const void *buf;
1269 android::base::unique_fd fd;
1270
1271 ret = data.readUniqueParcelFileDescriptor(&fd);
1272 if (ret != NO_ERROR) {
1273 return ret;
1274 }
1275 ret = data.readInt32(&size);
1276 if (ret != NO_ERROR) {
1277 return ret;
1278 }
1279 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001280 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001281 return BAD_VALUE;
1282 }
1283 ret = write(fd.get(), buf, size);
1284 if (ret != size) return UNKNOWN_ERROR;
1285 return NO_ERROR;
1286 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001287 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1288 alarm(10);
1289 return NO_ERROR;
1290 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001291 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001292 ;
1293 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001294 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001295 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001296 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001297 return NO_ERROR;
1298 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001299 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1300 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001301 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001302 return NO_ERROR;
1303 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001304 case BINDER_LIB_TEST_ECHO_VECTOR: {
1305 std::vector<uint64_t> vector;
1306 auto err = data.readUint64Vector(&vector);
1307 if (err != NO_ERROR)
1308 return err;
1309 reply->writeUint64Vector(vector);
1310 return NO_ERROR;
1311 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001312 default:
1313 return UNKNOWN_TRANSACTION;
1314 };
1315 }
1316 private:
1317 int32_t m_id;
1318 int32_t m_nextServerId;
1319 pthread_mutex_t m_serverWaitMutex;
1320 pthread_cond_t m_serverWaitCond;
1321 bool m_serverStartRequested;
1322 sp<IBinder> m_serverStarted;
1323 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001324 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001325};
1326
Martijn Coenen45b07b42017-08-09 12:07:45 +02001327int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001328{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001329 binderLibTestServiceName += String16(binderserversuffix);
1330
Riley Andrews06b01ad2014-12-18 12:10:08 -08001331 status_t ret;
1332 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001333 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001334 {
1335 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001336
1337 /*
1338 * Normally would also contain functionality as well, but we are only
1339 * testing the extension mechanism.
1340 */
1341 testService->setExtension(new BBinder());
1342
Martijn Coenen45b07b42017-08-09 12:07:45 +02001343 /*
1344 * We need this below, but can't hold a sp<> because it prevents the
1345 * node from being cleaned up automatically. It's safe in this case
1346 * because of how the tests are written.
1347 */
1348 testServicePtr = testService.get();
1349
Riley Andrews06b01ad2014-12-18 12:10:08 -08001350 if (index == 0) {
1351 ret = sm->addService(binderLibTestServiceName, testService);
1352 } else {
1353 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1354 Parcel data, reply;
1355 data.writeInt32(index);
1356 data.writeStrongBinder(testService);
1357
1358 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1359 }
1360 }
1361 write(readypipefd, &ret, sizeof(ret));
1362 close(readypipefd);
1363 //printf("%s: ret %d\n", __func__, ret);
1364 if (ret)
1365 return 1;
1366 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001367 if (usePoll) {
1368 int fd;
1369 struct epoll_event ev;
1370 int epoll_fd;
1371 IPCThreadState::self()->setupPolling(&fd);
1372 if (fd < 0) {
1373 return 1;
1374 }
1375 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1376
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001377 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001378 if (epoll_fd == -1) {
1379 return 1;
1380 }
1381
1382 ev.events = EPOLLIN;
1383 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1384 return 1;
1385 }
1386
1387 while (1) {
1388 /*
1389 * We simulate a single-threaded process using the binder poll
1390 * interface; besides handling binder commands, it can also
1391 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001392 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001393 *
1394 * processPendingCall() will then issue that transaction.
1395 */
1396 struct epoll_event events[1];
1397 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1398 if (numEvents < 0) {
1399 if (errno == EINTR) {
1400 continue;
1401 }
1402 return 1;
1403 }
1404 if (numEvents > 0) {
1405 IPCThreadState::self()->handlePolledCommands();
1406 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1407 testServicePtr->processPendingCall();
1408 }
1409 }
1410 } else {
1411 ProcessState::self()->startThreadPool();
1412 IPCThreadState::self()->joinThreadPool();
1413 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001414 //printf("%s: joinThreadPool returned\n", __func__);
1415 return 1; /* joinThreadPool should not return */
1416}
1417
1418int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001419 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001420 binderservername = argv[2];
1421 } else {
1422 binderservername = argv[0];
1423 }
1424
Martijn Coenen45b07b42017-08-09 12:07:45 +02001425 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1426 binderserversuffix = argv[5];
1427 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001428 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001429 binderserversuffix = new char[16];
1430 snprintf(binderserversuffix, 16, "%d", getpid());
1431 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001432
1433 ::testing::InitGoogleTest(&argc, argv);
1434 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1435 ProcessState::self()->startThreadPool();
1436 return RUN_ALL_TESTS();
1437}