blob: f1b64b7a68df0d6a617651311c58601b01aa42e8 [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"
Shaju Mathew3f7b4402023-01-19 21:56:37 -080036#include "test_utils/test_utils.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070037
Josh Gao8d49e122018-12-19 13:37:41 -080038using namespace std::string_literals;
39using namespace std::string_view_literals;
40
Yabin Cui2ce9d562015-09-15 16:27:09 -070041struct ThreadArg {
42 int first_read_fd;
43 int last_write_fd;
44 size_t middle_pipe_count;
45};
46
Josh Gao511763b2016-02-10 14:49:00 -080047class LocalSocketTest : public FdeventTest {};
Yabin Cui2ce9d562015-09-15 16:27:09 -070048
Yabin Cui2ce9d562015-09-15 16:27:09 -070049TEST_F(LocalSocketTest, smoke) {
Josh Gao511763b2016-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 Cui2ce9d562015-09-15 16:27:09 -070057 const std::string MESSAGE = "socket_test";
Yabin Cui2ce9d562015-09-15 16:27:09 -070058
Josh Gao511763b2016-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);
Josh Gaoc2705962019-01-23 15:36:42 -080062 asocket* prev_tail = create_local_socket(unique_fd(first[1]));
Josh Gao511763b2016-02-10 14:49:00 -080063 ASSERT_NE(nullptr, prev_tail);
Yabin Cui2ce9d562015-09-15 16:27:09 -070064
Josh Gao511763b2016-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
Josh Gaoc2705962019-01-23 15:36:42 -080074 asocket* head = create_local_socket(unique_fd(intermediate[0]));
Josh Gao511763b2016-02-10 14:49:00 -080075 ASSERT_NE(nullptr, head);
76
Josh Gaoc2705962019-01-23 15:36:42 -080077 asocket* tail = create_local_socket(unique_fd(intermediate[1]));
Josh Gao511763b2016-02-10 14:49:00 -080078 ASSERT_NE(nullptr, tail);
79
80 connect(prev_tail, head);
81 prev_tail = tail;
82 }
83
Josh Gaoc2705962019-01-23 15:36:42 -080084 asocket* end = create_local_socket(unique_fd(last[0]));
Josh Gao511763b2016-02-10 14:49:00 -080085 ASSERT_NE(nullptr, end);
86 connect(prev_tail, end);
87
88 PrepareThread();
Josh Gao511763b2016-02-10 14:49:00 -080089
Yabin Cui2ce9d562015-09-15 16:27:09 -070090 for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
91 std::string read_buffer = MESSAGE;
92 std::string write_buffer(MESSAGE.size(), 'a');
Josh Gao511763b2016-02-10 14:49:00 -080093 ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
94 ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
Yabin Cui2ce9d562015-09-15 16:27:09 -070095 ASSERT_EQ(read_buffer, write_buffer);
96 }
Yabin Cui2ce9d562015-09-15 16:27:09 -070097
Josh Gao511763b2016-02-10 14:49:00 -080098 ASSERT_EQ(0, adb_close(first[0]));
99 ASSERT_EQ(0, adb_close(last[1]));
100
101 // Wait until the local sockets are closed.
Josh Gaocd608202018-03-28 18:53:30 -0700102 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700103 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700104 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700105}
106
107struct CloseWithPacketArg {
Josh Gaoc2705962019-01-23 15:36:42 -0800108 unique_fd socket_fd;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700109 size_t bytes_written;
Josh Gaoc2705962019-01-23 15:36:42 -0800110 unique_fd cause_close_fd;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700111};
112
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700113static void CreateCloser(CloseWithPacketArg* arg) {
Shaju Mathew705fa1c2022-12-31 19:36:54 -0800114 fdevent_run_on_looper([arg]() {
Josh Gaoc2705962019-01-23 15:36:42 -0800115 asocket* s = create_local_socket(std::move(arg->socket_fd));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700116 ASSERT_TRUE(s != nullptr);
117 arg->bytes_written = 0;
Josh Gaod5e56ce2018-03-19 15:36:17 -0700118
Josh Gao205a8f42018-03-30 14:05:40 -0700119 // On platforms that implement sockets via underlying sockets (e.g. Wine),
120 // a socket can appear to be full, and then become available for writes
121 // again without read being called on the other end. Loop and sleep after
122 // each write to give the underlying implementation time to flush.
123 bool socket_filled = false;
124 for (int i = 0; i < 128; ++i) {
Josh Gaocd2a5292018-03-07 16:52:28 -0800125 apacket::payload_type data;
Josh Gao205a8f42018-03-30 14:05:40 -0700126 data.resize(MAX_PAYLOAD);
127 arg->bytes_written += data.size();
128 int ret = s->enqueue(s, std::move(data));
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000129
130 // Return value of 0 implies that more data can be accepted.
Josh Gao205a8f42018-03-30 14:05:40 -0700131 if (ret == 1) {
132 socket_filled = true;
133 break;
134 }
135 ASSERT_NE(-1, ret);
136
137 std::this_thread::sleep_for(250ms);
138 }
139 ASSERT_TRUE(socket_filled);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700140
Josh Gaoc2705962019-01-23 15:36:42 -0800141 asocket* cause_close_s = create_local_socket(std::move(arg->cause_close_fd));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700142 ASSERT_TRUE(cause_close_s != nullptr);
143 cause_close_s->peer = s;
144 s->peer = cause_close_s;
145 cause_close_s->ready(cause_close_s);
146 });
147 WaitForFdeventLoop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700148}
149
150// This test checks if we can close local socket in the following situation:
151// The socket is closing but having some packets, so it is not closed. Then
152// some write error happens in the socket's file handler, e.g., the file
153// handler is closed.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700154TEST_F(LocalSocketTest, close_socket_with_packet) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700155 int socket_fd[2];
156 ASSERT_EQ(0, adb_socketpair(socket_fd));
157 int cause_close_fd[2];
158 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
159 CloseWithPacketArg arg;
Josh Gaoc2705962019-01-23 15:36:42 -0800160 arg.socket_fd.reset(socket_fd[1]);
161 arg.cause_close_fd.reset(cause_close_fd[1]);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700162
Josh Gao511763b2016-02-10 14:49:00 -0800163 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700164 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700165
Josh Gao511763b2016-02-10 14:49:00 -0800166 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700167
168 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700169 EXPECT_EQ(1u, fdevent_installed_count());
Josh Gao511763b2016-02-10 14:49:00 -0800170 ASSERT_EQ(0, adb_close(socket_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700171
172 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700173 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700174 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700175}
176
Yabin Cui2ce9d562015-09-15 16:27:09 -0700177// This test checks if we can read packets from a closing local socket.
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700178TEST_F(LocalSocketTest, read_from_closing_socket) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700179 int socket_fd[2];
180 ASSERT_EQ(0, adb_socketpair(socket_fd));
181 int cause_close_fd[2];
182 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
183 CloseWithPacketArg arg;
Josh Gaoc2705962019-01-23 15:36:42 -0800184 arg.socket_fd.reset(socket_fd[1]);
185 arg.cause_close_fd.reset(cause_close_fd[1]);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700186
Josh Gao511763b2016-02-10 14:49:00 -0800187 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700188 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700189
190 WaitForFdeventLoop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700191 ASSERT_EQ(0, adb_close(cause_close_fd[0]));
Josh Gaocd608202018-03-28 18:53:30 -0700192
193 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700194 EXPECT_EQ(1u, fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700195
196 // Verify if we can read successfully.
197 std::vector<char> buf(arg.bytes_written);
Josh Gao511763b2016-02-10 14:49:00 -0800198 ASSERT_NE(0u, arg.bytes_written);
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000199
200 ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); // TODO: b/237341044
201
Yabin Cui2ce9d562015-09-15 16:27:09 -0700202 ASSERT_EQ(0, adb_close(socket_fd[0]));
203
Josh Gaocd608202018-03-28 18:53:30 -0700204 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700205 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700206 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700207}
208
209// This test checks if we can close local socket in the following situation:
210// The socket is not closed and has some packets. When it fails to write to
211// the socket's file handler because the other end is closed, we check if the
212// socket is closed.
213TEST_F(LocalSocketTest, write_error_when_having_packets) {
214 int socket_fd[2];
215 ASSERT_EQ(0, adb_socketpair(socket_fd));
216 int cause_close_fd[2];
217 ASSERT_EQ(0, adb_socketpair(cause_close_fd));
218 CloseWithPacketArg arg;
Josh Gaoc2705962019-01-23 15:36:42 -0800219 arg.socket_fd.reset(socket_fd[1]);
220 arg.cause_close_fd.reset(cause_close_fd[1]);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700221
Josh Gao511763b2016-02-10 14:49:00 -0800222 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700223 CreateCloser(&arg);
Josh Gaocd608202018-03-28 18:53:30 -0700224
225 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700226 EXPECT_EQ(2u, fdevent_installed_count());
Yabin Cui2ce9d562015-09-15 16:27:09 -0700227 ASSERT_EQ(0, adb_close(socket_fd[0]));
228
Josh Gao18f7a5c2019-01-11 14:42:08 -0800229 std::this_thread::sleep_for(2s);
230
Josh Gaocd608202018-03-28 18:53:30 -0700231 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700232 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700233 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700234}
235
Josh Gao80206022018-03-15 15:28:55 -0700236// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
237TEST_F(LocalSocketTest, flush_after_shutdown) {
238 int head_fd[2];
239 int tail_fd[2];
240 ASSERT_EQ(0, adb_socketpair(head_fd));
241 ASSERT_EQ(0, adb_socketpair(tail_fd));
242
Josh Gaoc2705962019-01-23 15:36:42 -0800243 asocket* head = create_local_socket(unique_fd(head_fd[1]));
244 asocket* tail = create_local_socket(unique_fd(tail_fd[1]));
Josh Gao80206022018-03-15 15:28:55 -0700245
246 head->peer = tail;
247 head->ready(head);
248
249 tail->peer = head;
250 tail->ready(tail);
251
252 PrepareThread();
Josh Gao80206022018-03-15 15:28:55 -0700253
Josh Gaod5e56ce2018-03-19 15:36:17 -0700254 EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
Josh Gao80206022018-03-15 15:28:55 -0700255
Josh Gaod5e56ce2018-03-19 15:36:17 -0700256 EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
257 const char* str = "write succeeds, but local_socket will fail to write";
258 EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
259 EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
260
261 char buf[6];
262 EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
263 EXPECT_EQ(0, memcmp(buf, "foobar", 6));
Josh Gao80206022018-03-15 15:28:55 -0700264
265 adb_close(head_fd[0]);
266 adb_close(tail_fd[0]);
267
Josh Gaocd608202018-03-28 18:53:30 -0700268 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700269 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700270 TerminateThread();
Josh Gao80206022018-03-15 15:28:55 -0700271}
Josh Gao80206022018-03-15 15:28:55 -0700272
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700273#if defined(__linux__)
274
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000275static void ClientThreadFunc(const int assigned_port) {
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700276 std::string error;
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000277 const int fd = network_loopback_client(assigned_port, SOCK_STREAM, &error);
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700278 ASSERT_GE(fd, 0) << error;
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700279 std::this_thread::sleep_for(1s);
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700280 ASSERT_EQ(0, adb_close(fd));
281}
282
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700283// This test checks if we can close sockets in CLOSE_WAIT state.
284TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
Josh Gao511763b2016-02-10 14:49:00 -0800285 std::string error;
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000286 // Allow the system to allocate an available port.
Shaju Mathew3f7b4402023-01-19 21:56:37 -0800287 unique_fd listen_fd;
288 const int assigned_port(test_utils::GetUnassignedPort(listen_fd));
Josh Gao511763b2016-02-10 14:49:00 -0800289
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000290 std::thread client_thread(ClientThreadFunc, assigned_port);
Shaju Mathew3f7b4402023-01-19 21:56:37 -0800291 const int accept_fd = adb_socket_accept(listen_fd.get(), nullptr, nullptr);
Shaju Mathew7a0735c2022-09-06 17:11:21 +0000292
Josh Gao511763b2016-02-10 14:49:00 -0800293 ASSERT_GE(accept_fd, 0);
Josh Gao511763b2016-02-10 14:49:00 -0800294
295 PrepareThread();
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700296
Shaju Mathew705fa1c2022-12-31 19:36:54 -0800297 fdevent_run_on_looper([accept_fd]() {
Josh Gaoc2705962019-01-23 15:36:42 -0800298 asocket* s = create_local_socket(unique_fd(accept_fd));
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700299 ASSERT_TRUE(s != nullptr);
300 });
Josh Gao511763b2016-02-10 14:49:00 -0800301
Josh Gaocd608202018-03-28 18:53:30 -0700302 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700303 EXPECT_EQ(1u, fdevent_installed_count());
Josh Gao511763b2016-02-10 14:49:00 -0800304
305 // Wait until the client closes its socket.
Josh Gao0f3312a2017-04-12 17:00:49 -0700306 client_thread.join();
Josh Gao511763b2016-02-10 14:49:00 -0800307
Josh Gaocd608202018-03-28 18:53:30 -0700308 WaitForFdeventLoop();
Fabien Sanglard3bff3302022-08-24 16:38:07 -0700309 ASSERT_EQ(0u, fdevent_installed_count());
Josh Gaoc7bdb6d2018-03-29 16:27:13 -0700310 TerminateThread();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700311}
Yabin Cuiec2e7d82015-09-29 12:25:33 -0700312
313#endif // defined(__linux__)
David Pursellc929c6f2016-03-01 08:58:26 -0800314
315#if ADB_HOST
316
Josh Gao8d49e122018-12-19 13:37:41 -0800317#define VerifyParseHostServiceFailed(s) \
318 do { \
319 std::string service(s); \
320 std::string_view serial, command; \
321 bool result = internal::parse_host_service(&serial, &command, service); \
322 EXPECT_FALSE(result); \
323 } while (0)
324
325#define VerifyParseHostService(s, expected_serial, expected_command) \
326 do { \
327 std::string service(s); \
328 std::string_view serial, command; \
329 bool result = internal::parse_host_service(&serial, &command, service); \
330 EXPECT_TRUE(result); \
331 EXPECT_EQ(std::string(expected_serial), std::string(serial)); \
332 EXPECT_EQ(std::string(expected_command), std::string(command)); \
333 } while (0);
David Pursellc929c6f2016-03-01 08:58:26 -0800334
335// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
Josh Gao8d49e122018-12-19 13:37:41 -0800336TEST(socket_test, test_parse_host_service) {
David Pursellc929c6f2016-03-01 08:58:26 -0800337 for (const std::string& protocol : {"", "tcp:", "udp:"}) {
Josh Gao8d49e122018-12-19 13:37:41 -0800338 VerifyParseHostServiceFailed(protocol);
339 VerifyParseHostServiceFailed(protocol + "foo");
David Pursellc929c6f2016-03-01 08:58:26 -0800340
Josh Gao8d49e122018-12-19 13:37:41 -0800341 {
342 std::string serial = protocol + "foo";
343 VerifyParseHostService(serial + ":bar", serial, "bar");
344 VerifyParseHostService(serial + " :bar:baz", serial, "bar:baz");
345 }
David Pursellc929c6f2016-03-01 08:58:26 -0800346
Josh Gao8d49e122018-12-19 13:37:41 -0800347 {
348 // With port.
349 std::string serial = protocol + "foo:123";
350 VerifyParseHostService(serial + ":bar", serial, "bar");
351 VerifyParseHostService(serial + ":456", serial, "456");
352 VerifyParseHostService(serial + ":bar:baz", serial, "bar:baz");
353 }
David Pursellc929c6f2016-03-01 08:58:26 -0800354
355 // Don't register a port unless it's all numbers and ends with ':'.
Josh Gao8d49e122018-12-19 13:37:41 -0800356 VerifyParseHostService(protocol + "foo:123", protocol + "foo", "123");
357 VerifyParseHostService(protocol + "foo:123bar:baz", protocol + "foo", "123bar:baz");
David Pursell24b62a72016-09-21 12:08:37 -0700358
Josh Gao8d49e122018-12-19 13:37:41 -0800359 std::string addresses[] = {"100.100.100.100", "[0123:4567:89ab:CDEF:0:9:a:f]", "[::1]"};
360 for (const std::string& address : addresses) {
361 std::string serial = protocol + address;
362 std::string serial_with_port = protocol + address + ":5555";
363 VerifyParseHostService(serial + ":foo", serial, "foo");
364 VerifyParseHostService(serial_with_port + ":foo", serial_with_port, "foo");
365 }
David Pursell24b62a72016-09-21 12:08:37 -0700366
367 // If we can't find both [] then treat it as a normal serial with [ in it.
Josh Gao8d49e122018-12-19 13:37:41 -0800368 VerifyParseHostService(protocol + "[0123:foo", protocol + "[0123", "foo");
David Pursell24b62a72016-09-21 12:08:37 -0700369
370 // Don't be fooled by random IPv6 addresses in the command string.
Josh Gao8d49e122018-12-19 13:37:41 -0800371 VerifyParseHostService(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
372 protocol + "foo", "ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
373
374 // Handle embedded NULs properly.
375 VerifyParseHostService(protocol + "foo:echo foo\0bar"s, protocol + "foo",
376 "echo foo\0bar"sv);
David Pursellc929c6f2016-03-01 08:58:26 -0800377 }
378}
379
380// Check <prefix>:<serial>:<command> format.
Josh Gao8d49e122018-12-19 13:37:41 -0800381TEST(socket_test, test_parse_host_service_prefix) {
David Pursellc929c6f2016-03-01 08:58:26 -0800382 for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
Josh Gao8d49e122018-12-19 13:37:41 -0800383 VerifyParseHostServiceFailed(prefix);
384 VerifyParseHostServiceFailed(prefix + "foo");
David Pursellc929c6f2016-03-01 08:58:26 -0800385
Josh Gao8d49e122018-12-19 13:37:41 -0800386 VerifyParseHostService(prefix + "foo:bar", prefix + "foo", "bar");
387 VerifyParseHostService(prefix + "foo:bar:baz", prefix + "foo", "bar:baz");
388 VerifyParseHostService(prefix + "foo:123:bar", prefix + "foo", "123:bar");
David Pursellc929c6f2016-03-01 08:58:26 -0800389 }
390}
391
392#endif // ADB_HOST