sockets_test.cpp 7.0 KB

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