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