Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <poll.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <binder/Binder.h> |
| 27 | #include <binder/IBinder.h> |
| 28 | #include <binder/IPCThreadState.h> |
| 29 | #include <binder/IServiceManager.h> |
| 30 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 31 | #include <sys/epoll.h> |
| 32 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 33 | #define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) |
| 34 | |
| 35 | using namespace android; |
| 36 | |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 37 | static ::testing::AssertionResult IsPageAligned(void *buf) { |
| 38 | if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0) |
| 39 | return ::testing::AssertionSuccess(); |
| 40 | else |
| 41 | return ::testing::AssertionFailure() << buf << " is not page aligned"; |
| 42 | } |
| 43 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 44 | static testing::Environment* binder_env; |
| 45 | static char *binderservername; |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 46 | static char *binderserversuffix; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 47 | static char binderserverarg[] = "--binderserver"; |
| 48 | |
| 49 | static String16 binderLibTestServiceName = String16("test.binderLib"); |
| 50 | |
| 51 | enum BinderLibTestTranscationCode { |
| 52 | BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION, |
| 53 | BINDER_LIB_TEST_REGISTER_SERVER, |
| 54 | BINDER_LIB_TEST_ADD_SERVER, |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 55 | BINDER_LIB_TEST_ADD_POLL_SERVER, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 56 | BINDER_LIB_TEST_CALL_BACK, |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 57 | BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF, |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 58 | BINDER_LIB_TEST_DELAYED_CALL_BACK, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 59 | BINDER_LIB_TEST_NOP_CALL_BACK, |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 60 | BINDER_LIB_TEST_GET_SELF_TRANSACTION, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 61 | BINDER_LIB_TEST_GET_ID_TRANSACTION, |
| 62 | BINDER_LIB_TEST_INDIRECT_TRANSACTION, |
| 63 | BINDER_LIB_TEST_SET_ERROR_TRANSACTION, |
| 64 | BINDER_LIB_TEST_GET_STATUS_TRANSACTION, |
| 65 | BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, |
| 66 | BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, |
| 67 | BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, |
| 68 | BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, |
| 69 | BINDER_LIB_TEST_EXIT_TRANSACTION, |
| 70 | BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION, |
| 71 | BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 72 | BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 75 | pid_t start_server_process(int arg2, bool usePoll = false) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 76 | { |
| 77 | int ret; |
| 78 | pid_t pid; |
| 79 | status_t status; |
| 80 | int pipefd[2]; |
| 81 | char stri[16]; |
| 82 | char strpipefd1[16]; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 83 | char usepoll[2]; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 84 | char *childargv[] = { |
| 85 | binderservername, |
| 86 | binderserverarg, |
| 87 | stri, |
| 88 | strpipefd1, |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 89 | usepoll, |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 90 | binderserversuffix, |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 91 | NULL |
| 92 | }; |
| 93 | |
| 94 | ret = pipe(pipefd); |
| 95 | if (ret < 0) |
| 96 | return ret; |
| 97 | |
| 98 | snprintf(stri, sizeof(stri), "%d", arg2); |
| 99 | snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 100 | snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 101 | |
| 102 | pid = fork(); |
| 103 | if (pid == -1) |
| 104 | return pid; |
| 105 | if (pid == 0) { |
| 106 | close(pipefd[0]); |
| 107 | execv(binderservername, childargv); |
| 108 | status = -errno; |
| 109 | write(pipefd[1], &status, sizeof(status)); |
| 110 | fprintf(stderr, "execv failed, %s\n", strerror(errno)); |
| 111 | _exit(EXIT_FAILURE); |
| 112 | } |
| 113 | close(pipefd[1]); |
| 114 | ret = read(pipefd[0], &status, sizeof(status)); |
| 115 | //printf("pipe read returned %d, status %d\n", ret, status); |
| 116 | close(pipefd[0]); |
| 117 | if (ret == sizeof(status)) { |
| 118 | ret = status; |
| 119 | } else { |
| 120 | kill(pid, SIGKILL); |
| 121 | if (ret >= 0) { |
| 122 | ret = NO_INIT; |
| 123 | } |
| 124 | } |
| 125 | if (ret < 0) { |
| 126 | wait(NULL); |
| 127 | return ret; |
| 128 | } |
| 129 | return pid; |
| 130 | } |
| 131 | |
| 132 | class BinderLibTestEnv : public ::testing::Environment { |
| 133 | public: |
| 134 | BinderLibTestEnv() {} |
| 135 | sp<IBinder> getServer(void) { |
| 136 | return m_server; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | virtual void SetUp() { |
| 141 | m_serverpid = start_server_process(0); |
| 142 | //printf("m_serverpid %d\n", m_serverpid); |
| 143 | ASSERT_GT(m_serverpid, 0); |
| 144 | |
| 145 | sp<IServiceManager> sm = defaultServiceManager(); |
| 146 | //printf("%s: pid %d, get service\n", __func__, m_pid); |
| 147 | m_server = sm->getService(binderLibTestServiceName); |
| 148 | ASSERT_TRUE(m_server != NULL); |
| 149 | //printf("%s: pid %d, get service done\n", __func__, m_pid); |
| 150 | } |
| 151 | virtual void TearDown() { |
| 152 | status_t ret; |
| 153 | Parcel data, reply; |
| 154 | int exitStatus; |
| 155 | pid_t pid; |
| 156 | |
| 157 | //printf("%s: pid %d\n", __func__, m_pid); |
| 158 | if (m_server != NULL) { |
| 159 | ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply); |
| 160 | EXPECT_EQ(0, ret); |
| 161 | ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 162 | EXPECT_EQ(0, ret); |
| 163 | } |
| 164 | if (m_serverpid > 0) { |
| 165 | //printf("wait for %d\n", m_pids[i]); |
| 166 | pid = wait(&exitStatus); |
| 167 | EXPECT_EQ(m_serverpid, pid); |
| 168 | EXPECT_TRUE(WIFEXITED(exitStatus)); |
| 169 | EXPECT_EQ(0, WEXITSTATUS(exitStatus)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | pid_t m_serverpid; |
| 174 | sp<IBinder> m_server; |
| 175 | }; |
| 176 | |
| 177 | class BinderLibTest : public ::testing::Test { |
| 178 | public: |
| 179 | virtual void SetUp() { |
| 180 | m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer(); |
| 181 | } |
| 182 | virtual void TearDown() { |
| 183 | } |
| 184 | protected: |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 185 | sp<IBinder> addServerEtc(int32_t *idPtr, int code) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 186 | { |
| 187 | int ret; |
| 188 | int32_t id; |
| 189 | Parcel data, reply; |
| 190 | sp<IBinder> binder; |
| 191 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 192 | ret = m_server->transact(code, data, &reply); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 193 | EXPECT_EQ(NO_ERROR, ret); |
| 194 | |
| 195 | EXPECT_FALSE(binder != NULL); |
| 196 | binder = reply.readStrongBinder(); |
| 197 | EXPECT_TRUE(binder != NULL); |
| 198 | ret = reply.readInt32(&id); |
| 199 | EXPECT_EQ(NO_ERROR, ret); |
| 200 | if (idPtr) |
| 201 | *idPtr = id; |
| 202 | return binder; |
| 203 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 204 | |
| 205 | sp<IBinder> addServer(int32_t *idPtr = NULL) |
| 206 | { |
| 207 | return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER); |
| 208 | } |
| 209 | |
| 210 | sp<IBinder> addPollServer(int32_t *idPtr = NULL) |
| 211 | { |
| 212 | return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER); |
| 213 | } |
| 214 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 215 | void waitForReadData(int fd, int timeout_ms) { |
| 216 | int ret; |
| 217 | pollfd pfd = pollfd(); |
| 218 | |
| 219 | pfd.fd = fd; |
| 220 | pfd.events = POLLIN; |
| 221 | ret = poll(&pfd, 1, timeout_ms); |
| 222 | EXPECT_EQ(1, ret); |
| 223 | } |
| 224 | |
| 225 | sp<IBinder> m_server; |
| 226 | }; |
| 227 | |
| 228 | class BinderLibTestBundle : public Parcel |
| 229 | { |
| 230 | public: |
| 231 | BinderLibTestBundle(void) {} |
| 232 | BinderLibTestBundle(const Parcel *source) : m_isValid(false) { |
| 233 | int32_t mark; |
| 234 | int32_t bundleLen; |
| 235 | size_t pos; |
| 236 | |
| 237 | if (source->readInt32(&mark)) |
| 238 | return; |
| 239 | if (mark != MARK_START) |
| 240 | return; |
| 241 | if (source->readInt32(&bundleLen)) |
| 242 | return; |
| 243 | pos = source->dataPosition(); |
| 244 | if (Parcel::appendFrom(source, pos, bundleLen)) |
| 245 | return; |
| 246 | source->setDataPosition(pos + bundleLen); |
| 247 | if (source->readInt32(&mark)) |
| 248 | return; |
| 249 | if (mark != MARK_END) |
| 250 | return; |
| 251 | m_isValid = true; |
| 252 | setDataPosition(0); |
| 253 | } |
| 254 | void appendTo(Parcel *dest) { |
| 255 | dest->writeInt32(MARK_START); |
| 256 | dest->writeInt32(dataSize()); |
| 257 | dest->appendFrom(this, 0, dataSize()); |
| 258 | dest->writeInt32(MARK_END); |
| 259 | }; |
| 260 | bool isValid(void) { |
| 261 | return m_isValid; |
| 262 | } |
| 263 | private: |
| 264 | enum { |
| 265 | MARK_START = B_PACK_CHARS('B','T','B','S'), |
| 266 | MARK_END = B_PACK_CHARS('B','T','B','E'), |
| 267 | }; |
| 268 | bool m_isValid; |
| 269 | }; |
| 270 | |
| 271 | class BinderLibTestEvent |
| 272 | { |
| 273 | public: |
| 274 | BinderLibTestEvent(void) |
| 275 | : m_eventTriggered(false) |
| 276 | { |
| 277 | pthread_mutex_init(&m_waitMutex, NULL); |
| 278 | pthread_cond_init(&m_waitCond, NULL); |
| 279 | } |
| 280 | int waitEvent(int timeout_s) |
| 281 | { |
| 282 | int ret; |
| 283 | pthread_mutex_lock(&m_waitMutex); |
| 284 | if (!m_eventTriggered) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 285 | struct timespec ts; |
| 286 | clock_gettime(CLOCK_REALTIME, &ts); |
| 287 | ts.tv_sec += timeout_s; |
| 288 | pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 289 | } |
| 290 | ret = m_eventTriggered ? NO_ERROR : TIMED_OUT; |
| 291 | pthread_mutex_unlock(&m_waitMutex); |
| 292 | return ret; |
| 293 | } |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 294 | pthread_t getTriggeringThread() |
| 295 | { |
| 296 | return m_triggeringThread; |
| 297 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 298 | protected: |
| 299 | void triggerEvent(void) { |
| 300 | pthread_mutex_lock(&m_waitMutex); |
| 301 | pthread_cond_signal(&m_waitCond); |
| 302 | m_eventTriggered = true; |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 303 | m_triggeringThread = pthread_self(); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 304 | pthread_mutex_unlock(&m_waitMutex); |
| 305 | }; |
| 306 | private: |
| 307 | pthread_mutex_t m_waitMutex; |
| 308 | pthread_cond_t m_waitCond; |
| 309 | bool m_eventTriggered; |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 310 | pthread_t m_triggeringThread; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent |
| 314 | { |
| 315 | public: |
| 316 | BinderLibTestCallBack() |
| 317 | : m_result(NOT_ENOUGH_DATA) |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 318 | , m_prev_end(NULL) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 319 | { |
| 320 | } |
| 321 | status_t getResult(void) |
| 322 | { |
| 323 | return m_result; |
| 324 | } |
| 325 | |
| 326 | private: |
| 327 | virtual status_t onTransact(uint32_t code, |
| 328 | const Parcel& data, Parcel* reply, |
| 329 | uint32_t flags = 0) |
| 330 | { |
| 331 | (void)reply; |
| 332 | (void)flags; |
| 333 | switch(code) { |
| 334 | case BINDER_LIB_TEST_CALL_BACK: |
| 335 | m_result = data.readInt32(); |
| 336 | triggerEvent(); |
| 337 | return NO_ERROR; |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 338 | case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: { |
| 339 | sp<IBinder> server; |
| 340 | int ret; |
| 341 | const uint8_t *buf = data.data(); |
| 342 | size_t size = data.dataSize(); |
| 343 | if (m_prev_end) { |
| 344 | /* 64-bit kernel needs at most 8 bytes to align buffer end */ |
| 345 | EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8); |
| 346 | } else { |
| 347 | EXPECT_TRUE(IsPageAligned((void *)buf)); |
| 348 | } |
| 349 | |
| 350 | m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t); |
| 351 | |
| 352 | if (size > 0) { |
| 353 | server = static_cast<BinderLibTestEnv *>(binder_env)->getServer(); |
| 354 | ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, |
| 355 | data, reply); |
| 356 | EXPECT_EQ(NO_ERROR, ret); |
| 357 | } |
| 358 | return NO_ERROR; |
| 359 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 360 | default: |
| 361 | return UNKNOWN_TRANSACTION; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | status_t m_result; |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 366 | const uint8_t *m_prev_end; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent |
| 370 | { |
| 371 | private: |
| 372 | virtual void binderDied(const wp<IBinder>& who) { |
| 373 | (void)who; |
| 374 | triggerEvent(); |
| 375 | }; |
| 376 | }; |
| 377 | |
| 378 | TEST_F(BinderLibTest, NopTransaction) { |
| 379 | status_t ret; |
| 380 | Parcel data, reply; |
| 381 | ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply); |
| 382 | EXPECT_EQ(NO_ERROR, ret); |
| 383 | } |
| 384 | |
| 385 | TEST_F(BinderLibTest, SetError) { |
| 386 | int32_t testValue[] = { 0, -123, 123 }; |
| 387 | for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) { |
| 388 | status_t ret; |
| 389 | Parcel data, reply; |
| 390 | data.writeInt32(testValue[i]); |
| 391 | ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply); |
| 392 | EXPECT_EQ(testValue[i], ret); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | TEST_F(BinderLibTest, GetId) { |
| 397 | status_t ret; |
| 398 | int32_t id; |
| 399 | Parcel data, reply; |
| 400 | ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply); |
| 401 | EXPECT_EQ(NO_ERROR, ret); |
| 402 | ret = reply.readInt32(&id); |
| 403 | EXPECT_EQ(NO_ERROR, ret); |
| 404 | EXPECT_EQ(0, id); |
| 405 | } |
| 406 | |
| 407 | TEST_F(BinderLibTest, PtrSize) { |
| 408 | status_t ret; |
| 409 | int32_t ptrsize; |
| 410 | Parcel data, reply; |
| 411 | sp<IBinder> server = addServer(); |
| 412 | ASSERT_TRUE(server != NULL); |
| 413 | ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply); |
| 414 | EXPECT_EQ(NO_ERROR, ret); |
| 415 | ret = reply.readInt32(&ptrsize); |
| 416 | EXPECT_EQ(NO_ERROR, ret); |
| 417 | RecordProperty("TestPtrSize", sizeof(void *)); |
| 418 | RecordProperty("ServerPtrSize", sizeof(void *)); |
| 419 | } |
| 420 | |
| 421 | TEST_F(BinderLibTest, IndirectGetId2) |
| 422 | { |
| 423 | status_t ret; |
| 424 | int32_t id; |
| 425 | int32_t count; |
| 426 | Parcel data, reply; |
| 427 | int32_t serverId[3]; |
| 428 | |
| 429 | data.writeInt32(ARRAY_SIZE(serverId)); |
| 430 | for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) { |
| 431 | sp<IBinder> server; |
| 432 | BinderLibTestBundle datai; |
| 433 | |
| 434 | server = addServer(&serverId[i]); |
| 435 | ASSERT_TRUE(server != NULL); |
| 436 | data.writeStrongBinder(server); |
| 437 | data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION); |
| 438 | datai.appendTo(&data); |
| 439 | } |
| 440 | |
| 441 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 442 | ASSERT_EQ(NO_ERROR, ret); |
| 443 | |
| 444 | ret = reply.readInt32(&id); |
| 445 | ASSERT_EQ(NO_ERROR, ret); |
| 446 | EXPECT_EQ(0, id); |
| 447 | |
| 448 | ret = reply.readInt32(&count); |
| 449 | ASSERT_EQ(NO_ERROR, ret); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 450 | EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 451 | |
| 452 | for (size_t i = 0; i < (size_t)count; i++) { |
| 453 | BinderLibTestBundle replyi(&reply); |
| 454 | EXPECT_TRUE(replyi.isValid()); |
| 455 | ret = replyi.readInt32(&id); |
| 456 | EXPECT_EQ(NO_ERROR, ret); |
| 457 | EXPECT_EQ(serverId[i], id); |
| 458 | EXPECT_EQ(replyi.dataSize(), replyi.dataPosition()); |
| 459 | } |
| 460 | |
| 461 | EXPECT_EQ(reply.dataSize(), reply.dataPosition()); |
| 462 | } |
| 463 | |
| 464 | TEST_F(BinderLibTest, IndirectGetId3) |
| 465 | { |
| 466 | status_t ret; |
| 467 | int32_t id; |
| 468 | int32_t count; |
| 469 | Parcel data, reply; |
| 470 | int32_t serverId[3]; |
| 471 | |
| 472 | data.writeInt32(ARRAY_SIZE(serverId)); |
| 473 | for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) { |
| 474 | sp<IBinder> server; |
| 475 | BinderLibTestBundle datai; |
| 476 | BinderLibTestBundle datai2; |
| 477 | |
| 478 | server = addServer(&serverId[i]); |
| 479 | ASSERT_TRUE(server != NULL); |
| 480 | data.writeStrongBinder(server); |
| 481 | data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION); |
| 482 | |
| 483 | datai.writeInt32(1); |
| 484 | datai.writeStrongBinder(m_server); |
| 485 | datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION); |
| 486 | datai2.appendTo(&datai); |
| 487 | |
| 488 | datai.appendTo(&data); |
| 489 | } |
| 490 | |
| 491 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 492 | ASSERT_EQ(NO_ERROR, ret); |
| 493 | |
| 494 | ret = reply.readInt32(&id); |
| 495 | ASSERT_EQ(NO_ERROR, ret); |
| 496 | EXPECT_EQ(0, id); |
| 497 | |
| 498 | ret = reply.readInt32(&count); |
| 499 | ASSERT_EQ(NO_ERROR, ret); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 500 | EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 501 | |
| 502 | for (size_t i = 0; i < (size_t)count; i++) { |
| 503 | int32_t counti; |
| 504 | |
| 505 | BinderLibTestBundle replyi(&reply); |
| 506 | EXPECT_TRUE(replyi.isValid()); |
| 507 | ret = replyi.readInt32(&id); |
| 508 | EXPECT_EQ(NO_ERROR, ret); |
| 509 | EXPECT_EQ(serverId[i], id); |
| 510 | |
| 511 | ret = replyi.readInt32(&counti); |
| 512 | ASSERT_EQ(NO_ERROR, ret); |
| 513 | EXPECT_EQ(1, counti); |
| 514 | |
| 515 | BinderLibTestBundle replyi2(&replyi); |
| 516 | EXPECT_TRUE(replyi2.isValid()); |
| 517 | ret = replyi2.readInt32(&id); |
| 518 | EXPECT_EQ(NO_ERROR, ret); |
| 519 | EXPECT_EQ(0, id); |
| 520 | EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition()); |
| 521 | |
| 522 | EXPECT_EQ(replyi.dataSize(), replyi.dataPosition()); |
| 523 | } |
| 524 | |
| 525 | EXPECT_EQ(reply.dataSize(), reply.dataPosition()); |
| 526 | } |
| 527 | |
| 528 | TEST_F(BinderLibTest, CallBack) |
| 529 | { |
| 530 | status_t ret; |
| 531 | Parcel data, reply; |
| 532 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 533 | data.writeStrongBinder(callBack); |
| 534 | ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY); |
| 535 | EXPECT_EQ(NO_ERROR, ret); |
| 536 | ret = callBack->waitEvent(5); |
| 537 | EXPECT_EQ(NO_ERROR, ret); |
| 538 | ret = callBack->getResult(); |
| 539 | EXPECT_EQ(NO_ERROR, ret); |
| 540 | } |
| 541 | |
| 542 | TEST_F(BinderLibTest, AddServer) |
| 543 | { |
| 544 | sp<IBinder> server = addServer(); |
| 545 | ASSERT_TRUE(server != NULL); |
| 546 | } |
| 547 | |
| 548 | TEST_F(BinderLibTest, DeathNotificationNoRefs) |
| 549 | { |
| 550 | status_t ret; |
| 551 | |
| 552 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 553 | |
| 554 | { |
| 555 | sp<IBinder> binder = addServer(); |
| 556 | ASSERT_TRUE(binder != NULL); |
| 557 | ret = binder->linkToDeath(testDeathRecipient); |
| 558 | EXPECT_EQ(NO_ERROR, ret); |
| 559 | } |
| 560 | IPCThreadState::self()->flushCommands(); |
| 561 | ret = testDeathRecipient->waitEvent(5); |
| 562 | EXPECT_EQ(NO_ERROR, ret); |
| 563 | #if 0 /* Is there an unlink api that does not require a strong reference? */ |
| 564 | ret = binder->unlinkToDeath(testDeathRecipient); |
| 565 | EXPECT_EQ(NO_ERROR, ret); |
| 566 | #endif |
| 567 | } |
| 568 | |
| 569 | TEST_F(BinderLibTest, DeathNotificationWeakRef) |
| 570 | { |
| 571 | status_t ret; |
| 572 | wp<IBinder> wbinder; |
| 573 | |
| 574 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 575 | |
| 576 | { |
| 577 | sp<IBinder> binder = addServer(); |
| 578 | ASSERT_TRUE(binder != NULL); |
| 579 | ret = binder->linkToDeath(testDeathRecipient); |
| 580 | EXPECT_EQ(NO_ERROR, ret); |
| 581 | wbinder = binder; |
| 582 | } |
| 583 | IPCThreadState::self()->flushCommands(); |
| 584 | ret = testDeathRecipient->waitEvent(5); |
| 585 | EXPECT_EQ(NO_ERROR, ret); |
| 586 | #if 0 /* Is there an unlink api that does not require a strong reference? */ |
| 587 | ret = binder->unlinkToDeath(testDeathRecipient); |
| 588 | EXPECT_EQ(NO_ERROR, ret); |
| 589 | #endif |
| 590 | } |
| 591 | |
| 592 | TEST_F(BinderLibTest, DeathNotificationStrongRef) |
| 593 | { |
| 594 | status_t ret; |
| 595 | sp<IBinder> sbinder; |
| 596 | |
| 597 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 598 | |
| 599 | { |
| 600 | sp<IBinder> binder = addServer(); |
| 601 | ASSERT_TRUE(binder != NULL); |
| 602 | ret = binder->linkToDeath(testDeathRecipient); |
| 603 | EXPECT_EQ(NO_ERROR, ret); |
| 604 | sbinder = binder; |
| 605 | } |
| 606 | { |
| 607 | Parcel data, reply; |
| 608 | ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 609 | EXPECT_EQ(0, ret); |
| 610 | } |
| 611 | IPCThreadState::self()->flushCommands(); |
| 612 | ret = testDeathRecipient->waitEvent(5); |
| 613 | EXPECT_EQ(NO_ERROR, ret); |
| 614 | ret = sbinder->unlinkToDeath(testDeathRecipient); |
| 615 | EXPECT_EQ(DEAD_OBJECT, ret); |
| 616 | } |
| 617 | |
| 618 | TEST_F(BinderLibTest, DeathNotificationMultiple) |
| 619 | { |
| 620 | status_t ret; |
| 621 | const int clientcount = 2; |
| 622 | sp<IBinder> target; |
| 623 | sp<IBinder> linkedclient[clientcount]; |
| 624 | sp<BinderLibTestCallBack> callBack[clientcount]; |
| 625 | sp<IBinder> passiveclient[clientcount]; |
| 626 | |
| 627 | target = addServer(); |
| 628 | ASSERT_TRUE(target != NULL); |
| 629 | for (int i = 0; i < clientcount; i++) { |
| 630 | { |
| 631 | Parcel data, reply; |
| 632 | |
| 633 | linkedclient[i] = addServer(); |
| 634 | ASSERT_TRUE(linkedclient[i] != NULL); |
| 635 | callBack[i] = new BinderLibTestCallBack(); |
| 636 | data.writeStrongBinder(target); |
| 637 | data.writeStrongBinder(callBack[i]); |
| 638 | ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 639 | EXPECT_EQ(NO_ERROR, ret); |
| 640 | } |
| 641 | { |
| 642 | Parcel data, reply; |
| 643 | |
| 644 | passiveclient[i] = addServer(); |
| 645 | ASSERT_TRUE(passiveclient[i] != NULL); |
| 646 | data.writeStrongBinder(target); |
| 647 | ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 648 | EXPECT_EQ(NO_ERROR, ret); |
| 649 | } |
| 650 | } |
| 651 | { |
| 652 | Parcel data, reply; |
| 653 | ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 654 | EXPECT_EQ(0, ret); |
| 655 | } |
| 656 | |
| 657 | for (int i = 0; i < clientcount; i++) { |
| 658 | ret = callBack[i]->waitEvent(5); |
| 659 | EXPECT_EQ(NO_ERROR, ret); |
| 660 | ret = callBack[i]->getResult(); |
| 661 | EXPECT_EQ(NO_ERROR, ret); |
| 662 | } |
| 663 | } |
| 664 | |
Martijn Coenen | f7100e4 | 2017-07-31 12:14:09 +0200 | [diff] [blame] | 665 | TEST_F(BinderLibTest, DeathNotificationThread) |
| 666 | { |
| 667 | status_t ret; |
| 668 | sp<BinderLibTestCallBack> callback; |
| 669 | sp<IBinder> target = addServer(); |
| 670 | ASSERT_TRUE(target != NULL); |
| 671 | sp<IBinder> client = addServer(); |
| 672 | ASSERT_TRUE(client != NULL); |
| 673 | |
| 674 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 675 | |
| 676 | ret = target->linkToDeath(testDeathRecipient); |
| 677 | EXPECT_EQ(NO_ERROR, ret); |
| 678 | |
| 679 | { |
| 680 | Parcel data, reply; |
| 681 | ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 682 | EXPECT_EQ(0, ret); |
| 683 | } |
| 684 | |
| 685 | /* Make sure it's dead */ |
| 686 | testDeathRecipient->waitEvent(5); |
| 687 | |
| 688 | /* Now, pass the ref to another process and ask that process to |
| 689 | * call linkToDeath() on it, and wait for a response. This tests |
| 690 | * two things: |
| 691 | * 1) You still get death notifications when calling linkToDeath() |
| 692 | * on a ref that is already dead when it was passed to you. |
| 693 | * 2) That death notifications are not directly pushed to the thread |
| 694 | * registering them, but to the threadpool (proc workqueue) instead. |
| 695 | * |
| 696 | * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION |
| 697 | * is blocked on a condition variable waiting for the death notification to be |
| 698 | * called; therefore, that thread is not available for handling proc work. |
| 699 | * So, if the death notification was pushed to the thread workqueue, the callback |
| 700 | * would never be called, and the test would timeout and fail. |
| 701 | * |
| 702 | * Note that we can't do this part of the test from this thread itself, because |
| 703 | * the binder driver would only push death notifications to the thread if |
| 704 | * it is a looper thread, which this thread is not. |
| 705 | * |
| 706 | * See b/23525545 for details. |
| 707 | */ |
| 708 | { |
| 709 | Parcel data, reply; |
| 710 | |
| 711 | callback = new BinderLibTestCallBack(); |
| 712 | data.writeStrongBinder(target); |
| 713 | data.writeStrongBinder(callback); |
| 714 | ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY); |
| 715 | EXPECT_EQ(NO_ERROR, ret); |
| 716 | } |
| 717 | |
| 718 | ret = callback->waitEvent(5); |
| 719 | EXPECT_EQ(NO_ERROR, ret); |
| 720 | ret = callback->getResult(); |
| 721 | EXPECT_EQ(NO_ERROR, ret); |
| 722 | } |
| 723 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 724 | TEST_F(BinderLibTest, PassFile) { |
| 725 | int ret; |
| 726 | int pipefd[2]; |
| 727 | uint8_t buf[1] = { 0 }; |
| 728 | uint8_t write_value = 123; |
| 729 | |
| 730 | ret = pipe2(pipefd, O_NONBLOCK); |
| 731 | ASSERT_EQ(0, ret); |
| 732 | |
| 733 | { |
| 734 | Parcel data, reply; |
| 735 | uint8_t writebuf[1] = { write_value }; |
| 736 | |
| 737 | ret = data.writeFileDescriptor(pipefd[1], true); |
| 738 | EXPECT_EQ(NO_ERROR, ret); |
| 739 | |
| 740 | ret = data.writeInt32(sizeof(writebuf)); |
| 741 | EXPECT_EQ(NO_ERROR, ret); |
| 742 | |
| 743 | ret = data.write(writebuf, sizeof(writebuf)); |
| 744 | EXPECT_EQ(NO_ERROR, ret); |
| 745 | |
| 746 | ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply); |
| 747 | EXPECT_EQ(NO_ERROR, ret); |
| 748 | } |
| 749 | |
| 750 | ret = read(pipefd[0], buf, sizeof(buf)); |
Arve Hjønnevåg | 6d5fa94 | 2016-08-12 15:32:48 -0700 | [diff] [blame] | 751 | EXPECT_EQ(sizeof(buf), (size_t)ret); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 752 | EXPECT_EQ(write_value, buf[0]); |
| 753 | |
| 754 | waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */ |
| 755 | |
| 756 | ret = read(pipefd[0], buf, sizeof(buf)); |
| 757 | EXPECT_EQ(0, ret); |
| 758 | |
| 759 | close(pipefd[0]); |
| 760 | } |
| 761 | |
| 762 | TEST_F(BinderLibTest, PromoteLocal) { |
| 763 | sp<IBinder> strong = new BBinder(); |
| 764 | wp<IBinder> weak = strong; |
| 765 | sp<IBinder> strong_from_weak = weak.promote(); |
| 766 | EXPECT_TRUE(strong != NULL); |
| 767 | EXPECT_EQ(strong, strong_from_weak); |
| 768 | strong = NULL; |
| 769 | strong_from_weak = NULL; |
| 770 | strong_from_weak = weak.promote(); |
| 771 | EXPECT_TRUE(strong_from_weak == NULL); |
| 772 | } |
| 773 | |
| 774 | TEST_F(BinderLibTest, PromoteRemote) { |
| 775 | int ret; |
| 776 | Parcel data, reply; |
| 777 | sp<IBinder> strong = new BBinder(); |
| 778 | sp<IBinder> server = addServer(); |
| 779 | |
| 780 | ASSERT_TRUE(server != NULL); |
| 781 | ASSERT_TRUE(strong != NULL); |
| 782 | |
| 783 | ret = data.writeWeakBinder(strong); |
| 784 | EXPECT_EQ(NO_ERROR, ret); |
| 785 | |
| 786 | ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply); |
| 787 | EXPECT_GE(ret, 0); |
| 788 | } |
| 789 | |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 790 | TEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) { |
| 791 | status_t ret; |
| 792 | Parcel data, reply; |
| 793 | |
| 794 | ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply); |
| 795 | EXPECT_EQ(NO_ERROR, ret); |
| 796 | |
| 797 | const flat_binder_object *fb = reply.readObject(false); |
| 798 | ASSERT_TRUE(fb != NULL); |
| 799 | EXPECT_EQ(fb->hdr.type, BINDER_TYPE_HANDLE); |
| 800 | EXPECT_EQ(ProcessState::self()->getStrongProxyForHandle(fb->handle), m_server); |
| 801 | EXPECT_EQ(fb->cookie, (binder_uintptr_t)0); |
| 802 | EXPECT_EQ(fb->binder >> 32, (binder_uintptr_t)0); |
| 803 | } |
| 804 | |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 805 | TEST_F(BinderLibTest, FreedBinder) { |
| 806 | status_t ret; |
| 807 | |
| 808 | sp<IBinder> server = addServer(); |
| 809 | ASSERT_TRUE(server != NULL); |
| 810 | |
| 811 | __u32 freedHandle; |
| 812 | wp<IBinder> keepFreedBinder; |
| 813 | { |
| 814 | Parcel data, reply; |
| 815 | data.writeBool(false); /* request weak reference */ |
| 816 | ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply); |
| 817 | ASSERT_EQ(NO_ERROR, ret); |
| 818 | struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data()); |
| 819 | freedHandle = freed->handle; |
| 820 | /* Add a weak ref to the freed binder so the driver does not |
| 821 | * delete its reference to it - otherwise the transaction |
| 822 | * fails regardless of whether the driver is fixed. |
| 823 | */ |
| 824 | keepFreedBinder = reply.readWeakBinder(); |
| 825 | } |
| 826 | { |
| 827 | Parcel data, reply; |
| 828 | data.writeStrongBinder(server); |
| 829 | /* Replace original handle with handle to the freed binder */ |
| 830 | struct flat_binder_object *strong = (struct flat_binder_object *)(data.data()); |
| 831 | __u32 oldHandle = strong->handle; |
| 832 | strong->handle = freedHandle; |
| 833 | ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply); |
| 834 | /* Returns DEAD_OBJECT (-32) if target crashes and |
| 835 | * FAILED_TRANSACTION if the driver rejects the invalid |
| 836 | * object. |
| 837 | */ |
| 838 | EXPECT_EQ((status_t)FAILED_TRANSACTION, ret); |
| 839 | /* Restore original handle so parcel destructor does not use |
| 840 | * the wrong handle. |
| 841 | */ |
| 842 | strong->handle = oldHandle; |
| 843 | } |
| 844 | } |
| 845 | |
Sherry Yang | 336cdd3 | 2017-07-24 14:12:27 -0700 | [diff] [blame] | 846 | TEST_F(BinderLibTest, CheckNoHeaderMappedInUser) { |
| 847 | status_t ret; |
| 848 | Parcel data, reply; |
| 849 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 850 | for (int i = 0; i < 2; i++) { |
| 851 | BinderLibTestBundle datai; |
| 852 | datai.appendFrom(&data, 0, data.dataSize()); |
| 853 | |
| 854 | data.freeData(); |
| 855 | data.writeInt32(1); |
| 856 | data.writeStrongBinder(callBack); |
| 857 | data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF); |
| 858 | |
| 859 | datai.appendTo(&data); |
| 860 | } |
| 861 | ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply); |
| 862 | EXPECT_EQ(NO_ERROR, ret); |
| 863 | } |
| 864 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 865 | TEST_F(BinderLibTest, OnewayQueueing) |
| 866 | { |
| 867 | status_t ret; |
| 868 | Parcel data, data2; |
| 869 | |
| 870 | sp<IBinder> pollServer = addPollServer(); |
| 871 | |
| 872 | sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack(); |
| 873 | data.writeStrongBinder(callBack); |
| 874 | data.writeInt32(500000); // delay in us before calling back |
| 875 | |
| 876 | sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack(); |
| 877 | data2.writeStrongBinder(callBack2); |
| 878 | data2.writeInt32(0); // delay in us |
| 879 | |
| 880 | ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, NULL, TF_ONE_WAY); |
| 881 | EXPECT_EQ(NO_ERROR, ret); |
| 882 | |
| 883 | // The delay ensures that this second transaction will end up on the async_todo list |
| 884 | // (for a single-threaded server) |
| 885 | ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, NULL, TF_ONE_WAY); |
| 886 | EXPECT_EQ(NO_ERROR, ret); |
| 887 | |
| 888 | // The server will ensure that the two transactions are handled in the expected order; |
| 889 | // If the ordering is not as expected, an error will be returned through the callbacks. |
| 890 | ret = callBack->waitEvent(2); |
| 891 | EXPECT_EQ(NO_ERROR, ret); |
| 892 | ret = callBack->getResult(); |
| 893 | EXPECT_EQ(NO_ERROR, ret); |
| 894 | |
| 895 | ret = callBack2->waitEvent(2); |
| 896 | EXPECT_EQ(NO_ERROR, ret); |
| 897 | ret = callBack2->getResult(); |
| 898 | EXPECT_EQ(NO_ERROR, ret); |
| 899 | } |
| 900 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 901 | class BinderLibTestService : public BBinder |
| 902 | { |
| 903 | public: |
| 904 | BinderLibTestService(int32_t id) |
| 905 | : m_id(id) |
| 906 | , m_nextServerId(id + 1) |
| 907 | , m_serverStartRequested(false) |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 908 | , m_callback(NULL) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 909 | { |
| 910 | pthread_mutex_init(&m_serverWaitMutex, NULL); |
| 911 | pthread_cond_init(&m_serverWaitCond, NULL); |
| 912 | } |
| 913 | ~BinderLibTestService() |
| 914 | { |
| 915 | exit(EXIT_SUCCESS); |
| 916 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 917 | |
| 918 | void processPendingCall() { |
| 919 | if (m_callback != NULL) { |
| 920 | Parcel data; |
| 921 | data.writeInt32(NO_ERROR); |
| 922 | m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY); |
| 923 | m_callback = NULL; |
| 924 | } |
| 925 | } |
| 926 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 927 | virtual status_t onTransact(uint32_t code, |
| 928 | const Parcel& data, Parcel* reply, |
| 929 | uint32_t flags = 0) { |
| 930 | //printf("%s: code %d\n", __func__, code); |
| 931 | (void)flags; |
| 932 | |
| 933 | if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) { |
| 934 | return PERMISSION_DENIED; |
| 935 | } |
| 936 | switch (code) { |
| 937 | case BINDER_LIB_TEST_REGISTER_SERVER: { |
| 938 | int32_t id; |
| 939 | sp<IBinder> binder; |
| 940 | id = data.readInt32(); |
| 941 | binder = data.readStrongBinder(); |
| 942 | if (binder == NULL) { |
| 943 | return BAD_VALUE; |
| 944 | } |
| 945 | |
| 946 | if (m_id != 0) |
| 947 | return INVALID_OPERATION; |
| 948 | |
| 949 | pthread_mutex_lock(&m_serverWaitMutex); |
| 950 | if (m_serverStartRequested) { |
| 951 | m_serverStartRequested = false; |
| 952 | m_serverStarted = binder; |
| 953 | pthread_cond_signal(&m_serverWaitCond); |
| 954 | } |
| 955 | pthread_mutex_unlock(&m_serverWaitMutex); |
| 956 | return NO_ERROR; |
| 957 | } |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 958 | case BINDER_LIB_TEST_ADD_POLL_SERVER: |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 959 | case BINDER_LIB_TEST_ADD_SERVER: { |
| 960 | int ret; |
| 961 | uint8_t buf[1] = { 0 }; |
| 962 | int serverid; |
| 963 | |
| 964 | if (m_id != 0) { |
| 965 | return INVALID_OPERATION; |
| 966 | } |
| 967 | pthread_mutex_lock(&m_serverWaitMutex); |
| 968 | if (m_serverStartRequested) { |
| 969 | ret = -EBUSY; |
| 970 | } else { |
| 971 | serverid = m_nextServerId++; |
| 972 | m_serverStartRequested = true; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 973 | bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 974 | |
| 975 | pthread_mutex_unlock(&m_serverWaitMutex); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 976 | ret = start_server_process(serverid, usePoll); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 977 | pthread_mutex_lock(&m_serverWaitMutex); |
| 978 | } |
| 979 | if (ret > 0) { |
| 980 | if (m_serverStartRequested) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 981 | struct timespec ts; |
| 982 | clock_gettime(CLOCK_REALTIME, &ts); |
| 983 | ts.tv_sec += 5; |
| 984 | ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 985 | } |
| 986 | if (m_serverStartRequested) { |
| 987 | m_serverStartRequested = false; |
| 988 | ret = -ETIMEDOUT; |
| 989 | } else { |
| 990 | reply->writeStrongBinder(m_serverStarted); |
| 991 | reply->writeInt32(serverid); |
| 992 | m_serverStarted = NULL; |
| 993 | ret = NO_ERROR; |
| 994 | } |
| 995 | } else if (ret >= 0) { |
| 996 | m_serverStartRequested = false; |
| 997 | ret = UNKNOWN_ERROR; |
| 998 | } |
| 999 | pthread_mutex_unlock(&m_serverWaitMutex); |
| 1000 | return ret; |
| 1001 | } |
| 1002 | case BINDER_LIB_TEST_NOP_TRANSACTION: |
| 1003 | return NO_ERROR; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1004 | case BINDER_LIB_TEST_DELAYED_CALL_BACK: { |
| 1005 | // Note: this transaction is only designed for use with a |
| 1006 | // poll() server. See comments around epoll_wait(). |
| 1007 | if (m_callback != NULL) { |
| 1008 | // A callback was already pending; this means that |
| 1009 | // we received a second call while still processing |
| 1010 | // the first one. Fail the test. |
| 1011 | sp<IBinder> callback = data.readStrongBinder(); |
| 1012 | Parcel data2; |
| 1013 | data2.writeInt32(UNKNOWN_ERROR); |
| 1014 | |
| 1015 | callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, NULL, TF_ONE_WAY); |
| 1016 | } else { |
| 1017 | m_callback = data.readStrongBinder(); |
| 1018 | int32_t delayUs = data.readInt32(); |
| 1019 | /* |
| 1020 | * It's necessary that we sleep here, so the next |
| 1021 | * transaction the caller makes will be queued to |
| 1022 | * the async queue. |
| 1023 | */ |
| 1024 | usleep(delayUs); |
| 1025 | |
| 1026 | /* |
| 1027 | * Now when we return, libbinder will tell the kernel |
| 1028 | * we are done with this transaction, and the kernel |
| 1029 | * can move the queued transaction to either the |
| 1030 | * thread todo worklist (for kernels without the fix), |
| 1031 | * or the proc todo worklist. In case of the former, |
| 1032 | * the next outbound call will pick up the pending |
| 1033 | * transaction, which leads to undesired reentrant |
| 1034 | * behavior. This is caught in the if() branch above. |
| 1035 | */ |
| 1036 | } |
| 1037 | |
| 1038 | return NO_ERROR; |
| 1039 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1040 | case BINDER_LIB_TEST_NOP_CALL_BACK: { |
| 1041 | Parcel data2, reply2; |
| 1042 | sp<IBinder> binder; |
| 1043 | binder = data.readStrongBinder(); |
| 1044 | if (binder == NULL) { |
| 1045 | return BAD_VALUE; |
| 1046 | } |
| 1047 | reply2.writeInt32(NO_ERROR); |
| 1048 | binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2); |
| 1049 | return NO_ERROR; |
| 1050 | } |
Arve Hjønnevåg | 7060431 | 2016-08-12 15:34:51 -0700 | [diff] [blame] | 1051 | case BINDER_LIB_TEST_GET_SELF_TRANSACTION: |
| 1052 | reply->writeStrongBinder(this); |
| 1053 | return NO_ERROR; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1054 | case BINDER_LIB_TEST_GET_ID_TRANSACTION: |
| 1055 | reply->writeInt32(m_id); |
| 1056 | return NO_ERROR; |
| 1057 | case BINDER_LIB_TEST_INDIRECT_TRANSACTION: { |
| 1058 | int32_t count; |
| 1059 | uint32_t indirect_code; |
| 1060 | sp<IBinder> binder; |
| 1061 | |
| 1062 | count = data.readInt32(); |
| 1063 | reply->writeInt32(m_id); |
| 1064 | reply->writeInt32(count); |
| 1065 | for (int i = 0; i < count; i++) { |
| 1066 | binder = data.readStrongBinder(); |
| 1067 | if (binder == NULL) { |
| 1068 | return BAD_VALUE; |
| 1069 | } |
| 1070 | indirect_code = data.readInt32(); |
| 1071 | BinderLibTestBundle data2(&data); |
| 1072 | if (!data2.isValid()) { |
| 1073 | return BAD_VALUE; |
| 1074 | } |
| 1075 | BinderLibTestBundle reply2; |
| 1076 | binder->transact(indirect_code, data2, &reply2); |
| 1077 | reply2.appendTo(reply); |
| 1078 | } |
| 1079 | return NO_ERROR; |
| 1080 | } |
| 1081 | case BINDER_LIB_TEST_SET_ERROR_TRANSACTION: |
| 1082 | reply->setError(data.readInt32()); |
| 1083 | return NO_ERROR; |
| 1084 | case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION: |
| 1085 | reply->writeInt32(sizeof(void *)); |
| 1086 | return NO_ERROR; |
| 1087 | case BINDER_LIB_TEST_GET_STATUS_TRANSACTION: |
| 1088 | return NO_ERROR; |
| 1089 | case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION: |
| 1090 | m_strongRef = data.readStrongBinder(); |
| 1091 | return NO_ERROR; |
| 1092 | case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: { |
| 1093 | int ret; |
| 1094 | Parcel data2, reply2; |
| 1095 | sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient(); |
| 1096 | sp<IBinder> target; |
| 1097 | sp<IBinder> callback; |
| 1098 | |
| 1099 | target = data.readStrongBinder(); |
| 1100 | if (target == NULL) { |
| 1101 | return BAD_VALUE; |
| 1102 | } |
| 1103 | callback = data.readStrongBinder(); |
| 1104 | if (callback == NULL) { |
| 1105 | return BAD_VALUE; |
| 1106 | } |
| 1107 | ret = target->linkToDeath(testDeathRecipient); |
| 1108 | if (ret == NO_ERROR) |
| 1109 | ret = testDeathRecipient->waitEvent(5); |
| 1110 | data2.writeInt32(ret); |
| 1111 | callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2); |
| 1112 | return NO_ERROR; |
| 1113 | } |
| 1114 | case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: { |
| 1115 | int ret; |
| 1116 | int32_t size; |
| 1117 | const void *buf; |
| 1118 | int fd; |
| 1119 | |
| 1120 | fd = data.readFileDescriptor(); |
| 1121 | if (fd < 0) { |
| 1122 | return BAD_VALUE; |
| 1123 | } |
| 1124 | ret = data.readInt32(&size); |
| 1125 | if (ret != NO_ERROR) { |
| 1126 | return ret; |
| 1127 | } |
| 1128 | buf = data.readInplace(size); |
| 1129 | if (buf == NULL) { |
| 1130 | return BAD_VALUE; |
| 1131 | } |
| 1132 | ret = write(fd, buf, size); |
| 1133 | if (ret != size) |
| 1134 | return UNKNOWN_ERROR; |
| 1135 | return NO_ERROR; |
| 1136 | } |
| 1137 | case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: { |
| 1138 | int ret; |
| 1139 | wp<IBinder> weak; |
| 1140 | sp<IBinder> strong; |
| 1141 | Parcel data2, reply2; |
| 1142 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1143 | sp<IBinder> server = sm->getService(binderLibTestServiceName); |
| 1144 | |
| 1145 | weak = data.readWeakBinder(); |
| 1146 | if (weak == NULL) { |
| 1147 | return BAD_VALUE; |
| 1148 | } |
| 1149 | strong = weak.promote(); |
| 1150 | |
| 1151 | ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2); |
| 1152 | if (ret != NO_ERROR) |
| 1153 | exit(EXIT_FAILURE); |
| 1154 | |
| 1155 | if (strong == NULL) { |
| 1156 | reply->setError(1); |
| 1157 | } |
| 1158 | return NO_ERROR; |
| 1159 | } |
| 1160 | case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION: |
| 1161 | alarm(10); |
| 1162 | return NO_ERROR; |
| 1163 | case BINDER_LIB_TEST_EXIT_TRANSACTION: |
| 1164 | while (wait(NULL) != -1 || errno != ECHILD) |
| 1165 | ; |
| 1166 | exit(EXIT_SUCCESS); |
Connor O'Brien | 52be2c9 | 2016-09-20 14:18:08 -0700 | [diff] [blame] | 1167 | case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: { |
| 1168 | bool strongRef = data.readBool(); |
| 1169 | sp<IBinder> binder = new BBinder(); |
| 1170 | if (strongRef) { |
| 1171 | reply->writeStrongBinder(binder); |
| 1172 | } else { |
| 1173 | reply->writeWeakBinder(binder); |
| 1174 | } |
| 1175 | return NO_ERROR; |
| 1176 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1177 | default: |
| 1178 | return UNKNOWN_TRANSACTION; |
| 1179 | }; |
| 1180 | } |
| 1181 | private: |
| 1182 | int32_t m_id; |
| 1183 | int32_t m_nextServerId; |
| 1184 | pthread_mutex_t m_serverWaitMutex; |
| 1185 | pthread_cond_t m_serverWaitCond; |
| 1186 | bool m_serverStartRequested; |
| 1187 | sp<IBinder> m_serverStarted; |
| 1188 | sp<IBinder> m_strongRef; |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1189 | bool m_callbackPending; |
| 1190 | sp<IBinder> m_callback; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1191 | }; |
| 1192 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1193 | int run_server(int index, int readypipefd, bool usePoll) |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1194 | { |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1195 | binderLibTestServiceName += String16(binderserversuffix); |
| 1196 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1197 | status_t ret; |
| 1198 | sp<IServiceManager> sm = defaultServiceManager(); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1199 | BinderLibTestService* testServicePtr; |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1200 | { |
| 1201 | sp<BinderLibTestService> testService = new BinderLibTestService(index); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1202 | /* |
| 1203 | * We need this below, but can't hold a sp<> because it prevents the |
| 1204 | * node from being cleaned up automatically. It's safe in this case |
| 1205 | * because of how the tests are written. |
| 1206 | */ |
| 1207 | testServicePtr = testService.get(); |
| 1208 | |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1209 | if (index == 0) { |
| 1210 | ret = sm->addService(binderLibTestServiceName, testService); |
| 1211 | } else { |
| 1212 | sp<IBinder> server = sm->getService(binderLibTestServiceName); |
| 1213 | Parcel data, reply; |
| 1214 | data.writeInt32(index); |
| 1215 | data.writeStrongBinder(testService); |
| 1216 | |
| 1217 | ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply); |
| 1218 | } |
| 1219 | } |
| 1220 | write(readypipefd, &ret, sizeof(ret)); |
| 1221 | close(readypipefd); |
| 1222 | //printf("%s: ret %d\n", __func__, ret); |
| 1223 | if (ret) |
| 1224 | return 1; |
| 1225 | //printf("%s: joinThreadPool\n", __func__); |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1226 | if (usePoll) { |
| 1227 | int fd; |
| 1228 | struct epoll_event ev; |
| 1229 | int epoll_fd; |
| 1230 | IPCThreadState::self()->setupPolling(&fd); |
| 1231 | if (fd < 0) { |
| 1232 | return 1; |
| 1233 | } |
| 1234 | IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER |
| 1235 | |
| 1236 | epoll_fd = epoll_create1(0); |
| 1237 | if (epoll_fd == -1) { |
| 1238 | return 1; |
| 1239 | } |
| 1240 | |
| 1241 | ev.events = EPOLLIN; |
| 1242 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 1243 | return 1; |
| 1244 | } |
| 1245 | |
| 1246 | while (1) { |
| 1247 | /* |
| 1248 | * We simulate a single-threaded process using the binder poll |
| 1249 | * interface; besides handling binder commands, it can also |
| 1250 | * issue outgoing transactions, by storing a callback in |
| 1251 | * m_callback and setting m_callbackPending. |
| 1252 | * |
| 1253 | * processPendingCall() will then issue that transaction. |
| 1254 | */ |
| 1255 | struct epoll_event events[1]; |
| 1256 | int numEvents = epoll_wait(epoll_fd, events, 1, 1000); |
| 1257 | if (numEvents < 0) { |
| 1258 | if (errno == EINTR) { |
| 1259 | continue; |
| 1260 | } |
| 1261 | return 1; |
| 1262 | } |
| 1263 | if (numEvents > 0) { |
| 1264 | IPCThreadState::self()->handlePolledCommands(); |
| 1265 | IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER |
| 1266 | testServicePtr->processPendingCall(); |
| 1267 | } |
| 1268 | } |
| 1269 | } else { |
| 1270 | ProcessState::self()->startThreadPool(); |
| 1271 | IPCThreadState::self()->joinThreadPool(); |
| 1272 | } |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1273 | //printf("%s: joinThreadPool returned\n", __func__); |
| 1274 | return 1; /* joinThreadPool should not return */ |
| 1275 | } |
| 1276 | |
| 1277 | int main(int argc, char **argv) { |
| 1278 | int ret; |
| 1279 | |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1280 | if (argc == 4 && !strcmp(argv[1], "--servername")) { |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1281 | binderservername = argv[2]; |
| 1282 | } else { |
| 1283 | binderservername = argv[0]; |
| 1284 | } |
| 1285 | |
Martijn Coenen | 45b07b4 | 2017-08-09 12:07:45 +0200 | [diff] [blame^] | 1286 | if (argc == 6 && !strcmp(argv[1], binderserverarg)) { |
| 1287 | binderserversuffix = argv[5]; |
| 1288 | return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1289 | } |
Connor O'Brien | 87c03cf | 2016-10-26 17:58:51 -0700 | [diff] [blame] | 1290 | binderserversuffix = new char[16]; |
| 1291 | snprintf(binderserversuffix, 16, "%d", getpid()); |
| 1292 | binderLibTestServiceName += String16(binderserversuffix); |
Riley Andrews | 06b01ad | 2014-12-18 12:10:08 -0800 | [diff] [blame] | 1293 | |
| 1294 | ::testing::InitGoogleTest(&argc, argv); |
| 1295 | binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv()); |
| 1296 | ProcessState::self()->startThreadPool(); |
| 1297 | return RUN_ALL_TESTS(); |
| 1298 | } |
| 1299 | |