2015-09-16 01:27:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fdevent.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
#include <array>
|
2015-09-16 01:27:09 +02:00
|
|
|
#include <limits>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
2016-11-15 21:37:32 +01:00
|
|
|
#include <thread>
|
2015-09-16 01:27:09 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_io.h"
|
2016-02-10 23:49:00 +01:00
|
|
|
#include "fdevent_test.h"
|
2015-09-16 01:27:09 +02:00
|
|
|
#include "socket.h"
|
|
|
|
#include "sysdeps.h"
|
2016-11-16 03:55:47 +01:00
|
|
|
#include "sysdeps/chrono.h"
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
using namespace std::string_literals;
|
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
2015-09-16 01:27:09 +02:00
|
|
|
struct ThreadArg {
|
|
|
|
int first_read_fd;
|
|
|
|
int last_write_fd;
|
|
|
|
size_t middle_pipe_count;
|
|
|
|
};
|
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
class LocalSocketTest : public FdeventTest {};
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
TEST_F(LocalSocketTest, smoke) {
|
|
|
|
// Join two socketpairs with a chain of intermediate socketpairs.
|
|
|
|
int first[2];
|
|
|
|
std::vector<std::array<int, 2>> intermediates;
|
|
|
|
int last[2];
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
constexpr size_t INTERMEDIATE_COUNT = 50;
|
|
|
|
constexpr size_t MESSAGE_LOOP_COUNT = 100;
|
|
|
|
const std::string MESSAGE = "socket_test";
|
|
|
|
|
|
|
|
intermediates.resize(INTERMEDIATE_COUNT);
|
|
|
|
ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno);
|
|
|
|
ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno);
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* prev_tail = create_local_socket(unique_fd(first[1]));
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_NE(nullptr, prev_tail);
|
|
|
|
|
|
|
|
auto connect = [](asocket* tail, asocket* head) {
|
|
|
|
tail->peer = head;
|
|
|
|
head->peer = tail;
|
|
|
|
tail->ready(tail);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& intermediate : intermediates) {
|
|
|
|
ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno);
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* head = create_local_socket(unique_fd(intermediate[0]));
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_NE(nullptr, head);
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* tail = create_local_socket(unique_fd(intermediate[1]));
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_NE(nullptr, tail);
|
|
|
|
|
|
|
|
connect(prev_tail, head);
|
|
|
|
prev_tail = tail;
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* end = create_local_socket(unique_fd(last[0]));
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_NE(nullptr, end);
|
|
|
|
connect(prev_tail, end);
|
|
|
|
|
|
|
|
PrepareThread();
|
|
|
|
|
2015-09-16 01:27:09 +02:00
|
|
|
for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
|
|
|
|
std::string read_buffer = MESSAGE;
|
|
|
|
std::string write_buffer(MESSAGE.size(), 'a');
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size()));
|
|
|
|
ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size()));
|
2015-09-16 01:27:09 +02:00
|
|
|
ASSERT_EQ(read_buffer, write_buffer);
|
|
|
|
}
|
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_EQ(0, adb_close(first[0]));
|
|
|
|
ASSERT_EQ(0, adb_close(last[1]));
|
|
|
|
|
|
|
|
// Wait until the local sockets are closed.
|
2018-03-29 03:53:30 +02:00
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2018-03-30 01:27:13 +02:00
|
|
|
TerminateThread();
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CloseWithPacketArg {
|
2019-01-24 00:36:42 +01:00
|
|
|
unique_fd socket_fd;
|
2015-09-16 01:27:09 +02:00
|
|
|
size_t bytes_written;
|
2019-01-24 00:36:42 +01:00
|
|
|
unique_fd cause_close_fd;
|
2015-09-16 01:27:09 +02:00
|
|
|
};
|
|
|
|
|
2018-03-30 01:27:13 +02:00
|
|
|
static void CreateCloser(CloseWithPacketArg* arg) {
|
|
|
|
fdevent_run_on_main_thread([arg]() {
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* s = create_local_socket(std::move(arg->socket_fd));
|
2018-03-30 01:27:13 +02:00
|
|
|
ASSERT_TRUE(s != nullptr);
|
|
|
|
arg->bytes_written = 0;
|
|
|
|
|
2018-03-30 23:05:40 +02:00
|
|
|
// On platforms that implement sockets via underlying sockets (e.g. Wine),
|
|
|
|
// a socket can appear to be full, and then become available for writes
|
|
|
|
// again without read being called on the other end. Loop and sleep after
|
|
|
|
// each write to give the underlying implementation time to flush.
|
|
|
|
bool socket_filled = false;
|
|
|
|
for (int i = 0; i < 128; ++i) {
|
2018-03-08 01:52:28 +01:00
|
|
|
apacket::payload_type data;
|
2018-03-30 23:05:40 +02:00
|
|
|
data.resize(MAX_PAYLOAD);
|
|
|
|
arg->bytes_written += data.size();
|
|
|
|
int ret = s->enqueue(s, std::move(data));
|
|
|
|
if (ret == 1) {
|
|
|
|
socket_filled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ASSERT_NE(-1, ret);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(250ms);
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(socket_filled);
|
2018-03-30 01:27:13 +02:00
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* cause_close_s = create_local_socket(std::move(arg->cause_close_fd));
|
2018-03-30 01:27:13 +02:00
|
|
|
ASSERT_TRUE(cause_close_s != nullptr);
|
|
|
|
cause_close_s->peer = s;
|
|
|
|
s->peer = cause_close_s;
|
|
|
|
cause_close_s->ready(cause_close_s);
|
|
|
|
});
|
|
|
|
WaitForFdeventLoop();
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks if we can close local socket in the following situation:
|
|
|
|
// The socket is closing but having some packets, so it is not closed. Then
|
|
|
|
// some write error happens in the socket's file handler, e.g., the file
|
|
|
|
// handler is closed.
|
2015-09-29 21:25:33 +02:00
|
|
|
TEST_F(LocalSocketTest, close_socket_with_packet) {
|
2015-09-16 01:27:09 +02:00
|
|
|
int socket_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(socket_fd));
|
|
|
|
int cause_close_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(cause_close_fd));
|
|
|
|
CloseWithPacketArg arg;
|
2019-01-24 00:36:42 +01:00
|
|
|
arg.socket_fd.reset(socket_fd[1]);
|
|
|
|
arg.cause_close_fd.reset(cause_close_fd[1]);
|
2016-02-10 23:49:00 +01:00
|
|
|
|
|
|
|
PrepareThread();
|
2018-03-30 01:27:13 +02:00
|
|
|
CreateCloser(&arg);
|
2018-03-29 03:53:30 +02:00
|
|
|
|
2015-09-16 01:27:09 +02:00
|
|
|
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
|
2018-03-29 03:53:30 +02:00
|
|
|
|
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2015-09-16 01:27:09 +02:00
|
|
|
ASSERT_EQ(0, adb_close(socket_fd[0]));
|
2018-03-29 03:53:30 +02:00
|
|
|
|
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2018-03-30 01:27:13 +02:00
|
|
|
TerminateThread();
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks if we can read packets from a closing local socket.
|
2015-09-29 21:25:33 +02:00
|
|
|
TEST_F(LocalSocketTest, read_from_closing_socket) {
|
2015-09-16 01:27:09 +02:00
|
|
|
int socket_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(socket_fd));
|
|
|
|
int cause_close_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(cause_close_fd));
|
|
|
|
CloseWithPacketArg arg;
|
2019-01-24 00:36:42 +01:00
|
|
|
arg.socket_fd.reset(socket_fd[1]);
|
|
|
|
arg.cause_close_fd.reset(cause_close_fd[1]);
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
PrepareThread();
|
2018-03-30 01:27:13 +02:00
|
|
|
CreateCloser(&arg);
|
2018-03-29 03:53:30 +02:00
|
|
|
|
|
|
|
WaitForFdeventLoop();
|
2015-09-16 01:27:09 +02:00
|
|
|
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
|
2018-03-29 03:53:30 +02:00
|
|
|
|
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2015-09-16 01:27:09 +02:00
|
|
|
|
|
|
|
// Verify if we can read successfully.
|
|
|
|
std::vector<char> buf(arg.bytes_written);
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_NE(0u, arg.bytes_written);
|
2015-09-16 01:27:09 +02:00
|
|
|
ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
|
|
|
|
ASSERT_EQ(0, adb_close(socket_fd[0]));
|
|
|
|
|
2018-03-29 03:53:30 +02:00
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2018-03-30 01:27:13 +02:00
|
|
|
TerminateThread();
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks if we can close local socket in the following situation:
|
|
|
|
// The socket is not closed and has some packets. When it fails to write to
|
|
|
|
// the socket's file handler because the other end is closed, we check if the
|
|
|
|
// socket is closed.
|
|
|
|
TEST_F(LocalSocketTest, write_error_when_having_packets) {
|
|
|
|
int socket_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(socket_fd));
|
|
|
|
int cause_close_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(cause_close_fd));
|
|
|
|
CloseWithPacketArg arg;
|
2019-01-24 00:36:42 +01:00
|
|
|
arg.socket_fd.reset(socket_fd[1]);
|
|
|
|
arg.cause_close_fd.reset(cause_close_fd[1]);
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
PrepareThread();
|
2018-03-30 01:27:13 +02:00
|
|
|
CreateCloser(&arg);
|
2018-03-29 03:53:30 +02:00
|
|
|
|
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2015-09-16 01:27:09 +02:00
|
|
|
ASSERT_EQ(0, adb_close(socket_fd[0]));
|
|
|
|
|
2019-01-11 23:42:08 +01:00
|
|
|
std::this_thread::sleep_for(2s);
|
|
|
|
|
2018-03-29 03:53:30 +02:00
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2018-03-30 01:27:13 +02:00
|
|
|
TerminateThread();
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:28:55 +01:00
|
|
|
// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
|
|
|
|
TEST_F(LocalSocketTest, flush_after_shutdown) {
|
|
|
|
int head_fd[2];
|
|
|
|
int tail_fd[2];
|
|
|
|
ASSERT_EQ(0, adb_socketpair(head_fd));
|
|
|
|
ASSERT_EQ(0, adb_socketpair(tail_fd));
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* head = create_local_socket(unique_fd(head_fd[1]));
|
|
|
|
asocket* tail = create_local_socket(unique_fd(tail_fd[1]));
|
2018-03-15 23:28:55 +01:00
|
|
|
|
|
|
|
head->peer = tail;
|
|
|
|
head->ready(head);
|
|
|
|
|
|
|
|
tail->peer = head;
|
|
|
|
tail->ready(tail);
|
|
|
|
|
|
|
|
PrepareThread();
|
|
|
|
|
2018-03-19 23:36:17 +01:00
|
|
|
EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
|
|
|
|
|
|
|
|
EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
|
2018-03-15 23:28:55 +01:00
|
|
|
const char* str = "write succeeds, but local_socket will fail to write";
|
2018-03-19 23:36:17 +01:00
|
|
|
EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
|
|
|
|
EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
|
2018-03-15 23:28:55 +01:00
|
|
|
|
2018-03-19 23:36:17 +01:00
|
|
|
char buf[6];
|
|
|
|
EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
|
|
|
|
EXPECT_EQ(0, memcmp(buf, "foobar", 6));
|
2018-03-15 23:28:55 +01:00
|
|
|
|
|
|
|
adb_close(head_fd[0]);
|
|
|
|
adb_close(tail_fd[0]);
|
|
|
|
|
2018-03-29 03:53:30 +02:00
|
|
|
WaitForFdeventLoop();
|
2018-03-15 23:28:55 +01:00
|
|
|
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2018-03-30 01:27:13 +02:00
|
|
|
TerminateThread();
|
2018-03-15 23:28:55 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:25:33 +02:00
|
|
|
#if defined(__linux__)
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2015-09-29 21:25:33 +02:00
|
|
|
static void ClientThreadFunc() {
|
|
|
|
std::string error;
|
|
|
|
int fd = network_loopback_client(5038, SOCK_STREAM, &error);
|
|
|
|
ASSERT_GE(fd, 0) << error;
|
2018-03-30 01:27:13 +02:00
|
|
|
std::this_thread::sleep_for(1s);
|
2015-09-29 21:25:33 +02:00
|
|
|
ASSERT_EQ(0, adb_close(fd));
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:25:33 +02:00
|
|
|
// This test checks if we can close sockets in CLOSE_WAIT state.
|
|
|
|
TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
|
2016-02-10 23:49:00 +01:00
|
|
|
std::string error;
|
|
|
|
int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
|
|
|
|
ASSERT_GE(listen_fd, 0);
|
|
|
|
|
2017-04-13 02:00:49 +02:00
|
|
|
std::thread client_thread(ClientThreadFunc);
|
2016-02-10 23:49:00 +01:00
|
|
|
|
2016-08-23 21:50:00 +02:00
|
|
|
int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
|
2016-02-10 23:49:00 +01:00
|
|
|
ASSERT_GE(accept_fd, 0);
|
|
|
|
|
|
|
|
PrepareThread();
|
2018-03-30 01:27:13 +02:00
|
|
|
|
|
|
|
fdevent_run_on_main_thread([accept_fd]() {
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* s = create_local_socket(unique_fd(accept_fd));
|
2018-03-30 01:27:13 +02:00
|
|
|
ASSERT_TRUE(s != nullptr);
|
|
|
|
});
|
2016-02-10 23:49:00 +01:00
|
|
|
|
2018-03-29 03:53:30 +02:00
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2016-02-10 23:49:00 +01:00
|
|
|
|
|
|
|
// Wait until the client closes its socket.
|
2017-04-13 02:00:49 +02:00
|
|
|
client_thread.join();
|
2016-02-10 23:49:00 +01:00
|
|
|
|
2018-03-29 03:53:30 +02:00
|
|
|
WaitForFdeventLoop();
|
2016-04-26 04:48:19 +02:00
|
|
|
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
|
2018-03-30 01:27:13 +02:00
|
|
|
TerminateThread();
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|
2015-09-29 21:25:33 +02:00
|
|
|
|
|
|
|
#endif // defined(__linux__)
|
2016-03-01 17:58:26 +01:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
#define VerifyParseHostServiceFailed(s) \
|
|
|
|
do { \
|
|
|
|
std::string service(s); \
|
|
|
|
std::string_view serial, command; \
|
|
|
|
bool result = internal::parse_host_service(&serial, &command, service); \
|
|
|
|
EXPECT_FALSE(result); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define VerifyParseHostService(s, expected_serial, expected_command) \
|
|
|
|
do { \
|
|
|
|
std::string service(s); \
|
|
|
|
std::string_view serial, command; \
|
|
|
|
bool result = internal::parse_host_service(&serial, &command, service); \
|
|
|
|
EXPECT_TRUE(result); \
|
|
|
|
EXPECT_EQ(std::string(expected_serial), std::string(serial)); \
|
|
|
|
EXPECT_EQ(std::string(expected_command), std::string(command)); \
|
|
|
|
} while (0);
|
2016-03-01 17:58:26 +01:00
|
|
|
|
|
|
|
// Check [tcp:|udp:]<serial>[:<port>]:<command> format.
|
2018-12-19 22:37:41 +01:00
|
|
|
TEST(socket_test, test_parse_host_service) {
|
2016-03-01 17:58:26 +01:00
|
|
|
for (const std::string& protocol : {"", "tcp:", "udp:"}) {
|
2018-12-19 22:37:41 +01:00
|
|
|
VerifyParseHostServiceFailed(protocol);
|
|
|
|
VerifyParseHostServiceFailed(protocol + "foo");
|
2016-03-01 17:58:26 +01:00
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
{
|
|
|
|
std::string serial = protocol + "foo";
|
|
|
|
VerifyParseHostService(serial + ":bar", serial, "bar");
|
|
|
|
VerifyParseHostService(serial + " :bar:baz", serial, "bar:baz");
|
|
|
|
}
|
2016-03-01 17:58:26 +01:00
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
{
|
|
|
|
// With port.
|
|
|
|
std::string serial = protocol + "foo:123";
|
|
|
|
VerifyParseHostService(serial + ":bar", serial, "bar");
|
|
|
|
VerifyParseHostService(serial + ":456", serial, "456");
|
|
|
|
VerifyParseHostService(serial + ":bar:baz", serial, "bar:baz");
|
|
|
|
}
|
2016-03-01 17:58:26 +01:00
|
|
|
|
|
|
|
// Don't register a port unless it's all numbers and ends with ':'.
|
2018-12-19 22:37:41 +01:00
|
|
|
VerifyParseHostService(protocol + "foo:123", protocol + "foo", "123");
|
|
|
|
VerifyParseHostService(protocol + "foo:123bar:baz", protocol + "foo", "123bar:baz");
|
|
|
|
|
|
|
|
std::string addresses[] = {"100.100.100.100", "[0123:4567:89ab:CDEF:0:9:a:f]", "[::1]"};
|
|
|
|
for (const std::string& address : addresses) {
|
|
|
|
std::string serial = protocol + address;
|
|
|
|
std::string serial_with_port = protocol + address + ":5555";
|
|
|
|
VerifyParseHostService(serial + ":foo", serial, "foo");
|
|
|
|
VerifyParseHostService(serial_with_port + ":foo", serial_with_port, "foo");
|
|
|
|
}
|
2016-09-21 21:08:37 +02:00
|
|
|
|
|
|
|
// If we can't find both [] then treat it as a normal serial with [ in it.
|
2018-12-19 22:37:41 +01:00
|
|
|
VerifyParseHostService(protocol + "[0123:foo", protocol + "[0123", "foo");
|
2016-09-21 21:08:37 +02:00
|
|
|
|
|
|
|
// Don't be fooled by random IPv6 addresses in the command string.
|
2018-12-19 22:37:41 +01:00
|
|
|
VerifyParseHostService(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555",
|
|
|
|
protocol + "foo", "ping [0123:4567:89ab:CDEF:0:9:a:f]:5555");
|
|
|
|
|
|
|
|
// Handle embedded NULs properly.
|
|
|
|
VerifyParseHostService(protocol + "foo:echo foo\0bar"s, protocol + "foo",
|
|
|
|
"echo foo\0bar"sv);
|
2016-03-01 17:58:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check <prefix>:<serial>:<command> format.
|
2018-12-19 22:37:41 +01:00
|
|
|
TEST(socket_test, test_parse_host_service_prefix) {
|
2016-03-01 17:58:26 +01:00
|
|
|
for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) {
|
2018-12-19 22:37:41 +01:00
|
|
|
VerifyParseHostServiceFailed(prefix);
|
|
|
|
VerifyParseHostServiceFailed(prefix + "foo");
|
2016-03-01 17:58:26 +01:00
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
VerifyParseHostService(prefix + "foo:bar", prefix + "foo", "bar");
|
|
|
|
VerifyParseHostService(prefix + "foo:bar:baz", prefix + "foo", "bar:baz");
|
|
|
|
VerifyParseHostService(prefix + "foo:123:bar", prefix + "foo", "123:bar");
|
2016-03-01 17:58:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ADB_HOST
|