blob: 5c6cf9db64e6aea7fb8a37ec846db912979d66e9 [file] [log] [blame]
Riley Andrews06b01ad2014-12-18 12:10:08 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <poll.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <gtest/gtest.h>
25
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28#include <binder/IPCThreadState.h>
29#include <binder/IServiceManager.h>
30
Martijn Coenen45b07b42017-08-09 12:07:45 +020031#include <sys/epoll.h>
32
Riley Andrews06b01ad2014-12-18 12:10:08 -080033#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
34
35using namespace android;
36
Sherry Yang336cdd32017-07-24 14:12:27 -070037static ::testing::AssertionResult IsPageAligned(void *buf) {
38 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
39 return ::testing::AssertionSuccess();
40 else
41 return ::testing::AssertionFailure() << buf << " is not page aligned";
42}
43
Riley Andrews06b01ad2014-12-18 12:10:08 -080044static testing::Environment* binder_env;
45static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070046static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080047static char binderserverarg[] = "--binderserver";
48
49static String16 binderLibTestServiceName = String16("test.binderLib");
50
51enum BinderLibTestTranscationCode {
52 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
53 BINDER_LIB_TEST_REGISTER_SERVER,
54 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020055 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080056 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070057 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020058 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080059 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070060 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080061 BINDER_LIB_TEST_GET_ID_TRANSACTION,
62 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
63 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
64 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
65 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
66 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
67 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090068 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_EXIT_TRANSACTION,
70 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
71 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070072 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010073 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080074 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020075 BINDER_LIB_TEST_REJECT_BUF,
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
Martijn Coenen82c75312019-07-24 15:18:30 +02001028TEST_F(BinderLibTest, BufRejected) {
1029 Parcel data, reply;
1030 uint32_t buf;
1031 sp<IBinder> server = addServer();
1032 ASSERT_TRUE(server != nullptr);
1033
1034 binder_buffer_object obj {
1035 .hdr = { .type = BINDER_TYPE_PTR },
1036 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1037 .length = 4,
1038 .flags = 0,
1039 };
1040 data.setDataCapacity(1024);
1041 // Write a bogus object at offset 0 to get an entry in the offset table
1042 data.writeFileDescriptor(0);
1043 EXPECT_EQ(data.objectsCount(), 1);
1044 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1045 // And now, overwrite it with the buffer object
1046 memcpy(parcelData, &obj, sizeof(obj));
1047 data.setDataSize(sizeof(obj));
1048
1049 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1050 // Either the kernel should reject this transaction (if it's correct), but
1051 // if it's not, the server implementation should return an error if it
1052 // finds an object in the received Parcel.
1053 EXPECT_NE(NO_ERROR, ret);
1054}
1055
Riley Andrews06b01ad2014-12-18 12:10:08 -08001056class BinderLibTestService : public BBinder
1057{
1058 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001059 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001060 : m_id(id)
1061 , m_nextServerId(id + 1)
1062 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001063 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001064 {
Yi Kong91635562018-06-07 14:38:36 -07001065 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1066 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001067 }
1068 ~BinderLibTestService()
1069 {
1070 exit(EXIT_SUCCESS);
1071 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001072
1073 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001074 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001075 Parcel data;
1076 data.writeInt32(NO_ERROR);
1077 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001078 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001079 }
1080 }
1081
Riley Andrews06b01ad2014-12-18 12:10:08 -08001082 virtual status_t onTransact(uint32_t code,
1083 const Parcel& data, Parcel* reply,
1084 uint32_t flags = 0) {
1085 //printf("%s: code %d\n", __func__, code);
1086 (void)flags;
1087
1088 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1089 return PERMISSION_DENIED;
1090 }
1091 switch (code) {
1092 case BINDER_LIB_TEST_REGISTER_SERVER: {
1093 int32_t id;
1094 sp<IBinder> binder;
1095 id = data.readInt32();
1096 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001097 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001098 return BAD_VALUE;
1099 }
1100
1101 if (m_id != 0)
1102 return INVALID_OPERATION;
1103
1104 pthread_mutex_lock(&m_serverWaitMutex);
1105 if (m_serverStartRequested) {
1106 m_serverStartRequested = false;
1107 m_serverStarted = binder;
1108 pthread_cond_signal(&m_serverWaitCond);
1109 }
1110 pthread_mutex_unlock(&m_serverWaitMutex);
1111 return NO_ERROR;
1112 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001113 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001114 case BINDER_LIB_TEST_ADD_SERVER: {
1115 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001116 int serverid;
1117
1118 if (m_id != 0) {
1119 return INVALID_OPERATION;
1120 }
1121 pthread_mutex_lock(&m_serverWaitMutex);
1122 if (m_serverStartRequested) {
1123 ret = -EBUSY;
1124 } else {
1125 serverid = m_nextServerId++;
1126 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001127 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001128
1129 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001130 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001131 pthread_mutex_lock(&m_serverWaitMutex);
1132 }
1133 if (ret > 0) {
1134 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001135 struct timespec ts;
1136 clock_gettime(CLOCK_REALTIME, &ts);
1137 ts.tv_sec += 5;
1138 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001139 }
1140 if (m_serverStartRequested) {
1141 m_serverStartRequested = false;
1142 ret = -ETIMEDOUT;
1143 } else {
1144 reply->writeStrongBinder(m_serverStarted);
1145 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001146 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001147 ret = NO_ERROR;
1148 }
1149 } else if (ret >= 0) {
1150 m_serverStartRequested = false;
1151 ret = UNKNOWN_ERROR;
1152 }
1153 pthread_mutex_unlock(&m_serverWaitMutex);
1154 return ret;
1155 }
1156 case BINDER_LIB_TEST_NOP_TRANSACTION:
1157 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001158 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1159 // Note: this transaction is only designed for use with a
1160 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001161 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001162 // A callback was already pending; this means that
1163 // we received a second call while still processing
1164 // the first one. Fail the test.
1165 sp<IBinder> callback = data.readStrongBinder();
1166 Parcel data2;
1167 data2.writeInt32(UNKNOWN_ERROR);
1168
Yi Kong91635562018-06-07 14:38:36 -07001169 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001170 } else {
1171 m_callback = data.readStrongBinder();
1172 int32_t delayUs = data.readInt32();
1173 /*
1174 * It's necessary that we sleep here, so the next
1175 * transaction the caller makes will be queued to
1176 * the async queue.
1177 */
1178 usleep(delayUs);
1179
1180 /*
1181 * Now when we return, libbinder will tell the kernel
1182 * we are done with this transaction, and the kernel
1183 * can move the queued transaction to either the
1184 * thread todo worklist (for kernels without the fix),
1185 * or the proc todo worklist. In case of the former,
1186 * the next outbound call will pick up the pending
1187 * transaction, which leads to undesired reentrant
1188 * behavior. This is caught in the if() branch above.
1189 */
1190 }
1191
1192 return NO_ERROR;
1193 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001194 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1195 Parcel data2, reply2;
1196 sp<IBinder> binder;
1197 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001198 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001199 return BAD_VALUE;
1200 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001201 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001202 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1203 return NO_ERROR;
1204 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001205 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1206 reply->writeStrongBinder(this);
1207 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001208 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1209 reply->writeInt32(m_id);
1210 return NO_ERROR;
1211 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1212 int32_t count;
1213 uint32_t indirect_code;
1214 sp<IBinder> binder;
1215
1216 count = data.readInt32();
1217 reply->writeInt32(m_id);
1218 reply->writeInt32(count);
1219 for (int i = 0; i < count; i++) {
1220 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001221 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001222 return BAD_VALUE;
1223 }
1224 indirect_code = data.readInt32();
1225 BinderLibTestBundle data2(&data);
1226 if (!data2.isValid()) {
1227 return BAD_VALUE;
1228 }
1229 BinderLibTestBundle reply2;
1230 binder->transact(indirect_code, data2, &reply2);
1231 reply2.appendTo(reply);
1232 }
1233 return NO_ERROR;
1234 }
1235 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1236 reply->setError(data.readInt32());
1237 return NO_ERROR;
1238 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1239 reply->writeInt32(sizeof(void *));
1240 return NO_ERROR;
1241 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1242 return NO_ERROR;
1243 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1244 m_strongRef = data.readStrongBinder();
1245 return NO_ERROR;
1246 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1247 int ret;
1248 Parcel data2, reply2;
1249 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1250 sp<IBinder> target;
1251 sp<IBinder> callback;
1252
1253 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001254 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001255 return BAD_VALUE;
1256 }
1257 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001258 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001259 return BAD_VALUE;
1260 }
1261 ret = target->linkToDeath(testDeathRecipient);
1262 if (ret == NO_ERROR)
1263 ret = testDeathRecipient->waitEvent(5);
1264 data2.writeInt32(ret);
1265 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1266 return NO_ERROR;
1267 }
1268 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1269 int ret;
1270 int32_t size;
1271 const void *buf;
1272 int fd;
1273
1274 fd = data.readFileDescriptor();
1275 if (fd < 0) {
1276 return BAD_VALUE;
1277 }
1278 ret = data.readInt32(&size);
1279 if (ret != NO_ERROR) {
1280 return ret;
1281 }
1282 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001283 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001284 return BAD_VALUE;
1285 }
1286 ret = write(fd, buf, size);
1287 if (ret != size)
1288 return UNKNOWN_ERROR;
1289 return NO_ERROR;
1290 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001291 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1292 int ret;
1293 int32_t size;
1294 const void *buf;
1295 android::base::unique_fd fd;
1296
1297 ret = data.readUniqueParcelFileDescriptor(&fd);
1298 if (ret != NO_ERROR) {
1299 return ret;
1300 }
1301 ret = data.readInt32(&size);
1302 if (ret != NO_ERROR) {
1303 return ret;
1304 }
1305 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001306 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001307 return BAD_VALUE;
1308 }
1309 ret = write(fd.get(), buf, size);
1310 if (ret != size) return UNKNOWN_ERROR;
1311 return NO_ERROR;
1312 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001313 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1314 alarm(10);
1315 return NO_ERROR;
1316 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001317 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001318 ;
1319 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001320 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001321 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001322 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001323 return NO_ERROR;
1324 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001325 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1326 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001327 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001328 return NO_ERROR;
1329 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001330 case BINDER_LIB_TEST_ECHO_VECTOR: {
1331 std::vector<uint64_t> vector;
1332 auto err = data.readUint64Vector(&vector);
1333 if (err != NO_ERROR)
1334 return err;
1335 reply->writeUint64Vector(vector);
1336 return NO_ERROR;
1337 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001338 case BINDER_LIB_TEST_REJECT_BUF: {
1339 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1340 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001341 default:
1342 return UNKNOWN_TRANSACTION;
1343 };
1344 }
1345 private:
1346 int32_t m_id;
1347 int32_t m_nextServerId;
1348 pthread_mutex_t m_serverWaitMutex;
1349 pthread_cond_t m_serverWaitCond;
1350 bool m_serverStartRequested;
1351 sp<IBinder> m_serverStarted;
1352 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001353 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001354};
1355
Martijn Coenen45b07b42017-08-09 12:07:45 +02001356int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001357{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001358 binderLibTestServiceName += String16(binderserversuffix);
1359
Riley Andrews06b01ad2014-12-18 12:10:08 -08001360 status_t ret;
1361 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001362 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001363 {
1364 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001365
1366 /*
1367 * Normally would also contain functionality as well, but we are only
1368 * testing the extension mechanism.
1369 */
1370 testService->setExtension(new BBinder());
1371
Martijn Coenen82c75312019-07-24 15:18:30 +02001372 // Required for test "BufRejected'
1373 testService->setRequestingSid(true);
1374
Martijn Coenen45b07b42017-08-09 12:07:45 +02001375 /*
1376 * We need this below, but can't hold a sp<> because it prevents the
1377 * node from being cleaned up automatically. It's safe in this case
1378 * because of how the tests are written.
1379 */
1380 testServicePtr = testService.get();
1381
Riley Andrews06b01ad2014-12-18 12:10:08 -08001382 if (index == 0) {
1383 ret = sm->addService(binderLibTestServiceName, testService);
1384 } else {
1385 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1386 Parcel data, reply;
1387 data.writeInt32(index);
1388 data.writeStrongBinder(testService);
1389
1390 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1391 }
1392 }
1393 write(readypipefd, &ret, sizeof(ret));
1394 close(readypipefd);
1395 //printf("%s: ret %d\n", __func__, ret);
1396 if (ret)
1397 return 1;
1398 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001399 if (usePoll) {
1400 int fd;
1401 struct epoll_event ev;
1402 int epoll_fd;
1403 IPCThreadState::self()->setupPolling(&fd);
1404 if (fd < 0) {
1405 return 1;
1406 }
1407 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1408
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001409 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001410 if (epoll_fd == -1) {
1411 return 1;
1412 }
1413
1414 ev.events = EPOLLIN;
1415 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1416 return 1;
1417 }
1418
1419 while (1) {
1420 /*
1421 * We simulate a single-threaded process using the binder poll
1422 * interface; besides handling binder commands, it can also
1423 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001424 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001425 *
1426 * processPendingCall() will then issue that transaction.
1427 */
1428 struct epoll_event events[1];
1429 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1430 if (numEvents < 0) {
1431 if (errno == EINTR) {
1432 continue;
1433 }
1434 return 1;
1435 }
1436 if (numEvents > 0) {
1437 IPCThreadState::self()->handlePolledCommands();
1438 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1439 testServicePtr->processPendingCall();
1440 }
1441 }
1442 } else {
1443 ProcessState::self()->startThreadPool();
1444 IPCThreadState::self()->joinThreadPool();
1445 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001446 //printf("%s: joinThreadPool returned\n", __func__);
1447 return 1; /* joinThreadPool should not return */
1448}
1449
1450int main(int argc, char **argv) {
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001451 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001452 binderservername = argv[2];
1453 } else {
1454 binderservername = argv[0];
1455 }
1456
Martijn Coenen45b07b42017-08-09 12:07:45 +02001457 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1458 binderserversuffix = argv[5];
1459 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001460 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001461 binderserversuffix = new char[16];
1462 snprintf(binderserversuffix, 16, "%d", getpid());
1463 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001464
1465 ::testing::InitGoogleTest(&argc, argv);
1466 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1467 ProcessState::self()->startThreadPool();
1468 return RUN_ALL_TESTS();
1469}