blob: f09e4b3dfc3c26f400aee40849b483260e957134 [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
Josh Gao511763b2016-02-10 14:49:00 -080021#include <array>
Yabin Cui2ce9d562015-09-15 16:27:09 -070022#include <limits>
23#include <queue>
24#include <string>
Elliott Hughes73925982016-11-15 12:37:32 -080025#include <thread>
Yabin Cui2ce9d562015-09-15 16:27:09 -070026#include <vector>
27
Yabin Cui2ce9d562015-09-15 16:27:09 -070028#include <unistd.h>
29
30#include "adb.h"
31#include "adb_io.h"
Josh Gao511763b2016-02-10 14:49:00 -080032#include "fdevent_test.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070033#include "socket.h"
34#include "sysdeps.h"
Josh Gao70267e42016-11-15 18:55:47 -080035#include "sysdeps/chrono.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070036
Yabin Cui2ce9d562015-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 Gao511763b2016-02-10 14:49:00 -080043class LocalSocketTest : public FdeventTest {};
Yabin Cui2ce9d562015-09-15 16:27:09 -070044
Yabin Cui2ce9d562015-09-15 16:27:09 -070045TEST_F(LocalSocketTest, smoke) {
Josh Gao511763b2016-02-10 14:49:00 -080046 // Join two socketpairs with a chain of intermediate socketpairs.
47 int first[2];
48 std::vector<std::array<int, 2>> intermediates;
49 int last[2];
50
51 constexpr size_t INTERMEDIATE_COUNT = 50;
52 constexpr size_t MESSAGE_LOOP_COUNT = 100;
Yabin Cui2ce9d562015-09-15 16:27:09 -070053 const std::string MESSAGE = "socket_test";
Yabin Cui2ce9d562015-09-15 16:27:09 -070054
Josh Gao511763b2016-02-10 14:49:00 -080055 intermediates.resize(INTERMEDIATE_COUNT);
56 ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno);
57 ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno);
58 asocket* prev_tail = create_local_socket(first[1]);
59 ASSERT_NE(nullptr, prev_tail);
Yabin Cui2ce9d562015-09-15 16:27:09 -070060
Josh Gao511763b2016-02-10 14:49:00 -080061 auto connect = [](asocket* tail, asocket* head) {
62 tail->peer = head;
63 head->peer = tail;
64 tail->ready(tail);
65 };
66
67 for (auto& intermediate : intermediates) {
68 ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno);
69
70 asocket* head = create_local_socket(intermediate[0]);
71 ASSERT_NE(nullptr, head);
72
73 asocket* tail = create_local_socket(intermediate[1]);
74 ASSERT_NE(nullptr, tail);
75
76 connect(prev_tail, head);
77 prev_tail = tail;
78 }
79
80 asocket* end = create_local_socket(last[0]);
81 ASSERT_NE(nullptr, end);
82 connect(prev_tail, end);
83
84 PrepareThread();
Josh Gao511763b2016-02-10 14:49:00 -080085
Yabin Cui2ce9d562015-09-15 16:27:09 -070086 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
87 std::string read_buffer = MESSAGE;
88 std::string write_buffer(MESSAGE.size(), 'a');
Josh Gao511763b2016-02-10 14:49:00 -080089 ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
90 ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
Yabin Cui2ce9d562015-09-15 16:27:09 -070091 ASSERT_EQ(read_buffer, write_buffer);
92 }
Yabin Cui2ce9d562015-09-15 16:27:09 -070093
Josh Gao511763b2016-02-10 14:49:00 -080094 ASSERT_EQ(0, adb_close(first[0]));
95 ASSERT_EQ(0, adb_close(last[1]));
96
97 // Wait until the local sockets are closed.
Josh Gaocd608202018-03-28 18:53:30 -070098 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -070099 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700100 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700101}
102
103struct CloseWithPacketArg {
104 int socket_fd;
105 size_t bytes_written;
106 int cause_close_fd;
107};
108
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700109static void CreateCloser(CloseWithPacketArg* arg) {
110 fdevent_run_on_main_thread([arg]() {
111 asocket* s = create_local_socket(arg->socket_fd);
112 ASSERT_TRUE(s != nullptr);
113 arg->bytes_written = 0;
Josh Gaod5e56ce2018-03-19 15:36:17 -0700114
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700115 std::string data;
116 data.resize(MAX_PAYLOAD);
117 arg->bytes_written += data.size();
118 int ret = s->enqueue(s, std::move(data));
119 ASSERT_EQ(1, ret);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700120
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700121 asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
122 ASSERT_TRUE(cause_close_s != nullptr);
123 cause_close_s->peer = s;
124 s->peer = cause_close_s;
125 cause_close_s->ready(cause_close_s);
126 });
127 WaitForFdeventLoop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700128}
129
130// This test checks if we can close local socket in the following situation:
131// The socket is closing but having some packets, so it is not closed. Then
132// some write error happens in the socket's file handler, e.g., the file
133// handler is closed.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700134TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700135 int socket_fd[2];
136 ASSERT_EQ(0, adb_socketpair(socket_fd));
137 int cause_close_fd[2];
138 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
139 CloseWithPacketArg arg;
140 arg.socket_fd = socket_fd[1];
141 arg.cause_close_fd = cause_close_fd[1];
Yabin Cui2ce9d562015-09-15 16:27:09 -0700142
Josh Gao511763b2016-02-10 14:49:00 -0800143 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700144 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700145
Josh Gao511763b2016-02-10 14:49:00 -0800146 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700147
148 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700149 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao511763b2016-02-10 14:49:00 -0800150 ASSERT_EQ(0, adb_close(socket_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700151
152 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700153 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700154 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700155}
156
Yabin Cui2ce9d562015-09-15 16:27:09 -0700157// This test checks if we can read packets from a closing local socket.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700158TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700159 int socket_fd[2];
160 ASSERT_EQ(0, adb_socketpair(socket_fd));
161 int cause_close_fd[2];
162 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
163 CloseWithPacketArg arg;
164 arg.socket_fd = socket_fd[1];
165 arg.cause_close_fd = cause_close_fd[1];
166
Josh Gao511763b2016-02-10 14:49:00 -0800167 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700168 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700169
170 WaitForFdeventLoop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700171 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700172
173 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700174 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700175
176 // Verify if we can read successfully.
177 std::vector<char> buf(arg.bytes_written);
Josh Gao511763b2016-02-10 14:49:00 -0800178 ASSERT_NE(0u, arg.bytes_written);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700179 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
180 ASSERT_EQ(0, adb_close(socket_fd[0]));
181
Josh Gaocd608202018-03-28 18:53:30 -0700182 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700183 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700184 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700185}
186
187// This test checks if we can close local socket in the following situation:
188// The socket is not closed and has some packets. When it fails to write to
189// the socket's file handler because the other end is closed, we check if the
190// socket is closed.
191TEST_F(LocalSocketTest, write_error_when_having_packets) {
192 int socket_fd[2];
193 ASSERT_EQ(0, adb_socketpair(socket_fd));
194 int cause_close_fd[2];
195 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
196 CloseWithPacketArg arg;
197 arg.socket_fd = socket_fd[1];
198 arg.cause_close_fd = cause_close_fd[1];
199
Josh Gao511763b2016-02-10 14:49:00 -0800200 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700201 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700202
203 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700204 EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700205 ASSERT_EQ(0, adb_close(socket_fd[0]));
206
Josh Gaocd608202018-03-28 18:53:30 -0700207 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700208 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700209 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700210}
211
Josh Gao80206022018-03-15 15:28:55 -0700212// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
213TEST_F(LocalSocketTest, flush_after_shutdown) {
214 int head_fd[2];
215 int tail_fd[2];
216 ASSERT_EQ(0, adb_socketpair(head_fd));
217 ASSERT_EQ(0, adb_socketpair(tail_fd));
218
219 asocket* head = create_local_socket(head_fd[1]);
220 asocket* tail = create_local_socket(tail_fd[1]);
221
222 head->peer = tail;
223 head->ready(head);
224
225 tail->peer = head;
226 tail->ready(tail);
227
228 PrepareThread();
Josh Gao80206022018-03-15 15:28:55 -0700229
Josh Gaod5e56ce2018-03-19 15:36:17 -0700230 EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
Josh Gao80206022018-03-15 15:28:55 -0700231
Josh Gaod5e56ce2018-03-19 15:36:17 -0700232 EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
233 const char* str = "write succeeds, but local_socket will fail to write";
234 EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
235 EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
236
237 char buf[6];
238 EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
239 EXPECT_EQ(0, memcmp(buf, "foobar", 6));
Josh Gao80206022018-03-15 15:28:55 -0700240
241 adb_close(head_fd[0]);
242 adb_close(tail_fd[0]);
243
Josh Gaocd608202018-03-28 18:53:30 -0700244 WaitForFdeventLoop();
Josh Gao80206022018-03-15 15:28:55 -0700245 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700246 TerminateThread();
Josh Gao80206022018-03-15 15:28:55 -0700247}
Josh Gao80206022018-03-15 15:28:55 -0700248
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700249#if defined(__linux__)
250
251static void ClientThreadFunc() {
252 std::string error;
253 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
254 ASSERT_GE(fd, 0) << error;
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700255 std::this_thread::sleep_for(1s);
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700256 ASSERT_EQ(0, adb_close(fd));
257}
258
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700259// This test checks if we can close sockets in CLOSE_WAIT state.
260TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao511763b2016-02-10 14:49:00 -0800261 std::string error;
262 int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
263 ASSERT_GE(listen_fd, 0);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700264
Josh Gao0f3312a2017-04-12 17:00:49 -0700265 std::thread client_thread(ClientThreadFunc);
Josh Gao511763b2016-02-10 14:49:00 -0800266
Elliott Hughes97a035c2016-08-23 12:50:00 -0700267 int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
Josh Gao511763b2016-02-10 14:49:00 -0800268 ASSERT_GE(accept_fd, 0);
Josh Gao511763b2016-02-10 14:49:00 -0800269
270 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700271
272 fdevent_run_on_main_thread([accept_fd]() {
273 asocket* s = create_local_socket(accept_fd);
274 ASSERT_TRUE(s != nullptr);
275 });
Josh Gao511763b2016-02-10 14:49:00 -0800276
Josh Gaocd608202018-03-28 18:53:30 -0700277 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700278 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao511763b2016-02-10 14:49:00 -0800279
280 // Wait until the client closes its socket.
Josh Gao0f3312a2017-04-12 17:00:49 -0700281 client_thread.join();
Josh Gao511763b2016-02-10 14:49:00 -0800282
Josh Gaocd608202018-03-28 18:53:30 -0700283 WaitForFdeventLoop();
Yabin Cui40a47272016-04-25 19:48:19 -0700284 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700285 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700286}
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700287
288#endif // defined(__linux__)
David Pursellc929c6f2016-03-01 08:58:26 -0800289
290#if ADB_HOST
291
292// Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches
293// |expected|, otherwise logs the failure to gtest.
Dan Austinca35e9e2016-03-28 15:32:37 -0700294void VerifySkipHostSerial(std::string serial, const char* expected) {
295 char* result = internal::skip_host_serial(&serial[0]);
David Pursellc929c6f2016-03-01 08:58:26 -0800296 if (expected == nullptr) {
297 EXPECT_EQ(nullptr, result);
298 } else {
299 EXPECT_STREQ(expected, result);
300 }
301}
302
303// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
304TEST(socket_test, test_skip_host_serial) {
305 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
306 VerifySkipHostSerial(protocol, nullptr);
307 VerifySkipHostSerial(protocol + "foo", nullptr);
308
309 VerifySkipHostSerial(protocol + "foo:bar", ":bar");
310 VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz");
311
312 VerifySkipHostSerial(protocol + "foo:123:bar", ":bar");
313 VerifySkipHostSerial(protocol + "foo:123:456", ":456");
314 VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz");
315
316 // Don't register a port unless it's all numbers and ends with ':'.
317 VerifySkipHostSerial(protocol + "foo:123", ":123");
318 VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz");
David Pursell24b62a72016-09-21 12:08:37 -0700319
320 VerifySkipHostSerial(protocol + "100.100.100.100:5555:foo", ":foo");
321 VerifySkipHostSerial(protocol + "[0123:4567:89ab:CDEF:0:9:a:f]:5555:foo", ":foo");
322 VerifySkipHostSerial(protocol + "[::1]:5555:foo", ":foo");
323
324 // If we can't find both [] then treat it as a normal serial with [ in it.
325 VerifySkipHostSerial(protocol + "[0123:foo", ":foo");
326
327 // Don't be fooled by random IPv6 addresses in the command string.
328 VerifySkipHostSerial(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
329 ":ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
David Pursellc929c6f2016-03-01 08:58:26 -0800330 }
331}
332
333// Check <prefix>:<serial>:<command> format.
334TEST(socket_test, test_skip_host_serial_prefix) {
335 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
336 VerifySkipHostSerial(prefix, nullptr);
337 VerifySkipHostSerial(prefix + "foo", nullptr);
338
339 VerifySkipHostSerial(prefix + "foo:bar", ":bar");
340 VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz");
341 VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar");
342 }
343}
344
345#endif // ADB_HOST