Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/test/parse_name_tests.cpp
- This file was moved from src/seeder/test/dns_tests.cpp.
// Copyright (c) 2019 The Bitcoin developers | // Copyright (c) 2019 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <seeder/dns.h> | #include <seeder/dns.h> | ||||
#include <seeder/test/dns_util.h> | |||||
#include <seeder/test/util.h> | #include <seeder/test/util.h> | ||||
#include <ostream> | #include <ostream> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
std::ostream &operator<<(std::ostream &os, const ParseNameStatus &status) { | std::ostream &operator<<(std::ostream &os, const ParseNameStatus &status) { | ||||
os << to_integral(status); | os << to_integral(status); | ||||
return os; | return os; | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE(dns_tests) | BOOST_AUTO_TEST_SUITE(parse_name_tests) | ||||
static const uint8_t END_OF_NAME_FIELD = 0; | |||||
// Builds the name field of the question section of a DNS query | |||||
static std::vector<uint8_t> | |||||
CreateDNSQuestionNameField(const std::string &queryName) { | |||||
std::vector<uint8_t> nameField; | |||||
size_t i = 0; | |||||
size_t labelIndex = 0; | |||||
while (i < queryName.size()) { | |||||
if (queryName[i] == '.') { | |||||
// Push the length of the label and then the label | |||||
nameField.push_back(i - labelIndex); | |||||
while (labelIndex < i) { | |||||
nameField.push_back(queryName[labelIndex]); | |||||
labelIndex++; | |||||
} | |||||
labelIndex = i + 1; | |||||
} | |||||
i++; | |||||
} | |||||
// Push the length of the label and then the label | |||||
nameField.push_back(i - labelIndex); | |||||
while (labelIndex < i) { | |||||
nameField.push_back(queryName[labelIndex]); | |||||
labelIndex++; | |||||
} | |||||
nameField.push_back(END_OF_NAME_FIELD); | |||||
return nameField; | |||||
} | |||||
static void CheckParseName(const std::string &queryName) { | static void CheckParseName(const std::string &queryName) { | ||||
std::vector<uint8_t> nameField = CreateDNSQuestionNameField(queryName); | std::vector<uint8_t> nameField = CreateDNSQuestionNameField(queryName); | ||||
// Test when name field is too short to reach null-terminator | // Test when name field is too short to reach null-terminator | ||||
for (size_t nameFieldEndIndex = 0; nameFieldEndIndex < nameField.size(); | for (size_t nameFieldEndIndex = 0; nameFieldEndIndex < nameField.size(); | ||||
nameFieldEndIndex++) { | nameFieldEndIndex++) { | ||||
std::vector<char> parsedQueryName(MAX_QUERY_NAME_BUFFER_LENGTH, 0); | std::vector<char> parsedQueryName(MAX_QUERY_NAME_BUFFER_LENGTH, 0); | ||||
Show All 39 Lines | static void CheckParseNameError( | ||||
const uint8_t *nameFieldBegin = nameField.data(); | const uint8_t *nameFieldBegin = nameField.data(); | ||||
ParseNameStatus ret = parse_name( | ParseNameStatus ret = parse_name( | ||||
&nameFieldBegin, nameFieldBegin + nameField.size(), nameField.data(), | &nameFieldBegin, nameFieldBegin + nameField.size(), nameField.data(), | ||||
parsedQueryName.data(), parsedQueryName.size()); | parsedQueryName.data(), parsedQueryName.size()); | ||||
BOOST_CHECK_EQUAL(ret, expectedError); | BOOST_CHECK_EQUAL(ret, expectedError); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(parse_name_tests) { | BOOST_AUTO_TEST_CASE(parse_name_simple_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"); | ||||
} | } | ||||
Show All 32 Lines |