test-ping.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. *count = 1;
  47. }
  48. TEST_F(Ping, icmp)
  49. {
  50. struct ping_host_struct *ping_host;
  51. int count = 0;
  52. if (smartdns::IsICMPAvailable() == false) {
  53. tlog(TLOG_INFO, "ICMP is not available, skip this test.");
  54. GTEST_SKIP();
  55. return;
  56. }
  57. ping_host = fast_ping_start(PING_TYPE_ICMP, "127.0.0.1", 1, 1, 200, ping_result_callback, &count);
  58. ASSERT_NE(ping_host, nullptr);
  59. usleep(10000);
  60. fast_ping_stop(ping_host);
  61. EXPECT_EQ(count, 1);
  62. }
  63. TEST_F(Ping, tcp)
  64. {
  65. struct ping_host_struct *ping_host;
  66. int count = 0;
  67. ping_host = fast_ping_start(PING_TYPE_TCP, "127.0.0.1:1", 1, 1, 200, ping_result_callback, &count);
  68. ASSERT_NE(ping_host, nullptr);
  69. usleep(10000);
  70. fast_ping_stop(ping_host);
  71. EXPECT_EQ(count, 1);
  72. }
  73. void fake_ping_result_callback(struct ping_host_struct *ping_host, const char *host, FAST_PING_RESULT result,
  74. struct sockaddr *addr, socklen_t addr_len, int seqno, int ttl, struct timeval *tv,
  75. int error, void *userptr)
  76. {
  77. if (result == PING_RESULT_RESPONSE) {
  78. int *count = (int *)userptr;
  79. double rtt = tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0;
  80. tlog(TLOG_INFO, "from %15s: seq=%d ttl=%d time=%.3f\n", host, seqno, ttl, rtt);
  81. *count = (int)rtt;
  82. }
  83. }
  84. TEST_F(Ping, fake_icmp)
  85. {
  86. struct ping_host_struct *ping_host;
  87. int count = 0;
  88. fast_ping_fake_ip_add(PING_TYPE_ICMP, "1.2.3.4", 60, 5);
  89. ping_host = fast_ping_start(PING_TYPE_ICMP, "1.2.3.4", 1, 1000, 200, fake_ping_result_callback, &count);
  90. ASSERT_NE(ping_host, nullptr);
  91. usleep(100000);
  92. fast_ping_stop(ping_host);
  93. EXPECT_GE(count, 5);
  94. }