test-ping.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 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 "fast_ping.h"
  20. #include "include/utils.h"
  21. #include "server.h"
  22. #include "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, DISABLED_icmp)
  49. {
  50. struct ping_host_struct *ping_host;
  51. int count = 0;
  52. ping_host = fast_ping_start(PING_TYPE_ICMP, "127.0.0.1", 1, 1, 200, ping_result_callback, &count);
  53. ASSERT_NE(ping_host, nullptr);
  54. usleep(10000);
  55. fast_ping_stop(ping_host);
  56. EXPECT_EQ(count, 1);
  57. }
  58. TEST_F(Ping, DISABLED_tcp)
  59. {
  60. struct ping_host_struct *ping_host;
  61. int count = 0;
  62. ping_host = fast_ping_start(PING_TYPE_TCP, "127.0.0.1:1", 1, 1, 200, ping_result_callback, &count);
  63. ASSERT_NE(ping_host, nullptr);
  64. usleep(10000);
  65. fast_ping_stop(ping_host);
  66. EXPECT_EQ(count, 1);
  67. }