blob: 57a1fda95fc806aa30d89c840fbe9663e7b850f5 [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>
Riley Andrews06b01ad2014-12-18 12:10:08 -080018#include <poll.h>
19#include <pthread.h>
20#include <stdio.h>
21#include <stdlib.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070022
23#include <chrono>
Yifan Hong8b890852021-06-10 13:44:09 -070024#include <fstream>
Steven Morelandd7088702021-01-13 00:27:00 +000025#include <thread>
Riley Andrews06b01ad2014-12-18 12:10:08 -080026
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070027#include <gmock/gmock.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080028#include <gtest/gtest.h>
29
Yifan Hong84bedeb2021-04-21 21:37:17 -070030#include <android-base/properties.h>
Yifan Hong28d6c352021-06-04 17:27:35 -070031#include <android-base/result-gmock.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070032#include <android-base/result.h>
Yifan Hong8b890852021-06-10 13:44:09 -070033#include <android-base/strings.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070034#include <android-base/unique_fd.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080035#include <binder/Binder.h>
Yifan Hong34823232021-06-07 17:23:00 -070036#include <binder/BpBinder.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080037#include <binder/IBinder.h>
38#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070040#include <binder/RpcServer.h>
41#include <binder/RpcSession.h>
Riley Andrews06b01ad2014-12-18 12:10:08 -080042
Steven Morelandcf03cf12020-12-04 02:58:40 +000043#include <linux/sched.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020044#include <sys/epoll.h>
Steven Morelandda048352020-02-19 13:25:53 -080045#include <sys/prctl.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070046#include <sys/socket.h>
47#include <sys/un.h>
Martijn Coenen45b07b42017-08-09 12:07:45 +020048
Steven Moreland6ba5a252021-05-04 22:49:00 +000049#include "../binder_module.h"
Steven Morelandf9f3de22020-05-06 17:14:39 -070050#include "binderAbiHelper.h"
51
Riley Andrews06b01ad2014-12-18 12:10:08 -080052#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
53
54using namespace android;
Yifan Hong84bedeb2021-04-21 21:37:17 -070055using namespace std::string_literals;
56using namespace std::chrono_literals;
Yifan Hong28d6c352021-06-04 17:27:35 -070057using android::base::testing::HasValue;
Yifan Hong8b890852021-06-10 13:44:09 -070058using android::base::testing::Ok;
Yifan Hong84bedeb2021-04-21 21:37:17 -070059using testing::ExplainMatchResult;
Yifan Hongbd276552022-02-28 15:28:51 -080060using testing::Matcher;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070061using testing::Not;
Yifan Hong84bedeb2021-04-21 21:37:17 -070062using testing::WithParamInterface;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -070063
64// e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message";
65MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) {
66 *result_listener << statusToString(arg);
67 return expected == arg;
68}
Riley Andrews06b01ad2014-12-18 12:10:08 -080069
Sherry Yang336cdd32017-07-24 14:12:27 -070070static ::testing::AssertionResult IsPageAligned(void *buf) {
71 if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
72 return ::testing::AssertionSuccess();
73 else
74 return ::testing::AssertionFailure() << buf << " is not page aligned";
75}
76
Riley Andrews06b01ad2014-12-18 12:10:08 -080077static testing::Environment* binder_env;
78static char *binderservername;
Connor O'Brien87c03cf2016-10-26 17:58:51 -070079static char *binderserversuffix;
Riley Andrews06b01ad2014-12-18 12:10:08 -080080static char binderserverarg[] = "--binderserver";
81
Steven Morelandbf1915b2020-07-16 22:43:02 +000082static constexpr int kSchedPolicy = SCHED_RR;
83static constexpr int kSchedPriority = 7;
Steven Morelandcf03cf12020-12-04 02:58:40 +000084static constexpr int kSchedPriorityMore = 8;
Steven Morelandbf1915b2020-07-16 22:43:02 +000085
Riley Andrews06b01ad2014-12-18 12:10:08 -080086static String16 binderLibTestServiceName = String16("test.binderLib");
87
88enum BinderLibTestTranscationCode {
89 BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
90 BINDER_LIB_TEST_REGISTER_SERVER,
91 BINDER_LIB_TEST_ADD_SERVER,
Martijn Coenen45b07b42017-08-09 12:07:45 +020092 BINDER_LIB_TEST_ADD_POLL_SERVER,
Steven Moreland35626652021-05-15 01:32:04 +000093 BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080094 BINDER_LIB_TEST_CALL_BACK,
Sherry Yang336cdd32017-07-24 14:12:27 -070095 BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
Martijn Coenen45b07b42017-08-09 12:07:45 +020096 BINDER_LIB_TEST_DELAYED_CALL_BACK,
Riley Andrews06b01ad2014-12-18 12:10:08 -080097 BINDER_LIB_TEST_NOP_CALL_BACK,
Arve Hjønnevåg70604312016-08-12 15:34:51 -070098 BINDER_LIB_TEST_GET_SELF_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -080099 BINDER_LIB_TEST_GET_ID_TRANSACTION,
100 BINDER_LIB_TEST_INDIRECT_TRANSACTION,
101 BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
102 BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
103 BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
104 BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
105 BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
Ryo Hashimotobf551892018-05-31 16:58:35 +0900106 BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800107 BINDER_LIB_TEST_EXIT_TRANSACTION,
108 BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
109 BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
Connor O'Brien52be2c92016-09-20 14:18:08 -0700110 BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100111 BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
Steven Morelandbf1915b2020-07-16 22:43:02 +0000112 BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
Marco Ballesio7ee17572020-09-08 10:30:03 -0700113 BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
114 BINDER_LIB_TEST_GETPID,
Kevin DuBois2f82d5b2018-12-05 12:56:10 -0800115 BINDER_LIB_TEST_ECHO_VECTOR,
Steven Morelandf2e0a952021-11-01 18:17:23 -0700116 BINDER_LIB_TEST_REJECT_OBJECTS,
Steven Moreland254e8ef2021-04-19 22:28:50 +0000117 BINDER_LIB_TEST_CAN_GET_SID,
Riley Andrews06b01ad2014-12-18 12:10:08 -0800118};
119
Martijn Coenen45b07b42017-08-09 12:07:45 +0200120pid_t start_server_process(int arg2, bool usePoll = false)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800121{
122 int ret;
123 pid_t pid;
124 status_t status;
125 int pipefd[2];
126 char stri[16];
127 char strpipefd1[16];
Martijn Coenen45b07b42017-08-09 12:07:45 +0200128 char usepoll[2];
Riley Andrews06b01ad2014-12-18 12:10:08 -0800129 char *childargv[] = {
130 binderservername,
131 binderserverarg,
132 stri,
133 strpipefd1,
Martijn Coenen45b07b42017-08-09 12:07:45 +0200134 usepoll,
Connor O'Brien87c03cf2016-10-26 17:58:51 -0700135 binderserversuffix,
Yi Kong91635562018-06-07 14:38:36 -0700136 nullptr
Riley Andrews06b01ad2014-12-18 12:10:08 -0800137 };
138
139 ret = pipe(pipefd);
140 if (ret < 0)
141 return ret;
142
143 snprintf(stri, sizeof(stri), "%d", arg2);
144 snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
Martijn Coenen45b07b42017-08-09 12:07:45 +0200145 snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800146
147 pid = fork();
148 if (pid == -1)
149 return pid;
150 if (pid == 0) {
Steven Morelandda048352020-02-19 13:25:53 -0800151 prctl(PR_SET_PDEATHSIG, SIGHUP);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800152 close(pipefd[0]);
153 execv(binderservername, childargv);
154 status = -errno;
155 write(pipefd[1], &status, sizeof(status));
156 fprintf(stderr, "execv failed, %s\n", strerror(errno));
157 _exit(EXIT_FAILURE);
158 }
159 close(pipefd[1]);
160 ret = read(pipefd[0], &status, sizeof(status));
161 //printf("pipe read returned %d, status %d\n", ret, status);
162 close(pipefd[0]);
163 if (ret == sizeof(status)) {
164 ret = status;
165 } else {
166 kill(pid, SIGKILL);
167 if (ret >= 0) {
168 ret = NO_INIT;
169 }
170 }
171 if (ret < 0) {
Yi Kong91635562018-06-07 14:38:36 -0700172 wait(nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800173 return ret;
174 }
175 return pid;
176}
177
Yifan Hong84bedeb2021-04-21 21:37:17 -0700178android::base::Result<int32_t> GetId(sp<IBinder> service) {
179 using android::base::Error;
180 Parcel data, reply;
181 data.markForBinder(service);
182 const char *prefix = data.isForRpc() ? "On RPC server, " : "On binder server, ";
183 status_t status = service->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
184 if (status != OK)
185 return Error(status) << prefix << "transact(GET_ID): " << statusToString(status);
186 int32_t result = 0;
187 status = reply.readInt32(&result);
188 if (status != OK) return Error(status) << prefix << "readInt32: " << statusToString(status);
189 return result;
190}
191
Riley Andrews06b01ad2014-12-18 12:10:08 -0800192class BinderLibTestEnv : public ::testing::Environment {
193 public:
194 BinderLibTestEnv() {}
195 sp<IBinder> getServer(void) {
196 return m_server;
197 }
198
199 private:
200 virtual void SetUp() {
201 m_serverpid = start_server_process(0);
202 //printf("m_serverpid %d\n", m_serverpid);
203 ASSERT_GT(m_serverpid, 0);
204
205 sp<IServiceManager> sm = defaultServiceManager();
206 //printf("%s: pid %d, get service\n", __func__, m_pid);
207 m_server = sm->getService(binderLibTestServiceName);
Yi Kong91635562018-06-07 14:38:36 -0700208 ASSERT_TRUE(m_server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800209 //printf("%s: pid %d, get service done\n", __func__, m_pid);
210 }
211 virtual void TearDown() {
212 status_t ret;
213 Parcel data, reply;
214 int exitStatus;
215 pid_t pid;
216
217 //printf("%s: pid %d\n", __func__, m_pid);
Yi Kong91635562018-06-07 14:38:36 -0700218 if (m_server != nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800219 ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
220 EXPECT_EQ(0, ret);
221 ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
222 EXPECT_EQ(0, ret);
223 }
224 if (m_serverpid > 0) {
225 //printf("wait for %d\n", m_pids[i]);
226 pid = wait(&exitStatus);
227 EXPECT_EQ(m_serverpid, pid);
228 EXPECT_TRUE(WIFEXITED(exitStatus));
229 EXPECT_EQ(0, WEXITSTATUS(exitStatus));
230 }
231 }
232
233 pid_t m_serverpid;
234 sp<IBinder> m_server;
235};
236
237class BinderLibTest : public ::testing::Test {
238 public:
239 virtual void SetUp() {
240 m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
Olivier Gaillard91a04802018-11-14 17:32:41 +0000241 IPCThreadState::self()->restoreCallingWorkSource(0);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800242 }
243 virtual void TearDown() {
244 }
245 protected:
Martijn Coenen45b07b42017-08-09 12:07:45 +0200246 sp<IBinder> addServerEtc(int32_t *idPtr, int code)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800247 {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800248 int32_t id;
249 Parcel data, reply;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800250
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700251 EXPECT_THAT(m_server->transact(code, data, &reply), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800252
Elie Kheirallahb7246642022-05-03 18:01:43 +0000253 sp<IBinder> binder = reply.readStrongBinder();
254 EXPECT_NE(nullptr, binder);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700255 EXPECT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800256 if (idPtr)
257 *idPtr = id;
258 return binder;
259 }
Martijn Coenen45b07b42017-08-09 12:07:45 +0200260
Yi Kong91635562018-06-07 14:38:36 -0700261 sp<IBinder> addServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200262 {
263 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
264 }
265
Yi Kong91635562018-06-07 14:38:36 -0700266 sp<IBinder> addPollServer(int32_t *idPtr = nullptr)
Martijn Coenen45b07b42017-08-09 12:07:45 +0200267 {
268 return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
269 }
270
Riley Andrews06b01ad2014-12-18 12:10:08 -0800271 void waitForReadData(int fd, int timeout_ms) {
272 int ret;
273 pollfd pfd = pollfd();
274
275 pfd.fd = fd;
276 pfd.events = POLLIN;
277 ret = poll(&pfd, 1, timeout_ms);
278 EXPECT_EQ(1, ret);
279 }
280
281 sp<IBinder> m_server;
282};
283
284class BinderLibTestBundle : public Parcel
285{
286 public:
287 BinderLibTestBundle(void) {}
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800288 explicit BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800289 int32_t mark;
290 int32_t bundleLen;
291 size_t pos;
292
293 if (source->readInt32(&mark))
294 return;
295 if (mark != MARK_START)
296 return;
297 if (source->readInt32(&bundleLen))
298 return;
299 pos = source->dataPosition();
300 if (Parcel::appendFrom(source, pos, bundleLen))
301 return;
302 source->setDataPosition(pos + bundleLen);
303 if (source->readInt32(&mark))
304 return;
305 if (mark != MARK_END)
306 return;
307 m_isValid = true;
308 setDataPosition(0);
309 }
310 void appendTo(Parcel *dest) {
311 dest->writeInt32(MARK_START);
312 dest->writeInt32(dataSize());
313 dest->appendFrom(this, 0, dataSize());
314 dest->writeInt32(MARK_END);
315 };
316 bool isValid(void) {
317 return m_isValid;
318 }
319 private:
320 enum {
321 MARK_START = B_PACK_CHARS('B','T','B','S'),
322 MARK_END = B_PACK_CHARS('B','T','B','E'),
323 };
324 bool m_isValid;
325};
326
327class BinderLibTestEvent
328{
329 public:
330 BinderLibTestEvent(void)
331 : m_eventTriggered(false)
332 {
Yi Kong91635562018-06-07 14:38:36 -0700333 pthread_mutex_init(&m_waitMutex, nullptr);
334 pthread_cond_init(&m_waitCond, nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800335 }
336 int waitEvent(int timeout_s)
337 {
338 int ret;
339 pthread_mutex_lock(&m_waitMutex);
340 if (!m_eventTriggered) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800341 struct timespec ts;
342 clock_gettime(CLOCK_REALTIME, &ts);
343 ts.tv_sec += timeout_s;
344 pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800345 }
346 ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
347 pthread_mutex_unlock(&m_waitMutex);
348 return ret;
349 }
Martijn Coenenf7100e42017-07-31 12:14:09 +0200350 pthread_t getTriggeringThread()
351 {
352 return m_triggeringThread;
353 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800354 protected:
355 void triggerEvent(void) {
356 pthread_mutex_lock(&m_waitMutex);
357 pthread_cond_signal(&m_waitCond);
358 m_eventTriggered = true;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200359 m_triggeringThread = pthread_self();
Riley Andrews06b01ad2014-12-18 12:10:08 -0800360 pthread_mutex_unlock(&m_waitMutex);
361 };
362 private:
363 pthread_mutex_t m_waitMutex;
364 pthread_cond_t m_waitCond;
365 bool m_eventTriggered;
Martijn Coenenf7100e42017-07-31 12:14:09 +0200366 pthread_t m_triggeringThread;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800367};
368
369class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
370{
371 public:
372 BinderLibTestCallBack()
373 : m_result(NOT_ENOUGH_DATA)
Yi Kong91635562018-06-07 14:38:36 -0700374 , m_prev_end(nullptr)
Riley Andrews06b01ad2014-12-18 12:10:08 -0800375 {
376 }
377 status_t getResult(void)
378 {
379 return m_result;
380 }
381
382 private:
383 virtual status_t onTransact(uint32_t code,
384 const Parcel& data, Parcel* reply,
385 uint32_t flags = 0)
386 {
387 (void)reply;
388 (void)flags;
389 switch(code) {
Martijn Coenenfb368f72017-08-10 15:03:18 +0200390 case BINDER_LIB_TEST_CALL_BACK: {
391 status_t status = data.readInt32(&m_result);
392 if (status != NO_ERROR) {
393 m_result = status;
394 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800395 triggerEvent();
396 return NO_ERROR;
Martijn Coenenfb368f72017-08-10 15:03:18 +0200397 }
Sherry Yang336cdd32017-07-24 14:12:27 -0700398 case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
399 sp<IBinder> server;
400 int ret;
401 const uint8_t *buf = data.data();
402 size_t size = data.dataSize();
403 if (m_prev_end) {
404 /* 64-bit kernel needs at most 8 bytes to align buffer end */
405 EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
406 } else {
407 EXPECT_TRUE(IsPageAligned((void *)buf));
408 }
409
410 m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
411
412 if (size > 0) {
413 server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
414 ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
415 data, reply);
416 EXPECT_EQ(NO_ERROR, ret);
417 }
418 return NO_ERROR;
419 }
Riley Andrews06b01ad2014-12-18 12:10:08 -0800420 default:
421 return UNKNOWN_TRANSACTION;
422 }
423 }
424
425 status_t m_result;
Sherry Yang336cdd32017-07-24 14:12:27 -0700426 const uint8_t *m_prev_end;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800427};
428
429class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
430{
431 private:
432 virtual void binderDied(const wp<IBinder>& who) {
433 (void)who;
434 triggerEvent();
435 };
436};
437
Steven Morelandbd98e0f2021-10-14 14:24:15 -0700438TEST_F(BinderLibTest, CannotUseBinderAfterFork) {
439 // EXPECT_DEATH works by forking the process
440 EXPECT_DEATH({ ProcessState::self(); }, "libbinder ProcessState can not be used after fork");
441}
442
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500443TEST_F(BinderLibTest, WasParceled) {
444 auto binder = sp<BBinder>::make();
445 EXPECT_FALSE(binder->wasParceled());
446 Parcel data;
447 data.writeStrongBinder(binder);
448 EXPECT_TRUE(binder->wasParceled());
449}
450
Riley Andrews06b01ad2014-12-18 12:10:08 -0800451TEST_F(BinderLibTest, NopTransaction) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800452 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700453 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply),
454 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800455}
456
Steven Moreland80844f72020-12-12 02:06:08 +0000457TEST_F(BinderLibTest, NopTransactionOneway) {
Steven Moreland80844f72020-12-12 02:06:08 +0000458 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700459 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_ONE_WAY),
460 StatusEq(NO_ERROR));
Steven Moreland80844f72020-12-12 02:06:08 +0000461}
462
Steven Morelandf183fdd2020-10-27 00:12:12 +0000463TEST_F(BinderLibTest, NopTransactionClear) {
Steven Morelandf183fdd2020-10-27 00:12:12 +0000464 Parcel data, reply;
465 // make sure it accepts the transaction flag
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700466 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply, TF_CLEAR_BUF),
467 StatusEq(NO_ERROR));
Steven Morelandf183fdd2020-10-27 00:12:12 +0000468}
469
Marco Ballesio7ee17572020-09-08 10:30:03 -0700470TEST_F(BinderLibTest, Freeze) {
Marco Ballesio7ee17572020-09-08 10:30:03 -0700471 Parcel data, reply, replypid;
Li Li6f059292021-09-10 09:59:30 -0700472 std::ifstream freezer_file("/sys/fs/cgroup/uid_0/cgroup.freeze");
Marco Ballesio7ee17572020-09-08 10:30:03 -0700473
Li Li6f059292021-09-10 09:59:30 -0700474 // Pass test on devices where the cgroup v2 freezer is not supported
Marco Ballesio7ee17572020-09-08 10:30:03 -0700475 if (freezer_file.fail()) {
476 GTEST_SKIP();
477 return;
478 }
479
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700480 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GETPID, data, &replypid), StatusEq(NO_ERROR));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700481 int32_t pid = replypid.readInt32();
Marco Ballesio7ee17572020-09-08 10:30:03 -0700482 for (int i = 0; i < 10; i++) {
483 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION_WAIT, data, &reply, TF_ONE_WAY));
484 }
Li Li6f059292021-09-10 09:59:30 -0700485
486 // Pass test on devices where BINDER_FREEZE ioctl is not supported
487 int ret = IPCThreadState::self()->freeze(pid, false, 0);
488 if (ret != 0) {
489 GTEST_SKIP();
490 return;
491 }
492
493 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
494 EXPECT_EQ(-EAGAIN, IPCThreadState::self()->freeze(pid, true, 0));
495 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, true, 1000));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700496 EXPECT_EQ(FAILED_TRANSACTION, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700497
Li Li4e678b92021-09-14 12:14:42 -0700498 uint32_t sync_received, async_received;
Marco Ballesiob09fc4a2020-09-11 16:17:21 -0700499
500 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->getProcessFreezeInfo(pid, &sync_received,
501 &async_received));
502
503 EXPECT_EQ(sync_received, 1);
504 EXPECT_EQ(async_received, 0);
505
Li Li4e678b92021-09-14 12:14:42 -0700506 EXPECT_EQ(NO_ERROR, IPCThreadState::self()->freeze(pid, false, 0));
Marco Ballesio7ee17572020-09-08 10:30:03 -0700507 EXPECT_EQ(NO_ERROR, m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply));
508}
509
Riley Andrews06b01ad2014-12-18 12:10:08 -0800510TEST_F(BinderLibTest, SetError) {
511 int32_t testValue[] = { 0, -123, 123 };
512 for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800513 Parcel data, reply;
514 data.writeInt32(testValue[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700515 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply),
516 StatusEq(testValue[i]));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800517 }
518}
519
520TEST_F(BinderLibTest, GetId) {
Yifan Hong28d6c352021-06-04 17:27:35 -0700521 EXPECT_THAT(GetId(m_server), HasValue(0));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800522}
523
524TEST_F(BinderLibTest, PtrSize) {
Riley Andrews06b01ad2014-12-18 12:10:08 -0800525 int32_t ptrsize;
526 Parcel data, reply;
527 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700528 ASSERT_TRUE(server != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700529 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply),
530 StatusEq(NO_ERROR));
531 EXPECT_THAT(reply.readInt32(&ptrsize), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800532 RecordProperty("TestPtrSize", sizeof(void *));
533 RecordProperty("ServerPtrSize", sizeof(void *));
534}
535
536TEST_F(BinderLibTest, IndirectGetId2)
537{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800538 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
548 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700549 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800550 data.writeStrongBinder(server);
551 data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
552 datai.appendTo(&data);
553 }
554
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700555 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
556 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800557
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700558 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800559 EXPECT_EQ(0, id);
560
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700561 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700562 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800563
564 for (size_t i = 0; i < (size_t)count; i++) {
565 BinderLibTestBundle replyi(&reply);
566 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700567 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800568 EXPECT_EQ(serverId[i], id);
569 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
570 }
571
572 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
573}
574
575TEST_F(BinderLibTest, IndirectGetId3)
576{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800577 int32_t id;
578 int32_t count;
579 Parcel data, reply;
580 int32_t serverId[3];
581
582 data.writeInt32(ARRAY_SIZE(serverId));
583 for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
584 sp<IBinder> server;
585 BinderLibTestBundle datai;
586 BinderLibTestBundle datai2;
587
588 server = addServer(&serverId[i]);
Yi Kong91635562018-06-07 14:38:36 -0700589 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800590 data.writeStrongBinder(server);
591 data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
592
593 datai.writeInt32(1);
594 datai.writeStrongBinder(m_server);
595 datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
596 datai2.appendTo(&datai);
597
598 datai.appendTo(&data);
599 }
600
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700601 ASSERT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
602 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800603
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700604 ASSERT_THAT(reply.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800605 EXPECT_EQ(0, id);
606
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700607 ASSERT_THAT(reply.readInt32(&count), StatusEq(NO_ERROR));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700608 EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800609
610 for (size_t i = 0; i < (size_t)count; i++) {
611 int32_t counti;
612
613 BinderLibTestBundle replyi(&reply);
614 EXPECT_TRUE(replyi.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700615 EXPECT_THAT(replyi.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800616 EXPECT_EQ(serverId[i], id);
617
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700618 ASSERT_THAT(replyi.readInt32(&counti), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800619 EXPECT_EQ(1, counti);
620
621 BinderLibTestBundle replyi2(&replyi);
622 EXPECT_TRUE(replyi2.isValid());
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700623 EXPECT_THAT(replyi2.readInt32(&id), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800624 EXPECT_EQ(0, id);
625 EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
626
627 EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
628 }
629
630 EXPECT_EQ(reply.dataSize(), reply.dataPosition());
631}
632
633TEST_F(BinderLibTest, CallBack)
634{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800635 Parcel data, reply;
636 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
637 data.writeStrongBinder(callBack);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700638 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY),
639 StatusEq(NO_ERROR));
640 EXPECT_THAT(callBack->waitEvent(5), StatusEq(NO_ERROR));
641 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800642}
643
Steven Moreland35626652021-05-15 01:32:04 +0000644TEST_F(BinderLibTest, BinderCallContextGuard) {
645 sp<IBinder> binder = addServer();
646 Parcel data, reply;
647 EXPECT_THAT(binder->transact(BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION, data, &reply),
648 StatusEq(DEAD_OBJECT));
649}
650
Riley Andrews06b01ad2014-12-18 12:10:08 -0800651TEST_F(BinderLibTest, AddServer)
652{
653 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700654 ASSERT_TRUE(server != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800655}
656
Riley Andrews06b01ad2014-12-18 12:10:08 -0800657TEST_F(BinderLibTest, DeathNotificationStrongRef)
658{
Riley Andrews06b01ad2014-12-18 12:10:08 -0800659 sp<IBinder> sbinder;
660
661 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
662
663 {
664 sp<IBinder> binder = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700665 ASSERT_TRUE(binder != nullptr);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700666 EXPECT_THAT(binder->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800667 sbinder = binder;
668 }
669 {
670 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700671 EXPECT_THAT(sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY),
672 StatusEq(OK));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800673 }
674 IPCThreadState::self()->flushCommands();
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700675 EXPECT_THAT(testDeathRecipient->waitEvent(5), StatusEq(NO_ERROR));
676 EXPECT_THAT(sbinder->unlinkToDeath(testDeathRecipient), StatusEq(DEAD_OBJECT));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800677}
678
679TEST_F(BinderLibTest, DeathNotificationMultiple)
680{
681 status_t ret;
682 const int clientcount = 2;
683 sp<IBinder> target;
684 sp<IBinder> linkedclient[clientcount];
685 sp<BinderLibTestCallBack> callBack[clientcount];
686 sp<IBinder> passiveclient[clientcount];
687
688 target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700689 ASSERT_TRUE(target != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800690 for (int i = 0; i < clientcount; i++) {
691 {
692 Parcel data, reply;
693
694 linkedclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700695 ASSERT_TRUE(linkedclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800696 callBack[i] = new BinderLibTestCallBack();
697 data.writeStrongBinder(target);
698 data.writeStrongBinder(callBack[i]);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700699 EXPECT_THAT(linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data,
700 &reply, TF_ONE_WAY),
701 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800702 }
703 {
704 Parcel data, reply;
705
706 passiveclient[i] = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700707 ASSERT_TRUE(passiveclient[i] != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800708 data.writeStrongBinder(target);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700709 EXPECT_THAT(passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data,
710 &reply, TF_ONE_WAY),
711 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800712 }
713 }
714 {
715 Parcel data, reply;
716 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
717 EXPECT_EQ(0, ret);
718 }
719
720 for (int i = 0; i < clientcount; i++) {
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700721 EXPECT_THAT(callBack[i]->waitEvent(5), StatusEq(NO_ERROR));
722 EXPECT_THAT(callBack[i]->getResult(), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800723 }
724}
725
Martijn Coenenf7100e42017-07-31 12:14:09 +0200726TEST_F(BinderLibTest, DeathNotificationThread)
727{
728 status_t ret;
729 sp<BinderLibTestCallBack> callback;
730 sp<IBinder> target = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700731 ASSERT_TRUE(target != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200732 sp<IBinder> client = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700733 ASSERT_TRUE(client != nullptr);
Martijn Coenenf7100e42017-07-31 12:14:09 +0200734
735 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
736
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700737 EXPECT_THAT(target->linkToDeath(testDeathRecipient), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200738
739 {
740 Parcel data, reply;
741 ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
742 EXPECT_EQ(0, ret);
743 }
744
745 /* Make sure it's dead */
746 testDeathRecipient->waitEvent(5);
747
748 /* Now, pass the ref to another process and ask that process to
749 * call linkToDeath() on it, and wait for a response. This tests
750 * two things:
751 * 1) You still get death notifications when calling linkToDeath()
752 * on a ref that is already dead when it was passed to you.
753 * 2) That death notifications are not directly pushed to the thread
754 * registering them, but to the threadpool (proc workqueue) instead.
755 *
756 * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
757 * is blocked on a condition variable waiting for the death notification to be
758 * called; therefore, that thread is not available for handling proc work.
759 * So, if the death notification was pushed to the thread workqueue, the callback
760 * would never be called, and the test would timeout and fail.
761 *
762 * Note that we can't do this part of the test from this thread itself, because
763 * the binder driver would only push death notifications to the thread if
764 * it is a looper thread, which this thread is not.
765 *
766 * See b/23525545 for details.
767 */
768 {
769 Parcel data, reply;
770
771 callback = new BinderLibTestCallBack();
772 data.writeStrongBinder(target);
773 data.writeStrongBinder(callback);
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700774 EXPECT_THAT(client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply,
775 TF_ONE_WAY),
776 StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200777 }
778
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700779 EXPECT_THAT(callback->waitEvent(5), StatusEq(NO_ERROR));
780 EXPECT_THAT(callback->getResult(), StatusEq(NO_ERROR));
Martijn Coenenf7100e42017-07-31 12:14:09 +0200781}
782
Riley Andrews06b01ad2014-12-18 12:10:08 -0800783TEST_F(BinderLibTest, PassFile) {
784 int ret;
785 int pipefd[2];
786 uint8_t buf[1] = { 0 };
787 uint8_t write_value = 123;
788
789 ret = pipe2(pipefd, O_NONBLOCK);
790 ASSERT_EQ(0, ret);
791
792 {
793 Parcel data, reply;
794 uint8_t writebuf[1] = { write_value };
795
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700796 EXPECT_THAT(data.writeFileDescriptor(pipefd[1], true), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800797
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700798 EXPECT_THAT(data.writeInt32(sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800799
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700800 EXPECT_THAT(data.write(writebuf, sizeof(writebuf)), StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800801
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700802 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply),
803 StatusEq(NO_ERROR));
Riley Andrews06b01ad2014-12-18 12:10:08 -0800804 }
805
806 ret = read(pipefd[0], buf, sizeof(buf));
Arve Hjønnevåg6d5fa942016-08-12 15:32:48 -0700807 EXPECT_EQ(sizeof(buf), (size_t)ret);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800808 EXPECT_EQ(write_value, buf[0]);
809
810 waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
811
812 ret = read(pipefd[0], buf, sizeof(buf));
813 EXPECT_EQ(0, ret);
814
815 close(pipefd[0]);
816}
817
Ryo Hashimotobf551892018-05-31 16:58:35 +0900818TEST_F(BinderLibTest, PassParcelFileDescriptor) {
819 const int datasize = 123;
820 std::vector<uint8_t> writebuf(datasize);
821 for (size_t i = 0; i < writebuf.size(); ++i) {
822 writebuf[i] = i;
823 }
824
825 android::base::unique_fd read_end, write_end;
826 {
827 int pipefd[2];
828 ASSERT_EQ(0, pipe2(pipefd, O_NONBLOCK));
829 read_end.reset(pipefd[0]);
830 write_end.reset(pipefd[1]);
831 }
832 {
833 Parcel data;
834 EXPECT_EQ(NO_ERROR, data.writeDupParcelFileDescriptor(write_end.get()));
835 write_end.reset();
836 EXPECT_EQ(NO_ERROR, data.writeInt32(datasize));
837 EXPECT_EQ(NO_ERROR, data.write(writebuf.data(), datasize));
838
839 Parcel reply;
840 EXPECT_EQ(NO_ERROR,
841 m_server->transact(BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION, data,
842 &reply));
843 }
844 std::vector<uint8_t> readbuf(datasize);
845 EXPECT_EQ(datasize, read(read_end.get(), readbuf.data(), datasize));
846 EXPECT_EQ(writebuf, readbuf);
847
848 waitForReadData(read_end.get(), 5000); /* wait for other proccess to close pipe */
849
850 EXPECT_EQ(0, read(read_end.get(), readbuf.data(), datasize));
851}
852
Riley Andrews06b01ad2014-12-18 12:10:08 -0800853TEST_F(BinderLibTest, PromoteLocal) {
854 sp<IBinder> strong = new BBinder();
855 wp<IBinder> weak = strong;
856 sp<IBinder> strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700857 EXPECT_TRUE(strong != nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800858 EXPECT_EQ(strong, strong_from_weak);
Yi Kong91635562018-06-07 14:38:36 -0700859 strong = nullptr;
860 strong_from_weak = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -0800861 strong_from_weak = weak.promote();
Yi Kong91635562018-06-07 14:38:36 -0700862 EXPECT_TRUE(strong_from_weak == nullptr);
Riley Andrews06b01ad2014-12-18 12:10:08 -0800863}
864
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700865TEST_F(BinderLibTest, LocalGetExtension) {
866 sp<BBinder> binder = new BBinder();
867 sp<IBinder> ext = new BBinder();
868 binder->setExtension(ext);
869 EXPECT_EQ(ext, binder->getExtension());
870}
871
872TEST_F(BinderLibTest, RemoteGetExtension) {
873 sp<IBinder> server = addServer();
874 ASSERT_TRUE(server != nullptr);
875
876 sp<IBinder> extension;
877 EXPECT_EQ(NO_ERROR, server->getExtension(&extension));
878 ASSERT_NE(nullptr, extension.get());
879
880 EXPECT_EQ(NO_ERROR, extension->pingBinder());
881}
882
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700883TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700884 Parcel data, reply;
885
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700886 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply),
887 StatusEq(NO_ERROR));
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700888
889 const flat_binder_object *fb = reply.readObject(false);
Yi Kong91635562018-06-07 14:38:36 -0700890 ASSERT_TRUE(fb != nullptr);
Hsin-Yi Chenad6503c2017-07-28 11:28:52 +0800891 EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
892 EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
893 EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
894 EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
Arve Hjønnevåg70604312016-08-12 15:34:51 -0700895}
896
Connor O'Brien52be2c92016-09-20 14:18:08 -0700897TEST_F(BinderLibTest, FreedBinder) {
898 status_t ret;
899
900 sp<IBinder> server = addServer();
Yi Kong91635562018-06-07 14:38:36 -0700901 ASSERT_TRUE(server != nullptr);
Connor O'Brien52be2c92016-09-20 14:18:08 -0700902
903 __u32 freedHandle;
904 wp<IBinder> keepFreedBinder;
905 {
906 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700907 ASSERT_THAT(server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply),
908 StatusEq(NO_ERROR));
Connor O'Brien52be2c92016-09-20 14:18:08 -0700909 struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
910 freedHandle = freed->handle;
911 /* Add a weak ref to the freed binder so the driver does not
912 * delete its reference to it - otherwise the transaction
913 * fails regardless of whether the driver is fixed.
914 */
Steven Morelande171d622019-07-17 16:06:01 -0700915 keepFreedBinder = reply.readStrongBinder();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700916 }
Steven Morelande171d622019-07-17 16:06:01 -0700917 IPCThreadState::self()->flushCommands();
Connor O'Brien52be2c92016-09-20 14:18:08 -0700918 {
919 Parcel data, reply;
920 data.writeStrongBinder(server);
921 /* Replace original handle with handle to the freed binder */
922 struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
923 __u32 oldHandle = strong->handle;
924 strong->handle = freedHandle;
925 ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
926 /* Returns DEAD_OBJECT (-32) if target crashes and
927 * FAILED_TRANSACTION if the driver rejects the invalid
928 * object.
929 */
930 EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
931 /* Restore original handle so parcel destructor does not use
932 * the wrong handle.
933 */
934 strong->handle = oldHandle;
935 }
936}
937
Sherry Yang336cdd32017-07-24 14:12:27 -0700938TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
Sherry Yang336cdd32017-07-24 14:12:27 -0700939 Parcel data, reply;
940 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
941 for (int i = 0; i < 2; i++) {
942 BinderLibTestBundle datai;
943 datai.appendFrom(&data, 0, data.dataSize());
944
945 data.freeData();
946 data.writeInt32(1);
947 data.writeStrongBinder(callBack);
948 data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
949
950 datai.appendTo(&data);
951 }
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700952 EXPECT_THAT(m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply),
953 StatusEq(NO_ERROR));
Sherry Yang336cdd32017-07-24 14:12:27 -0700954}
955
Martijn Coenen45b07b42017-08-09 12:07:45 +0200956TEST_F(BinderLibTest, OnewayQueueing)
957{
Martijn Coenen45b07b42017-08-09 12:07:45 +0200958 Parcel data, data2;
959
960 sp<IBinder> pollServer = addPollServer();
961
962 sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
963 data.writeStrongBinder(callBack);
964 data.writeInt32(500000); // delay in us before calling back
965
966 sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
967 data2.writeStrongBinder(callBack2);
968 data2.writeInt32(0); // delay in us
969
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700970 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, nullptr, TF_ONE_WAY),
971 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200972
973 // The delay ensures that this second transaction will end up on the async_todo list
974 // (for a single-threaded server)
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700975 EXPECT_THAT(pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, nullptr, TF_ONE_WAY),
976 StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200977
978 // The server will ensure that the two transactions are handled in the expected order;
979 // If the ordering is not as expected, an error will be returned through the callbacks.
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700980 EXPECT_THAT(callBack->waitEvent(2), StatusEq(NO_ERROR));
981 EXPECT_THAT(callBack->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200982
Yifan Hongbbd2a0d2021-05-07 22:12:23 -0700983 EXPECT_THAT(callBack2->waitEvent(2), StatusEq(NO_ERROR));
984 EXPECT_THAT(callBack2->getResult(), StatusEq(NO_ERROR));
Martijn Coenen45b07b42017-08-09 12:07:45 +0200985}
986
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100987TEST_F(BinderLibTest, WorkSourceUnsetByDefault)
988{
989 status_t ret;
990 Parcel data, reply;
991 data.writeInterfaceToken(binderLibTestServiceName);
992 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
993 EXPECT_EQ(-1, reply.readInt32());
994 EXPECT_EQ(NO_ERROR, ret);
995}
996
997TEST_F(BinderLibTest, WorkSourceSet)
998{
999 status_t ret;
1000 Parcel data, reply;
Olivier Gaillard91a04802018-11-14 17:32:41 +00001001 IPCThreadState::self()->clearCallingWorkSource();
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001002 int64_t previousWorkSource = IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001003 data.writeInterfaceToken(binderLibTestServiceName);
1004 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1005 EXPECT_EQ(100, reply.readInt32());
1006 EXPECT_EQ(-1, previousWorkSource);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001007 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1008 EXPECT_EQ(NO_ERROR, ret);
1009}
1010
1011TEST_F(BinderLibTest, WorkSourceSetWithoutPropagation)
1012{
1013 status_t ret;
1014 Parcel data, reply;
1015
1016 IPCThreadState::self()->setCallingWorkSourceUidWithoutPropagation(100);
1017 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1018
1019 data.writeInterfaceToken(binderLibTestServiceName);
1020 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1021 EXPECT_EQ(-1, reply.readInt32());
1022 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001023 EXPECT_EQ(NO_ERROR, ret);
1024}
1025
1026TEST_F(BinderLibTest, WorkSourceCleared)
1027{
1028 status_t ret;
1029 Parcel data, reply;
1030
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001031 IPCThreadState::self()->setCallingWorkSourceUid(100);
Olivier Gaillard91a04802018-11-14 17:32:41 +00001032 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1033 int32_t previousWorkSource = (int32_t)token;
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001034 data.writeInterfaceToken(binderLibTestServiceName);
1035 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1036
1037 EXPECT_EQ(-1, reply.readInt32());
1038 EXPECT_EQ(100, previousWorkSource);
1039 EXPECT_EQ(NO_ERROR, ret);
1040}
1041
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001042TEST_F(BinderLibTest, WorkSourceRestored)
1043{
1044 status_t ret;
1045 Parcel data, reply;
1046
1047 IPCThreadState::self()->setCallingWorkSourceUid(100);
1048 int64_t token = IPCThreadState::self()->clearCallingWorkSource();
1049 IPCThreadState::self()->restoreCallingWorkSource(token);
1050
1051 data.writeInterfaceToken(binderLibTestServiceName);
1052 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1053
1054 EXPECT_EQ(100, reply.readInt32());
Olivier Gaillard91a04802018-11-14 17:32:41 +00001055 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001056 EXPECT_EQ(NO_ERROR, ret);
1057}
1058
Olivier Gaillard91a04802018-11-14 17:32:41 +00001059TEST_F(BinderLibTest, PropagateFlagSet)
1060{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001061 IPCThreadState::self()->clearPropagateWorkSource();
1062 IPCThreadState::self()->setCallingWorkSourceUid(100);
1063 EXPECT_EQ(true, IPCThreadState::self()->shouldPropagateWorkSource());
1064}
1065
1066TEST_F(BinderLibTest, PropagateFlagCleared)
1067{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001068 IPCThreadState::self()->setCallingWorkSourceUid(100);
1069 IPCThreadState::self()->clearPropagateWorkSource();
1070 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1071}
1072
1073TEST_F(BinderLibTest, PropagateFlagRestored)
1074{
Olivier Gaillard91a04802018-11-14 17:32:41 +00001075 int token = IPCThreadState::self()->setCallingWorkSourceUid(100);
1076 IPCThreadState::self()->restoreCallingWorkSource(token);
1077
1078 EXPECT_EQ(false, IPCThreadState::self()->shouldPropagateWorkSource());
1079}
1080
1081TEST_F(BinderLibTest, WorkSourcePropagatedForAllFollowingBinderCalls)
1082{
1083 IPCThreadState::self()->setCallingWorkSourceUid(100);
1084
1085 Parcel data, reply;
1086 status_t ret;
1087 data.writeInterfaceToken(binderLibTestServiceName);
1088 ret = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data, &reply);
1089
1090 Parcel data2, reply2;
1091 status_t ret2;
1092 data2.writeInterfaceToken(binderLibTestServiceName);
1093 ret2 = m_server->transact(BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION, data2, &reply2);
1094 EXPECT_EQ(100, reply2.readInt32());
1095 EXPECT_EQ(NO_ERROR, ret2);
1096}
1097
Steven Morelandbf1915b2020-07-16 22:43:02 +00001098TEST_F(BinderLibTest, SchedPolicySet) {
1099 sp<IBinder> server = addServer();
1100 ASSERT_TRUE(server != nullptr);
1101
1102 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001103 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1104 StatusEq(NO_ERROR));
Steven Morelandbf1915b2020-07-16 22:43:02 +00001105
1106 int policy = reply.readInt32();
1107 int priority = reply.readInt32();
1108
1109 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1110 EXPECT_EQ(kSchedPriority, priority);
1111}
1112
Steven Morelandcf03cf12020-12-04 02:58:40 +00001113TEST_F(BinderLibTest, InheritRt) {
1114 sp<IBinder> server = addServer();
1115 ASSERT_TRUE(server != nullptr);
1116
1117 const struct sched_param param {
1118 .sched_priority = kSchedPriorityMore,
1119 };
1120 EXPECT_EQ(0, sched_setscheduler(getpid(), SCHED_RR, &param));
1121
1122 Parcel data, reply;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001123 EXPECT_THAT(server->transact(BINDER_LIB_TEST_GET_SCHEDULING_POLICY, data, &reply),
1124 StatusEq(NO_ERROR));
Steven Morelandcf03cf12020-12-04 02:58:40 +00001125
1126 int policy = reply.readInt32();
1127 int priority = reply.readInt32();
1128
1129 EXPECT_EQ(kSchedPolicy, policy & (~SCHED_RESET_ON_FORK));
1130 EXPECT_EQ(kSchedPriorityMore, priority);
1131}
Steven Morelandbf1915b2020-07-16 22:43:02 +00001132
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001133TEST_F(BinderLibTest, VectorSent) {
1134 Parcel data, reply;
1135 sp<IBinder> server = addServer();
1136 ASSERT_TRUE(server != nullptr);
1137
1138 std::vector<uint64_t> const testValue = { std::numeric_limits<uint64_t>::max(), 0, 200 };
1139 data.writeUint64Vector(testValue);
1140
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001141 EXPECT_THAT(server->transact(BINDER_LIB_TEST_ECHO_VECTOR, data, &reply), StatusEq(NO_ERROR));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001142 std::vector<uint64_t> readValue;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001143 EXPECT_THAT(reply.readUint64Vector(&readValue), StatusEq(OK));
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001144 EXPECT_EQ(readValue, testValue);
1145}
1146
Martijn Coenen82c75312019-07-24 15:18:30 +02001147TEST_F(BinderLibTest, BufRejected) {
1148 Parcel data, reply;
1149 uint32_t buf;
1150 sp<IBinder> server = addServer();
1151 ASSERT_TRUE(server != nullptr);
1152
1153 binder_buffer_object obj {
1154 .hdr = { .type = BINDER_TYPE_PTR },
Nick Desaulniers54891cd2019-11-19 09:31:05 -08001155 .flags = 0,
Martijn Coenen82c75312019-07-24 15:18:30 +02001156 .buffer = reinterpret_cast<binder_uintptr_t>((void*)&buf),
1157 .length = 4,
Martijn Coenen82c75312019-07-24 15:18:30 +02001158 };
1159 data.setDataCapacity(1024);
1160 // Write a bogus object at offset 0 to get an entry in the offset table
1161 data.writeFileDescriptor(0);
1162 EXPECT_EQ(data.objectsCount(), 1);
1163 uint8_t *parcelData = const_cast<uint8_t*>(data.data());
1164 // And now, overwrite it with the buffer object
1165 memcpy(parcelData, &obj, sizeof(obj));
1166 data.setDataSize(sizeof(obj));
1167
Steven Morelandf2e0a952021-11-01 18:17:23 -07001168 EXPECT_EQ(data.objectsCount(), 1);
1169
Martijn Coenen82c75312019-07-24 15:18:30 +02001170 // Either the kernel should reject this transaction (if it's correct), but
1171 // if it's not, the server implementation should return an error if it
1172 // finds an object in the received Parcel.
Steven Morelandf2e0a952021-11-01 18:17:23 -07001173 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_OBJECTS, data, &reply),
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001174 Not(StatusEq(NO_ERROR)));
Martijn Coenen82c75312019-07-24 15:18:30 +02001175}
1176
Steven Morelandf2e0a952021-11-01 18:17:23 -07001177TEST_F(BinderLibTest, WeakRejected) {
1178 Parcel data, reply;
1179 sp<IBinder> server = addServer();
1180 ASSERT_TRUE(server != nullptr);
1181
1182 auto binder = sp<BBinder>::make();
1183 wp<BBinder> wpBinder(binder);
1184 flat_binder_object obj{
1185 .hdr = {.type = BINDER_TYPE_WEAK_BINDER},
1186 .flags = 0,
1187 .binder = reinterpret_cast<uintptr_t>(wpBinder.get_refs()),
1188 .cookie = reinterpret_cast<uintptr_t>(wpBinder.unsafe_get()),
1189 };
1190 data.setDataCapacity(1024);
1191 // Write a bogus object at offset 0 to get an entry in the offset table
1192 data.writeFileDescriptor(0);
1193 EXPECT_EQ(data.objectsCount(), 1);
1194 uint8_t *parcelData = const_cast<uint8_t *>(data.data());
1195 // And now, overwrite it with the weak binder
1196 memcpy(parcelData, &obj, sizeof(obj));
1197 data.setDataSize(sizeof(obj));
1198
1199 // a previous bug caused other objects to be released an extra time, so we
1200 // test with an object that libbinder will actually try to release
1201 EXPECT_EQ(OK, data.writeStrongBinder(sp<BBinder>::make()));
1202
1203 EXPECT_EQ(data.objectsCount(), 2);
1204
1205 // send it many times, since previous error was memory corruption, make it
1206 // more likely that the server crashes
1207 for (size_t i = 0; i < 100; i++) {
1208 EXPECT_THAT(server->transact(BINDER_LIB_TEST_REJECT_OBJECTS, data, &reply),
1209 StatusEq(BAD_VALUE));
1210 }
1211
1212 EXPECT_THAT(server->pingBinder(), StatusEq(NO_ERROR));
1213}
1214
Steven Moreland254e8ef2021-04-19 22:28:50 +00001215TEST_F(BinderLibTest, GotSid) {
1216 sp<IBinder> server = addServer();
1217
1218 Parcel data;
Yifan Hongbbd2a0d2021-05-07 22:12:23 -07001219 EXPECT_THAT(server->transact(BINDER_LIB_TEST_CAN_GET_SID, data, nullptr), StatusEq(OK));
Steven Moreland254e8ef2021-04-19 22:28:50 +00001220}
1221
Jayant Chowdhary30700942022-01-31 14:12:40 -08001222TEST(ServiceNotifications, Unregister) {
1223 auto sm = defaultServiceManager();
1224 using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
1225 class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
1226 void onServiceRegistration(const String16 &, const sp<IBinder> &) override {}
1227 virtual ~LocalRegistrationCallbackImpl() {}
1228 };
1229 sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();
1230
1231 EXPECT_EQ(sm->registerForNotifications(String16("RogerRafa"), cb), OK);
1232 EXPECT_EQ(sm->unregisterForNotifications(String16("RogerRafa"), cb), OK);
1233}
1234
Yifan Hong84bedeb2021-04-21 21:37:17 -07001235class BinderLibRpcTestBase : public BinderLibTest {
1236public:
1237 void SetUp() override {
1238 if (!base::GetBoolProperty("ro.debuggable", false)) {
1239 GTEST_SKIP() << "Binder RPC is only enabled on debuggable builds, skipping test on "
1240 "non-debuggable builds.";
1241 }
1242 BinderLibTest::SetUp();
1243 }
1244
1245 std::tuple<android::base::unique_fd, unsigned int> CreateSocket() {
1246 auto rpcServer = RpcServer::make();
1247 EXPECT_NE(nullptr, rpcServer);
1248 if (rpcServer == nullptr) return {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001249 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001250 if (status_t status = rpcServer->setupInetServer("127.0.0.1", 0, &port); status != OK) {
1251 ADD_FAILURE() << "setupInetServer failed" << statusToString(status);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001252 return {};
1253 }
1254 return {rpcServer->releaseServer(), port};
1255 }
1256};
1257
Yifan Hong8b890852021-06-10 13:44:09 -07001258class BinderLibRpcTest : public BinderLibRpcTestBase {};
Yifan Hong84bedeb2021-04-21 21:37:17 -07001259
Yifan Hongbd276552022-02-28 15:28:51 -08001260// e.g. EXPECT_THAT(expr, Debuggable(StatusEq(...))
1261// If device is debuggable AND not on user builds, expects matcher.
1262// Otherwise expects INVALID_OPERATION.
1263// Debuggable + non user builds is necessary but not sufficient for setRpcClientDebug to work.
1264static Matcher<status_t> Debuggable(const Matcher<status_t> &matcher) {
1265 bool isDebuggable = android::base::GetBoolProperty("ro.debuggable", false) &&
1266 android::base::GetProperty("ro.build.type", "") != "user";
1267 return isDebuggable ? matcher : StatusEq(INVALID_OPERATION);
1268}
1269
Yifan Hong8b890852021-06-10 13:44:09 -07001270TEST_F(BinderLibRpcTest, SetRpcClientDebug) {
1271 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001272 ASSERT_TRUE(binder != nullptr);
1273 auto [socket, port] = CreateSocket();
1274 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001275 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), sp<BBinder>::make()),
1276 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001277}
1278
Yifan Hong8b890852021-06-10 13:44:09 -07001279// Tests for multiple RpcServer's on the same binder object.
1280TEST_F(BinderLibRpcTest, SetRpcClientDebugTwice) {
1281 auto binder = addServer();
Yifan Hong84bedeb2021-04-21 21:37:17 -07001282 ASSERT_TRUE(binder != nullptr);
1283
1284 auto [socket1, port1] = CreateSocket();
1285 ASSERT_TRUE(socket1.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001286 auto keepAliveBinder1 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001287 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket1), keepAliveBinder1),
1288 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001289
1290 auto [socket2, port2] = CreateSocket();
1291 ASSERT_TRUE(socket2.ok());
Yifan Hong02530ec2021-06-10 13:38:38 -07001292 auto keepAliveBinder2 = sp<BBinder>::make();
Yifan Hongbd276552022-02-28 15:28:51 -08001293 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket2), keepAliveBinder2),
1294 Debuggable(StatusEq(OK)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001295}
1296
Yifan Hong8b890852021-06-10 13:44:09 -07001297// Negative tests for RPC APIs on IBinder. Call should fail in the same way on both remote and
1298// local binders.
1299class BinderLibRpcTestP : public BinderLibRpcTestBase, public WithParamInterface<bool> {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001300public:
1301 sp<IBinder> GetService() {
1302 return GetParam() ? sp<IBinder>(addServer()) : sp<IBinder>(sp<BBinder>::make());
1303 }
1304 static std::string ParamToString(const testing::TestParamInfo<ParamType> &info) {
1305 return info.param ? "remote" : "local";
1306 }
1307};
1308
Yifan Hong8b890852021-06-10 13:44:09 -07001309TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoFd) {
1310 auto binder = GetService();
1311 ASSERT_TRUE(binder != nullptr);
1312 EXPECT_THAT(binder->setRpcClientDebug(android::base::unique_fd(), sp<BBinder>::make()),
Yifan Hongbd276552022-02-28 15:28:51 -08001313 Debuggable(StatusEq(BAD_VALUE)));
Yifan Hong8b890852021-06-10 13:44:09 -07001314}
1315
1316TEST_P(BinderLibRpcTestP, SetRpcClientDebugNoKeepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001317 auto binder = GetService();
1318 ASSERT_TRUE(binder != nullptr);
1319 auto [socket, port] = CreateSocket();
1320 ASSERT_TRUE(socket.ok());
Yifan Hongbd276552022-02-28 15:28:51 -08001321 EXPECT_THAT(binder->setRpcClientDebug(std::move(socket), nullptr),
1322 Debuggable(StatusEq(UNEXPECTED_NULL)));
Yifan Hong84bedeb2021-04-21 21:37:17 -07001323}
Yifan Hong8b890852021-06-10 13:44:09 -07001324INSTANTIATE_TEST_CASE_P(BinderLibTest, BinderLibRpcTestP, testing::Bool(),
1325 BinderLibRpcTestP::ParamToString);
Yifan Hong84bedeb2021-04-21 21:37:17 -07001326
Yifan Hong543edcd2021-05-18 19:47:30 -07001327class BinderLibTestService : public BBinder {
1328public:
Yifan Hong84bedeb2021-04-21 21:37:17 -07001329 explicit BinderLibTestService(int32_t id, bool exitOnDestroy = true)
1330 : m_id(id),
1331 m_nextServerId(id + 1),
1332 m_serverStartRequested(false),
1333 m_callback(nullptr),
1334 m_exitOnDestroy(exitOnDestroy) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001335 pthread_mutex_init(&m_serverWaitMutex, nullptr);
1336 pthread_cond_init(&m_serverWaitCond, nullptr);
1337 }
Yifan Hong84bedeb2021-04-21 21:37:17 -07001338 ~BinderLibTestService() {
1339 if (m_exitOnDestroy) exit(EXIT_SUCCESS);
1340 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001341
Yifan Hong543edcd2021-05-18 19:47:30 -07001342 void processPendingCall() {
1343 if (m_callback != nullptr) {
1344 Parcel data;
1345 data.writeInt32(NO_ERROR);
1346 m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
1347 m_callback = nullptr;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001348 }
Yifan Hong543edcd2021-05-18 19:47:30 -07001349 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001350
Yifan Hong543edcd2021-05-18 19:47:30 -07001351 virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply,
1352 uint32_t flags = 0) {
Yifan Hong84bedeb2021-04-21 21:37:17 -07001353 // TODO(b/182914638): also checks getCallingUid() for RPC
1354 if (!data.isForRpc() && getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
Yifan Hong543edcd2021-05-18 19:47:30 -07001355 return PERMISSION_DENIED;
1356 }
1357 switch (code) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001358 case BINDER_LIB_TEST_REGISTER_SERVER: {
1359 int32_t id;
1360 sp<IBinder> binder;
1361 id = data.readInt32();
1362 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001363 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001364 return BAD_VALUE;
1365 }
1366
Yifan Hong543edcd2021-05-18 19:47:30 -07001367 if (m_id != 0) return INVALID_OPERATION;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001368
1369 pthread_mutex_lock(&m_serverWaitMutex);
1370 if (m_serverStartRequested) {
1371 m_serverStartRequested = false;
1372 m_serverStarted = binder;
1373 pthread_cond_signal(&m_serverWaitCond);
1374 }
1375 pthread_mutex_unlock(&m_serverWaitMutex);
1376 return NO_ERROR;
1377 }
Martijn Coenen45b07b42017-08-09 12:07:45 +02001378 case BINDER_LIB_TEST_ADD_POLL_SERVER:
Riley Andrews06b01ad2014-12-18 12:10:08 -08001379 case BINDER_LIB_TEST_ADD_SERVER: {
1380 int ret;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001381 int serverid;
1382
1383 if (m_id != 0) {
1384 return INVALID_OPERATION;
1385 }
1386 pthread_mutex_lock(&m_serverWaitMutex);
1387 if (m_serverStartRequested) {
1388 ret = -EBUSY;
1389 } else {
1390 serverid = m_nextServerId++;
1391 m_serverStartRequested = true;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001392 bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001393
1394 pthread_mutex_unlock(&m_serverWaitMutex);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001395 ret = start_server_process(serverid, usePoll);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001396 pthread_mutex_lock(&m_serverWaitMutex);
1397 }
1398 if (ret > 0) {
1399 if (m_serverStartRequested) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001400 struct timespec ts;
1401 clock_gettime(CLOCK_REALTIME, &ts);
1402 ts.tv_sec += 5;
1403 ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001404 }
1405 if (m_serverStartRequested) {
1406 m_serverStartRequested = false;
1407 ret = -ETIMEDOUT;
1408 } else {
1409 reply->writeStrongBinder(m_serverStarted);
1410 reply->writeInt32(serverid);
Yi Kong91635562018-06-07 14:38:36 -07001411 m_serverStarted = nullptr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001412 ret = NO_ERROR;
1413 }
1414 } else if (ret >= 0) {
1415 m_serverStartRequested = false;
1416 ret = UNKNOWN_ERROR;
1417 }
1418 pthread_mutex_unlock(&m_serverWaitMutex);
1419 return ret;
1420 }
Steven Moreland35626652021-05-15 01:32:04 +00001421 case BINDER_LIB_TEST_USE_CALLING_GUARD_TRANSACTION: {
1422 IPCThreadState::SpGuard spGuard{
1423 .address = __builtin_frame_address(0),
1424 .context = "GuardInBinderTransaction",
1425 };
1426 const IPCThreadState::SpGuard *origGuard =
1427 IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1428
1429 // if the guard works, this should abort
1430 (void)IPCThreadState::self()->getCallingPid();
1431
1432 IPCThreadState::self()->restoreGetCallingSpGuard(origGuard);
1433 return NO_ERROR;
1434 }
1435
Marco Ballesio7ee17572020-09-08 10:30:03 -07001436 case BINDER_LIB_TEST_GETPID:
1437 reply->writeInt32(getpid());
1438 return NO_ERROR;
1439 case BINDER_LIB_TEST_NOP_TRANSACTION_WAIT:
1440 usleep(5000);
Steven Moreland80844f72020-12-12 02:06:08 +00001441 [[fallthrough]];
Riley Andrews06b01ad2014-12-18 12:10:08 -08001442 case BINDER_LIB_TEST_NOP_TRANSACTION:
Steven Moreland80844f72020-12-12 02:06:08 +00001443 // oneway error codes should be ignored
1444 if (flags & TF_ONE_WAY) {
1445 return UNKNOWN_ERROR;
1446 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001447 return NO_ERROR;
Martijn Coenen45b07b42017-08-09 12:07:45 +02001448 case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
1449 // Note: this transaction is only designed for use with a
1450 // poll() server. See comments around epoll_wait().
Yi Kong91635562018-06-07 14:38:36 -07001451 if (m_callback != nullptr) {
Martijn Coenen45b07b42017-08-09 12:07:45 +02001452 // A callback was already pending; this means that
1453 // we received a second call while still processing
1454 // the first one. Fail the test.
1455 sp<IBinder> callback = data.readStrongBinder();
1456 Parcel data2;
1457 data2.writeInt32(UNKNOWN_ERROR);
1458
Yi Kong91635562018-06-07 14:38:36 -07001459 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, nullptr, TF_ONE_WAY);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001460 } else {
1461 m_callback = data.readStrongBinder();
1462 int32_t delayUs = data.readInt32();
1463 /*
1464 * It's necessary that we sleep here, so the next
1465 * transaction the caller makes will be queued to
1466 * the async queue.
1467 */
1468 usleep(delayUs);
1469
1470 /*
1471 * Now when we return, libbinder will tell the kernel
1472 * we are done with this transaction, and the kernel
1473 * can move the queued transaction to either the
1474 * thread todo worklist (for kernels without the fix),
1475 * or the proc todo worklist. In case of the former,
1476 * the next outbound call will pick up the pending
1477 * transaction, which leads to undesired reentrant
1478 * behavior. This is caught in the if() branch above.
1479 */
1480 }
1481
1482 return NO_ERROR;
1483 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001484 case BINDER_LIB_TEST_NOP_CALL_BACK: {
1485 Parcel data2, reply2;
1486 sp<IBinder> binder;
1487 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001488 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001489 return BAD_VALUE;
1490 }
Martijn Coenenfb368f72017-08-10 15:03:18 +02001491 data2.writeInt32(NO_ERROR);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001492 binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1493 return NO_ERROR;
1494 }
Arve Hjønnevåg70604312016-08-12 15:34:51 -07001495 case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
1496 reply->writeStrongBinder(this);
1497 return NO_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001498 case BINDER_LIB_TEST_GET_ID_TRANSACTION:
1499 reply->writeInt32(m_id);
1500 return NO_ERROR;
1501 case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
1502 int32_t count;
1503 uint32_t indirect_code;
1504 sp<IBinder> binder;
1505
1506 count = data.readInt32();
1507 reply->writeInt32(m_id);
1508 reply->writeInt32(count);
1509 for (int i = 0; i < count; i++) {
1510 binder = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001511 if (binder == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001512 return BAD_VALUE;
1513 }
1514 indirect_code = data.readInt32();
1515 BinderLibTestBundle data2(&data);
1516 if (!data2.isValid()) {
1517 return BAD_VALUE;
1518 }
1519 BinderLibTestBundle reply2;
1520 binder->transact(indirect_code, data2, &reply2);
1521 reply2.appendTo(reply);
1522 }
1523 return NO_ERROR;
1524 }
1525 case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
1526 reply->setError(data.readInt32());
1527 return NO_ERROR;
1528 case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
1529 reply->writeInt32(sizeof(void *));
1530 return NO_ERROR;
1531 case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
1532 return NO_ERROR;
1533 case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
1534 m_strongRef = data.readStrongBinder();
1535 return NO_ERROR;
1536 case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
1537 int ret;
1538 Parcel data2, reply2;
1539 sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
1540 sp<IBinder> target;
1541 sp<IBinder> callback;
1542
1543 target = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001544 if (target == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001545 return BAD_VALUE;
1546 }
1547 callback = data.readStrongBinder();
Yi Kong91635562018-06-07 14:38:36 -07001548 if (callback == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001549 return BAD_VALUE;
1550 }
1551 ret = target->linkToDeath(testDeathRecipient);
Yifan Hong543edcd2021-05-18 19:47:30 -07001552 if (ret == NO_ERROR) ret = testDeathRecipient->waitEvent(5);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001553 data2.writeInt32(ret);
1554 callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
1555 return NO_ERROR;
1556 }
1557 case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
1558 int ret;
1559 int32_t size;
1560 const void *buf;
1561 int fd;
1562
1563 fd = data.readFileDescriptor();
1564 if (fd < 0) {
1565 return BAD_VALUE;
1566 }
1567 ret = data.readInt32(&size);
1568 if (ret != NO_ERROR) {
1569 return ret;
1570 }
1571 buf = data.readInplace(size);
Yi Kong91635562018-06-07 14:38:36 -07001572 if (buf == nullptr) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001573 return BAD_VALUE;
1574 }
1575 ret = write(fd, buf, size);
Yifan Hong543edcd2021-05-18 19:47:30 -07001576 if (ret != size) return UNKNOWN_ERROR;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001577 return NO_ERROR;
1578 }
Ryo Hashimotobf551892018-05-31 16:58:35 +09001579 case BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION: {
1580 int ret;
1581 int32_t size;
1582 const void *buf;
1583 android::base::unique_fd fd;
1584
1585 ret = data.readUniqueParcelFileDescriptor(&fd);
1586 if (ret != NO_ERROR) {
1587 return ret;
1588 }
1589 ret = data.readInt32(&size);
1590 if (ret != NO_ERROR) {
1591 return ret;
1592 }
1593 buf = data.readInplace(size);
Yi Kong0cf75842018-07-10 11:44:36 -07001594 if (buf == nullptr) {
Ryo Hashimotobf551892018-05-31 16:58:35 +09001595 return BAD_VALUE;
1596 }
1597 ret = write(fd.get(), buf, size);
1598 if (ret != size) return UNKNOWN_ERROR;
1599 return NO_ERROR;
1600 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001601 case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
1602 alarm(10);
1603 return NO_ERROR;
1604 case BINDER_LIB_TEST_EXIT_TRANSACTION:
Yi Kong91635562018-06-07 14:38:36 -07001605 while (wait(nullptr) != -1 || errno != ECHILD)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001606 ;
1607 exit(EXIT_SUCCESS);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001608 case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
Connor O'Brien52be2c92016-09-20 14:18:08 -07001609 sp<IBinder> binder = new BBinder();
Steven Morelande171d622019-07-17 16:06:01 -07001610 reply->writeStrongBinder(binder);
Connor O'Brien52be2c92016-09-20 14:18:08 -07001611 return NO_ERROR;
1612 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001613 case BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION: {
1614 data.enforceInterface(binderLibTestServiceName);
Olivier Gaillarda8e7bf22018-11-14 15:35:50 +00001615 reply->writeInt32(IPCThreadState::self()->getCallingWorkSourceUid());
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001616 return NO_ERROR;
1617 }
Steven Morelandbf1915b2020-07-16 22:43:02 +00001618 case BINDER_LIB_TEST_GET_SCHEDULING_POLICY: {
1619 int policy = 0;
1620 sched_param param;
1621 if (0 != pthread_getschedparam(pthread_self(), &policy, &param)) {
1622 return UNKNOWN_ERROR;
1623 }
1624 reply->writeInt32(policy);
1625 reply->writeInt32(param.sched_priority);
1626 return NO_ERROR;
1627 }
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001628 case BINDER_LIB_TEST_ECHO_VECTOR: {
1629 std::vector<uint64_t> vector;
1630 auto err = data.readUint64Vector(&vector);
Yifan Hong543edcd2021-05-18 19:47:30 -07001631 if (err != NO_ERROR) return err;
Kevin DuBois2f82d5b2018-12-05 12:56:10 -08001632 reply->writeUint64Vector(vector);
1633 return NO_ERROR;
1634 }
Steven Morelandf2e0a952021-11-01 18:17:23 -07001635 case BINDER_LIB_TEST_REJECT_OBJECTS: {
Martijn Coenen82c75312019-07-24 15:18:30 +02001636 return data.objectsCount() == 0 ? BAD_VALUE : NO_ERROR;
1637 }
Steven Moreland254e8ef2021-04-19 22:28:50 +00001638 case BINDER_LIB_TEST_CAN_GET_SID: {
1639 return IPCThreadState::self()->getCallingSid() == nullptr ? BAD_VALUE : NO_ERROR;
1640 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001641 default:
1642 return UNKNOWN_TRANSACTION;
Yifan Hong543edcd2021-05-18 19:47:30 -07001643 };
1644 }
1645
1646private:
1647 int32_t m_id;
1648 int32_t m_nextServerId;
1649 pthread_mutex_t m_serverWaitMutex;
1650 pthread_cond_t m_serverWaitCond;
1651 bool m_serverStartRequested;
1652 sp<IBinder> m_serverStarted;
1653 sp<IBinder> m_strongRef;
1654 sp<IBinder> m_callback;
Yifan Hong84bedeb2021-04-21 21:37:17 -07001655 bool m_exitOnDestroy;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001656};
1657
Martijn Coenen45b07b42017-08-09 12:07:45 +02001658int run_server(int index, int readypipefd, bool usePoll)
Riley Andrews06b01ad2014-12-18 12:10:08 -08001659{
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001660 binderLibTestServiceName += String16(binderserversuffix);
1661
Steven Moreland35626652021-05-15 01:32:04 +00001662 // Testing to make sure that calls that we are serving can use getCallin*
1663 // even though we don't here.
1664 IPCThreadState::SpGuard spGuard{
1665 .address = __builtin_frame_address(0),
1666 .context = "main server thread",
1667 };
1668 (void)IPCThreadState::self()->pushGetCallingSpGuard(&spGuard);
1669
Riley Andrews06b01ad2014-12-18 12:10:08 -08001670 status_t ret;
1671 sp<IServiceManager> sm = defaultServiceManager();
Martijn Coenen45b07b42017-08-09 12:07:45 +02001672 BinderLibTestService* testServicePtr;
Riley Andrews06b01ad2014-12-18 12:10:08 -08001673 {
1674 sp<BinderLibTestService> testService = new BinderLibTestService(index);
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001675
Steven Morelandbf1915b2020-07-16 22:43:02 +00001676 testService->setMinSchedulerPolicy(kSchedPolicy, kSchedPriority);
1677
Steven Morelandcf03cf12020-12-04 02:58:40 +00001678 testService->setInheritRt(true);
1679
Steven Morelandb8ad08d2019-08-09 14:42:56 -07001680 /*
1681 * Normally would also contain functionality as well, but we are only
1682 * testing the extension mechanism.
1683 */
1684 testService->setExtension(new BBinder());
1685
Martijn Coenen82c75312019-07-24 15:18:30 +02001686 // Required for test "BufRejected'
1687 testService->setRequestingSid(true);
1688
Martijn Coenen45b07b42017-08-09 12:07:45 +02001689 /*
1690 * We need this below, but can't hold a sp<> because it prevents the
1691 * node from being cleaned up automatically. It's safe in this case
1692 * because of how the tests are written.
1693 */
1694 testServicePtr = testService.get();
1695
Riley Andrews06b01ad2014-12-18 12:10:08 -08001696 if (index == 0) {
1697 ret = sm->addService(binderLibTestServiceName, testService);
1698 } else {
1699 sp<IBinder> server = sm->getService(binderLibTestServiceName);
1700 Parcel data, reply;
1701 data.writeInt32(index);
1702 data.writeStrongBinder(testService);
1703
1704 ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
1705 }
1706 }
1707 write(readypipefd, &ret, sizeof(ret));
1708 close(readypipefd);
1709 //printf("%s: ret %d\n", __func__, ret);
1710 if (ret)
1711 return 1;
1712 //printf("%s: joinThreadPool\n", __func__);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001713 if (usePoll) {
1714 int fd;
1715 struct epoll_event ev;
1716 int epoll_fd;
1717 IPCThreadState::self()->setupPolling(&fd);
1718 if (fd < 0) {
1719 return 1;
1720 }
1721 IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
1722
Nick Kralevichfcf1b2b2018-12-15 11:59:30 -08001723 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Martijn Coenen45b07b42017-08-09 12:07:45 +02001724 if (epoll_fd == -1) {
1725 return 1;
1726 }
1727
1728 ev.events = EPOLLIN;
1729 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
1730 return 1;
1731 }
1732
1733 while (1) {
1734 /*
1735 * We simulate a single-threaded process using the binder poll
1736 * interface; besides handling binder commands, it can also
1737 * issue outgoing transactions, by storing a callback in
Steven Moreland573adc12019-07-17 13:29:06 -07001738 * m_callback.
Martijn Coenen45b07b42017-08-09 12:07:45 +02001739 *
1740 * processPendingCall() will then issue that transaction.
1741 */
1742 struct epoll_event events[1];
1743 int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
1744 if (numEvents < 0) {
1745 if (errno == EINTR) {
1746 continue;
1747 }
1748 return 1;
1749 }
1750 if (numEvents > 0) {
1751 IPCThreadState::self()->handlePolledCommands();
1752 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
1753 testServicePtr->processPendingCall();
1754 }
1755 }
1756 } else {
1757 ProcessState::self()->startThreadPool();
1758 IPCThreadState::self()->joinThreadPool();
1759 }
Riley Andrews06b01ad2014-12-18 12:10:08 -08001760 //printf("%s: joinThreadPool returned\n", __func__);
1761 return 1; /* joinThreadPool should not return */
1762}
1763
1764int main(int argc, char **argv) {
Steven Morelandf9f3de22020-05-06 17:14:39 -07001765 ExitIfWrongAbi();
1766
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001767 if (argc == 4 && !strcmp(argv[1], "--servername")) {
Riley Andrews06b01ad2014-12-18 12:10:08 -08001768 binderservername = argv[2];
1769 } else {
1770 binderservername = argv[0];
1771 }
1772
Martijn Coenen45b07b42017-08-09 12:07:45 +02001773 if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
1774 binderserversuffix = argv[5];
1775 return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001776 }
Connor O'Brien87c03cf2016-10-26 17:58:51 -07001777 binderserversuffix = new char[16];
1778 snprintf(binderserversuffix, 16, "%d", getpid());
1779 binderLibTestServiceName += String16(binderserversuffix);
Riley Andrews06b01ad2014-12-18 12:10:08 -08001780
1781 ::testing::InitGoogleTest(&argc, argv);
1782 binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
1783 ProcessState::self()->startThreadPool();
1784 return RUN_ALL_TESTS();
1785}