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