blob: 80f9430b73f7600f7d82ae33bbd177833fa3fac5 [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
Josh Gaobd767202018-12-19 13:37:41 -080037using namespace std::string_literals;
38using namespace std::string_view_literals;
39
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070040struct ThreadArg {
41 int first_read_fd;
42 int last_write_fd;
43 size_t middle_pipe_count;
44};
45
Josh Gao022d4472016-02-10 14:49:00 -080046class LocalSocketTest : public FdeventTest {};
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070047
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070048TEST_F(LocalSocketTest, smoke) {
Josh Gao022d4472016-02-10 14:49:00 -080049 // Join two socketpairs with a chain of intermediate socketpairs.
50 int first[2];
51 std::vector<std::array<int, 2>> intermediates;
52 int last[2];
53
54 constexpr size_t INTERMEDIATE_COUNT = 50;
55 constexpr size_t MESSAGE_LOOP_COUNT = 100;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070056 const std::string MESSAGE = "socket_test";
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070057
Josh Gao022d4472016-02-10 14:49:00 -080058 intermediates.resize(INTERMEDIATE_COUNT);
59 ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno);
60 ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno);
61 asocket* prev_tail = create_local_socket(first[1]);
62 ASSERT_NE(nullptr, prev_tail);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070063
Josh Gao022d4472016-02-10 14:49:00 -080064 auto connect = [](asocket* tail, asocket* head) {
65 tail->peer = head;
66 head->peer = tail;
67 tail->ready(tail);
68 };
69
70 for (auto& intermediate : intermediates) {
71 ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno);
72
73 asocket* head = create_local_socket(intermediate[0]);
74 ASSERT_NE(nullptr, head);
75
76 asocket* tail = create_local_socket(intermediate[1]);
77 ASSERT_NE(nullptr, tail);
78
79 connect(prev_tail, head);
80 prev_tail = tail;
81 }
82
83 asocket* end = create_local_socket(last[0]);
84 ASSERT_NE(nullptr, end);
85 connect(prev_tail, end);
86
87 PrepareThread();
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.
Josh Gaofd7486f2018-03-28 18:53:30 -0700101 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700102 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao7ab55712018-03-29 16:27:13 -0700103 TerminateThread();
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
Josh Gao7ab55712018-03-29 16:27:13 -0700112static void CreateCloser(CloseWithPacketArg* arg) {
113 fdevent_run_on_main_thread([arg]() {
114 asocket* s = create_local_socket(arg->socket_fd);
115 ASSERT_TRUE(s != nullptr);
116 arg->bytes_written = 0;
Josh Gaoecb96ac2018-03-19 15:36:17 -0700117
Josh Gao7651eb92018-03-30 14:05:40 -0700118 // On platforms that implement sockets via underlying sockets (e.g. Wine),
119 // a socket can appear to be full, and then become available for writes
120 // again without read being called on the other end. Loop and sleep after
121 // each write to give the underlying implementation time to flush.
122 bool socket_filled = false;
123 for (int i = 0; i < 128; ++i) {
Josh Gao1ce99572018-03-07 16:52:28 -0800124 apacket::payload_type data;
Josh Gao7651eb92018-03-30 14:05:40 -0700125 data.resize(MAX_PAYLOAD);
126 arg->bytes_written += data.size();
127 int ret = s->enqueue(s, std::move(data));
128 if (ret == 1) {
129 socket_filled = true;
130 break;
131 }
132 ASSERT_NE(-1, ret);
133
134 std::this_thread::sleep_for(250ms);
135 }
136 ASSERT_TRUE(socket_filled);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700137
Josh Gao7ab55712018-03-29 16:27:13 -0700138 asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
139 ASSERT_TRUE(cause_close_s != nullptr);
140 cause_close_s->peer = s;
141 s->peer = cause_close_s;
142 cause_close_s->ready(cause_close_s);
143 });
144 WaitForFdeventLoop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700145}
146
147// This test checks if we can close local socket in the following situation:
148// The socket is closing but having some packets, so it is not closed. Then
149// some write error happens in the socket's file handler, e.g., the file
150// handler is closed.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700151TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700152 int socket_fd[2];
153 ASSERT_EQ(0, adb_socketpair(socket_fd));
154 int cause_close_fd[2];
155 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
156 CloseWithPacketArg arg;
157 arg.socket_fd = socket_fd[1];
158 arg.cause_close_fd = cause_close_fd[1];
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700159
Josh Gao022d4472016-02-10 14:49:00 -0800160 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700161 CreateCloser(&arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700162
Josh Gao022d4472016-02-10 14:49:00 -0800163 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700164
165 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700166 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800167 ASSERT_EQ(0, adb_close(socket_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700168
169 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700170 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao7ab55712018-03-29 16:27:13 -0700171 TerminateThread();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700172}
173
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700174// This test checks if we can read packets from a closing local socket.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700175TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700176 int socket_fd[2];
177 ASSERT_EQ(0, adb_socketpair(socket_fd));
178 int cause_close_fd[2];
179 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
180 CloseWithPacketArg arg;
181 arg.socket_fd = socket_fd[1];
182 arg.cause_close_fd = cause_close_fd[1];
183
Josh Gao022d4472016-02-10 14:49:00 -0800184 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700185 CreateCloser(&arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700186
187 WaitForFdeventLoop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700188 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700189
190 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700191 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700192
193 // Verify if we can read successfully.
194 std::vector<char> buf(arg.bytes_written);
Josh Gao022d4472016-02-10 14:49:00 -0800195 ASSERT_NE(0u, arg.bytes_written);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700196 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
197 ASSERT_EQ(0, adb_close(socket_fd[0]));
198
Josh Gaofd7486f2018-03-28 18:53:30 -0700199 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700200 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao7ab55712018-03-29 16:27:13 -0700201 TerminateThread();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700202}
203
204// This test checks if we can close local socket in the following situation:
205// The socket is not closed and has some packets. When it fails to write to
206// the socket's file handler because the other end is closed, we check if the
207// socket is closed.
208TEST_F(LocalSocketTest, write_error_when_having_packets) {
209 int socket_fd[2];
210 ASSERT_EQ(0, adb_socketpair(socket_fd));
211 int cause_close_fd[2];
212 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
213 CloseWithPacketArg arg;
214 arg.socket_fd = socket_fd[1];
215 arg.cause_close_fd = cause_close_fd[1];
216
Josh Gao022d4472016-02-10 14:49:00 -0800217 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700218 CreateCloser(&arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700219
220 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700221 EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700222 ASSERT_EQ(0, adb_close(socket_fd[0]));
223
Josh Gaofd7486f2018-03-28 18:53:30 -0700224 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700225 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao7ab55712018-03-29 16:27:13 -0700226 TerminateThread();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700227}
228
Josh Gaodf3bae92018-03-15 15:28:55 -0700229// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
230TEST_F(LocalSocketTest, flush_after_shutdown) {
231 int head_fd[2];
232 int tail_fd[2];
233 ASSERT_EQ(0, adb_socketpair(head_fd));
234 ASSERT_EQ(0, adb_socketpair(tail_fd));
235
236 asocket* head = create_local_socket(head_fd[1]);
237 asocket* tail = create_local_socket(tail_fd[1]);
238
239 head->peer = tail;
240 head->ready(head);
241
242 tail->peer = head;
243 tail->ready(tail);
244
245 PrepareThread();
Josh Gaodf3bae92018-03-15 15:28:55 -0700246
Josh Gaoecb96ac2018-03-19 15:36:17 -0700247 EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
Josh Gaodf3bae92018-03-15 15:28:55 -0700248
Josh Gaoecb96ac2018-03-19 15:36:17 -0700249 EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
250 const char* str = "write succeeds, but local_socket will fail to write";
251 EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
252 EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
253
254 char buf[6];
255 EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
256 EXPECT_EQ(0, memcmp(buf, "foobar", 6));
Josh Gaodf3bae92018-03-15 15:28:55 -0700257
258 adb_close(head_fd[0]);
259 adb_close(tail_fd[0]);
260
Josh Gaofd7486f2018-03-28 18:53:30 -0700261 WaitForFdeventLoop();
Josh Gaodf3bae92018-03-15 15:28:55 -0700262 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao7ab55712018-03-29 16:27:13 -0700263 TerminateThread();
Josh Gaodf3bae92018-03-15 15:28:55 -0700264}
Josh Gaodf3bae92018-03-15 15:28:55 -0700265
Yabin Cuiaa77e222015-09-29 12:25:33 -0700266#if defined(__linux__)
267
268static void ClientThreadFunc() {
269 std::string error;
270 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
271 ASSERT_GE(fd, 0) << error;
Josh Gao7ab55712018-03-29 16:27:13 -0700272 std::this_thread::sleep_for(1s);
Yabin Cuiaa77e222015-09-29 12:25:33 -0700273 ASSERT_EQ(0, adb_close(fd));
274}
275
Yabin Cuiaa77e222015-09-29 12:25:33 -0700276// This test checks if we can close sockets in CLOSE_WAIT state.
277TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao022d4472016-02-10 14:49:00 -0800278 std::string error;
279 int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
280 ASSERT_GE(listen_fd, 0);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700281
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700282 std::thread client_thread(ClientThreadFunc);
Josh Gao022d4472016-02-10 14:49:00 -0800283
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700284 int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
Josh Gao022d4472016-02-10 14:49:00 -0800285 ASSERT_GE(accept_fd, 0);
Josh Gao022d4472016-02-10 14:49:00 -0800286
287 PrepareThread();
Josh Gao7ab55712018-03-29 16:27:13 -0700288
289 fdevent_run_on_main_thread([accept_fd]() {
290 asocket* s = create_local_socket(accept_fd);
291 ASSERT_TRUE(s != nullptr);
292 });
Josh Gao022d4472016-02-10 14:49:00 -0800293
Josh Gaofd7486f2018-03-28 18:53:30 -0700294 WaitForFdeventLoop();
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
Josh Gaofd7486f2018-03-28 18:53:30 -0700300 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700301 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao7ab55712018-03-29 16:27:13 -0700302 TerminateThread();
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
Josh Gaobd767202018-12-19 13:37:41 -0800309#define VerifyParseHostServiceFailed(s) \
310 do { \
311 std::string service(s); \
312 std::string_view serial, command; \
313 bool result = internal::parse_host_service(&serial, &command, service); \
314 EXPECT_FALSE(result); \
315 } while (0)
316
317#define VerifyParseHostService(s, expected_serial, expected_command) \
318 do { \
319 std::string service(s); \
320 std::string_view serial, command; \
321 bool result = internal::parse_host_service(&serial, &command, service); \
322 EXPECT_TRUE(result); \
323 EXPECT_EQ(std::string(expected_serial), std::string(serial)); \
324 EXPECT_EQ(std::string(expected_command), std::string(command)); \
325 } while (0);
David Pursell3f902aa2016-03-01 08:58:26 -0800326
327// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
Josh Gaobd767202018-12-19 13:37:41 -0800328TEST(socket_test, test_parse_host_service) {
David Pursell3f902aa2016-03-01 08:58:26 -0800329 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
Josh Gaobd767202018-12-19 13:37:41 -0800330 VerifyParseHostServiceFailed(protocol);
331 VerifyParseHostServiceFailed(protocol + "foo");
David Pursell3f902aa2016-03-01 08:58:26 -0800332
Josh Gaobd767202018-12-19 13:37:41 -0800333 {
334 std::string serial = protocol + "foo";
335 VerifyParseHostService(serial + ":bar", serial, "bar");
336 VerifyParseHostService(serial + " :bar:baz", serial, "bar:baz");
337 }
David Pursell3f902aa2016-03-01 08:58:26 -0800338
Josh Gaobd767202018-12-19 13:37:41 -0800339 {
340 // With port.
341 std::string serial = protocol + "foo:123";
342 VerifyParseHostService(serial + ":bar", serial, "bar");
343 VerifyParseHostService(serial + ":456", serial, "456");
344 VerifyParseHostService(serial + ":bar:baz", serial, "bar:baz");
345 }
David Pursell3f902aa2016-03-01 08:58:26 -0800346
347 // Don't register a port unless it's all numbers and ends with ':'.
Josh Gaobd767202018-12-19 13:37:41 -0800348 VerifyParseHostService(protocol + "foo:123", protocol + "foo", "123");
349 VerifyParseHostService(protocol + "foo:123bar:baz", protocol + "foo", "123bar:baz");
David Pursell73d55aa2016-09-21 12:08:37 -0700350
Josh Gaobd767202018-12-19 13:37:41 -0800351 std::string addresses[] = {"100.100.100.100", "[0123:4567:89ab:CDEF:0:9:a:f]", "[::1]"};
352 for (const std::string& address : addresses) {
353 std::string serial = protocol + address;
354 std::string serial_with_port = protocol + address + ":5555";
355 VerifyParseHostService(serial + ":foo", serial, "foo");
356 VerifyParseHostService(serial_with_port + ":foo", serial_with_port, "foo");
357 }
David Pursell73d55aa2016-09-21 12:08:37 -0700358
359 // If we can't find both [] then treat it as a normal serial with [ in it.
Josh Gaobd767202018-12-19 13:37:41 -0800360 VerifyParseHostService(protocol + "[0123:foo", protocol + "[0123", "foo");
David Pursell73d55aa2016-09-21 12:08:37 -0700361
362 // Don't be fooled by random IPv6 addresses in the command string.
Josh Gaobd767202018-12-19 13:37:41 -0800363 VerifyParseHostService(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
364 protocol + "foo", "ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
365
366 // Handle embedded NULs properly.
367 VerifyParseHostService(protocol + "foo:echo foo\0bar"s, protocol + "foo",
368 "echo foo\0bar"sv);
David Pursell3f902aa2016-03-01 08:58:26 -0800369 }
370}
371
372// Check <prefix>:<serial>:<command> format.
Josh Gaobd767202018-12-19 13:37:41 -0800373TEST(socket_test, test_parse_host_service_prefix) {
David Pursell3f902aa2016-03-01 08:58:26 -0800374 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
Josh Gaobd767202018-12-19 13:37:41 -0800375 VerifyParseHostServiceFailed(prefix);
376 VerifyParseHostServiceFailed(prefix + "foo");
David Pursell3f902aa2016-03-01 08:58:26 -0800377
Josh Gaobd767202018-12-19 13:37:41 -0800378 VerifyParseHostService(prefix + "foo:bar", prefix + "foo", "bar");
379 VerifyParseHostService(prefix + "foo:bar:baz", prefix + "foo", "bar:baz");
380 VerifyParseHostService(prefix + "foo:123:bar", prefix + "foo", "123:bar");
David Pursell3f902aa2016-03-01 08:58:26 -0800381 }
382}
383
384#endif // ADB_HOST