| 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 | |
| Josh Gao | b51193a | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 17 | #include "fdevent/fdevent.h" |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 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 | b51193a | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 32 | #include "fdevent/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 | |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 37 | using namespace std::string_literals; |
| 38 | using namespace std::string_view_literals; |
| 39 | |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 40 | struct ThreadArg { |
| 41 | int first_read_fd; |
| 42 | int last_write_fd; |
| 43 | size_t middle_pipe_count; |
| 44 | }; |
| 45 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 46 | class LocalSocketTest : public FdeventTest {}; |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 47 | |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 48 | TEST_F(LocalSocketTest, smoke) { |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 49 | // Join two socketpairs with a chain of intermediate socketpairs. |
| 50 | int first[2]; |
| 51 | std::vector<std::array<int, 2>> intermediates; |
| 52 | int last[2]; |
| 53 | |
| 54 | constexpr size_t INTERMEDIATE_COUNT = 50; |
| 55 | constexpr size_t MESSAGE_LOOP_COUNT = 100; |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 56 | const std::string MESSAGE = "socket_test"; |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 57 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 58 | intermediates.resize(INTERMEDIATE_COUNT); |
| 59 | ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno); |
| 60 | ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno); |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 61 | asocket* prev_tail = create_local_socket(unique_fd(first[1])); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 62 | ASSERT_NE(nullptr, prev_tail); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 63 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 64 | auto connect = [](asocket* tail, asocket* head) { |
| 65 | tail->peer = head; |
| 66 | head->peer = tail; |
| 67 | tail->ready(tail); |
| 68 | }; |
| 69 | |
| 70 | for (auto& intermediate : intermediates) { |
| 71 | ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno); |
| 72 | |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 73 | asocket* head = create_local_socket(unique_fd(intermediate[0])); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 74 | ASSERT_NE(nullptr, head); |
| 75 | |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 76 | asocket* tail = create_local_socket(unique_fd(intermediate[1])); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 77 | ASSERT_NE(nullptr, tail); |
| 78 | |
| 79 | connect(prev_tail, head); |
| 80 | prev_tail = tail; |
| 81 | } |
| 82 | |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 83 | asocket* end = create_local_socket(unique_fd(last[0])); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 84 | ASSERT_NE(nullptr, end); |
| 85 | connect(prev_tail, end); |
| 86 | |
| 87 | PrepareThread(); |
| 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. |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 101 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 102 | ASSERT_EQ(0u, fdevent_installed_count()); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 103 | TerminateThread(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | struct CloseWithPacketArg { |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 107 | unique_fd socket_fd; |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 108 | size_t bytes_written; |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 109 | unique_fd cause_close_fd; |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 112 | static void CreateCloser(CloseWithPacketArg* arg) { |
| 113 | fdevent_run_on_main_thread([arg]() { |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 114 | asocket* s = create_local_socket(std::move(arg->socket_fd)); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 115 | ASSERT_TRUE(s != nullptr); |
| 116 | arg->bytes_written = 0; |
| Josh Gao | d5e56ce | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 117 | |
| Josh Gao | 205a8f4 | 2018-03-30 14:05:40 -0700 | [diff] [blame] | 118 | // On platforms that implement sockets via underlying sockets (e.g. Wine), |
| 119 | // a socket can appear to be full, and then become available for writes |
| 120 | // again without read being called on the other end. Loop and sleep after |
| 121 | // each write to give the underlying implementation time to flush. |
| 122 | bool socket_filled = false; |
| 123 | for (int i = 0; i < 128; ++i) { |
| Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 124 | apacket::payload_type data; |
| Josh Gao | 205a8f4 | 2018-03-30 14:05:40 -0700 | [diff] [blame] | 125 | data.resize(MAX_PAYLOAD); |
| 126 | arg->bytes_written += data.size(); |
| 127 | int ret = s->enqueue(s, std::move(data)); |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 128 | |
| 129 | // Return value of 0 implies that more data can be accepted. |
| Josh Gao | 205a8f4 | 2018-03-30 14:05:40 -0700 | [diff] [blame] | 130 | if (ret == 1) { |
| 131 | socket_filled = true; |
| 132 | break; |
| 133 | } |
| 134 | ASSERT_NE(-1, ret); |
| 135 | |
| 136 | std::this_thread::sleep_for(250ms); |
| 137 | } |
| 138 | ASSERT_TRUE(socket_filled); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 139 | |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 140 | asocket* cause_close_s = create_local_socket(std::move(arg->cause_close_fd)); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 141 | ASSERT_TRUE(cause_close_s != nullptr); |
| 142 | cause_close_s->peer = s; |
| 143 | s->peer = cause_close_s; |
| 144 | cause_close_s->ready(cause_close_s); |
| 145 | }); |
| 146 | WaitForFdeventLoop(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // This test checks if we can close local socket in the following situation: |
| 150 | // The socket is closing but having some packets, so it is not closed. Then |
| 151 | // some write error happens in the socket's file handler, e.g., the file |
| 152 | // handler is closed. |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 153 | TEST_F(LocalSocketTest, close_socket_with_packet) { |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 154 | int socket_fd[2]; |
| 155 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 156 | int cause_close_fd[2]; |
| 157 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 158 | CloseWithPacketArg arg; |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 159 | arg.socket_fd.reset(socket_fd[1]); |
| 160 | arg.cause_close_fd.reset(cause_close_fd[1]); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 161 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 162 | PrepareThread(); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 163 | CreateCloser(&arg); |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 164 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 165 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 166 | |
| 167 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 168 | EXPECT_EQ(1u, fdevent_installed_count()); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 169 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 170 | |
| 171 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 172 | ASSERT_EQ(0u, fdevent_installed_count()); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 173 | TerminateThread(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 176 | // 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] | 177 | TEST_F(LocalSocketTest, read_from_closing_socket) { |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 178 | int socket_fd[2]; |
| 179 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 180 | int cause_close_fd[2]; |
| 181 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 182 | CloseWithPacketArg arg; |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 183 | arg.socket_fd.reset(socket_fd[1]); |
| 184 | arg.cause_close_fd.reset(cause_close_fd[1]); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 185 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 186 | PrepareThread(); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 187 | CreateCloser(&arg); |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 188 | |
| 189 | WaitForFdeventLoop(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 190 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 191 | |
| 192 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 193 | EXPECT_EQ(1u, fdevent_installed_count()); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 194 | |
| 195 | // Verify if we can read successfully. |
| 196 | std::vector<char> buf(arg.bytes_written); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 197 | ASSERT_NE(0u, arg.bytes_written); |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 198 | |
| 199 | ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); // TODO: b/237341044 |
| 200 | |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 201 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| 202 | |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 203 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 204 | ASSERT_EQ(0u, fdevent_installed_count()); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 205 | TerminateThread(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // This test checks if we can close local socket in the following situation: |
| 209 | // The socket is not closed and has some packets. When it fails to write to |
| 210 | // the socket's file handler because the other end is closed, we check if the |
| 211 | // socket is closed. |
| 212 | TEST_F(LocalSocketTest, write_error_when_having_packets) { |
| 213 | int socket_fd[2]; |
| 214 | ASSERT_EQ(0, adb_socketpair(socket_fd)); |
| 215 | int cause_close_fd[2]; |
| 216 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); |
| 217 | CloseWithPacketArg arg; |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 218 | arg.socket_fd.reset(socket_fd[1]); |
| 219 | arg.cause_close_fd.reset(cause_close_fd[1]); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 220 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 221 | PrepareThread(); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 222 | CreateCloser(&arg); |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 223 | |
| 224 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 225 | EXPECT_EQ(2u, fdevent_installed_count()); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 226 | ASSERT_EQ(0, adb_close(socket_fd[0])); |
| 227 | |
| Josh Gao | 18f7a5c | 2019-01-11 14:42:08 -0800 | [diff] [blame] | 228 | std::this_thread::sleep_for(2s); |
| 229 | |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 230 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 231 | ASSERT_EQ(0u, fdevent_installed_count()); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 232 | TerminateThread(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 235 | // Ensure that if we fail to write output to an fd, we will still flush data coming from it. |
| 236 | TEST_F(LocalSocketTest, flush_after_shutdown) { |
| 237 | int head_fd[2]; |
| 238 | int tail_fd[2]; |
| 239 | ASSERT_EQ(0, adb_socketpair(head_fd)); |
| 240 | ASSERT_EQ(0, adb_socketpair(tail_fd)); |
| 241 | |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 242 | asocket* head = create_local_socket(unique_fd(head_fd[1])); |
| 243 | asocket* tail = create_local_socket(unique_fd(tail_fd[1])); |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 244 | |
| 245 | head->peer = tail; |
| 246 | head->ready(head); |
| 247 | |
| 248 | tail->peer = head; |
| 249 | tail->ready(tail); |
| 250 | |
| 251 | PrepareThread(); |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 252 | |
| Josh Gao | d5e56ce | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 253 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3)); |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 254 | |
| Josh Gao | d5e56ce | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 255 | EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD)); |
| 256 | const char* str = "write succeeds, but local_socket will fail to write"; |
| 257 | EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str))); |
| 258 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3)); |
| 259 | |
| 260 | char buf[6]; |
| 261 | EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6)); |
| 262 | EXPECT_EQ(0, memcmp(buf, "foobar", 6)); |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 263 | |
| 264 | adb_close(head_fd[0]); |
| 265 | adb_close(tail_fd[0]); |
| 266 | |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 267 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 268 | ASSERT_EQ(0u, fdevent_installed_count()); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 269 | TerminateThread(); |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 270 | } |
| Josh Gao | 8020602 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 271 | |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 272 | #if defined(__linux__) |
| 273 | |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 274 | static void ClientThreadFunc(const int assigned_port) { |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 275 | std::string error; |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 276 | const int fd = network_loopback_client(assigned_port, SOCK_STREAM, &error); |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 277 | ASSERT_GE(fd, 0) << error; |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 278 | std::this_thread::sleep_for(1s); |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 279 | ASSERT_EQ(0, adb_close(fd)); |
| 280 | } |
| 281 | |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 282 | // This test checks if we can close sockets in CLOSE_WAIT state. |
| 283 | TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 284 | std::string error; |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 285 | // Allow the system to allocate an available port. |
| 286 | const int listen_fd = network_inaddr_any_server(0, SOCK_STREAM, &error); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 287 | ASSERT_GE(listen_fd, 0); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 288 | |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 289 | sockaddr_storage ss; |
| 290 | socklen_t ss_size = sizeof(ss); |
| 291 | ASSERT_EQ(0, adb_getsockname(listen_fd, reinterpret_cast<sockaddr*>(&ss), &ss_size)); |
| 292 | int assigned_port; |
| 293 | if (ss.ss_family == AF_INET) { |
| 294 | assigned_port = ntohs(reinterpret_cast<sockaddr_in*>(&ss)->sin_port); |
| 295 | } else { |
| 296 | assigned_port = ntohs(reinterpret_cast<sockaddr_in6*>(&ss)->sin6_port); |
| 297 | } |
| 298 | ASSERT_GE(assigned_port, 0); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 299 | |
| Shaju Mathew | 7a0735c | 2022-09-06 17:11:21 +0000 | [diff] [blame] | 300 | std::thread client_thread(ClientThreadFunc, assigned_port); |
| 301 | const int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr); |
| 302 | |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 303 | ASSERT_GE(accept_fd, 0); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 304 | |
| 305 | PrepareThread(); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 306 | |
| 307 | fdevent_run_on_main_thread([accept_fd]() { |
| Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 308 | asocket* s = create_local_socket(unique_fd(accept_fd)); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 309 | ASSERT_TRUE(s != nullptr); |
| 310 | }); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 311 | |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 312 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 313 | EXPECT_EQ(1u, fdevent_installed_count()); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 314 | |
| 315 | // Wait until the client closes its socket. |
| Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 316 | client_thread.join(); |
| Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 317 | |
| Josh Gao | cd60820 | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 318 | WaitForFdeventLoop(); |
| Fabien Sanglard | 3bff330 | 2022-08-24 16:38:07 -0700 | [diff] [blame^] | 319 | ASSERT_EQ(0u, fdevent_installed_count()); |
| Josh Gao | c7bdb6d | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 320 | TerminateThread(); |
| Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 321 | } |
| Yabin Cui | ec2e7d8 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 322 | |
| 323 | #endif // defined(__linux__) |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 324 | |
| 325 | #if ADB_HOST |
| 326 | |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 327 | #define VerifyParseHostServiceFailed(s) \ |
| 328 | do { \ |
| 329 | std::string service(s); \ |
| 330 | std::string_view serial, command; \ |
| 331 | bool result = internal::parse_host_service(&serial, &command, service); \ |
| 332 | EXPECT_FALSE(result); \ |
| 333 | } while (0) |
| 334 | |
| 335 | #define VerifyParseHostService(s, expected_serial, expected_command) \ |
| 336 | do { \ |
| 337 | std::string service(s); \ |
| 338 | std::string_view serial, command; \ |
| 339 | bool result = internal::parse_host_service(&serial, &command, service); \ |
| 340 | EXPECT_TRUE(result); \ |
| 341 | EXPECT_EQ(std::string(expected_serial), std::string(serial)); \ |
| 342 | EXPECT_EQ(std::string(expected_command), std::string(command)); \ |
| 343 | } while (0); |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 344 | |
| 345 | // Check [tcp:|udp:]<serial>[:<port>]:<command> format. |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 346 | TEST(socket_test, test_parse_host_service) { |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 347 | for (const std::string& protocol : {"", "tcp:", "udp:"}) { |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 348 | VerifyParseHostServiceFailed(protocol); |
| 349 | VerifyParseHostServiceFailed(protocol + "foo"); |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 350 | |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 351 | { |
| 352 | std::string serial = protocol + "foo"; |
| 353 | VerifyParseHostService(serial + ":bar", serial, "bar"); |
| 354 | VerifyParseHostService(serial + " :bar:baz", serial, "bar:baz"); |
| 355 | } |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 356 | |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 357 | { |
| 358 | // With port. |
| 359 | std::string serial = protocol + "foo:123"; |
| 360 | VerifyParseHostService(serial + ":bar", serial, "bar"); |
| 361 | VerifyParseHostService(serial + ":456", serial, "456"); |
| 362 | VerifyParseHostService(serial + ":bar:baz", serial, "bar:baz"); |
| 363 | } |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 364 | |
| 365 | // Don't register a port unless it's all numbers and ends with ':'. |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 366 | VerifyParseHostService(protocol + "foo:123", protocol + "foo", "123"); |
| 367 | VerifyParseHostService(protocol + "foo:123bar:baz", protocol + "foo", "123bar:baz"); |
| David Pursell | 24b62a7 | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 368 | |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 369 | std::string addresses[] = {"100.100.100.100", "[0123:4567:89ab:CDEF:0:9:a:f]", "[::1]"}; |
| 370 | for (const std::string& address : addresses) { |
| 371 | std::string serial = protocol + address; |
| 372 | std::string serial_with_port = protocol + address + ":5555"; |
| 373 | VerifyParseHostService(serial + ":foo", serial, "foo"); |
| 374 | VerifyParseHostService(serial_with_port + ":foo", serial_with_port, "foo"); |
| 375 | } |
| David Pursell | 24b62a7 | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 376 | |
| 377 | // If we can't find both [] then treat it as a normal serial with [ in it. |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 378 | VerifyParseHostService(protocol + "[0123:foo", protocol + "[0123", "foo"); |
| David Pursell | 24b62a7 | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 379 | |
| 380 | // Don't be fooled by random IPv6 addresses in the command string. |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 381 | VerifyParseHostService(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555", |
| 382 | protocol + "foo", "ping [0123:4567:89ab:CDEF:0:9:a:f]:5555"); |
| 383 | |
| 384 | // Handle embedded NULs properly. |
| 385 | VerifyParseHostService(protocol + "foo:echo foo\0bar"s, protocol + "foo", |
| 386 | "echo foo\0bar"sv); |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | // Check <prefix>:<serial>:<command> format. |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 391 | TEST(socket_test, test_parse_host_service_prefix) { |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 392 | for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) { |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 393 | VerifyParseHostServiceFailed(prefix); |
| 394 | VerifyParseHostServiceFailed(prefix + "foo"); |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 395 | |
| Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 396 | VerifyParseHostService(prefix + "foo:bar", prefix + "foo", "bar"); |
| 397 | VerifyParseHostService(prefix + "foo:bar:baz", prefix + "foo", "bar:baz"); |
| 398 | VerifyParseHostService(prefix + "foo:123:bar", prefix + "foo", "123:bar"); |
| David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | #endif // ADB_HOST |