binderDriverInterfaceTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <gtest/gtest.h>
  21. #include <linux/binder.h>
  22. #include <binder/IBinder.h>
  23. #include <sys/mman.h>
  24. #include <poll.h>
  25. #define BINDER_DEV_NAME "/dev/binder"
  26. testing::Environment* binder_env;
  27. class BinderDriverInterfaceTestEnv : public ::testing::Environment {
  28. virtual void SetUp() {
  29. int ret;
  30. uint32_t max_threads = 0;
  31. m_binderFd = open(BINDER_DEV_NAME, O_RDWR | O_NONBLOCK);
  32. ASSERT_GE(m_binderFd, 0);
  33. m_buffer = mmap(NULL, 64*1024, PROT_READ, MAP_SHARED, m_binderFd, 0);
  34. ASSERT_NE(m_buffer, (void *)NULL);
  35. ret = ioctl(m_binderFd, BINDER_SET_MAX_THREADS, &max_threads);
  36. EXPECT_EQ(0, ret);
  37. EnterLooper();
  38. }
  39. virtual void TearDown() {
  40. close(m_binderFd);
  41. }
  42. private:
  43. int m_binderFd;
  44. void *m_buffer;
  45. public:
  46. int getBinderFd(void) {
  47. return m_binderFd;
  48. }
  49. void EnterLooper(void) {
  50. int ret;
  51. const uint32_t bc[] = {
  52. BC_ENTER_LOOPER,
  53. };
  54. struct binder_write_read bwr = binder_write_read();
  55. bwr.write_buffer = (uintptr_t)bc;
  56. bwr.write_size = sizeof(bc);
  57. ret = ioctl(m_binderFd, BINDER_WRITE_READ, &bwr);
  58. EXPECT_EQ(0, ret);
  59. if (ret < 0) {
  60. EXPECT_EQ(0, errno);
  61. }
  62. EXPECT_EQ(sizeof(bc), bwr.write_consumed);
  63. }
  64. };
  65. class BinderDriverInterfaceTest : public ::testing::Test {
  66. public:
  67. virtual void SetUp() {
  68. m_binderFd = static_cast<BinderDriverInterfaceTestEnv *>(binder_env)->getBinderFd();
  69. }
  70. virtual void TearDown() {
  71. }
  72. protected:
  73. void binderTestIoctlRetErr2(int cmd, void *arg, int expect_ret, int expect_errno, int accept_errno) {
  74. int ret;
  75. ret = ioctl(m_binderFd, cmd, arg);
  76. EXPECT_EQ(expect_ret, ret);
  77. if (ret < 0) {
  78. if (errno != accept_errno)
  79. EXPECT_EQ(expect_errno, errno);
  80. }
  81. }
  82. void binderTestIoctlErr2(int cmd, void *arg, int expect_errno, int accept_errno) {
  83. binderTestIoctlRetErr2(cmd, arg, -1, expect_errno, accept_errno);
  84. }
  85. void binderTestIoctlErr1(int cmd, void *arg, int expect_errno) {
  86. binderTestIoctlErr2(cmd, arg, expect_errno, expect_errno);
  87. }
  88. void binderTestIoctl(int cmd, void *arg) {
  89. binderTestIoctlRetErr2(cmd, arg, 0, 0, 0);
  90. }
  91. void binderTestIoctlUnimplemented(int cmd, void *arg) {
  92. int ret;
  93. ret = ioctl(m_binderFd, cmd, arg);
  94. if (ret < 0) {
  95. /* Not currently implmented. Allow ret == -1, errno == EINVAL */
  96. EXPECT_EQ(-1, ret);
  97. EXPECT_EQ(EINVAL, errno);
  98. }
  99. }
  100. void binderTestReadEmpty(void) {
  101. size_t i;
  102. uint32_t br[32];
  103. struct binder_write_read bwr = binder_write_read();
  104. SCOPED_TRACE("TestReadEmpty");
  105. bwr.read_buffer = (uintptr_t)br;
  106. bwr.read_size = sizeof(br);
  107. binderTestIoctlErr1(BINDER_WRITE_READ, &bwr, EAGAIN);
  108. EXPECT_EQ(0u, bwr.read_consumed);
  109. for (i = 0; i * sizeof(uint32_t) < bwr.read_consumed; i++) {
  110. SCOPED_TRACE(testing::Message() << "i = " << i);
  111. EXPECT_EQ(BR_NOOP, br[i]);
  112. }
  113. }
  114. void binderWaitForReadData(int timeout_ms) {
  115. int ret;
  116. pollfd pfd = pollfd();
  117. pfd.fd = m_binderFd;
  118. pfd.events = POLLIN;
  119. ret = poll(&pfd, 1, timeout_ms);
  120. EXPECT_EQ(1, ret);
  121. }
  122. private:
  123. int m_binderFd;
  124. };
  125. TEST_F(BinderDriverInterfaceTest, Version) {
  126. struct binder_version version;
  127. binderTestIoctl(BINDER_VERSION, &version);
  128. ASSERT_EQ(BINDER_CURRENT_PROTOCOL_VERSION, version.protocol_version);
  129. }
  130. TEST_F(BinderDriverInterfaceTest, WriteReadNull) {
  131. binderTestIoctlErr1(BINDER_WRITE_READ, NULL, EFAULT);
  132. }
  133. TEST_F(BinderDriverInterfaceTest, SetIdleTimeoutNull) {
  134. binderTestIoctlErr2(BINDER_SET_IDLE_TIMEOUT, NULL, EFAULT, EINVAL);
  135. }
  136. TEST_F(BinderDriverInterfaceTest, SetMaxThreadsNull) {
  137. binderTestIoctlErr2(BINDER_SET_MAX_THREADS, NULL, EFAULT, EINVAL); /* TODO: don't accept EINVAL */
  138. }
  139. TEST_F(BinderDriverInterfaceTest, SetIdlePriorityNull) {
  140. binderTestIoctlErr2(BINDER_SET_IDLE_PRIORITY, NULL, EFAULT, EINVAL);
  141. }
  142. TEST_F(BinderDriverInterfaceTest, VersionNull) {
  143. binderTestIoctlErr2(BINDER_VERSION, NULL, EFAULT, EINVAL); /* TODO: don't accept EINVAL */
  144. }
  145. TEST_F(BinderDriverInterfaceTest, SetIdleTimeoutNoTest) {
  146. int64_t idle_timeout = 100000;
  147. binderTestIoctlUnimplemented(BINDER_SET_IDLE_TIMEOUT, &idle_timeout);
  148. }
  149. TEST_F(BinderDriverInterfaceTest, SetMaxThreads) {
  150. uint32_t max_threads = 0;
  151. binderTestIoctl(BINDER_SET_MAX_THREADS, &max_threads);
  152. }
  153. TEST_F(BinderDriverInterfaceTest, SetIdlePriorityNoTest) {
  154. int idle_priority = 0;
  155. binderTestIoctlUnimplemented(BINDER_SET_IDLE_PRIORITY, &idle_priority);
  156. }
  157. TEST_F(BinderDriverInterfaceTest, SetContextMgrBusy) {
  158. int32_t dummy = 0;
  159. binderTestIoctlErr1(BINDER_SET_CONTEXT_MGR, &dummy, EBUSY);
  160. }
  161. TEST_F(BinderDriverInterfaceTest, ThreadExit) {
  162. int32_t dummy = 0;
  163. binderTestIoctl(BINDER_THREAD_EXIT, &dummy);
  164. static_cast<BinderDriverInterfaceTestEnv *>(binder_env)->EnterLooper();
  165. }
  166. TEST_F(BinderDriverInterfaceTest, WriteReadEmpty) {
  167. struct binder_write_read bwr = binder_write_read();
  168. binderTestIoctl(BINDER_WRITE_READ, &bwr);
  169. }
  170. TEST_F(BinderDriverInterfaceTest, Read) {
  171. binderTestReadEmpty();
  172. }
  173. TEST_F(BinderDriverInterfaceTest, IncRefsAcquireReleaseDecRefs) {
  174. const uint32_t bc[] = {
  175. BC_INCREFS,
  176. 0,
  177. BC_ACQUIRE,
  178. 0,
  179. BC_RELEASE,
  180. 0,
  181. BC_DECREFS,
  182. 0,
  183. };
  184. struct binder_write_read bwr = binder_write_read();
  185. bwr.write_buffer = (uintptr_t)bc;
  186. bwr.write_size = sizeof(bc);
  187. binderTestIoctl(BINDER_WRITE_READ, &bwr);
  188. EXPECT_EQ(sizeof(bc), bwr.write_consumed);
  189. binderTestReadEmpty();
  190. }
  191. TEST_F(BinderDriverInterfaceTest, Transaction) {
  192. binder_uintptr_t cookie = 1234;
  193. struct {
  194. uint32_t cmd1;
  195. struct binder_transaction_data arg1;
  196. } __attribute__((packed)) bc1 = {
  197. .cmd1 = BC_TRANSACTION,
  198. .arg1 = {
  199. .target = { 0 },
  200. .cookie = 0,
  201. .code = android::IBinder::PING_TRANSACTION,
  202. .flags = 0,
  203. .sender_pid = 0,
  204. .sender_euid = 0,
  205. .data_size = 0,
  206. .offsets_size = 0,
  207. .data = {0, 0},
  208. },
  209. };
  210. struct {
  211. uint32_t cmd0;
  212. uint32_t cmd1;
  213. uint32_t cmd2;
  214. binder_transaction_data arg2;
  215. uint32_t pad[16];
  216. } __attribute__((packed)) br;
  217. struct binder_write_read bwr = binder_write_read();
  218. bwr.write_buffer = (uintptr_t)&bc1;
  219. bwr.write_size = sizeof(bc1);
  220. bwr.read_buffer = (uintptr_t)&br;
  221. bwr.read_size = sizeof(br);
  222. {
  223. SCOPED_TRACE("1st WriteRead");
  224. binderTestIoctl(BINDER_WRITE_READ, &bwr);
  225. }
  226. EXPECT_EQ(sizeof(bc1), bwr.write_consumed);
  227. if (bwr.read_consumed < offsetof(typeof(br), pad)) {
  228. SCOPED_TRACE("2nd WriteRead");
  229. binderWaitForReadData(10000);
  230. binderTestIoctl(BINDER_WRITE_READ, &bwr);
  231. }
  232. EXPECT_EQ(offsetof(typeof(br), pad), bwr.read_consumed);
  233. if (bwr.read_consumed > offsetof(typeof(br), cmd0))
  234. EXPECT_EQ(BR_NOOP, br.cmd0);
  235. if (bwr.read_consumed > offsetof(typeof(br), cmd1))
  236. EXPECT_EQ(BR_TRANSACTION_COMPLETE, br.cmd1);
  237. if (bwr.read_consumed > offsetof(typeof(br), cmd2))
  238. EXPECT_EQ(BR_REPLY, br.cmd2);
  239. if (bwr.read_consumed >= offsetof(typeof(br), pad)) {
  240. EXPECT_EQ(0u, br.arg2.target.ptr);
  241. EXPECT_EQ(0u, br.arg2.cookie);
  242. EXPECT_EQ(0u, br.arg2.code);
  243. EXPECT_EQ(0u, br.arg2.flags);
  244. EXPECT_EQ(0u, br.arg2.data_size);
  245. EXPECT_EQ(0u, br.arg2.offsets_size);
  246. SCOPED_TRACE("3rd WriteRead");
  247. binderTestReadEmpty();
  248. struct {
  249. uint32_t cmd1;
  250. binder_uintptr_t arg1;
  251. } __attribute__((packed)) bc2 = {
  252. .cmd1 = BC_FREE_BUFFER,
  253. .arg1 = br.arg2.data.ptr.buffer,
  254. };
  255. bwr.write_buffer = (uintptr_t)&bc2;
  256. bwr.write_size = sizeof(bc2);
  257. bwr.write_consumed = 0;
  258. bwr.read_size = 0;
  259. binderTestIoctl(BINDER_WRITE_READ, &bwr);
  260. EXPECT_EQ(sizeof(bc2), bwr.write_consumed);
  261. }
  262. binderTestReadEmpty();
  263. }
  264. TEST_F(BinderDriverInterfaceTest, RequestDeathNotification) {
  265. binder_uintptr_t cookie = 1234;
  266. struct {
  267. uint32_t cmd0;
  268. uint32_t arg0;
  269. uint32_t cmd1;
  270. struct binder_handle_cookie arg1;
  271. uint32_t cmd2;
  272. struct binder_handle_cookie arg2;
  273. uint32_t cmd3;
  274. uint32_t arg3;
  275. } __attribute__((packed)) bc = {
  276. .cmd0 = BC_INCREFS,
  277. .arg0 = 0,
  278. .cmd1 = BC_REQUEST_DEATH_NOTIFICATION,
  279. .arg1 = {
  280. .handle = 0,
  281. .cookie = cookie,
  282. },
  283. .cmd2 = BC_CLEAR_DEATH_NOTIFICATION,
  284. .arg2 = {
  285. .handle = 0,
  286. .cookie = cookie,
  287. },
  288. .cmd3 = BC_DECREFS,
  289. .arg3 = 0,
  290. };
  291. struct {
  292. uint32_t cmd0;
  293. uint32_t cmd1;
  294. binder_uintptr_t arg1;
  295. uint32_t pad[16];
  296. } __attribute__((packed)) br;
  297. struct binder_write_read bwr = binder_write_read();
  298. bwr.write_buffer = (uintptr_t)&bc;
  299. bwr.write_size = sizeof(bc);
  300. bwr.read_buffer = (uintptr_t)&br;
  301. bwr.read_size = sizeof(br);
  302. binderTestIoctl(BINDER_WRITE_READ, &bwr);
  303. EXPECT_EQ(sizeof(bc), bwr.write_consumed);
  304. EXPECT_EQ(sizeof(br) - sizeof(br.pad), bwr.read_consumed);
  305. EXPECT_EQ(BR_NOOP, br.cmd0);
  306. EXPECT_EQ(BR_CLEAR_DEATH_NOTIFICATION_DONE, br.cmd1);
  307. EXPECT_EQ(cookie, br.arg1);
  308. binderTestReadEmpty();
  309. }
  310. int main(int argc, char **argv) {
  311. ::testing::InitGoogleTest(&argc, argv);
  312. binder_env = AddGlobalTestEnvironment(new BinderDriverInterfaceTestEnv());
  313. return RUN_ALL_TESTS();
  314. }