| 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 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 45 | TEST_F(LocalSocketTest, smoke) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 46 | // Join two socketpairs with a chain of intermediate socketpairs. | 
|  | 47 | int first[2]; | 
|  | 48 | std::vector<std::array<int, 2>> intermediates; | 
|  | 49 | int last[2]; | 
|  | 50 |  | 
|  | 51 | constexpr size_t INTERMEDIATE_COUNT = 50; | 
|  | 52 | constexpr size_t MESSAGE_LOOP_COUNT = 100; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 53 | const std::string MESSAGE = "socket_test"; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 54 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 55 | intermediates.resize(INTERMEDIATE_COUNT); | 
|  | 56 | ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno); | 
|  | 57 | ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno); | 
|  | 58 | asocket* prev_tail = create_local_socket(first[1]); | 
|  | 59 | ASSERT_NE(nullptr, prev_tail); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 60 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 61 | auto connect = [](asocket* tail, asocket* head) { | 
|  | 62 | tail->peer = head; | 
|  | 63 | head->peer = tail; | 
|  | 64 | tail->ready(tail); | 
|  | 65 | }; | 
|  | 66 |  | 
|  | 67 | for (auto& intermediate : intermediates) { | 
|  | 68 | ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno); | 
|  | 69 |  | 
|  | 70 | asocket* head = create_local_socket(intermediate[0]); | 
|  | 71 | ASSERT_NE(nullptr, head); | 
|  | 72 |  | 
|  | 73 | asocket* tail = create_local_socket(intermediate[1]); | 
|  | 74 | ASSERT_NE(nullptr, tail); | 
|  | 75 |  | 
|  | 76 | connect(prev_tail, head); | 
|  | 77 | prev_tail = tail; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | asocket* end = create_local_socket(last[0]); | 
|  | 81 | ASSERT_NE(nullptr, end); | 
|  | 82 | connect(prev_tail, end); | 
|  | 83 |  | 
|  | 84 | PrepareThread(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 85 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 86 | for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) { | 
|  | 87 | std::string read_buffer = MESSAGE; | 
|  | 88 | std::string write_buffer(MESSAGE.size(), 'a'); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 89 | ASSERT_TRUE(WriteFdExactly(first[0], &read_buffer[0], read_buffer.size())); | 
|  | 90 | ASSERT_TRUE(ReadFdExactly(last[1], &write_buffer[0], write_buffer.size())); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 91 | ASSERT_EQ(read_buffer, write_buffer); | 
|  | 92 | } | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 93 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 94 | ASSERT_EQ(0, adb_close(first[0])); | 
|  | 95 | ASSERT_EQ(0, adb_close(last[1])); | 
|  | 96 |  | 
|  | 97 | // Wait until the local sockets are closed. | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 98 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 99 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 100 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | struct CloseWithPacketArg { | 
|  | 104 | int socket_fd; | 
|  | 105 | size_t bytes_written; | 
|  | 106 | int cause_close_fd; | 
|  | 107 | }; | 
|  | 108 |  | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 109 | static void CreateCloser(CloseWithPacketArg* arg) { | 
|  | 110 | fdevent_run_on_main_thread([arg]() { | 
|  | 111 | asocket* s = create_local_socket(arg->socket_fd); | 
|  | 112 | ASSERT_TRUE(s != nullptr); | 
|  | 113 | arg->bytes_written = 0; | 
| Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 114 |  | 
| Josh Gao | 7651eb9 | 2018-03-30 14:05:40 -0700 | [diff] [blame] | 115 | // On platforms that implement sockets via underlying sockets (e.g. Wine), | 
|  | 116 | // a socket can appear to be full, and then become available for writes | 
|  | 117 | // again without read being called on the other end. Loop and sleep after | 
|  | 118 | // each write to give the underlying implementation time to flush. | 
|  | 119 | bool socket_filled = false; | 
|  | 120 | for (int i = 0; i < 128; ++i) { | 
| Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 121 | apacket::payload_type data; | 
| Josh Gao | 7651eb9 | 2018-03-30 14:05:40 -0700 | [diff] [blame] | 122 | data.resize(MAX_PAYLOAD); | 
|  | 123 | arg->bytes_written += data.size(); | 
|  | 124 | int ret = s->enqueue(s, std::move(data)); | 
|  | 125 | if (ret == 1) { | 
|  | 126 | socket_filled = true; | 
|  | 127 | break; | 
|  | 128 | } | 
|  | 129 | ASSERT_NE(-1, ret); | 
|  | 130 |  | 
|  | 131 | std::this_thread::sleep_for(250ms); | 
|  | 132 | } | 
|  | 133 | ASSERT_TRUE(socket_filled); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 134 |  | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 135 | asocket* cause_close_s = create_local_socket(arg->cause_close_fd); | 
|  | 136 | ASSERT_TRUE(cause_close_s != nullptr); | 
|  | 137 | cause_close_s->peer = s; | 
|  | 138 | s->peer = cause_close_s; | 
|  | 139 | cause_close_s->ready(cause_close_s); | 
|  | 140 | }); | 
|  | 141 | WaitForFdeventLoop(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
|  | 144 | // This test checks if we can close local socket in the following situation: | 
|  | 145 | // The socket is closing but having some packets, so it is not closed. Then | 
|  | 146 | // some write error happens in the socket's file handler, e.g., the file | 
|  | 147 | // handler is closed. | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 148 | TEST_F(LocalSocketTest, close_socket_with_packet) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 149 | int socket_fd[2]; | 
|  | 150 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 151 | int cause_close_fd[2]; | 
|  | 152 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 153 | CloseWithPacketArg arg; | 
|  | 154 | arg.socket_fd = socket_fd[1]; | 
|  | 155 | arg.cause_close_fd = cause_close_fd[1]; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 156 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 157 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 158 | CreateCloser(&arg); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 159 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 160 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 161 |  | 
|  | 162 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 163 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 164 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 165 |  | 
|  | 166 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 167 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 168 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 171 | // 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] | 172 | TEST_F(LocalSocketTest, read_from_closing_socket) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 173 | int socket_fd[2]; | 
|  | 174 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 175 | int cause_close_fd[2]; | 
|  | 176 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 177 | CloseWithPacketArg arg; | 
|  | 178 | arg.socket_fd = socket_fd[1]; | 
|  | 179 | arg.cause_close_fd = cause_close_fd[1]; | 
|  | 180 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 181 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 182 | CreateCloser(&arg); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 183 |  | 
|  | 184 | WaitForFdeventLoop(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 185 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 188 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 189 |  | 
|  | 190 | // Verify if we can read successfully. | 
|  | 191 | std::vector<char> buf(arg.bytes_written); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 192 | ASSERT_NE(0u, arg.bytes_written); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 193 | ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); | 
|  | 194 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 195 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 196 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 197 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 198 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
|  | 201 | // This test checks if we can close local socket in the following situation: | 
|  | 202 | // The socket is not closed and has some packets. When it fails to write to | 
|  | 203 | // the socket's file handler because the other end is closed, we check if the | 
|  | 204 | // socket is closed. | 
|  | 205 | TEST_F(LocalSocketTest, write_error_when_having_packets) { | 
|  | 206 | int socket_fd[2]; | 
|  | 207 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 208 | int cause_close_fd[2]; | 
|  | 209 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 210 | CloseWithPacketArg arg; | 
|  | 211 | arg.socket_fd = socket_fd[1]; | 
|  | 212 | arg.cause_close_fd = cause_close_fd[1]; | 
|  | 213 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 214 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 215 | CreateCloser(&arg); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 218 | EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 219 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 220 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 221 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 222 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 223 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 226 | // Ensure that if we fail to write output to an fd, we will still flush data coming from it. | 
|  | 227 | TEST_F(LocalSocketTest, flush_after_shutdown) { | 
|  | 228 | int head_fd[2]; | 
|  | 229 | int tail_fd[2]; | 
|  | 230 | ASSERT_EQ(0, adb_socketpair(head_fd)); | 
|  | 231 | ASSERT_EQ(0, adb_socketpair(tail_fd)); | 
|  | 232 |  | 
|  | 233 | asocket* head = create_local_socket(head_fd[1]); | 
|  | 234 | asocket* tail = create_local_socket(tail_fd[1]); | 
|  | 235 |  | 
|  | 236 | head->peer = tail; | 
|  | 237 | head->ready(head); | 
|  | 238 |  | 
|  | 239 | tail->peer = head; | 
|  | 240 | tail->ready(tail); | 
|  | 241 |  | 
|  | 242 | PrepareThread(); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 243 |  | 
| Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 244 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3)); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 245 |  | 
| Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 246 | EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD)); | 
|  | 247 | const char* str = "write succeeds, but local_socket will fail to write"; | 
|  | 248 | EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str))); | 
|  | 249 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3)); | 
|  | 250 |  | 
|  | 251 | char buf[6]; | 
|  | 252 | EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6)); | 
|  | 253 | EXPECT_EQ(0, memcmp(buf, "foobar", 6)); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | adb_close(head_fd[0]); | 
|  | 256 | adb_close(tail_fd[0]); | 
|  | 257 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 258 | WaitForFdeventLoop(); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 259 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 260 | TerminateThread(); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 261 | } | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 262 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 263 | #if defined(__linux__) | 
|  | 264 |  | 
|  | 265 | static void ClientThreadFunc() { | 
|  | 266 | std::string error; | 
|  | 267 | int fd = network_loopback_client(5038, SOCK_STREAM, &error); | 
|  | 268 | ASSERT_GE(fd, 0) << error; | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 269 | std::this_thread::sleep_for(1s); | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 270 | ASSERT_EQ(0, adb_close(fd)); | 
|  | 271 | } | 
|  | 272 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 273 | // This test checks if we can close sockets in CLOSE_WAIT state. | 
|  | 274 | TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 275 | std::string error; | 
|  | 276 | int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error); | 
|  | 277 | ASSERT_GE(listen_fd, 0); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 278 |  | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 279 | std::thread client_thread(ClientThreadFunc); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 280 |  | 
| Elliott Hughes | 3dcfa3f | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 281 | int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 282 | ASSERT_GE(accept_fd, 0); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 283 |  | 
|  | 284 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 285 |  | 
|  | 286 | fdevent_run_on_main_thread([accept_fd]() { | 
|  | 287 | asocket* s = create_local_socket(accept_fd); | 
|  | 288 | ASSERT_TRUE(s != nullptr); | 
|  | 289 | }); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 290 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 291 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 292 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 293 |  | 
|  | 294 | // Wait until the client closes its socket. | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 295 | client_thread.join(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 296 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 297 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 298 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 299 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 300 | } | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 301 |  | 
|  | 302 | #endif  // defined(__linux__) | 
| David Pursell | 3f902aa | 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 | b4cff49 | 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 | 3f902aa | 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 | 73d55aa | 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 | 3f902aa | 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 |