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