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