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" |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame^] | 23 | #include "sysdeps/condition_variable.h" |
| 24 | #include "sysdeps/mutex.h" |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 25 | |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 26 | static void increment_atomic_int(void* c) { |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 27 | sleep(1); |
| 28 | reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1); |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | TEST(sysdeps_thread, smoke) { |
| 32 | std::atomic<int> counter(0); |
| 33 | |
| 34 | for (int i = 0; i < 100; ++i) { |
| 35 | ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter)); |
| 36 | } |
| 37 | |
| 38 | sleep(2); |
| 39 | ASSERT_EQ(100, counter.load()); |
| 40 | } |
| 41 | |
| 42 | TEST(sysdeps_thread, join) { |
| 43 | std::atomic<int> counter(0); |
| 44 | std::vector<adb_thread_t> threads(500); |
| 45 | for (size_t i = 0; i < threads.size(); ++i) { |
| 46 | ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i])); |
| 47 | } |
| 48 | |
| 49 | int current = counter.load(); |
| 50 | ASSERT_GE(current, 0); |
| 51 | // Make sure that adb_thread_create actually creates threads, and doesn't do something silly |
| 52 | // like synchronously run the function passed in. The sleep in increment_atomic_int should be |
| 53 | // enough to keep this from being flakey. |
| 54 | ASSERT_LT(current, 500); |
| 55 | |
| 56 | for (const auto& thread : threads) { |
| 57 | ASSERT_TRUE(adb_thread_join(thread)); |
| 58 | } |
| 59 | |
| 60 | ASSERT_EQ(500, counter.load()); |
| 61 | } |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 62 | |
| 63 | TEST(sysdeps_thread, exit) { |
| 64 | adb_thread_t thread; |
| 65 | ASSERT_TRUE(adb_thread_create( |
| 66 | [](void*) { |
| 67 | adb_thread_exit(); |
| 68 | for (;;) continue; |
| 69 | }, |
| 70 | nullptr, &thread)); |
| 71 | ASSERT_TRUE(adb_thread_join(thread)); |
| 72 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 73 | |
| 74 | TEST(sysdeps_socketpair, smoke) { |
| 75 | int fds[2]; |
| 76 | ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno); |
| 77 | ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4)); |
| 78 | ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4)); |
| 79 | |
| 80 | char buf[4]; |
| 81 | ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4)); |
| 82 | ASSERT_STREQ(buf, "foo"); |
| 83 | ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4)); |
| 84 | ASSERT_STREQ(buf, "bar"); |
| 85 | ASSERT_EQ(0, adb_close(fds[0])); |
| 86 | ASSERT_EQ(0, adb_close(fds[1])); |
| 87 | } |
| 88 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 89 | TEST(sysdeps_fd, exhaustion) { |
| 90 | std::vector<int> fds; |
| 91 | int socketpair[2]; |
| 92 | |
| 93 | while (adb_socketpair(socketpair) == 0) { |
| 94 | fds.push_back(socketpair[0]); |
| 95 | fds.push_back(socketpair[1]); |
| 96 | } |
| 97 | |
| 98 | ASSERT_EQ(EMFILE, errno) << strerror(errno); |
| 99 | for (int fd : fds) { |
| 100 | ASSERT_EQ(0, adb_close(fd)); |
| 101 | } |
| 102 | ASSERT_EQ(0, adb_socketpair(socketpair)); |
| 103 | ASSERT_EQ(socketpair[0], fds[0]); |
| 104 | ASSERT_EQ(socketpair[1], fds[1]); |
| 105 | ASSERT_EQ(0, adb_close(socketpair[0])); |
| 106 | ASSERT_EQ(0, adb_close(socketpair[1])); |
| 107 | } |
| 108 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 109 | class sysdeps_poll : public ::testing::Test { |
| 110 | protected: |
| 111 | int fds[2]; |
| 112 | void SetUp() override { |
| 113 | ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno); |
| 114 | } |
| 115 | |
| 116 | void TearDown() override { |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 117 | if (fds[0] >= 0) { |
| 118 | ASSERT_EQ(0, adb_close(fds[0])); |
| 119 | } |
| 120 | if (fds[1] >= 0) { |
| 121 | ASSERT_EQ(0, adb_close(fds[1])); |
| 122 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 123 | } |
| 124 | }; |
| 125 | |
| 126 | TEST_F(sysdeps_poll, smoke) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 127 | adb_pollfd pfd[2] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 128 | pfd[0].fd = fds[0]; |
| 129 | pfd[0].events = POLLRDNORM; |
| 130 | pfd[1].fd = fds[1]; |
| 131 | pfd[1].events = POLLWRNORM; |
| 132 | |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 133 | pfd[0].revents = -1; |
| 134 | pfd[1].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 135 | EXPECT_EQ(1, adb_poll(pfd, 2, 0)); |
| 136 | EXPECT_EQ(0, pfd[0].revents); |
| 137 | EXPECT_EQ(POLLWRNORM, pfd[1].revents); |
| 138 | |
| 139 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 140 | |
| 141 | // Wait for the socketpair to be flushed. |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 142 | pfd[0].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 143 | EXPECT_EQ(1, adb_poll(pfd, 1, 100)); |
| 144 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 145 | pfd[0].revents = -1; |
| 146 | pfd[1].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 147 | EXPECT_EQ(2, adb_poll(pfd, 2, 0)); |
| 148 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 149 | EXPECT_EQ(POLLWRNORM, pfd[1].revents); |
| 150 | } |
| 151 | |
| 152 | TEST_F(sysdeps_poll, timeout) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 153 | adb_pollfd pfd = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 154 | pfd.fd = fds[0]; |
| 155 | pfd.events = POLLRDNORM; |
| 156 | |
| 157 | EXPECT_EQ(0, adb_poll(&pfd, 1, 100)); |
| 158 | EXPECT_EQ(0, pfd.revents); |
| 159 | |
| 160 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 161 | |
| 162 | EXPECT_EQ(1, adb_poll(&pfd, 1, 100)); |
| 163 | EXPECT_EQ(POLLRDNORM, pfd.revents); |
| 164 | } |
| 165 | |
| 166 | TEST_F(sysdeps_poll, invalid_fd) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 167 | adb_pollfd pfd[3] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 168 | pfd[0].fd = fds[0]; |
| 169 | pfd[0].events = POLLRDNORM; |
| 170 | pfd[1].fd = INT_MAX; |
| 171 | pfd[1].events = POLLRDNORM; |
| 172 | pfd[2].fd = fds[1]; |
| 173 | pfd[2].events = POLLWRNORM; |
| 174 | |
| 175 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 176 | |
| 177 | // Wait for the socketpair to be flushed. |
| 178 | EXPECT_EQ(1, adb_poll(pfd, 1, 100)); |
| 179 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 180 | |
| 181 | EXPECT_EQ(3, adb_poll(pfd, 3, 0)); |
| 182 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 183 | EXPECT_EQ(POLLNVAL, pfd[1].revents); |
| 184 | EXPECT_EQ(POLLWRNORM, pfd[2].revents); |
| 185 | } |
| 186 | |
| 187 | TEST_F(sysdeps_poll, duplicate_fd) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 188 | adb_pollfd pfd[2] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 189 | pfd[0].fd = fds[0]; |
| 190 | pfd[0].events = POLLRDNORM; |
| 191 | pfd[1] = pfd[0]; |
| 192 | |
| 193 | EXPECT_EQ(0, adb_poll(pfd, 2, 0)); |
| 194 | EXPECT_EQ(0, pfd[0].revents); |
| 195 | EXPECT_EQ(0, pfd[1].revents); |
| 196 | |
| 197 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 198 | |
| 199 | EXPECT_EQ(2, adb_poll(pfd, 2, 100)); |
| 200 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 201 | EXPECT_EQ(POLLRDNORM, pfd[1].revents); |
| 202 | } |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 203 | |
| 204 | TEST_F(sysdeps_poll, disconnect) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 205 | adb_pollfd pfd = {}; |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 206 | pfd.fd = fds[0]; |
| 207 | pfd.events = POLLIN; |
| 208 | |
| 209 | EXPECT_EQ(0, adb_poll(&pfd, 1, 0)); |
| 210 | EXPECT_EQ(0, pfd.revents); |
| 211 | |
| 212 | EXPECT_EQ(0, adb_close(fds[1])); |
| 213 | fds[1] = -1; |
| 214 | |
| 215 | EXPECT_EQ(1, adb_poll(&pfd, 1, 100)); |
| 216 | |
| 217 | // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP. |
| 218 | EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP); |
| 219 | } |
Josh Gao | 7c9e5fb | 2016-04-18 11:09:28 -0700 | [diff] [blame] | 220 | |
| 221 | TEST_F(sysdeps_poll, fd_count) { |
| 222 | // https://code.google.com/p/android/issues/detail?id=12141 |
Josh Gao | f987b8e | 2016-04-26 15:45:05 -0700 | [diff] [blame] | 223 | static constexpr int num_sockets = 256; |
Josh Gao | 7c9e5fb | 2016-04-18 11:09:28 -0700 | [diff] [blame] | 224 | std::vector<int> sockets; |
| 225 | std::vector<adb_pollfd> pfds; |
| 226 | sockets.resize(num_sockets * 2); |
| 227 | for (int32_t i = 0; i < num_sockets; ++i) { |
| 228 | ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno); |
| 229 | ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i))); |
| 230 | adb_pollfd pfd; |
| 231 | pfd.events = POLLIN; |
| 232 | pfd.fd = sockets[i * 2 + 1]; |
| 233 | pfds.push_back(pfd); |
| 234 | } |
| 235 | |
| 236 | ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0)); |
| 237 | for (int i = 0; i < num_sockets; ++i) { |
| 238 | ASSERT_NE(0, pfds[i].revents & POLLIN); |
| 239 | |
| 240 | int32_t buf[2] = { -1, -1 }; |
| 241 | ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t))); |
| 242 | ASSERT_EQ(i, buf[0]); |
| 243 | } |
| 244 | |
| 245 | for (int fd : sockets) { |
| 246 | adb_close(fd); |
| 247 | } |
| 248 | } |
Josh Gao | b4a778e | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 249 | |
Josh Gao | b4a778e | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 250 | TEST(sysdeps_mutex, mutex_smoke) { |
| 251 | static std::atomic<bool> finished(false); |
| 252 | static std::mutex &m = *new std::mutex(); |
| 253 | m.lock(); |
| 254 | ASSERT_FALSE(m.try_lock()); |
| 255 | adb_thread_create([](void*) { |
| 256 | ASSERT_FALSE(m.try_lock()); |
| 257 | m.lock(); |
| 258 | finished.store(true); |
| 259 | adb_sleep_ms(200); |
| 260 | m.unlock(); |
| 261 | }, nullptr); |
| 262 | |
| 263 | ASSERT_FALSE(finished.load()); |
| 264 | adb_sleep_ms(100); |
| 265 | ASSERT_FALSE(finished.load()); |
| 266 | m.unlock(); |
| 267 | adb_sleep_ms(100); |
| 268 | m.lock(); |
| 269 | ASSERT_TRUE(finished.load()); |
| 270 | m.unlock(); |
| 271 | } |
| 272 | |
| 273 | // Our implementation on Windows aborts on double lock. |
| 274 | #if defined(_WIN32) |
| 275 | TEST(sysdeps_mutex, mutex_reentrant_lock) { |
| 276 | std::mutex &m = *new std::mutex(); |
| 277 | |
| 278 | m.lock(); |
| 279 | ASSERT_FALSE(m.try_lock()); |
| 280 | EXPECT_DEATH(m.lock(), "non-recursive mutex locked reentrantly"); |
| 281 | } |
| 282 | #endif |
| 283 | |
| 284 | TEST(sysdeps_mutex, recursive_mutex_smoke) { |
| 285 | static std::recursive_mutex &m = *new std::recursive_mutex(); |
| 286 | |
| 287 | m.lock(); |
| 288 | ASSERT_TRUE(m.try_lock()); |
| 289 | m.unlock(); |
| 290 | |
| 291 | adb_thread_create([](void*) { |
| 292 | ASSERT_FALSE(m.try_lock()); |
| 293 | m.lock(); |
| 294 | adb_sleep_ms(500); |
| 295 | m.unlock(); |
| 296 | }, nullptr); |
| 297 | |
| 298 | adb_sleep_ms(100); |
| 299 | m.unlock(); |
| 300 | adb_sleep_ms(100); |
| 301 | ASSERT_FALSE(m.try_lock()); |
| 302 | m.lock(); |
| 303 | m.unlock(); |
| 304 | } |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame^] | 305 | |
| 306 | TEST(sysdeps_condition_variable, smoke) { |
| 307 | static std::mutex &m = *new std::mutex; |
| 308 | static std::condition_variable &cond = *new std::condition_variable; |
| 309 | static volatile bool flag = false; |
| 310 | |
| 311 | std::unique_lock<std::mutex> lock(m); |
| 312 | adb_thread_create([](void*) { |
| 313 | m.lock(); |
| 314 | flag = true; |
| 315 | cond.notify_one(); |
| 316 | m.unlock(); |
| 317 | }, nullptr); |
| 318 | |
| 319 | while (!flag) { |
| 320 | cond.wait(lock); |
| 321 | } |
| 322 | } |