blob: f587fdb305e973666fb33d6b77557b34c852b502 [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 Gaofd7486f2018-03-28 18:53:30 -070045static void WaitForFdeventLoop() {
46 std::this_thread::sleep_for(100ms);
47}
Yabin Cui2407d7c2016-04-25 19:48:19 -070048
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070049TEST_F(LocalSocketTest, smoke) {
Josh Gao022d4472016-02-10 14:49:00 -080050 // Join two socketpairs with a chain of intermediate socketpairs.
51 int first[2];
52 std::vector<std::array<int, 2>> intermediates;
53 int last[2];
54
55 constexpr size_t INTERMEDIATE_COUNT = 50;
56 constexpr size_t MESSAGE_LOOP_COUNT = 100;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070057 const std::string MESSAGE = "socket_test";
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070058
Josh Gao022d4472016-02-10 14:49:00 -080059 intermediates.resize(INTERMEDIATE_COUNT);
60 ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno);
61 ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno);
62 asocket* prev_tail = create_local_socket(first[1]);
63 ASSERT_NE(nullptr, prev_tail);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070064
Josh Gao022d4472016-02-10 14:49:00 -080065 auto connect = [](asocket* tail, asocket* head) {
66 tail->peer = head;
67 head->peer = tail;
68 tail->ready(tail);
69 };
70
71 for (auto& intermediate : intermediates) {
72 ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno);
73
74 asocket* head = create_local_socket(intermediate[0]);
75 ASSERT_NE(nullptr, head);
76
77 asocket* tail = create_local_socket(intermediate[1]);
78 ASSERT_NE(nullptr, tail);
79
80 connect(prev_tail, head);
81 prev_tail = tail;
82 }
83
84 asocket* end = create_local_socket(last[0]);
85 ASSERT_NE(nullptr, end);
86 connect(prev_tail, end);
87
88 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -070089 std::thread thread(fdevent_loop);
Josh Gao022d4472016-02-10 14:49:00 -080090
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070091 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
92 std::string read_buffer = MESSAGE;
93 std::string write_buffer(MESSAGE.size(), 'a');
Josh Gao022d4472016-02-10 14:49:00 -080094 ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
95 ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070096 ASSERT_EQ(read_buffer, write_buffer);
97 }
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070098
Josh Gao022d4472016-02-10 14:49:00 -080099 ASSERT_EQ(0, adb_close(first[0]));
100 ASSERT_EQ(0, adb_close(last[1]));
101
102 // Wait until the local sockets are closed.
Josh Gaofd7486f2018-03-28 18:53:30 -0700103 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700104 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800105 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700106}
107
108struct CloseWithPacketArg {
109 int socket_fd;
110 size_t bytes_written;
111 int cause_close_fd;
112};
113
114static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
115 asocket* s = create_local_socket(arg->socket_fd);
116 ASSERT_TRUE(s != nullptr);
117 arg->bytes_written = 0;
Josh Gaoecb96ac2018-03-19 15:36:17 -0700118
119 std::string data;
120 data.resize(MAX_PAYLOAD);
121 arg->bytes_written += data.size();
122 int ret = s->enqueue(s, std::move(data));
123 ASSERT_EQ(1, ret);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700124
125 asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
126 ASSERT_TRUE(cause_close_s != nullptr);
127 cause_close_s->peer = s;
128 s->peer = cause_close_s;
129 cause_close_s->ready(cause_close_s);
130
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700131 fdevent_loop();
132}
133
134// This test checks if we can close local socket in the following situation:
135// The socket is closing but having some packets, so it is not closed. Then
136// some write error happens in the socket's file handler, e.g., the file
137// handler is closed.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700138TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700139 int socket_fd[2];
140 ASSERT_EQ(0, adb_socketpair(socket_fd));
141 int cause_close_fd[2];
142 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
143 CloseWithPacketArg arg;
144 arg.socket_fd = socket_fd[1];
145 arg.cause_close_fd = cause_close_fd[1];
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700146
Josh Gao022d4472016-02-10 14:49:00 -0800147 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700148 std::thread thread(CloseWithPacketThreadFunc, &arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700149
150 WaitForFdeventLoop();
Josh Gao022d4472016-02-10 14:49:00 -0800151 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700152
153 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700154 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800155 ASSERT_EQ(0, adb_close(socket_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700156
157 WaitForFdeventLoop();
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);
Josh Gaofd7486f2018-03-28 18:53:30 -0700174
175 WaitForFdeventLoop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700176 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700177
178 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700179 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700180
181 // Verify if we can read successfully.
182 std::vector<char> buf(arg.bytes_written);
Josh Gao022d4472016-02-10 14:49:00 -0800183 ASSERT_NE(0u, arg.bytes_written);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700184 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
185 ASSERT_EQ(0, adb_close(socket_fd[0]));
186
Josh Gaofd7486f2018-03-28 18:53:30 -0700187 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700188 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800189 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700190}
191
192// This test checks if we can close local socket in the following situation:
193// The socket is not closed and has some packets. When it fails to write to
194// the socket's file handler because the other end is closed, we check if the
195// socket is closed.
196TEST_F(LocalSocketTest, write_error_when_having_packets) {
197 int socket_fd[2];
198 ASSERT_EQ(0, adb_socketpair(socket_fd));
199 int cause_close_fd[2];
200 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
201 CloseWithPacketArg arg;
202 arg.socket_fd = socket_fd[1];
203 arg.cause_close_fd = cause_close_fd[1];
204
Josh Gao022d4472016-02-10 14:49:00 -0800205 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700206 std::thread thread(CloseWithPacketThreadFunc, &arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700207
208 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700209 EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700210 ASSERT_EQ(0, adb_close(socket_fd[0]));
211
Josh Gaofd7486f2018-03-28 18:53:30 -0700212 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700213 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800214 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700215}
216
Josh Gaodf3bae92018-03-15 15:28:55 -0700217// 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
Josh Gaoecb96ac2018-03-19 15:36:17 -0700236 EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
Josh Gaodf3bae92018-03-15 15:28:55 -0700237
Josh Gaoecb96ac2018-03-19 15:36:17 -0700238 EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
239 const char* str = "write succeeds, but local_socket will fail to write";
240 EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
241 EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
242
243 char buf[6];
244 EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
245 EXPECT_EQ(0, memcmp(buf, "foobar", 6));
Josh Gaodf3bae92018-03-15 15:28:55 -0700246
247 adb_close(head_fd[0]);
248 adb_close(tail_fd[0]);
249
Josh Gaofd7486f2018-03-28 18:53:30 -0700250 WaitForFdeventLoop();
Josh Gaodf3bae92018-03-15 15:28:55 -0700251 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
252 TerminateThread(thread);
253}
Josh Gaodf3bae92018-03-15 15:28:55 -0700254
Yabin Cuiaa77e222015-09-29 12:25:33 -0700255#if defined(__linux__)
256
257static void ClientThreadFunc() {
258 std::string error;
259 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
260 ASSERT_GE(fd, 0) << error;
Josh Gao4602adb2016-11-15 18:55:47 -0800261 std::this_thread::sleep_for(200ms);
Yabin Cuiaa77e222015-09-29 12:25:33 -0700262 ASSERT_EQ(0, adb_close(fd));
263}
264
265struct CloseRdHupSocketArg {
Josh Gao022d4472016-02-10 14:49:00 -0800266 int socket_fd;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700267};
268
Yabin Cuiaa77e222015-09-29 12:25:33 -0700269static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
Josh Gao022d4472016-02-10 14:49:00 -0800270 asocket* s = create_local_socket(arg->socket_fd);
271 ASSERT_TRUE(s != nullptr);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700272
Josh Gao022d4472016-02-10 14:49:00 -0800273 fdevent_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700274}
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);
286 CloseRdHupSocketArg arg;
287 arg.socket_fd = accept_fd;
288
289 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700290 std::thread thread(CloseRdHupSocketThreadFunc, &arg);
Josh Gao022d4472016-02-10 14:49:00 -0800291
Josh Gaofd7486f2018-03-28 18:53:30 -0700292 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700293 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800294
295 // Wait until the client closes its socket.
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700296 client_thread.join();
Josh Gao022d4472016-02-10 14:49:00 -0800297
Josh Gaofd7486f2018-03-28 18:53:30 -0700298 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700299 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800300 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700301}
Yabin Cuiaa77e222015-09-29 12:25:33 -0700302
303#endif // defined(__linux__)
David Pursell3f902aa2016-03-01 08:58:26 -0800304
305#if ADB_HOST
306
307// Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches
308// |expected|, otherwise logs the failure to gtest.
Dan Austinb4cff492016-03-28 15:32:37 -0700309void VerifySkipHostSerial(std::string serial, const char* expected) {
310 char* result = internal::skip_host_serial(&serial[0]);
David Pursell3f902aa2016-03-01 08:58:26 -0800311 if (expected == nullptr) {
312 EXPECT_EQ(nullptr, result);
313 } else {
314 EXPECT_STREQ(expected, result);
315 }
316}
317
318// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
319TEST(socket_test, test_skip_host_serial) {
320 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
321 VerifySkipHostSerial(protocol, nullptr);
322 VerifySkipHostSerial(protocol + "foo", nullptr);
323
324 VerifySkipHostSerial(protocol + "foo:bar", ":bar");
325 VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz");
326
327 VerifySkipHostSerial(protocol + "foo:123:bar", ":bar");
328 VerifySkipHostSerial(protocol + "foo:123:456", ":456");
329 VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz");
330
331 // Don't register a port unless it's all numbers and ends with ':'.
332 VerifySkipHostSerial(protocol + "foo:123", ":123");
333 VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz");
David Pursell73d55aa2016-09-21 12:08:37 -0700334
335 VerifySkipHostSerial(protocol + "100.100.100.100:5555:foo", ":foo");
336 VerifySkipHostSerial(protocol + "[0123:4567:89ab:CDEF:0:9:a:f]:5555:foo", ":foo");
337 VerifySkipHostSerial(protocol + "[::1]:5555:foo", ":foo");
338
339 // If we can't find both [] then treat it as a normal serial with [ in it.
340 VerifySkipHostSerial(protocol + "[0123:foo", ":foo");
341
342 // Don't be fooled by random IPv6 addresses in the command string.
343 VerifySkipHostSerial(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
344 ":ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
David Pursell3f902aa2016-03-01 08:58:26 -0800345 }
346}
347
348// Check <prefix>:<serial>:<command> format.
349TEST(socket_test, test_skip_host_serial_prefix) {
350 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
351 VerifySkipHostSerial(prefix, nullptr);
352 VerifySkipHostSerial(prefix + "foo", nullptr);
353
354 VerifySkipHostSerial(prefix + "foo:bar", ":bar");
355 VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz");
356 VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar");
357 }
358}
359
360#endif // ADB_HOST