blob: ad4729d127000651d734b160e9f2b51157b5fd3b [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>
Marco Ballesio7ee17572020-09-08 10:30:03 -070019#include <fstream>
Riley Andrews06b01ad2014-12-18 12:10:08 -080020#include <poll.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <gtest/gtest.h>
26
27#include <binder/Binder.h>
28#include <binder/IBinder.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31
Steven Morelandd63ed9d2019-09-04 18:15:25 -070032#include <private/binder/binder_module.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020033#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080034#include <sys/prctl.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020035
Steven Morelandf9f3de22020-05-06 17:14:39 -070036#include "binderAbiHelper.h"
37
Riley Andrews06b01ad2014-12-18 12:10:08 -080038#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
39
40using namespace android;
41
Sherry Yang336cdd32017-07-24 14:12:27 -070042static ::testing::AssertionResult IsPageAligned(void *buf) {
43 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
44 return ::testing::AssertionSuccess();
45 else
46 return ::testing::AssertionFailure() << buf << " is not page aligned";
47}
48
Riley Andrews06b01ad2014-12-18 12:10:08 -080049static testing::Environment* binder_env;
50static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070051static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080052static char binderserverarg[] = "--binderserver";
53
Steven Morelandbf1915b2020-07-16 22:43:02 +000054static constexpr int kSchedPolicy = SCHED_RR;
55static constexpr int kSchedPriority = 7;
56
Riley Andrews06b01ad2014-12-18 12:10:08 -080057static String16 binderLibTestServiceName = String16("test.binderLib");
58
59enum BinderLibTestTranscationCode {
60 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
61 BINDER_LIB_TEST_REGISTER_SERVER,
62 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020063 BINDER_LIB_TEST_ADD_POLL_SERVER,
Riley Andrews06b01ad2014-12-18 12:10:08 -080064 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070065 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020066 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080067 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070068 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080069 BINDER_LIB_TEST_GET_ID_TRANSACTION,
70 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
71 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
72 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
73 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
74 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
75 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +090076 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080077 BINDER_LIB_TEST_EXIT_TRANSACTION,
78 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
79 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -070080 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +010081 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +000082 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -070083 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
84 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -080085 BINDER_LIB_TEST_ECHO_VECTOR,
Martijn Coenen82c75312019-07-24 15:18:30 +020086 BINDER_LIB_TEST_REJECT_BUF,
Riley Andrews06b01ad2014-12-18 12:10:08 -080087};
88
Martijn Coenen45b07b42017-08-09 12:07:45 +020089pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -080090{
91 int ret;
92 pid_t pid;
93 status_t status;
94 int pipefd[2];
95 char stri[16];
96 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +020097 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -080098 char *childargv[] = {
99 binderservername,
100 binderserverarg,
101 stri,
102 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200103 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700104 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700105 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800106 };
107
108 ret = pipe(pipefd);
109 if (ret < 0)
110 return ret;
111
112 snprintf(stri, sizeof(stri), "%d", arg2);
113 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200114 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800115
116 pid = fork();
117 if (pid == -1)
118 return pid;
119 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800120 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800121 close(pipefd[0]);
122 execv(binderservername, childargv);
123 status = -errno;
124 write(pipefd[1], &status, sizeof(status));
125 fprintf(stderr, "execv failed, %s\n", strerror(errno));
126 _exit(EXIT_FAILURE);
127 }
128 close(pipefd[1]);
129 ret = read(pipefd[0], &status, sizeof(status));
130 //printf("pipe read returned %d, status %d\n", ret, status);
131 close(pipefd[0]);
132 if (ret == sizeof(status)) {
133 ret = status;
134 } else {
135 kill(pid, SIGKILL);
136 if (ret >= 0) {
137 ret = NO_INIT;
138 }
139 }
140 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700141 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800142 return ret;
143 }
144 return pid;
145}
146
147class BinderLibTestEnv : public ::testing::Environment {
148 public:
149 BinderLibTestEnv() {}
150 sp<IBinder> getServer(void) {
151 return m_server;
152 }
153
154 private:
155 virtual void SetUp() {
156 m_serverpid = start_server_process(0);
157 //printf("m_serverpid %d\n", m_serverpid);
158 ASSERT_GT(m_serverpid, 0);
159
160 sp<IServiceManager> sm = defaultServiceManager();
161 //printf("%s: pid %d, get service\n", __func__, m_pid);
162 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700163 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800164 //printf("%s: pid %d, get service done\n", __func__, m_pid);
165 }
166 virtual void TearDown() {
167 status_t ret;
168 Parcel data, reply;
169 int exitStatus;
170 pid_t pid;
171
172 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700173 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800174 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
175 EXPECT_EQ(0, ret);
176 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
177 EXPECT_EQ(0, ret);
178 }
179 if (m_serverpid > 0) {
180 //printf("wait for %d\n", m_pids[i]);
181 pid = wait(&exitStatus);
182 EXPECT_EQ(m_serverpid, pid);
183 EXPECT_TRUE(WIFEXITED(exitStatus));
184 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
185 }
186 }
187
188 pid_t m_serverpid;
189 sp<IBinder> m_server;
190};
191
192class BinderLibTest : public ::testing::Test {
193 public:
194 virtual void SetUp() {
195 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000196 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800197 }
198 virtual void TearDown() {
199 }
200 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200201 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800202 {
203 int ret;
204 int32_t id;
205 Parcel data, reply;
206 sp<IBinder> binder;
207
Martijn Coenen45b07b42017-08-09 12:07:45 +0200208 ret = m_server->transact(code, data, &reply);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 EXPECT_EQ(NO_ERROR, ret);
210
Yi Kong91635562018-06-07 14:38:36 -0700211 EXPECT_FALSE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800212 binder = reply.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -0700213 EXPECT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800214 ret = reply.readInt32(&id);
215 EXPECT_EQ(NO_ERROR, ret);
216 if (idPtr)
217 *idPtr = id;
218 return binder;
219 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200220
Yi Kong91635562018-06-07 14:38:36 -0700221 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200222 {
223 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
224 }
225
Yi Kong91635562018-06-07 14:38:36 -0700226 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200227 {
228 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
229 }
230
Riley Andrews06b01ad2014-12-18 12:10:08 -0800231 void waitForReadData(int fd, int timeout_ms) {
232 int ret;
233 pollfd pfd = pollfd();
234
235 pfd.fd = fd;
236 pfd.events = POLLIN;
237 ret = poll(&pfd, 1, timeout_ms);
238 EXPECT_EQ(1, ret);
239 }
240
241 sp<IBinder> m_server;
242};
243
244class BinderLibTestBundle : public Parcel
245{
246 public:
247 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800248 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800249 int32_t mark;
250 int32_t bundleLen;
251 size_t pos;
252
253 if (source->readInt32(&mark))
254 return;
255 if (mark != MARK_START)
256 return;
257 if (source->readInt32(&bundleLen))
258 return;
259 pos = source->dataPosition();
260 if (Parcel::appendFrom(source, pos, bundleLen))
261 return;
262 source->setDataPosition(pos + bundleLen);
263 if (source->readInt32(&mark))
264 return;
265 if (mark != MARK_END)
266 return;
267 m_isValid = true;
268 setDataPosition(0);
269 }
270 void appendTo(Parcel *dest) {
271 dest->writeInt32(MARK_START);
272 dest->writeInt32(dataSize());
273 dest->appendFrom(this, 0, dataSize());
274 dest->writeInt32(MARK_END);
275 };
276 bool isValid(void) {
277 return m_isValid;
278 }
279 private:
280 enum {
281 MARK_START = B_PACK_CHARS('B','T','B','S'),
282 MARK_END = B_PACK_CHARS('B','T','B','E'),
283 };
284 bool m_isValid;
285};
286
287class BinderLibTestEvent
288{
289 public:
290 BinderLibTestEvent(void)
291 : m_eventTriggered(false)
292 {
Yi Kong91635562018-06-07 14:38:36 -0700293 pthread_mutex_init(&m_waitMutex, nullptr);
294 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800295 }
296 int waitEvent(int timeout_s)
297 {
298 int ret;
299 pthread_mutex_lock(&m_waitMutex);
300 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800301 struct timespec ts;
302 clock_gettime(CLOCK_REALTIME, &ts);
303 ts.tv_sec += timeout_s;
304 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800305 }
306 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
307 pthread_mutex_unlock(&m_waitMutex);
308 return ret;
309 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200310 pthread_t getTriggeringThread()
311 {
312 return m_triggeringThread;
313 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800314 protected:
315 void triggerEvent(void) {
316 pthread_mutex_lock(&m_waitMutex);
317 pthread_cond_signal(&m_waitCond);
318 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200319 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800320 pthread_mutex_unlock(&m_waitMutex);
321 };
322 private:
323 pthread_mutex_t m_waitMutex;
324 pthread_cond_t m_waitCond;
325 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200326 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800327};
328
329class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
330{
331 public:
332 BinderLibTestCallBack()
333 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700334 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800335 {
336 }
337 status_t getResult(void)
338 {
339 return m_result;
340 }
341
342 private:
343 virtual status_t onTransact(uint32_t code,
344 const Parcel& data, Parcel* reply,
345 uint32_t flags = 0)
346 {
347 (void)reply;
348 (void)flags;
349 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200350 case BINDER_LIB_TEST_CALL_BACK: {
351 status_t status = data.readInt32(&m_result);
352 if (status != NO_ERROR) {
353 m_result = status;
354 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800355 triggerEvent();
356 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200357 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700358 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
359 sp<IBinder> server;
360 int ret;
361 const uint8_t *buf = data.data();
362 size_t size = data.dataSize();
363 if (m_prev_end) {
364 /* 64-bit kernel needs at most 8 bytes to align buffer end */
365 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
366 } else {
367 EXPECT_TRUE(IsPageAligned((void *)buf));
368 }
369
370 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
371
372 if (size > 0) {
373 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
374 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
375 data, reply);
376 EXPECT_EQ(NO_ERROR, ret);
377 }
378 return NO_ERROR;
379 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800380 default:
381 return UNKNOWN_TRANSACTION;
382 }
383 }
384
385 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700386 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800387};
388
389class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
390{
391 private:
392 virtual void binderDied(const wp<IBinder>& who) {
393 (void)who;
394 triggerEvent();
395 };
396};
397
398TEST_F(BinderLibTest, NopTransaction) {
399 status_t ret;
400 Parcel data, reply;
401 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
402 EXPECT_EQ(NO_ERROR, ret);
403}
404
Steven Morelandf183fdd2020-10-27 00:12:12 +0000405TEST_F(BinderLibTest, NopTransactionClear) {
406 status_t ret;
407 Parcel data, reply;
408 // make sure it accepts the transaction flag
409 ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF);
410 EXPECT_EQ(NO_ERROR, ret);
411}
412
Marco Ballesio7ee17572020-09-08 10:30:03 -0700413TEST_F(BinderLibTest, Freeze) {
414 status_t ret;
415 Parcel data, reply, replypid;
416 std::ifstream freezer_file("/sys/fs/cgroup/freezer/cgroup.freeze");
417
418 //Pass test on devices where the freezer is not supported
419 if (freezer_file.fail()) {
420 GTEST_SKIP();
421 return;
422 }
423
424 std::string freezer_enabled;
425 std::getline(freezer_file, freezer_enabled);
426
427 //Pass test on devices where the freezer is disabled
428 if (freezer_enabled != "1") {
429 GTEST_SKIP();
430 return;
431 }
432
433 ret = m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid);
434 int32_t pid = replypid.readInt32();
435 EXPECT_EQ(NO_ERROR, ret);
436 for (int i = 0; i < 10; i++) {
437 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
438 }
439 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
440 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, 1, 0));
441 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 1, 1000));
442 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700443
444 bool sync_received, async_received;
445
446 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
447 &async_received));
448
449 EXPECT_EQ(sync_received, 1);
450 EXPECT_EQ(async_received, 0);
451
Marco Ballesio7ee17572020-09-08 10:30:03 -0700452 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, 0, 0));
453 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
454}
455
Riley Andrews06b01ad2014-12-18 12:10:08 -0800456TEST_F(BinderLibTest, SetError) {
457 int32_t testValue[] = { 0, -123, 123 };
458 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
459 status_t ret;
460 Parcel data, reply;
461 data.writeInt32(testValue[i]);
462 ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
463 EXPECT_EQ(testValue[i], ret);
464 }
465}
466
467TEST_F(BinderLibTest, GetId) {
468 status_t ret;
469 int32_t id;
470 Parcel data, reply;
471 ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
472 EXPECT_EQ(NO_ERROR, ret);
473 ret = reply.readInt32(&id);
474 EXPECT_EQ(NO_ERROR, ret);
475 EXPECT_EQ(0, id);
476}
477
478TEST_F(BinderLibTest, PtrSize) {
479 status_t ret;
480 int32_t ptrsize;
481 Parcel data, reply;
482 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700483 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800484 ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
485 EXPECT_EQ(NO_ERROR, ret);
486 ret = reply.readInt32(&ptrsize);
487 EXPECT_EQ(NO_ERROR, ret);
488 RecordProperty("TestPtrSize", sizeof(void *));
489 RecordProperty("ServerPtrSize", sizeof(void *));
490}
491
492TEST_F(BinderLibTest, IndirectGetId2)
493{
494 status_t ret;
495 int32_t id;
496 int32_t count;
497 Parcel data, reply;
498 int32_t serverId[3];
499
500 data.writeInt32(ARRAY_SIZE(serverId));
501 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
502 sp<IBinder> server;
503 BinderLibTestBundle datai;
504
505 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700506 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800507 data.writeStrongBinder(server);
508 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
509 datai.appendTo(&data);
510 }
511
512 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
513 ASSERT_EQ(NO_ERROR, ret);
514
515 ret = reply.readInt32(&id);
516 ASSERT_EQ(NO_ERROR, ret);
517 EXPECT_EQ(0, id);
518
519 ret = reply.readInt32(&count);
520 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700521 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800522
523 for (size_t i = 0; i < (size_t)count; i++) {
524 BinderLibTestBundle replyi(&reply);
525 EXPECT_TRUE(replyi.isValid());
526 ret = replyi.readInt32(&id);
527 EXPECT_EQ(NO_ERROR, ret);
528 EXPECT_EQ(serverId[i], id);
529 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
530 }
531
532 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
533}
534
535TEST_F(BinderLibTest, IndirectGetId3)
536{
537 status_t ret;
538 int32_t id;
539 int32_t count;
540 Parcel data, reply;
541 int32_t serverId[3];
542
543 data.writeInt32(ARRAY_SIZE(serverId));
544 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
545 sp<IBinder> server;
546 BinderLibTestBundle datai;
547 BinderLibTestBundle datai2;
548
549 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700550 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800551 data.writeStrongBinder(server);
552 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
553
554 datai.writeInt32(1);
555 datai.writeStrongBinder(m_server);
556 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
557 datai2.appendTo(&datai);
558
559 datai.appendTo(&data);
560 }
561
562 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
563 ASSERT_EQ(NO_ERROR, ret);
564
565 ret = reply.readInt32(&id);
566 ASSERT_EQ(NO_ERROR, ret);
567 EXPECT_EQ(0, id);
568
569 ret = reply.readInt32(&count);
570 ASSERT_EQ(NO_ERROR, ret);
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700571 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800572
573 for (size_t i = 0; i < (size_t)count; i++) {
574 int32_t counti;
575
576 BinderLibTestBundle replyi(&reply);
577 EXPECT_TRUE(replyi.isValid());
578 ret = replyi.readInt32(&id);
579 EXPECT_EQ(NO_ERROR, ret);
580 EXPECT_EQ(serverId[i], id);
581
582 ret = replyi.readInt32(&counti);
583 ASSERT_EQ(NO_ERROR, ret);
584 EXPECT_EQ(1, counti);
585
586 BinderLibTestBundle replyi2(&replyi);
587 EXPECT_TRUE(replyi2.isValid());
588 ret = replyi2.readInt32(&id);
589 EXPECT_EQ(NO_ERROR, ret);
590 EXPECT_EQ(0, id);
591 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
592
593 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
594 }
595
596 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
597}
598
599TEST_F(BinderLibTest, CallBack)
600{
601 status_t ret;
602 Parcel data, reply;
603 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
604 data.writeStrongBinder(callBack);
605 ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
606 EXPECT_EQ(NO_ERROR, ret);
607 ret = callBack->waitEvent(5);
608 EXPECT_EQ(NO_ERROR, ret);
609 ret = callBack->getResult();
610 EXPECT_EQ(NO_ERROR, ret);
611}
612
613TEST_F(BinderLibTest, AddServer)
614{
615 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700616 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800617}
618
Riley Andrews06b01ad2014-12-18 12:10:08 -0800619TEST_F(BinderLibTest, DeathNotificationStrongRef)
620{
621 status_t ret;
622 sp<IBinder> sbinder;
623
624 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
625
626 {
627 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700628 ASSERT_TRUE(binder != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800629 ret = binder->linkToDeath(testDeathRecipient);
630 EXPECT_EQ(NO_ERROR, ret);
631 sbinder = binder;
632 }
633 {
634 Parcel data, reply;
635 ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
636 EXPECT_EQ(0, ret);
637 }
638 IPCThreadState::self()->flushCommands();
639 ret = testDeathRecipient->waitEvent(5);
640 EXPECT_EQ(NO_ERROR, ret);
641 ret = sbinder->unlinkToDeath(testDeathRecipient);
642 EXPECT_EQ(DEAD_OBJECT, ret);
643}
644
645TEST_F(BinderLibTest, DeathNotificationMultiple)
646{
647 status_t ret;
648 const int clientcount = 2;
649 sp<IBinder> target;
650 sp<IBinder> linkedclient[clientcount];
651 sp<BinderLibTestCallBack> callBack[clientcount];
652 sp<IBinder> passiveclient[clientcount];
653
654 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700655 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800656 for (int i = 0; i < clientcount; i++) {
657 {
658 Parcel data, reply;
659
660 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700661 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800662 callBack[i] = new BinderLibTestCallBack();
663 data.writeStrongBinder(target);
664 data.writeStrongBinder(callBack[i]);
665 ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
666 EXPECT_EQ(NO_ERROR, ret);
667 }
668 {
669 Parcel data, reply;
670
671 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700672 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800673 data.writeStrongBinder(target);
674 ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
675 EXPECT_EQ(NO_ERROR, ret);
676 }
677 }
678 {
679 Parcel data, reply;
680 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
681 EXPECT_EQ(0, ret);
682 }
683
684 for (int i = 0; i < clientcount; i++) {
685 ret = callBack[i]->waitEvent(5);
686 EXPECT_EQ(NO_ERROR, ret);
687 ret = callBack[i]->getResult();
688 EXPECT_EQ(NO_ERROR, ret);
689 }
690}
691
Martijn Coenenf7100e42017-07-31 12:14:09 +0200692TEST_F(BinderLibTest, DeathNotificationThread)
693{
694 status_t ret;
695 sp<BinderLibTestCallBack> callback;
696 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700697 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200698 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700699 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200700
701 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
702
703 ret = target->linkToDeath(testDeathRecipient);
704 EXPECT_EQ(NO_ERROR, ret);
705
706 {
707 Parcel data, reply;
708 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
709 EXPECT_EQ(0, ret);
710 }
711
712 /* Make sure it's dead */
713 testDeathRecipient->waitEvent(5);
714
715 /* Now, pass the ref to another process and ask that process to
716 * call linkToDeath() on it, and wait for a response. This tests
717 * two things:
718 * 1) You still get death notifications when calling linkToDeath()
719 * on a ref that is already dead when it was passed to you.
720 * 2) That death notifications are not directly pushed to the thread
721 * registering them, but to the threadpool (proc workqueue) instead.
722 *
723 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
724 * is blocked on a condition variable waiting for the death notification to be
725 * called; therefore, that thread is not available for handling proc work.
726 * So, if the death notification was pushed to the thread workqueue, the callback
727 * would never be called, and the test would timeout and fail.
728 *
729 * Note that we can't do this part of the test from this thread itself, because
730 * the binder driver would only push death notifications to the thread if
731 * it is a looper thread, which this thread is not.
732 *
733 * See b/23525545 for details.
734 */
735 {
736 Parcel data, reply;
737
738 callback = new BinderLibTestCallBack();
739 data.writeStrongBinder(target);
740 data.writeStrongBinder(callback);
741 ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
742 EXPECT_EQ(NO_ERROR, ret);
743 }
744
745 ret = callback->waitEvent(5);
746 EXPECT_EQ(NO_ERROR, ret);
747 ret = callback->getResult();
748 EXPECT_EQ(NO_ERROR, ret);
749}
750
Riley Andrews06b01ad2014-12-18 12:10:08 -0800751TEST_F(BinderLibTest, PassFile) {
752 int ret;
753 int pipefd[2];
754 uint8_t buf[1] = { 0 };
755 uint8_t write_value = 123;
756
757 ret = pipe2(pipefd, O_NONBLOCK);
758 ASSERT_EQ(0, ret);
759
760 {
761 Parcel data, reply;
762 uint8_t writebuf[1] = { write_value };
763
764 ret = data.writeFileDescriptor(pipefd[1], true);
765 EXPECT_EQ(NO_ERROR, ret);
766
767 ret = data.writeInt32(sizeof(writebuf));
768 EXPECT_EQ(NO_ERROR, ret);
769
770 ret = data.write(writebuf, sizeof(writebuf));
771 EXPECT_EQ(NO_ERROR, ret);
772
773 ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
774 EXPECT_EQ(NO_ERROR, ret);
775 }
776
777 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700778 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800779 EXPECT_EQ(write_value, buf[0]);
780
781 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
782
783 ret = read(pipefd[0], buf, sizeof(buf));
784 EXPECT_EQ(0, ret);
785
786 close(pipefd[0]);
787}
788
Ryo Hashimotobf551892018-05-31 16:58:35 +0900789TEST_F(BinderLibTest, PassParcelFileDescriptor) {
790 const int datasize = 123;
791 std::vector<uint8_t> writebuf(datasize);
792 for (size_t i = 0; i < writebuf.size(); ++i) {
793 writebuf[i] = i;
794 }
795
796 android::base::unique_fd read_end, write_end;
797 {
798 int pipefd[2];
799 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
800 read_end.reset(pipefd[0]);
801 write_end.reset(pipefd[1]);
802 }
803 {
804 Parcel data;
805 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
806 write_end.reset();
807 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
808 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
809
810 Parcel reply;
811 EXPECT_EQ(NO_ERROR,
812 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
813 &reply));
814 }
815 std::vector<uint8_t> readbuf(datasize);
816 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
817 EXPECT_EQ(writebuf, readbuf);
818
819 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
820
821 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
822}
823
Riley Andrews06b01ad2014-12-18 12:10:08 -0800824TEST_F(BinderLibTest, PromoteLocal) {
825 sp<IBinder> strong = new BBinder();
826 wp<IBinder> weak = strong;
827 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700828 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800829 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700830 strong = nullptr;
831 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800832 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700833 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800834}
835
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700836TEST_F(BinderLibTest, LocalGetExtension) {
837 sp<BBinder> binder = new BBinder();
838 sp<IBinder> ext = new BBinder();
839 binder->setExtension(ext);
840 EXPECT_EQ(ext, binder->getExtension());
841}
842
843TEST_F(BinderLibTest, RemoteGetExtension) {
844 sp<IBinder> server = addServer();
845 ASSERT_TRUE(server != nullptr);
846
847 sp<IBinder> extension;
848 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
849 ASSERT_NE(nullptr, extension.get());
850
851 EXPECT_EQ(NO_ERROR, extension->pingBinder());
852}
853
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700854TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
855 status_t ret;
856 Parcel data, reply;
857
858 ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
859 EXPECT_EQ(NO_ERROR, ret);
860
861 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700862 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800863 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
864 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
865 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
866 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700867}
868
Connor O'Brien52be2c92016-09-20 14:18:08 -0700869TEST_F(BinderLibTest, FreedBinder) {
870 status_t ret;
871
872 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700873 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700874
875 __u32 freedHandle;
876 wp<IBinder> keepFreedBinder;
877 {
878 Parcel data, reply;
Connor O'Brien52be2c92016-09-20 14:18:08 -0700879 ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
880 ASSERT_EQ(NO_ERROR, ret);
881 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
882 freedHandle = freed->handle;
883 /* Add a weak ref to the freed binder so the driver does not
884 * delete its reference to it - otherwise the transaction
885 * fails regardless of whether the driver is fixed.
886 */
Steven Morelande171d622019-07-17 16:06:01 -0700887 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700888 }
Steven Morelande171d622019-07-17 16:06:01 -0700889 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700890 {
891 Parcel data, reply;
892 data.writeStrongBinder(server);
893 /* Replace original handle with handle to the freed binder */
894 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
895 __u32 oldHandle = strong->handle;
896 strong->handle = freedHandle;
897 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
898 /* Returns DEAD_OBJECT (-32) if target crashes and
899 * FAILED_TRANSACTION if the driver rejects the invalid
900 * object.
901 */
902 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
903 /* Restore original handle so parcel destructor does not use
904 * the wrong handle.
905 */
906 strong->handle = oldHandle;
907 }
908}
909
Sherry Yang336cdd32017-07-24 14:12:27 -0700910TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
911 status_t ret;
912 Parcel data, reply;
913 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
914 for (int i = 0; i < 2; i++) {
915 BinderLibTestBundle datai;
916 datai.appendFrom(&data, 0, data.dataSize());
917
918 data.freeData();
919 data.writeInt32(1);
920 data.writeStrongBinder(callBack);
921 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
922
923 datai.appendTo(&data);
924 }
925 ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
926 EXPECT_EQ(NO_ERROR, ret);
927}
928
Martijn Coenen45b07b42017-08-09 12:07:45 +0200929TEST_F(BinderLibTest, OnewayQueueing)
930{
931 status_t ret;
932 Parcel data, data2;
933
934 sp<IBinder> pollServer = addPollServer();
935
936 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
937 data.writeStrongBinder(callBack);
938 data.writeInt32(500000); // delay in us before calling back
939
940 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
941 data2.writeStrongBinder(callBack2);
942 data2.writeInt32(0); // delay in us
943
Yi Kong91635562018-06-07 14:38:36 -0700944 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200945 EXPECT_EQ(NO_ERROR, ret);
946
947 // The delay ensures that this second transaction will end up on the async_todo list
948 // (for a single-threaded server)
Yi Kong91635562018-06-07 14:38:36 -0700949 ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200950 EXPECT_EQ(NO_ERROR, ret);
951
952 // The server will ensure that the two transactions are handled in the expected order;
953 // If the ordering is not as expected, an error will be returned through the callbacks.
954 ret = callBack->waitEvent(2);
955 EXPECT_EQ(NO_ERROR, ret);
956 ret = callBack->getResult();
957 EXPECT_EQ(NO_ERROR, ret);
958
959 ret = callBack2->waitEvent(2);
960 EXPECT_EQ(NO_ERROR, ret);
961 ret = callBack2->getResult();
962 EXPECT_EQ(NO_ERROR, ret);
963}
964
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100965TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
966{
967 status_t ret;
968 Parcel data, reply;
969 data.writeInterfaceToken(binderLibTestServiceName);
970 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
971 EXPECT_EQ(-1, reply.readInt32());
972 EXPECT_EQ(NO_ERROR, ret);
973}
974
975TEST_F(BinderLibTest, WorkSourceSet)
976{
977 status_t ret;
978 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +0000979 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +0000980 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100981 data.writeInterfaceToken(binderLibTestServiceName);
982 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
983 EXPECT_EQ(100, reply.readInt32());
984 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +0000985 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
986 EXPECT_EQ(NO_ERROR, ret);
987}
988
989TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
990{
991 status_t ret;
992 Parcel data, reply;
993
994 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
995 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
996
997 data.writeInterfaceToken(binderLibTestServiceName);
998 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
999 EXPECT_EQ(-1, reply.readInt32());
1000 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001001 EXPECT_EQ(NO_ERROR, ret);
1002}
1003
1004TEST_F(BinderLibTest, WorkSourceCleared)
1005{
1006 status_t ret;
1007 Parcel data, reply;
1008
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001009 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001010 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1011 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001012 data.writeInterfaceToken(binderLibTestServiceName);
1013 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1014
1015 EXPECT_EQ(-1, reply.readInt32());
1016 EXPECT_EQ(100, previousWorkSource);
1017 EXPECT_EQ(NO_ERROR, ret);
1018}
1019
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001020TEST_F(BinderLibTest, WorkSourceRestored)
1021{
1022 status_t ret;
1023 Parcel data, reply;
1024
1025 IPCThreadState::self()->setCallingWorkSourceUid(100);
1026 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1027 IPCThreadState::self()->restoreCallingWorkSource(token);
1028
1029 data.writeInterfaceToken(binderLibTestServiceName);
1030 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1031
1032 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001033 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001034 EXPECT_EQ(NO_ERROR, ret);
1035}
1036
Olivier Gaillard91a04802018-11-14 17:32:41 +00001037TEST_F(BinderLibTest, PropagateFlagSet)
1038{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001039 IPCThreadState::self()->clearPropagateWorkSource();
1040 IPCThreadState::self()->setCallingWorkSourceUid(100);
1041 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1042}
1043
1044TEST_F(BinderLibTest, PropagateFlagCleared)
1045{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001046 IPCThreadState::self()->setCallingWorkSourceUid(100);
1047 IPCThreadState::self()->clearPropagateWorkSource();
1048 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1049}
1050
1051TEST_F(BinderLibTest, PropagateFlagRestored)
1052{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001053 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1054 IPCThreadState::self()->restoreCallingWorkSource(token);
1055
1056 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1057}
1058
1059TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1060{
1061 IPCThreadState::self()->setCallingWorkSourceUid(100);
1062
1063 Parcel data, reply;
1064 status_t ret;
1065 data.writeInterfaceToken(binderLibTestServiceName);
1066 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1067
1068 Parcel data2, reply2;
1069 status_t ret2;
1070 data2.writeInterfaceToken(binderLibTestServiceName);
1071 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1072 EXPECT_EQ(100, reply2.readInt32());
1073 EXPECT_EQ(NO_ERROR, ret2);
1074}
1075
Steven Morelandbf1915b2020-07-16 22:43:02 +00001076TEST_F(BinderLibTest, SchedPolicySet) {
1077 sp<IBinder> server = addServer();
1078 ASSERT_TRUE(server != nullptr);
1079
1080 Parcel data, reply;
1081 status_t ret = server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply);
1082 EXPECT_EQ(NO_ERROR, ret);
1083
1084 int policy = reply.readInt32();
1085 int priority = reply.readInt32();
1086
1087 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1088 EXPECT_EQ(kSchedPriority, priority);
1089}
1090
1091
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001092TEST_F(BinderLibTest, VectorSent) {
1093 Parcel data, reply;
1094 sp<IBinder> server = addServer();
1095 ASSERT_TRUE(server != nullptr);
1096
1097 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1098 data.writeUint64Vector(testValue);
1099
1100 status_t ret = server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply);
1101 EXPECT_EQ(NO_ERROR, ret);
1102 std::vector<uint64_t> readValue;
1103 ret = reply.readUint64Vector(&readValue);
1104 EXPECT_EQ(readValue, testValue);
1105}
1106
Martijn Coenen82c75312019-07-24 15:18:30 +02001107TEST_F(BinderLibTest, BufRejected) {
1108 Parcel data, reply;
1109 uint32_t buf;
1110 sp<IBinder> server = addServer();
1111 ASSERT_TRUE(server != nullptr);
1112
1113 binder_buffer_object obj {
1114 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001115 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001116 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1117 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001118 };
1119 data.setDataCapacity(1024);
1120 // Write a bogus object at offset 0 to get an entry in the offset table
1121 data.writeFileDescriptor(0);
1122 EXPECT_EQ(data.objectsCount(), 1);
1123 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1124 // And now, overwrite it with the buffer object
1125 memcpy(parcelData, &obj, sizeof(obj));
1126 data.setDataSize(sizeof(obj));
1127
1128 status_t ret = server->transact(BINDER_LIB_TEST_REJECT_BUF, data, &reply);
1129 // Either the kernel should reject this transaction (if it's correct), but
1130 // if it's not, the server implementation should return an error if it
1131 // finds an object in the received Parcel.
1132 EXPECT_NE(NO_ERROR, ret);
1133}
1134
Riley Andrews06b01ad2014-12-18 12:10:08 -08001135class BinderLibTestService : public BBinder
1136{
1137 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -08001138 explicit BinderLibTestService(int32_t id)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001139 : m_id(id)
1140 , m_nextServerId(id + 1)
1141 , m_serverStartRequested(false)
Yi Kong91635562018-06-07 14:38:36 -07001142 , m_callback(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001143 {
Yi Kong91635562018-06-07 14:38:36 -07001144 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1145 pthread_cond_init(&m_serverWaitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001146 }
1147 ~BinderLibTestService()
1148 {
1149 exit(EXIT_SUCCESS);
1150 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001151
1152 void processPendingCall() {
Yi Kong91635562018-06-07 14:38:36 -07001153 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001154 Parcel data;
1155 data.writeInt32(NO_ERROR);
1156 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
Yi Kong91635562018-06-07 14:38:36 -07001157 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001158 }
1159 }
1160
Riley Andrews06b01ad2014-12-18 12:10:08 -08001161 virtual status_t onTransact(uint32_t code,
1162 const Parcel& data, Parcel* reply,
1163 uint32_t flags = 0) {
1164 //printf("%s: code %d\n", __func__, code);
1165 (void)flags;
1166
1167 if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
1168 return PERMISSION_DENIED;
1169 }
1170 switch (code) {
1171 case BINDER_LIB_TEST_REGISTER_SERVER: {
1172 int32_t id;
1173 sp<IBinder> binder;
1174 id = data.readInt32();
1175 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001176 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001177 return BAD_VALUE;
1178 }
1179
1180 if (m_id != 0)
1181 return INVALID_OPERATION;
1182
1183 pthread_mutex_lock(&m_serverWaitMutex);
1184 if (m_serverStartRequested) {
1185 m_serverStartRequested = false;
1186 m_serverStarted = binder;
1187 pthread_cond_signal(&m_serverWaitCond);
1188 }
1189 pthread_mutex_unlock(&m_serverWaitMutex);
1190 return NO_ERROR;
1191 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001192 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001193 case BINDER_LIB_TEST_ADD_SERVER: {
1194 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001195 int serverid;
1196
1197 if (m_id != 0) {
1198 return INVALID_OPERATION;
1199 }
1200 pthread_mutex_lock(&m_serverWaitMutex);
1201 if (m_serverStartRequested) {
1202 ret = -EBUSY;
1203 } else {
1204 serverid = m_nextServerId++;
1205 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001206 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001207
1208 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001209 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001210 pthread_mutex_lock(&m_serverWaitMutex);
1211 }
1212 if (ret > 0) {
1213 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001214 struct timespec ts;
1215 clock_gettime(CLOCK_REALTIME, &ts);
1216 ts.tv_sec += 5;
1217 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001218 }
1219 if (m_serverStartRequested) {
1220 m_serverStartRequested = false;
1221 ret = -ETIMEDOUT;
1222 } else {
1223 reply->writeStrongBinder(m_serverStarted);
1224 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001225 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001226 ret = NO_ERROR;
1227 }
1228 } else if (ret >= 0) {
1229 m_serverStartRequested = false;
1230 ret = UNKNOWN_ERROR;
1231 }
1232 pthread_mutex_unlock(&m_serverWaitMutex);
1233 return ret;
1234 }
Marco Ballesio7ee17572020-09-08 10:30:03 -07001235 case BINDER_LIB_TEST_GETPID:
1236 reply->writeInt32(getpid());
1237 return NO_ERROR;
1238 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1239 usleep(5000);
1240 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001241 case BINDER_LIB_TEST_NOP_TRANSACTION:
1242 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001243 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1244 // Note: this transaction is only designed for use with a
1245 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001246 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001247 // A callback was already pending; this means that
1248 // we received a second call while still processing
1249 // the first one. Fail the test.
1250 sp<IBinder> callback = data.readStrongBinder();
1251 Parcel data2;
1252 data2.writeInt32(UNKNOWN_ERROR);
1253
Yi Kong91635562018-06-07 14:38:36 -07001254 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001255 } else {
1256 m_callback = data.readStrongBinder();
1257 int32_t delayUs = data.readInt32();
1258 /*
1259 * It's necessary that we sleep here, so the next
1260 * transaction the caller makes will be queued to
1261 * the async queue.
1262 */
1263 usleep(delayUs);
1264
1265 /*
1266 * Now when we return, libbinder will tell the kernel
1267 * we are done with this transaction, and the kernel
1268 * can move the queued transaction to either the
1269 * thread todo worklist (for kernels without the fix),
1270 * or the proc todo worklist. In case of the former,
1271 * the next outbound call will pick up the pending
1272 * transaction, which leads to undesired reentrant
1273 * behavior. This is caught in the if() branch above.
1274 */
1275 }
1276
1277 return NO_ERROR;
1278 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001279 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1280 Parcel data2, reply2;
1281 sp<IBinder> binder;
1282 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001283 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001284 return BAD_VALUE;
1285 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001286 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001287 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1288 return NO_ERROR;
1289 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001290 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1291 reply->writeStrongBinder(this);
1292 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001293 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1294 reply->writeInt32(m_id);
1295 return NO_ERROR;
1296 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1297 int32_t count;
1298 uint32_t indirect_code;
1299 sp<IBinder> binder;
1300
1301 count = data.readInt32();
1302 reply->writeInt32(m_id);
1303 reply->writeInt32(count);
1304 for (int i = 0; i < count; i++) {
1305 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001306 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001307 return BAD_VALUE;
1308 }
1309 indirect_code = data.readInt32();
1310 BinderLibTestBundle data2(&data);
1311 if (!data2.isValid()) {
1312 return BAD_VALUE;
1313 }
1314 BinderLibTestBundle reply2;
1315 binder->transact(indirect_code, data2, &reply2);
1316 reply2.appendTo(reply);
1317 }
1318 return NO_ERROR;
1319 }
1320 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1321 reply->setError(data.readInt32());
1322 return NO_ERROR;
1323 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1324 reply->writeInt32(sizeof(void *));
1325 return NO_ERROR;
1326 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1327 return NO_ERROR;
1328 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1329 m_strongRef = data.readStrongBinder();
1330 return NO_ERROR;
1331 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1332 int ret;
1333 Parcel data2, reply2;
1334 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1335 sp<IBinder> target;
1336 sp<IBinder> callback;
1337
1338 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001339 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001340 return BAD_VALUE;
1341 }
1342 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001343 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001344 return BAD_VALUE;
1345 }
1346 ret = target->linkToDeath(testDeathRecipient);
1347 if (ret == NO_ERROR)
1348 ret = testDeathRecipient->waitEvent(5);
1349 data2.writeInt32(ret);
1350 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1351 return NO_ERROR;
1352 }
1353 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1354 int ret;
1355 int32_t size;
1356 const void *buf;
1357 int fd;
1358
1359 fd = data.readFileDescriptor();
1360 if (fd < 0) {
1361 return BAD_VALUE;
1362 }
1363 ret = data.readInt32(&size);
1364 if (ret != NO_ERROR) {
1365 return ret;
1366 }
1367 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001368 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001369 return BAD_VALUE;
1370 }
1371 ret = write(fd, buf, size);
1372 if (ret != size)
1373 return UNKNOWN_ERROR;
1374 return NO_ERROR;
1375 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001376 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1377 int ret;
1378 int32_t size;
1379 const void *buf;
1380 android::base::unique_fd fd;
1381
1382 ret = data.readUniqueParcelFileDescriptor(&fd);
1383 if (ret != NO_ERROR) {
1384 return ret;
1385 }
1386 ret = data.readInt32(&size);
1387 if (ret != NO_ERROR) {
1388 return ret;
1389 }
1390 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001391 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001392 return BAD_VALUE;
1393 }
1394 ret = write(fd.get(), buf, size);
1395 if (ret != size) return UNKNOWN_ERROR;
1396 return NO_ERROR;
1397 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001398 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1399 alarm(10);
1400 return NO_ERROR;
1401 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001402 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001403 ;
1404 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001405 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001406 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001407 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001408 return NO_ERROR;
1409 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001410 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1411 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001412 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001413 return NO_ERROR;
1414 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001415 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1416 int policy = 0;
1417 sched_param param;
1418 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1419 return UNKNOWN_ERROR;
1420 }
1421 reply->writeInt32(policy);
1422 reply->writeInt32(param.sched_priority);
1423 return NO_ERROR;
1424 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001425 case BINDER_LIB_TEST_ECHO_VECTOR: {
1426 std::vector<uint64_t> vector;
1427 auto err = data.readUint64Vector(&vector);
1428 if (err != NO_ERROR)
1429 return err;
1430 reply->writeUint64Vector(vector);
1431 return NO_ERROR;
1432 }
Martijn Coenen82c75312019-07-24 15:18:30 +02001433 case BINDER_LIB_TEST_REJECT_BUF: {
1434 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1435 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001436 default:
1437 return UNKNOWN_TRANSACTION;
1438 };
1439 }
1440 private:
1441 int32_t m_id;
1442 int32_t m_nextServerId;
1443 pthread_mutex_t m_serverWaitMutex;
1444 pthread_cond_t m_serverWaitCond;
1445 bool m_serverStartRequested;
1446 sp<IBinder> m_serverStarted;
1447 sp<IBinder> m_strongRef;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001448 sp<IBinder> m_callback;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001449};
1450
Martijn Coenen45b07b42017-08-09 12:07:45 +02001451int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001452{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001453 binderLibTestServiceName += String16(binderserversuffix);
1454
Riley Andrews06b01ad2014-12-18 12:10:08 -08001455 status_t ret;
1456 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001457 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001458 {
1459 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001460
Steven Morelandbf1915b2020-07-16 22:43:02 +00001461 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1462
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001463 /*
1464 * Normally would also contain functionality as well, but we are only
1465 * testing the extension mechanism.
1466 */
1467 testService->setExtension(new BBinder());
1468
Martijn Coenen82c75312019-07-24 15:18:30 +02001469 // Required for test "BufRejected'
1470 testService->setRequestingSid(true);
1471
Martijn Coenen45b07b42017-08-09 12:07:45 +02001472 /*
1473 * We need this below, but can't hold a sp<> because it prevents the
1474 * node from being cleaned up automatically. It's safe in this case
1475 * because of how the tests are written.
1476 */
1477 testServicePtr = testService.get();
1478
Riley Andrews06b01ad2014-12-18 12:10:08 -08001479 if (index == 0) {
1480 ret = sm->addService(binderLibTestServiceName, testService);
1481 } else {
1482 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1483 Parcel data, reply;
1484 data.writeInt32(index);
1485 data.writeStrongBinder(testService);
1486
1487 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1488 }
1489 }
1490 write(readypipefd, &ret, sizeof(ret));
1491 close(readypipefd);
1492 //printf("%s: ret %d\n", __func__, ret);
1493 if (ret)
1494 return 1;
1495 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001496 if (usePoll) {
1497 int fd;
1498 struct epoll_event ev;
1499 int epoll_fd;
1500 IPCThreadState::self()->setupPolling(&fd);
1501 if (fd < 0) {
1502 return 1;
1503 }
1504 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1505
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001506 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001507 if (epoll_fd == -1) {
1508 return 1;
1509 }
1510
1511 ev.events = EPOLLIN;
1512 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1513 return 1;
1514 }
1515
1516 while (1) {
1517 /*
1518 * We simulate a single-threaded process using the binder poll
1519 * interface; besides handling binder commands, it can also
1520 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001521 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001522 *
1523 * processPendingCall() will then issue that transaction.
1524 */
1525 struct epoll_event events[1];
1526 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1527 if (numEvents < 0) {
1528 if (errno == EINTR) {
1529 continue;
1530 }
1531 return 1;
1532 }
1533 if (numEvents > 0) {
1534 IPCThreadState::self()->handlePolledCommands();
1535 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1536 testServicePtr->processPendingCall();
1537 }
1538 }
1539 } else {
1540 ProcessState::self()->startThreadPool();
1541 IPCThreadState::self()->joinThreadPool();
1542 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001543 //printf("%s: joinThreadPool returned\n", __func__);
1544 return 1; /* joinThreadPool should not return */
1545}
1546
1547int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001548 ExitIfWrongAbi();
1549
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001550 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001551 binderservername = argv[2];
1552 } else {
1553 binderservername = argv[0];
1554 }
1555
Martijn Coenen45b07b42017-08-09 12:07:45 +02001556 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1557 binderserversuffix = argv[5];
1558 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001559 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001560 binderserversuffix = new char[16];
1561 snprintf(binderserversuffix, 16, "%d", getpid());
1562 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001563
1564 ::testing::InitGoogleTest(&argc, argv);
1565 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1566 ProcessState::self()->startThreadPool();
1567 return RUN_ALL_TESTS();
1568}