sysdeps_test.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2016 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 <gtest/gtest.h>
  17. #include <unistd.h>
  18. #include <atomic>
  19. #include <condition_variable>
  20. #include <thread>
  21. #include "adb_io.h"
  22. #include "sysdeps.h"
  23. #include "sysdeps/chrono.h"
  24. TEST(sysdeps_socketpair, smoke) {
  25. int fds[2];
  26. ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
  27. ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4));
  28. ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4));
  29. char buf[4];
  30. ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4));
  31. ASSERT_STREQ(buf, "foo");
  32. ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4));
  33. ASSERT_STREQ(buf, "bar");
  34. ASSERT_EQ(0, adb_close(fds[0]));
  35. ASSERT_EQ(0, adb_close(fds[1]));
  36. }
  37. TEST(sysdeps_fd, exhaustion) {
  38. std::vector<int> fds;
  39. int socketpair[2];
  40. while (adb_socketpair(socketpair) == 0) {
  41. fds.push_back(socketpair[0]);
  42. fds.push_back(socketpair[1]);
  43. }
  44. ASSERT_EQ(EMFILE, errno) << strerror(errno);
  45. for (int fd : fds) {
  46. ASSERT_EQ(0, adb_close(fd));
  47. }
  48. ASSERT_EQ(0, adb_socketpair(socketpair));
  49. ASSERT_EQ(socketpair[0], fds[0]);
  50. ASSERT_EQ(socketpair[1], fds[1]);
  51. ASSERT_EQ(0, adb_close(socketpair[0]));
  52. ASSERT_EQ(0, adb_close(socketpair[1]));
  53. }
  54. class sysdeps_poll : public ::testing::Test {
  55. protected:
  56. int fds[2];
  57. void SetUp() override {
  58. ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
  59. }
  60. void TearDown() override {
  61. if (fds[0] >= 0) {
  62. ASSERT_EQ(0, adb_close(fds[0]));
  63. }
  64. if (fds[1] >= 0) {
  65. ASSERT_EQ(0, adb_close(fds[1]));
  66. }
  67. }
  68. };
  69. TEST_F(sysdeps_poll, smoke) {
  70. adb_pollfd pfd[2] = {};
  71. pfd[0].fd = fds[0];
  72. pfd[0].events = POLLRDNORM;
  73. pfd[1].fd = fds[1];
  74. pfd[1].events = POLLWRNORM;
  75. pfd[0].revents = -1;
  76. pfd[1].revents = -1;
  77. EXPECT_EQ(1, adb_poll(pfd, 2, 0));
  78. EXPECT_EQ(0, pfd[0].revents);
  79. EXPECT_EQ(POLLWRNORM, pfd[1].revents);
  80. ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
  81. // Wait for the socketpair to be flushed.
  82. pfd[0].revents = -1;
  83. EXPECT_EQ(1, adb_poll(pfd, 1, 100));
  84. EXPECT_EQ(POLLRDNORM, pfd[0].revents);
  85. pfd[0].revents = -1;
  86. pfd[1].revents = -1;
  87. EXPECT_EQ(2, adb_poll(pfd, 2, 0));
  88. EXPECT_EQ(POLLRDNORM, pfd[0].revents);
  89. EXPECT_EQ(POLLWRNORM, pfd[1].revents);
  90. }
  91. TEST_F(sysdeps_poll, timeout) {
  92. adb_pollfd pfd = {};
  93. pfd.fd = fds[0];
  94. pfd.events = POLLRDNORM;
  95. EXPECT_EQ(0, adb_poll(&pfd, 1, 100));
  96. EXPECT_EQ(0, pfd.revents);
  97. ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
  98. EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
  99. EXPECT_EQ(POLLRDNORM, pfd.revents);
  100. }
  101. TEST_F(sysdeps_poll, invalid_fd) {
  102. adb_pollfd pfd[3] = {};
  103. pfd[0].fd = fds[0];
  104. pfd[0].events = POLLRDNORM;
  105. pfd[0].revents = ~0;
  106. pfd[1].fd = INT_MAX;
  107. pfd[1].events = POLLRDNORM;
  108. pfd[1].revents = ~0;
  109. pfd[2].fd = fds[1];
  110. pfd[2].events = POLLWRNORM;
  111. pfd[2].revents = ~0;
  112. ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
  113. // Wait for the socketpair to be flushed.
  114. EXPECT_EQ(1, adb_poll(pfd, 1, 100));
  115. EXPECT_EQ(POLLRDNORM, pfd[0].revents);
  116. EXPECT_EQ(3, adb_poll(pfd, 3, 0));
  117. EXPECT_EQ(POLLRDNORM, pfd[0].revents);
  118. EXPECT_EQ(POLLNVAL, pfd[1].revents);
  119. EXPECT_EQ(POLLWRNORM, pfd[2].revents);
  120. // Make sure that we return immediately if an invalid FD is given.
  121. pfd[0].fd = fds[0];
  122. pfd[0].events = POLLRDNORM;
  123. pfd[0].revents = ~0;
  124. pfd[1].fd = INT_MAX;
  125. pfd[1].events = POLLRDNORM;
  126. pfd[1].revents = ~0;
  127. EXPECT_EQ(2, adb_poll(pfd, 2, -1));
  128. EXPECT_EQ(POLLRDNORM, pfd[0].revents);
  129. EXPECT_EQ(POLLNVAL, pfd[1].revents);
  130. }
  131. TEST_F(sysdeps_poll, duplicate_fd) {
  132. adb_pollfd pfd[2] = {};
  133. pfd[0].fd = fds[0];
  134. pfd[0].events = POLLRDNORM;
  135. pfd[1] = pfd[0];
  136. EXPECT_EQ(0, adb_poll(pfd, 2, 0));
  137. EXPECT_EQ(0, pfd[0].revents);
  138. EXPECT_EQ(0, pfd[1].revents);
  139. ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
  140. EXPECT_EQ(2, adb_poll(pfd, 2, 100));
  141. EXPECT_EQ(POLLRDNORM, pfd[0].revents);
  142. EXPECT_EQ(POLLRDNORM, pfd[1].revents);
  143. }
  144. TEST_F(sysdeps_poll, disconnect) {
  145. adb_pollfd pfd = {};
  146. pfd.fd = fds[0];
  147. pfd.events = POLLIN;
  148. EXPECT_EQ(0, adb_poll(&pfd, 1, 0));
  149. EXPECT_EQ(0, pfd.revents);
  150. EXPECT_EQ(0, adb_close(fds[1]));
  151. fds[1] = -1;
  152. EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
  153. // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
  154. EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
  155. }
  156. TEST_F(sysdeps_poll, fd_count) {
  157. // https://code.google.com/p/android/issues/detail?id=12141
  158. static constexpr int num_sockets = 256;
  159. std::vector<int> sockets;
  160. std::vector<adb_pollfd> pfds;
  161. sockets.resize(num_sockets * 2);
  162. for (int32_t i = 0; i < num_sockets; ++i) {
  163. ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno);
  164. ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i)));
  165. adb_pollfd pfd;
  166. pfd.events = POLLIN;
  167. pfd.fd = sockets[i * 2 + 1];
  168. pfds.push_back(pfd);
  169. }
  170. ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0));
  171. for (int i = 0; i < num_sockets; ++i) {
  172. ASSERT_NE(0, pfds[i].revents & POLLIN);
  173. int32_t buf[2] = { -1, -1 };
  174. ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t)));
  175. ASSERT_EQ(i, buf[0]);
  176. }
  177. for (int fd : sockets) {
  178. adb_close(fd);
  179. }
  180. }
  181. TEST(sysdeps_condition_variable, smoke) {
  182. static std::mutex &m = *new std::mutex;
  183. static std::condition_variable &cond = *new std::condition_variable;
  184. static volatile bool flag = false;
  185. std::unique_lock<std::mutex> lock(m);
  186. std::thread thread([]() {
  187. m.lock();
  188. flag = true;
  189. cond.notify_one();
  190. m.unlock();
  191. });
  192. while (!flag) {
  193. cond.wait(lock);
  194. }
  195. thread.join();
  196. }