test-ping.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "client.h"
  19. #include "smartdns/fast_ping.h"
  20. #include "include/utils.h"
  21. #include "server.h"
  22. #include "smartdns/tlog.h"
  23. #include "gtest/gtest.h"
  24. class Ping : public ::testing::Test
  25. {
  26. protected:
  27. virtual void SetUp()
  28. {
  29. EXPECT_EQ(fast_ping_init(), 0);
  30. loglevel = tlog_getlevel();
  31. tlog_setlevel(TLOG_DEBUG);
  32. }
  33. virtual void TearDown()
  34. {
  35. fast_ping_exit();
  36. tlog_setlevel(loglevel);
  37. }
  38. private:
  39. tlog_level loglevel;
  40. };
  41. void ping_result_callback(struct ping_host_struct *ping_host, const char *host, FAST_PING_RESULT result,
  42. struct sockaddr *addr, socklen_t addr_len, int seqno, int ttl, struct timeval *tv, int error,
  43. void *userptr)
  44. {
  45. int *count = (int *)userptr;
  46. if (result == PING_RESULT_RESPONSE) {
  47. *count = 1;
  48. tlog(TLOG_INFO, "ping to %s succeeded, seq=%d, ttl=%d", host, seqno, ttl);
  49. }
  50. }
  51. TEST_F(Ping, icmp)
  52. {
  53. struct ping_host_struct *ping_host;
  54. int count = 0;
  55. if (smartdns::IsICMPAvailable() == false) {
  56. tlog(TLOG_INFO, "ICMP is not available, skip this test.");
  57. GTEST_SKIP();
  58. return;
  59. }
  60. ping_host = fast_ping_start(PING_TYPE_ICMP, "127.0.0.1", 1, 1, 200, ping_result_callback, &count);
  61. ASSERT_NE(ping_host, nullptr);
  62. usleep(10000);
  63. fast_ping_stop(ping_host);
  64. EXPECT_EQ(count, 1);
  65. }
  66. TEST_F(Ping, tcp)
  67. {
  68. struct ping_host_struct *ping_host;
  69. int count = 0;
  70. ping_host = fast_ping_start(PING_TYPE_TCP, "127.0.0.1:1", 1, 1, 200, ping_result_callback, &count);
  71. ASSERT_NE(ping_host, nullptr);
  72. usleep(10000);
  73. fast_ping_stop(ping_host);
  74. EXPECT_EQ(count, 1);
  75. }
  76. TEST_F(Ping, tcp_syn)
  77. {
  78. struct ping_host_struct *ping_host;
  79. int count = 0;
  80. /* Test TCP SYN ping - may not work in all environments */
  81. ping_host = fast_ping_start(PING_TYPE_TCP_SYN, "127.0.0.1:53", 5, 1000, 1000, ping_result_callback, &count);
  82. if (ping_host == nullptr) {
  83. tlog(TLOG_INFO, "TCP SYN ping not available (need root/CAP_NET_RAW), skip this test.");
  84. GTEST_SKIP();
  85. return;
  86. }
  87. usleep(10000);
  88. fast_ping_stop(ping_host);
  89. EXPECT_GT(count, 0);
  90. }
  91. TEST_F(Ping, tcp_syn_v6)
  92. {
  93. struct ping_host_struct *ping_host;
  94. int count = 0;
  95. /* Test TCP SYN ping - may not work in all environments */
  96. ping_host = fast_ping_start(PING_TYPE_TCP_SYN, "[::1]:443", 2, 1000, 1000, ping_result_callback, &count);
  97. if (ping_host == nullptr) {
  98. tlog(TLOG_INFO, "TCP SYN ping not available (need root/CAP_NET_RAW), skip this test.");
  99. GTEST_SKIP();
  100. return;
  101. }
  102. usleep(1000000);
  103. fast_ping_stop(ping_host);
  104. EXPECT_GT(count, 0);
  105. }
  106. TEST_F(Ping, tcp_syn_concurrent)
  107. {
  108. struct ping_host_struct *ping_host1;
  109. struct ping_host_struct *ping_host2;
  110. int count1 = 0, count2 = 0;
  111. /* Test concurrent TCP SYN pings to different servers */
  112. ping_host1 = fast_ping_start(PING_TYPE_TCP_SYN, "127.0.0.1:22", 1, 1, 500, ping_result_callback, &count1);
  113. if (ping_host1 == nullptr) {
  114. tlog(TLOG_INFO, "TCP SYN ping not available, skip this test.");
  115. GTEST_SKIP();
  116. return;
  117. }
  118. tlog(TLOG_INFO, "First ping started, now starting second...");
  119. /* Use Alibaba DNS server (accessible in China) */
  120. ping_host2 = fast_ping_start(PING_TYPE_TCP_SYN, "127.0.0.2:53", 1, 1, 500, ping_result_callback, &count2);
  121. ASSERT_NE(ping_host2, nullptr);
  122. tlog(TLOG_INFO, "Both pings started, waiting 200ms...");
  123. usleep(200000); /* Wait 200ms for responses */
  124. tlog(TLOG_INFO, "Wait complete, stopping pings...");
  125. fast_ping_stop(ping_host1);
  126. fast_ping_stop(ping_host2);
  127. usleep(50000); /* Brief wait for cleanup */
  128. tlog(TLOG_INFO, "TCP SYN concurrent ping test completed, count1=%d, count2=%d", count1, count2);
  129. /* At least one should succeed (localhost SSH should be reachable) */
  130. EXPECT_GE(count1 + count2, 1);
  131. }
  132. void fake_ping_result_callback(struct ping_host_struct *ping_host, const char *host, FAST_PING_RESULT result,
  133. struct sockaddr *addr, socklen_t addr_len, int seqno, int ttl, struct timeval *tv,
  134. int error, void *userptr)
  135. {
  136. if (result == PING_RESULT_RESPONSE) {
  137. int *count = (int *)userptr;
  138. double rtt = tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0;
  139. tlog(TLOG_INFO, "from %15s: seq=%d ttl=%d time=%.3f\n", host, seqno, ttl, rtt);
  140. *count = (int)rtt;
  141. }
  142. }
  143. TEST_F(Ping, fake_icmp)
  144. {
  145. struct ping_host_struct *ping_host;
  146. int count = 0;
  147. fast_ping_fake_ip_add(PING_TYPE_ICMP, "1.2.3.4", 60, 5);
  148. ping_host = fast_ping_start(PING_TYPE_ICMP, "1.2.3.4", 1, 1000, 200, fake_ping_result_callback, &count);
  149. ASSERT_NE(ping_host, nullptr);
  150. usleep(100000);
  151. fast_ping_stop(ping_host);
  152. EXPECT_GE(count, 5);
  153. }