| 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 | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 47 | TEST_F(LocalSocketTest, smoke) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 48 | // Join two socketpairs with a chain of intermediate socketpairs. | 
|  | 49 | int first[2]; | 
|  | 50 | std::vector<std::array<int, 2>> intermediates; | 
|  | 51 | int last[2]; | 
|  | 52 |  | 
|  | 53 | constexpr size_t INTERMEDIATE_COUNT = 50; | 
|  | 54 | constexpr size_t MESSAGE_LOOP_COUNT = 100; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 55 | const std::string MESSAGE = "socket_test"; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 56 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 57 | intermediates.resize(INTERMEDIATE_COUNT); | 
|  | 58 | ASSERT_EQ(0, adb_socketpair(first)) << strerror(errno); | 
|  | 59 | ASSERT_EQ(0, adb_socketpair(last)) << strerror(errno); | 
|  | 60 | asocket* prev_tail = create_local_socket(first[1]); | 
|  | 61 | ASSERT_NE(nullptr, prev_tail); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 62 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 63 | auto connect = [](asocket* tail, asocket* head) { | 
|  | 64 | tail->peer = head; | 
|  | 65 | head->peer = tail; | 
|  | 66 | tail->ready(tail); | 
|  | 67 | }; | 
|  | 68 |  | 
|  | 69 | for (auto& intermediate : intermediates) { | 
|  | 70 | ASSERT_EQ(0, adb_socketpair(intermediate.data())) << strerror(errno); | 
|  | 71 |  | 
|  | 72 | asocket* head = create_local_socket(intermediate[0]); | 
|  | 73 | ASSERT_NE(nullptr, head); | 
|  | 74 |  | 
|  | 75 | asocket* tail = create_local_socket(intermediate[1]); | 
|  | 76 | ASSERT_NE(nullptr, tail); | 
|  | 77 |  | 
|  | 78 | connect(prev_tail, head); | 
|  | 79 | prev_tail = tail; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | asocket* end = create_local_socket(last[0]); | 
|  | 83 | ASSERT_NE(nullptr, end); | 
|  | 84 | connect(prev_tail, end); | 
|  | 85 |  | 
|  | 86 | PrepareThread(); | 
|  | 87 | adb_thread_t thread; | 
|  | 88 | ASSERT_TRUE(adb_thread_create(FdEventThreadFunc, nullptr, &thread)); | 
|  | 89 |  | 
| Yabin Cui | c1b1f6f | 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 | 022d447 | 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 | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 95 | ASSERT_EQ(read_buffer, write_buffer); | 
|  | 96 | } | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 97 |  | 
| Josh Gao | 022d447 | 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. | 
|  | 102 | adb_sleep_ms(100); | 
|  | 103 | TerminateThread(thread); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
|  | 106 | struct CloseWithPacketArg { | 
|  | 107 | int socket_fd; | 
|  | 108 | size_t bytes_written; | 
|  | 109 | int cause_close_fd; | 
|  | 110 | }; | 
|  | 111 |  | 
|  | 112 | static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) { | 
|  | 113 | asocket* s = create_local_socket(arg->socket_fd); | 
|  | 114 | ASSERT_TRUE(s != nullptr); | 
|  | 115 | arg->bytes_written = 0; | 
|  | 116 | while (true) { | 
|  | 117 | apacket* p = get_apacket(); | 
|  | 118 | p->len = sizeof(p->data); | 
|  | 119 | arg->bytes_written += p->len; | 
|  | 120 | int ret = s->enqueue(s, p); | 
|  | 121 | if (ret == 1) { | 
|  | 122 | // The writer has one packet waiting to send. | 
|  | 123 | break; | 
|  | 124 | } | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | asocket* cause_close_s = create_local_socket(arg->cause_close_fd); | 
|  | 128 | ASSERT_TRUE(cause_close_s != nullptr); | 
|  | 129 | cause_close_s->peer = s; | 
|  | 130 | s->peer = cause_close_s; | 
|  | 131 | cause_close_s->ready(cause_close_s); | 
|  | 132 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 133 | fdevent_loop(); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | // This test checks if we can close local socket in the following situation: | 
|  | 137 | // The socket is closing but having some packets, so it is not closed. Then | 
|  | 138 | // some write error happens in the socket's file handler, e.g., the file | 
|  | 139 | // handler is closed. | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 140 | TEST_F(LocalSocketTest, close_socket_with_packet) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 141 | int socket_fd[2]; | 
|  | 142 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 143 | int cause_close_fd[2]; | 
|  | 144 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 145 | CloseWithPacketArg arg; | 
|  | 146 | arg.socket_fd = socket_fd[1]; | 
|  | 147 | arg.cause_close_fd = cause_close_fd[1]; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 148 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 149 | PrepareThread(); | 
|  | 150 | adb_thread_t thread; | 
|  | 151 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), | 
|  | 152 | &arg, &thread)); | 
|  | 153 | // Wait until the fdevent_loop() starts. | 
|  | 154 | adb_sleep_ms(100); | 
|  | 155 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); | 
|  | 156 | adb_sleep_ms(100); | 
|  | 157 | EXPECT_EQ(2u, fdevent_installed_count()); | 
|  | 158 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 159 |  | 
|  | 160 | TerminateThread(thread); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 163 | // 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] | 164 | TEST_F(LocalSocketTest, read_from_closing_socket) { | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 165 | int socket_fd[2]; | 
|  | 166 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 167 | int cause_close_fd[2]; | 
|  | 168 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 169 | CloseWithPacketArg arg; | 
|  | 170 | arg.socket_fd = socket_fd[1]; | 
|  | 171 | arg.cause_close_fd = cause_close_fd[1]; | 
|  | 172 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 173 | PrepareThread(); | 
|  | 174 | adb_thread_t thread; | 
|  | 175 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), | 
|  | 176 | &arg, &thread)); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 177 | // Wait until the fdevent_loop() starts. | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 178 | adb_sleep_ms(100); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 179 | ASSERT_EQ(0, adb_close(cause_close_fd[0])); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 180 | adb_sleep_ms(100); | 
|  | 181 | EXPECT_EQ(2u, fdevent_installed_count()); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 182 |  | 
|  | 183 | // Verify if we can read successfully. | 
|  | 184 | std::vector<char> buf(arg.bytes_written); | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 185 | ASSERT_NE(0u, arg.bytes_written); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 186 | ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); | 
|  | 187 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 188 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 189 | TerminateThread(thread); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
|  | 192 | // This test checks if we can close local socket in the following situation: | 
|  | 193 | // The socket is not closed and has some packets. When it fails to write to | 
|  | 194 | // the socket's file handler because the other end is closed, we check if the | 
|  | 195 | // socket is closed. | 
|  | 196 | TEST_F(LocalSocketTest, write_error_when_having_packets) { | 
|  | 197 | int socket_fd[2]; | 
|  | 198 | ASSERT_EQ(0, adb_socketpair(socket_fd)); | 
|  | 199 | int cause_close_fd[2]; | 
|  | 200 | ASSERT_EQ(0, adb_socketpair(cause_close_fd)); | 
|  | 201 | CloseWithPacketArg arg; | 
|  | 202 | arg.socket_fd = socket_fd[1]; | 
|  | 203 | arg.cause_close_fd = cause_close_fd[1]; | 
|  | 204 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 205 | PrepareThread(); | 
|  | 206 | adb_thread_t thread; | 
|  | 207 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), | 
|  | 208 | &arg, &thread)); | 
|  | 209 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 210 | // Wait until the fdevent_loop() starts. | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 211 | adb_sleep_ms(100); | 
|  | 212 | EXPECT_EQ(3u, fdevent_installed_count()); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 213 | ASSERT_EQ(0, adb_close(socket_fd[0])); | 
|  | 214 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 215 | TerminateThread(thread); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 218 | #if defined(__linux__) | 
|  | 219 |  | 
|  | 220 | static void ClientThreadFunc() { | 
|  | 221 | std::string error; | 
|  | 222 | int fd = network_loopback_client(5038, SOCK_STREAM, &error); | 
|  | 223 | ASSERT_GE(fd, 0) << error; | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 224 | adb_sleep_ms(200); | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 225 | ASSERT_EQ(0, adb_close(fd)); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | struct CloseRdHupSocketArg { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 229 | int socket_fd; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 230 | }; | 
|  | 231 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 232 | static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 233 | asocket* s = create_local_socket(arg->socket_fd); | 
|  | 234 | ASSERT_TRUE(s != nullptr); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 235 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 236 | fdevent_loop(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 239 | // This test checks if we can close sockets in CLOSE_WAIT state. | 
|  | 240 | TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 241 | std::string error; | 
|  | 242 | int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error); | 
|  | 243 | ASSERT_GE(listen_fd, 0); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 244 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 245 | adb_thread_t client_thread; | 
|  | 246 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(ClientThreadFunc), nullptr, | 
|  | 247 | &client_thread)); | 
|  | 248 |  | 
|  | 249 | struct sockaddr addr; | 
|  | 250 | socklen_t alen; | 
|  | 251 | alen = sizeof(addr); | 
|  | 252 | int accept_fd = adb_socket_accept(listen_fd, &addr, &alen); | 
|  | 253 | ASSERT_GE(accept_fd, 0); | 
|  | 254 | CloseRdHupSocketArg arg; | 
|  | 255 | arg.socket_fd = accept_fd; | 
|  | 256 |  | 
|  | 257 | PrepareThread(); | 
|  | 258 | adb_thread_t thread; | 
|  | 259 | ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseRdHupSocketThreadFunc), | 
|  | 260 | &arg, &thread)); | 
|  | 261 |  | 
|  | 262 | // Wait until the fdevent_loop() starts. | 
|  | 263 | adb_sleep_ms(100); | 
|  | 264 | EXPECT_EQ(2u, fdevent_installed_count()); | 
|  | 265 |  | 
|  | 266 | // Wait until the client closes its socket. | 
|  | 267 | ASSERT_TRUE(adb_thread_join(client_thread)); | 
|  | 268 |  | 
|  | 269 | TerminateThread(thread); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 270 | } | 
| Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 271 |  | 
|  | 272 | #endif  // defined(__linux__) |