Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 1 | /* |
Josh Gao | c285cd4 | 2016-02-12 11:33:53 -0800 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 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 <gtest/gtest.h> |
| 18 | #include <unistd.h> |
| 19 | #include <atomic> |
| 20 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 21 | #include "adb_io.h" |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 22 | #include "sysdeps.h" |
| 23 | |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 24 | static void increment_atomic_int(void* c) { |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 25 | sleep(1); |
| 26 | reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1); |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | TEST(sysdeps_thread, smoke) { |
| 30 | std::atomic<int> counter(0); |
| 31 | |
| 32 | for (int i = 0; i < 100; ++i) { |
| 33 | ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter)); |
| 34 | } |
| 35 | |
| 36 | sleep(2); |
| 37 | ASSERT_EQ(100, counter.load()); |
| 38 | } |
| 39 | |
| 40 | TEST(sysdeps_thread, join) { |
| 41 | std::atomic<int> counter(0); |
| 42 | std::vector<adb_thread_t> threads(500); |
| 43 | for (size_t i = 0; i < threads.size(); ++i) { |
| 44 | ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i])); |
| 45 | } |
| 46 | |
| 47 | int current = counter.load(); |
| 48 | ASSERT_GE(current, 0); |
| 49 | // Make sure that adb_thread_create actually creates threads, and doesn't do something silly |
| 50 | // like synchronously run the function passed in. The sleep in increment_atomic_int should be |
| 51 | // enough to keep this from being flakey. |
| 52 | ASSERT_LT(current, 500); |
| 53 | |
| 54 | for (const auto& thread : threads) { |
| 55 | ASSERT_TRUE(adb_thread_join(thread)); |
| 56 | } |
| 57 | |
| 58 | ASSERT_EQ(500, counter.load()); |
| 59 | } |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 60 | |
| 61 | TEST(sysdeps_thread, exit) { |
| 62 | adb_thread_t thread; |
| 63 | ASSERT_TRUE(adb_thread_create( |
| 64 | [](void*) { |
| 65 | adb_thread_exit(); |
| 66 | for (;;) continue; |
| 67 | }, |
| 68 | nullptr, &thread)); |
| 69 | ASSERT_TRUE(adb_thread_join(thread)); |
| 70 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 71 | |
| 72 | TEST(sysdeps_socketpair, smoke) { |
| 73 | int fds[2]; |
| 74 | ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno); |
| 75 | ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4)); |
| 76 | ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4)); |
| 77 | |
| 78 | char buf[4]; |
| 79 | ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4)); |
| 80 | ASSERT_STREQ(buf, "foo"); |
| 81 | ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4)); |
| 82 | ASSERT_STREQ(buf, "bar"); |
| 83 | ASSERT_EQ(0, adb_close(fds[0])); |
| 84 | ASSERT_EQ(0, adb_close(fds[1])); |
| 85 | } |
| 86 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 87 | TEST(sysdeps_fd, exhaustion) { |
| 88 | std::vector<int> fds; |
| 89 | int socketpair[2]; |
| 90 | |
| 91 | while (adb_socketpair(socketpair) == 0) { |
| 92 | fds.push_back(socketpair[0]); |
| 93 | fds.push_back(socketpair[1]); |
| 94 | } |
| 95 | |
| 96 | ASSERT_EQ(EMFILE, errno) << strerror(errno); |
| 97 | for (int fd : fds) { |
| 98 | ASSERT_EQ(0, adb_close(fd)); |
| 99 | } |
| 100 | ASSERT_EQ(0, adb_socketpair(socketpair)); |
| 101 | ASSERT_EQ(socketpair[0], fds[0]); |
| 102 | ASSERT_EQ(socketpair[1], fds[1]); |
| 103 | ASSERT_EQ(0, adb_close(socketpair[0])); |
| 104 | ASSERT_EQ(0, adb_close(socketpair[1])); |
| 105 | } |
| 106 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 107 | class sysdeps_poll : public ::testing::Test { |
| 108 | protected: |
| 109 | int fds[2]; |
| 110 | void SetUp() override { |
| 111 | ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno); |
| 112 | } |
| 113 | |
| 114 | void TearDown() override { |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 115 | if (fds[0] >= 0) { |
| 116 | ASSERT_EQ(0, adb_close(fds[0])); |
| 117 | } |
| 118 | if (fds[1] >= 0) { |
| 119 | ASSERT_EQ(0, adb_close(fds[1])); |
| 120 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | |
| 124 | TEST_F(sysdeps_poll, smoke) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 125 | adb_pollfd pfd[2] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 126 | pfd[0].fd = fds[0]; |
| 127 | pfd[0].events = POLLRDNORM; |
| 128 | pfd[1].fd = fds[1]; |
| 129 | pfd[1].events = POLLWRNORM; |
| 130 | |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 131 | pfd[0].revents = -1; |
| 132 | pfd[1].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 133 | EXPECT_EQ(1, adb_poll(pfd, 2, 0)); |
| 134 | EXPECT_EQ(0, pfd[0].revents); |
| 135 | EXPECT_EQ(POLLWRNORM, pfd[1].revents); |
| 136 | |
| 137 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 138 | |
| 139 | // Wait for the socketpair to be flushed. |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 140 | pfd[0].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 141 | EXPECT_EQ(1, adb_poll(pfd, 1, 100)); |
| 142 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 143 | pfd[0].revents = -1; |
| 144 | pfd[1].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 145 | EXPECT_EQ(2, adb_poll(pfd, 2, 0)); |
| 146 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 147 | EXPECT_EQ(POLLWRNORM, pfd[1].revents); |
| 148 | } |
| 149 | |
| 150 | TEST_F(sysdeps_poll, timeout) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 151 | adb_pollfd pfd = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 152 | pfd.fd = fds[0]; |
| 153 | pfd.events = POLLRDNORM; |
| 154 | |
| 155 | EXPECT_EQ(0, adb_poll(&pfd, 1, 100)); |
| 156 | EXPECT_EQ(0, pfd.revents); |
| 157 | |
| 158 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 159 | |
| 160 | EXPECT_EQ(1, adb_poll(&pfd, 1, 100)); |
| 161 | EXPECT_EQ(POLLRDNORM, pfd.revents); |
| 162 | } |
| 163 | |
| 164 | TEST_F(sysdeps_poll, invalid_fd) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 165 | adb_pollfd pfd[3] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 166 | pfd[0].fd = fds[0]; |
| 167 | pfd[0].events = POLLRDNORM; |
| 168 | pfd[1].fd = INT_MAX; |
| 169 | pfd[1].events = POLLRDNORM; |
| 170 | pfd[2].fd = fds[1]; |
| 171 | pfd[2].events = POLLWRNORM; |
| 172 | |
| 173 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 174 | |
| 175 | // Wait for the socketpair to be flushed. |
| 176 | EXPECT_EQ(1, adb_poll(pfd, 1, 100)); |
| 177 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 178 | |
| 179 | EXPECT_EQ(3, adb_poll(pfd, 3, 0)); |
| 180 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 181 | EXPECT_EQ(POLLNVAL, pfd[1].revents); |
| 182 | EXPECT_EQ(POLLWRNORM, pfd[2].revents); |
| 183 | } |
| 184 | |
| 185 | TEST_F(sysdeps_poll, duplicate_fd) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 186 | adb_pollfd pfd[2] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 187 | pfd[0].fd = fds[0]; |
| 188 | pfd[0].events = POLLRDNORM; |
| 189 | pfd[1] = pfd[0]; |
| 190 | |
| 191 | EXPECT_EQ(0, adb_poll(pfd, 2, 0)); |
| 192 | EXPECT_EQ(0, pfd[0].revents); |
| 193 | EXPECT_EQ(0, pfd[1].revents); |
| 194 | |
| 195 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 196 | |
| 197 | EXPECT_EQ(2, adb_poll(pfd, 2, 100)); |
| 198 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 199 | EXPECT_EQ(POLLRDNORM, pfd[1].revents); |
| 200 | } |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 201 | |
| 202 | TEST_F(sysdeps_poll, disconnect) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 203 | adb_pollfd pfd = {}; |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 204 | pfd.fd = fds[0]; |
| 205 | pfd.events = POLLIN; |
| 206 | |
| 207 | EXPECT_EQ(0, adb_poll(&pfd, 1, 0)); |
| 208 | EXPECT_EQ(0, pfd.revents); |
| 209 | |
| 210 | EXPECT_EQ(0, adb_close(fds[1])); |
| 211 | fds[1] = -1; |
| 212 | |
| 213 | EXPECT_EQ(1, adb_poll(&pfd, 1, 100)); |
| 214 | |
| 215 | // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP. |
| 216 | EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP); |
| 217 | } |
Josh Gao | 7c9e5fb | 2016-04-18 11:09:28 -0700 | [diff] [blame^] | 218 | |
| 219 | TEST_F(sysdeps_poll, fd_count) { |
| 220 | // https://code.google.com/p/android/issues/detail?id=12141 |
| 221 | static constexpr int num_sockets = 512; |
| 222 | std::vector<int> sockets; |
| 223 | std::vector<adb_pollfd> pfds; |
| 224 | sockets.resize(num_sockets * 2); |
| 225 | for (int32_t i = 0; i < num_sockets; ++i) { |
| 226 | ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno); |
| 227 | ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i))); |
| 228 | adb_pollfd pfd; |
| 229 | pfd.events = POLLIN; |
| 230 | pfd.fd = sockets[i * 2 + 1]; |
| 231 | pfds.push_back(pfd); |
| 232 | } |
| 233 | |
| 234 | ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0)); |
| 235 | for (int i = 0; i < num_sockets; ++i) { |
| 236 | ASSERT_NE(0, pfds[i].revents & POLLIN); |
| 237 | |
| 238 | int32_t buf[2] = { -1, -1 }; |
| 239 | ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t))); |
| 240 | ASSERT_EQ(i, buf[0]); |
| 241 | } |
| 242 | |
| 243 | for (int fd : sockets) { |
| 244 | adb_close(fd); |
| 245 | } |
| 246 | } |