sysdeps_test.cpp revision 6487e74a5991263cda5e59dbd21710d2372b0fa1
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#include <atomic>
20
21#include "adb_io.h"
22#include "sysdeps.h"
23
24static void increment_atomic_int(void* c) {
25    sleep(1);
26    reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
27}
28
29TEST(sysdeps_thread, smoke) {
30    std::atomic<int> counter(0);
31
32    for (int i = 0; i < 100; ++i) {
33        ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter));
34    }
35
36    sleep(2);
37    ASSERT_EQ(100, counter.load());
38}
39
40TEST(sysdeps_thread, join) {
41    std::atomic<int> counter(0);
42    std::vector<adb_thread_t> threads(500);
43    for (size_t i = 0; i < threads.size(); ++i) {
44        ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i]));
45    }
46
47    int current = counter.load();
48    ASSERT_GE(current, 0);
49    // Make sure that adb_thread_create actually creates threads, and doesn't do something silly
50    // like synchronously run the function passed in. The sleep in increment_atomic_int should be
51    // enough to keep this from being flakey.
52    ASSERT_LT(current, 500);
53
54    for (const auto& thread : threads) {
55        ASSERT_TRUE(adb_thread_join(thread));
56    }
57
58    ASSERT_EQ(500, counter.load());
59}
60
61TEST(sysdeps_thread, exit) {
62    adb_thread_t thread;
63    ASSERT_TRUE(adb_thread_create(
64        [](void*) {
65            adb_thread_exit();
66            for (;;) continue;
67        },
68        nullptr, &thread));
69    ASSERT_TRUE(adb_thread_join(thread));
70}
71
72TEST(sysdeps_socketpair, smoke) {
73    int fds[2];
74    ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
75    ASSERT_TRUE(WriteFdExactly(fds[0], "foo", 4));
76    ASSERT_TRUE(WriteFdExactly(fds[1], "bar", 4));
77
78    char buf[4];
79    ASSERT_TRUE(ReadFdExactly(fds[1], buf, 4));
80    ASSERT_STREQ(buf, "foo");
81    ASSERT_TRUE(ReadFdExactly(fds[0], buf, 4));
82    ASSERT_STREQ(buf, "bar");
83    ASSERT_EQ(0, adb_close(fds[0]));
84    ASSERT_EQ(0, adb_close(fds[1]));
85}
86
87TEST(sysdeps_fd, exhaustion) {
88    std::vector<int> fds;
89    int socketpair[2];
90
91    while (adb_socketpair(socketpair) == 0) {
92        fds.push_back(socketpair[0]);
93        fds.push_back(socketpair[1]);
94    }
95
96    ASSERT_EQ(EMFILE, errno) << strerror(errno);
97    for (int fd : fds) {
98        ASSERT_EQ(0, adb_close(fd));
99    }
100    ASSERT_EQ(0, adb_socketpair(socketpair));
101    ASSERT_EQ(socketpair[0], fds[0]);
102    ASSERT_EQ(socketpair[1], fds[1]);
103    ASSERT_EQ(0, adb_close(socketpair[0]));
104    ASSERT_EQ(0, adb_close(socketpair[1]));
105}
106
107class sysdeps_poll : public ::testing::Test {
108  protected:
109    int fds[2];
110    void SetUp() override {
111        ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
112    }
113
114    void TearDown() override {
115        ASSERT_EQ(0, adb_close(fds[0]));
116        ASSERT_EQ(0, adb_close(fds[1]));
117    }
118};
119
120TEST_F(sysdeps_poll, smoke) {
121    adb_pollfd pfd[2];
122    pfd[0].fd = fds[0];
123    pfd[0].events = POLLRDNORM;
124    pfd[1].fd = fds[1];
125    pfd[1].events = POLLWRNORM;
126
127    EXPECT_EQ(1, adb_poll(pfd, 2, 0));
128    EXPECT_EQ(0, pfd[0].revents);
129    EXPECT_EQ(POLLWRNORM, pfd[1].revents);
130
131    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
132
133    // Wait for the socketpair to be flushed.
134    EXPECT_EQ(1, adb_poll(pfd, 1, 100));
135    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
136
137    EXPECT_EQ(2, adb_poll(pfd, 2, 0));
138    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
139    EXPECT_EQ(POLLWRNORM, pfd[1].revents);
140}
141
142TEST_F(sysdeps_poll, timeout) {
143    adb_pollfd pfd;
144    pfd.fd = fds[0];
145    pfd.events = POLLRDNORM;
146
147    EXPECT_EQ(0, adb_poll(&pfd, 1, 100));
148    EXPECT_EQ(0, pfd.revents);
149
150    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
151
152    EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
153    EXPECT_EQ(POLLRDNORM, pfd.revents);
154}
155
156TEST_F(sysdeps_poll, invalid_fd) {
157    adb_pollfd pfd[3];
158    pfd[0].fd = fds[0];
159    pfd[0].events = POLLRDNORM;
160    pfd[1].fd = INT_MAX;
161    pfd[1].events = POLLRDNORM;
162    pfd[2].fd = fds[1];
163    pfd[2].events = POLLWRNORM;
164
165    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
166
167    // Wait for the socketpair to be flushed.
168    EXPECT_EQ(1, adb_poll(pfd, 1, 100));
169    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
170
171    EXPECT_EQ(3, adb_poll(pfd, 3, 0));
172    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
173    EXPECT_EQ(POLLNVAL, pfd[1].revents);
174    EXPECT_EQ(POLLWRNORM, pfd[2].revents);
175}
176
177TEST_F(sysdeps_poll, duplicate_fd) {
178    adb_pollfd pfd[2];
179    pfd[0].fd = fds[0];
180    pfd[0].events = POLLRDNORM;
181    pfd[1] = pfd[0];
182
183    EXPECT_EQ(0, adb_poll(pfd, 2, 0));
184    EXPECT_EQ(0, pfd[0].revents);
185    EXPECT_EQ(0, pfd[1].revents);
186
187    ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
188
189    EXPECT_EQ(2, adb_poll(pfd, 2, 100));
190    EXPECT_EQ(POLLRDNORM, pfd[0].revents);
191    EXPECT_EQ(POLLRDNORM, pfd[1].revents);
192}
193