From d248f6d1408d22ea8920f7036572ba3b6ae93674 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Sun, 18 Aug 2024 08:59:45 +0800 Subject: [PATCH] dns_server: fix nameserver not working in config group --- src/dns_server.c | 2 +- test/cases/test-client-rule.cc | 68 ++++++++++++++++++++++++++++++++++ test/cases/test-mock-server.cc | 12 +++--- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/dns_server.c b/src/dns_server.c index 92cd09451d..e65b100a11 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -6871,7 +6871,7 @@ static void _dns_server_setup_dns_group_name(struct dns_request *request, const group_name = temp_group_name; } - if (request->dns_group_name[0] != '\0') { + if (request->dns_group_name[0] != '\0' && group_name == NULL) { group_name = request->dns_group_name; } else { safe_strncpy(request->dns_group_name, group_name, sizeof(request->dns_group_name)); diff --git a/test/cases/test-client-rule.cc b/test/cases/test-client-rule.cc index b5072841c3..be80e6fb76 100644 --- a/test/cases/test-client-rule.cc +++ b/test/cases/test-client-rule.cc @@ -144,3 +144,71 @@ acl-enable yes EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com"); EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); } + + +TEST_F(ClientRule, in_group_nameserver) +{ + smartdns::MockServer server_upstream; + smartdns::MockServer server_upstream2; + smartdns::MockServer server_upstream3; + smartdns::Server server; + + server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { + if (request->qtype != DNS_T_A) { + return smartdns::SERVER_REQUEST_SOA; + } + + smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611); + return smartdns::SERVER_REQUEST_OK; + }); + + server_upstream2.Start("udp://0.0.0.0:62053", + [](struct smartdns::ServerRequestContext *request) { + if (request->qtype != DNS_T_A) { + return smartdns::SERVER_REQUEST_SOA; + } + + smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 611); + return smartdns::SERVER_REQUEST_OK; + }); + + server_upstream3.Start("udp://0.0.0.0:63053", + [](struct smartdns::ServerRequestContext *request) { + if (request->qtype != DNS_T_A) { + return smartdns::SERVER_REQUEST_SOA; + } + + usleep(10000); + smartdns::MockServer::AddIP(request, request->domain.c_str(), "7.8.9.10", 611); + return smartdns::SERVER_REQUEST_OK; + }); + + /* this ip will be discard, but is reachable */ + server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 10); + server.MockPing(PING_TYPE_ICMP, "4.5.6.7", 60, 10); + server.MockPing(PING_TYPE_ICMP, "7.8.9.10", 60, 10); + + server.Start(R"""(bind [::]:60053 +server udp://127.0.0.1:62053 +server udp://127.0.0.1:63053 -g in_group +group-begin client +server udp://127.0.0.1:61053 -e +client-rules 127.0.0.1 +nameserver /cn/in_group +group-end +)"""); + smartdns::Client client; + ASSERT_TRUE(client.Query("b.com", 60053)); + std::cout << client.GetResult() << std::endl; + ASSERT_EQ(client.GetAnswerNum(), 1); + EXPECT_EQ(client.GetStatus(), "NOERROR"); + EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com"); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); + + ASSERT_TRUE(client.Query("b.cn", 60053)); + std::cout << client.GetResult() << std::endl; + ASSERT_EQ(client.GetAnswerNum(), 1); + EXPECT_EQ(client.GetStatus(), "NOERROR"); + EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.cn"); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "7.8.9.10"); +} \ No newline at end of file diff --git a/test/cases/test-mock-server.cc b/test/cases/test-mock-server.cc index e996cbabcc..b715fdc391 100644 --- a/test/cases/test-mock-server.cc +++ b/test/cases/test-mock-server.cc @@ -25,12 +25,12 @@ TEST(MockServer, query_fail) { smartdns::MockServer server; smartdns::Client client; - server.Start("udp://0.0.0.0:7053", [](struct smartdns::ServerRequestContext *request) { + server.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { request->response_data_len = 0; return smartdns::SERVER_REQUEST_ERROR; }); - ASSERT_TRUE(client.Query("example.com", 7053)); + ASSERT_TRUE(client.Query("example.com", 61053)); std::cout << client.GetResult() << std::endl; EXPECT_EQ(client.GetStatus(), "SERVFAIL"); } @@ -39,10 +39,10 @@ TEST(MockServer, soa) { smartdns::MockServer server; smartdns::Client client; - server.Start("udp://0.0.0.0:7053", + server.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_SOA; }); - ASSERT_TRUE(client.Query("example.com", 7053)); + ASSERT_TRUE(client.Query("example.com", 61053)); std::cout << client.GetResult() << std::endl; EXPECT_EQ(client.GetStatus(), "NXDOMAIN"); } @@ -51,10 +51,10 @@ TEST(MockServer, noerror) { smartdns::MockServer server; smartdns::Client client; - server.Start("udp://0.0.0.0:7053", + server.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_OK; }); - ASSERT_TRUE(client.Query("example.com", 7053)); + ASSERT_TRUE(client.Query("example.com", 61053)); std::cout << client.GetResult() << std::endl; EXPECT_EQ(client.GetStatus(), "NOERROR"); }