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