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>
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
38 static bool IsWine() {
39     return false;
40 }
41 #endif
42 
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 
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];
81     void SetUp() override {
82         ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
83     }
84 
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 
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 
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 
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 
170 TEST_F(sysdeps_poll, duplicate_fd) {
171     adb_pollfd pfd[2] = {};
172     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 }
186 
187 TEST_F(sysdeps_poll, disconnect) {
188     adb_pollfd pfd = {};
189     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 
200     if (!IsWine()) {
201         // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
202         EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
203     }
204 }
205 
206 TEST_F(sysdeps_poll, fd_count) {
207     // https://code.google.com/p/android/issues/detail?id=12141
208     static constexpr int num_sockets = 256;
209     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 }
234 
235 TEST(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);
241     std::thread thread([]() {
242         m.lock();
243         flag = true;
244         cond.notify_one();
245         m.unlock();
246     });
247 
248     while (!flag) {
249         cond.wait(lock);
250     }
251 
252     thread.join();
253 }
254