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 // Tests socket functionality using loopback connections. Requires IPv4 and
18 // IPv6 capabilities. These tests assume that no UDP packets are lost, which
19 // should be the case for loopback communication, but is not guaranteed.
20
21 #include <cutils/sockets.h>
22
23 #include <time.h>
24
25 #include <gtest/gtest.h>
26
27 // Makes sure the passed sockets are valid, sends data between them, and closes
28 // them. Any failures are logged with gtest.
29 //
30 // On Mac recvfrom() will not fill in the address for TCP sockets, so we need
31 // separate logic paths depending on socket type.
TestConnectedSockets(cutils_socket_t server,cutils_socket_t client,int type)32 static void TestConnectedSockets(cutils_socket_t server, cutils_socket_t client,
33 int type) {
34 ASSERT_NE(INVALID_SOCKET, server);
35 ASSERT_NE(INVALID_SOCKET, client);
36
37 char buffer[128];
38 sockaddr_storage addr;
39 socklen_t addr_size = sizeof(addr);
40
41 // Send client -> server first to get the UDP client's address.
42 ASSERT_EQ(3, send(client, "foo", 3, 0));
43 if (type == SOCK_DGRAM) {
44 EXPECT_EQ(3, recvfrom(server, buffer, sizeof(buffer), 0,
45 reinterpret_cast<sockaddr*>(&addr), &addr_size));
46 } else {
47 EXPECT_EQ(3, recv(server, buffer, sizeof(buffer), 0));
48 }
49 EXPECT_EQ(0, memcmp(buffer, "foo", 3));
50
51 // Now send server -> client.
52 if (type == SOCK_DGRAM) {
53 ASSERT_EQ(3, sendto(server, "bar", 3, 0,
54 reinterpret_cast<sockaddr*>(&addr), addr_size));
55 } else {
56 ASSERT_EQ(3, send(server, "bar", 3, 0));
57 }
58 EXPECT_EQ(3, recv(client, buffer, sizeof(buffer), 0));
59 EXPECT_EQ(0, memcmp(buffer, "bar", 3));
60
61 // Send multiple buffers using socket_send_buffers().
62 std::string data[] = {"foo", "bar", "12345"};
63 cutils_socket_buffer_t socket_buffers[] = { {data[0].data(), data[0].length()},
64 {data[1].data(), data[1].length()},
65 {data[2].data(), data[2].length()} };
66 EXPECT_EQ(11, socket_send_buffers(client, socket_buffers, 3));
67 EXPECT_EQ(11, recv(server, buffer, sizeof(buffer), 0));
68 EXPECT_EQ(0, memcmp(buffer, "foobar12345", 11));
69
70 EXPECT_EQ(0, socket_close(server));
71 EXPECT_EQ(0, socket_close(client));
72 }
73
74 // Tests receive timeout. The timing verification logic must be very coarse to
75 // make sure different systems can all pass these tests.
TestReceiveTimeout(cutils_socket_t sock)76 void TestReceiveTimeout(cutils_socket_t sock) {
77 time_t start_time;
78 char buffer[32];
79
80 // Make sure a 20ms timeout completes in 1 second or less.
81 EXPECT_EQ(0, socket_set_receive_timeout(sock, 20));
82 start_time = time(nullptr);
83 EXPECT_EQ(-1, recv(sock, buffer, sizeof(buffer), 0));
84 EXPECT_LE(difftime(time(nullptr), start_time), 1.0);
85
86 // Make sure a 1250ms timeout takes 1 second or more.
87 EXPECT_EQ(0, socket_set_receive_timeout(sock, 1250));
88 start_time = time(nullptr);
89 EXPECT_EQ(-1, recv(sock, buffer, sizeof(buffer), 0));
90 EXPECT_LE(1.0, difftime(time(nullptr), start_time));
91 }
92
93 // Tests socket_get_local_port().
TEST(SocketsTest,TestGetLocalPort)94 TEST(SocketsTest, TestGetLocalPort) {
95 cutils_socket_t server;
96
97 // Check a bunch of ports so that we can ignore any conflicts in case
98 // of ports already being taken, but if a server is able to start up we
99 // should always be able to read its port.
100 for (int port : {10000, 12345, 15999, 20202, 25000}) {
101 for (int type : {SOCK_DGRAM, SOCK_STREAM}) {
102 server = socket_inaddr_any_server(port, SOCK_DGRAM);
103 if (server != INVALID_SOCKET) {
104 EXPECT_EQ(port, socket_get_local_port(server));
105 }
106 socket_close(server);
107 }
108 }
109
110 // Check expected failure for an invalid socket.
111 EXPECT_EQ(-1, socket_get_local_port(INVALID_SOCKET));
112 }
113
114 // Tests socket_inaddr_any_server() and socket_network_client() for IPv4 UDP.
TEST(SocketsTest,TestIpv4UdpLoopback)115 TEST(SocketsTest, TestIpv4UdpLoopback) {
116 cutils_socket_t server = socket_inaddr_any_server(0, SOCK_DGRAM);
117 cutils_socket_t client = socket_network_client(
118 "127.0.0.1", socket_get_local_port(server), SOCK_DGRAM);
119
120 TestConnectedSockets(server, client, SOCK_DGRAM);
121 }
122
123 // Tests socket_inaddr_any_server() and socket_network_client() for IPv4 TCP.
TEST(SocketsTest,TestIpv4TcpLoopback)124 TEST(SocketsTest, TestIpv4TcpLoopback) {
125 cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
126 ASSERT_NE(INVALID_SOCKET, server);
127
128 cutils_socket_t client = socket_network_client(
129 "127.0.0.1", socket_get_local_port(server), SOCK_STREAM);
130 cutils_socket_t handler = accept(server, nullptr, nullptr);
131 EXPECT_EQ(0, socket_close(server));
132
133 TestConnectedSockets(handler, client, SOCK_STREAM);
134 }
135
136 // Tests socket_inaddr_any_server() and socket_network_client() for IPv6 UDP.
TEST(SocketsTest,TestIpv6UdpLoopback)137 TEST(SocketsTest, TestIpv6UdpLoopback) {
138 cutils_socket_t server = socket_inaddr_any_server(0, SOCK_DGRAM);
139 cutils_socket_t client = socket_network_client(
140 "::1", socket_get_local_port(server), SOCK_DGRAM);
141
142 TestConnectedSockets(server, client, SOCK_DGRAM);
143 }
144
145 // Tests socket_inaddr_any_server() and socket_network_client() for IPv6 TCP.
TEST(SocketsTest,TestIpv6TcpLoopback)146 TEST(SocketsTest, TestIpv6TcpLoopback) {
147 cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
148 ASSERT_NE(INVALID_SOCKET, server);
149
150 cutils_socket_t client = socket_network_client(
151 "::1", socket_get_local_port(server), SOCK_STREAM);
152 cutils_socket_t handler = accept(server, nullptr, nullptr);
153 EXPECT_EQ(0, socket_close(server));
154
155 TestConnectedSockets(handler, client, SOCK_STREAM);
156 }
157
158 // Tests setting a receive timeout for UDP sockets.
TEST(SocketsTest,TestUdpReceiveTimeout)159 TEST(SocketsTest, TestUdpReceiveTimeout) {
160 cutils_socket_t sock = socket_inaddr_any_server(0, SOCK_DGRAM);
161 ASSERT_NE(INVALID_SOCKET, sock);
162
163 TestReceiveTimeout(sock);
164
165 EXPECT_EQ(0, socket_close(sock));
166 }
167
168 // Tests setting a receive timeout for TCP sockets.
TEST(SocketsTest,TestTcpReceiveTimeout)169 TEST(SocketsTest, TestTcpReceiveTimeout) {
170 cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
171 ASSERT_NE(INVALID_SOCKET, server);
172
173 cutils_socket_t client = socket_network_client(
174 "localhost", socket_get_local_port(server), SOCK_STREAM);
175 cutils_socket_t handler = accept(server, nullptr, nullptr);
176 EXPECT_EQ(0, socket_close(server));
177
178 TestReceiveTimeout(handler);
179
180 EXPECT_EQ(0, socket_close(client));
181 EXPECT_EQ(0, socket_close(handler));
182 }
183
184 // Tests socket_send_buffers() failure.
TEST(SocketsTest,TestSocketSendBuffersFailure)185 TEST(SocketsTest, TestSocketSendBuffersFailure) {
186 EXPECT_EQ(-1, socket_send_buffers(INVALID_SOCKET, nullptr, 0));
187 }
188