2018-10-11 11:51:13 +00:00
|
|
|
#include <gtest/gtest.h>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-03 13:24:45 +00:00
|
|
|
#include <llarp.h> // for llarp_main_init
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <logic.hpp> // for threadpool/llarp::Logic
|
|
|
|
#include <net.hpp> // for llarp::Addr
|
|
|
|
#include <dnsd.hpp>
|
2018-10-11 11:51:13 +00:00
|
|
|
|
|
|
|
unsigned int g_length = 0;
|
2018-11-07 15:30:22 +00:00
|
|
|
std::string g_result = "";
|
2018-10-11 11:51:13 +00:00
|
|
|
|
2018-11-07 15:30:22 +00:00
|
|
|
ssize_t
|
|
|
|
test_sendto_dns_hook(__attribute__((unused)) void *sock,
|
|
|
|
__attribute__((unused)) const struct sockaddr *from,
|
2018-11-23 14:37:26 +00:00
|
|
|
llarp_buffer_t buf)
|
2018-11-07 15:30:22 +00:00
|
|
|
{
|
2018-11-23 14:37:26 +00:00
|
|
|
char *hex_buffer = new char[buf.sz * 3 + 1];
|
2018-12-03 13:24:45 +00:00
|
|
|
hex_buffer[buf.sz * 3] = 0;
|
2018-11-23 14:37:26 +00:00
|
|
|
for(unsigned int j = 0; j < buf.sz; j++)
|
2018-12-03 13:24:45 +00:00
|
|
|
sprintf(&hex_buffer[3 * j], "%02X ", ((char *)buf.base)[j]);
|
2018-11-07 15:30:22 +00:00
|
|
|
// printf("Got [%zu] bytes: [%s]\n", length, hex_buffer);
|
2018-10-11 11:51:13 +00:00
|
|
|
g_result = hex_buffer;
|
2018-11-23 14:37:26 +00:00
|
|
|
g_length = buf.sz;
|
2018-11-09 12:25:14 +00:00
|
|
|
delete[] hex_buffer;
|
2018-11-23 14:37:26 +00:00
|
|
|
return buf.sz;
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct llarpDNSdTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
dnsd_question_request test_request;
|
2018-11-07 15:30:22 +00:00
|
|
|
|
2018-10-11 11:51:13 +00:00
|
|
|
llarpDNSdTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
2018-12-03 13:24:45 +00:00
|
|
|
test_request.hdr.id = 0;
|
2018-11-07 15:30:22 +00:00
|
|
|
test_request.llarp = true; // we don't care about raw atm
|
|
|
|
test_request.from = nullptr;
|
|
|
|
test_request.context = nullptr;
|
|
|
|
test_request.sendto_hook = &test_sendto_dns_hook;
|
2018-10-11 11:51:13 +00:00
|
|
|
test_request.question.name = "loki.network";
|
|
|
|
test_request.question.type = 1;
|
|
|
|
test_request.question.qClass = 1;
|
2018-11-07 15:30:22 +00:00
|
|
|
g_result = ""; // reset test global
|
|
|
|
g_length = 0;
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestNxDomain)
|
|
|
|
{
|
2018-11-20 19:51:14 +00:00
|
|
|
write404_dnss_response(&test_request);
|
2018-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(g_length == 55);
|
2018-11-07 15:30:22 +00:00
|
|
|
std::string expected_output =
|
|
|
|
"00 00 FFF03 00 01 00 01 00 00 00 00 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 "
|
|
|
|
"6B 00 00 01 00 01 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 6B 00 00 01 00 01 "
|
|
|
|
"00 00 00 01 00 01 00 ";
|
2018-12-03 13:24:45 +00:00
|
|
|
// TODO: rewrite this test
|
|
|
|
// ASSERT_TRUE(expected_output == g_result);
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestAResponse)
|
|
|
|
{
|
2018-12-03 13:24:45 +00:00
|
|
|
llarp::huint32_t hostRes = {0};
|
2018-11-07 15:30:22 +00:00
|
|
|
// sockaddr hostRes;
|
|
|
|
// llarp::Zero(&hostRes, sizeof(sockaddr));
|
2018-11-20 19:51:14 +00:00
|
|
|
writesend_dnss_response(&hostRes, &test_request);
|
2018-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(g_length == 58);
|
2018-11-07 15:30:22 +00:00
|
|
|
std::string expected_output =
|
|
|
|
"00 00 FFF00 00 01 00 01 00 00 00 00 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 "
|
|
|
|
"6B 00 00 01 00 01 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 6B 00 00 01 00 01 "
|
|
|
|
"00 00 00 01 00 04 00 00 00 00 ";
|
2018-12-03 13:24:45 +00:00
|
|
|
// TODO: rewrite this test
|
|
|
|
// ASSERT_TRUE(expected_output == g_result);
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestPTRResponse)
|
|
|
|
{
|
2018-11-20 19:51:14 +00:00
|
|
|
writesend_dnss_revresponse("loki.network", &test_request);
|
2018-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(g_length == 68);
|
2018-11-07 15:30:22 +00:00
|
|
|
std::string expected_output =
|
|
|
|
"00 00 FFF00 00 01 00 01 00 00 00 00 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 "
|
|
|
|
"6B 00 00 01 00 01 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 6B 00 00 01 00 01 "
|
|
|
|
"00 00 00 01 00 0E 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 6B 00 ";
|
2018-12-03 13:24:45 +00:00
|
|
|
// TODO: rewrite this test
|
|
|
|
// ASSERT_TRUE(expected_output == g_result);
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestCname)
|
|
|
|
{
|
2018-11-20 19:51:14 +00:00
|
|
|
writecname_dnss_response("test.cname", &test_request);
|
2018-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(g_length == 122);
|
2018-11-07 15:30:22 +00:00
|
|
|
std::string expected_output =
|
|
|
|
"00 00 FFF00 00 01 00 01 00 01 00 01 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 "
|
|
|
|
"6B 00 00 01 00 01 04 6C 6F 6B 69 07 6E 65 74 77 6F 72 6B 00 00 05 00 01 "
|
|
|
|
"00 00 00 01 00 0C 04 74 65 73 74 05 63 6E 61 6D 65 00 04 74 65 73 74 05 "
|
|
|
|
"63 6E 61 6D 65 00 00 02 00 01 00 00 00 01 00 0A 03 6E 73 31 04 6C 6F 6B "
|
|
|
|
"69 00 03 6E 73 31 04 6C 6F 6B 69 00 00 01 00 01 00 00 00 01 00 04 7F 00 "
|
|
|
|
"00 01 ";
|
2018-12-03 13:24:45 +00:00
|
|
|
// TODO: rewrite this test
|
|
|
|
// ASSERT_TRUE(expected_output == g_result);
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|