blob: 068c1754316e22e424ad108add6e7876a3c67a31 [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
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070045TEST_F(LocalSocketTest, smoke) {
Josh Gao022d4472016-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 Cuic1b1f6f2015-09-15 16:27:09 -070053 const std::string MESSAGE = "socket_test";
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070054
Josh Gao022d4472016-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 Cuic1b1f6f2015-09-15 16:27:09 -070060
Josh Gao022d4472016-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 Gaoe1dacfc2017-04-12 17:00:49 -070085 std::thread thread(fdevent_loop);
Josh Gao022d4472016-02-10 14:49:00 -080086
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070087 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
88 std::string read_buffer = MESSAGE;
89 std::string write_buffer(MESSAGE.size(), 'a');
Josh Gao022d4472016-02-10 14:49:00 -080090 ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
91 ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070092 ASSERT_EQ(read_buffer, write_buffer);
93 }
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070094
Josh Gao022d4472016-02-10 14:49:00 -080095 ASSERT_EQ(0, adb_close(first[0]));
96 ASSERT_EQ(0, adb_close(last[1]));
97
98 // Wait until the local sockets are closed.
Josh Gaofd7486f2018-03-28 18:53:30 -070099 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700100 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800101 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700102}
103
104struct CloseWithPacketArg {
105 int socket_fd;
106 size_t bytes_written;
107 int cause_close_fd;
108};
109
110static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
111 asocket* s = create_local_socket(arg->socket_fd);
112 ASSERT_TRUE(s != nullptr);
113 arg->bytes_written = 0;
Josh Gaoecb96ac2018-03-19 15:36:17 -0700114
115 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 Cuic1b1f6f2015-09-15 16:27:09 -0700120
121 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
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700127 fdevent_loop();
128}
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 Cuiaa77e222015-09-29 12:25:33 -0700134TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cuic1b1f6f2015-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 Cuic1b1f6f2015-09-15 16:27:09 -0700142
Josh Gao022d4472016-02-10 14:49:00 -0800143 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700144 std::thread thread(CloseWithPacketThreadFunc, &arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700145
146 WaitForFdeventLoop();
Josh Gao022d4472016-02-10 14:49:00 -0800147 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700148
149 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700150 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800151 ASSERT_EQ(0, adb_close(socket_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700152
153 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700154 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800155 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700156}
157
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700158// This test checks if we can read packets from a closing local socket.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700159TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700160 int socket_fd[2];
161 ASSERT_EQ(0, adb_socketpair(socket_fd));
162 int cause_close_fd[2];
163 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
164 CloseWithPacketArg arg;
165 arg.socket_fd = socket_fd[1];
166 arg.cause_close_fd = cause_close_fd[1];
167
Josh Gao022d4472016-02-10 14:49:00 -0800168 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700169 std::thread thread(CloseWithPacketThreadFunc, &arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700170
171 WaitForFdeventLoop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700172 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaofd7486f2018-03-28 18:53:30 -0700173
174 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700175 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700176
177 // Verify if we can read successfully.
178 std::vector<char> buf(arg.bytes_written);
Josh Gao022d4472016-02-10 14:49:00 -0800179 ASSERT_NE(0u, arg.bytes_written);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700180 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
181 ASSERT_EQ(0, adb_close(socket_fd[0]));
182
Josh Gaofd7486f2018-03-28 18:53:30 -0700183 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700184 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800185 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700186}
187
188// This test checks if we can close local socket in the following situation:
189// The socket is not closed and has some packets. When it fails to write to
190// the socket's file handler because the other end is closed, we check if the
191// socket is closed.
192TEST_F(LocalSocketTest, write_error_when_having_packets) {
193 int socket_fd[2];
194 ASSERT_EQ(0, adb_socketpair(socket_fd));
195 int cause_close_fd[2];
196 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
197 CloseWithPacketArg arg;
198 arg.socket_fd = socket_fd[1];
199 arg.cause_close_fd = cause_close_fd[1];
200
Josh Gao022d4472016-02-10 14:49:00 -0800201 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700202 std::thread thread(CloseWithPacketThreadFunc, &arg);
Josh Gaofd7486f2018-03-28 18:53:30 -0700203
204 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700205 EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700206 ASSERT_EQ(0, adb_close(socket_fd[0]));
207
Josh Gaofd7486f2018-03-28 18:53:30 -0700208 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700209 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800210 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700211}
212
Josh Gaodf3bae92018-03-15 15:28:55 -0700213// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
214TEST_F(LocalSocketTest, flush_after_shutdown) {
215 int head_fd[2];
216 int tail_fd[2];
217 ASSERT_EQ(0, adb_socketpair(head_fd));
218 ASSERT_EQ(0, adb_socketpair(tail_fd));
219
220 asocket* head = create_local_socket(head_fd[1]);
221 asocket* tail = create_local_socket(tail_fd[1]);
222
223 head->peer = tail;
224 head->ready(head);
225
226 tail->peer = head;
227 tail->ready(tail);
228
229 PrepareThread();
230 std::thread thread(fdevent_loop);
231
Josh Gaoecb96ac2018-03-19 15:36:17 -0700232 EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
Josh Gaodf3bae92018-03-15 15:28:55 -0700233
Josh Gaoecb96ac2018-03-19 15:36:17 -0700234 EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
235 const char* str = "write succeeds, but local_socket will fail to write";
236 EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
237 EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
238
239 char buf[6];
240 EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
241 EXPECT_EQ(0, memcmp(buf, "foobar", 6));
Josh Gaodf3bae92018-03-15 15:28:55 -0700242
243 adb_close(head_fd[0]);
244 adb_close(tail_fd[0]);
245
Josh Gaofd7486f2018-03-28 18:53:30 -0700246 WaitForFdeventLoop();
Josh Gaodf3bae92018-03-15 15:28:55 -0700247 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
248 TerminateThread(thread);
249}
Josh Gaodf3bae92018-03-15 15:28:55 -0700250
Yabin Cuiaa77e222015-09-29 12:25:33 -0700251#if defined(__linux__)
252
253static void ClientThreadFunc() {
254 std::string error;
255 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
256 ASSERT_GE(fd, 0) << error;
Josh Gao4602adb2016-11-15 18:55:47 -0800257 std::this_thread::sleep_for(200ms);
Yabin Cuiaa77e222015-09-29 12:25:33 -0700258 ASSERT_EQ(0, adb_close(fd));
259}
260
261struct CloseRdHupSocketArg {
Josh Gao022d4472016-02-10 14:49:00 -0800262 int socket_fd;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700263};
264
Yabin Cuiaa77e222015-09-29 12:25:33 -0700265static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
Josh Gao022d4472016-02-10 14:49:00 -0800266 asocket* s = create_local_socket(arg->socket_fd);
267 ASSERT_TRUE(s != nullptr);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700268
Josh Gao022d4472016-02-10 14:49:00 -0800269 fdevent_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700270}
271
Yabin Cuiaa77e222015-09-29 12:25:33 -0700272// This test checks if we can close sockets in CLOSE_WAIT state.
273TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao022d4472016-02-10 14:49:00 -0800274 std::string error;
275 int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
276 ASSERT_GE(listen_fd, 0);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700277
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700278 std::thread client_thread(ClientThreadFunc);
Josh Gao022d4472016-02-10 14:49:00 -0800279
Elliott Hughes3dcfa3f2016-08-23 12:50:00 -0700280 int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
Josh Gao022d4472016-02-10 14:49:00 -0800281 ASSERT_GE(accept_fd, 0);
282 CloseRdHupSocketArg arg;
283 arg.socket_fd = accept_fd;
284
285 PrepareThread();
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700286 std::thread thread(CloseRdHupSocketThreadFunc, &arg);
Josh Gao022d4472016-02-10 14:49:00 -0800287
Josh Gaofd7486f2018-03-28 18:53:30 -0700288 WaitForFdeventLoop();
Yabin Cui2407d7c2016-04-25 19:48:19 -0700289 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800290
291 // Wait until the client closes its socket.
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700292 client_thread.join();
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 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800296 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700297}
Yabin Cuiaa77e222015-09-29 12:25:33 -0700298
299#endif // defined(__linux__)
David Pursell3f902aa2016-03-01 08:58:26 -0800300
301#if ADB_HOST
302
303// Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches
304// |expected|, otherwise logs the failure to gtest.
Dan Austinb4cff492016-03-28 15:32:37 -0700305void VerifySkipHostSerial(std::string serial, const char* expected) {
306 char* result = internal::skip_host_serial(&serial[0]);
David Pursell3f902aa2016-03-01 08:58:26 -0800307 if (expected == nullptr) {
308 EXPECT_EQ(nullptr, result);
309 } else {
310 EXPECT_STREQ(expected, result);
311 }
312}
313
314// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
315TEST(socket_test, test_skip_host_serial) {
316 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
317 VerifySkipHostSerial(protocol, nullptr);
318 VerifySkipHostSerial(protocol + "foo", nullptr);
319
320 VerifySkipHostSerial(protocol + "foo:bar", ":bar");
321 VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz");
322
323 VerifySkipHostSerial(protocol + "foo:123:bar", ":bar");
324 VerifySkipHostSerial(protocol + "foo:123:456", ":456");
325 VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz");
326
327 // Don't register a port unless it's all numbers and ends with ':'.
328 VerifySkipHostSerial(protocol + "foo:123", ":123");
329 VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz");
David Pursell73d55aa2016-09-21 12:08:37 -0700330
331 VerifySkipHostSerial(protocol + "100.100.100.100:5555:foo", ":foo");
332 VerifySkipHostSerial(protocol + "[0123:4567:89ab:CDEF:0:9:a:f]:5555:foo", ":foo");
333 VerifySkipHostSerial(protocol + "[::1]:5555:foo", ":foo");
334
335 // If we can't find both [] then treat it as a normal serial with [ in it.
336 VerifySkipHostSerial(protocol + "[0123:foo", ":foo");
337
338 // Don't be fooled by random IPv6 addresses in the command string.
339 VerifySkipHostSerial(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
340 ":ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
David Pursell3f902aa2016-03-01 08:58:26 -0800341 }
342}
343
344// Check <prefix>:<serial>:<command> format.
345TEST(socket_test, test_skip_host_serial_prefix) {
346 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
347 VerifySkipHostSerial(prefix, nullptr);
348 VerifySkipHostSerial(prefix + "foo", nullptr);
349
350 VerifySkipHostSerial(prefix + "foo:bar", ":bar");
351 VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz");
352 VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar");
353 }
354}
355
356#endif // ADB_HOST