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