blob: f0c334ef2bf4eaf17be03560f7330db050ac9f9b [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>
19#include <atomic>
20
Josh Gaoe7388122016-02-16 17:34:53 -080021#include "adb_io.h"
Josh Gaoa4f5e032016-02-09 14:59:09 -080022#include "sysdeps.h"
23
Josh Gao7d405252016-02-12 14:31:15 -080024static void increment_atomic_int(void* c) {
Josh Gaoa4f5e032016-02-09 14:59:09 -080025 sleep(1);
26 reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
Josh Gaoa4f5e032016-02-09 14:59:09 -080027}
28
29TEST(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
40TEST(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 Gao7d405252016-02-12 14:31:15 -080060
61TEST(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 Gaoe7388122016-02-16 17:34:53 -080071
72TEST(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 Gao61eda8d2016-02-18 13:43:55 -080087TEST(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 Gaoe7388122016-02-16 17:34:53 -0800107class 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 Gaoc1fab362016-02-19 10:42:40 -0800115 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 Gaoe7388122016-02-16 17:34:53 -0800121 }
122};
123
124TEST_F(sysdeps_poll, smoke) {
Josh Gao579fc252016-02-19 18:14:20 -0800125 adb_pollfd pfd[2] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800126 pfd[0].fd = fds[0];
127 pfd[0].events = POLLRDNORM;
128 pfd[1].fd = fds[1];
129 pfd[1].events = POLLWRNORM;
130
Josh Gao579fc252016-02-19 18:14:20 -0800131 pfd[0].revents = -1;
132 pfd[1].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800133 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 Gao579fc252016-02-19 18:14:20 -0800140 pfd[0].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800141 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
142 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
Josh Gao579fc252016-02-19 18:14:20 -0800143 pfd[0].revents = -1;
144 pfd[1].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800145 EXPECT_EQ(2, adb_poll(pfd, 2, 0));
146 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
147 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
148}
149
150TEST_F(sysdeps_poll, timeout) {
Josh Gao579fc252016-02-19 18:14:20 -0800151 adb_pollfd pfd = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800152 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
164TEST_F(sysdeps_poll, invalid_fd) {
Josh Gao579fc252016-02-19 18:14:20 -0800165 adb_pollfd pfd[3] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800166 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
185TEST_F(sysdeps_poll, duplicate_fd) {
Josh Gao579fc252016-02-19 18:14:20 -0800186 adb_pollfd pfd[2] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800187 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 Gaoc1fab362016-02-19 10:42:40 -0800201
202TEST_F(sysdeps_poll, disconnect) {
Josh Gao579fc252016-02-19 18:14:20 -0800203 adb_pollfd pfd = {};
Josh Gaoc1fab362016-02-19 10:42:40 -0800204 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 Gao7c9e5fb2016-04-18 11:09:28 -0700218
219TEST_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}