test-mock-server.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2024 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 "include/utils.h"
  20. #include "server.h"
  21. #include "gtest/gtest.h"
  22. TEST(MockServer, query_fail)
  23. {
  24. smartdns::MockServer server;
  25. smartdns::Client client;
  26. server.Start("udp://0.0.0.0:7053", [](struct smartdns::ServerRequestContext *request) {
  27. request->response_data_len = 0;
  28. return smartdns::SERVER_REQUEST_ERROR;
  29. });
  30. ASSERT_TRUE(client.Query("example.com", 7053));
  31. std::cout << client.GetResult() << std::endl;
  32. EXPECT_EQ(client.GetStatus(), "SERVFAIL");
  33. }
  34. TEST(MockServer, soa)
  35. {
  36. smartdns::MockServer server;
  37. smartdns::Client client;
  38. server.Start("udp://0.0.0.0:7053",
  39. [](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_SOA; });
  40. ASSERT_TRUE(client.Query("example.com", 7053));
  41. std::cout << client.GetResult() << std::endl;
  42. EXPECT_EQ(client.GetStatus(), "NXDOMAIN");
  43. }
  44. TEST(MockServer, noerror)
  45. {
  46. smartdns::MockServer server;
  47. smartdns::Client client;
  48. server.Start("udp://0.0.0.0:7053",
  49. [](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_OK; });
  50. ASSERT_TRUE(client.Query("example.com", 7053));
  51. std::cout << client.GetResult() << std::endl;
  52. EXPECT_EQ(client.GetStatus(), "NOERROR");
  53. }