blob: 5a95fa35a0e2214b904ce3b374552fa94ced7f81 [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
Josh Gaob51193a2019-06-28 13:50:37 -070017#include "fdevent/fdevent.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070018
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 Gaob51193a2019-06-28 13:50:37 -070032#include "fdevent/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
Josh Gao8d49e122018-12-19 13:37:41 -080037using namespace std::string_literals;
38using namespace std::string_view_literals;
39
Yabin Cui2ce9d562015-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 Gao511763b2016-02-10 14:49:00 -080046class LocalSocketTest : public FdeventTest {};
Yabin Cui2ce9d562015-09-15 16:27:09 -070047
Yabin Cui2ce9d562015-09-15 16:27:09 -070048TEST_F(LocalSocketTest, smoke) {
Josh Gao511763b2016-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 Cui2ce9d562015-09-15 16:27:09 -070056 const std::string MESSAGE = "socket_test";
Yabin Cui2ce9d562015-09-15 16:27:09 -070057
Josh Gao511763b2016-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);
Josh Gaoc2705962019-01-23 15:36:42 -080061 asocket* prev_tail = create_local_socket(unique_fd(first[1]));
Josh Gao511763b2016-02-10 14:49:00 -080062 ASSERT_NE(nullptr, prev_tail);
Yabin Cui2ce9d562015-09-15 16:27:09 -070063
Josh Gao511763b2016-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
Josh Gaoc2705962019-01-23 15:36:42 -080073 asocket* head = create_local_socket(unique_fd(intermediate[0]));
Josh Gao511763b2016-02-10 14:49:00 -080074 ASSERT_NE(nullptr, head);
75
Josh Gaoc2705962019-01-23 15:36:42 -080076 asocket* tail = create_local_socket(unique_fd(intermediate[1]));
Josh Gao511763b2016-02-10 14:49:00 -080077 ASSERT_NE(nullptr, tail);
78
79 connect(prev_tail, head);
80 prev_tail = tail;
81 }
82
Josh Gaoc2705962019-01-23 15:36:42 -080083 asocket* end = create_local_socket(unique_fd(last[0]));
Josh Gao511763b2016-02-10 14:49:00 -080084 ASSERT_NE(nullptr, end);
85 connect(prev_tail, end);
86
87 PrepareThread();
Josh Gao511763b2016-02-10 14:49:00 -080088
Yabin Cui2ce9d562015-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 Gao511763b2016-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 Cui2ce9d562015-09-15 16:27:09 -070094 ASSERT_EQ(read_buffer, write_buffer);
95 }
Yabin Cui2ce9d562015-09-15 16:27:09 -070096
Josh Gao511763b2016-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 Gaocd608202018-03-28 18:53:30 -0700101 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700102 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700103 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700104}
105
106struct CloseWithPacketArg {
Josh Gaoc2705962019-01-23 15:36:42 -0800107 unique_fd socket_fd;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700108 size_t bytes_written;
Josh Gaoc2705962019-01-23 15:36:42 -0800109 unique_fd cause_close_fd;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700110};
111
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700112static void CreateCloser(CloseWithPacketArg* arg) {
113 fdevent_run_on_main_thread([arg]() {
Josh Gaoc2705962019-01-23 15:36:42 -0800114 asocket* s = create_local_socket(std::move(arg->socket_fd));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700115 ASSERT_TRUE(s != nullptr);
116 arg->bytes_written = 0;
Josh Gaod5e56ce2018-03-19 15:36:17 -0700117
Josh Gao205a8f42018-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 Gaocd2a5292018-03-07 16:52:28 -0800124 apacket::payload_type data;
Josh Gao205a8f42018-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));
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000128
129 // Return value of 0 implies that more data can be accepted.
Josh Gao205a8f42018-03-30 14:05:40 -0700130 if (ret == 1) {
131 socket_filled = true;
132 break;
133 }
134 ASSERT_NE(-1, ret);
135
136 std::this_thread::sleep_for(250ms);
137 }
138 ASSERT_TRUE(socket_filled);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700139
Josh Gaoc2705962019-01-23 15:36:42 -0800140 asocket* cause_close_s = create_local_socket(std::move(arg->cause_close_fd));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700141 ASSERT_TRUE(cause_close_s != nullptr);
142 cause_close_s->peer = s;
143 s->peer = cause_close_s;
144 cause_close_s->ready(cause_close_s);
145 });
146 WaitForFdeventLoop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700147}
148
149// This test checks if we can close local socket in the following situation:
150// The socket is closing but having some packets, so it is not closed. Then
151// some write error happens in the socket's file handler, e.g., the file
152// handler is closed.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700153TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700154 int socket_fd[2];
155 ASSERT_EQ(0, adb_socketpair(socket_fd));
156 int cause_close_fd[2];
157 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
158 CloseWithPacketArg arg;
Josh Gaoc2705962019-01-23 15:36:42 -0800159 arg.socket_fd.reset(socket_fd[1]);
160 arg.cause_close_fd.reset(cause_close_fd[1]);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700161
Josh Gao511763b2016-02-10 14:49:00 -0800162 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700163 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700164
Josh Gao511763b2016-02-10 14:49:00 -0800165 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700166
167 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700168 EXPECT_EQ(1u, fdevent_installed_count());
Josh Gao511763b2016-02-10 14:49:00 -0800169 ASSERT_EQ(0, adb_close(socket_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700170
171 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700172 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700173 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700174}
175
Yabin Cui2ce9d562015-09-15 16:27:09 -0700176// This test checks if we can read packets from a closing local socket.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700177TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700178 int socket_fd[2];
179 ASSERT_EQ(0, adb_socketpair(socket_fd));
180 int cause_close_fd[2];
181 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
182 CloseWithPacketArg arg;
Josh Gaoc2705962019-01-23 15:36:42 -0800183 arg.socket_fd.reset(socket_fd[1]);
184 arg.cause_close_fd.reset(cause_close_fd[1]);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700185
Josh Gao511763b2016-02-10 14:49:00 -0800186 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700187 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700188
189 WaitForFdeventLoop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700190 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700191
192 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700193 EXPECT_EQ(1u, fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700194
195 // Verify if we can read successfully.
196 std::vector<char> buf(arg.bytes_written);
Josh Gao511763b2016-02-10 14:49:00 -0800197 ASSERT_NE(0u, arg.bytes_written);
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000198
199 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); // TODO: b/237341044
200
Yabin Cui2ce9d562015-09-15 16:27:09 -0700201 ASSERT_EQ(0, adb_close(socket_fd[0]));
202
Josh Gaocd608202018-03-28 18:53:30 -0700203 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700204 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700205 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700206}
207
208// This test checks if we can close local socket in the following situation:
209// The socket is not closed and has some packets. When it fails to write to
210// the socket's file handler because the other end is closed, we check if the
211// socket is closed.
212TEST_F(LocalSocketTest, write_error_when_having_packets) {
213 int socket_fd[2];
214 ASSERT_EQ(0, adb_socketpair(socket_fd));
215 int cause_close_fd[2];
216 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
217 CloseWithPacketArg arg;
Josh Gaoc2705962019-01-23 15:36:42 -0800218 arg.socket_fd.reset(socket_fd[1]);
219 arg.cause_close_fd.reset(cause_close_fd[1]);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700220
Josh Gao511763b2016-02-10 14:49:00 -0800221 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700222 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700223
224 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700225 EXPECT_EQ(2u, fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700226 ASSERT_EQ(0, adb_close(socket_fd[0]));
227
Josh Gao18f7a5c2019-01-11 14:42:08 -0800228 std::this_thread::sleep_for(2s);
229
Josh Gaocd608202018-03-28 18:53:30 -0700230 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700231 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700232 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700233}
234
Josh Gao80206022018-03-15 15:28:55 -0700235// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
236TEST_F(LocalSocketTest, flush_after_shutdown) {
237 int head_fd[2];
238 int tail_fd[2];
239 ASSERT_EQ(0, adb_socketpair(head_fd));
240 ASSERT_EQ(0, adb_socketpair(tail_fd));
241
Josh Gaoc2705962019-01-23 15:36:42 -0800242 asocket* head = create_local_socket(unique_fd(head_fd[1]));
243 asocket* tail = create_local_socket(unique_fd(tail_fd[1]));
Josh Gao80206022018-03-15 15:28:55 -0700244
245 head->peer = tail;
246 head->ready(head);
247
248 tail->peer = head;
249 tail->ready(tail);
250
251 PrepareThread();
Josh Gao80206022018-03-15 15:28:55 -0700252
Josh Gaod5e56ce2018-03-19 15:36:17 -0700253 EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
Josh Gao80206022018-03-15 15:28:55 -0700254
Josh Gaod5e56ce2018-03-19 15:36:17 -0700255 EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
256 const char* str = "write succeeds, but local_socket will fail to write";
257 EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
258 EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
259
260 char buf[6];
261 EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
262 EXPECT_EQ(0, memcmp(buf, "foobar", 6));
Josh Gao80206022018-03-15 15:28:55 -0700263
264 adb_close(head_fd[0]);
265 adb_close(tail_fd[0]);
266
Josh Gaocd608202018-03-28 18:53:30 -0700267 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700268 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700269 TerminateThread();
Josh Gao80206022018-03-15 15:28:55 -0700270}
Josh Gao80206022018-03-15 15:28:55 -0700271
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700272#if defined(__linux__)
273
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000274static void ClientThreadFunc(const int assigned_port) {
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700275 std::string error;
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000276 const int fd = network_loopback_client(assigned_port, SOCK_STREAM, &error);
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700277 ASSERT_GE(fd, 0) << error;
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700278 std::this_thread::sleep_for(1s);
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700279 ASSERT_EQ(0, adb_close(fd));
280}
281
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700282// This test checks if we can close sockets in CLOSE_WAIT state.
283TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao511763b2016-02-10 14:49:00 -0800284 std::string error;
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000285 // Allow the system to allocate an available port.
286 const int listen_fd = network_inaddr_any_server(0, SOCK_STREAM, &error);
Josh Gao511763b2016-02-10 14:49:00 -0800287 ASSERT_GE(listen_fd, 0);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700288
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000289 sockaddr_storage ss;
290 socklen_t ss_size = sizeof(ss);
291 ASSERT_EQ(0, adb_getsockname(listen_fd, reinterpret_cast<sockaddr*>(&ss), &ss_size));
292 int assigned_port;
293 if (ss.ss_family == AF_INET) {
294 assigned_port = ntohs(reinterpret_cast<sockaddr_in*>(&ss)->sin_port);
295 } else {
296 assigned_port = ntohs(reinterpret_cast<sockaddr_in6*>(&ss)->sin6_port);
297 }
298 ASSERT_GE(assigned_port, 0);
Josh Gao511763b2016-02-10 14:49:00 -0800299
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000300 std::thread client_thread(ClientThreadFunc, assigned_port);
301 const int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
302
Josh Gao511763b2016-02-10 14:49:00 -0800303 ASSERT_GE(accept_fd, 0);
Josh Gao511763b2016-02-10 14:49:00 -0800304
305 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700306
307 fdevent_run_on_main_thread([accept_fd]() {
Josh Gaoc2705962019-01-23 15:36:42 -0800308 asocket* s = create_local_socket(unique_fd(accept_fd));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700309 ASSERT_TRUE(s != nullptr);
310 });
Josh Gao511763b2016-02-10 14:49:00 -0800311
Josh Gaocd608202018-03-28 18:53:30 -0700312 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700313 EXPECT_EQ(1u, fdevent_installed_count());
Josh Gao511763b2016-02-10 14:49:00 -0800314
315 // Wait until the client closes its socket.
Josh Gao0f3312a2017-04-12 17:00:49 -0700316 client_thread.join();
Josh Gao511763b2016-02-10 14:49:00 -0800317
Josh Gaocd608202018-03-28 18:53:30 -0700318 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700319 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700320 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700321}
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700322
323#endif // defined(__linux__)
David Pursellc929c6f2016-03-01 08:58:26 -0800324
325#if ADB_HOST
326
Josh Gao8d49e122018-12-19 13:37:41 -0800327#define VerifyParseHostServiceFailed(s) \
328 do { \
329 std::string service(s); \
330 std::string_view serial, command; \
331 bool result = internal::parse_host_service(&serial, &command, service); \
332 EXPECT_FALSE(result); \
333 } while (0)
334
335#define VerifyParseHostService(s, expected_serial, expected_command) \
336 do { \
337 std::string service(s); \
338 std::string_view serial, command; \
339 bool result = internal::parse_host_service(&serial, &command, service); \
340 EXPECT_TRUE(result); \
341 EXPECT_EQ(std::string(expected_serial), std::string(serial)); \
342 EXPECT_EQ(std::string(expected_command), std::string(command)); \
343 } while (0);
David Pursellc929c6f2016-03-01 08:58:26 -0800344
345// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
Josh Gao8d49e122018-12-19 13:37:41 -0800346TEST(socket_test, test_parse_host_service) {
David Pursellc929c6f2016-03-01 08:58:26 -0800347 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
Josh Gao8d49e122018-12-19 13:37:41 -0800348 VerifyParseHostServiceFailed(protocol);
349 VerifyParseHostServiceFailed(protocol + "foo");
David Pursellc929c6f2016-03-01 08:58:26 -0800350
Josh Gao8d49e122018-12-19 13:37:41 -0800351 {
352 std::string serial = protocol + "foo";
353 VerifyParseHostService(serial + ":bar", serial, "bar");
354 VerifyParseHostService(serial + " :bar:baz", serial, "bar:baz");
355 }
David Pursellc929c6f2016-03-01 08:58:26 -0800356
Josh Gao8d49e122018-12-19 13:37:41 -0800357 {
358 // With port.
359 std::string serial = protocol + "foo:123";
360 VerifyParseHostService(serial + ":bar", serial, "bar");
361 VerifyParseHostService(serial + ":456", serial, "456");
362 VerifyParseHostService(serial + ":bar:baz", serial, "bar:baz");
363 }
David Pursellc929c6f2016-03-01 08:58:26 -0800364
365 // Don't register a port unless it's all numbers and ends with ':'.
Josh Gao8d49e122018-12-19 13:37:41 -0800366 VerifyParseHostService(protocol + "foo:123", protocol + "foo", "123");
367 VerifyParseHostService(protocol + "foo:123bar:baz", protocol + "foo", "123bar:baz");
David Pursell24b62a72016-09-21 12:08:37 -0700368
Josh Gao8d49e122018-12-19 13:37:41 -0800369 std::string addresses[] = {"100.100.100.100", "[0123:4567:89ab:CDEF:0:9:a:f]", "[::1]"};
370 for (const std::string& address : addresses) {
371 std::string serial = protocol + address;
372 std::string serial_with_port = protocol + address + ":5555";
373 VerifyParseHostService(serial + ":foo", serial, "foo");
374 VerifyParseHostService(serial_with_port + ":foo", serial_with_port, "foo");
375 }
David Pursell24b62a72016-09-21 12:08:37 -0700376
377 // If we can't find both [] then treat it as a normal serial with [ in it.
Josh Gao8d49e122018-12-19 13:37:41 -0800378 VerifyParseHostService(protocol + "[0123:foo", protocol + "[0123", "foo");
David Pursell24b62a72016-09-21 12:08:37 -0700379
380 // Don't be fooled by random IPv6 addresses in the command string.
Josh Gao8d49e122018-12-19 13:37:41 -0800381 VerifyParseHostService(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
382 protocol + "foo", "ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
383
384 // Handle embedded NULs properly.
385 VerifyParseHostService(protocol + "foo:echo foo\0bar"s, protocol + "foo",
386 "echo foo\0bar"sv);
David Pursellc929c6f2016-03-01 08:58:26 -0800387 }
388}
389
390// Check <prefix>:<serial>:<command> format.
Josh Gao8d49e122018-12-19 13:37:41 -0800391TEST(socket_test, test_parse_host_service_prefix) {
David Pursellc929c6f2016-03-01 08:58:26 -0800392 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
Josh Gao8d49e122018-12-19 13:37:41 -0800393 VerifyParseHostServiceFailed(prefix);
394 VerifyParseHostServiceFailed(prefix + "foo");
David Pursellc929c6f2016-03-01 08:58:26 -0800395
Josh Gao8d49e122018-12-19 13:37:41 -0800396 VerifyParseHostService(prefix + "foo:bar", prefix + "foo", "bar");
397 VerifyParseHostService(prefix + "foo:bar:baz", prefix + "foo", "bar:baz");
398 VerifyParseHostService(prefix + "foo:123:bar", prefix + "foo", "123:bar");
David Pursellc929c6f2016-03-01 08:58:26 -0800399 }
400}
401
402#endif // ADB_HOST