1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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>
19 
20 #include <atomic>
21 #include <condition_variable>
22 #include <thread>
23 
24 #include "adb_io.h"
25 #include "sysdeps.h"
26 #include "sysdeps/chrono.h"
27 
28 #if defined(_WIN32)
29 #include <windows.h>
IsWine()30 static 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
IsWine()38 static bool IsWine() {
39     return false;
40 }
41 #endif
42 
TEST(sysdeps_socketpair,smoke)43 TEST(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 
TEST(sysdeps_fd,exhaustion)58 TEST(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 
78 class sysdeps_poll : public ::testing::Test {
79   protected:
80     int fds[2];
SetUp()81     void SetUp() override {
82         ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
83     }
84 
TearDown()85     void TearDown() override {
86         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         }
92     }
93 };
94 
TEST_F(sysdeps_poll,smoke)95 TEST_F(sysdeps_poll, smoke) {
96     adb_pollfd pfd[2] = {};
97     pfd[0].fd = fds[0];
98     pfd[0].events = POLLRDNORM;
99     pfd[1].fd = fds[1];
100     pfd[1].events = POLLWRNORM;
101 
102     pfd[0].revents = -1;
103     pfd[1].revents = -1;
104     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.
111     pfd[0].revents = -1;
112     EXPECT_EQ(1, adb_poll(pfd, 1, 100));
113     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
114     pfd[0].revents = -1;
115     pfd[1].revents = -1;
116     EXPECT_EQ(2, adb_poll(pfd, 2, 0));
117     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
118     EXPECT_EQ(POLLWRNORM, pfd[1].revents);
119 }
120 
TEST_F(sysdeps_poll,timeout)121 TEST_F(sysdeps_poll, timeout) {
122     adb_pollfd pfd = {};
123     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 
TEST_F(sysdeps_poll,invalid_fd)135 TEST_F(sysdeps_poll, invalid_fd) {
136     adb_pollfd pfd[3] = {};
137     pfd[0].fd = fds[0];
138     pfd[0].events = POLLRDNORM;
139     pfd[0].revents = ~0;
140     pfd[1].fd = INT_MAX;
141     pfd[1].events = POLLRDNORM;
142     pfd[1].revents = ~0;
143     pfd[2].fd = fds[1];
144     pfd[2].events = POLLWRNORM;
145     pfd[2].revents = ~0;
146 
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);
157 
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);
168 }
169 
TEST_F(sysdeps_poll,duplicate_fd)170 TEST_F(sysdeps_poll, duplicate_fd) {
171 #ifndef __APPLE__
172     adb_pollfd pfd[2] = {};
173     pfd[0].fd = fds[0];
174     pfd[0].events = POLLRDNORM;
175     pfd[1] = pfd[0];
176 
177     EXPECT_EQ(0, adb_poll(pfd, 2, 0));
178     EXPECT_EQ(0, pfd[0].revents);
179     EXPECT_EQ(0, pfd[1].revents);
180 
181     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
182 
183     EXPECT_EQ(2, adb_poll(pfd, 2, 100));  // On __APPLE__, only *one* fd is set.
184     // Like so: EXPECT_EQ(1, adb_poll(pfd, 2, 100));
185 
186     EXPECT_EQ(POLLRDNORM, pfd[0].revents);  // On __APPLE__, it appears there is
187     // *no* data to be read (even for the single fd): EXPECT_EQ(0, pfd[0].revents);
188 
189     EXPECT_EQ(POLLRDNORM, pfd[1].revents);
190 #else
191     // Behavior on __APPLE__ diverges from BSD.
192     GTEST_SKIP() << " When poll() returns on __APPLE__, only the read socket is set";
193 #endif
194 }
195 
TEST_F(sysdeps_poll,disconnect)196 TEST_F(sysdeps_poll, disconnect) {
197     adb_pollfd pfd = {};
198     pfd.fd = fds[0];
199     pfd.events = POLLIN;
200 
201     EXPECT_EQ(0, adb_poll(&pfd, 1, 0));
202     EXPECT_EQ(0, pfd.revents);
203 
204     EXPECT_EQ(0, adb_close(fds[1]));
205     fds[1] = -1;
206 
207     EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
208 
209     if (!IsWine()) {
210         // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
211         EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
212     }
213 }
214 
TEST_F(sysdeps_poll,fd_count)215 TEST_F(sysdeps_poll, fd_count) {
216     // https://code.google.com/p/android/issues/detail?id=12141
217     static constexpr int num_sockets = 256;
218     std::vector<int> sockets;
219     std::vector<adb_pollfd> pfds;
220     sockets.resize(num_sockets * 2);
221     for (int32_t i = 0; i < num_sockets; ++i) {
222         ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno);
223         ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i)));
224         adb_pollfd pfd;
225         pfd.events = POLLIN;
226         pfd.fd = sockets[i * 2 + 1];
227         pfds.push_back(pfd);
228     }
229 
230     ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0));
231     for (int i = 0; i < num_sockets; ++i) {
232         ASSERT_NE(0, pfds[i].revents & POLLIN);
233 
234         int32_t buf[2] = { -1, -1 };
235         ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t)));
236         ASSERT_EQ(i, buf[0]);
237     }
238 
239     for (int fd : sockets) {
240         adb_close(fd);
241     }
242 }
243 
TEST(sysdeps_condition_variable,smoke)244 TEST(sysdeps_condition_variable, smoke) {
245     static std::mutex &m = *new std::mutex;
246     static std::condition_variable &cond = *new std::condition_variable;
247     static volatile bool flag = false;
248 
249     std::unique_lock<std::mutex> lock(m);
250     std::thread thread([]() {
251         m.lock();
252         flag = true;
253         cond.notify_one();
254         m.unlock();
255     });
256 
257     while (!flag) {
258         cond.wait(lock);
259     }
260 
261     thread.join();
262 }
263