• 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[1].fd = INT_MAX;
125     pfd[1].events = POLLRDNORM;
126     pfd[2].fd = fds[1];
127     pfd[2].events = POLLWRNORM;
128 
129     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
130 
131     // Wait for the socketpair to be flushed.
132     EXPECT_EQ(1, adb_poll(pfd, 1, 100));
133     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
134 
135     EXPECT_EQ(3, adb_poll(pfd, 3, 0));
136     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
137     EXPECT_EQ(POLLNVAL, pfd[1].revents);
138     EXPECT_EQ(POLLWRNORM, pfd[2].revents);
139 }
140 
TEST_F(sysdeps_poll,duplicate_fd)141 TEST_F(sysdeps_poll, duplicate_fd) {
142     adb_pollfd pfd[2] = {};
143     pfd[0].fd = fds[0];
144     pfd[0].events = POLLRDNORM;
145     pfd[1] = pfd[0];
146 
147     EXPECT_EQ(0, adb_poll(pfd, 2, 0));
148     EXPECT_EQ(0, pfd[0].revents);
149     EXPECT_EQ(0, pfd[1].revents);
150 
151     ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
152 
153     EXPECT_EQ(2, adb_poll(pfd, 2, 100));
154     EXPECT_EQ(POLLRDNORM, pfd[0].revents);
155     EXPECT_EQ(POLLRDNORM, pfd[1].revents);
156 }
157 
TEST_F(sysdeps_poll,disconnect)158 TEST_F(sysdeps_poll, disconnect) {
159     adb_pollfd pfd = {};
160     pfd.fd = fds[0];
161     pfd.events = POLLIN;
162 
163     EXPECT_EQ(0, adb_poll(&pfd, 1, 0));
164     EXPECT_EQ(0, pfd.revents);
165 
166     EXPECT_EQ(0, adb_close(fds[1]));
167     fds[1] = -1;
168 
169     EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
170 
171     // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
172     EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
173 }
174 
TEST_F(sysdeps_poll,fd_count)175 TEST_F(sysdeps_poll, fd_count) {
176     // https://code.google.com/p/android/issues/detail?id=12141
177     static constexpr int num_sockets = 256;
178     std::vector<int> sockets;
179     std::vector<adb_pollfd> pfds;
180     sockets.resize(num_sockets * 2);
181     for (int32_t i = 0; i < num_sockets; ++i) {
182         ASSERT_EQ(0, adb_socketpair(&sockets[i * 2])) << strerror(errno);
183         ASSERT_TRUE(WriteFdExactly(sockets[i * 2], &i, sizeof(i)));
184         adb_pollfd pfd;
185         pfd.events = POLLIN;
186         pfd.fd = sockets[i * 2 + 1];
187         pfds.push_back(pfd);
188     }
189 
190     ASSERT_EQ(num_sockets, adb_poll(pfds.data(), pfds.size(), 0));
191     for (int i = 0; i < num_sockets; ++i) {
192         ASSERT_NE(0, pfds[i].revents & POLLIN);
193 
194         int32_t buf[2] = { -1, -1 };
195         ASSERT_EQ(adb_read(pfds[i].fd, buf, sizeof(buf)), static_cast<ssize_t>(sizeof(int32_t)));
196         ASSERT_EQ(i, buf[0]);
197     }
198 
199     for (int fd : sockets) {
200         adb_close(fd);
201     }
202 }
203 
TEST(sysdeps_condition_variable,smoke)204 TEST(sysdeps_condition_variable, smoke) {
205     static std::mutex &m = *new std::mutex;
206     static std::condition_variable &cond = *new std::condition_variable;
207     static volatile bool flag = false;
208 
209     std::unique_lock<std::mutex> lock(m);
210     std::thread thread([]() {
211         m.lock();
212         flag = true;
213         cond.notify_one();
214         m.unlock();
215     });
216 
217     while (!flag) {
218         cond.wait(lock);
219     }
220 
221     thread.join();
222 }
223