Yabin Cui | c1b1f6f | 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 | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 21 | #include <array> |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 22 | #include <limits> |
| 23 | #include <queue> |
| 24 | #include <string> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 25 | #include <thread> |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Yabin Cui | c1b1f6f | 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 | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 32 | #include "fdevent_test.h" |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 33 | #include "socket.h" |
| 34 | #include "sysdeps.h" |
Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 35 | #include "sysdeps/chrono.h" |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 36 | |
Yabin Cui | c1b1f6f | 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 | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 43 | class LocalSocketTest : public FdeventTest {}; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 44 | |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 45 | static void WaitForFdeventLoop() { |
| 46 | std::this_thread::sleep_for(100ms); |
| 47 | } |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 48 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 49 | TEST_F(LocalSocketTest, smoke) { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 50 | // 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 Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 57 | const std::string MESSAGE = "socket_test"; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 58 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 59 | 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 Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 64 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 65 | 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(); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 89 | std::thread thread(fdevent_loop); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 90 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 91 | for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) { |
| 92 | std::string read_buffer = MESSAGE; |
| 93 | std::string write_buffer(MESSAGE.size(), 'a'); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 94 | ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size())); |
| 95 | ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size())); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 96 | ASSERT_EQ(read_buffer, write_buffer); |
| 97 | } |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 98 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 99 | ASSERT_EQ(0, adb_close(first[0])); |
| 100 | ASSERT_EQ(0, adb_close(last[1])); |
| 101 | |
| 102 | // Wait until the local sockets are closed. |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 103 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 104 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 105 | TerminateThread(thread); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | struct CloseWithPacketArg { |
| 109 | int socket_fd; |
| 110 | size_t bytes_written; |
| 111 | int cause_close_fd; |
| 112 | }; |
| 113 | |
| 114 | static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) { |
| 115 | asocket* s = create_local_socket(arg->socket_fd); |
| 116 | ASSERT_TRUE(s != nullptr); |
| 117 | arg->bytes_written = 0; |
Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 118 | |
| 119 | std::string data; |
| 120 | data.resize(MAX_PAYLOAD); |
| 121 | arg->bytes_written += data.size(); |
| 122 | int ret = s->enqueue(s, std::move(data)); |
| 123 | ASSERT_EQ(1, ret); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 124 | |
| 125 | asocket* cause_close_s = create_local_socket(arg->cause_close_fd); |
| 126 | ASSERT_TRUE(cause_close_s != nullptr); |
| 127 | cause_close_s->peer = s; |
| 128 | s->peer = cause_close_s; |
| 129 | cause_close_s->ready(cause_close_s); |
| 130 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 131 | fdevent_loop(); |
| 132 | } |
| 133 | |
| 134 | // This test checks if we can close local socket in the following situation: |
| 135 | // The socket is closing but having some packets, so it is not closed. Then |
| 136 | // some write error happens in the socket's file handler, e.g., the file |
| 137 | // handler is closed. |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 138 | TEST_F(LocalSocketTest, close_socket_with_packet) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 139 | int socket_fd[2]; |
| 140 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 141 | int cause_close_fd[2]; |
| 142 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 143 | CloseWithPacketArg arg; |
| 144 | arg.socket_fd = socket_fd[1]; |
| 145 | arg.cause_close_fd = cause_close_fd[1]; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 146 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 147 | PrepareThread(); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 148 | std::thread thread(CloseWithPacketThreadFunc, &arg); |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 149 | |
| 150 | WaitForFdeventLoop(); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 151 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 152 | |
| 153 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 154 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 155 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 156 | |
| 157 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 158 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 159 | TerminateThread(thread); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 162 | // This test checks if we can read packets from a closing local socket. |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 163 | TEST_F(LocalSocketTest, read_from_closing_socket) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 164 | int socket_fd[2]; |
| 165 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 166 | int cause_close_fd[2]; |
| 167 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 168 | CloseWithPacketArg arg; |
| 169 | arg.socket_fd = socket_fd[1]; |
| 170 | arg.cause_close_fd = cause_close_fd[1]; |
| 171 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 172 | PrepareThread(); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 173 | std::thread thread(CloseWithPacketThreadFunc, &arg); |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 174 | |
| 175 | WaitForFdeventLoop(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 176 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 177 | |
| 178 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 179 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 180 | |
| 181 | // Verify if we can read successfully. |
| 182 | std::vector<char> buf(arg.bytes_written); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 183 | ASSERT_NE(0u, arg.bytes_written); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 184 | ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); |
| 185 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| 186 | |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 187 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 188 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 189 | TerminateThread(thread); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // This test checks if we can close local socket in the following situation: |
| 193 | // The socket is not closed and has some packets. When it fails to write to |
| 194 | // the socket's file handler because the other end is closed, we check if the |
| 195 | // socket is closed. |
| 196 | TEST_F(LocalSocketTest, write_error_when_having_packets) { |
| 197 | int socket_fd[2]; |
| 198 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 199 | int cause_close_fd[2]; |
| 200 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 201 | CloseWithPacketArg arg; |
| 202 | arg.socket_fd = socket_fd[1]; |
| 203 | arg.cause_close_fd = cause_close_fd[1]; |
| 204 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 205 | PrepareThread(); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 206 | std::thread thread(CloseWithPacketThreadFunc, &arg); |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 207 | |
| 208 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 209 | EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 210 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| 211 | |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 212 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 213 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 214 | TerminateThread(thread); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 217 | // Ensure that if we fail to write output to an fd, we will still flush data coming from it. |
| 218 | TEST_F(LocalSocketTest, flush_after_shutdown) { |
| 219 | int head_fd[2]; |
| 220 | int tail_fd[2]; |
| 221 | ASSERT_EQ(0, adb_socketpair(head_fd)); |
| 222 | ASSERT_EQ(0, adb_socketpair(tail_fd)); |
| 223 | |
| 224 | asocket* head = create_local_socket(head_fd[1]); |
| 225 | asocket* tail = create_local_socket(tail_fd[1]); |
| 226 | |
| 227 | head->peer = tail; |
| 228 | head->ready(head); |
| 229 | |
| 230 | tail->peer = head; |
| 231 | tail->ready(tail); |
| 232 | |
| 233 | PrepareThread(); |
| 234 | std::thread thread(fdevent_loop); |
| 235 | |
Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 236 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3)); |
Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 237 | |
Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 238 | EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD)); |
| 239 | const char* str = "write succeeds, but local_socket will fail to write"; |
| 240 | EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str))); |
| 241 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3)); |
| 242 | |
| 243 | char buf[6]; |
| 244 | EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6)); |
| 245 | EXPECT_EQ(0, memcmp(buf, "foobar", 6)); |
Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 246 | |
| 247 | adb_close(head_fd[0]); |
| 248 | adb_close(tail_fd[0]); |
| 249 | |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 250 | WaitForFdeventLoop(); |
Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 251 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
| 252 | TerminateThread(thread); |
| 253 | } |
Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 254 | |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 255 | #if defined(__linux__) |
| 256 | |
| 257 | static void ClientThreadFunc() { |
| 258 | std::string error; |
| 259 | int fd = network_loopback_client(5038, SOCK_STREAM, &error); |
| 260 | ASSERT_GE(fd, 0) << error; |
Josh Gao | 4602adb | 2016-11-15 18:55:47 -0800 | [diff] [blame] | 261 | std::this_thread::sleep_for(200ms); |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 262 | ASSERT_EQ(0, adb_close(fd)); |
| 263 | } |
| 264 | |
| 265 | struct CloseRdHupSocketArg { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 266 | int socket_fd; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 267 | }; |
| 268 | |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 269 | static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 270 | asocket* s = create_local_socket(arg->socket_fd); |
| 271 | ASSERT_TRUE(s != nullptr); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 272 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 273 | fdevent_loop(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 276 | // This test checks if we can close sockets in CLOSE_WAIT state. |
| 277 | TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 278 | std::string error; |
| 279 | int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error); |
| 280 | ASSERT_GE(listen_fd, 0); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 281 | |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 282 | std::thread client_thread(ClientThreadFunc); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 283 | |
Elliott Hughes | 3dcfa3f | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 284 | int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 285 | ASSERT_GE(accept_fd, 0); |
| 286 | CloseRdHupSocketArg arg; |
| 287 | arg.socket_fd = accept_fd; |
| 288 | |
| 289 | PrepareThread(); |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 290 | std::thread thread(CloseRdHupSocketThreadFunc, &arg); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 291 | |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 292 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 293 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 294 | |
| 295 | // Wait until the client closes its socket. |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 296 | client_thread.join(); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 297 | |
Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 298 | WaitForFdeventLoop(); |
Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 299 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 300 | TerminateThread(thread); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 301 | } |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 302 | |
| 303 | #endif // defined(__linux__) |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 304 | |
| 305 | #if ADB_HOST |
| 306 | |
| 307 | // Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches |
| 308 | // |expected|, otherwise logs the failure to gtest. |
Dan Austin | b4cff49 | 2016-03-28 15:32:37 -0700 | [diff] [blame] | 309 | void VerifySkipHostSerial(std::string serial, const char* expected) { |
| 310 | char* result = internal::skip_host_serial(&serial[0]); |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 311 | if (expected == nullptr) { |
| 312 | EXPECT_EQ(nullptr, result); |
| 313 | } else { |
| 314 | EXPECT_STREQ(expected, result); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Check [tcp:|udp:]<serial>[:<port>]:<command> format. |
| 319 | TEST(socket_test, test_skip_host_serial) { |
| 320 | for (const std::string& protocol : {"", "tcp:", "udp:"}) { |
| 321 | VerifySkipHostSerial(protocol, nullptr); |
| 322 | VerifySkipHostSerial(protocol + "foo", nullptr); |
| 323 | |
| 324 | VerifySkipHostSerial(protocol + "foo:bar", ":bar"); |
| 325 | VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz"); |
| 326 | |
| 327 | VerifySkipHostSerial(protocol + "foo:123:bar", ":bar"); |
| 328 | VerifySkipHostSerial(protocol + "foo:123:456", ":456"); |
| 329 | VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz"); |
| 330 | |
| 331 | // Don't register a port unless it's all numbers and ends with ':'. |
| 332 | VerifySkipHostSerial(protocol + "foo:123", ":123"); |
| 333 | VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz"); |
David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 334 | |
| 335 | VerifySkipHostSerial(protocol + "100.100.100.100:5555:foo", ":foo"); |
| 336 | VerifySkipHostSerial(protocol + "[0123:4567:89ab:CDEF:0:9:a:f]:5555:foo", ":foo"); |
| 337 | VerifySkipHostSerial(protocol + "[::1]:5555:foo", ":foo"); |
| 338 | |
| 339 | // If we can't find both [] then treat it as a normal serial with [ in it. |
| 340 | VerifySkipHostSerial(protocol + "[0123:foo", ":foo"); |
| 341 | |
| 342 | // Don't be fooled by random IPv6 addresses in the command string. |
| 343 | VerifySkipHostSerial(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555", |
| 344 | ":ping [0123:4567:89ab:CDEF:0:9:a:f]:5555"); |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | // Check <prefix>:<serial>:<command> format. |
| 349 | TEST(socket_test, test_skip_host_serial_prefix) { |
| 350 | for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) { |
| 351 | VerifySkipHostSerial(prefix, nullptr); |
| 352 | VerifySkipHostSerial(prefix + "foo", nullptr); |
| 353 | |
| 354 | VerifySkipHostSerial(prefix + "foo:bar", ":bar"); |
| 355 | VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz"); |
| 356 | VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar"); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | #endif // ADB_HOST |