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