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 | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 27 | #include <gtest/gtest-spi.h> |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 28 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 29 | enum { kTestTimeoutMs = 3000 }; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 30 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 31 | // Creates connected sockets |server| and |client|. Returns true on success. |
| 32 | bool MakeConnectedSockets(Socket::Protocol protocol, std::unique_ptr<Socket>* server, |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 33 | std::unique_ptr<Socket>* client, |
| 34 | const std::string hostname = "localhost") { |
| 35 | *server = Socket::NewServer(protocol, 0); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 36 | if (*server == nullptr) { |
| 37 | ADD_FAILURE() << "Failed to create server."; |
| 38 | return false; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 39 | } |
| 40 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 41 | *client = Socket::NewClient(protocol, hostname, (*server)->GetLocalPort(), nullptr); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 42 | if (*client == nullptr) { |
| 43 | ADD_FAILURE() << "Failed to create client."; |
| 44 | return false; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 45 | } |
| 46 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 47 | // TCP passes the client off to a new socket. |
| 48 | if (protocol == Socket::Protocol::kTcp) { |
| 49 | *server = (*server)->Accept(); |
| 50 | if (*server == nullptr) { |
| 51 | ADD_FAILURE() << "Failed to accept client connection."; |
| 52 | return false; |
| 53 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 54 | } |
| 55 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 56 | return true; |
| 57 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 58 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 59 | // 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] | 60 | // was sent. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 61 | static bool SendString(Socket* sock, const std::string& message) { |
| 62 | return sock->Send(message.c_str(), message.length()) == static_cast<ssize_t>(message.length()); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 63 | } |
| 64 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 65 | // Receives a string from a Socket. Returns true if the full string (without terminating char) |
| 66 | // was received. |
| 67 | static bool ReceiveString(Socket* sock, const std::string& message) { |
| 68 | std::string received(message.length(), '\0'); |
| 69 | ssize_t bytes = sock->ReceiveAll(&received[0], received.length(), kTestTimeoutMs); |
| 70 | return static_cast<size_t>(bytes) == received.length() && received == message; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Tests sending packets client -> server, then server -> client. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 74 | TEST(SocketTest, TestSendAndReceive) { |
| 75 | std::unique_ptr<Socket> server, client; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 76 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 77 | for (Socket::Protocol protocol : {Socket::Protocol::kUdp, Socket::Protocol::kTcp}) { |
| 78 | ASSERT_TRUE(MakeConnectedSockets(protocol, &server, &client)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 79 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 80 | EXPECT_TRUE(SendString(client.get(), "foo")); |
| 81 | EXPECT_TRUE(ReceiveString(server.get(), "foo")); |
| 82 | |
| 83 | EXPECT_TRUE(SendString(server.get(), "bar baz")); |
| 84 | EXPECT_TRUE(ReceiveString(client.get(), "bar baz")); |
| 85 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Tests sending and receiving large packets. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 89 | TEST(SocketTest, TestLargePackets) { |
| 90 | std::string message(1024, '\0'); |
| 91 | std::unique_ptr<Socket> server, client; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 92 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 93 | for (Socket::Protocol protocol : {Socket::Protocol::kUdp, Socket::Protocol::kTcp}) { |
| 94 | ASSERT_TRUE(MakeConnectedSockets(protocol, &server, &client)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 95 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 96 | // Run through the test a few times. |
| 97 | for (int i = 0; i < 10; ++i) { |
| 98 | // Use a different message each iteration to prevent false positives. |
| 99 | for (size_t j = 0; j < message.length(); ++j) { |
| 100 | message[j] = static_cast<char>(i + j); |
| 101 | } |
| 102 | |
| 103 | EXPECT_TRUE(SendString(client.get(), message)); |
| 104 | EXPECT_TRUE(ReceiveString(server.get(), message)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 105 | } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 109 | // Tests UDP receive overflow when the UDP packet is larger than the receive buffer. |
| 110 | TEST(SocketTest, TestUdpReceiveOverflow) { |
| 111 | std::unique_ptr<Socket> server, client; |
| 112 | ASSERT_TRUE(MakeConnectedSockets(Socket::Protocol::kUdp, &server, &client)); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 113 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 114 | EXPECT_TRUE(SendString(client.get(), "1234567890")); |
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 | // This behaves differently on different systems, either truncating the packet or returning -1. |
| 117 | char buffer[5]; |
| 118 | ssize_t bytes = server->Receive(buffer, 5, kTestTimeoutMs); |
| 119 | if (bytes == 5) { |
| 120 | EXPECT_EQ(0, memcmp(buffer, "12345", 5)); |
| 121 | } else { |
| 122 | EXPECT_EQ(-1, bytes); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 125 | |
| 126 | TEST(SocketMockTest, TestSendSuccess) { |
| 127 | SocketMock mock; |
| 128 | |
| 129 | mock.ExpectSend("foo"); |
| 130 | EXPECT_TRUE(SendString(&mock, "foo")); |
| 131 | |
| 132 | mock.ExpectSend("abc"); |
| 133 | mock.ExpectSend("123"); |
| 134 | EXPECT_TRUE(SendString(&mock, "abc")); |
| 135 | EXPECT_TRUE(SendString(&mock, "123")); |
| 136 | } |
| 137 | |
| 138 | TEST(SocketMockTest, TestSendFailure) { |
| 139 | SocketMock* mock = new SocketMock; |
| 140 | |
| 141 | EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "no message was expected"); |
| 142 | |
| 143 | mock->ExpectSend("foo"); |
| 144 | EXPECT_NONFATAL_FAILURE(SendString(mock, "bar"), "expected foo, but got bar"); |
| 145 | EXPECT_TRUE(SendString(mock, "foo")); |
| 146 | |
| 147 | mock->AddReceive("foo"); |
| 148 | EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "called out-of-order"); |
| 149 | EXPECT_TRUE(ReceiveString(mock, "foo")); |
| 150 | |
| 151 | mock->ExpectSend("foo"); |
| 152 | EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); |
| 153 | } |
| 154 | |
| 155 | TEST(SocketMockTest, TestReceiveSuccess) { |
| 156 | SocketMock mock; |
| 157 | |
| 158 | mock.AddReceive("foo"); |
| 159 | EXPECT_TRUE(ReceiveString(&mock, "foo")); |
| 160 | |
| 161 | mock.AddReceive("abc"); |
| 162 | mock.AddReceive("123"); |
| 163 | EXPECT_TRUE(ReceiveString(&mock, "abc")); |
| 164 | EXPECT_TRUE(ReceiveString(&mock, "123")); |
| 165 | } |
| 166 | |
| 167 | TEST(SocketMockTest, TestReceiveFailure) { |
| 168 | SocketMock* mock = new SocketMock; |
| 169 | |
| 170 | EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "no message was ready"); |
| 171 | |
| 172 | mock->ExpectSend("foo"); |
| 173 | EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "called out-of-order"); |
| 174 | EXPECT_TRUE(SendString(mock, "foo")); |
| 175 | |
| 176 | char c; |
| 177 | mock->AddReceive("foo"); |
| 178 | EXPECT_NONFATAL_FAILURE(mock->Receive(&c, 1, 0), "not enough bytes (1) for foo"); |
| 179 | EXPECT_TRUE(ReceiveString(mock, "foo")); |
| 180 | |
| 181 | mock->AddReceive("foo"); |
| 182 | EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); |
| 183 | } |
| 184 | |
| 185 | TEST(SocketMockTest, TestAcceptSuccess) { |
| 186 | SocketMock mock; |
| 187 | |
| 188 | SocketMock* mock_handler = new SocketMock; |
| 189 | mock.AddAccept(std::unique_ptr<SocketMock>(mock_handler)); |
| 190 | EXPECT_EQ(mock_handler, mock.Accept().get()); |
| 191 | |
| 192 | mock.AddAccept(nullptr); |
| 193 | EXPECT_EQ(nullptr, mock.Accept().get()); |
| 194 | } |
| 195 | |
| 196 | TEST(SocketMockTest, TestAcceptFailure) { |
| 197 | SocketMock* mock = new SocketMock; |
| 198 | |
| 199 | EXPECT_NONFATAL_FAILURE(mock->Accept(), "no socket was ready"); |
| 200 | |
| 201 | mock->ExpectSend("foo"); |
| 202 | EXPECT_NONFATAL_FAILURE(mock->Accept(), "called out-of-order"); |
| 203 | EXPECT_TRUE(SendString(mock, "foo")); |
| 204 | |
| 205 | mock->AddAccept(nullptr); |
| 206 | EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled"); |
| 207 | } |