Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 21 | #include <array> |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 22 | #include <limits> |
| 23 | #include <queue> |
| 24 | #include <string> |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 25 | #include <thread> |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
| 30 | #include "adb.h" |
| 31 | #include "adb_io.h" |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 32 | #include "fdevent_test.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 33 | #include "socket.h" |
| 34 | #include "sysdeps.h" |
Josh Gao | 70267e4 | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 35 | #include "sysdeps/chrono.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 36 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 37 | struct ThreadArg { |
| 38 | int first_read_fd; |
| 39 | int last_write_fd; |
| 40 | size_t middle_pipe_count; |
| 41 | }; |
| 42 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 43 | class LocalSocketTest : public FdeventTest {}; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 44 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 45 | static void FdEventThreadFunc(void*) { |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 46 | fdevent_loop(); |
| 47 | } |
| 48 | |
Josh Gao | 70267e4 | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 49 | constexpr auto SLEEP_FOR_FDEVENT = 100ms; |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 50 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 51 | TEST_F(LocalSocketTest, smoke) { |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 52 | // Join two socketpairs with a chain of intermediate socketpairs. |
| 53 | int first[2]; |
| 54 | std::vector<std::array<int, 2>> intermediates; |
| 55 | int last[2]; |
| 56 | |
| 57 | constexpr size_t INTERMEDIATE_COUNT = 50; |
| 58 | constexpr size_t MESSAGE_LOOP_COUNT = 100; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 59 | const std::string MESSAGE = "socket_test"; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 60 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 61 | intermediates.resize(INTERMEDIATE_COUNT); |
| 62 | ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno); |
| 63 | ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno); |
| 64 | asocket* prev_tail = create_local_socket(first[1]); |
| 65 | ASSERT_NE(nullptr, prev_tail); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 66 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 67 | auto connect = [](asocket* tail, asocket* head) { |
| 68 | tail->peer = head; |
| 69 | head->peer = tail; |
| 70 | tail->ready(tail); |
| 71 | }; |
| 72 | |
| 73 | for (auto& intermediate : intermediates) { |
| 74 | ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno); |
| 75 | |
| 76 | asocket* head = create_local_socket(intermediate[0]); |
| 77 | ASSERT_NE(nullptr, head); |
| 78 | |
| 79 | asocket* tail = create_local_socket(intermediate[1]); |
| 80 | ASSERT_NE(nullptr, tail); |
| 81 | |
| 82 | connect(prev_tail, head); |
| 83 | prev_tail = tail; |
| 84 | } |
| 85 | |
| 86 | asocket* end = create_local_socket(last[0]); |
| 87 | ASSERT_NE(nullptr, end); |
| 88 | connect(prev_tail, end); |
| 89 | |
| 90 | PrepareThread(); |
| 91 | adb_thread_t thread; |
| 92 | ASSERT_TRUE(adb_thread_create(FdEventThreadFunc, nullptr, &thread)); |
| 93 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 94 | for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) { |
| 95 | std::string read_buffer = MESSAGE; |
| 96 | std::string write_buffer(MESSAGE.size(), 'a'); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 97 | ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size())); |
| 98 | ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size())); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 99 | ASSERT_EQ(read_buffer, write_buffer); |
| 100 | } |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 101 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 102 | ASSERT_EQ(0, adb_close(first[0])); |
| 103 | ASSERT_EQ(0, adb_close(last[1])); |
| 104 | |
| 105 | // Wait until the local sockets are closed. |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 106 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 107 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 108 | TerminateThread(thread); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | struct CloseWithPacketArg { |
| 112 | int socket_fd; |
| 113 | size_t bytes_written; |
| 114 | int cause_close_fd; |
| 115 | }; |
| 116 | |
| 117 | static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) { |
| 118 | asocket* s = create_local_socket(arg->socket_fd); |
| 119 | ASSERT_TRUE(s != nullptr); |
| 120 | arg->bytes_written = 0; |
| 121 | while (true) { |
| 122 | apacket* p = get_apacket(); |
| 123 | p->len = sizeof(p->data); |
| 124 | arg->bytes_written += p->len; |
| 125 | int ret = s->enqueue(s, p); |
| 126 | if (ret == 1) { |
| 127 | // The writer has one packet waiting to send. |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | asocket* cause_close_s = create_local_socket(arg->cause_close_fd); |
| 133 | ASSERT_TRUE(cause_close_s != nullptr); |
| 134 | cause_close_s->peer = s; |
| 135 | s->peer = cause_close_s; |
| 136 | cause_close_s->ready(cause_close_s); |
| 137 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 138 | fdevent_loop(); |
| 139 | } |
| 140 | |
| 141 | // This test checks if we can close local socket in the following situation: |
| 142 | // The socket is closing but having some packets, so it is not closed. Then |
| 143 | // some write error happens in the socket's file handler, e.g., the file |
| 144 | // handler is closed. |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 145 | TEST_F(LocalSocketTest, close_socket_with_packet) { |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 146 | int socket_fd[2]; |
| 147 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 148 | int cause_close_fd[2]; |
| 149 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 150 | CloseWithPacketArg arg; |
| 151 | arg.socket_fd = socket_fd[1]; |
| 152 | arg.cause_close_fd = cause_close_fd[1]; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 153 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 154 | PrepareThread(); |
| 155 | adb_thread_t thread; |
| 156 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), |
| 157 | &arg, &thread)); |
| 158 | // Wait until the fdevent_loop() starts. |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 159 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 160 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 161 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 162 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 163 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 164 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 165 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 166 | TerminateThread(thread); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 169 | // This test checks if we can read packets from a closing local socket. |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 170 | TEST_F(LocalSocketTest, read_from_closing_socket) { |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 171 | int socket_fd[2]; |
| 172 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 173 | int cause_close_fd[2]; |
| 174 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 175 | CloseWithPacketArg arg; |
| 176 | arg.socket_fd = socket_fd[1]; |
| 177 | arg.cause_close_fd = cause_close_fd[1]; |
| 178 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 179 | PrepareThread(); |
| 180 | adb_thread_t thread; |
| 181 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), |
| 182 | &arg, &thread)); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 183 | // Wait until the fdevent_loop() starts. |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 184 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 185 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 186 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 187 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 188 | |
| 189 | // Verify if we can read successfully. |
| 190 | std::vector<char> buf(arg.bytes_written); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 191 | ASSERT_NE(0u, arg.bytes_written); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 192 | ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); |
| 193 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| 194 | |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 195 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 196 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 197 | TerminateThread(thread); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // This test checks if we can close local socket in the following situation: |
| 201 | // The socket is not closed and has some packets. When it fails to write to |
| 202 | // the socket's file handler because the other end is closed, we check if the |
| 203 | // socket is closed. |
| 204 | TEST_F(LocalSocketTest, write_error_when_having_packets) { |
| 205 | int socket_fd[2]; |
| 206 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 207 | int cause_close_fd[2]; |
| 208 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 209 | CloseWithPacketArg arg; |
| 210 | arg.socket_fd = socket_fd[1]; |
| 211 | arg.cause_close_fd = cause_close_fd[1]; |
| 212 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 213 | PrepareThread(); |
| 214 | adb_thread_t thread; |
| 215 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), |
| 216 | &arg, &thread)); |
| 217 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 218 | // Wait until the fdevent_loop() starts. |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 219 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 220 | EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 221 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| 222 | |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 223 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 224 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 225 | TerminateThread(thread); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 228 | #if defined(__linux__) |
| 229 | |
| 230 | static void ClientThreadFunc() { |
| 231 | std::string error; |
| 232 | int fd = network_loopback_client(5038, SOCK_STREAM, &error); |
| 233 | ASSERT_GE(fd, 0) << error; |
Josh Gao | 70267e4 | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 234 | std::this_thread::sleep_for(200ms); |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 235 | ASSERT_EQ(0, adb_close(fd)); |
| 236 | } |
| 237 | |
| 238 | struct CloseRdHupSocketArg { |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 239 | int socket_fd; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 242 | static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) { |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 243 | asocket* s = create_local_socket(arg->socket_fd); |
| 244 | ASSERT_TRUE(s != nullptr); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 245 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 246 | fdevent_loop(); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 249 | // This test checks if we can close sockets in CLOSE_WAIT state. |
| 250 | TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 251 | std::string error; |
| 252 | int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error); |
| 253 | ASSERT_GE(listen_fd, 0); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 254 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 255 | adb_thread_t client_thread; |
| 256 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(ClientThreadFunc), nullptr, |
| 257 | &client_thread)); |
| 258 | |
Elliott Hughes | 97a035c | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 259 | int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 260 | ASSERT_GE(accept_fd, 0); |
| 261 | CloseRdHupSocketArg arg; |
| 262 | arg.socket_fd = accept_fd; |
| 263 | |
| 264 | PrepareThread(); |
| 265 | adb_thread_t thread; |
| 266 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseRdHupSocketThreadFunc), |
| 267 | &arg, &thread)); |
| 268 | |
| 269 | // Wait until the fdevent_loop() starts. |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 270 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 271 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 272 | |
| 273 | // Wait until the client closes its socket. |
| 274 | ASSERT_TRUE(adb_thread_join(client_thread)); |
| 275 | |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 276 | std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 277 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 278 | TerminateThread(thread); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 279 | } |
Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 280 | |
| 281 | #endif // defined(__linux__) |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 282 | |
| 283 | #if ADB_HOST |
| 284 | |
| 285 | // Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches |
| 286 | // |expected|, otherwise logs the failure to gtest. |
Dan Austin | ca35e9e | 2016-03-28 15:32:37 -0700 | [diff] [blame] | 287 | void VerifySkipHostSerial(std::string serial, const char* expected) { |
| 288 | char* result = internal::skip_host_serial(&serial[0]); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 289 | if (expected == nullptr) { |
| 290 | EXPECT_EQ(nullptr, result); |
| 291 | } else { |
| 292 | EXPECT_STREQ(expected, result); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Check [tcp:|udp:]<serial>[:<port>]:<command> format. |
| 297 | TEST(socket_test, test_skip_host_serial) { |
| 298 | for (const std::string& protocol : {"", "tcp:", "udp:"}) { |
| 299 | VerifySkipHostSerial(protocol, nullptr); |
| 300 | VerifySkipHostSerial(protocol + "foo", nullptr); |
| 301 | |
| 302 | VerifySkipHostSerial(protocol + "foo:bar", ":bar"); |
| 303 | VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz"); |
| 304 | |
| 305 | VerifySkipHostSerial(protocol + "foo:123:bar", ":bar"); |
| 306 | VerifySkipHostSerial(protocol + "foo:123:456", ":456"); |
| 307 | VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz"); |
| 308 | |
| 309 | // Don't register a port unless it's all numbers and ends with ':'. |
| 310 | VerifySkipHostSerial(protocol + "foo:123", ":123"); |
| 311 | VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz"); |
David Pursell | 24b62a7 | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 312 | |
| 313 | VerifySkipHostSerial(protocol + "100.100.100.100:5555:foo", ":foo"); |
| 314 | VerifySkipHostSerial(protocol + "[0123:4567:89ab:CDEF:0:9:a:f]:5555:foo", ":foo"); |
| 315 | VerifySkipHostSerial(protocol + "[::1]:5555:foo", ":foo"); |
| 316 | |
| 317 | // If we can't find both [] then treat it as a normal serial with [ in it. |
| 318 | VerifySkipHostSerial(protocol + "[0123:foo", ":foo"); |
| 319 | |
| 320 | // Don't be fooled by random IPv6 addresses in the command string. |
| 321 | VerifySkipHostSerial(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555", |
| 322 | ":ping [0123:4567:89ab:CDEF:0:9:a:f]:5555"); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | // Check <prefix>:<serial>:<command> format. |
| 327 | TEST(socket_test, test_skip_host_serial_prefix) { |
| 328 | for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) { |
| 329 | VerifySkipHostSerial(prefix, nullptr); |
| 330 | VerifySkipHostSerial(prefix + "foo", nullptr); |
| 331 | |
| 332 | VerifySkipHostSerial(prefix + "foo:bar", ":bar"); |
| 333 | VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz"); |
| 334 | VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar"); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | #endif // ADB_HOST |