Skip to content

Commit

Permalink
Merge pull request #320 from pymumu/master
Browse files Browse the repository at this point in the history
Fork Sync
  • Loading branch information
PikuZheng authored Aug 18, 2024
2 parents 4dabeb6 + d248f6d commit a835feb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
68 changes: 68 additions & 0 deletions test/cases/test-client-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
12 changes: 6 additions & 6 deletions test/cases/test-mock-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}

0 comments on commit a835feb

Please sign in to comment.