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