David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [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 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 17 | // Tests socket functionality using loopback connections. The UDP tests assume that no packets are |
| 18 | // lost, which should be the case for loopback communication, but is not guaranteed. |
| 19 | // |
| 20 | // Also tests our SocketMock class to make sure it works as expected and reports errors properly |
| 21 | // if the mock expectations aren't met during a test. |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 22 | |
| 23 | #include "socket.h" |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 24 | #include "socket_mock.h" |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 25 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame^] | 26 | #include <list> |
| 27 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 28 | #include <gtest/gtest-spi.h> |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame^] | 29 | #include <gtest/gtest.h> |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 30 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 31 | enum { kTestTimeoutMs = 3000 }; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 32 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 33 | // Creates connected sockets |server| and |client|. Returns true on success. |
| 34 | bool MakeConnectedSockets(Socket::Protocol protocol, std::unique_ptr<Socket>* server, |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 35 | std::unique_ptr<Socket>* client, |
| 36 | const std::string hostname = "localhost") { |
| 37 | *server = Socket::NewServer(protocol, 0); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 38 | if (*server == nullptr) { |
| 39 | ADD_FAILURE() << "Failed to create server."; |
| 40 | return false; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 41 | } |
| 42 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 43 | *client = Socket::NewClient(protocol, hostname, (*server)->GetLocalPort(), nullptr); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 44 | if (*client == nullptr) { |
| 45 | ADD_FAILURE() << "Failed to create client."; |
| 46 | return false; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 47 | } |
| 48 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 49 | // TCP passes the client off to a new socket. |
| 50 | if (protocol == Socket::Protocol::kTcp) { |
| 51 | *server = (*server)->Accept(); |
| 52 | if (*server == nullptr) { |
| 53 | ADD_FAILURE() << "Failed to accept client connection."; |
| 54 | return false; |
| 55 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 56 | } |
| 57 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 58 | return true; |
| 59 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 60 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 61 | // Sends a string over a Socket. Returns true if the full string (without terminating char) |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 62 | // was sent. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 63 | static bool SendString(Socket* sock, const std::string& message) { |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame^] | 64 | return sock->Send(message.c_str(), message.length()); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 65 | } |
| 66 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 67 | // Receives a string from a Socket. Returns true if the full string (without terminating char) |
| 68 | // was received. |
| 69 | static bool ReceiveString(Socket* sock, const std::string& message) { |
| 70 | std::string received(message.length(), '\0'); |
| 71 | ssize_t bytes = sock->ReceiveAll(&received[0], received.length(), kTestTimeoutMs); |
| 72 | return static_cast<size_t>(bytes) == received.length() && received == message; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Tests sending packets client -> server, then server -> client. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 76 | TEST(SocketTest, TestSendAndReceive) { |
| 77 | std::unique_ptr<Socket> server, client; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 78 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 79 | for (Socket::Protocol protocol : {Socket::Protocol::kUdp, Socket::Protocol::kTcp}) { |
| 80 | ASSERT_TRUE(MakeConnectedSockets(protocol, &server, &client)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 81 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 82 | EXPECT_TRUE(SendString(client.get(), "foo")); |
| 83 | EXPECT_TRUE(ReceiveString(server.get(), "foo")); |
| 84 | |
| 85 | EXPECT_TRUE(SendString(server.get(), "bar baz")); |
| 86 | EXPECT_TRUE(ReceiveString(client.get(), "bar baz")); |
| 87 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Tests sending and receiving large packets. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 91 | TEST(SocketTest, TestLargePackets) { |
| 92 | std::string message(1024, '\0'); |
| 93 | std::unique_ptr<Socket> server, client; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 94 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 95 | for (Socket::Protocol protocol : {Socket::Protocol::kUdp, Socket::Protocol::kTcp}) { |
| 96 | ASSERT_TRUE(MakeConnectedSockets(protocol, &server, &client)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 97 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 98 | // Run through the test a few times. |
| 99 | for (int i = 0; i < 10; ++i) { |
| 100 | // Use a different message each iteration to prevent false positives. |
| 101 | for (size_t j = 0; j < message.length(); ++j) { |
| 102 | message[j] = static_cast<char>(i + j); |
| 103 | } |
| 104 | |
| 105 | EXPECT_TRUE(SendString(client.get(), message)); |
| 106 | EXPECT_TRUE(ReceiveString(server.get(), message)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 107 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 111 | // Tests UDP receive overflow when the UDP packet is larger than the receive buffer. |
| 112 | TEST(SocketTest, TestUdpReceiveOverflow) { |
| 113 | std::unique_ptr<Socket> server, client; |
| 114 | ASSERT_TRUE(MakeConnectedSockets(Socket::Protocol::kUdp, &server, &client)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 115 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 116 | EXPECT_TRUE(SendString(client.get(), "1234567890")); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 117 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 118 | // This behaves differently on different systems, either truncating the packet or returning -1. |
| 119 | char buffer[5]; |
| 120 | ssize_t bytes = server->Receive(buffer, 5, kTestTimeoutMs); |
| 121 | if (bytes == 5) { |
| 122 | EXPECT_EQ(0, memcmp(buffer, "12345", 5)); |
| 123 | } else { |
| 124 | EXPECT_EQ(-1, bytes); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 127 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame^] | 128 | // Tests UDP multi-buffer send. |
| 129 | TEST(SocketTest, TestUdpSendBuffers) { |
| 130 | std::unique_ptr<Socket> sock = Socket::NewServer(Socket::Protocol::kUdp, 0); |
| 131 | std::vector<std::string> data{"foo", "bar", "12345"}; |
| 132 | std::vector<cutils_socket_buffer_t> buffers{{data[0].data(), data[0].length()}, |
| 133 | {data[1].data(), data[1].length()}, |
| 134 | {data[2].data(), data[2].length()}}; |
| 135 | ssize_t mock_return_value = 0; |
| 136 | |
| 137 | // Mock out socket_send_buffers() to verify we're sending in the correct buffers and |
| 138 | // return |mock_return_value|. |
| 139 | sock->socket_send_buffers_function_ = [&buffers, &mock_return_value]( |
| 140 | cutils_socket_t /*cutils_sock*/, cutils_socket_buffer_t* sent_buffers, |
| 141 | size_t num_sent_buffers) -> ssize_t { |
| 142 | EXPECT_EQ(buffers.size(), num_sent_buffers); |
| 143 | for (size_t i = 0; i < num_sent_buffers; ++i) { |
| 144 | EXPECT_EQ(buffers[i].data, sent_buffers[i].data); |
| 145 | EXPECT_EQ(buffers[i].length, sent_buffers[i].length); |
| 146 | } |
| 147 | return mock_return_value; |
| 148 | }; |
| 149 | |
| 150 | mock_return_value = strlen("foobar12345"); |
| 151 | EXPECT_TRUE(sock->Send(buffers)); |
| 152 | |
| 153 | mock_return_value -= 1; |
| 154 | EXPECT_FALSE(sock->Send(buffers)); |
| 155 | |
| 156 | mock_return_value = 0; |
| 157 | EXPECT_FALSE(sock->Send(buffers)); |
| 158 | |
| 159 | mock_return_value = -1; |
| 160 | EXPECT_FALSE(sock->Send(buffers)); |
| 161 | } |
| 162 | |
| 163 | // Tests TCP re-sending until socket_send_buffers() sends all data. This is a little complicated, |
| 164 | // but the general idea is that we intercept calls to socket_send_buffers() using a lambda mock |
| 165 | // function that simulates partial writes. |
| 166 | TEST(SocketTest, TestTcpSendBuffers) { |
| 167 | std::unique_ptr<Socket> sock = Socket::NewServer(Socket::Protocol::kTcp, 0); |
| 168 | std::vector<std::string> data{"foo", "bar", "12345"}; |
| 169 | std::vector<cutils_socket_buffer_t> buffers{{data[0].data(), data[0].length()}, |
| 170 | {data[1].data(), data[1].length()}, |
| 171 | {data[2].data(), data[2].length()}}; |
| 172 | |
| 173 | // Test breaking up the buffered send at various points. |
| 174 | std::list<std::string> test_sends[] = { |
| 175 | // Successes. |
| 176 | {"foobar12345"}, |
| 177 | {"f", "oob", "ar12345"}, |
| 178 | {"fo", "obar12", "345"}, |
| 179 | {"foo", "bar12345"}, |
| 180 | {"foob", "ar123", "45"}, |
| 181 | {"f", "o", "o", "b", "a", "r", "1", "2", "3", "4", "5"}, |
| 182 | |
| 183 | // Failures. |
| 184 | {}, |
| 185 | {"f"}, |
| 186 | {"foo", "bar"}, |
| 187 | {"fo", "obar12"}, |
| 188 | {"foobar1234"} |
| 189 | }; |
| 190 | |
| 191 | for (auto& test : test_sends) { |
| 192 | ssize_t bytes_sent = 0; |
| 193 | bool expect_success = true; |
| 194 | |
| 195 | // Create a mock function for custom socket_send_buffers() behavior. This function will |
| 196 | // check to make sure the input buffers start at the next unsent byte, then return the |
| 197 | // number of bytes indicated by the next entry in |test|. |
| 198 | sock->socket_send_buffers_function_ = [&bytes_sent, &data, &expect_success, &test]( |
| 199 | cutils_socket_t /*cutils_sock*/, cutils_socket_buffer_t* buffers, |
| 200 | size_t num_buffers) -> ssize_t { |
| 201 | EXPECT_TRUE(num_buffers > 0); |
| 202 | |
| 203 | // Failure case - pretend we errored out before sending all the buffers. |
| 204 | if (test.empty()) { |
| 205 | expect_success = false; |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | // Count the bytes we've sent to find where the next buffer should start and how many |
| 210 | // bytes should be left in it. |
| 211 | size_t byte_count = bytes_sent, data_index = 0; |
| 212 | while (data_index < data.size()) { |
| 213 | if (byte_count >= data[data_index].length()) { |
| 214 | byte_count -= data[data_index].length(); |
| 215 | ++data_index; |
| 216 | } else { |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | void* expected_next_byte = &data[data_index][byte_count]; |
| 221 | size_t expected_next_size = data[data_index].length() - byte_count; |
| 222 | |
| 223 | EXPECT_EQ(data.size() - data_index, num_buffers); |
| 224 | EXPECT_EQ(expected_next_byte, buffers[0].data); |
| 225 | EXPECT_EQ(expected_next_size, buffers[0].length); |
| 226 | |
| 227 | std::string to_send = std::move(test.front()); |
| 228 | test.pop_front(); |
| 229 | bytes_sent += to_send.length(); |
| 230 | return to_send.length(); |
| 231 | }; |
| 232 | |
| 233 | EXPECT_EQ(expect_success, sock->Send(buffers)); |
| 234 | EXPECT_TRUE(test.empty()); |
| 235 | } |
| 236 | } |
| 237 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 238 | TEST(SocketMockTest, TestSendSuccess) { |
| 239 | SocketMock mock; |
| 240 | |
| 241 | mock.ExpectSend("foo"); |
| 242 | EXPECT_TRUE(SendString(&mock, "foo")); |
| 243 | |
| 244 | mock.ExpectSend("abc"); |
| 245 | mock.ExpectSend("123"); |
| 246 | EXPECT_TRUE(SendString(&mock, "abc")); |
| 247 | EXPECT_TRUE(SendString(&mock, "123")); |
| 248 | } |
| 249 | |
| 250 | TEST(SocketMockTest, TestSendFailure) { |
| 251 | SocketMock* mock = new SocketMock; |
| 252 | |
| 253 | EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "no message was expected"); |
| 254 | |
| 255 | mock->ExpectSend("foo"); |
| 256 | EXPECT_NONFATAL_FAILURE(SendString(mock, "bar"), "expected foo, but got bar"); |
| 257 | EXPECT_TRUE(SendString(mock, "foo")); |
| 258 | |
| 259 | mock->AddReceive("foo"); |
| 260 | EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "called out-of-order"); |
| 261 | EXPECT_TRUE(ReceiveString(mock, "foo")); |
| 262 | |
| 263 | mock->ExpectSend("foo"); |
| 264 | EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); |
| 265 | } |
| 266 | |
| 267 | TEST(SocketMockTest, TestReceiveSuccess) { |
| 268 | SocketMock mock; |
| 269 | |
| 270 | mock.AddReceive("foo"); |
| 271 | EXPECT_TRUE(ReceiveString(&mock, "foo")); |
| 272 | |
| 273 | mock.AddReceive("abc"); |
| 274 | mock.AddReceive("123"); |
| 275 | EXPECT_TRUE(ReceiveString(&mock, "abc")); |
| 276 | EXPECT_TRUE(ReceiveString(&mock, "123")); |
| 277 | } |
| 278 | |
| 279 | TEST(SocketMockTest, TestReceiveFailure) { |
| 280 | SocketMock* mock = new SocketMock; |
| 281 | |
| 282 | EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "no message was ready"); |
| 283 | |
| 284 | mock->ExpectSend("foo"); |
| 285 | EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "called out-of-order"); |
| 286 | EXPECT_TRUE(SendString(mock, "foo")); |
| 287 | |
| 288 | char c; |
| 289 | mock->AddReceive("foo"); |
| 290 | EXPECT_NONFATAL_FAILURE(mock->Receive(&c, 1, 0), "not enough bytes (1) for foo"); |
| 291 | EXPECT_TRUE(ReceiveString(mock, "foo")); |
| 292 | |
| 293 | mock->AddReceive("foo"); |
| 294 | EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); |
| 295 | } |
| 296 | |
| 297 | TEST(SocketMockTest, TestAcceptSuccess) { |
| 298 | SocketMock mock; |
| 299 | |
| 300 | SocketMock* mock_handler = new SocketMock; |
| 301 | mock.AddAccept(std::unique_ptr<SocketMock>(mock_handler)); |
| 302 | EXPECT_EQ(mock_handler, mock.Accept().get()); |
| 303 | |
| 304 | mock.AddAccept(nullptr); |
| 305 | EXPECT_EQ(nullptr, mock.Accept().get()); |
| 306 | } |
| 307 | |
| 308 | TEST(SocketMockTest, TestAcceptFailure) { |
| 309 | SocketMock* mock = new SocketMock; |
| 310 | |
| 311 | EXPECT_NONFATAL_FAILURE(mock->Accept(), "no socket was ready"); |
| 312 | |
| 313 | mock->ExpectSend("foo"); |
| 314 | EXPECT_NONFATAL_FAILURE(mock->Accept(), "called out-of-order"); |
| 315 | EXPECT_TRUE(SendString(mock, "foo")); |
| 316 | |
| 317 | mock->AddAccept(nullptr); |
| 318 | EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); |
| 319 | } |