2018-10-11 11:51:13 +00:00
|
|
|
#include <gtest/gtest.h>
|
2018-11-07 15:30:22 +00:00
|
|
|
#include <llarp.h> // for llarp_main_init
|
|
|
|
#include <llarp/logic.h> // for threadpool/llarp_logic
|
2018-10-11 11:51:13 +00:00
|
|
|
#include "llarp/net.hpp" // for llarp::Addr
|
|
|
|
#include "llarp/dnsd.hpp"
|
|
|
|
|
|
|
|
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,
|
|
|
|
const void *buffer, size_t length)
|
|
|
|
{
|
2018-11-09 12:25:14 +00:00
|
|
|
char *hex_buffer = new char[length * 3 + 1];
|
2018-10-11 11:51:13 +00:00
|
|
|
hex_buffer[length * 3] = 0;
|
|
|
|
for(unsigned int j = 0; j < length; j++)
|
|
|
|
sprintf(&hex_buffer[3 * j], "%02X ", ((const char *)buffer)[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;
|
|
|
|
g_length = length;
|
2018-11-09 12:25:14 +00:00
|
|
|
delete[] hex_buffer;
|
2018-10-11 11:51:13 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-07 15:30:22 +00:00
|
|
|
test_request.id = 0;
|
|
|
|
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;
|
|
|
|
llarp::SetLogLevel(
|
|
|
|
llarp::eLogNone); // turn off logging to keep gtest output pretty
|
2018-10-11 11:51:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestNxDomain)
|
|
|
|
{
|
|
|
|
write404_dnss_response(nullptr, &test_request);
|
|
|
|
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-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(expected_output == g_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestAResponse)
|
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::huint32_t hostRes;
|
|
|
|
llarp::Zero(&hostRes.h, sizeof(uint32_t));
|
2018-11-07 15:30:22 +00:00
|
|
|
// sockaddr hostRes;
|
|
|
|
// llarp::Zero(&hostRes, sizeof(sockaddr));
|
2018-10-11 11:51:13 +00:00
|
|
|
writesend_dnss_response(&hostRes, nullptr, &test_request);
|
|
|
|
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-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(expected_output == g_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestPTRResponse)
|
|
|
|
{
|
2018-11-07 15:30:22 +00:00
|
|
|
writesend_dnss_revresponse("loki.network", nullptr, &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-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(expected_output == g_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(llarpDNSdTest, TestCname)
|
|
|
|
{
|
|
|
|
writecname_dnss_response("test.cname", nullptr, &test_request);
|
|
|
|
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-10-11 11:51:13 +00:00
|
|
|
ASSERT_TRUE(expected_output == g_result);
|
|
|
|
}
|