Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/test/dns_tests.cpp
Show All 9 Lines | |||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
BOOST_AUTO_TEST_SUITE(dns_tests) | BOOST_AUTO_TEST_SUITE(dns_tests) | ||||
static const int MAX_QUERY_NAME_LENGTH = 255; | static const int MAX_QUERY_NAME_LENGTH = 255; | ||||
// Max size of the null-terminated buffer parse_name() writes to. | // Max size of the null-terminated buffer parse_name() writes to. | ||||
static const int MAX_QUERY_NAME_BUFFER_LENGTH = MAX_QUERY_NAME_LENGTH + 1; | static const int MAX_QUERY_NAME_BUFFER_LENGTH = MAX_QUERY_NAME_LENGTH + 1; | ||||
static const uint8_t END_OF_NAME_FIELD = 0; | static const uint8_t END_OF_NAME_FIELD = 0; | ||||
static const size_t MAX_LABEL_LENGTH = 63; | |||||
// Builds the name field of the question section of a DNS query | // Builds the name field of the question section of a DNS query | ||||
static std::vector<uint8_t> | static std::vector<uint8_t> | ||||
CreateDNSQuestionNameField(const std::string &queryName) { | CreateDNSQuestionNameField(const std::string &queryName) { | ||||
std::vector<uint8_t> nameField; | std::vector<uint8_t> nameField; | ||||
size_t i = 0; | size_t i = 0; | ||||
size_t labelIndex = 0; | size_t labelIndex = 0; | ||||
while (i < queryName.size()) { | while (i < queryName.size()) { | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | while (outputBufferSize <= MAX_QUERY_NAME_BUFFER_LENGTH) { | ||||
nameField.data(), parsedQueryName.data(), | nameField.data(), parsedQueryName.data(), | ||||
parsedQueryName.size()); | parsedQueryName.size()); | ||||
BOOST_CHECK_EQUAL(ret, 0); | BOOST_CHECK_EQUAL(ret, 0); | ||||
BOOST_CHECK_EQUAL(parsedQueryName.data(), queryName); | BOOST_CHECK_EQUAL(parsedQueryName.data(), queryName); | ||||
outputBufferSize++; | outputBufferSize++; | ||||
} | } | ||||
} | } | ||||
static void CheckParseNameError( | |||||
const std::string &queryName, const int expectedError, | |||||
const size_t &outputBufferSize = MAX_QUERY_NAME_BUFFER_LENGTH) { | |||||
std::vector<uint8_t> nameField = CreateDNSQuestionNameField(queryName); | |||||
std::vector<char> parsedQueryName(outputBufferSize, 0); | |||||
const uint8_t *nameFieldBegin = nameField.data(); | |||||
int ret = parse_name(&nameFieldBegin, nameFieldBegin + nameField.size(), | |||||
nameField.data(), parsedQueryName.data(), | |||||
parsedQueryName.size()); | |||||
BOOST_CHECK_EQUAL(ret, expectedError); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(parse_name_tests) { | BOOST_AUTO_TEST_CASE(parse_name_tests) { | ||||
CheckParseName("www.domain.com"); | CheckParseName("www.domain.com"); | ||||
CheckParseName("domain.com"); | CheckParseName("domain.com"); | ||||
CheckParseName("sub1.sub2.domain.co.uk"); | CheckParseName("sub1.sub2.domain.co.uk"); | ||||
// Shortest valid domain name is 1 char followed by the extension | // Shortest valid domain name is 1 char followed by the extension | ||||
CheckParseName("a.co"); | CheckParseName("a.co"); | ||||
// Domain name with valid non-alphanumeric character | // Domain name with valid non-alphanumeric character | ||||
CheckParseName("my-domain.com"); | CheckParseName("my-domain.com"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(parse_name_label_tests) { | |||||
// Check behavior for name with maximum length label | |||||
const std::string maxLengthLabel(MAX_LABEL_LENGTH, 'a'); | |||||
CheckParseName("www." + maxLengthLabel + ".com"); | |||||
// Check that an oversized label causes an error | |||||
CheckParseNameError("www." + maxLengthLabel + "a.com", -1); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |