Skip to content

Commit

Permalink
test: add test case for edns.
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Dec 27, 2023
1 parent beff0f0 commit ecaa107
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ static int _dns_server_get_cache_timeout(struct dns_request *request, struct dns
return ttl + 1;
}

if (dns_conf_prefetch && _dns_cache_is_specify_packet(request->qtype) != 0) {
if (dns_conf_prefetch) {
prefetch_time = 1;
}

Expand Down Expand Up @@ -2403,6 +2403,9 @@ static int _dns_server_request_complete_with_all_IPs(struct dns_request *request
context.do_audit = 1;
context.do_reply = 1;
context.reply_ttl = _dns_server_get_reply_ttl(request, ttl);
if (with_all_ips == 0) {
context.cache_ttl = _dns_server_get_reply_ttl(request, ttl);
}
context.skip_notify_count = 1;
context.select_all_best_ip = with_all_ips;
context.no_release_parent = 1;
Expand Down Expand Up @@ -5824,7 +5827,7 @@ static int _dns_server_do_query(struct dns_request *request, int skip_notify_eve
if (_dns_server_process_dns64(request) != 0) {
goto errout;
}

// Get reference for DNS query
request->request_wait++;
_dns_server_request_get(request);
Expand Down Expand Up @@ -7052,7 +7055,7 @@ static dns_cache_tmout_action_t _dns_server_cache_expired(struct dns_cache *dns_
return DNS_CACHE_TMOUT_ACTION_DEL;
}

if (dns_conf_prefetch == 1 && _dns_cache_is_specify_packet(dns_cache->info.qtype) != 0) {
if (dns_conf_prefetch == 1) {
if (dns_conf_serve_expired == 1) {
return _dns_server_prefetch_expired_domain(dns_cache);
} else {
Expand Down
49 changes: 45 additions & 4 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ daemon_ret daemon_run(int *wstatus)
return DAEMON_RET_ERR;
}

#ifdef DEBUG
#if defined(DEBUG) || defined(TEST)
struct _dns_read_packet_info {
int data_len;
int message_len;
Expand Down Expand Up @@ -1922,8 +1922,9 @@ static int _dns_debug_display(struct dns_packet *packet)
struct dns_rrs *rrs = NULL;
int rr_count = 0;
char req_host[MAX_IP_LEN];
int ret;

for (j = 1; j < DNS_RRS_END; j++) {
for (j = 1; j < DNS_RRS_OPT; j++) {
rrs = dns_get_rrs_start(packet, j, &rr_count);
printf("section: %d\n", j);
for (i = 0; i < rr_count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
Expand All @@ -1949,7 +1950,6 @@ static int _dns_debug_display(struct dns_packet *packet)
unsigned short priority = 0;
unsigned short weight = 0;
unsigned short port = 0;
int ret = 0;

char name[DNS_MAX_CNAME_LEN] = {0};
char target[DNS_MAX_CNAME_LEN];
Expand All @@ -1969,7 +1969,6 @@ static int _dns_debug_display(struct dns_packet *packet)
char target[DNS_MAX_CNAME_LEN] = {0};
struct dns_https_param *p = NULL;
int priority = 0;
int ret = 0;

ret = dns_get_HTTPS_svcparm_start(rrs, &p, name, DNS_MAX_CNAME_LEN, &ttl, &priority, target,
DNS_MAX_CNAME_LEN);
Expand Down Expand Up @@ -2068,6 +2067,48 @@ static int _dns_debug_display(struct dns_packet *packet)
printf("\n");
}

rr_count = 0;
rrs = dns_get_rrs_start(packet, DNS_RRS_OPT, &rr_count);
if (rr_count <= 0) {
return 0;
}

printf("section opt:\n");
for (i = 0; i < rr_count && rrs; i++, rrs = dns_get_rrs_next(packet, rrs)) {
switch (rrs->type) {
case DNS_OPT_T_TCP_KEEPALIVE: {
unsigned short idle_timeout = 0;
ret = dns_get_OPT_TCP_KEEPALIVE(rrs, &idle_timeout);
if (idle_timeout == 0) {
continue;
}

printf("tcp keepalive: %d\n", idle_timeout);
} break;
case DNS_OPT_T_ECS: {
struct dns_opt_ecs ecs;
memset(&ecs, 0, sizeof(ecs));
ret = dns_get_OPT_ECS(rrs, &ecs);
if (ret != 0) {
continue;
}
printf("ecs family: %d, src_prefix: %d, scope_prefix: %d, ", ecs.family, ecs.source_prefix,
ecs.scope_prefix);
if (ecs.family == 1) {
char ip[16] = {0};
inet_ntop(AF_INET, ecs.addr, ip, sizeof(ip));
printf("ecs address: %s\n", ip);
} else if (ecs.family == 2) {
char ip[64] = {0};
inet_ntop(AF_INET6, ecs.addr, ip, sizeof(ip));
printf("ecs address: %s\n", ip);
}
} break;
default:
break;
}
}

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion test/cases/test-bind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ server 127.0.0.1:61053
std::cout << client.GetResult() << std::endl;
ASSERT_EQ(client.GetAnswerNum(), 1);
EXPECT_EQ(client.GetStatus(), "NOERROR");
EXPECT_GE(client.GetAnswer()[0].GetTTL(), 609);
EXPECT_GE(client.GetAnswer()[0].GetTTL(), 3);
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
}

Expand Down
2 changes: 1 addition & 1 deletion test/cases/test-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ rr-ttl-reply-max 6
ASSERT_EQ(client.GetAnswerNum(), 1);
EXPECT_EQ(client.GetStatus(), "NOERROR");
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
EXPECT_GE(client.GetAnswer()[0].GetTTL(), 5);
EXPECT_GE(client.GetAnswer()[0].GetTTL(), 3);
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
}

Expand Down
Loading

0 comments on commit ecaa107

Please sign in to comment.