blob: 79cebe6b5681370799a5a979fa7acbe0daae7aec [file] [log] [blame]
Josh Gao3b3e10d2016-02-09 14:59:09 -08001/*
Josh Gao6b42a2b2016-02-12 11:33:53 -08002 * Copyright (C) 2016 The Android Open Source Project
Josh Gao3b3e10d2016-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 Hughesdbe91ee2016-11-15 12:37:32 -080019
Josh Gao3b3e10d2016-02-09 14:59:09 -080020#include <atomic>
Pirama Arumuga Nainar7eaef8a2016-09-06 13:34:42 -070021#include <condition_variable>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080022#include <thread>
Josh Gao3b3e10d2016-02-09 14:59:09 -080023
Josh Gao3777d2e2016-02-16 17:34:53 -080024#include "adb_io.h"
Josh Gao3b3e10d2016-02-09 14:59:09 -080025#include "sysdeps.h"
Josh Gao4602adb2016-11-15 18:55:47 -080026#include "sysdeps/chrono.h"
Josh Gao3b3e10d2016-02-09 14:59:09 -080027
Josh Gao3777d2e2016-02-16 17:34:53 -080028TEST(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 Gao6487e742016-02-18 13:43:55 -080043TEST(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 Gao3777d2e2016-02-16 17:34:53 -080063class 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 Gao09855472016-02-19 10:42:40 -080071 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 Gao3777d2e2016-02-16 17:34:53 -080077 }
78};
79
80TEST_F(sysdeps_poll, smoke) {
Josh Gao2275f7d2016-02-19 18:14:20 -080081 adb_pollfd pfd[2] = {};
Josh Gao3777d2e2016-02-16 17:34:53 -080082 pfd[0].fd = fds[0];
83 pfd[0].events = POLLRDNORM;
84 pfd[1].fd = fds[1];
85 pfd[1].events = POLLWRNORM;
86
Josh Gao2275f7d2016-02-19 18:14:20 -080087 pfd[0].revents = -1;
88 pfd[1].revents = -1;
Josh Gao3777d2e2016-02-16 17:34:53 -080089 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 Gao2275f7d2016-02-19 18:14:20 -080096 pfd[0].revents = -1;
Josh Gao3777d2e2016-02-16 17:34:53 -080097 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
98 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
Josh Gao2275f7d2016-02-19 18:14:20 -080099 pfd[0].revents = -1;
100 pfd[1].revents = -1;
Josh Gao3777d2e2016-02-16 17:34:53 -0800101 EXPECT_EQ(2, adb_poll(pfd, 2, 0));
102 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
103 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
104}
105
106TEST_F(sysdeps_poll, timeout) {
Josh Gao2275f7d2016-02-19 18:14:20 -0800107 adb_pollfd pfd = {};
Josh Gao3777d2e2016-02-16 17:34:53 -0800108 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
120TEST_F(sysdeps_poll, invalid_fd) {
Josh Gao2275f7d2016-02-19 18:14:20 -0800121 adb_pollfd pfd[3] = {};
Josh Gao3777d2e2016-02-16 17:34:53 -0800122 pfd[0].fd = fds[0];
123 pfd[0].events = POLLRDNORM;
Josh Gao05fb45b2018-03-29 12:34:28 -0700124 pfd[0].revents = ~0;
Josh Gao3777d2e2016-02-16 17:34:53 -0800125 pfd[1].fd = INT_MAX;
126 pfd[1].events = POLLRDNORM;
Josh Gao05fb45b2018-03-29 12:34:28 -0700127 pfd[1].revents = ~0;
Josh Gao3777d2e2016-02-16 17:34:53 -0800128 pfd[2].fd = fds[1];
129 pfd[2].events = POLLWRNORM;
Josh Gao05fb45b2018-03-29 12:34:28 -0700130 pfd[2].revents = ~0;
Josh Gao3777d2e2016-02-16 17:34:53 -0800131
132 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
133
134 // Wait for the socketpair to be flushed.
135 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
136 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
137
138 EXPECT_EQ(3, adb_poll(pfd, 3, 0));
139 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
140 EXPECT_EQ(POLLNVAL, pfd[1].revents);
141 EXPECT_EQ(POLLWRNORM, pfd[2].revents);
Josh Gao05fb45b2018-03-29 12:34:28 -0700142
143 // Make sure that we return immediately if an invalid FD is given.
144 pfd[0].fd = fds[0];
145 pfd[0].events = POLLRDNORM;
146 pfd[0].revents = ~0;
147 pfd[1].fd = INT_MAX;
148 pfd[1].events = POLLRDNORM;
149 pfd[1].revents = ~0;
150 EXPECT_EQ(2, adb_poll(pfd, 2, -1));
151 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
152 EXPECT_EQ(POLLNVAL, pfd[1].revents);
Josh Gao3777d2e2016-02-16 17:34:53 -0800153}
154
155TEST_F(sysdeps_poll, duplicate_fd) {
Josh Gao2275f7d2016-02-19 18:14:20 -0800156 adb_pollfd pfd[2] = {};
Josh Gao3777d2e2016-02-16 17:34:53 -0800157 pfd[0].fd = fds[0];
158 pfd[0].events = POLLRDNORM;
159 pfd[1] = pfd[0];
160
161 EXPECT_EQ(0, adb_poll(pfd, 2, 0));
162 EXPECT_EQ(0, pfd[0].revents);
163 EXPECT_EQ(0, pfd[1].revents);
164
165 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
166
167 EXPECT_EQ(2, adb_poll(pfd, 2, 100));
168 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
169 EXPECT_EQ(POLLRDNORM, pfd[1].revents);
170}
Josh Gao09855472016-02-19 10:42:40 -0800171
172TEST_F(sysdeps_poll, disconnect) {
Josh Gao2275f7d2016-02-19 18:14:20 -0800173 adb_pollfd pfd = {};
Josh Gao09855472016-02-19 10:42:40 -0800174 pfd.fd = fds[0];
175 pfd.events = POLLIN;
176
177 EXPECT_EQ(0, adb_poll(&pfd, 1, 0));
178 EXPECT_EQ(0, pfd.revents);
179
180 EXPECT_EQ(0, adb_close(fds[1]));
181 fds[1] = -1;
182
183 EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
184
185 // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
186 EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
187}
Josh Gaob31e1712016-04-18 11:09:28 -0700188
189TEST_F(sysdeps_poll, fd_count) {
190 // https://code.google.com/p/android/issues/detail?id=12141
Josh Gao858f1332016-04-26 15:45:05 -0700191 static constexpr int num_sockets = 256;
Josh Gaob31e1712016-04-18 11:09:28 -0700192 std::vector<int> sockets;
193 std::vector<adb_pollfd> pfds;
194 sockets.resize(num_sockets * 2);
195 for (int32_t i = 0; i < num_sockets; ++i) {
196 ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno);
197 ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i)));
198 adb_pollfd pfd;
199 pfd.events = POLLIN;
200 pfd.fd = sockets[i * 2 + 1];
201 pfds.push_back(pfd);
202 }
203
204 ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0));
205 for (int i = 0; i < num_sockets; ++i) {
206 ASSERT_NE(0, pfds[i].revents & POLLIN);
207
208 int32_t buf[2] = { -1, -1 };
209 ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t)));
210 ASSERT_EQ(i, buf[0]);
211 }
212
213 for (int fd : sockets) {
214 adb_close(fd);
215 }
216}
Josh Gao903b7492016-05-17 19:23:39 -0700217
Yabin Cuib74c6492016-04-29 16:53:52 -0700218TEST(sysdeps_condition_variable, smoke) {
219 static std::mutex &m = *new std::mutex;
220 static std::condition_variable &cond = *new std::condition_variable;
221 static volatile bool flag = false;
222
223 std::unique_lock<std::mutex> lock(m);
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700224 std::thread thread([]() {
Yabin Cuib74c6492016-04-29 16:53:52 -0700225 m.lock();
226 flag = true;
227 cond.notify_one();
228 m.unlock();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700229 });
Yabin Cuib74c6492016-04-29 16:53:52 -0700230
231 while (!flag) {
232 cond.wait(lock);
233 }
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700234
235 thread.join();
Yabin Cuib74c6492016-04-29 16:53:52 -0700236}