binderLibTest.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <poll.h>
  19. #include <pthread.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <gtest/gtest.h>
  23. #include <binder/Binder.h>
  24. #include <binder/IBinder.h>
  25. #include <binder/IPCThreadState.h>
  26. #include <binder/IServiceManager.h>
  27. #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
  28. using namespace android;
  29. static testing::Environment* binder_env;
  30. static char *binderservername;
  31. static char binderserverarg[] = "--binderserver";
  32. static String16 binderLibTestServiceName = String16("test.binderLib");
  33. enum BinderLibTestTranscationCode {
  34. BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
  35. BINDER_LIB_TEST_REGISTER_SERVER,
  36. BINDER_LIB_TEST_ADD_SERVER,
  37. BINDER_LIB_TEST_CALL_BACK,
  38. BINDER_LIB_TEST_NOP_CALL_BACK,
  39. BINDER_LIB_TEST_GET_ID_TRANSACTION,
  40. BINDER_LIB_TEST_INDIRECT_TRANSACTION,
  41. BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
  42. BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
  43. BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
  44. BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
  45. BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
  46. BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION,
  47. BINDER_LIB_TEST_EXIT_TRANSACTION,
  48. BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
  49. BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
  50. };
  51. pid_t start_server_process(int arg2)
  52. {
  53. int ret;
  54. pid_t pid;
  55. status_t status;
  56. int pipefd[2];
  57. char stri[16];
  58. char strpipefd1[16];
  59. char *childargv[] = {
  60. binderservername,
  61. binderserverarg,
  62. stri,
  63. strpipefd1,
  64. NULL
  65. };
  66. ret = pipe(pipefd);
  67. if (ret < 0)
  68. return ret;
  69. snprintf(stri, sizeof(stri), "%d", arg2);
  70. snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
  71. pid = fork();
  72. if (pid == -1)
  73. return pid;
  74. if (pid == 0) {
  75. close(pipefd[0]);
  76. execv(binderservername, childargv);
  77. status = -errno;
  78. write(pipefd[1], &status, sizeof(status));
  79. fprintf(stderr, "execv failed, %s\n", strerror(errno));
  80. _exit(EXIT_FAILURE);
  81. }
  82. close(pipefd[1]);
  83. ret = read(pipefd[0], &status, sizeof(status));
  84. //printf("pipe read returned %d, status %d\n", ret, status);
  85. close(pipefd[0]);
  86. if (ret == sizeof(status)) {
  87. ret = status;
  88. } else {
  89. kill(pid, SIGKILL);
  90. if (ret >= 0) {
  91. ret = NO_INIT;
  92. }
  93. }
  94. if (ret < 0) {
  95. wait(NULL);
  96. return ret;
  97. }
  98. return pid;
  99. }
  100. class BinderLibTestEnv : public ::testing::Environment {
  101. public:
  102. BinderLibTestEnv() {}
  103. sp<IBinder> getServer(void) {
  104. return m_server;
  105. }
  106. private:
  107. virtual void SetUp() {
  108. m_serverpid = start_server_process(0);
  109. //printf("m_serverpid %d\n", m_serverpid);
  110. ASSERT_GT(m_serverpid, 0);
  111. sp<IServiceManager> sm = defaultServiceManager();
  112. //printf("%s: pid %d, get service\n", __func__, m_pid);
  113. m_server = sm->getService(binderLibTestServiceName);
  114. ASSERT_TRUE(m_server != NULL);
  115. //printf("%s: pid %d, get service done\n", __func__, m_pid);
  116. }
  117. virtual void TearDown() {
  118. status_t ret;
  119. Parcel data, reply;
  120. int exitStatus;
  121. pid_t pid;
  122. //printf("%s: pid %d\n", __func__, m_pid);
  123. if (m_server != NULL) {
  124. ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
  125. EXPECT_EQ(0, ret);
  126. ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
  127. EXPECT_EQ(0, ret);
  128. }
  129. if (m_serverpid > 0) {
  130. //printf("wait for %d\n", m_pids[i]);
  131. pid = wait(&exitStatus);
  132. EXPECT_EQ(m_serverpid, pid);
  133. EXPECT_TRUE(WIFEXITED(exitStatus));
  134. EXPECT_EQ(0, WEXITSTATUS(exitStatus));
  135. }
  136. }
  137. pid_t m_serverpid;
  138. sp<IBinder> m_server;
  139. };
  140. class BinderLibTest : public ::testing::Test {
  141. public:
  142. virtual void SetUp() {
  143. m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
  144. }
  145. virtual void TearDown() {
  146. }
  147. protected:
  148. sp<IBinder> addServer(int32_t *idPtr = NULL)
  149. {
  150. int ret;
  151. int32_t id;
  152. Parcel data, reply;
  153. sp<IBinder> binder;
  154. ret = m_server->transact(BINDER_LIB_TEST_ADD_SERVER, data, &reply);
  155. EXPECT_EQ(NO_ERROR, ret);
  156. EXPECT_FALSE(binder != NULL);
  157. binder = reply.readStrongBinder();
  158. EXPECT_TRUE(binder != NULL);
  159. ret = reply.readInt32(&id);
  160. EXPECT_EQ(NO_ERROR, ret);
  161. if (idPtr)
  162. *idPtr = id;
  163. return binder;
  164. }
  165. void waitForReadData(int fd, int timeout_ms) {
  166. int ret;
  167. pollfd pfd = pollfd();
  168. pfd.fd = fd;
  169. pfd.events = POLLIN;
  170. ret = poll(&pfd, 1, timeout_ms);
  171. EXPECT_EQ(1, ret);
  172. }
  173. sp<IBinder> m_server;
  174. };
  175. class BinderLibTestBundle : public Parcel
  176. {
  177. public:
  178. BinderLibTestBundle(void) {}
  179. BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
  180. int32_t mark;
  181. int32_t bundleLen;
  182. size_t pos;
  183. if (source->readInt32(&mark))
  184. return;
  185. if (mark != MARK_START)
  186. return;
  187. if (source->readInt32(&bundleLen))
  188. return;
  189. pos = source->dataPosition();
  190. if (Parcel::appendFrom(source, pos, bundleLen))
  191. return;
  192. source->setDataPosition(pos + bundleLen);
  193. if (source->readInt32(&mark))
  194. return;
  195. if (mark != MARK_END)
  196. return;
  197. m_isValid = true;
  198. setDataPosition(0);
  199. }
  200. void appendTo(Parcel *dest) {
  201. dest->writeInt32(MARK_START);
  202. dest->writeInt32(dataSize());
  203. dest->appendFrom(this, 0, dataSize());
  204. dest->writeInt32(MARK_END);
  205. };
  206. bool isValid(void) {
  207. return m_isValid;
  208. }
  209. private:
  210. enum {
  211. MARK_START = B_PACK_CHARS('B','T','B','S'),
  212. MARK_END = B_PACK_CHARS('B','T','B','E'),
  213. };
  214. bool m_isValid;
  215. };
  216. class BinderLibTestEvent
  217. {
  218. public:
  219. BinderLibTestEvent(void)
  220. : m_eventTriggered(false)
  221. {
  222. pthread_mutex_init(&m_waitMutex, NULL);
  223. pthread_cond_init(&m_waitCond, NULL);
  224. }
  225. int waitEvent(int timeout_s)
  226. {
  227. int ret;
  228. pthread_mutex_lock(&m_waitMutex);
  229. if (!m_eventTriggered) {
  230. #if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
  231. pthread_cond_timeout_np(&m_waitCond, &m_waitMutex, timeout_s * 1000);
  232. #else
  233. struct timespec ts;
  234. clock_gettime(CLOCK_REALTIME, &ts);
  235. ts.tv_sec += timeout_s;
  236. pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
  237. #endif
  238. }
  239. ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
  240. pthread_mutex_unlock(&m_waitMutex);
  241. return ret;
  242. }
  243. protected:
  244. void triggerEvent(void) {
  245. pthread_mutex_lock(&m_waitMutex);
  246. pthread_cond_signal(&m_waitCond);
  247. m_eventTriggered = true;
  248. pthread_mutex_unlock(&m_waitMutex);
  249. };
  250. private:
  251. pthread_mutex_t m_waitMutex;
  252. pthread_cond_t m_waitCond;
  253. bool m_eventTriggered;
  254. };
  255. class BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
  256. {
  257. public:
  258. BinderLibTestCallBack()
  259. : m_result(NOT_ENOUGH_DATA)
  260. {
  261. }
  262. status_t getResult(void)
  263. {
  264. return m_result;
  265. }
  266. private:
  267. virtual status_t onTransact(uint32_t code,
  268. const Parcel& data, Parcel* reply,
  269. uint32_t flags = 0)
  270. {
  271. (void)reply;
  272. (void)flags;
  273. switch(code) {
  274. case BINDER_LIB_TEST_CALL_BACK:
  275. m_result = data.readInt32();
  276. triggerEvent();
  277. return NO_ERROR;
  278. default:
  279. return UNKNOWN_TRANSACTION;
  280. }
  281. }
  282. status_t m_result;
  283. };
  284. class TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
  285. {
  286. private:
  287. virtual void binderDied(const wp<IBinder>& who) {
  288. (void)who;
  289. triggerEvent();
  290. };
  291. };
  292. TEST_F(BinderLibTest, NopTransaction) {
  293. status_t ret;
  294. Parcel data, reply;
  295. ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
  296. EXPECT_EQ(NO_ERROR, ret);
  297. }
  298. TEST_F(BinderLibTest, SetError) {
  299. int32_t testValue[] = { 0, -123, 123 };
  300. for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
  301. status_t ret;
  302. Parcel data, reply;
  303. data.writeInt32(testValue[i]);
  304. ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
  305. EXPECT_EQ(testValue[i], ret);
  306. }
  307. }
  308. TEST_F(BinderLibTest, GetId) {
  309. status_t ret;
  310. int32_t id;
  311. Parcel data, reply;
  312. ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
  313. EXPECT_EQ(NO_ERROR, ret);
  314. ret = reply.readInt32(&id);
  315. EXPECT_EQ(NO_ERROR, ret);
  316. EXPECT_EQ(0, id);
  317. }
  318. TEST_F(BinderLibTest, PtrSize) {
  319. status_t ret;
  320. int32_t ptrsize;
  321. Parcel data, reply;
  322. sp<IBinder> server = addServer();
  323. ASSERT_TRUE(server != NULL);
  324. ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
  325. EXPECT_EQ(NO_ERROR, ret);
  326. ret = reply.readInt32(&ptrsize);
  327. EXPECT_EQ(NO_ERROR, ret);
  328. RecordProperty("TestPtrSize", sizeof(void *));
  329. RecordProperty("ServerPtrSize", sizeof(void *));
  330. }
  331. TEST_F(BinderLibTest, IndirectGetId2)
  332. {
  333. status_t ret;
  334. int32_t id;
  335. int32_t count;
  336. Parcel data, reply;
  337. int32_t serverId[3];
  338. data.writeInt32(ARRAY_SIZE(serverId));
  339. for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
  340. sp<IBinder> server;
  341. BinderLibTestBundle datai;
  342. server = addServer(&serverId[i]);
  343. ASSERT_TRUE(server != NULL);
  344. data.writeStrongBinder(server);
  345. data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
  346. datai.appendTo(&data);
  347. }
  348. ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
  349. ASSERT_EQ(NO_ERROR, ret);
  350. ret = reply.readInt32(&id);
  351. ASSERT_EQ(NO_ERROR, ret);
  352. EXPECT_EQ(0, id);
  353. ret = reply.readInt32(&count);
  354. ASSERT_EQ(NO_ERROR, ret);
  355. EXPECT_EQ(ARRAY_SIZE(serverId), count);
  356. for (size_t i = 0; i < (size_t)count; i++) {
  357. BinderLibTestBundle replyi(&reply);
  358. EXPECT_TRUE(replyi.isValid());
  359. ret = replyi.readInt32(&id);
  360. EXPECT_EQ(NO_ERROR, ret);
  361. EXPECT_EQ(serverId[i], id);
  362. EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
  363. }
  364. EXPECT_EQ(reply.dataSize(), reply.dataPosition());
  365. }
  366. TEST_F(BinderLibTest, IndirectGetId3)
  367. {
  368. status_t ret;
  369. int32_t id;
  370. int32_t count;
  371. Parcel data, reply;
  372. int32_t serverId[3];
  373. data.writeInt32(ARRAY_SIZE(serverId));
  374. for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
  375. sp<IBinder> server;
  376. BinderLibTestBundle datai;
  377. BinderLibTestBundle datai2;
  378. server = addServer(&serverId[i]);
  379. ASSERT_TRUE(server != NULL);
  380. data.writeStrongBinder(server);
  381. data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
  382. datai.writeInt32(1);
  383. datai.writeStrongBinder(m_server);
  384. datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
  385. datai2.appendTo(&datai);
  386. datai.appendTo(&data);
  387. }
  388. ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
  389. ASSERT_EQ(NO_ERROR, ret);
  390. ret = reply.readInt32(&id);
  391. ASSERT_EQ(NO_ERROR, ret);
  392. EXPECT_EQ(0, id);
  393. ret = reply.readInt32(&count);
  394. ASSERT_EQ(NO_ERROR, ret);
  395. EXPECT_EQ(ARRAY_SIZE(serverId), count);
  396. for (size_t i = 0; i < (size_t)count; i++) {
  397. int32_t counti;
  398. BinderLibTestBundle replyi(&reply);
  399. EXPECT_TRUE(replyi.isValid());
  400. ret = replyi.readInt32(&id);
  401. EXPECT_EQ(NO_ERROR, ret);
  402. EXPECT_EQ(serverId[i], id);
  403. ret = replyi.readInt32(&counti);
  404. ASSERT_EQ(NO_ERROR, ret);
  405. EXPECT_EQ(1, counti);
  406. BinderLibTestBundle replyi2(&replyi);
  407. EXPECT_TRUE(replyi2.isValid());
  408. ret = replyi2.readInt32(&id);
  409. EXPECT_EQ(NO_ERROR, ret);
  410. EXPECT_EQ(0, id);
  411. EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
  412. EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
  413. }
  414. EXPECT_EQ(reply.dataSize(), reply.dataPosition());
  415. }
  416. TEST_F(BinderLibTest, CallBack)
  417. {
  418. status_t ret;
  419. Parcel data, reply;
  420. sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
  421. data.writeStrongBinder(callBack);
  422. ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
  423. EXPECT_EQ(NO_ERROR, ret);
  424. ret = callBack->waitEvent(5);
  425. EXPECT_EQ(NO_ERROR, ret);
  426. ret = callBack->getResult();
  427. EXPECT_EQ(NO_ERROR, ret);
  428. }
  429. TEST_F(BinderLibTest, AddServer)
  430. {
  431. sp<IBinder> server = addServer();
  432. ASSERT_TRUE(server != NULL);
  433. }
  434. TEST_F(BinderLibTest, DeathNotificationNoRefs)
  435. {
  436. status_t ret;
  437. sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
  438. {
  439. sp<IBinder> binder = addServer();
  440. ASSERT_TRUE(binder != NULL);
  441. ret = binder->linkToDeath(testDeathRecipient);
  442. EXPECT_EQ(NO_ERROR, ret);
  443. }
  444. IPCThreadState::self()->flushCommands();
  445. ret = testDeathRecipient->waitEvent(5);
  446. EXPECT_EQ(NO_ERROR, ret);
  447. #if 0 /* Is there an unlink api that does not require a strong reference? */
  448. ret = binder->unlinkToDeath(testDeathRecipient);
  449. EXPECT_EQ(NO_ERROR, ret);
  450. #endif
  451. }
  452. TEST_F(BinderLibTest, DeathNotificationWeakRef)
  453. {
  454. status_t ret;
  455. wp<IBinder> wbinder;
  456. sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
  457. {
  458. sp<IBinder> binder = addServer();
  459. ASSERT_TRUE(binder != NULL);
  460. ret = binder->linkToDeath(testDeathRecipient);
  461. EXPECT_EQ(NO_ERROR, ret);
  462. wbinder = binder;
  463. }
  464. IPCThreadState::self()->flushCommands();
  465. ret = testDeathRecipient->waitEvent(5);
  466. EXPECT_EQ(NO_ERROR, ret);
  467. #if 0 /* Is there an unlink api that does not require a strong reference? */
  468. ret = binder->unlinkToDeath(testDeathRecipient);
  469. EXPECT_EQ(NO_ERROR, ret);
  470. #endif
  471. }
  472. TEST_F(BinderLibTest, DeathNotificationStrongRef)
  473. {
  474. status_t ret;
  475. sp<IBinder> sbinder;
  476. sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
  477. {
  478. sp<IBinder> binder = addServer();
  479. ASSERT_TRUE(binder != NULL);
  480. ret = binder->linkToDeath(testDeathRecipient);
  481. EXPECT_EQ(NO_ERROR, ret);
  482. sbinder = binder;
  483. }
  484. {
  485. Parcel data, reply;
  486. ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
  487. EXPECT_EQ(0, ret);
  488. }
  489. IPCThreadState::self()->flushCommands();
  490. ret = testDeathRecipient->waitEvent(5);
  491. EXPECT_EQ(NO_ERROR, ret);
  492. ret = sbinder->unlinkToDeath(testDeathRecipient);
  493. EXPECT_EQ(DEAD_OBJECT, ret);
  494. }
  495. TEST_F(BinderLibTest, DeathNotificationMultiple)
  496. {
  497. status_t ret;
  498. const int clientcount = 2;
  499. sp<IBinder> target;
  500. sp<IBinder> linkedclient[clientcount];
  501. sp<BinderLibTestCallBack> callBack[clientcount];
  502. sp<IBinder> passiveclient[clientcount];
  503. target = addServer();
  504. ASSERT_TRUE(target != NULL);
  505. for (int i = 0; i < clientcount; i++) {
  506. {
  507. Parcel data, reply;
  508. linkedclient[i] = addServer();
  509. ASSERT_TRUE(linkedclient[i] != NULL);
  510. callBack[i] = new BinderLibTestCallBack();
  511. data.writeStrongBinder(target);
  512. data.writeStrongBinder(callBack[i]);
  513. ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
  514. EXPECT_EQ(NO_ERROR, ret);
  515. }
  516. {
  517. Parcel data, reply;
  518. passiveclient[i] = addServer();
  519. ASSERT_TRUE(passiveclient[i] != NULL);
  520. data.writeStrongBinder(target);
  521. ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
  522. EXPECT_EQ(NO_ERROR, ret);
  523. }
  524. }
  525. {
  526. Parcel data, reply;
  527. ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
  528. EXPECT_EQ(0, ret);
  529. }
  530. for (int i = 0; i < clientcount; i++) {
  531. ret = callBack[i]->waitEvent(5);
  532. EXPECT_EQ(NO_ERROR, ret);
  533. ret = callBack[i]->getResult();
  534. EXPECT_EQ(NO_ERROR, ret);
  535. }
  536. }
  537. TEST_F(BinderLibTest, PassFile) {
  538. int ret;
  539. int pipefd[2];
  540. uint8_t buf[1] = { 0 };
  541. uint8_t write_value = 123;
  542. ret = pipe2(pipefd, O_NONBLOCK);
  543. ASSERT_EQ(0, ret);
  544. {
  545. Parcel data, reply;
  546. uint8_t writebuf[1] = { write_value };
  547. ret = data.writeFileDescriptor(pipefd[1], true);
  548. EXPECT_EQ(NO_ERROR, ret);
  549. ret = data.writeInt32(sizeof(writebuf));
  550. EXPECT_EQ(NO_ERROR, ret);
  551. ret = data.write(writebuf, sizeof(writebuf));
  552. EXPECT_EQ(NO_ERROR, ret);
  553. ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
  554. EXPECT_EQ(NO_ERROR, ret);
  555. }
  556. ret = read(pipefd[0], buf, sizeof(buf));
  557. EXPECT_EQ(sizeof(buf), ret);
  558. EXPECT_EQ(write_value, buf[0]);
  559. waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
  560. ret = read(pipefd[0], buf, sizeof(buf));
  561. EXPECT_EQ(0, ret);
  562. close(pipefd[0]);
  563. }
  564. TEST_F(BinderLibTest, PromoteLocal) {
  565. sp<IBinder> strong = new BBinder();
  566. wp<IBinder> weak = strong;
  567. sp<IBinder> strong_from_weak = weak.promote();
  568. EXPECT_TRUE(strong != NULL);
  569. EXPECT_EQ(strong, strong_from_weak);
  570. strong = NULL;
  571. strong_from_weak = NULL;
  572. strong_from_weak = weak.promote();
  573. EXPECT_TRUE(strong_from_weak == NULL);
  574. }
  575. TEST_F(BinderLibTest, PromoteRemote) {
  576. int ret;
  577. Parcel data, reply;
  578. sp<IBinder> strong = new BBinder();
  579. sp<IBinder> server = addServer();
  580. ASSERT_TRUE(server != NULL);
  581. ASSERT_TRUE(strong != NULL);
  582. ret = data.writeWeakBinder(strong);
  583. EXPECT_EQ(NO_ERROR, ret);
  584. ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
  585. EXPECT_GE(ret, 0);
  586. }
  587. class BinderLibTestService : public BBinder
  588. {
  589. public:
  590. BinderLibTestService(int32_t id)
  591. : m_id(id)
  592. , m_nextServerId(id + 1)
  593. , m_serverStartRequested(false)
  594. {
  595. pthread_mutex_init(&m_serverWaitMutex, NULL);
  596. pthread_cond_init(&m_serverWaitCond, NULL);
  597. }
  598. ~BinderLibTestService()
  599. {
  600. exit(EXIT_SUCCESS);
  601. }
  602. virtual status_t onTransact(uint32_t code,
  603. const Parcel& data, Parcel* reply,
  604. uint32_t flags = 0) {
  605. //printf("%s: code %d\n", __func__, code);
  606. (void)flags;
  607. if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
  608. return PERMISSION_DENIED;
  609. }
  610. switch (code) {
  611. case BINDER_LIB_TEST_REGISTER_SERVER: {
  612. int32_t id;
  613. sp<IBinder> binder;
  614. id = data.readInt32();
  615. binder = data.readStrongBinder();
  616. if (binder == NULL) {
  617. return BAD_VALUE;
  618. }
  619. if (m_id != 0)
  620. return INVALID_OPERATION;
  621. pthread_mutex_lock(&m_serverWaitMutex);
  622. if (m_serverStartRequested) {
  623. m_serverStartRequested = false;
  624. m_serverStarted = binder;
  625. pthread_cond_signal(&m_serverWaitCond);
  626. }
  627. pthread_mutex_unlock(&m_serverWaitMutex);
  628. return NO_ERROR;
  629. }
  630. case BINDER_LIB_TEST_ADD_SERVER: {
  631. int ret;
  632. uint8_t buf[1] = { 0 };
  633. int serverid;
  634. if (m_id != 0) {
  635. return INVALID_OPERATION;
  636. }
  637. pthread_mutex_lock(&m_serverWaitMutex);
  638. if (m_serverStartRequested) {
  639. ret = -EBUSY;
  640. } else {
  641. serverid = m_nextServerId++;
  642. m_serverStartRequested = true;
  643. pthread_mutex_unlock(&m_serverWaitMutex);
  644. ret = start_server_process(serverid);
  645. pthread_mutex_lock(&m_serverWaitMutex);
  646. }
  647. if (ret > 0) {
  648. if (m_serverStartRequested) {
  649. #if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
  650. ret = pthread_cond_timeout_np(&m_serverWaitCond, &m_serverWaitMutex, 5000);
  651. #else
  652. struct timespec ts;
  653. clock_gettime(CLOCK_REALTIME, &ts);
  654. ts.tv_sec += 5;
  655. ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
  656. #endif
  657. }
  658. if (m_serverStartRequested) {
  659. m_serverStartRequested = false;
  660. ret = -ETIMEDOUT;
  661. } else {
  662. reply->writeStrongBinder(m_serverStarted);
  663. reply->writeInt32(serverid);
  664. m_serverStarted = NULL;
  665. ret = NO_ERROR;
  666. }
  667. } else if (ret >= 0) {
  668. m_serverStartRequested = false;
  669. ret = UNKNOWN_ERROR;
  670. }
  671. pthread_mutex_unlock(&m_serverWaitMutex);
  672. return ret;
  673. }
  674. case BINDER_LIB_TEST_NOP_TRANSACTION:
  675. return NO_ERROR;
  676. case BINDER_LIB_TEST_NOP_CALL_BACK: {
  677. Parcel data2, reply2;
  678. sp<IBinder> binder;
  679. binder = data.readStrongBinder();
  680. if (binder == NULL) {
  681. return BAD_VALUE;
  682. }
  683. reply2.writeInt32(NO_ERROR);
  684. binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
  685. return NO_ERROR;
  686. }
  687. case BINDER_LIB_TEST_GET_ID_TRANSACTION:
  688. reply->writeInt32(m_id);
  689. return NO_ERROR;
  690. case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
  691. int32_t count;
  692. uint32_t indirect_code;
  693. sp<IBinder> binder;
  694. count = data.readInt32();
  695. reply->writeInt32(m_id);
  696. reply->writeInt32(count);
  697. for (int i = 0; i < count; i++) {
  698. binder = data.readStrongBinder();
  699. if (binder == NULL) {
  700. return BAD_VALUE;
  701. }
  702. indirect_code = data.readInt32();
  703. BinderLibTestBundle data2(&data);
  704. if (!data2.isValid()) {
  705. return BAD_VALUE;
  706. }
  707. BinderLibTestBundle reply2;
  708. binder->transact(indirect_code, data2, &reply2);
  709. reply2.appendTo(reply);
  710. }
  711. return NO_ERROR;
  712. }
  713. case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
  714. reply->setError(data.readInt32());
  715. return NO_ERROR;
  716. case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
  717. reply->writeInt32(sizeof(void *));
  718. return NO_ERROR;
  719. case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
  720. return NO_ERROR;
  721. case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
  722. m_strongRef = data.readStrongBinder();
  723. return NO_ERROR;
  724. case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
  725. int ret;
  726. Parcel data2, reply2;
  727. sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
  728. sp<IBinder> target;
  729. sp<IBinder> callback;
  730. target = data.readStrongBinder();
  731. if (target == NULL) {
  732. return BAD_VALUE;
  733. }
  734. callback = data.readStrongBinder();
  735. if (callback == NULL) {
  736. return BAD_VALUE;
  737. }
  738. ret = target->linkToDeath(testDeathRecipient);
  739. if (ret == NO_ERROR)
  740. ret = testDeathRecipient->waitEvent(5);
  741. data2.writeInt32(ret);
  742. callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
  743. return NO_ERROR;
  744. }
  745. case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
  746. int ret;
  747. int32_t size;
  748. const void *buf;
  749. int fd;
  750. fd = data.readFileDescriptor();
  751. if (fd < 0) {
  752. return BAD_VALUE;
  753. }
  754. ret = data.readInt32(&size);
  755. if (ret != NO_ERROR) {
  756. return ret;
  757. }
  758. buf = data.readInplace(size);
  759. if (buf == NULL) {
  760. return BAD_VALUE;
  761. }
  762. ret = write(fd, buf, size);
  763. if (ret != size)
  764. return UNKNOWN_ERROR;
  765. return NO_ERROR;
  766. }
  767. case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
  768. int ret;
  769. wp<IBinder> weak;
  770. sp<IBinder> strong;
  771. Parcel data2, reply2;
  772. sp<IServiceManager> sm = defaultServiceManager();
  773. sp<IBinder> server = sm->getService(binderLibTestServiceName);
  774. weak = data.readWeakBinder();
  775. if (weak == NULL) {
  776. return BAD_VALUE;
  777. }
  778. strong = weak.promote();
  779. ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
  780. if (ret != NO_ERROR)
  781. exit(EXIT_FAILURE);
  782. if (strong == NULL) {
  783. reply->setError(1);
  784. }
  785. return NO_ERROR;
  786. }
  787. case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
  788. alarm(10);
  789. return NO_ERROR;
  790. case BINDER_LIB_TEST_EXIT_TRANSACTION:
  791. while (wait(NULL) != -1 || errno != ECHILD)
  792. ;
  793. exit(EXIT_SUCCESS);
  794. default:
  795. return UNKNOWN_TRANSACTION;
  796. };
  797. }
  798. private:
  799. int32_t m_id;
  800. int32_t m_nextServerId;
  801. pthread_mutex_t m_serverWaitMutex;
  802. pthread_cond_t m_serverWaitCond;
  803. bool m_serverStartRequested;
  804. sp<IBinder> m_serverStarted;
  805. sp<IBinder> m_strongRef;
  806. };
  807. int run_server(int index, int readypipefd)
  808. {
  809. status_t ret;
  810. sp<IServiceManager> sm = defaultServiceManager();
  811. {
  812. sp<BinderLibTestService> testService = new BinderLibTestService(index);
  813. if (index == 0) {
  814. ret = sm->addService(binderLibTestServiceName, testService);
  815. } else {
  816. sp<IBinder> server = sm->getService(binderLibTestServiceName);
  817. Parcel data, reply;
  818. data.writeInt32(index);
  819. data.writeStrongBinder(testService);
  820. ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
  821. }
  822. }
  823. write(readypipefd, &ret, sizeof(ret));
  824. close(readypipefd);
  825. //printf("%s: ret %d\n", __func__, ret);
  826. if (ret)
  827. return 1;
  828. //printf("%s: joinThreadPool\n", __func__);
  829. ProcessState::self()->startThreadPool();
  830. IPCThreadState::self()->joinThreadPool();
  831. //printf("%s: joinThreadPool returned\n", __func__);
  832. return 1; /* joinThreadPool should not return */
  833. }
  834. int main(int argc, char **argv) {
  835. int ret;
  836. if (argc == 3 && !strcmp(argv[1], "--servername")) {
  837. binderservername = argv[2];
  838. } else {
  839. binderservername = argv[0];
  840. }
  841. if (argc == 4 && !strcmp(argv[1], binderserverarg)) {
  842. return run_server(atoi(argv[2]), atoi(argv[3]));
  843. }
  844. ::testing::InitGoogleTest(&argc, argv);
  845. binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
  846. ProcessState::self()->startThreadPool();
  847. return RUN_ALL_TESTS();
  848. }