• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TEST(sysdeps_socketpair,smoke)28 TEST(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 
TEST(sysdeps_fd,exhaustion)43 TEST(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 
63 class sysdeps_poll : public ::testing::Test {
64   protected:
65     int fds[2];
SetUp()66     void SetUp() override {
67         ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
68     }
69 
TearDown()70     void TearDown() override {
71         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         }
77     }
78 };
79 
TEST_F(sysdeps_poll,smoke)80 TEST_F(sysdeps_poll, smoke) {
81     adb_pollfd pfd[2] = {};
82     pfd[0].fd = fds[0];
83     pfd[0].events = POLLRDNORM;
84     pfd[1].fd = fds[1];
85     pfd[1].events = POLLWRNORM;
86 
87     pfd[0].revents = -1;
88     pfd[1].revents = -1;
89     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.
96     pfd[0].revents = -1;
97     EXPECT_EQ(1, adb_poll(pfd, 1, 100));
98     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
99     pfd[0].revents = -1;
100     pfd[1].revents = -1;
101     EXPECT_EQ(2, adb_poll(pfd, 2, 0));
102     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
103     EXPECT_EQ(POLLWRNORM, pfd[1].revents);
104 }
105 
TEST_F(sysdeps_poll,timeout)106 TEST_F(sysdeps_poll, timeout) {
107     adb_pollfd pfd = {};
108     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 
TEST_F(sysdeps_poll,invalid_fd)120 TEST_F(sysdeps_poll, invalid_fd) {
121     adb_pollfd pfd[3] = {};
122     pfd[0].fd = fds[0];
123     pfd[0].events = POLLRDNORM;
124     pfd[0].revents = ~0;
125     pfd[1].fd = INT_MAX;
126     pfd[1].events = POLLRDNORM;
127     pfd[1].revents = ~0;
128     pfd[2].fd = fds[1];
129     pfd[2].events = POLLWRNORM;
130     pfd[2].revents = ~0;
131 
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);
142 
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);
153 }
154 
TEST_F(sysdeps_poll,duplicate_fd)155 TEST_F(sysdeps_poll, duplicate_fd) {
156     adb_pollfd pfd[2] = {};
157     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 }
171 
TEST_F(sysdeps_poll,disconnect)172 TEST_F(sysdeps_poll, disconnect) {
173     adb_pollfd pfd = {};
174     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 }
188 
TEST_F(sysdeps_poll,fd_count)189 TEST_F(sysdeps_poll, fd_count) {
190     // https://code.google.com/p/android/issues/detail?id=12141
191     static constexpr int num_sockets = 256;
192     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 }
217 
TEST(sysdeps_condition_variable,smoke)218 TEST(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);
224     std::thread thread([]() {
225         m.lock();
226         flag = true;
227         cond.notify_one();
228         m.unlock();
229     });
230 
231     while (!flag) {
232         cond.wait(lock);
233     }
234 
235     thread.join();
236 }
237