test: add test case for performance
This commit is contained in:
@@ -1,3 +1,21 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* 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 "include/utils.h"
|
||||
#include "server.h"
|
||||
|
||||
@@ -1,35 +1,53 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* 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"
|
||||
#include "dns.h"
|
||||
|
||||
TEST(server, cname)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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(), 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
EXPECT_EQ(domain, "e.com");
|
||||
EXPECT_EQ(domain, "e.com");
|
||||
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server.Start(R"""(bind [::]:60053
|
||||
cname /a.com/b.com
|
||||
@@ -43,10 +61,10 @@ log-level debug
|
||||
cache-persist no)""");
|
||||
smartdns::Client client;
|
||||
ASSERT_TRUE(client.Query("a.com", 60053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
ASSERT_EQ(client.GetAnswerNum(), 2);
|
||||
EXPECT_EQ(client.GetStatus(), "NOERROR");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "b.com.");
|
||||
EXPECT_EQ(client.GetAnswer()[1].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* 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"
|
||||
@@ -6,20 +24,20 @@
|
||||
|
||||
TEST(DiscardBlockIP, first_ping)
|
||||
{
|
||||
smartdns::MockServer server_upstream1;
|
||||
smartdns::MockServer server_upstream1;
|
||||
smartdns::MockServer server_upstream2;
|
||||
smartdns::Server server;
|
||||
|
||||
server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
|
||||
unsigned char addr[4] = {0, 0, 0, 0};
|
||||
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;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
usleep(20000);
|
||||
server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
|
||||
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);
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
@@ -37,26 +55,26 @@ 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_EQ(client.GetAnswer()[0].GetTTL(), 600);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
|
||||
TEST(DiscardBlockIP, first_response)
|
||||
{
|
||||
smartdns::MockServer server_upstream1;
|
||||
smartdns::MockServer server_upstream1;
|
||||
smartdns::MockServer server_upstream2;
|
||||
smartdns::Server server;
|
||||
|
||||
server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
|
||||
unsigned char addr[4] = {0, 0, 0, 0};
|
||||
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;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
|
||||
server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
|
||||
unsigned char addr[4] = {1, 2, 3, 4};
|
||||
usleep(20000);
|
||||
server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
|
||||
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);
|
||||
request->response_packet->head.rcode = DNS_RC_NOERROR;
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
@@ -75,6 +93,6 @@ 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(), 3);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
|
||||
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
|
||||
}
|
||||
@@ -1,3 +1,21 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* 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 "include/utils.h"
|
||||
#include "server.h"
|
||||
@@ -21,9 +39,8 @@ TEST(MockServer, soa)
|
||||
{
|
||||
smartdns::MockServer server;
|
||||
smartdns::Client client;
|
||||
server.Start("udp://0.0.0.0:7053", [](struct smartdns::ServerRequestContext *request) {
|
||||
return smartdns::SERVER_REQUEST_SOA;
|
||||
});
|
||||
server.Start("udp://0.0.0.0:7053",
|
||||
[](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_SOA; });
|
||||
|
||||
ASSERT_TRUE(client.Query("example.com", 7053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
@@ -34,9 +51,8 @@ TEST(MockServer, noerror)
|
||||
{
|
||||
smartdns::MockServer server;
|
||||
smartdns::Client client;
|
||||
server.Start("udp://0.0.0.0:7053", [](struct smartdns::ServerRequestContext *request) {
|
||||
return smartdns::SERVER_REQUEST_OK;
|
||||
});
|
||||
server.Start("udp://0.0.0.0:7053",
|
||||
[](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_OK; });
|
||||
|
||||
ASSERT_TRUE(client.Query("example.com", 7053));
|
||||
std::cout << client.GetResult() << std::endl;
|
||||
|
||||
99
test/cases/test-perf.cc
Normal file
99
test/cases/test-perf.cc
Normal file
@@ -0,0 +1,99 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* 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"
|
||||
#include <fstream>
|
||||
|
||||
class Perf : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp() {}
|
||||
virtual void TearDown() {}
|
||||
};
|
||||
|
||||
TEST(Perf, no_speed_check)
|
||||
{
|
||||
smartdns::MockServer server_upstream;
|
||||
smartdns::Server server;
|
||||
if (smartdns::IsCommandExists("dnsperf") == false) {
|
||||
printf("dnsperf not found, skip test, please install dnsperf first.\n");
|
||||
GTEST_SKIP();
|
||||
}
|
||||
|
||||
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(), 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
|
||||
log-num 0
|
||||
log-console yes
|
||||
speed-check-mode none
|
||||
log-level error
|
||||
cache-persist no)""");
|
||||
std::string file = "/tmp/smartdns-perftest-domain.list" + smartdns::GenerateRandomString(5);
|
||||
std::string cmd = "dnsperf -p 60053";
|
||||
cmd += " -d ";
|
||||
cmd += file;
|
||||
std::ofstream ofs(file);
|
||||
ASSERT_TRUE(ofs.is_open());
|
||||
Defer
|
||||
{
|
||||
ofs.close();
|
||||
unlink(file.c_str());
|
||||
};
|
||||
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
std::string domain = smartdns::GenerateRandomString(10);
|
||||
domain += ".";
|
||||
domain += smartdns::GenerateRandomString(3);
|
||||
|
||||
if (random() % 2 == 0) {
|
||||
domain += " A";
|
||||
} else {
|
||||
domain += " AAAA";
|
||||
}
|
||||
|
||||
domain += "\n";
|
||||
|
||||
ofs.write(domain.c_str(), domain.length());
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
system(cmd.c_str());
|
||||
}
|
||||
@@ -1,9 +1,27 @@
|
||||
/*************************************************************************
|
||||
*
|
||||
* 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 "fast_ping.h"
|
||||
#include "include/utils.h"
|
||||
#include "server.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "tlog.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class Ping : public ::testing::Test
|
||||
{
|
||||
@@ -11,16 +29,17 @@ class Ping : public ::testing::Test
|
||||
virtual void SetUp()
|
||||
{
|
||||
EXPECT_EQ(fast_ping_init(), 0);
|
||||
loglevel = tlog_getlevel();
|
||||
tlog_setlevel(TLOG_DEBUG);
|
||||
loglevel = tlog_getlevel();
|
||||
tlog_setlevel(TLOG_DEBUG);
|
||||
}
|
||||
virtual void TearDown()
|
||||
{
|
||||
fast_ping_exit();
|
||||
tlog_setlevel(loglevel);
|
||||
tlog_setlevel(loglevel);
|
||||
}
|
||||
private:
|
||||
tlog_level loglevel;
|
||||
|
||||
private:
|
||||
tlog_level loglevel;
|
||||
};
|
||||
|
||||
void ping_result_callback(struct ping_host_struct *ping_host, const char *host, FAST_PING_RESULT result,
|
||||
@@ -38,8 +57,8 @@ TEST_F(Ping, DISABLED_icmp)
|
||||
ping_host = fast_ping_start(PING_TYPE_ICMP, "127.0.0.1", 1, 1, 200, ping_result_callback, &count);
|
||||
ASSERT_NE(ping_host, nullptr);
|
||||
usleep(10000);
|
||||
fast_ping_stop(ping_host);
|
||||
EXPECT_EQ(count, 1);
|
||||
fast_ping_stop(ping_host);
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
TEST_F(Ping, DISABLED_tcp)
|
||||
@@ -49,6 +68,6 @@ TEST_F(Ping, DISABLED_tcp)
|
||||
ping_host = fast_ping_start(PING_TYPE_TCP, "127.0.0.1:1", 1, 1, 200, ping_result_callback, &count);
|
||||
ASSERT_NE(ping_host, nullptr);
|
||||
usleep(10000);
|
||||
fast_ping_stop(ping_host);
|
||||
EXPECT_EQ(count, 1);
|
||||
fast_ping_stop(ping_host);
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user