blob: 94ab9f0905ad4e2bd653bd77498ab6def05f5f5e [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,
Martijn Coenen82c75312019-07-24 15:18:30 +020076 BINDER_LIB_TEST_REJECT_BUF,
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) {
110 close(pipefd[0]);
111 execv(binderservername, childargv);
112 status = -errno;
113 write(pipefd[1], &status, sizeof(status));
114 fprintf(stderr, "execv failed, %s\n", strerror(errno));
115 _exit(EXIT_FAILURE);
116 }
117 close(pipefd[1]);
118 ret = read(pipefd[0], &status, sizeof(status));
119 //printf("pipe read returned %d, status %d\n", ret, status);
120 close(pipefd[0]);
121 if (ret == sizeof(status)) {
122 ret = status;
123 } else {
124 kill(pid, SIGKILL);
125 if (ret >= 0) {
126 ret = NO_INIT;
127 }
128 }
129 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700130 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800131 return ret;
132 }
133 return pid;
134}
135
136class BinderLibTestEnv : public ::testing::Environment {
137 public:
138 BinderLibTestEnv() {}
139 sp<IBinder> getServer(void) {
140 return m_server;
141 }
142
143 private:
144 virtual void SetUp() {
145 m_serverpid = start_server_process(0);
146 //printf("m_serverpid %d\n", m_serverpid);
147 ASSERT_GT(m_serverpid, 0);
148
149 sp<IServiceManager> sm = defaultServiceManager();
150 //printf("%s: pid %d, get service\n", __func__, m_pid);
151 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700152 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800153 //printf("%s: pid %d, get service done\n", __func__, m_pid);
154 }
155 virtual void TearDown() {
156 status_t ret;
157 Parcel data, reply;
158 int exitStatus;
159 pid_t pid;
160
161 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700162 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800163 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
164 EXPECT_EQ(0, ret);
165 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
166 EXPECT_EQ(0, ret);
167 }
168 if (m_serverpid > 0) {
169 //printf("wait for %d\n", m_pids[i]);
170 pid = wait(&exitStatus);
171 EXPECT_EQ(m_serverpid, pid);
172 EXPECT_TRUE(WIFEXITED(exitStatus));
173 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
174 }
175 }
176
177 pid_t m_serverpid;
178 sp<IBinder> m_server;
179};
180
181class BinderLibTest : public ::testing::Test {
182 public:
183 virtual void SetUp() {
184 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000185 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800186 }
187 virtual void TearDown() {
188 }
189 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200190 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800191 {
192 int ret;
193 int32_t id;
194 Parcel data, reply;
195 sp<IBinder> binder;
196
Martijn Coenen45b07b42017-08-09 12:07:45 +0200197 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800198 EXPECT_EQ(NO_ERROR, ret);
199
Yi Kong91635562018-06-07 14:38:36 -0700200 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800201 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700202 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800203 ret = reply.readInt32(&id);
204 EXPECT_EQ(NO_ERROR, ret);
205 if (idPtr)
206 *idPtr = id;
207 return binder;
208 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200209
Yi Kong91635562018-06-07 14:38:36 -0700210 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200211 {
212 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
213 }
214
Yi Kong91635562018-06-07 14:38:36 -0700215 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200216 {
217 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
218 }
219
Riley Andrews06b01ad2014-12-18 12:10:08 -0800220 void waitForReadData(int fd, int timeout_ms) {
221 int ret;
222 pollfd pfd = pollfd();
223
224 pfd.fd = fd;
225 pfd.events = POLLIN;
226 ret = poll(&pfd, 1, timeout_ms);
227 EXPECT_EQ(1, ret);
228 }
229
230 sp<IBinder> m_server;
231};
232
233class BinderLibTestBundle : public Parcel
234{
235 public:
236 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800237 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800238 int32_t mark;
239 int32_t bundleLen;
240 size_t pos;
241
242 if (source->readInt32(&mark))
243 return;
244 if (mark != MARK_START)
245 return;
246 if (source->readInt32(&bundleLen))
247 return;
248 pos = source->dataPosition();
249 if (Parcel::appendFrom(source, pos, bundleLen))
250 return;
251 source->setDataPosition(pos + bundleLen);
252 if (source->readInt32(&mark))
253 return;
254 if (mark != MARK_END)
255 return;
256 m_isValid = true;
257 setDataPosition(0);
258 }
259 void appendTo(Parcel *dest) {
260 dest->writeInt32(MARK_START);
261 dest->writeInt32(dataSize());
262 dest->appendFrom(this, 0, dataSize());
263 dest->writeInt32(MARK_END);
264 };
265 bool isValid(void) {
266 return m_isValid;
267 }
268 private:
269 enum {
270 MARK_START = B_PACK_CHARS('B','T','B','S'),
271 MARK_END = B_PACK_CHARS('B','T','B','E'),
272 };
273 bool m_isValid;
274};
275
276class BinderLibTestEvent
277{
278 public:
279 BinderLibTestEvent(void)
280 : m_eventTriggered(false)
281 {
Yi Kong91635562018-06-07 14:38:36 -0700282 pthread_mutex_init(&m_waitMutex, nullptr);
283 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800284 }
285 int waitEvent(int timeout_s)
286 {
287 int ret;
288 pthread_mutex_lock(&m_waitMutex);
289 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800290 struct timespec ts;
291 clock_gettime(CLOCK_REALTIME, &ts);
292 ts.tv_sec += timeout_s;
293 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800294 }
295 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
296 pthread_mutex_unlock(&m_waitMutex);
297 return ret;
298 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200299 pthread_t getTriggeringThread()
300 {
301 return m_triggeringThread;
302 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800303 protected:
304 void triggerEvent(void) {
305 pthread_mutex_lock(&m_waitMutex);
306 pthread_cond_signal(&m_waitCond);
307 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200308 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800309 pthread_mutex_unlock(&m_waitMutex);
310 };
311 private:
312 pthread_mutex_t m_waitMutex;
313 pthread_cond_t m_waitCond;
314 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200315 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800316};
317
318class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
319{
320 public:
321 BinderLibTestCallBack()
322 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700323 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800324 {
325 }
326 status_t getResult(void)
327 {
328 return m_result;
329 }
330
331 private:
332 virtual status_t onTransact(uint32_t code,
333 const Parcel& data, Parcel* reply,
334 uint32_t flags = 0)
335 {
336 (void)reply;
337 (void)flags;
338 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200339 case BINDER_LIB_TEST_CALL_BACK: {
340 status_t status = data.readInt32(&m_result);
341 if (status != NO_ERROR) {
342 m_result = status;
343 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800344 triggerEvent();
345 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200346 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700347 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
348 sp<IBinder> server;
349 int ret;
350 const uint8_t *buf = data.data();
351 size_t size = data.dataSize();
352 if (m_prev_end) {
353 /* 64-bit kernel needs at most 8 bytes to align buffer end */
354 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
355 } else {
356 EXPECT_TRUE(IsPageAligned((void *)buf));
357 }
358
359 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
360
361 if (size > 0) {
362 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
363 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
364 data, reply);
365 EXPECT_EQ(NO_ERROR, ret);
366 }
367 return NO_ERROR;
368 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800369 default:
370 return UNKNOWN_TRANSACTION;
371 }
372 }
373
374 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700375 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800376};
377
378class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
379{
380 private:
381 virtual void binderDied(const wp<IBinder>& who) {
382 (void)who;
383 triggerEvent();
384 };
385};
386
387TEST_F(BinderLibTest, NopTransaction) {
388 status_t ret;
389 Parcel data, reply;
390 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
391 EXPECT_EQ(NO_ERROR, ret);
392}
393
394TEST_F(BinderLibTest, SetError) {
395 int32_t testValue[] = { 0, -123, 123 };
396 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
397 status_t ret;
398 Parcel data, reply;
399 data.writeInt32(testValue[i]);
400 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
401 EXPECT_EQ(testValue[i], ret);
402 }
403}
404
405TEST_F(BinderLibTest, GetId) {
406 status_t ret;
407 int32_t id;
408 Parcel data, reply;
409 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
410 EXPECT_EQ(NO_ERROR, ret);
411 ret = reply.readInt32(&id);
412 EXPECT_EQ(NO_ERROR, ret);
413 EXPECT_EQ(0, id);
414}
415
416TEST_F(BinderLibTest, PtrSize) {
417 status_t ret;
418 int32_t ptrsize;
419 Parcel data, reply;
420 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700421 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800422 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
423 EXPECT_EQ(NO_ERROR, ret);
424 ret = reply.readInt32(&ptrsize);
425 EXPECT_EQ(NO_ERROR, ret);
426 RecordProperty("TestPtrSize", sizeof(void *));
427 RecordProperty("ServerPtrSize", sizeof(void *));
428}
429
430TEST_F(BinderLibTest, IndirectGetId2)
431{
432 status_t ret;
433 int32_t id;
434 int32_t count;
435 Parcel data, reply;
436 int32_t serverId[3];
437
438 data.writeInt32(ARRAY_SIZE(serverId));
439 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
440 sp<IBinder> server;
441 BinderLibTestBundle datai;
442
443 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700444 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800445 data.writeStrongBinder(server);
446 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
447 datai.appendTo(&data);
448 }
449
450 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
451 ASSERT_EQ(NO_ERROR, ret);
452
453 ret = reply.readInt32(&id);
454 ASSERT_EQ(NO_ERROR, ret);
455 EXPECT_EQ(0, id);
456
457 ret = reply.readInt32(&count);
458 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700459 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800460
461 for (size_t i = 0; i < (size_t)count; i++) {
462 BinderLibTestBundle replyi(&reply);
463 EXPECT_TRUE(replyi.isValid());
464 ret = replyi.readInt32(&id);
465 EXPECT_EQ(NO_ERROR, ret);
466 EXPECT_EQ(serverId[i], id);
467 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
468 }
469
470 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
471}
472
473TEST_F(BinderLibTest, IndirectGetId3)
474{
475 status_t ret;
476 int32_t id;
477 int32_t count;
478 Parcel data, reply;
479 int32_t serverId[3];
480
481 data.writeInt32(ARRAY_SIZE(serverId));
482 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
483 sp<IBinder> server;
484 BinderLibTestBundle datai;
485 BinderLibTestBundle datai2;
486
487 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700488 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800489 data.writeStrongBinder(server);
490 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
491
492 datai.writeInt32(1);
493 datai.writeStrongBinder(m_server);
494 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
495 datai2.appendTo(&datai);
496
497 datai.appendTo(&data);
498 }
499
500 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
501 ASSERT_EQ(NO_ERROR, ret);
502
503 ret = reply.readInt32(&id);
504 ASSERT_EQ(NO_ERROR, ret);
505 EXPECT_EQ(0, id);
506
507 ret = reply.readInt32(&count);
508 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700509 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800510
511 for (size_t i = 0; i < (size_t)count; i++) {
512 int32_t counti;
513
514 BinderLibTestBundle replyi(&reply);
515 EXPECT_TRUE(replyi.isValid());
516 ret = replyi.readInt32(&id);
517 EXPECT_EQ(NO_ERROR, ret);
518 EXPECT_EQ(serverId[i], id);
519
520 ret = replyi.readInt32(&counti);
521 ASSERT_EQ(NO_ERROR, ret);
522 EXPECT_EQ(1, counti);
523
524 BinderLibTestBundle replyi2(&replyi);
525 EXPECT_TRUE(replyi2.isValid());
526 ret = replyi2.readInt32(&id);
527 EXPECT_EQ(NO_ERROR, ret);
528 EXPECT_EQ(0, id);
529 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
530
531 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
532 }
533
534 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
535}
536
537TEST_F(BinderLibTest, CallBack)
538{
539 status_t ret;
540 Parcel data, reply;
541 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
542 data.writeStrongBinder(callBack);
543 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
544 EXPECT_EQ(NO_ERROR, ret);
545 ret = callBack->waitEvent(5);
546 EXPECT_EQ(NO_ERROR, ret);
547 ret = callBack->getResult();
548 EXPECT_EQ(NO_ERROR, ret);
549}
550
551TEST_F(BinderLibTest, AddServer)
552{
553 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700554 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800555}
556
Riley Andrews06b01ad2014-12-18 12:10:08 -0800557TEST_F(BinderLibTest, DeathNotificationStrongRef)
558{
559 status_t ret;
560 sp<IBinder> sbinder;
561
562 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
563
564 {
565 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700566 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800567 ret = binder->linkToDeath(testDeathRecipient);
568 EXPECT_EQ(NO_ERROR, ret);
569 sbinder = binder;
570 }
571 {
572 Parcel data, reply;
573 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
574 EXPECT_EQ(0, ret);
575 }
576 IPCThreadState::self()->flushCommands();
577 ret = testDeathRecipient->waitEvent(5);
578 EXPECT_EQ(NO_ERROR, ret);
579 ret = sbinder->unlinkToDeath(testDeathRecipient);
580 EXPECT_EQ(DEAD_OBJECT, ret);
581}
582
583TEST_F(BinderLibTest, DeathNotificationMultiple)
584{
585 status_t ret;
586 const int clientcount = 2;
587 sp<IBinder> target;
588 sp<IBinder> linkedclient[clientcount];
589 sp<BinderLibTestCallBack> callBack[clientcount];
590 sp<IBinder> passiveclient[clientcount];
591
592 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700593 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800594 for (int i = 0; i < clientcount; i++) {
595 {
596 Parcel data, reply;
597
598 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700599 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800600 callBack[i] = new BinderLibTestCallBack();
601 data.writeStrongBinder(target);
602 data.writeStrongBinder(callBack[i]);
603 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
604 EXPECT_EQ(NO_ERROR, ret);
605 }
606 {
607 Parcel data, reply;
608
609 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700610 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800611 data.writeStrongBinder(target);
612 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
613 EXPECT_EQ(NO_ERROR, ret);
614 }
615 }
616 {
617 Parcel data, reply;
618 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
619 EXPECT_EQ(0, ret);
620 }
621
622 for (int i = 0; i < clientcount; i++) {
623 ret = callBack[i]->waitEvent(5);
624 EXPECT_EQ(NO_ERROR, ret);
625 ret = callBack[i]->getResult();
626 EXPECT_EQ(NO_ERROR, ret);
627 }
628}
629
Martijn Coenenf7100e42017-07-31 12:14:09 +0200630TEST_F(BinderLibTest, DeathNotificationThread)
631{
632 status_t ret;
633 sp<BinderLibTestCallBack> callback;
634 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700635 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200636 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700637 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200638
639 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
640
641 ret = target->linkToDeath(testDeathRecipient);
642 EXPECT_EQ(NO_ERROR, ret);
643
644 {
645 Parcel data, reply;
646 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
647 EXPECT_EQ(0, ret);
648 }
649
650 /* Make sure it's dead */
651 testDeathRecipient->waitEvent(5);
652
653 /* Now, pass the ref to another process and ask that process to
654 * call linkToDeath() on it, and wait for a response. This tests
655 * two things:
656 * 1) You still get death notifications when calling linkToDeath()
657 * on a ref that is already dead when it was passed to you.
658 * 2) That death notifications are not directly pushed to the thread
659 * registering them, but to the threadpool (proc workqueue) instead.
660 *
661 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
662 * is blocked on a condition variable waiting for the death notification to be
663 * called; therefore, that thread is not available for handling proc work.
664 * So, if the death notification was pushed to the thread workqueue, the callback
665 * would never be called, and the test would timeout and fail.
666 *
667 * Note that we can't do this part of the test from this thread itself, because
668 * the binder driver would only push death notifications to the thread if
669 * it is a looper thread, which this thread is not.
670 *
671 * See b/23525545 for details.
672 */
673 {
674 Parcel data, reply;
675
676 callback = new BinderLibTestCallBack();
677 data.writeStrongBinder(target);
678 data.writeStrongBinder(callback);
679 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
680 EXPECT_EQ(NO_ERROR, ret);
681 }
682
683 ret = callback->waitEvent(5);
684 EXPECT_EQ(NO_ERROR, ret);
685 ret = callback->getResult();
686 EXPECT_EQ(NO_ERROR, ret);
687}
688
Riley Andrews06b01ad2014-12-18 12:10:08 -0800689TEST_F(BinderLibTest, PassFile) {
690 int ret;
691 int pipefd[2];
692 uint8_t buf[1] = { 0 };
693 uint8_t write_value = 123;
694
695 ret = pipe2(pipefd, O_NONBLOCK);
696 ASSERT_EQ(0, ret);
697
698 {
699 Parcel data, reply;
700 uint8_t writebuf[1] = { write_value };
701
702 ret = data.writeFileDescriptor(pipefd[1], true);
703 EXPECT_EQ(NO_ERROR, ret);
704
705 ret = data.writeInt32(sizeof(writebuf));
706 EXPECT_EQ(NO_ERROR, ret);
707
708 ret = data.write(writebuf, sizeof(writebuf));
709 EXPECT_EQ(NO_ERROR, ret);
710
711 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
712 EXPECT_EQ(NO_ERROR, ret);
713 }
714
715 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700716 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800717 EXPECT_EQ(write_value, buf[0]);
718
719 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
720
721 ret = read(pipefd[0], buf, sizeof(buf));
722 EXPECT_EQ(0, ret);
723
724 close(pipefd[0]);
725}
726
Ryo Hashimotobf551892018-05-31 16:58:35 +0900727TEST_F(BinderLibTest, PassParcelFileDescriptor) {
728 const int datasize = 123;
729 std::vector<uint8_t> writebuf(datasize);
730 for (size_t i = 0; i < writebuf.size(); ++i) {
731 writebuf[i] = i;
732 }
733
734 android::base::unique_fd read_end, write_end;
735 {
736 int pipefd[2];
737 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
738 read_end.reset(pipefd[0]);
739 write_end.reset(pipefd[1]);
740 }
741 {
742 Parcel data;
743 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
744 write_end.reset();
745 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
746 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
747
748 Parcel reply;
749 EXPECT_EQ(NO_ERROR,
750 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
751 &reply));
752 }
753 std::vector<uint8_t> readbuf(datasize);
754 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
755 EXPECT_EQ(writebuf, readbuf);
756
757 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
758
759 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
760}
761
Riley Andrews06b01ad2014-12-18 12:10:08 -0800762TEST_F(BinderLibTest, PromoteLocal) {
763 sp<IBinder> strong = new BBinder();
764 wp<IBinder> weak = strong;
765 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700766 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800767 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700768 strong = nullptr;
769 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800770 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700771 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800772}
773
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700774TEST_F(BinderLibTest, LocalGetExtension) {
775 sp<BBinder> binder = new BBinder();
776 sp<IBinder> ext = new BBinder();
777 binder->setExtension(ext);
778 EXPECT_EQ(ext, binder->getExtension());
779}
780
781TEST_F(BinderLibTest, RemoteGetExtension) {
782 sp<IBinder> server = addServer();
783 ASSERT_TRUE(server != nullptr);
784
785 sp<IBinder> extension;
786 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
787 ASSERT_NE(nullptr, extension.get());
788
789 EXPECT_EQ(NO_ERROR, extension->pingBinder());
790}
791
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700792TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
793 status_t ret;
794 Parcel data, reply;
795
796 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
797 EXPECT_EQ(NO_ERROR, ret);
798
799 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700800 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800801 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
802 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
803 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
804 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700805}
806
Connor O'Brien52be2c92016-09-20 14:18:08 -0700807TEST_F(BinderLibTest, FreedBinder) {
808 status_t ret;
809
810 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700811 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700812
813 __u32 freedHandle;
814 wp<IBinder> keepFreedBinder;
815 {
816 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700817 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
818 ASSERT_EQ(NO_ERROR, ret);
819 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
820 freedHandle = freed->handle;
821 /* Add a weak ref to the freed binder so the driver does not
822 * delete its reference to it - otherwise the transaction
823 * fails regardless of whether the driver is fixed.
824 */
Steven Morelande171d622019-07-17 16:06:01 -0700825 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700826 }
Steven Morelande171d622019-07-17 16:06:01 -0700827 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700828 {
829 Parcel data, reply;
830 data.writeStrongBinder(server);
831 /* Replace original handle with handle to the freed binder */
832 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
833 __u32 oldHandle = strong->handle;
834 strong->handle = freedHandle;
835 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
836 /* Returns DEAD_OBJECT (-32) if target crashes and
837 * FAILED_TRANSACTION if the driver rejects the invalid
838 * object.
839 */
840 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
841 /* Restore original handle so parcel destructor does not use
842 * the wrong handle.
843 */
844 strong->handle = oldHandle;
845 }
846}
847
Sherry Yang336cdd32017-07-24 14:12:27 -0700848TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
849 status_t ret;
850 Parcel data, reply;
851 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
852 for (int i = 0; i < 2; i++) {
853 BinderLibTestBundle datai;
854 datai.appendFrom(&data, 0, data.dataSize());
855
856 data.freeData();
857 data.writeInt32(1);
858 data.writeStrongBinder(callBack);
859 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
860
861 datai.appendTo(&data);
862 }
863 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
864 EXPECT_EQ(NO_ERROR, ret);
865}
866
Martijn Coenen45b07b42017-08-09 12:07:45 +0200867TEST_F(BinderLibTest, OnewayQueueing)
868{
869 status_t ret;
870 Parcel data, data2;
871
872 sp<IBinder> pollServer = addPollServer();
873
874 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
875 data.writeStrongBinder(callBack);
876 data.writeInt32(500000); // delay in us before calling back
877
878 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
879 data2.writeStrongBinder(callBack2);
880 data2.writeInt32(0); // delay in us
881
Yi Kong91635562018-06-07 14:38:36 -0700882 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200883 EXPECT_EQ(NO_ERROR, ret);
884
885 // The delay ensures that this second transaction will end up on the async_todo list
886 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700887 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200888 EXPECT_EQ(NO_ERROR, ret);
889
890 // The server will ensure that the two transactions are handled in the expected order;
891 // If the ordering is not as expected, an error will be returned through the callbacks.
892 ret = callBack->waitEvent(2);
893 EXPECT_EQ(NO_ERROR, ret);
894 ret = callBack->getResult();
895 EXPECT_EQ(NO_ERROR, ret);
896
897 ret = callBack2->waitEvent(2);
898 EXPECT_EQ(NO_ERROR, ret);
899 ret = callBack2->getResult();
900 EXPECT_EQ(NO_ERROR, ret);
901}
902
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100903TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
904{
905 status_t ret;
906 Parcel data, reply;
907 data.writeInterfaceToken(binderLibTestServiceName);
908 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
909 EXPECT_EQ(-1, reply.readInt32());
910 EXPECT_EQ(NO_ERROR, ret);
911}
912
913TEST_F(BinderLibTest, WorkSourceSet)
914{
915 status_t ret;
916 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000917 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000918 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100919 data.writeInterfaceToken(binderLibTestServiceName);
920 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
921 EXPECT_EQ(100, reply.readInt32());
922 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000923 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
924 EXPECT_EQ(NO_ERROR, ret);
925}
926
927TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
928{
929 status_t ret;
930 Parcel data, reply;
931
932 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
933 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
934
935 data.writeInterfaceToken(binderLibTestServiceName);
936 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
937 EXPECT_EQ(-1, reply.readInt32());
938 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100939 EXPECT_EQ(NO_ERROR, ret);
940}
941
942TEST_F(BinderLibTest, WorkSourceCleared)
943{
944 status_t ret;
945 Parcel data, reply;
946
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000947 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000948 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
949 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100950 data.writeInterfaceToken(binderLibTestServiceName);
951 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
952
953 EXPECT_EQ(-1, reply.readInt32());
954 EXPECT_EQ(100, previousWorkSource);
955 EXPECT_EQ(NO_ERROR, ret);
956}
957
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000958TEST_F(BinderLibTest, WorkSourceRestored)
959{
960 status_t ret;
961 Parcel data, reply;
962
963 IPCThreadState::self()->setCallingWorkSourceUid(100);
964 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
965 IPCThreadState::self()->restoreCallingWorkSource(token);
966
967 data.writeInterfaceToken(binderLibTestServiceName);
968 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
969
970 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +0000971 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000972 EXPECT_EQ(NO_ERROR, ret);
973}
974
Olivier Gaillard91a04802018-11-14 17:32:41 +0000975TEST_F(BinderLibTest, PropagateFlagSet)
976{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000977 IPCThreadState::self()->clearPropagateWorkSource();
978 IPCThreadState::self()->setCallingWorkSourceUid(100);
979 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
980}
981
982TEST_F(BinderLibTest, PropagateFlagCleared)
983{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000984 IPCThreadState::self()->setCallingWorkSourceUid(100);
985 IPCThreadState::self()->clearPropagateWorkSource();
986 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
987}
988
989TEST_F(BinderLibTest, PropagateFlagRestored)
990{
Olivier Gaillard91a04802018-11-14 17:32:41 +0000991 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
992 IPCThreadState::self()->restoreCallingWorkSource(token);
993
994 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
995}
996
997TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
998{
999 IPCThreadState::self()->setCallingWorkSourceUid(100);
1000
1001 Parcel data, reply;
1002 status_t ret;
1003 data.writeInterfaceToken(binderLibTestServiceName);
1004 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1005
1006 Parcel data2, reply2;
1007 status_t ret2;
1008 data2.writeInterfaceToken(binderLibTestServiceName);
1009 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1010 EXPECT_EQ(100, reply2.readInt32());
1011 EXPECT_EQ(NO_ERROR, ret2);
1012}
1013
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001014TEST_F(BinderLibTest, VectorSent) {
1015 Parcel data, reply;
1016 sp<IBinder> server = addServer();
1017 ASSERT_TRUE(server != nullptr);
1018
1019 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1020 data.writeUint64Vector(testValue);
1021
1022 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1023 EXPECT_EQ(NO_ERROR, ret);
1024 std::vector<uint64_t> readValue;
1025 ret = reply.readUint64Vector(&readValue);
1026 EXPECT_EQ(readValue, testValue);
1027}
1028
Martijn Coenen82c75312019-07-24 15:18:30 +02001029TEST_F(BinderLibTest, BufRejected) {
1030 Parcel data, reply;
1031 uint32_t buf;
1032 sp<IBinder> server = addServer();
1033 ASSERT_TRUE(server != nullptr);
1034
1035 binder_buffer_object obj {
1036 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001037 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001038 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1039 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001040 };
1041 data.setDataCapacity(1024);
1042 // Write a bogus object at offset 0 to get an entry in the offset table
1043 data.writeFileDescriptor(0);
1044 EXPECT_EQ(data.objectsCount(), 1);
1045 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1046 // And now, overwrite it with the buffer object
1047 memcpy(parcelData, &obj, sizeof(obj));
1048 data.setDataSize(sizeof(obj));
1049
1050 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1051 // Either the kernel should reject this transaction (if it's correct), but
1052 // if it's not, the server implementation should return an error if it
1053 // finds an object in the received Parcel.
1054 EXPECT_NE(NO_ERROR, ret);
1055}
1056
Riley Andrews06b01ad2014-12-18 12:10:08 -08001057class BinderLibTestService : public BBinder
1058{
1059 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001060 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001061 : m_id(id)
1062 , m_nextServerId(id + 1)
1063 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001064 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001065 {
Yi Kong91635562018-06-07 14:38:36 -07001066 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1067 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001068 }
1069 ~BinderLibTestService()
1070 {
1071 exit(EXIT_SUCCESS);
1072 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001073
1074 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001075 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001076 Parcel data;
1077 data.writeInt32(NO_ERROR);
1078 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001079 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001080 }
1081 }
1082
Riley Andrews06b01ad2014-12-18 12:10:08 -08001083 virtual status_t onTransact(uint32_t code,
1084 const Parcel& data, Parcel* reply,
1085 uint32_t flags = 0) {
1086 //printf("%s: code %d\n", __func__, code);
1087 (void)flags;
1088
1089 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1090 return PERMISSION_DENIED;
1091 }
1092 switch (code) {
1093 case BINDER_LIB_TEST_REGISTER_SERVER: {
1094 int32_t id;
1095 sp<IBinder> binder;
1096 id = data.readInt32();
1097 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001098 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001099 return BAD_VALUE;
1100 }
1101
1102 if (m_id != 0)
1103 return INVALID_OPERATION;
1104
1105 pthread_mutex_lock(&m_serverWaitMutex);
1106 if (m_serverStartRequested) {
1107 m_serverStartRequested = false;
1108 m_serverStarted = binder;
1109 pthread_cond_signal(&m_serverWaitCond);
1110 }
1111 pthread_mutex_unlock(&m_serverWaitMutex);
1112 return NO_ERROR;
1113 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001114 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001115 case BINDER_LIB_TEST_ADD_SERVER: {
1116 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001117 int serverid;
1118
1119 if (m_id != 0) {
1120 return INVALID_OPERATION;
1121 }
1122 pthread_mutex_lock(&m_serverWaitMutex);
1123 if (m_serverStartRequested) {
1124 ret = -EBUSY;
1125 } else {
1126 serverid = m_nextServerId++;
1127 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001128 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001129
1130 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001131 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001132 pthread_mutex_lock(&m_serverWaitMutex);
1133 }
1134 if (ret > 0) {
1135 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001136 struct timespec ts;
1137 clock_gettime(CLOCK_REALTIME, &ts);
1138 ts.tv_sec += 5;
1139 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001140 }
1141 if (m_serverStartRequested) {
1142 m_serverStartRequested = false;
1143 ret = -ETIMEDOUT;
1144 } else {
1145 reply->writeStrongBinder(m_serverStarted);
1146 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001147 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001148 ret = NO_ERROR;
1149 }
1150 } else if (ret >= 0) {
1151 m_serverStartRequested = false;
1152 ret = UNKNOWN_ERROR;
1153 }
1154 pthread_mutex_unlock(&m_serverWaitMutex);
1155 return ret;
1156 }
1157 case BINDER_LIB_TEST_NOP_TRANSACTION:
1158 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001159 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1160 // Note: this transaction is only designed for use with a
1161 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001162 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001163 // A callback was already pending; this means that
1164 // we received a second call while still processing
1165 // the first one. Fail the test.
1166 sp<IBinder> callback = data.readStrongBinder();
1167 Parcel data2;
1168 data2.writeInt32(UNKNOWN_ERROR);
1169
Yi Kong91635562018-06-07 14:38:36 -07001170 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001171 } else {
1172 m_callback = data.readStrongBinder();
1173 int32_t delayUs = data.readInt32();
1174 /*
1175 * It's necessary that we sleep here, so the next
1176 * transaction the caller makes will be queued to
1177 * the async queue.
1178 */
1179 usleep(delayUs);
1180
1181 /*
1182 * Now when we return, libbinder will tell the kernel
1183 * we are done with this transaction, and the kernel
1184 * can move the queued transaction to either the
1185 * thread todo worklist (for kernels without the fix),
1186 * or the proc todo worklist. In case of the former,
1187 * the next outbound call will pick up the pending
1188 * transaction, which leads to undesired reentrant
1189 * behavior. This is caught in the if() branch above.
1190 */
1191 }
1192
1193 return NO_ERROR;
1194 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001195 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1196 Parcel data2, reply2;
1197 sp<IBinder> binder;
1198 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001199 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001200 return BAD_VALUE;
1201 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001202 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001203 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1204 return NO_ERROR;
1205 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001206 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1207 reply->writeStrongBinder(this);
1208 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001209 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1210 reply->writeInt32(m_id);
1211 return NO_ERROR;
1212 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1213 int32_t count;
1214 uint32_t indirect_code;
1215 sp<IBinder> binder;
1216
1217 count = data.readInt32();
1218 reply->writeInt32(m_id);
1219 reply->writeInt32(count);
1220 for (int i = 0; i < count; i++) {
1221 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001222 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001223 return BAD_VALUE;
1224 }
1225 indirect_code = data.readInt32();
1226 BinderLibTestBundle data2(&data);
1227 if (!data2.isValid()) {
1228 return BAD_VALUE;
1229 }
1230 BinderLibTestBundle reply2;
1231 binder->transact(indirect_code, data2, &reply2);
1232 reply2.appendTo(reply);
1233 }
1234 return NO_ERROR;
1235 }
1236 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1237 reply->setError(data.readInt32());
1238 return NO_ERROR;
1239 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1240 reply->writeInt32(sizeof(void *));
1241 return NO_ERROR;
1242 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1243 return NO_ERROR;
1244 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1245 m_strongRef = data.readStrongBinder();
1246 return NO_ERROR;
1247 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1248 int ret;
1249 Parcel data2, reply2;
1250 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1251 sp<IBinder> target;
1252 sp<IBinder> callback;
1253
1254 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001255 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001256 return BAD_VALUE;
1257 }
1258 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001259 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001260 return BAD_VALUE;
1261 }
1262 ret = target->linkToDeath(testDeathRecipient);
1263 if (ret == NO_ERROR)
1264 ret = testDeathRecipient->waitEvent(5);
1265 data2.writeInt32(ret);
1266 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1267 return NO_ERROR;
1268 }
1269 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1270 int ret;
1271 int32_t size;
1272 const void *buf;
1273 int fd;
1274
1275 fd = data.readFileDescriptor();
1276 if (fd < 0) {
1277 return BAD_VALUE;
1278 }
1279 ret = data.readInt32(&size);
1280 if (ret != NO_ERROR) {
1281 return ret;
1282 }
1283 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001284 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001285 return BAD_VALUE;
1286 }
1287 ret = write(fd, buf, size);
1288 if (ret != size)
1289 return UNKNOWN_ERROR;
1290 return NO_ERROR;
1291 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001292 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1293 int ret;
1294 int32_t size;
1295 const void *buf;
1296 android::base::unique_fd fd;
1297
1298 ret = data.readUniqueParcelFileDescriptor(&fd);
1299 if (ret != NO_ERROR) {
1300 return ret;
1301 }
1302 ret = data.readInt32(&size);
1303 if (ret != NO_ERROR) {
1304 return ret;
1305 }
1306 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001307 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001308 return BAD_VALUE;
1309 }
1310 ret = write(fd.get(), buf, size);
1311 if (ret != size) return UNKNOWN_ERROR;
1312 return NO_ERROR;
1313 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001314 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1315 alarm(10);
1316 return NO_ERROR;
1317 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001318 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001319 ;
1320 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001321 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001322 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001323 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001324 return NO_ERROR;
1325 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001326 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1327 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001328 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001329 return NO_ERROR;
1330 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001331 case BINDER_LIB_TEST_ECHO_VECTOR: {
1332 std::vector<uint64_t> vector;
1333 auto err = data.readUint64Vector(&vector);
1334 if (err != NO_ERROR)
1335 return err;
1336 reply->writeUint64Vector(vector);
1337 return NO_ERROR;
1338 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001339 case BINDER_LIB_TEST_REJECT_BUF: {
1340 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1341 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001342 default:
1343 return UNKNOWN_TRANSACTION;
1344 };
1345 }
1346 private:
1347 int32_t m_id;
1348 int32_t m_nextServerId;
1349 pthread_mutex_t m_serverWaitMutex;
1350 pthread_cond_t m_serverWaitCond;
1351 bool m_serverStartRequested;
1352 sp<IBinder> m_serverStarted;
1353 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001354 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001355};
1356
Martijn Coenen45b07b42017-08-09 12:07:45 +02001357int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001359 binderLibTestServiceName += String16(binderserversuffix);
1360
Riley Andrews06b01ad2014-12-18 12:10:08 -08001361 status_t ret;
1362 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001363 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001364 {
1365 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001366
1367 /*
1368 * Normally would also contain functionality as well, but we are only
1369 * testing the extension mechanism.
1370 */
1371 testService->setExtension(new BBinder());
1372
Martijn Coenen82c75312019-07-24 15:18:30 +02001373 // Required for test "BufRejected'
1374 testService->setRequestingSid(true);
1375
Martijn Coenen45b07b42017-08-09 12:07:45 +02001376 /*
1377 * We need this below, but can't hold a sp<> because it prevents the
1378 * node from being cleaned up automatically. It's safe in this case
1379 * because of how the tests are written.
1380 */
1381 testServicePtr = testService.get();
1382
Riley Andrews06b01ad2014-12-18 12:10:08 -08001383 if (index == 0) {
1384 ret = sm->addService(binderLibTestServiceName, testService);
1385 } else {
1386 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1387 Parcel data, reply;
1388 data.writeInt32(index);
1389 data.writeStrongBinder(testService);
1390
1391 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1392 }
1393 }
1394 write(readypipefd, &ret, sizeof(ret));
1395 close(readypipefd);
1396 //printf("%s: ret %d\n", __func__, ret);
1397 if (ret)
1398 return 1;
1399 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001400 if (usePoll) {
1401 int fd;
1402 struct epoll_event ev;
1403 int epoll_fd;
1404 IPCThreadState::self()->setupPolling(&fd);
1405 if (fd < 0) {
1406 return 1;
1407 }
1408 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1409
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001410 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001411 if (epoll_fd == -1) {
1412 return 1;
1413 }
1414
1415 ev.events = EPOLLIN;
1416 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1417 return 1;
1418 }
1419
1420 while (1) {
1421 /*
1422 * We simulate a single-threaded process using the binder poll
1423 * interface; besides handling binder commands, it can also
1424 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001425 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001426 *
1427 * processPendingCall() will then issue that transaction.
1428 */
1429 struct epoll_event events[1];
1430 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1431 if (numEvents < 0) {
1432 if (errno == EINTR) {
1433 continue;
1434 }
1435 return 1;
1436 }
1437 if (numEvents > 0) {
1438 IPCThreadState::self()->handlePolledCommands();
1439 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1440 testServicePtr->processPendingCall();
1441 }
1442 }
1443 } else {
1444 ProcessState::self()->startThreadPool();
1445 IPCThreadState::self()->joinThreadPool();
1446 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001447 //printf("%s: joinThreadPool returned\n", __func__);
1448 return 1; /* joinThreadPool should not return */
1449}
1450
1451int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001452 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001453 binderservername = argv[2];
1454 } else {
1455 binderservername = argv[0];
1456 }
1457
Martijn Coenen45b07b42017-08-09 12:07:45 +02001458 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1459 binderserversuffix = argv[5];
1460 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001461 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001462 binderserversuffix = new char[16];
1463 snprintf(binderserversuffix, 16, "%d", getpid());
1464 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001465
1466 ::testing::InitGoogleTest(&argc, argv);
1467 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1468 ProcessState::self()->startThreadPool();
1469 return RUN_ALL_TESTS();
1470}