test-audit.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "include/utils.h"
  20. #include "server.h"
  21. #include "smartdns/dns.h"
  22. #include "gtest/gtest.h"
  23. class Audit : public ::testing::Test
  24. {
  25. protected:
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(Audit, log)
  30. {
  31. smartdns::MockServer server_upstream;
  32. smartdns::Server server;
  33. Defer
  34. {
  35. unlink("/tmp/audit.log");
  36. };
  37. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  38. if (request->qtype != DNS_T_A) {
  39. return smartdns::SERVER_REQUEST_SOA;
  40. }
  41. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  42. return smartdns::SERVER_REQUEST_OK;
  43. });
  44. server.Start(R"""(bind [::]:60053
  45. audit-file /tmp/audit.log
  46. audit-enable yes
  47. server 127.0.0.1:61053
  48. )""");
  49. smartdns::Client client;
  50. ASSERT_TRUE(client.Query("a.com", 60053));
  51. std::cout << client.GetResult() << std::endl;
  52. ASSERT_EQ(client.GetAnswerNum(), 1);
  53. EXPECT_EQ(client.GetStatus(), "NOERROR");
  54. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  55. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  56. std::ifstream audit_file("/tmp/audit.log");
  57. std::string line;
  58. bool found = false;
  59. while (std::getline(audit_file, line)) {
  60. if (line.find("a.com") != std::string::npos) {
  61. found = true;
  62. break;
  63. }
  64. }
  65. audit_file.close();
  66. ASSERT_TRUE(found) << "Audit log for a.com not found";
  67. }