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> |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 19 | |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 20 | #include <atomic> |
Pirama Arumuga Nainar | bd4d4e1 | 2016-09-06 13:34:42 -0700 | [diff] [blame] | 21 | #include <condition_variable> |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 22 | #include <thread> |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 23 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 24 | #include "adb_io.h" |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 25 | #include "sysdeps.h" |
Josh Gao | 70267e4 | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 26 | #include "sysdeps/chrono.h" |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 27 | |
Josh Gao | a11c395 | 2019-07-16 15:19:53 -0700 | [diff] [blame] | 28 | #if defined(_WIN32) |
| 29 | #include <windows.h> |
| 30 | static bool IsWine() { |
| 31 | HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); |
| 32 | if (!ntdll) { |
| 33 | return false; |
| 34 | } |
| 35 | return GetProcAddress(ntdll, "wine_get_version") != nullptr; |
| 36 | } |
| 37 | #else |
| 38 | static bool IsWine() { |
| 39 | return false; |
| 40 | } |
| 41 | #endif |
| 42 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 43 | TEST(sysdeps_socketpair, smoke) { |
| 44 | int fds[2]; |
| 45 | ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno); |
| 46 | ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4)); |
| 47 | ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4)); |
| 48 | |
| 49 | char buf[4]; |
| 50 | ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4)); |
| 51 | ASSERT_STREQ(buf, "foo"); |
| 52 | ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4)); |
| 53 | ASSERT_STREQ(buf, "bar"); |
| 54 | ASSERT_EQ(0, adb_close(fds[0])); |
| 55 | ASSERT_EQ(0, adb_close(fds[1])); |
| 56 | } |
| 57 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 58 | TEST(sysdeps_fd, exhaustion) { |
| 59 | std::vector<int> fds; |
| 60 | int socketpair[2]; |
| 61 | |
| 62 | while (adb_socketpair(socketpair) == 0) { |
| 63 | fds.push_back(socketpair[0]); |
| 64 | fds.push_back(socketpair[1]); |
| 65 | } |
| 66 | |
| 67 | ASSERT_EQ(EMFILE, errno) << strerror(errno); |
| 68 | for (int fd : fds) { |
| 69 | ASSERT_EQ(0, adb_close(fd)); |
| 70 | } |
| 71 | ASSERT_EQ(0, adb_socketpair(socketpair)); |
| 72 | ASSERT_EQ(socketpair[0], fds[0]); |
| 73 | ASSERT_EQ(socketpair[1], fds[1]); |
| 74 | ASSERT_EQ(0, adb_close(socketpair[0])); |
| 75 | ASSERT_EQ(0, adb_close(socketpair[1])); |
| 76 | } |
| 77 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 78 | class sysdeps_poll : public ::testing::Test { |
| 79 | protected: |
| 80 | int fds[2]; |
| 81 | void SetUp() override { |
| 82 | ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno); |
| 83 | } |
| 84 | |
| 85 | void TearDown() override { |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 86 | if (fds[0] >= 0) { |
| 87 | ASSERT_EQ(0, adb_close(fds[0])); |
| 88 | } |
| 89 | if (fds[1] >= 0) { |
| 90 | ASSERT_EQ(0, adb_close(fds[1])); |
| 91 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
| 95 | TEST_F(sysdeps_poll, smoke) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 96 | adb_pollfd pfd[2] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 97 | pfd[0].fd = fds[0]; |
| 98 | pfd[0].events = POLLRDNORM; |
| 99 | pfd[1].fd = fds[1]; |
| 100 | pfd[1].events = POLLWRNORM; |
| 101 | |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 102 | pfd[0].revents = -1; |
| 103 | pfd[1].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 104 | EXPECT_EQ(1, adb_poll(pfd, 2, 0)); |
| 105 | EXPECT_EQ(0, pfd[0].revents); |
| 106 | EXPECT_EQ(POLLWRNORM, pfd[1].revents); |
| 107 | |
| 108 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 109 | |
| 110 | // Wait for the socketpair to be flushed. |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 111 | pfd[0].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 112 | EXPECT_EQ(1, adb_poll(pfd, 1, 100)); |
| 113 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 114 | pfd[0].revents = -1; |
| 115 | pfd[1].revents = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 116 | EXPECT_EQ(2, adb_poll(pfd, 2, 0)); |
| 117 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 118 | EXPECT_EQ(POLLWRNORM, pfd[1].revents); |
| 119 | } |
| 120 | |
| 121 | TEST_F(sysdeps_poll, timeout) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 122 | adb_pollfd pfd = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 123 | pfd.fd = fds[0]; |
| 124 | pfd.events = POLLRDNORM; |
| 125 | |
| 126 | EXPECT_EQ(0, adb_poll(&pfd, 1, 100)); |
| 127 | EXPECT_EQ(0, pfd.revents); |
| 128 | |
| 129 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 130 | |
| 131 | EXPECT_EQ(1, adb_poll(&pfd, 1, 100)); |
| 132 | EXPECT_EQ(POLLRDNORM, pfd.revents); |
| 133 | } |
| 134 | |
| 135 | TEST_F(sysdeps_poll, invalid_fd) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 136 | adb_pollfd pfd[3] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 137 | pfd[0].fd = fds[0]; |
| 138 | pfd[0].events = POLLRDNORM; |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 139 | pfd[0].revents = ~0; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 140 | pfd[1].fd = INT_MAX; |
| 141 | pfd[1].events = POLLRDNORM; |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 142 | pfd[1].revents = ~0; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 143 | pfd[2].fd = fds[1]; |
| 144 | pfd[2].events = POLLWRNORM; |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 145 | pfd[2].revents = ~0; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 146 | |
| 147 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 148 | |
| 149 | // Wait for the socketpair to be flushed. |
| 150 | EXPECT_EQ(1, adb_poll(pfd, 1, 100)); |
| 151 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 152 | |
| 153 | EXPECT_EQ(3, adb_poll(pfd, 3, 0)); |
| 154 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 155 | EXPECT_EQ(POLLNVAL, pfd[1].revents); |
| 156 | EXPECT_EQ(POLLWRNORM, pfd[2].revents); |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 157 | |
| 158 | // Make sure that we return immediately if an invalid FD is given. |
| 159 | pfd[0].fd = fds[0]; |
| 160 | pfd[0].events = POLLRDNORM; |
| 161 | pfd[0].revents = ~0; |
| 162 | pfd[1].fd = INT_MAX; |
| 163 | pfd[1].events = POLLRDNORM; |
| 164 | pfd[1].revents = ~0; |
| 165 | EXPECT_EQ(2, adb_poll(pfd, 2, -1)); |
| 166 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 167 | EXPECT_EQ(POLLNVAL, pfd[1].revents); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(sysdeps_poll, duplicate_fd) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 171 | adb_pollfd pfd[2] = {}; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 172 | pfd[0].fd = fds[0]; |
| 173 | pfd[0].events = POLLRDNORM; |
| 174 | pfd[1] = pfd[0]; |
| 175 | |
| 176 | EXPECT_EQ(0, adb_poll(pfd, 2, 0)); |
| 177 | EXPECT_EQ(0, pfd[0].revents); |
| 178 | EXPECT_EQ(0, pfd[1].revents); |
| 179 | |
| 180 | ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4)); |
| 181 | |
| 182 | EXPECT_EQ(2, adb_poll(pfd, 2, 100)); |
| 183 | EXPECT_EQ(POLLRDNORM, pfd[0].revents); |
| 184 | EXPECT_EQ(POLLRDNORM, pfd[1].revents); |
| 185 | } |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 186 | |
| 187 | TEST_F(sysdeps_poll, disconnect) { |
Josh Gao | 579fc25 | 2016-02-19 18:14:20 -0800 | [diff] [blame] | 188 | adb_pollfd pfd = {}; |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 189 | pfd.fd = fds[0]; |
| 190 | pfd.events = POLLIN; |
| 191 | |
| 192 | EXPECT_EQ(0, adb_poll(&pfd, 1, 0)); |
| 193 | EXPECT_EQ(0, pfd.revents); |
| 194 | |
| 195 | EXPECT_EQ(0, adb_close(fds[1])); |
| 196 | fds[1] = -1; |
| 197 | |
| 198 | EXPECT_EQ(1, adb_poll(&pfd, 1, 100)); |
| 199 | |
Josh Gao | a11c395 | 2019-07-16 15:19:53 -0700 | [diff] [blame] | 200 | if (!IsWine()) { |
| 201 | // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP. |
| 202 | EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP); |
| 203 | } |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 204 | } |
Josh Gao | 7c9e5fb | 2016-04-18 11:09:28 -0700 | [diff] [blame] | 205 | |
| 206 | TEST_F(sysdeps_poll, fd_count) { |
| 207 | // https://code.google.com/p/android/issues/detail?id=12141 |
Josh Gao | f987b8e | 2016-04-26 15:45:05 -0700 | [diff] [blame] | 208 | static constexpr int num_sockets = 256; |
Josh Gao | 7c9e5fb | 2016-04-18 11:09:28 -0700 | [diff] [blame] | 209 | std::vector<int> sockets; |
| 210 | std::vector<adb_pollfd> pfds; |
| 211 | sockets.resize(num_sockets * 2); |
| 212 | for (int32_t i = 0; i < num_sockets; ++i) { |
| 213 | ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno); |
| 214 | ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i))); |
| 215 | adb_pollfd pfd; |
| 216 | pfd.events = POLLIN; |
| 217 | pfd.fd = sockets[i * 2 + 1]; |
| 218 | pfds.push_back(pfd); |
| 219 | } |
| 220 | |
| 221 | ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0)); |
| 222 | for (int i = 0; i < num_sockets; ++i) { |
| 223 | ASSERT_NE(0, pfds[i].revents & POLLIN); |
| 224 | |
| 225 | int32_t buf[2] = { -1, -1 }; |
| 226 | ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t))); |
| 227 | ASSERT_EQ(i, buf[0]); |
| 228 | } |
| 229 | |
| 230 | for (int fd : sockets) { |
| 231 | adb_close(fd); |
| 232 | } |
| 233 | } |
Josh Gao | b4a778e | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 234 | |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 235 | TEST(sysdeps_condition_variable, smoke) { |
| 236 | static std::mutex &m = *new std::mutex; |
| 237 | static std::condition_variable &cond = *new std::condition_variable; |
| 238 | static volatile bool flag = false; |
| 239 | |
| 240 | std::unique_lock<std::mutex> lock(m); |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 241 | std::thread thread([]() { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 242 | m.lock(); |
| 243 | flag = true; |
| 244 | cond.notify_one(); |
| 245 | m.unlock(); |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 246 | }); |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 247 | |
| 248 | while (!flag) { |
| 249 | cond.wait(lock); |
| 250 | } |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 251 | |
| 252 | thread.join(); |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 253 | } |