blob: 6ada964ad2e635c3061a553c26d1ee708181591c [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
17// Tests UDP functionality using loopback connections. Requires that kDefaultPort is available
18// for loopback communication on the host. These tests also assume that no UDP packets are lost,
19// which should be the case for loopback communication, but is not guaranteed.
20
21#include "socket.h"
22
23#include <errno.h>
24#include <time.h>
25
26#include <memory>
27#include <string>
28#include <vector>
29
30#include <gtest/gtest.h>
31
32enum {
33 // This port must be available for loopback communication.
34 kDefaultPort = 54321,
35
36 // Don't wait forever in a unit test.
37 kDefaultTimeoutMs = 3000,
38};
39
40static const char kReceiveStringError[] = "Error receiving string";
41
42// Test fixture to provide some helper functions. Makes each test a little simpler since we can
43// just check a bool for socket creation and don't have to pass hostname or port information.
44class SocketTest : public ::testing::Test {
45 protected:
46 bool StartServer(int port = kDefaultPort) {
47 server_ = UdpSocket::NewUdpServer(port);
48 return server_ != nullptr;
49 }
50
51 bool StartClient(const std::string hostname = "localhost", int port = kDefaultPort) {
52 client_ = UdpSocket::NewUdpClient(hostname, port, nullptr);
53 return client_ != nullptr;
54 }
55
56 bool StartClient2(const std::string hostname = "localhost", int port = kDefaultPort) {
57 client2_ = UdpSocket::NewUdpClient(hostname, port, nullptr);
58 return client2_ != nullptr;
59 }
60
61 std::unique_ptr<UdpSocket> server_, client_, client2_;
62};
63
64// Sends a string over a UdpSocket. Returns true if the full string (without terminating char)
65// was sent.
66static bool SendString(UdpSocket* udp, const std::string& message) {
67 return udp->Send(message.c_str(), message.length()) == static_cast<ssize_t>(message.length());
68}
69
70// Receives a string from a UdpSocket. Returns the string, or kReceiveStringError on failure.
71static std::string ReceiveString(UdpSocket* udp, size_t receive_size = 128) {
72 std::vector<char> buffer(receive_size);
73
74 ssize_t result = udp->Receive(buffer.data(), buffer.size(), kDefaultTimeoutMs);
75 if (result >= 0) {
76 return std::string(buffer.data(), result);
77 }
78 return kReceiveStringError;
79}
80
81// Calls Receive() on the UdpSocket with the given timeout. Returns true if the call timed out.
82static bool ReceiveTimeout(UdpSocket* udp, int timeout_ms) {
83 char buffer[1];
84
85 errno = 0;
86 return udp->Receive(buffer, 1, timeout_ms) == -1 && (errno == EAGAIN || errno == EWOULDBLOCK);
87}
88
89// Tests sending packets client -> server, then server -> client.
90TEST_F(SocketTest, SendAndReceive) {
91 ASSERT_TRUE(StartServer());
92 ASSERT_TRUE(StartClient());
93
94 EXPECT_TRUE(SendString(client_.get(), "foo"));
95 EXPECT_EQ("foo", ReceiveString(server_.get()));
96
97 EXPECT_TRUE(SendString(server_.get(), "bar baz"));
98 EXPECT_EQ("bar baz", ReceiveString(client_.get()));
99}
100
101// Tests sending and receiving large packets.
102TEST_F(SocketTest, LargePackets) {
103 std::string message(512, '\0');
104
105 ASSERT_TRUE(StartServer());
106 ASSERT_TRUE(StartClient());
107
108 // Run through the test a few times.
109 for (int i = 0; i < 10; ++i) {
110 // Use a different message each iteration to prevent false positives.
111 for (size_t j = 0; j < message.length(); ++j) {
112 message[j] = static_cast<char>(i + j);
113 }
114
115 EXPECT_TRUE(SendString(client_.get(), message));
116 EXPECT_EQ(message, ReceiveString(server_.get(), message.length()));
117 }
118}
119
120// Tests IPv4 client/server.
121TEST_F(SocketTest, IPv4) {
122 ASSERT_TRUE(StartServer());
123 ASSERT_TRUE(StartClient("127.0.0.1"));
124
125 EXPECT_TRUE(SendString(client_.get(), "foo"));
126 EXPECT_EQ("foo", ReceiveString(server_.get()));
127
128 EXPECT_TRUE(SendString(server_.get(), "bar"));
129 EXPECT_EQ("bar", ReceiveString(client_.get()));
130}
131
132// Tests IPv6 client/server.
133TEST_F(SocketTest, IPv6) {
134 ASSERT_TRUE(StartServer());
135 ASSERT_TRUE(StartClient("::1"));
136
137 EXPECT_TRUE(SendString(client_.get(), "foo"));
138 EXPECT_EQ("foo", ReceiveString(server_.get()));
139
140 EXPECT_TRUE(SendString(server_.get(), "bar"));
141 EXPECT_EQ("bar", ReceiveString(client_.get()));
142}
143
144// Tests receive timeout. The timing verification logic must be very coarse to make sure different
145// systems running different loads can all pass these tests.
146TEST_F(SocketTest, ReceiveTimeout) {
147 time_t start_time;
148
149 ASSERT_TRUE(StartServer());
150
151 // Make sure a 20ms timeout completes in 1 second or less.
152 start_time = time(nullptr);
153 EXPECT_TRUE(ReceiveTimeout(server_.get(), 20));
154 EXPECT_LE(difftime(time(nullptr), start_time), 1.0);
155
156 // Make sure a 1250ms timeout takes 1 second or more.
157 start_time = time(nullptr);
158 EXPECT_TRUE(ReceiveTimeout(server_.get(), 1250));
159 EXPECT_LE(1.0, difftime(time(nullptr), start_time));
160}
161
162// Tests receive overflow (the UDP packet is larger than the receive buffer).
163TEST_F(SocketTest, ReceiveOverflow) {
164 ASSERT_TRUE(StartServer());
165 ASSERT_TRUE(StartClient());
166
167 EXPECT_TRUE(SendString(client_.get(), "1234567890"));
168
169 // This behaves differently on different systems; some give us a truncated UDP packet, others
170 // will error out and not return anything at all.
171 std::string rx_string = ReceiveString(server_.get(), 5);
172
173 // If we didn't get an error then the packet should have been truncated.
174 if (rx_string != kReceiveStringError) {
175 EXPECT_EQ("12345", rx_string);
176 }
177}
178
179// Tests multiple clients sending to the same server.
180TEST_F(SocketTest, MultipleClients) {
181 ASSERT_TRUE(StartServer());
182 ASSERT_TRUE(StartClient());
183 ASSERT_TRUE(StartClient2());
184
185 EXPECT_TRUE(SendString(client_.get(), "client"));
186 EXPECT_TRUE(SendString(client2_.get(), "client2"));
187
188 // Receive the packets and send a response for each (note that packets may be received
189 // out-of-order).
190 for (int i = 0; i < 2; ++i) {
191 std::string received = ReceiveString(server_.get());
192 EXPECT_TRUE(SendString(server_.get(), received + " response"));
193 }
194
195 EXPECT_EQ("client response", ReceiveString(client_.get()));
196 EXPECT_EQ("client2 response", ReceiveString(client2_.get()));
197}