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