test: add test case for cache and fix some issue
This commit is contained in:
@@ -84,7 +84,7 @@ cache-persist no)""");
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 611);
|
||||
EXPECT_GT(client.GetAnswer()[0].GetTTL(), 609);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
|
||||
175
test/cases/test-cache.cc
Normal file
175
test/cases/test-cache.cc
Normal file
@@ -0,0 +1,175 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* Copyright (C) 2018-2023 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||
*
|
||||
* smartdns is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* smartdns is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "client.h"
|
||||
#include "dns.h"
|
||||
#include "include/utils.h"
|
||||
#include "server.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class Cache : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() override {}
|
||||
|
||||
void TearDown() override {}
|
||||
};
|
||||
|
||||
TEST_F(Cache, min)
|
||||
{
|
||||
smartdns::MockServer server_upstream;
|
||||
smartdns::Server server;
|
||||
|
||||
server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
|
||||
std::string domain = request->domain;
|
||||
if (request->domain.length() == 0) {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
if (request->qtype == DNS_T_A) {
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
|
||||
} else if (request->qtype == DNS_T_AAAA) {
|
||||
unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
||||
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
|
||||
} else {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server.Start(R"""(bind [::]:60053
|
||||
server 127.0.0.1:61053
|
||||
log-num 0
|
||||
cache-size 1
|
||||
rr-ttl-min 1
|
||||
speed-check-mode none
|
||||
response-mode fastest-response
|
||||
log-console yes
|
||||
log-level debug
|
||||
cache-persist no)""");
|
||||
smartdns::Client client;
|
||||
ASSERT_TRUE(client.Query("a.com", 60053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 1);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
TEST_F(Cache, max_reply_ttl)
|
||||
{
|
||||
smartdns::MockServer server_upstream;
|
||||
smartdns::Server server;
|
||||
|
||||
server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
|
||||
std::string domain = request->domain;
|
||||
if (request->domain.length() == 0) {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
if (request->qtype == DNS_T_A) {
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
|
||||
} else if (request->qtype == DNS_T_AAAA) {
|
||||
unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
||||
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
|
||||
} else {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server.Start(R"""(bind [::]:60053
|
||||
server 127.0.0.1:61053
|
||||
log-num 0
|
||||
cache-size 1
|
||||
rr-ttl-min 600
|
||||
rr-ttl-reply-max 5
|
||||
speed-check-mode none
|
||||
response-mode fastest-response
|
||||
log-console yes
|
||||
log-level debug
|
||||
cache-persist no)""");
|
||||
smartdns::Client client;
|
||||
ASSERT_TRUE(client.Query("a.com", 60053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 5);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
TEST_F(Cache, max_reply_ttl_expired)
|
||||
{
|
||||
smartdns::MockServer server_upstream;
|
||||
smartdns::Server server;
|
||||
|
||||
server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
|
||||
std::string domain = request->domain;
|
||||
if (request->domain.length() == 0) {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
if (request->qtype == DNS_T_A) {
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
|
||||
} else if (request->qtype == DNS_T_AAAA) {
|
||||
unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
||||
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
|
||||
} else {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server.Start(R"""(bind [::]:60053
|
||||
server 127.0.0.1:61053
|
||||
log-num 0
|
||||
cache-size 1
|
||||
rr-ttl-min 600
|
||||
rr-ttl-reply-max 5
|
||||
log-console yes
|
||||
log-level debug
|
||||
cache-persist no)""");
|
||||
smartdns::Client client;
|
||||
ASSERT_TRUE(client.Query("a.com", 60053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
|
||||
ASSERT_TRUE(client.Query("a.com", 60053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 5);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ TEST(DiscardBlockIP, first_ping)
|
||||
smartdns::Server server;
|
||||
|
||||
server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
|
||||
if (request->qtype != DNS_T_A) {
|
||||
return smartdns::SERVER_REQUEST_SOA;
|
||||
}
|
||||
|
||||
unsigned char addr[4] = {0, 0, 0, 0};
|
||||
dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
@@ -36,6 +40,9 @@ TEST(DiscardBlockIP, first_ping)
|
||||
});
|
||||
|
||||
server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
|
||||
if (request->qtype != DNS_T_A) {
|
||||
return smartdns::SERVER_REQUEST_SOA;
|
||||
}
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
usleep(20000);
|
||||
dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
|
||||
@@ -55,7 +62,8 @@ cache-persist no)""");
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
|
||||
EXPECT_LT(client.GetQueryTime(), 100);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ class Perf : public ::testing::Test
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
TEST(Perf, no_speed_check)
|
||||
TEST_F(Perf, no_speed_check)
|
||||
{
|
||||
smartdns::MockServer server_upstream;
|
||||
smartdns::Server server;
|
||||
|
||||
@@ -50,7 +50,7 @@ void ping_result_callback(struct ping_host_struct *ping_host, const char *host,
|
||||
*count = 1;
|
||||
}
|
||||
|
||||
TEST_F(Ping, DISABLED_icmp)
|
||||
TEST_F(Ping, icmp)
|
||||
{
|
||||
struct ping_host_struct *ping_host;
|
||||
int count = 0;
|
||||
@@ -61,7 +61,7 @@ TEST_F(Ping, DISABLED_icmp)
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
TEST_F(Ping, DISABLED_tcp)
|
||||
TEST_F(Ping, tcp)
|
||||
{
|
||||
struct ping_host_struct *ping_host;
|
||||
int count = 0;
|
||||
|
||||
96
test/cases/test-same-pending-query.cc
Normal file
96
test/cases/test-same-pending-query.cc
Normal file
@@ -0,0 +1,96 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* Copyright (C) 2018-2023 Ruilin Peng (Nick) <pymumu@gmail.com>.
|
||||
*
|
||||
* smartdns is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* smartdns is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "client.h"
|
||||
#include "dns.h"
|
||||
#include "include/utils.h"
|
||||
#include "server.h"
|
||||
#include "util.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include <fstream>
|
||||
|
||||
class SamePending : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp() {}
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
TEST_F(SamePending, pending)
|
||||
{
|
||||
smartdns::MockServer server_upstream;
|
||||
smartdns::Server server;
|
||||
std::map<int, int> qid_map;
|
||||
|
||||
server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
|
||||
std::string domain = request->domain;
|
||||
if (qid_map.find(request->packet->head.id) != qid_map.end()) {
|
||||
qid_map[request->packet->head.id]++;
|
||||
usleep(5000);
|
||||
} else {
|
||||
qid_map[request->packet->head.id] = 1;
|
||||
usleep(20000);
|
||||
}
|
||||
|
||||
if (request->domain.length() == 0) {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
if (request->qtype == DNS_T_A) {
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
|
||||
} else if (request->qtype == DNS_T_AAAA) {
|
||||
unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
|
||||
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
|
||||
} else {
|
||||
return smartdns::SERVER_REQUEST_ERROR;
|
||||
}
|
||||
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server.Start(R"""(bind [::]:60053
|
||||
server 127.0.0.1:61053
|
||||
cache-size 0
|
||||
log-num 0
|
||||
log-console yes
|
||||
speed-check-mode none
|
||||
log-level error
|
||||
cache-persist no)""");
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
uint64_t tick = get_tick_count();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
auto t = std::thread([&]() {
|
||||
for (int j = 0; j < 10; j++) {
|
||||
smartdns::Client client;
|
||||
ASSERT_TRUE(client.Query("a.com", 60053));
|
||||
ASSERT_EQ(client.GetAnswerNum(), 1);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
});
|
||||
threads.push_back(std::move(t));
|
||||
}
|
||||
|
||||
for (auto &t : threads) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
@@ -125,10 +125,10 @@ bool Client::Query(const std::string &dig_cmds, int port, const std::string &ip)
|
||||
|
||||
cmd += " " + dig_cmds;
|
||||
cmd += " +tries=1";
|
||||
FILE *fp = NULL;
|
||||
FILE *fp = nullptr;
|
||||
|
||||
fp = popen(cmd.c_str(), "r");
|
||||
if (fp == NULL) {
|
||||
if (fp == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ void MockServer::Run()
|
||||
request.packet = packet;
|
||||
query_id = packet->head.id;
|
||||
if (packet->head.qr == DNS_QR_QUERY) {
|
||||
struct dns_rrs *rrs = NULL;
|
||||
struct dns_rrs *rrs = nullptr;
|
||||
int rr_count = 0;
|
||||
int qtype = 0;
|
||||
int qclass = 0;
|
||||
@@ -213,7 +213,7 @@ bool MockServer::GetAddr(const std::string &host, const std::string port, int ty
|
||||
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo *result = nullptr;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
@@ -231,7 +231,8 @@ errout:
|
||||
if (result) {
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
return NULL;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MockServer::Start(const std::string &url, ServerRequest callback)
|
||||
@@ -378,7 +379,7 @@ void Server::Stop(bool graceful)
|
||||
}
|
||||
}
|
||||
|
||||
waitpid(pid_, NULL, 0);
|
||||
waitpid(pid_, nullptr, 0);
|
||||
|
||||
pid_ = 0;
|
||||
if (clean_conf_file_ == true) {
|
||||
@@ -394,7 +395,7 @@ bool Server::IsRunning()
|
||||
return false;
|
||||
}
|
||||
|
||||
if (waitpid(pid_, NULL, WNOHANG) == 0) {
|
||||
if (waitpid(pid_, nullptr, WNOHANG) == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,17 +9,17 @@ namespace smartdns
|
||||
|
||||
bool IsCommandExists(const std::string &cmd)
|
||||
{
|
||||
char *copy_path = NULL;
|
||||
char *copy_path = nullptr;
|
||||
char cmd_path[4096];
|
||||
const char *env_path = getenv("PATH");
|
||||
char *save_ptr = NULL;
|
||||
char *save_ptr = nullptr;
|
||||
|
||||
if (env_path == NULL) {
|
||||
if (env_path == nullptr) {
|
||||
env_path = "/bin:/usr/bin:/usr/local/bin";
|
||||
}
|
||||
|
||||
copy_path = strdup(env_path);
|
||||
if (copy_path == NULL) {
|
||||
if (copy_path == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ bool IsCommandExists(const std::string &cmd)
|
||||
free(copy_path);
|
||||
};
|
||||
|
||||
for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(NULL, ":", &save_ptr)) {
|
||||
for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(nullptr, ":", &save_ptr)) {
|
||||
snprintf(cmd_path, sizeof(cmd_path), "%s/%s", tok, cmd.c_str());
|
||||
if (access(cmd_path, X_OK) != 0) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user