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