blob: 7bfe96714c81339975c2a1efbe183bc7da59924b [file] [log] [blame]
David Pursell815c7be2015-12-09 17:09:54 -08001/*
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 Pursellc3a46692016-01-29 08:10:50 -080017// 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 Pursell815c7be2015-12-09 17:09:54 -080022
23#include "socket.h"
David Pursellc3a46692016-01-29 08:10:50 -080024#include "socket_mock.h"
David Pursell815c7be2015-12-09 17:09:54 -080025
David Pursell815c7be2015-12-09 17:09:54 -080026#include <gtest/gtest.h>
David Pursellc3a46692016-01-29 08:10:50 -080027#include <gtest/gtest-spi.h>
David Pursell815c7be2015-12-09 17:09:54 -080028
David Pursellc3a46692016-01-29 08:10:50 -080029enum { kTestTimeoutMs = 3000 };
David Pursell815c7be2015-12-09 17:09:54 -080030
David Pursell572bce22016-01-15 14:19:56 -080031// Creates connected sockets |server| and |client|. Returns true on success.
32bool MakeConnectedSockets(Socket::Protocol protocol, std::unique_ptr<Socket>* server,
David Pursellc3a46692016-01-29 08:10:50 -080033 std::unique_ptr<Socket>* client,
34 const std::string hostname = "localhost") {
35 *server = Socket::NewServer(protocol, 0);
David Pursell572bce22016-01-15 14:19:56 -080036 if (*server == nullptr) {
37 ADD_FAILURE() << "Failed to create server.";
38 return false;
David Pursell815c7be2015-12-09 17:09:54 -080039 }
40
David Pursellc3a46692016-01-29 08:10:50 -080041 *client = Socket::NewClient(protocol, hostname, (*server)->GetLocalPort(), nullptr);
David Pursell572bce22016-01-15 14:19:56 -080042 if (*client == nullptr) {
43 ADD_FAILURE() << "Failed to create client.";
44 return false;
David Pursell815c7be2015-12-09 17:09:54 -080045 }
46
David Pursell572bce22016-01-15 14:19:56 -080047 // 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 Pursell815c7be2015-12-09 17:09:54 -080054 }
55
David Pursell572bce22016-01-15 14:19:56 -080056 return true;
57}
David Pursell815c7be2015-12-09 17:09:54 -080058
David Pursell572bce22016-01-15 14:19:56 -080059// Sends a string over a Socket. Returns true if the full string (without terminating char)
David Pursell815c7be2015-12-09 17:09:54 -080060// was sent.
David Pursell572bce22016-01-15 14:19:56 -080061static bool SendString(Socket* sock, const std::string& message) {
62 return sock->Send(message.c_str(), message.length()) == static_cast<ssize_t>(message.length());
David Pursell815c7be2015-12-09 17:09:54 -080063}
64
David Pursell572bce22016-01-15 14:19:56 -080065// Receives a string from a Socket. Returns true if the full string (without terminating char)
66// was received.
67static 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 Pursell815c7be2015-12-09 17:09:54 -080071}
72
73// Tests sending packets client -> server, then server -> client.
David Pursell572bce22016-01-15 14:19:56 -080074TEST(SocketTest, TestSendAndReceive) {
75 std::unique_ptr<Socket> server, client;
David Pursell815c7be2015-12-09 17:09:54 -080076
David Pursell572bce22016-01-15 14:19:56 -080077 for (Socket::Protocol protocol : {Socket::Protocol::kUdp, Socket::Protocol::kTcp}) {
78 ASSERT_TRUE(MakeConnectedSockets(protocol, &server, &client));
David Pursell815c7be2015-12-09 17:09:54 -080079
David Pursell572bce22016-01-15 14:19:56 -080080 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 Pursell815c7be2015-12-09 17:09:54 -080086}
87
88// Tests sending and receiving large packets.
David Pursell572bce22016-01-15 14:19:56 -080089TEST(SocketTest, TestLargePackets) {
90 std::string message(1024, '\0');
91 std::unique_ptr<Socket> server, client;
David Pursell815c7be2015-12-09 17:09:54 -080092
David Pursell572bce22016-01-15 14:19:56 -080093 for (Socket::Protocol protocol : {Socket::Protocol::kUdp, Socket::Protocol::kTcp}) {
94 ASSERT_TRUE(MakeConnectedSockets(protocol, &server, &client));
David Pursell815c7be2015-12-09 17:09:54 -080095
David Pursell572bce22016-01-15 14:19:56 -080096 // 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 Pursell815c7be2015-12-09 17:09:54 -0800105 }
David Pursell815c7be2015-12-09 17:09:54 -0800106 }
107}
108
David Pursell572bce22016-01-15 14:19:56 -0800109// Tests UDP receive overflow when the UDP packet is larger than the receive buffer.
110TEST(SocketTest, TestUdpReceiveOverflow) {
111 std::unique_ptr<Socket> server, client;
112 ASSERT_TRUE(MakeConnectedSockets(Socket::Protocol::kUdp, &server, &client));
David Pursell815c7be2015-12-09 17:09:54 -0800113
David Pursell572bce22016-01-15 14:19:56 -0800114 EXPECT_TRUE(SendString(client.get(), "1234567890"));
David Pursell815c7be2015-12-09 17:09:54 -0800115
David Pursell572bce22016-01-15 14:19:56 -0800116 // 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 Pursell815c7be2015-12-09 17:09:54 -0800123 }
124}
David Pursellc3a46692016-01-29 08:10:50 -0800125
126TEST(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
138TEST(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
155TEST(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
167TEST(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
185TEST(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
196TEST(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}