blob: 19856dcba7fae752b14bffff12e0213a5518c12a [file] [log] [blame]
Josh Gaoa4f5e032016-02-09 14:59:09 -08001/*
Josh Gaoc285cd42016-02-12 11:33:53 -08002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaoa4f5e032016-02-09 14:59:09 -08003 *
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
Josh Gaoe7388122016-02-16 17:34:53 -080021#include "adb_io.h"
Josh Gaoa4f5e032016-02-09 14:59:09 -080022#include "sysdeps.h"
23
Josh Gao7d405252016-02-12 14:31:15 -080024static void increment_atomic_int(void* c) {
Josh Gaoa4f5e032016-02-09 14:59:09 -080025 sleep(1);
26 reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
Josh Gaoa4f5e032016-02-09 14:59:09 -080027}
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}
Josh Gao7d405252016-02-12 14:31:15 -080060
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}
Josh Gaoe7388122016-02-16 17:34:53 -080071
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
87class sysdeps_poll : public ::testing::Test {
88 protected:
89 int fds[2];
90 void SetUp() override {
91 ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
92 }
93
94 void TearDown() override {
95 ASSERT_EQ(0, adb_close(fds[0]));
96 ASSERT_EQ(0, adb_close(fds[1]));
97 }
98};
99
100TEST_F(sysdeps_poll, smoke) {
101 adb_pollfd pfd[2];
102 pfd[0].fd = fds[0];
103 pfd[0].events = POLLRDNORM;
104 pfd[1].fd = fds[1];
105 pfd[1].events = POLLWRNORM;
106
107 EXPECT_EQ(1, adb_poll(pfd, 2, 0));
108 EXPECT_EQ(0, pfd[0].revents);
109 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
110
111 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
112
113 // Wait for the socketpair to be flushed.
114 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
115 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
116
117 EXPECT_EQ(2, adb_poll(pfd, 2, 0));
118 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
119 EXPECT_EQ(POLLWRNORM, pfd[1].revents);
120}
121
122TEST_F(sysdeps_poll, timeout) {
123 adb_pollfd pfd;
124 pfd.fd = fds[0];
125 pfd.events = POLLRDNORM;
126
127 EXPECT_EQ(0, adb_poll(&pfd, 1, 100));
128 EXPECT_EQ(0, pfd.revents);
129
130 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
131
132 EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
133 EXPECT_EQ(POLLRDNORM, pfd.revents);
134}
135
136TEST_F(sysdeps_poll, invalid_fd) {
137 adb_pollfd pfd[3];
138 pfd[0].fd = fds[0];
139 pfd[0].events = POLLRDNORM;
140 pfd[1].fd = INT_MAX;
141 pfd[1].events = POLLRDNORM;
142 pfd[2].fd = fds[1];
143 pfd[2].events = POLLWRNORM;
144
145 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
146
147 // Wait for the socketpair to be flushed.
148 EXPECT_EQ(1, adb_poll(pfd, 1, 100));
149 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
150
151 EXPECT_EQ(3, adb_poll(pfd, 3, 0));
152 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
153 EXPECT_EQ(POLLNVAL, pfd[1].revents);
154 EXPECT_EQ(POLLWRNORM, pfd[2].revents);
155}
156
157TEST_F(sysdeps_poll, duplicate_fd) {
158 adb_pollfd pfd[2];
159 pfd[0].fd = fds[0];
160 pfd[0].events = POLLRDNORM;
161 pfd[1] = pfd[0];
162
163 EXPECT_EQ(0, adb_poll(pfd, 2, 0));
164 EXPECT_EQ(0, pfd[0].revents);
165 EXPECT_EQ(0, pfd[1].revents);
166
167 ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
168
169 EXPECT_EQ(2, adb_poll(pfd, 2, 100));
170 EXPECT_EQ(POLLRDNORM, pfd[0].revents);
171 EXPECT_EQ(POLLRDNORM, pfd[1].revents);
172}