blob: 03cab64a5db37ce4c6ac0df74e377803839aaa2d [file] [log] [blame]
Yabin Cui2ce9d562015-09-15 16:27:09 -07001/*
2 * Copyright (C) 2015 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 "fdevent.h"
18
19#include <gtest/gtest.h>
20
21#include <limits>
22#include <queue>
23#include <string>
24#include <vector>
25
26#include <pthread.h>
27#include <signal.h>
28#include <unistd.h>
29
30#include "adb.h"
31#include "adb_io.h"
32#include "socket.h"
33#include "sysdeps.h"
34
35static void signal_handler(int) {
36 ASSERT_EQ(1u, fdevent_installed_count());
37 pthread_exit(nullptr);
38}
39
40// On host, register a dummy socket, so fdevet_loop() will not abort when previously
41// registered local sockets are all closed. On device, fdevent_subproc_setup() installs
42// one fdevent which can be considered as dummy socket.
43static void InstallDummySocket() {
44#if ADB_HOST
45 int dummy_fds[2];
46 ASSERT_EQ(0, pipe(dummy_fds));
47 asocket* dummy_socket = create_local_socket(dummy_fds[0]);
48 ASSERT_TRUE(dummy_socket != nullptr);
49 dummy_socket->ready(dummy_socket);
50#endif
51}
52
53struct ThreadArg {
54 int first_read_fd;
55 int last_write_fd;
56 size_t middle_pipe_count;
57};
58
59static void FdEventThreadFunc(ThreadArg* arg) {
60 std::vector<int> read_fds;
61 std::vector<int> write_fds;
62
63 read_fds.push_back(arg->first_read_fd);
64 for (size_t i = 0; i < arg->middle_pipe_count; ++i) {
65 int fds[2];
66 ASSERT_EQ(0, adb_socketpair(fds));
67 read_fds.push_back(fds[0]);
68 write_fds.push_back(fds[1]);
69 }
70 write_fds.push_back(arg->last_write_fd);
71
72 for (size_t i = 0; i < read_fds.size(); ++i) {
73 asocket* reader = create_local_socket(read_fds[i]);
74 ASSERT_TRUE(reader != nullptr);
75 asocket* writer = create_local_socket(write_fds[i]);
76 ASSERT_TRUE(writer != nullptr);
77 reader->peer = writer;
78 writer->peer = reader;
79 reader->ready(reader);
80 }
81
82 InstallDummySocket();
83 fdevent_loop();
84}
85
86class LocalSocketTest : public ::testing::Test {
87 protected:
88 static void SetUpTestCase() {
89 ASSERT_NE(SIG_ERR, signal(SIGUSR1, signal_handler));
90 ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN));
91 }
92
93 virtual void SetUp() {
94 fdevent_reset();
95 ASSERT_EQ(0u, fdevent_installed_count());
96 }
97};
98
99TEST_F(LocalSocketTest, smoke) {
100 const size_t PIPE_COUNT = 100;
101 const size_t MESSAGE_LOOP_COUNT = 100;
102 const std::string MESSAGE = "socket_test";
103 int fd_pair1[2];
104 int fd_pair2[2];
105 ASSERT_EQ(0, adb_socketpair(fd_pair1));
106 ASSERT_EQ(0, adb_socketpair(fd_pair2));
107 pthread_t thread;
108 ThreadArg thread_arg;
109 thread_arg.first_read_fd = fd_pair1[0];
110 thread_arg.last_write_fd = fd_pair2[1];
111 thread_arg.middle_pipe_count = PIPE_COUNT;
112 int writer = fd_pair1[1];
113 int reader = fd_pair2[0];
114
115 ASSERT_EQ(0, pthread_create(&thread, nullptr,
116 reinterpret_cast<void* (*)(void*)>(FdEventThreadFunc),
117 &thread_arg));
118
119 usleep(1000);
120 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
121 std::string read_buffer = MESSAGE;
122 std::string write_buffer(MESSAGE.size(), 'a');
123 ASSERT_TRUE(WriteFdExactly(writer, read_buffer.c_str(), read_buffer.size()));
124 ASSERT_TRUE(ReadFdExactly(reader, &write_buffer[0], write_buffer.size()));
125 ASSERT_EQ(read_buffer, write_buffer);
126 }
127 ASSERT_EQ(0, adb_close(writer));
128 ASSERT_EQ(0, adb_close(reader));
129 // Wait until the local sockets are closed.
130 sleep(1);
131
132 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
133 ASSERT_EQ(0, pthread_join(thread, nullptr));
134}
135
136struct CloseWithPacketArg {
137 int socket_fd;
138 size_t bytes_written;
139 int cause_close_fd;
140};
141
142static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
143 asocket* s = create_local_socket(arg->socket_fd);
144 ASSERT_TRUE(s != nullptr);
145 arg->bytes_written = 0;
146 while (true) {
147 apacket* p = get_apacket();
148 p->len = sizeof(p->data);
149 arg->bytes_written += p->len;
150 int ret = s->enqueue(s, p);
151 if (ret == 1) {
152 // The writer has one packet waiting to send.
153 break;
154 }
155 }
156
157 asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
158 ASSERT_TRUE(cause_close_s != nullptr);
159 cause_close_s->peer = s;
160 s->peer = cause_close_s;
161 cause_close_s->ready(cause_close_s);
162
163 InstallDummySocket();
164 fdevent_loop();
165}
166
167// This test checks if we can close local socket in the following situation:
168// The socket is closing but having some packets, so it is not closed. Then
169// some write error happens in the socket's file handler, e.g., the file
170// handler is closed.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700171TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700172 int socket_fd[2];
173 ASSERT_EQ(0, adb_socketpair(socket_fd));
174 int cause_close_fd[2];
175 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
176 CloseWithPacketArg arg;
177 arg.socket_fd = socket_fd[1];
178 arg.cause_close_fd = cause_close_fd[1];
179 pthread_t thread;
180 ASSERT_EQ(0, pthread_create(&thread, nullptr,
181 reinterpret_cast<void* (*)(void*)>(CloseWithPacketThreadFunc),
182 &arg));
183 // Wait until the fdevent_loop() starts.
184 sleep(1);
185 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
186 sleep(1);
187 ASSERT_EQ(2u, fdevent_installed_count());
188 ASSERT_EQ(0, adb_close(socket_fd[0]));
189 // Wait until the socket is closed.
190 sleep(1);
191
192 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
193 ASSERT_EQ(0, pthread_join(thread, nullptr));
194}
195
Yabin Cui2ce9d562015-09-15 16:27:09 -0700196// This test checks if we can read packets from a closing local socket.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700197TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700198 int socket_fd[2];
199 ASSERT_EQ(0, adb_socketpair(socket_fd));
200 int cause_close_fd[2];
201 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
202 CloseWithPacketArg arg;
203 arg.socket_fd = socket_fd[1];
204 arg.cause_close_fd = cause_close_fd[1];
205
206 pthread_t thread;
207 ASSERT_EQ(0, pthread_create(&thread, nullptr,
208 reinterpret_cast<void* (*)(void*)>(CloseWithPacketThreadFunc),
209 &arg));
210 // Wait until the fdevent_loop() starts.
211 sleep(1);
212 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
213 sleep(1);
214 ASSERT_EQ(2u, fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700215
216 // Verify if we can read successfully.
217 std::vector<char> buf(arg.bytes_written);
218 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
219 ASSERT_EQ(0, adb_close(socket_fd[0]));
220
221 // Wait until the socket is closed.
222 sleep(1);
223
224 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
225 ASSERT_EQ(0, pthread_join(thread, nullptr));
226}
227
228// This test checks if we can close local socket in the following situation:
229// The socket is not closed and has some packets. When it fails to write to
230// the socket's file handler because the other end is closed, we check if the
231// socket is closed.
232TEST_F(LocalSocketTest, write_error_when_having_packets) {
233 int socket_fd[2];
234 ASSERT_EQ(0, adb_socketpair(socket_fd));
235 int cause_close_fd[2];
236 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
237 CloseWithPacketArg arg;
238 arg.socket_fd = socket_fd[1];
239 arg.cause_close_fd = cause_close_fd[1];
240
241 pthread_t thread;
242 ASSERT_EQ(0, pthread_create(&thread, nullptr,
243 reinterpret_cast<void* (*)(void*)>(CloseWithPacketThreadFunc),
244 &arg));
245 // Wait until the fdevent_loop() starts.
246 sleep(1);
247 ASSERT_EQ(3u, fdevent_installed_count());
248 ASSERT_EQ(0, adb_close(socket_fd[0]));
249
250 // Wait until the socket is closed.
251 sleep(1);
252
253 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
254 ASSERT_EQ(0, pthread_join(thread, nullptr));
255}
256
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700257#if defined(__linux__)
258
259static void ClientThreadFunc() {
260 std::string error;
261 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
262 ASSERT_GE(fd, 0) << error;
263 sleep(2);
264 ASSERT_EQ(0, adb_close(fd));
265}
266
267struct CloseRdHupSocketArg {
268 int socket_fd;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700269};
270
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700271static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
272 asocket* s = create_local_socket(arg->socket_fd);
273 ASSERT_TRUE(s != nullptr);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700274
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700275 InstallDummySocket();
276 fdevent_loop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700277}
278
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700279// This test checks if we can close sockets in CLOSE_WAIT state.
280TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
281 std::string error;
282 int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
283 ASSERT_GE(listen_fd, 0);
284 pthread_t client_thread;
285 ASSERT_EQ(0, pthread_create(&client_thread, nullptr,
286 reinterpret_cast<void* (*)(void*)>(ClientThreadFunc), nullptr));
Yabin Cui2ce9d562015-09-15 16:27:09 -0700287
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700288 struct sockaddr addr;
289 socklen_t alen;
290 alen = sizeof(addr);
291 int accept_fd = adb_socket_accept(listen_fd, &addr, &alen);
292 ASSERT_GE(accept_fd, 0);
293 CloseRdHupSocketArg arg;
294 arg.socket_fd = accept_fd;
295 pthread_t thread;
296 ASSERT_EQ(0, pthread_create(&thread, nullptr,
297 reinterpret_cast<void* (*)(void*)>(CloseRdHupSocketThreadFunc),
298 &arg));
299 // Wait until the fdevent_loop() starts.
300 sleep(1);
301 ASSERT_EQ(2u, fdevent_installed_count());
302 // Wait until the client closes its socket.
303 ASSERT_EQ(0, pthread_join(client_thread, nullptr));
304 sleep(2);
305 ASSERT_EQ(0, pthread_kill(thread, SIGUSR1));
306 ASSERT_EQ(0, pthread_join(thread, nullptr));
Yabin Cui2ce9d562015-09-15 16:27:09 -0700307}
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700308
309#endif // defined(__linux__)