blob: c818fca0999aff3b781579ae99118a300b3c1bd1 [file] [log] [blame]
Yabin Cuic1b1f6f2015-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
Josh Gao022d4472016-02-10 14:49:00 -080021#include <array>
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070022#include <limits>
23#include <queue>
24#include <string>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080025#include <thread>
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070026#include <vector>
27
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070028#include <unistd.h>
29
30#include "adb.h"
31#include "adb_io.h"
Josh Gao022d4472016-02-10 14:49:00 -080032#include "fdevent_test.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070033#include "socket.h"
34#include "sysdeps.h"
Josh Gao4602adb2016-11-15 18:55:47 -080035#include "sysdeps/chrono.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070036
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070037struct ThreadArg {
38 int first_read_fd;
39 int last_write_fd;
40 size_t middle_pipe_count;
41};
42
Josh Gao022d4472016-02-10 14:49:00 -080043class LocalSocketTest : public FdeventTest {};
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070044
Josh Gao4602adb2016-11-15 18:55:47 -080045constexpr auto SLEEP_FOR_FDEVENT = 100ms;
Yabin Cui2407d7c2016-04-25 19:48:19 -070046
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070047TEST_F(LocalSocketTest, smoke) {
Josh Gao022d4472016-02-10 14:49:00 -080048 // Join two socketpairs with a chain of intermediate socketpairs.
49 int first[2];
50 std::vector<std::array<int, 2>> intermediates;
51 int last[2];
52
53 constexpr size_t INTERMEDIATE_COUNT = 50;
54 constexpr size_t MESSAGE_LOOP_COUNT = 100;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070055 const std::string MESSAGE = "socket_test";
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070056
Josh Gao022d4472016-02-10 14:49:00 -080057 intermediates.resize(INTERMEDIATE_COUNT);
58 ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno);
59 ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno);
60 asocket* prev_tail = create_local_socket(first[1]);
61 ASSERT_NE(nullptr, prev_tail);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070062
Josh Gao022d4472016-02-10 14:49:00 -080063 auto connect = [](asocket* tail, asocket* head) {
64 tail->peer = head;
65 head->peer = tail;
66 tail->ready(tail);
67 };
68
69 for (auto& intermediate : intermediates) {
70 ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno);
71
72 asocket* head = create_local_socket(intermediate[0]);
73 ASSERT_NE(nullptr, head);
74
75 asocket* tail = create_local_socket(intermediate[1]);
76 ASSERT_NE(nullptr, tail);
77
78 connect(prev_tail, head);
79 prev_tail = tail;
80 }
81
82 asocket* end = create_local_socket(last[0]);
83 ASSERT_NE(nullptr, end);
84 connect(prev_tail, end);
85
86 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -070087 std::thread thread(fdevent_loop);
Josh Gao022d4472016-02-10 14:49:00 -080088
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070089 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
90 std::string read_buffer = MESSAGE;
91 std::string write_buffer(MESSAGE.size(), 'a');
Josh Gao022d4472016-02-10 14:49:00 -080092 ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
93 ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070094 ASSERT_EQ(read_buffer, write_buffer);
95 }
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070096
Josh Gao022d4472016-02-10 14:49:00 -080097 ASSERT_EQ(0, adb_close(first[0]));
98 ASSERT_EQ(0, adb_close(last[1]));
99
100 // Wait until the local sockets are closed.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800101 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700102 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800103 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700104}
105
106struct CloseWithPacketArg {
107 int socket_fd;
108 size_t bytes_written;
109 int cause_close_fd;
110};
111
112static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
113 asocket* s = create_local_socket(arg->socket_fd);
114 ASSERT_TRUE(s != nullptr);
115 arg->bytes_written = 0;
116 while (true) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800117 std::string data;
118 data.resize(MAX_PAYLOAD);
119 arg->bytes_written += data.size();
120 int ret = s->enqueue(s, std::move(data));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700121 if (ret == 1) {
122 // The writer has one packet waiting to send.
123 break;
124 }
125 }
126
127 asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
128 ASSERT_TRUE(cause_close_s != nullptr);
129 cause_close_s->peer = s;
130 s->peer = cause_close_s;
131 cause_close_s->ready(cause_close_s);
132
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700133 fdevent_loop();
134}
135
136// This test checks if we can close local socket in the following situation:
137// The socket is closing but having some packets, so it is not closed. Then
138// some write error happens in the socket's file handler, e.g., the file
139// handler is closed.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700140TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700141 int socket_fd[2];
142 ASSERT_EQ(0, adb_socketpair(socket_fd));
143 int cause_close_fd[2];
144 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
145 CloseWithPacketArg arg;
146 arg.socket_fd = socket_fd[1];
147 arg.cause_close_fd = cause_close_fd[1];
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700148
Josh Gao022d4472016-02-10 14:49:00 -0800149 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700150 std::thread thread(CloseWithPacketThreadFunc, &arg);
Josh Gao022d4472016-02-10 14:49:00 -0800151 // Wait until the fdevent_loop() starts.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800152 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Josh Gao022d4472016-02-10 14:49:00 -0800153 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800154 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700155 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800156 ASSERT_EQ(0, adb_close(socket_fd[0]));
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800157 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700158 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800159 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700160}
161
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700162// This test checks if we can read packets from a closing local socket.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700163TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700164 int socket_fd[2];
165 ASSERT_EQ(0, adb_socketpair(socket_fd));
166 int cause_close_fd[2];
167 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
168 CloseWithPacketArg arg;
169 arg.socket_fd = socket_fd[1];
170 arg.cause_close_fd = cause_close_fd[1];
171
Josh Gao022d4472016-02-10 14:49:00 -0800172 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700173 std::thread thread(CloseWithPacketThreadFunc, &arg);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700174 // Wait until the fdevent_loop() starts.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800175 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700176 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800177 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700178 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700179
180 // Verify if we can read successfully.
181 std::vector<char> buf(arg.bytes_written);
Josh Gao022d4472016-02-10 14:49:00 -0800182 ASSERT_NE(0u, arg.bytes_written);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700183 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
184 ASSERT_EQ(0, adb_close(socket_fd[0]));
185
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800186 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700187 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800188 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700189}
190
191// This test checks if we can close local socket in the following situation:
192// The socket is not closed and has some packets. When it fails to write to
193// the socket's file handler because the other end is closed, we check if the
194// socket is closed.
195TEST_F(LocalSocketTest, write_error_when_having_packets) {
196 int socket_fd[2];
197 ASSERT_EQ(0, adb_socketpair(socket_fd));
198 int cause_close_fd[2];
199 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
200 CloseWithPacketArg arg;
201 arg.socket_fd = socket_fd[1];
202 arg.cause_close_fd = cause_close_fd[1];
203
Josh Gao022d4472016-02-10 14:49:00 -0800204 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700205 std::thread thread(CloseWithPacketThreadFunc, &arg);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700206 // Wait until the fdevent_loop() starts.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800207 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700208 EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700209 ASSERT_EQ(0, adb_close(socket_fd[0]));
210
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800211 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700212 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800213 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700214}
215
Josh Gao1430d392018-03-15 15:28:55 -0700216#if 0
217// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
218TEST_F(LocalSocketTest, flush_after_shutdown) {
219 int head_fd[2];
220 int tail_fd[2];
221 ASSERT_EQ(0, adb_socketpair(head_fd));
222 ASSERT_EQ(0, adb_socketpair(tail_fd));
223
224 asocket* head = create_local_socket(head_fd[1]);
225 asocket* tail = create_local_socket(tail_fd[1]);
226
227 head->peer = tail;
228 head->ready(head);
229
230 tail->peer = head;
231 tail->ready(tail);
232
233 PrepareThread();
234 std::thread thread(fdevent_loop);
235
236 ASSERT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
237 ASSERT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
238 const char* str = "write succeeds, but local_socket will fail to write";
239 ASSERT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
240 ASSERT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
241 char buf[6];
242 ASSERT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
243
244 ASSERT_EQ(0, memcmp(buf, "foobar", 6));
245
246 adb_close(head_fd[0]);
247 adb_close(tail_fd[0]);
248
249 // Wait until the local sockets are closed.
250 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
251 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
252 TerminateThread(thread);
253}
254#endif
255
Yabin Cuiaa77e222015-09-29 12:25:33 -0700256#if defined(__linux__)
257
258static void ClientThreadFunc() {
259 std::string error;
260 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
261 ASSERT_GE(fd, 0) << error;
Josh Gao4602adb2016-11-15 18:55:47 -0800262 std::this_thread::sleep_for(200ms);
Yabin Cuiaa77e222015-09-29 12:25:33 -0700263 ASSERT_EQ(0, adb_close(fd));
264}
265
266struct CloseRdHupSocketArg {
Josh Gao022d4472016-02-10 14:49:00 -0800267 int socket_fd;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700268};
269
Yabin Cuiaa77e222015-09-29 12:25:33 -0700270static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
Josh Gao022d4472016-02-10 14:49:00 -0800271 asocket* s = create_local_socket(arg->socket_fd);
272 ASSERT_TRUE(s != nullptr);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700273
Josh Gao022d4472016-02-10 14:49:00 -0800274 fdevent_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700275}
276
Yabin Cuiaa77e222015-09-29 12:25:33 -0700277// This test checks if we can close sockets in CLOSE_WAIT state.
278TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao022d4472016-02-10 14:49:00 -0800279 std::string error;
280 int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
281 ASSERT_GE(listen_fd, 0);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700282
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700283 std::thread client_thread(ClientThreadFunc);
Josh Gao022d4472016-02-10 14:49:00 -0800284
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700285 int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
Josh Gao022d4472016-02-10 14:49:00 -0800286 ASSERT_GE(accept_fd, 0);
287 CloseRdHupSocketArg arg;
288 arg.socket_fd = accept_fd;
289
290 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700291 std::thread thread(CloseRdHupSocketThreadFunc, &arg);
Josh Gao022d4472016-02-10 14:49:00 -0800292
293 // Wait until the fdevent_loop() starts.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800294 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700295 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800296
297 // Wait until the client closes its socket.
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700298 client_thread.join();
Josh Gao022d4472016-02-10 14:49:00 -0800299
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800300 std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
Yabin Cui2407d7c2016-04-25 19:48:19 -0700301 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800302 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700303}
Yabin Cuiaa77e222015-09-29 12:25:33 -0700304
305#endif // defined(__linux__)
David Pursell3f902aa2016-03-01 08:58:26 -0800306
307#if ADB_HOST
308
309// Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches
310// |expected|, otherwise logs the failure to gtest.
Dan Austinb4cff492016-03-28 15:32:37 -0700311void VerifySkipHostSerial(std::string serial, const char* expected) {
312 char* result = internal::skip_host_serial(&serial[0]);
David Pursell3f902aa2016-03-01 08:58:26 -0800313 if (expected == nullptr) {
314 EXPECT_EQ(nullptr, result);
315 } else {
316 EXPECT_STREQ(expected, result);
317 }
318}
319
320// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
321TEST(socket_test, test_skip_host_serial) {
322 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
323 VerifySkipHostSerial(protocol, nullptr);
324 VerifySkipHostSerial(protocol + "foo", nullptr);
325
326 VerifySkipHostSerial(protocol + "foo:bar", ":bar");
327 VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz");
328
329 VerifySkipHostSerial(protocol + "foo:123:bar", ":bar");
330 VerifySkipHostSerial(protocol + "foo:123:456", ":456");
331 VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz");
332
333 // Don't register a port unless it's all numbers and ends with ':'.
334 VerifySkipHostSerial(protocol + "foo:123", ":123");
335 VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz");
David Pursell73d55aa2016-09-21 12:08:37 -0700336
337 VerifySkipHostSerial(protocol + "100.100.100.100:5555:foo", ":foo");
338 VerifySkipHostSerial(protocol + "[0123:4567:89ab:CDEF:0:9:a:f]:5555:foo", ":foo");
339 VerifySkipHostSerial(protocol + "[::1]:5555:foo", ":foo");
340
341 // If we can't find both [] then treat it as a normal serial with [ in it.
342 VerifySkipHostSerial(protocol + "[0123:foo", ":foo");
343
344 // Don't be fooled by random IPv6 addresses in the command string.
345 VerifySkipHostSerial(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
346 ":ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
David Pursell3f902aa2016-03-01 08:58:26 -0800347 }
348}
349
350// Check <prefix>:<serial>:<command> format.
351TEST(socket_test, test_skip_host_serial_prefix) {
352 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
353 VerifySkipHostSerial(prefix, nullptr);
354 VerifySkipHostSerial(prefix + "foo", nullptr);
355
356 VerifySkipHostSerial(prefix + "foo:bar", ":bar");
357 VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz");
358 VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar");
359 }
360}
361
362#endif // ADB_HOST