| 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 |  | 
| Josh Gao | 57e09b1 | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 17 | #include "fdevent/fdevent.h" | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 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 | 57e09b1 | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 32 | #include "fdevent/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 |  | 
| Josh Gao | bd76720 | 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 | c1b1f6f | 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 | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 46 | class LocalSocketTest : public FdeventTest {}; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 47 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 48 | TEST_F(LocalSocketTest, smoke) { | 
| Josh Gao | 022d447 | 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 | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 56 | const std::string MESSAGE = "socket_test"; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 57 |  | 
| Josh Gao | 022d447 | 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 | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 61 | asocket* prev_tail = create_local_socket(unique_fd(first[1])); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 62 | ASSERT_NE(nullptr, prev_tail); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 63 |  | 
| Josh Gao | 022d447 | 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 | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 73 | asocket* head = create_local_socket(unique_fd(intermediate[0])); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 74 | ASSERT_NE(nullptr, head); | 
|  | 75 |  | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 76 | asocket* tail = create_local_socket(unique_fd(intermediate[1])); | 
| Josh Gao | 022d447 | 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 | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 83 | asocket* end = create_local_socket(unique_fd(last[0])); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 84 | ASSERT_NE(nullptr, end); | 
|  | 85 | connect(prev_tail, end); | 
|  | 86 |  | 
|  | 87 | PrepareThread(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 88 |  | 
| Yabin Cui | c1b1f6f | 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 | 022d447 | 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 | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 94 | ASSERT_EQ(read_buffer, write_buffer); | 
|  | 95 | } | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 96 |  | 
| Josh Gao | 022d447 | 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 | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 101 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 102 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 103 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
|  | 106 | struct CloseWithPacketArg { | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 107 | unique_fd socket_fd; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 108 | size_t bytes_written; | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 109 | unique_fd cause_close_fd; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 110 | }; | 
|  | 111 |  | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 112 | static void CreateCloser(CloseWithPacketArg* arg) { | 
|  | 113 | fdevent_run_on_main_thread([arg]() { | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 114 | asocket* s = create_local_socket(std::move(arg->socket_fd)); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 115 | ASSERT_TRUE(s != nullptr); | 
|  | 116 | arg->bytes_written = 0; | 
| Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 117 |  | 
| Josh Gao | 7651eb9 | 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 | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 124 | apacket::payload_type data; | 
| Josh Gao | 7651eb9 | 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)); | 
|  | 128 | if (ret == 1) { | 
|  | 129 | socket_filled = true; | 
|  | 130 | break; | 
|  | 131 | } | 
|  | 132 | ASSERT_NE(-1, ret); | 
|  | 133 |  | 
|  | 134 | std::this_thread::sleep_for(250ms); | 
|  | 135 | } | 
|  | 136 | ASSERT_TRUE(socket_filled); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 137 |  | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 138 | asocket* cause_close_s = create_local_socket(std::move(arg->cause_close_fd)); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 139 | ASSERT_TRUE(cause_close_s != nullptr); | 
|  | 140 | cause_close_s->peer = s; | 
|  | 141 | s->peer = cause_close_s; | 
|  | 142 | cause_close_s->ready(cause_close_s); | 
|  | 143 | }); | 
|  | 144 | WaitForFdeventLoop(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | // This test checks if we can close local socket in the following situation: | 
|  | 148 | // The socket is closing but having some packets, so it is not closed. Then | 
|  | 149 | // some write error happens in the socket's file handler, e.g., the file | 
|  | 150 | // handler is closed. | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 151 | TEST_F(LocalSocketTest, close_socket_with_packet) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 152 | int socket_fd[2]; | 
|  | 153 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 154 | int cause_close_fd[2]; | 
|  | 155 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 156 | CloseWithPacketArg arg; | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 157 | arg.socket_fd.reset(socket_fd[1]); | 
|  | 158 | arg.cause_close_fd.reset(cause_close_fd[1]); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 159 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 160 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 161 | CreateCloser(&arg); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 162 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 163 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 164 |  | 
|  | 165 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 166 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 167 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 170 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 171 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 174 | // 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] | 175 | TEST_F(LocalSocketTest, read_from_closing_socket) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 176 | int socket_fd[2]; | 
|  | 177 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 178 | int cause_close_fd[2]; | 
|  | 179 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 180 | CloseWithPacketArg arg; | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 181 | arg.socket_fd.reset(socket_fd[1]); | 
|  | 182 | arg.cause_close_fd.reset(cause_close_fd[1]); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 183 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 184 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 185 | CreateCloser(&arg); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | WaitForFdeventLoop(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 188 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 189 |  | 
|  | 190 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 191 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 192 |  | 
|  | 193 | // Verify if we can read successfully. | 
|  | 194 | std::vector<char> buf(arg.bytes_written); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 195 | ASSERT_NE(0u, arg.bytes_written); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 196 | ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); | 
|  | 197 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 198 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 199 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 200 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 201 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | // This test checks if we can close local socket in the following situation: | 
|  | 205 | // The socket is not closed and has some packets. When it fails to write to | 
|  | 206 | // the socket's file handler because the other end is closed, we check if the | 
|  | 207 | // socket is closed. | 
|  | 208 | TEST_F(LocalSocketTest, write_error_when_having_packets) { | 
|  | 209 | int socket_fd[2]; | 
|  | 210 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 211 | int cause_close_fd[2]; | 
|  | 212 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 213 | CloseWithPacketArg arg; | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 214 | arg.socket_fd.reset(socket_fd[1]); | 
|  | 215 | arg.cause_close_fd.reset(cause_close_fd[1]); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 216 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 217 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 218 | CreateCloser(&arg); | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 219 |  | 
|  | 220 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 221 | EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 222 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 223 |  | 
| Josh Gao | 74b7ec7 | 2019-01-11 14:42:08 -0800 | [diff] [blame] | 224 | std::this_thread::sleep_for(2s); | 
|  | 225 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 226 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 227 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 228 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 231 | // Ensure that if we fail to write output to an fd, we will still flush data coming from it. | 
|  | 232 | TEST_F(LocalSocketTest, flush_after_shutdown) { | 
|  | 233 | int head_fd[2]; | 
|  | 234 | int tail_fd[2]; | 
|  | 235 | ASSERT_EQ(0, adb_socketpair(head_fd)); | 
|  | 236 | ASSERT_EQ(0, adb_socketpair(tail_fd)); | 
|  | 237 |  | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 238 | asocket* head = create_local_socket(unique_fd(head_fd[1])); | 
|  | 239 | asocket* tail = create_local_socket(unique_fd(tail_fd[1])); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 240 |  | 
|  | 241 | head->peer = tail; | 
|  | 242 | head->ready(head); | 
|  | 243 |  | 
|  | 244 | tail->peer = head; | 
|  | 245 | tail->ready(tail); | 
|  | 246 |  | 
|  | 247 | PrepareThread(); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 248 |  | 
| Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3)); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 250 |  | 
| Josh Gao | ecb96ac | 2018-03-19 15:36:17 -0700 | [diff] [blame] | 251 | EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD)); | 
|  | 252 | const char* str = "write succeeds, but local_socket will fail to write"; | 
|  | 253 | EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str))); | 
|  | 254 | EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3)); | 
|  | 255 |  | 
|  | 256 | char buf[6]; | 
|  | 257 | EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6)); | 
|  | 258 | EXPECT_EQ(0, memcmp(buf, "foobar", 6)); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 259 |  | 
|  | 260 | adb_close(head_fd[0]); | 
|  | 261 | adb_close(tail_fd[0]); | 
|  | 262 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 263 | WaitForFdeventLoop(); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 264 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 265 | TerminateThread(); | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 266 | } | 
| Josh Gao | df3bae9 | 2018-03-15 15:28:55 -0700 | [diff] [blame] | 267 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 268 | #if defined(__linux__) | 
|  | 269 |  | 
|  | 270 | static void ClientThreadFunc() { | 
|  | 271 | std::string error; | 
|  | 272 | int fd = network_loopback_client(5038, SOCK_STREAM, &error); | 
|  | 273 | ASSERT_GE(fd, 0) << error; | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 274 | std::this_thread::sleep_for(1s); | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 275 | ASSERT_EQ(0, adb_close(fd)); | 
|  | 276 | } | 
|  | 277 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 278 | // This test checks if we can close sockets in CLOSE_WAIT state. | 
|  | 279 | TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 280 | std::string error; | 
|  | 281 | int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error); | 
|  | 282 | ASSERT_GE(listen_fd, 0); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 283 |  | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 284 | std::thread client_thread(ClientThreadFunc); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 285 |  | 
| Elliott Hughes | 3dcfa3f | 2016-08-23 12:50:00 -0700 | [diff] [blame] | 286 | int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 287 | ASSERT_GE(accept_fd, 0); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 288 |  | 
|  | 289 | PrepareThread(); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 290 |  | 
|  | 291 | fdevent_run_on_main_thread([accept_fd]() { | 
| Josh Gao | 74ccdf9 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 292 | asocket* s = create_local_socket(unique_fd(accept_fd)); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 293 | ASSERT_TRUE(s != nullptr); | 
|  | 294 | }); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 295 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 296 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 297 | EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 298 |  | 
|  | 299 | // Wait until the client closes its socket. | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 300 | client_thread.join(); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 301 |  | 
| Josh Gao | fd7486f | 2018-03-28 18:53:30 -0700 | [diff] [blame] | 302 | WaitForFdeventLoop(); | 
| Yabin Cui | 2407d7c | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 303 | ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); | 
| Josh Gao | 7ab5571 | 2018-03-29 16:27:13 -0700 | [diff] [blame] | 304 | TerminateThread(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 305 | } | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 306 |  | 
|  | 307 | #endif  // defined(__linux__) | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 308 |  | 
|  | 309 | #if ADB_HOST | 
|  | 310 |  | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 311 | #define VerifyParseHostServiceFailed(s)                                         \ | 
|  | 312 | do {                                                                        \ | 
|  | 313 | std::string service(s);                                                 \ | 
|  | 314 | std::string_view serial, command;                                       \ | 
|  | 315 | bool result = internal::parse_host_service(&serial, &command, service); \ | 
|  | 316 | EXPECT_FALSE(result);                                                   \ | 
|  | 317 | } while (0) | 
|  | 318 |  | 
|  | 319 | #define VerifyParseHostService(s, expected_serial, expected_command)            \ | 
|  | 320 | do {                                                                        \ | 
|  | 321 | std::string service(s);                                                 \ | 
|  | 322 | std::string_view serial, command;                                       \ | 
|  | 323 | bool result = internal::parse_host_service(&serial, &command, service); \ | 
|  | 324 | EXPECT_TRUE(result);                                                    \ | 
|  | 325 | EXPECT_EQ(std::string(expected_serial), std::string(serial));           \ | 
|  | 326 | EXPECT_EQ(std::string(expected_command), std::string(command));         \ | 
|  | 327 | } while (0); | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 328 |  | 
|  | 329 | // Check [tcp:|udp:]<serial>[:<port>]:<command> format. | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 330 | TEST(socket_test, test_parse_host_service) { | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 331 | for (const std::string& protocol : {"", "tcp:", "udp:"}) { | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 332 | VerifyParseHostServiceFailed(protocol); | 
|  | 333 | VerifyParseHostServiceFailed(protocol + "foo"); | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 334 |  | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 335 | { | 
|  | 336 | std::string serial = protocol + "foo"; | 
|  | 337 | VerifyParseHostService(serial + ":bar", serial, "bar"); | 
|  | 338 | VerifyParseHostService(serial + " :bar:baz", serial, "bar:baz"); | 
|  | 339 | } | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 340 |  | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 341 | { | 
|  | 342 | // With port. | 
|  | 343 | std::string serial = protocol + "foo:123"; | 
|  | 344 | VerifyParseHostService(serial + ":bar", serial, "bar"); | 
|  | 345 | VerifyParseHostService(serial + ":456", serial, "456"); | 
|  | 346 | VerifyParseHostService(serial + ":bar:baz", serial, "bar:baz"); | 
|  | 347 | } | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 348 |  | 
|  | 349 | // Don't register a port unless it's all numbers and ends with ':'. | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 350 | VerifyParseHostService(protocol + "foo:123", protocol + "foo", "123"); | 
|  | 351 | VerifyParseHostService(protocol + "foo:123bar:baz", protocol + "foo", "123bar:baz"); | 
| David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 352 |  | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 353 | std::string addresses[] = {"100.100.100.100", "[0123:4567:89ab:CDEF:0:9:a:f]", "[::1]"}; | 
|  | 354 | for (const std::string& address : addresses) { | 
|  | 355 | std::string serial = protocol + address; | 
|  | 356 | std::string serial_with_port = protocol + address + ":5555"; | 
|  | 357 | VerifyParseHostService(serial + ":foo", serial, "foo"); | 
|  | 358 | VerifyParseHostService(serial_with_port + ":foo", serial_with_port, "foo"); | 
|  | 359 | } | 
| David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 360 |  | 
|  | 361 | // If we can't find both [] then treat it as a normal serial with [ in it. | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 362 | VerifyParseHostService(protocol + "[0123:foo", protocol + "[0123", "foo"); | 
| David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 363 |  | 
|  | 364 | // Don't be fooled by random IPv6 addresses in the command string. | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 365 | VerifyParseHostService(protocol + "foo:ping [0123:4567:89ab:CDEF:0:9:a:f]:5555", | 
|  | 366 | protocol + "foo", "ping [0123:4567:89ab:CDEF:0:9:a:f]:5555"); | 
|  | 367 |  | 
|  | 368 | // Handle embedded NULs properly. | 
|  | 369 | VerifyParseHostService(protocol + "foo:echo foo\0bar"s, protocol + "foo", | 
|  | 370 | "echo foo\0bar"sv); | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 371 | } | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | // Check <prefix>:<serial>:<command> format. | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 375 | TEST(socket_test, test_parse_host_service_prefix) { | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 376 | for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) { | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 377 | VerifyParseHostServiceFailed(prefix); | 
|  | 378 | VerifyParseHostServiceFailed(prefix + "foo"); | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 379 |  | 
| Josh Gao | bd76720 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 380 | VerifyParseHostService(prefix + "foo:bar", prefix + "foo", "bar"); | 
|  | 381 | VerifyParseHostService(prefix + "foo:bar:baz", prefix + "foo", "bar:baz"); | 
|  | 382 | VerifyParseHostService(prefix + "foo:123:bar", prefix + "foo", "123:bar"); | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 383 | } | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | #endif  // ADB_HOST |