blob: 9007e75f392e7f798af6fdd0920d84c1b25d0d74 [file] [log] [blame]
Josh Gaoa4f5e032016-02-09 14:59:09 -08001/*
Josh Gaoc285cd42016-02-12 11:33:53 -08002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaoa4f5e032016-02-09 14:59:09 -08003 *
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 Hughes73925982016-11-15 12:37:32 -080019
Josh Gaoa4f5e032016-02-09 14:59:09 -080020#include <atomic>
Pirama Arumuga Nainarbd4d4e12016-09-06 13:34:42 -070021#include <condition_variable>
Elliott Hughes73925982016-11-15 12:37:32 -080022#include <thread>
Josh Gaoa4f5e032016-02-09 14:59:09 -080023
Josh Gaoe7388122016-02-16 17:34:53 -080024#include "adb_io.h"
Josh Gaoa4f5e032016-02-09 14:59:09 -080025#include "sysdeps.h"
Josh Gao70267e42016-11-15 18:55:47 -080026#include "sysdeps/chrono.h"
Josh Gaoa4f5e032016-02-09 14:59:09 -080027
Josh Gao7d405252016-02-12 14:31:15 -080028static void increment_atomic_int(void* c) {
Josh Gao70267e42016-11-15 18:55:47 -080029 std::this_thread::sleep_for(1s);
Josh Gaoa4f5e032016-02-09 14:59:09 -080030 reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
Josh Gaoa4f5e032016-02-09 14:59:09 -080031}
32
33TEST(sysdeps_thread, smoke) {
34 std::atomic<int> counter(0);
35
36 for (int i = 0; i < 100; ++i) {
37 ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter));
38 }
39
Josh Gao70267e42016-11-15 18:55:47 -080040 std::this_thread::sleep_for(2s);
Josh Gaoa4f5e032016-02-09 14:59:09 -080041 ASSERT_EQ(100, counter.load());
42}
43
44TEST(sysdeps_thread, join) {
45 std::atomic<int> counter(0);
46 std::vector<adb_thread_t> threads(500);
47 for (size_t i = 0; i < threads.size(); ++i) {
48 ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i]));
49 }
50
51 int current = counter.load();
52 ASSERT_GE(current, 0);
53 // Make sure that adb_thread_create actually creates threads, and doesn't do something silly
54 // like synchronously run the function passed in. The sleep in increment_atomic_int should be
55 // enough to keep this from being flakey.
56 ASSERT_LT(current, 500);
57
58 for (const auto& thread : threads) {
59 ASSERT_TRUE(adb_thread_join(thread));
60 }
61
62 ASSERT_EQ(500, counter.load());
63}
Josh Gao7d405252016-02-12 14:31:15 -080064
65TEST(sysdeps_thread, exit) {
66 adb_thread_t thread;
67 ASSERT_TRUE(adb_thread_create(
68 [](void*) {
69 adb_thread_exit();
70 for (;;) continue;
71 },
72 nullptr, &thread));
73 ASSERT_TRUE(adb_thread_join(thread));
74}
Josh Gaoe7388122016-02-16 17:34:53 -080075
76TEST(sysdeps_socketpair, smoke) {
77 int fds[2];
78 ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
79 ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4));
80 ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4));
81
82 char buf[4];
83 ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4));
84 ASSERT_STREQ(buf, "foo");
85 ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4));
86 ASSERT_STREQ(buf, "bar");
87 ASSERT_EQ(0, adb_close(fds[0]));
88 ASSERT_EQ(0, adb_close(fds[1]));
89}
90
Josh Gao61eda8d2016-02-18 13:43:55 -080091TEST(sysdeps_fd, exhaustion) {
92 std::vector<int> fds;
93 int socketpair[2];
94
95 while (adb_socketpair(socketpair) == 0) {
96 fds.push_back(socketpair[0]);
97 fds.push_back(socketpair[1]);
98 }
99
100 ASSERT_EQ(EMFILE, errno) << strerror(errno);
101 for (int fd : fds) {
102 ASSERT_EQ(0, adb_close(fd));
103 }
104 ASSERT_EQ(0, adb_socketpair(socketpair));
105 ASSERT_EQ(socketpair[0], fds[0]);
106 ASSERT_EQ(socketpair[1], fds[1]);
107 ASSERT_EQ(0, adb_close(socketpair[0]));
108 ASSERT_EQ(0, adb_close(socketpair[1]));
109}
110
Josh Gaoe7388122016-02-16 17:34:53 -0800111class sysdeps_poll : public ::testing::Test {
112 protected:
113 int fds[2];
114 void SetUp() override {
115 ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
116 }
117
118 void TearDown() override {
Josh Gaoc1fab362016-02-19 10:42:40 -0800119 if (fds[0] >= 0) {
120 ASSERT_EQ(0, adb_close(fds[0]));
121 }
122 if (fds[1] >= 0) {
123 ASSERT_EQ(0, adb_close(fds[1]));
124 }
Josh Gaoe7388122016-02-16 17:34:53 -0800125 }
126};
127
128TEST_F(sysdeps_poll, smoke) {
Josh Gao579fc252016-02-19 18:14:20 -0800129 adb_pollfd pfd[2] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800130 pfd[0].fd = fds[0];
131 pfd[0].events = POLLRDNORM;
132 pfd[1].fd = fds[1];
133 pfd[1].events = POLLWRNORM;
134
Josh Gao579fc252016-02-19 18:14:20 -0800135 pfd[0].revents = -1;
136 pfd[1].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800137 EXPECT_EQ(1, adb_poll(pfd, 2, 0));
138 EXPECT_EQ(0, pfd[0].revents);
139 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
140
141 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
142
143 // Wait for the socketpair to be flushed.
Josh Gao579fc252016-02-19 18:14:20 -0800144 pfd[0].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800145 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
146 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
Josh Gao579fc252016-02-19 18:14:20 -0800147 pfd[0].revents = -1;
148 pfd[1].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800149 EXPECT_EQ(2, adb_poll(pfd, 2, 0));
150 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
151 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
152}
153
154TEST_F(sysdeps_poll, timeout) {
Josh Gao579fc252016-02-19 18:14:20 -0800155 adb_pollfd pfd = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800156 pfd.fd = fds[0];
157 pfd.events = POLLRDNORM;
158
159 EXPECT_EQ(0, adb_poll(&pfd, 1, 100));
160 EXPECT_EQ(0, pfd.revents);
161
162 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
163
164 EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
165 EXPECT_EQ(POLLRDNORM, pfd.revents);
166}
167
168TEST_F(sysdeps_poll, invalid_fd) {
Josh Gao579fc252016-02-19 18:14:20 -0800169 adb_pollfd pfd[3] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800170 pfd[0].fd = fds[0];
171 pfd[0].events = POLLRDNORM;
172 pfd[1].fd = INT_MAX;
173 pfd[1].events = POLLRDNORM;
174 pfd[2].fd = fds[1];
175 pfd[2].events = POLLWRNORM;
176
177 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
178
179 // Wait for the socketpair to be flushed.
180 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
181 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
182
183 EXPECT_EQ(3, adb_poll(pfd, 3, 0));
184 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
185 EXPECT_EQ(POLLNVAL, pfd[1].revents);
186 EXPECT_EQ(POLLWRNORM, pfd[2].revents);
187}
188
189TEST_F(sysdeps_poll, duplicate_fd) {
Josh Gao579fc252016-02-19 18:14:20 -0800190 adb_pollfd pfd[2] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800191 pfd[0].fd = fds[0];
192 pfd[0].events = POLLRDNORM;
193 pfd[1] = pfd[0];
194
195 EXPECT_EQ(0, adb_poll(pfd, 2, 0));
196 EXPECT_EQ(0, pfd[0].revents);
197 EXPECT_EQ(0, pfd[1].revents);
198
199 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
200
201 EXPECT_EQ(2, adb_poll(pfd, 2, 100));
202 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
203 EXPECT_EQ(POLLRDNORM, pfd[1].revents);
204}
Josh Gaoc1fab362016-02-19 10:42:40 -0800205
206TEST_F(sysdeps_poll, disconnect) {
Josh Gao579fc252016-02-19 18:14:20 -0800207 adb_pollfd pfd = {};
Josh Gaoc1fab362016-02-19 10:42:40 -0800208 pfd.fd = fds[0];
209 pfd.events = POLLIN;
210
211 EXPECT_EQ(0, adb_poll(&pfd, 1, 0));
212 EXPECT_EQ(0, pfd.revents);
213
214 EXPECT_EQ(0, adb_close(fds[1]));
215 fds[1] = -1;
216
217 EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
218
219 // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
220 EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
221}
Josh Gao7c9e5fb2016-04-18 11:09:28 -0700222
223TEST_F(sysdeps_poll, fd_count) {
224 // https://code.google.com/p/android/issues/detail?id=12141
Josh Gaof987b8e2016-04-26 15:45:05 -0700225 static constexpr int num_sockets = 256;
Josh Gao7c9e5fb2016-04-18 11:09:28 -0700226 std::vector<int> sockets;
227 std::vector<adb_pollfd> pfds;
228 sockets.resize(num_sockets * 2);
229 for (int32_t i = 0; i < num_sockets; ++i) {
230 ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno);
231 ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i)));
232 adb_pollfd pfd;
233 pfd.events = POLLIN;
234 pfd.fd = sockets[i * 2 + 1];
235 pfds.push_back(pfd);
236 }
237
238 ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0));
239 for (int i = 0; i < num_sockets; ++i) {
240 ASSERT_NE(0, pfds[i].revents & POLLIN);
241
242 int32_t buf[2] = { -1, -1 };
243 ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t)));
244 ASSERT_EQ(i, buf[0]);
245 }
246
247 for (int fd : sockets) {
248 adb_close(fd);
249 }
250}
Josh Gaob4a778e2016-05-17 19:23:39 -0700251
Josh Gaob4a778e2016-05-17 19:23:39 -0700252TEST(sysdeps_mutex, mutex_smoke) {
253 static std::atomic<bool> finished(false);
254 static std::mutex &m = *new std::mutex();
255 m.lock();
256 ASSERT_FALSE(m.try_lock());
257 adb_thread_create([](void*) {
258 ASSERT_FALSE(m.try_lock());
259 m.lock();
260 finished.store(true);
Josh Gao70267e42016-11-15 18:55:47 -0800261 std::this_thread::sleep_for(200ms);
Josh Gaob4a778e2016-05-17 19:23:39 -0700262 m.unlock();
263 }, nullptr);
264
265 ASSERT_FALSE(finished.load());
Josh Gao70267e42016-11-15 18:55:47 -0800266 std::this_thread::sleep_for(100ms);
Josh Gaob4a778e2016-05-17 19:23:39 -0700267 ASSERT_FALSE(finished.load());
268 m.unlock();
Josh Gao70267e42016-11-15 18:55:47 -0800269 std::this_thread::sleep_for(100ms);
Josh Gaob4a778e2016-05-17 19:23:39 -0700270 m.lock();
271 ASSERT_TRUE(finished.load());
272 m.unlock();
273}
274
Josh Gaob4a778e2016-05-17 19:23:39 -0700275TEST(sysdeps_mutex, recursive_mutex_smoke) {
276 static std::recursive_mutex &m = *new std::recursive_mutex();
277
278 m.lock();
279 ASSERT_TRUE(m.try_lock());
280 m.unlock();
281
282 adb_thread_create([](void*) {
283 ASSERT_FALSE(m.try_lock());
284 m.lock();
Josh Gao70267e42016-11-15 18:55:47 -0800285 std::this_thread::sleep_for(500ms);
Josh Gaob4a778e2016-05-17 19:23:39 -0700286 m.unlock();
287 }, nullptr);
288
Josh Gao70267e42016-11-15 18:55:47 -0800289 std::this_thread::sleep_for(100ms);
Josh Gaob4a778e2016-05-17 19:23:39 -0700290 m.unlock();
Josh Gao70267e42016-11-15 18:55:47 -0800291 std::this_thread::sleep_for(100ms);
Josh Gaob4a778e2016-05-17 19:23:39 -0700292 ASSERT_FALSE(m.try_lock());
293 m.lock();
294 m.unlock();
295}
Yabin Cuif401ead2016-04-29 16:53:52 -0700296
297TEST(sysdeps_condition_variable, smoke) {
298 static std::mutex &m = *new std::mutex;
299 static std::condition_variable &cond = *new std::condition_variable;
300 static volatile bool flag = false;
301
302 std::unique_lock<std::mutex> lock(m);
303 adb_thread_create([](void*) {
304 m.lock();
305 flag = true;
306 cond.notify_one();
307 m.unlock();
308 }, nullptr);
309
310 while (!flag) {
311 cond.wait(lock);
312 }
313}