sock_diag_test.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 2016 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. * sock_diag_test.cpp - unit tests for SockDiag.cpp
  17. */
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <linux/inet_diag.h>
  21. #include <gtest/gtest.h>
  22. #include "NetdConstants.h"
  23. #include "SockDiag.h"
  24. #define NUM_SOCKETS 500
  25. class SockDiagTest : public ::testing::Test {
  26. };
  27. uint16_t bindAndListen(int s) {
  28. for (int i = 0; i < 10; i++) {
  29. uint16_t port = 1024 + arc4random_uniform(0xffff - 1024);
  30. sockaddr_in6 sin6 = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
  31. if (bind(s, (sockaddr *) &sin6, sizeof(sin6)) == 0) {
  32. listen(s, 1);
  33. return port;
  34. }
  35. }
  36. close(s);
  37. return 0;
  38. }
  39. const char *tcpStateName(uint8_t state) {
  40. static const char *states[] = {
  41. "???",
  42. "TCP_ESTABLISHED",
  43. "TCP_SYN_SENT",
  44. "TCP_SYN_RECV",
  45. "TCP_FIN_WAIT1",
  46. "TCP_FIN_WAIT2",
  47. "TCP_TIME_WAIT",
  48. "TCP_CLOSE",
  49. "TCP_CLOSE_WAIT",
  50. "TCP_LAST_ACK",
  51. "TCP_LISTEN",
  52. "TCP_CLOSING",
  53. "TCP_NEW_SYN_RECV",
  54. };
  55. return states[(state < ARRAY_SIZE(states)) ? state : 0];
  56. }
  57. TEST_F(SockDiagTest, TestDump) {
  58. int v4socket = socket(AF_INET, SOCK_STREAM, 0);
  59. int v6socket = socket(AF_INET6, SOCK_STREAM, 0);
  60. int listensocket = socket(AF_INET6, SOCK_STREAM, 0);
  61. ASSERT_NE(-1, v4socket) << "Failed to open IPv4 socket";
  62. ASSERT_NE(-1, v6socket) << "Failed to open IPv6 socket";
  63. ASSERT_NE(-1, listensocket) << "Failed to open listen socket";
  64. uint16_t port = bindAndListen(listensocket);
  65. ASSERT_NE(0, port) << "Can't bind to server port";
  66. // Connect to loopback.
  67. sockaddr_in server4 = { .sin_family = AF_INET, .sin_port = htons(port) };
  68. sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
  69. ASSERT_EQ(0, connect(v4socket, (sockaddr *) &server4, sizeof(server4)))
  70. << "IPv4 connect failed: " << strerror(errno);
  71. ASSERT_EQ(0, connect(v6socket, (sockaddr *) &server6, sizeof(server6)))
  72. << "IPv6 connect failed: " << strerror(errno);
  73. sockaddr_in6 client46, client6;
  74. socklen_t clientlen = std::max(sizeof(client46), sizeof(client6));
  75. int accepted4 = accept(listensocket, (sockaddr *) &client46, &clientlen);
  76. int accepted6 = accept(listensocket, (sockaddr *) &client6, &clientlen);
  77. ASSERT_NE(-1, accepted4);
  78. ASSERT_NE(-1, accepted6);
  79. int v4SocketsSeen = 0;
  80. bool seenclient46 = false;
  81. bool seenNull = false;
  82. char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
  83. fprintf(stderr, "Ports:\n server=%d. client46=%d, client6=%d\n",
  84. port, ntohs(client46.sin6_port), ntohs(client6.sin6_port));
  85. auto checkIPv4Dump = [&] (uint8_t /* proto */, const inet_diag_msg *msg) {
  86. if (msg == nullptr) {
  87. EXPECT_FALSE(seenNull);
  88. seenNull = true;
  89. return 0;
  90. }
  91. EXPECT_EQ(htonl(INADDR_LOOPBACK), msg->id.idiag_src[0]);
  92. v4SocketsSeen++;
  93. seenclient46 |= (msg->id.idiag_sport == client46.sin6_port);
  94. inet_ntop(AF_INET, msg->id.idiag_src, src, sizeof(src));
  95. inet_ntop(AF_INET, msg->id.idiag_src, dst, sizeof(dst));
  96. fprintf(stderr, " v4 %s:%d -> %s:%d %s\n",
  97. src, htons(msg->id.idiag_sport),
  98. dst, htons(msg->id.idiag_dport),
  99. tcpStateName(msg->idiag_state));
  100. return 0;
  101. };
  102. int v6SocketsSeen = 0;
  103. bool seenClient6 = false, seenServer46 = false, seenServer6 = false;
  104. auto checkIPv6Dump = [&] (uint8_t /* proto */, const inet_diag_msg *msg) {
  105. if (msg == nullptr) {
  106. EXPECT_FALSE(seenNull);
  107. seenNull = true;
  108. return 0;
  109. }
  110. struct in6_addr *saddr = (struct in6_addr *) msg->id.idiag_src;
  111. EXPECT_TRUE(
  112. IN6_IS_ADDR_LOOPBACK(saddr) ||
  113. (IN6_IS_ADDR_V4MAPPED(saddr) && saddr->s6_addr32[3] == htonl(INADDR_LOOPBACK)));
  114. v6SocketsSeen++;
  115. seenClient6 |= (msg->id.idiag_sport == client6.sin6_port);
  116. seenServer46 |= (msg->id.idiag_sport == htons(port));
  117. seenServer6 |= (msg->id.idiag_sport == htons(port));
  118. inet_ntop(AF_INET6, msg->id.idiag_src, src, sizeof(src));
  119. inet_ntop(AF_INET6, msg->id.idiag_src, dst, sizeof(dst));
  120. fprintf(stderr, " v6 [%s]:%d -> [%s]:%d %s\n",
  121. src, htons(msg->id.idiag_sport),
  122. dst, htons(msg->id.idiag_dport),
  123. tcpStateName(msg->idiag_state));
  124. return 0;
  125. };
  126. SockDiag sd;
  127. ASSERT_TRUE(sd.open()) << "Failed to open SOCK_DIAG socket";
  128. seenNull = false;
  129. int ret = sd.sendDumpRequest(IPPROTO_TCP, AF_INET, "127.0.0.1");
  130. ASSERT_EQ(0, ret) << "Failed to send IPv4 dump request: " << strerror(-ret);
  131. fprintf(stderr, "Sent IPv4 dump\n");
  132. sd.readDiagMsg(IPPROTO_TCP, checkIPv4Dump);
  133. EXPECT_GE(v4SocketsSeen, 1);
  134. EXPECT_TRUE(seenclient46);
  135. EXPECT_FALSE(seenServer46);
  136. seenNull = false;
  137. ret = sd.sendDumpRequest(IPPROTO_TCP, AF_INET6, "127.0.0.1");
  138. ASSERT_EQ(0, ret) << "Failed to send mapped dump request: " << strerror(-ret);
  139. fprintf(stderr, "Sent mapped dump\n");
  140. sd.readDiagMsg(IPPROTO_TCP, checkIPv6Dump);
  141. EXPECT_TRUE(seenServer46);
  142. seenNull = false;
  143. ret = sd.sendDumpRequest(IPPROTO_TCP, AF_INET6, "::1");
  144. ASSERT_EQ(0, ret) << "Failed to send IPv6 dump request: " << strerror(-ret);
  145. fprintf(stderr, "Sent IPv6 dump\n");
  146. sd.readDiagMsg(IPPROTO_TCP, checkIPv6Dump);
  147. EXPECT_GE(v6SocketsSeen, 1);
  148. EXPECT_TRUE(seenClient6);
  149. EXPECT_TRUE(seenServer6);
  150. close(v4socket);
  151. close(v6socket);
  152. close(listensocket);
  153. close(accepted4);
  154. close(accepted6);
  155. }
  156. TEST_F(SockDiagTest, TestMicroBenchmark) {
  157. fprintf(stderr, "Benchmarking closing %d sockets\n", NUM_SOCKETS);
  158. int listensocket = socket(AF_INET6, SOCK_STREAM, 0);
  159. ASSERT_NE(-1, listensocket) << "Failed to open listen socket";
  160. uint16_t port = bindAndListen(listensocket);
  161. ASSERT_NE(0, port) << "Can't bind to server port";
  162. sockaddr_in6 server = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
  163. using ms = std::chrono::duration<float, std::ratio<1, 1000>>;
  164. int clientsockets[NUM_SOCKETS], serversockets[NUM_SOCKETS];
  165. uint16_t clientports[NUM_SOCKETS];
  166. sockaddr_in6 client;
  167. socklen_t clientlen;
  168. auto start = std::chrono::steady_clock::now();
  169. for (int i = 0; i < NUM_SOCKETS; i++) {
  170. int s = socket(AF_INET6, SOCK_STREAM, 0);
  171. clientlen = sizeof(client);
  172. ASSERT_EQ(0, connect(s, (sockaddr *) &server, sizeof(server)))
  173. << "Connecting socket " << i << " failed " << strerror(errno);
  174. serversockets[i] = accept(listensocket, (sockaddr *) &client, &clientlen);
  175. ASSERT_NE(-1, serversockets[i])
  176. << "Accepting socket " << i << " failed " << strerror(errno);
  177. clientports[i] = client.sin6_port;
  178. clientsockets[i] = s;
  179. }
  180. fprintf(stderr, " Connecting: %6.1f ms\n",
  181. std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
  182. SockDiag sd;
  183. ASSERT_TRUE(sd.open()) << "Failed to open SOCK_DIAG socket";
  184. start = std::chrono::steady_clock::now();
  185. int ret = sd.destroySockets("::1");
  186. EXPECT_LE(0, ret) << ": Failed to destroy sockets on ::1: " << strerror(-ret);
  187. fprintf(stderr, " Destroying: %6.1f ms\n",
  188. std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
  189. int err;
  190. start = std::chrono::steady_clock::now();
  191. for (int i = 0; i < NUM_SOCKETS; i++) {
  192. ret = send(clientsockets[i], "foo", sizeof("foo"), 0);
  193. err = errno;
  194. EXPECT_EQ(-1, ret) << "Client socket " << i << " not closed";
  195. if (ret == -1) {
  196. // Since we're connected to ourselves, the error might be ECONNABORTED (if we destroyed
  197. // the socket) or ECONNRESET (if the other end was destroyed and sent a RST).
  198. EXPECT_TRUE(errno == ECONNABORTED || errno == ECONNRESET)
  199. << "Client socket: unexpected error: " << strerror(errno);
  200. }
  201. ret = send(serversockets[i], "foo", sizeof("foo"), 0);
  202. err = errno;
  203. EXPECT_EQ(-1, ret) << "Server socket " << i << " not closed";
  204. if (ret == -1) {
  205. EXPECT_TRUE(errno == ECONNABORTED || errno == ECONNRESET)
  206. << "Server socket: unexpected error: " << strerror(errno);
  207. }
  208. }
  209. fprintf(stderr, " Verifying: %6.1f ms\n",
  210. std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
  211. start = std::chrono::steady_clock::now();
  212. for (int i = 0; i < NUM_SOCKETS; i++) {
  213. close(clientsockets[i]);
  214. close(serversockets[i]);
  215. }
  216. fprintf(stderr, " Closing: %6.1f ms\n",
  217. std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
  218. close(listensocket);
  219. }