blob: d2ce2d875f5aeda468147e15f938f93e78f7ad4a [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>
25#include <vector>
26
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070027#include <unistd.h>
28
29#include "adb.h"
30#include "adb_io.h"
Josh Gao022d4472016-02-10 14:49:00 -080031#include "fdevent_test.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070032#include "socket.h"
33#include "sysdeps.h"
34
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070035struct ThreadArg {
36 int first_read_fd;
37 int last_write_fd;
38 size_t middle_pipe_count;
39};
40
Josh Gao022d4472016-02-10 14:49:00 -080041class LocalSocketTest : public FdeventTest {};
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070042
Josh Gao022d4472016-02-10 14:49:00 -080043static void FdEventThreadFunc(void*) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070044 fdevent_loop();
45}
46
Yabin Cui2407d7c2016-04-25 19:48:19 -070047const size_t SLEEP_FOR_FDEVENT_IN_MS = 100;
48
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();
89 adb_thread_t thread;
90 ASSERT_TRUE(adb_thread_create(FdEventThreadFunc, nullptr, &thread));
91
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070092 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
93 std::string read_buffer = MESSAGE;
94 std::string write_buffer(MESSAGE.size(), 'a');
Josh Gao022d4472016-02-10 14:49:00 -080095 ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
96 ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070097 ASSERT_EQ(read_buffer, write_buffer);
98 }
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070099
Josh Gao022d4472016-02-10 14:49:00 -0800100 ASSERT_EQ(0, adb_close(first[0]));
101 ASSERT_EQ(0, adb_close(last[1]));
102
103 // Wait until the local sockets are closed.
Yabin Cui2407d7c2016-04-25 19:48:19 -0700104 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
105 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800106 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700107}
108
109struct CloseWithPacketArg {
110 int socket_fd;
111 size_t bytes_written;
112 int cause_close_fd;
113};
114
115static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
116 asocket* s = create_local_socket(arg->socket_fd);
117 ASSERT_TRUE(s != nullptr);
118 arg->bytes_written = 0;
119 while (true) {
120 apacket* p = get_apacket();
121 p->len = sizeof(p->data);
122 arg->bytes_written += p->len;
123 int ret = s->enqueue(s, p);
124 if (ret == 1) {
125 // The writer has one packet waiting to send.
126 break;
127 }
128 }
129
130 asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
131 ASSERT_TRUE(cause_close_s != nullptr);
132 cause_close_s->peer = s;
133 s->peer = cause_close_s;
134 cause_close_s->ready(cause_close_s);
135
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700136 fdevent_loop();
137}
138
139// This test checks if we can close local socket in the following situation:
140// The socket is closing but having some packets, so it is not closed. Then
141// some write error happens in the socket's file handler, e.g., the file
142// handler is closed.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700143TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700144 int socket_fd[2];
145 ASSERT_EQ(0, adb_socketpair(socket_fd));
146 int cause_close_fd[2];
147 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
148 CloseWithPacketArg arg;
149 arg.socket_fd = socket_fd[1];
150 arg.cause_close_fd = cause_close_fd[1];
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700151
Josh Gao022d4472016-02-10 14:49:00 -0800152 PrepareThread();
153 adb_thread_t thread;
154 ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
155 &arg, &thread));
156 // Wait until the fdevent_loop() starts.
Yabin Cui2407d7c2016-04-25 19:48:19 -0700157 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
Josh Gao022d4472016-02-10 14:49:00 -0800158 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Yabin Cui2407d7c2016-04-25 19:48:19 -0700159 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
160 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800161 ASSERT_EQ(0, adb_close(socket_fd[0]));
Yabin Cui2407d7c2016-04-25 19:48:19 -0700162 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
163 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800164 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700165}
166
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700167// This test checks if we can read packets from a closing local socket.
Yabin Cuiaa77e222015-09-29 12:25:33 -0700168TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700169 int socket_fd[2];
170 ASSERT_EQ(0, adb_socketpair(socket_fd));
171 int cause_close_fd[2];
172 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
173 CloseWithPacketArg arg;
174 arg.socket_fd = socket_fd[1];
175 arg.cause_close_fd = cause_close_fd[1];
176
Josh Gao022d4472016-02-10 14:49:00 -0800177 PrepareThread();
178 adb_thread_t thread;
179 ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
180 &arg, &thread));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700181 // Wait until the fdevent_loop() starts.
Yabin Cui2407d7c2016-04-25 19:48:19 -0700182 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700183 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Yabin Cui2407d7c2016-04-25 19:48:19 -0700184 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
185 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700186
187 // Verify if we can read successfully.
188 std::vector<char> buf(arg.bytes_written);
Josh Gao022d4472016-02-10 14:49:00 -0800189 ASSERT_NE(0u, arg.bytes_written);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700190 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
191 ASSERT_EQ(0, adb_close(socket_fd[0]));
192
Yabin Cui2407d7c2016-04-25 19:48:19 -0700193 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
194 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800195 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700196}
197
198// This test checks if we can close local socket in the following situation:
199// The socket is not closed and has some packets. When it fails to write to
200// the socket's file handler because the other end is closed, we check if the
201// socket is closed.
202TEST_F(LocalSocketTest, write_error_when_having_packets) {
203 int socket_fd[2];
204 ASSERT_EQ(0, adb_socketpair(socket_fd));
205 int cause_close_fd[2];
206 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
207 CloseWithPacketArg arg;
208 arg.socket_fd = socket_fd[1];
209 arg.cause_close_fd = cause_close_fd[1];
210
Josh Gao022d4472016-02-10 14:49:00 -0800211 PrepareThread();
212 adb_thread_t thread;
213 ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
214 &arg, &thread));
215
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700216 // Wait until the fdevent_loop() starts.
Yabin Cui2407d7c2016-04-25 19:48:19 -0700217 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
218 EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700219 ASSERT_EQ(0, adb_close(socket_fd[0]));
220
Yabin Cui2407d7c2016-04-25 19:48:19 -0700221 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
222 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800223 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700224}
225
Yabin Cuiaa77e222015-09-29 12:25:33 -0700226#if defined(__linux__)
227
228static void ClientThreadFunc() {
229 std::string error;
230 int fd = network_loopback_client(5038, SOCK_STREAM, &error);
231 ASSERT_GE(fd, 0) << error;
Josh Gao022d4472016-02-10 14:49:00 -0800232 adb_sleep_ms(200);
Yabin Cuiaa77e222015-09-29 12:25:33 -0700233 ASSERT_EQ(0, adb_close(fd));
234}
235
236struct CloseRdHupSocketArg {
Josh Gao022d4472016-02-10 14:49:00 -0800237 int socket_fd;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700238};
239
Yabin Cuiaa77e222015-09-29 12:25:33 -0700240static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
Josh Gao022d4472016-02-10 14:49:00 -0800241 asocket* s = create_local_socket(arg->socket_fd);
242 ASSERT_TRUE(s != nullptr);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700243
Josh Gao022d4472016-02-10 14:49:00 -0800244 fdevent_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700245}
246
Yabin Cuiaa77e222015-09-29 12:25:33 -0700247// This test checks if we can close sockets in CLOSE_WAIT state.
248TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao022d4472016-02-10 14:49:00 -0800249 std::string error;
250 int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
251 ASSERT_GE(listen_fd, 0);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700252
Josh Gao022d4472016-02-10 14:49:00 -0800253 adb_thread_t client_thread;
254 ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(ClientThreadFunc), nullptr,
255 &client_thread));
256
257 struct sockaddr addr;
258 socklen_t alen;
259 alen = sizeof(addr);
260 int accept_fd = adb_socket_accept(listen_fd, &addr, &alen);
261 ASSERT_GE(accept_fd, 0);
262 CloseRdHupSocketArg arg;
263 arg.socket_fd = accept_fd;
264
265 PrepareThread();
266 adb_thread_t thread;
267 ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseRdHupSocketThreadFunc),
268 &arg, &thread));
269
270 // Wait until the fdevent_loop() starts.
Yabin Cui2407d7c2016-04-25 19:48:19 -0700271 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
272 EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800273
274 // Wait until the client closes its socket.
275 ASSERT_TRUE(adb_thread_join(client_thread));
276
Yabin Cui2407d7c2016-04-25 19:48:19 -0700277 adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
278 ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
Josh Gao022d4472016-02-10 14:49:00 -0800279 TerminateThread(thread);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700280}
Yabin Cuiaa77e222015-09-29 12:25:33 -0700281
282#endif // defined(__linux__)
David Pursell3f902aa2016-03-01 08:58:26 -0800283
284#if ADB_HOST
285
286// Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches
287// |expected|, otherwise logs the failure to gtest.
Dan Austinb4cff492016-03-28 15:32:37 -0700288void VerifySkipHostSerial(std::string serial, const char* expected) {
289 char* result = internal::skip_host_serial(&serial[0]);
David Pursell3f902aa2016-03-01 08:58:26 -0800290 if (expected == nullptr) {
291 EXPECT_EQ(nullptr, result);
292 } else {
293 EXPECT_STREQ(expected, result);
294 }
295}
296
297// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
298TEST(socket_test, test_skip_host_serial) {
299 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
300 VerifySkipHostSerial(protocol, nullptr);
301 VerifySkipHostSerial(protocol + "foo", nullptr);
302
303 VerifySkipHostSerial(protocol + "foo:bar", ":bar");
304 VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz");
305
306 VerifySkipHostSerial(protocol + "foo:123:bar", ":bar");
307 VerifySkipHostSerial(protocol + "foo:123:456", ":456");
308 VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz");
309
310 // Don't register a port unless it's all numbers and ends with ':'.
311 VerifySkipHostSerial(protocol + "foo:123", ":123");
312 VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz");
313 }
314}
315
316// Check <prefix>:<serial>:<command> format.
317TEST(socket_test, test_skip_host_serial_prefix) {
318 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
319 VerifySkipHostSerial(prefix, nullptr);
320 VerifySkipHostSerial(prefix + "foo", nullptr);
321
322 VerifySkipHostSerial(prefix + "foo:bar", ":bar");
323 VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz");
324 VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar");
325 }
326}
327
328#endif // ADB_HOST