blob: 5e0574ad8ab82b966d43891bf13f79a34b4d4e0b [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>
33
Riley Andrews06b01ad2014-12-18 12:10:08 -080034#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
35
36using namespace android;
37
Sherry Yang336cdd32017-07-24 14:12:27 -070038static ::testing::AssertionResult IsPageAligned(void *buf) {
39 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
40 return ::testing::AssertionSuccess();
41 else
42 return ::testing::AssertionFailure() << buf << " is not page aligned";
43}
44
Riley Andrews06b01ad2014-12-18 12:10:08 -080045static testing::Environment* binder_env;
46static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070047static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080048static char binderserverarg[] = "--binderserver";
49
50static String16 binderLibTestServiceName = String16("test.binderLib");
51
52enum BinderLibTestTranscationCode {
53 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
54 BINDER_LIB_TEST_REGISTER_SERVER,
55 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020056 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080057 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070058 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020059 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080060 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070061 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080062 BINDER_LIB_TEST_GET_ID_TRANSACTION,
63 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
64 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
65 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
66 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
67 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
68 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090069 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080070 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
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700773TEST_F(BinderLibTest, LocalGetExtension) {
774 sp<BBinder> binder = new BBinder();
775 sp<IBinder> ext = new BBinder();
776 binder->setExtension(ext);
777 EXPECT_EQ(ext, binder->getExtension());
778}
779
780TEST_F(BinderLibTest, RemoteGetExtension) {
781 sp<IBinder> server = addServer();
782 ASSERT_TRUE(server != nullptr);
783
784 sp<IBinder> extension;
785 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
786 ASSERT_NE(nullptr, extension.get());
787
788 EXPECT_EQ(NO_ERROR, extension->pingBinder());
789}
790
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700791TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
792 status_t ret;
793 Parcel data, reply;
794
795 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
796 EXPECT_EQ(NO_ERROR, ret);
797
798 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700799 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800800 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
801 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
802 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
803 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700804}
805
Connor O'Brien52be2c92016-09-20 14:18:08 -0700806TEST_F(BinderLibTest, FreedBinder) {
807 status_t ret;
808
809 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700810 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700811
812 __u32 freedHandle;
813 wp<IBinder> keepFreedBinder;
814 {
815 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700816 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
817 ASSERT_EQ(NO_ERROR, ret);
818 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
819 freedHandle = freed->handle;
820 /* Add a weak ref to the freed binder so the driver does not
821 * delete its reference to it - otherwise the transaction
822 * fails regardless of whether the driver is fixed.
823 */
Steven Morelande171d622019-07-17 16:06:01 -0700824 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700825 }
Steven Morelande171d622019-07-17 16:06:01 -0700826 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700827 {
828 Parcel data, reply;
829 data.writeStrongBinder(server);
830 /* Replace original handle with handle to the freed binder */
831 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
832 __u32 oldHandle = strong->handle;
833 strong->handle = freedHandle;
834 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
835 /* Returns DEAD_OBJECT (-32) if target crashes and
836 * FAILED_TRANSACTION if the driver rejects the invalid
837 * object.
838 */
839 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
840 /* Restore original handle so parcel destructor does not use
841 * the wrong handle.
842 */
843 strong->handle = oldHandle;
844 }
845}
846
Sherry Yang336cdd32017-07-24 14:12:27 -0700847TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
848 status_t ret;
849 Parcel data, reply;
850 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
851 for (int i = 0; i < 2; i++) {
852 BinderLibTestBundle datai;
853 datai.appendFrom(&data, 0, data.dataSize());
854
855 data.freeData();
856 data.writeInt32(1);
857 data.writeStrongBinder(callBack);
858 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
859
860 datai.appendTo(&data);
861 }
862 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
863 EXPECT_EQ(NO_ERROR, ret);
864}
865
Martijn Coenen45b07b42017-08-09 12:07:45 +0200866TEST_F(BinderLibTest, OnewayQueueing)
867{
868 status_t ret;
869 Parcel data, data2;
870
871 sp<IBinder> pollServer = addPollServer();
872
873 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
874 data.writeStrongBinder(callBack);
875 data.writeInt32(500000); // delay in us before calling back
876
877 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
878 data2.writeStrongBinder(callBack2);
879 data2.writeInt32(0); // delay in us
880
Yi Kong91635562018-06-07 14:38:36 -0700881 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200882 EXPECT_EQ(NO_ERROR, ret);
883
884 // The delay ensures that this second transaction will end up on the async_todo list
885 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700886 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200887 EXPECT_EQ(NO_ERROR, ret);
888
889 // The server will ensure that the two transactions are handled in the expected order;
890 // If the ordering is not as expected, an error will be returned through the callbacks.
891 ret = callBack->waitEvent(2);
892 EXPECT_EQ(NO_ERROR, ret);
893 ret = callBack->getResult();
894 EXPECT_EQ(NO_ERROR, ret);
895
896 ret = callBack2->waitEvent(2);
897 EXPECT_EQ(NO_ERROR, ret);
898 ret = callBack2->getResult();
899 EXPECT_EQ(NO_ERROR, ret);
900}
901
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100902TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
903{
904 status_t ret;
905 Parcel data, reply;
906 data.writeInterfaceToken(binderLibTestServiceName);
907 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
908 EXPECT_EQ(-1, reply.readInt32());
909 EXPECT_EQ(NO_ERROR, ret);
910}
911
912TEST_F(BinderLibTest, WorkSourceSet)
913{
914 status_t ret;
915 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000916 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000917 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100918 data.writeInterfaceToken(binderLibTestServiceName);
919 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
920 EXPECT_EQ(100, reply.readInt32());
921 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000922 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
923 EXPECT_EQ(NO_ERROR, ret);
924}
925
926TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
927{
928 status_t ret;
929 Parcel data, reply;
930
931 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
932 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
933
934 data.writeInterfaceToken(binderLibTestServiceName);
935 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
936 EXPECT_EQ(-1, reply.readInt32());
937 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100938 EXPECT_EQ(NO_ERROR, ret);
939}
940
941TEST_F(BinderLibTest, WorkSourceCleared)
942{
943 status_t ret;
944 Parcel data, reply;
945
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000946 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000947 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
948 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100949 data.writeInterfaceToken(binderLibTestServiceName);
950 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
951
952 EXPECT_EQ(-1, reply.readInt32());
953 EXPECT_EQ(100, previousWorkSource);
954 EXPECT_EQ(NO_ERROR, ret);
955}
956
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000957TEST_F(BinderLibTest, WorkSourceRestored)
958{
959 status_t ret;
960 Parcel data, reply;
961
962 IPCThreadState::self()->setCallingWorkSourceUid(100);
963 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
964 IPCThreadState::self()->restoreCallingWorkSource(token);
965
966 data.writeInterfaceToken(binderLibTestServiceName);
967 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
968
969 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000970 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000971 EXPECT_EQ(NO_ERROR, ret);
972}
973
Olivier Gaillard91a04802018-11-14 17:32:41 +0000974TEST_F(BinderLibTest, PropagateFlagSet)
975{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000976 IPCThreadState::self()->clearPropagateWorkSource();
977 IPCThreadState::self()->setCallingWorkSourceUid(100);
978 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
979}
980
981TEST_F(BinderLibTest, PropagateFlagCleared)
982{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000983 IPCThreadState::self()->setCallingWorkSourceUid(100);
984 IPCThreadState::self()->clearPropagateWorkSource();
985 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
986}
987
988TEST_F(BinderLibTest, PropagateFlagRestored)
989{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000990 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
991 IPCThreadState::self()->restoreCallingWorkSource(token);
992
993 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
994}
995
996TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
997{
998 IPCThreadState::self()->setCallingWorkSourceUid(100);
999
1000 Parcel data, reply;
1001 status_t ret;
1002 data.writeInterfaceToken(binderLibTestServiceName);
1003 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1004
1005 Parcel data2, reply2;
1006 status_t ret2;
1007 data2.writeInterfaceToken(binderLibTestServiceName);
1008 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1009 EXPECT_EQ(100, reply2.readInt32());
1010 EXPECT_EQ(NO_ERROR, ret2);
1011}
1012
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001013TEST_F(BinderLibTest, VectorSent) {
1014 Parcel data, reply;
1015 sp<IBinder> server = addServer();
1016 ASSERT_TRUE(server != nullptr);
1017
1018 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1019 data.writeUint64Vector(testValue);
1020
1021 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1022 EXPECT_EQ(NO_ERROR, ret);
1023 std::vector<uint64_t> readValue;
1024 ret = reply.readUint64Vector(&readValue);
1025 EXPECT_EQ(readValue, testValue);
1026}
1027
Riley Andrews06b01ad2014-12-18 12:10:08 -08001028class BinderLibTestService : public BBinder
1029{
1030 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001031 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001032 : m_id(id)
1033 , m_nextServerId(id + 1)
1034 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001035 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001036 {
Yi Kong91635562018-06-07 14:38:36 -07001037 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1038 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001039 }
1040 ~BinderLibTestService()
1041 {
1042 exit(EXIT_SUCCESS);
1043 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001044
1045 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001046 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001047 Parcel data;
1048 data.writeInt32(NO_ERROR);
1049 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001050 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001051 }
1052 }
1053
Riley Andrews06b01ad2014-12-18 12:10:08 -08001054 virtual status_t onTransact(uint32_t code,
1055 const Parcel& data, Parcel* reply,
1056 uint32_t flags = 0) {
1057 //printf("%s: code %d\n", __func__, code);
1058 (void)flags;
1059
1060 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1061 return PERMISSION_DENIED;
1062 }
1063 switch (code) {
1064 case BINDER_LIB_TEST_REGISTER_SERVER: {
1065 int32_t id;
1066 sp<IBinder> binder;
1067 id = data.readInt32();
1068 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001069 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001070 return BAD_VALUE;
1071 }
1072
1073 if (m_id != 0)
1074 return INVALID_OPERATION;
1075
1076 pthread_mutex_lock(&m_serverWaitMutex);
1077 if (m_serverStartRequested) {
1078 m_serverStartRequested = false;
1079 m_serverStarted = binder;
1080 pthread_cond_signal(&m_serverWaitCond);
1081 }
1082 pthread_mutex_unlock(&m_serverWaitMutex);
1083 return NO_ERROR;
1084 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001085 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001086 case BINDER_LIB_TEST_ADD_SERVER: {
1087 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001088 int serverid;
1089
1090 if (m_id != 0) {
1091 return INVALID_OPERATION;
1092 }
1093 pthread_mutex_lock(&m_serverWaitMutex);
1094 if (m_serverStartRequested) {
1095 ret = -EBUSY;
1096 } else {
1097 serverid = m_nextServerId++;
1098 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001099 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001100
1101 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001102 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001103 pthread_mutex_lock(&m_serverWaitMutex);
1104 }
1105 if (ret > 0) {
1106 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001107 struct timespec ts;
1108 clock_gettime(CLOCK_REALTIME, &ts);
1109 ts.tv_sec += 5;
1110 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001111 }
1112 if (m_serverStartRequested) {
1113 m_serverStartRequested = false;
1114 ret = -ETIMEDOUT;
1115 } else {
1116 reply->writeStrongBinder(m_serverStarted);
1117 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001118 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001119 ret = NO_ERROR;
1120 }
1121 } else if (ret >= 0) {
1122 m_serverStartRequested = false;
1123 ret = UNKNOWN_ERROR;
1124 }
1125 pthread_mutex_unlock(&m_serverWaitMutex);
1126 return ret;
1127 }
1128 case BINDER_LIB_TEST_NOP_TRANSACTION:
1129 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001130 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1131 // Note: this transaction is only designed for use with a
1132 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001133 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001134 // A callback was already pending; this means that
1135 // we received a second call while still processing
1136 // the first one. Fail the test.
1137 sp<IBinder> callback = data.readStrongBinder();
1138 Parcel data2;
1139 data2.writeInt32(UNKNOWN_ERROR);
1140
Yi Kong91635562018-06-07 14:38:36 -07001141 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001142 } else {
1143 m_callback = data.readStrongBinder();
1144 int32_t delayUs = data.readInt32();
1145 /*
1146 * It's necessary that we sleep here, so the next
1147 * transaction the caller makes will be queued to
1148 * the async queue.
1149 */
1150 usleep(delayUs);
1151
1152 /*
1153 * Now when we return, libbinder will tell the kernel
1154 * we are done with this transaction, and the kernel
1155 * can move the queued transaction to either the
1156 * thread todo worklist (for kernels without the fix),
1157 * or the proc todo worklist. In case of the former,
1158 * the next outbound call will pick up the pending
1159 * transaction, which leads to undesired reentrant
1160 * behavior. This is caught in the if() branch above.
1161 */
1162 }
1163
1164 return NO_ERROR;
1165 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001166 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1167 Parcel data2, reply2;
1168 sp<IBinder> binder;
1169 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001170 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001171 return BAD_VALUE;
1172 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001173 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001174 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1175 return NO_ERROR;
1176 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001177 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1178 reply->writeStrongBinder(this);
1179 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001180 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1181 reply->writeInt32(m_id);
1182 return NO_ERROR;
1183 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1184 int32_t count;
1185 uint32_t indirect_code;
1186 sp<IBinder> binder;
1187
1188 count = data.readInt32();
1189 reply->writeInt32(m_id);
1190 reply->writeInt32(count);
1191 for (int i = 0; i < count; i++) {
1192 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001193 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001194 return BAD_VALUE;
1195 }
1196 indirect_code = data.readInt32();
1197 BinderLibTestBundle data2(&data);
1198 if (!data2.isValid()) {
1199 return BAD_VALUE;
1200 }
1201 BinderLibTestBundle reply2;
1202 binder->transact(indirect_code, data2, &reply2);
1203 reply2.appendTo(reply);
1204 }
1205 return NO_ERROR;
1206 }
1207 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1208 reply->setError(data.readInt32());
1209 return NO_ERROR;
1210 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1211 reply->writeInt32(sizeof(void *));
1212 return NO_ERROR;
1213 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1214 return NO_ERROR;
1215 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1216 m_strongRef = data.readStrongBinder();
1217 return NO_ERROR;
1218 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1219 int ret;
1220 Parcel data2, reply2;
1221 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1222 sp<IBinder> target;
1223 sp<IBinder> callback;
1224
1225 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001226 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001227 return BAD_VALUE;
1228 }
1229 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001230 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001231 return BAD_VALUE;
1232 }
1233 ret = target->linkToDeath(testDeathRecipient);
1234 if (ret == NO_ERROR)
1235 ret = testDeathRecipient->waitEvent(5);
1236 data2.writeInt32(ret);
1237 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1238 return NO_ERROR;
1239 }
1240 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1241 int ret;
1242 int32_t size;
1243 const void *buf;
1244 int fd;
1245
1246 fd = data.readFileDescriptor();
1247 if (fd < 0) {
1248 return BAD_VALUE;
1249 }
1250 ret = data.readInt32(&size);
1251 if (ret != NO_ERROR) {
1252 return ret;
1253 }
1254 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001255 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001256 return BAD_VALUE;
1257 }
1258 ret = write(fd, buf, size);
1259 if (ret != size)
1260 return UNKNOWN_ERROR;
1261 return NO_ERROR;
1262 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001263 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1264 int ret;
1265 int32_t size;
1266 const void *buf;
1267 android::base::unique_fd fd;
1268
1269 ret = data.readUniqueParcelFileDescriptor(&fd);
1270 if (ret != NO_ERROR) {
1271 return ret;
1272 }
1273 ret = data.readInt32(&size);
1274 if (ret != NO_ERROR) {
1275 return ret;
1276 }
1277 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001278 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001279 return BAD_VALUE;
1280 }
1281 ret = write(fd.get(), buf, size);
1282 if (ret != size) return UNKNOWN_ERROR;
1283 return NO_ERROR;
1284 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001285 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1286 alarm(10);
1287 return NO_ERROR;
1288 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001289 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001290 ;
1291 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001292 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001293 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001294 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001295 return NO_ERROR;
1296 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001297 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1298 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001299 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001300 return NO_ERROR;
1301 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001302 case BINDER_LIB_TEST_ECHO_VECTOR: {
1303 std::vector<uint64_t> vector;
1304 auto err = data.readUint64Vector(&vector);
1305 if (err != NO_ERROR)
1306 return err;
1307 reply->writeUint64Vector(vector);
1308 return NO_ERROR;
1309 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001310 default:
1311 return UNKNOWN_TRANSACTION;
1312 };
1313 }
1314 private:
1315 int32_t m_id;
1316 int32_t m_nextServerId;
1317 pthread_mutex_t m_serverWaitMutex;
1318 pthread_cond_t m_serverWaitCond;
1319 bool m_serverStartRequested;
1320 sp<IBinder> m_serverStarted;
1321 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001322 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001323};
1324
Martijn Coenen45b07b42017-08-09 12:07:45 +02001325int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001326{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001327 binderLibTestServiceName += String16(binderserversuffix);
1328
Riley Andrews06b01ad2014-12-18 12:10:08 -08001329 status_t ret;
1330 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001331 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001332 {
1333 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001334
1335 /*
1336 * Normally would also contain functionality as well, but we are only
1337 * testing the extension mechanism.
1338 */
1339 testService->setExtension(new BBinder());
1340
Martijn Coenen45b07b42017-08-09 12:07:45 +02001341 /*
1342 * We need this below, but can't hold a sp<> because it prevents the
1343 * node from being cleaned up automatically. It's safe in this case
1344 * because of how the tests are written.
1345 */
1346 testServicePtr = testService.get();
1347
Riley Andrews06b01ad2014-12-18 12:10:08 -08001348 if (index == 0) {
1349 ret = sm->addService(binderLibTestServiceName, testService);
1350 } else {
1351 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1352 Parcel data, reply;
1353 data.writeInt32(index);
1354 data.writeStrongBinder(testService);
1355
1356 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1357 }
1358 }
1359 write(readypipefd, &ret, sizeof(ret));
1360 close(readypipefd);
1361 //printf("%s: ret %d\n", __func__, ret);
1362 if (ret)
1363 return 1;
1364 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001365 if (usePoll) {
1366 int fd;
1367 struct epoll_event ev;
1368 int epoll_fd;
1369 IPCThreadState::self()->setupPolling(&fd);
1370 if (fd < 0) {
1371 return 1;
1372 }
1373 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1374
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001375 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001376 if (epoll_fd == -1) {
1377 return 1;
1378 }
1379
1380 ev.events = EPOLLIN;
1381 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1382 return 1;
1383 }
1384
1385 while (1) {
1386 /*
1387 * We simulate a single-threaded process using the binder poll
1388 * interface; besides handling binder commands, it can also
1389 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001390 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001391 *
1392 * processPendingCall() will then issue that transaction.
1393 */
1394 struct epoll_event events[1];
1395 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1396 if (numEvents < 0) {
1397 if (errno == EINTR) {
1398 continue;
1399 }
1400 return 1;
1401 }
1402 if (numEvents > 0) {
1403 IPCThreadState::self()->handlePolledCommands();
1404 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1405 testServicePtr->processPendingCall();
1406 }
1407 }
1408 } else {
1409 ProcessState::self()->startThreadPool();
1410 IPCThreadState::self()->joinThreadPool();
1411 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001412 //printf("%s: joinThreadPool returned\n", __func__);
1413 return 1; /* joinThreadPool should not return */
1414}
1415
1416int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001417 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001418 binderservername = argv[2];
1419 } else {
1420 binderservername = argv[0];
1421 }
1422
Martijn Coenen45b07b42017-08-09 12:07:45 +02001423 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1424 binderserversuffix = argv[5];
1425 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001426 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001427 binderserversuffix = new char[16];
1428 snprintf(binderserversuffix, 16, "%d", getpid());
1429 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001430
1431 ::testing::InitGoogleTest(&argc, argv);
1432 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1433 ProcessState::self()->startThreadPool();
1434 return RUN_ALL_TESTS();
1435}