blob: 0f4b39c1f684707ce2193d9b1a9372076e509ae4 [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 Gaoa11c3952019-07-16 15:19:53 -070028#if defined(_WIN32)
29#include <windows.h>
30static 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
38static bool IsWine() {
39 return false;
40}
41#endif
42
Josh Gaoe7388122016-02-16 17:34:53 -080043TEST(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 Gao61eda8d2016-02-18 13:43:55 -080058TEST(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 Gaoe7388122016-02-16 17:34:53 -080078class 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 Gaoc1fab362016-02-19 10:42:40 -080086 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 Gaoe7388122016-02-16 17:34:53 -080092 }
93};
94
95TEST_F(sysdeps_poll, smoke) {
Josh Gao579fc252016-02-19 18:14:20 -080096 adb_pollfd pfd[2] = {};
Josh Gaoe7388122016-02-16 17:34:53 -080097 pfd[0].fd = fds[0];
98 pfd[0].events = POLLRDNORM;
99 pfd[1].fd = fds[1];
100 pfd[1].events = POLLWRNORM;
101
Josh Gao579fc252016-02-19 18:14:20 -0800102 pfd[0].revents = -1;
103 pfd[1].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800104 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 Gao579fc252016-02-19 18:14:20 -0800111 pfd[0].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800112 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
113 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
Josh Gao579fc252016-02-19 18:14:20 -0800114 pfd[0].revents = -1;
115 pfd[1].revents = -1;
Josh Gaoe7388122016-02-16 17:34:53 -0800116 EXPECT_EQ(2, adb_poll(pfd, 2, 0));
117 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
118 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
119}
120
121TEST_F(sysdeps_poll, timeout) {
Josh Gao579fc252016-02-19 18:14:20 -0800122 adb_pollfd pfd = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800123 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
135TEST_F(sysdeps_poll, invalid_fd) {
Josh Gao579fc252016-02-19 18:14:20 -0800136 adb_pollfd pfd[3] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800137 pfd[0].fd = fds[0];
138 pfd[0].events = POLLRDNORM;
Josh Gaobe2ee7b2018-03-29 12:34:28 -0700139 pfd[0].revents = ~0;
Josh Gaoe7388122016-02-16 17:34:53 -0800140 pfd[1].fd = INT_MAX;
141 pfd[1].events = POLLRDNORM;
Josh Gaobe2ee7b2018-03-29 12:34:28 -0700142 pfd[1].revents = ~0;
Josh Gaoe7388122016-02-16 17:34:53 -0800143 pfd[2].fd = fds[1];
144 pfd[2].events = POLLWRNORM;
Josh Gaobe2ee7b2018-03-29 12:34:28 -0700145 pfd[2].revents = ~0;
Josh Gaoe7388122016-02-16 17:34:53 -0800146
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 Gaobe2ee7b2018-03-29 12:34:28 -0700157
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 Gaoe7388122016-02-16 17:34:53 -0800168}
169
170TEST_F(sysdeps_poll, duplicate_fd) {
Josh Gao579fc252016-02-19 18:14:20 -0800171 adb_pollfd pfd[2] = {};
Josh Gaoe7388122016-02-16 17:34:53 -0800172 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 Gaoc1fab362016-02-19 10:42:40 -0800186
187TEST_F(sysdeps_poll, disconnect) {
Josh Gao579fc252016-02-19 18:14:20 -0800188 adb_pollfd pfd = {};
Josh Gaoc1fab362016-02-19 10:42:40 -0800189 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 Gaoa11c3952019-07-16 15:19:53 -0700200 if (!IsWine()) {
201 // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
202 EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
203 }
Josh Gaoc1fab362016-02-19 10:42:40 -0800204}
Josh Gao7c9e5fb2016-04-18 11:09:28 -0700205
206TEST_F(sysdeps_poll, fd_count) {
207 // https://code.google.com/p/android/issues/detail?id=12141
Josh Gaof987b8e2016-04-26 15:45:05 -0700208 static constexpr int num_sockets = 256;
Josh Gao7c9e5fb2016-04-18 11:09:28 -0700209 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 Gaob4a778e2016-05-17 19:23:39 -0700234
Yabin Cuif401ead2016-04-29 16:53:52 -0700235TEST(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 Gao0f3312a2017-04-12 17:00:49 -0700241 std::thread thread([]() {
Yabin Cuif401ead2016-04-29 16:53:52 -0700242 m.lock();
243 flag = true;
244 cond.notify_one();
245 m.unlock();
Josh Gao0f3312a2017-04-12 17:00:49 -0700246 });
Yabin Cuif401ead2016-04-29 16:53:52 -0700247
248 while (!flag) {
249 cond.wait(lock);
250 }
Josh Gao0f3312a2017-04-12 17:00:49 -0700251
252 thread.join();
Yabin Cuif401ead2016-04-29 16:53:52 -0700253}