add DNSName::isPartOf() + tests

This commit is contained in:
bert hubert 2018-10-15 22:06:57 +02:00
parent 7a91009a32
commit a214e5cf04
3 changed files with 34 additions and 0 deletions

View File

@ -21,6 +21,27 @@ bool DNSName::makeRelative(const DNSName& root)
return true;
}
//! Checks is this DNSName is part of root
bool DNSName::isPartOf(const DNSName& root) const
{
auto them = root.d_name.crbegin(), us = d_name.crbegin();
for(;;) {
if(them == root.d_name.crend())
return true;
if(us == d_name.crend())
return false;
if(*them == *us) {
++them;
++us;
}
else
break;
}
return false;
}
//! Append two DNSNames
DNSName operator+(const DNSName& a, const DNSName& b)
{

View File

@ -146,6 +146,7 @@ struct DNSName
auto size() { return d_name.size(); }
void clear() { d_name.clear(); }
bool makeRelative(const DNSName& root);
bool isPartOf(const DNSName& root) const;
std::string toString() const;
bool operator==(const DNSName& rhs) const
{

View File

@ -39,6 +39,18 @@ TEST_CASE("DNSName operations", "[dnsname]") {
REQUIRE(test2.makeRelative({"org"}));
REQUIRE(test2 == DNSName({"www", "powerdns"}));
DNSName parent({"powerdns", "com"}), root({}), child({"www", "powerdns", "com"});
DNSName unrelated({"www", "isc", "org"});
DNSName Org({"Org"});
REQUIRE(parent.isPartOf(root));
REQUIRE(child.isPartOf(parent));
REQUIRE(child.isPartOf(root));
REQUIRE(!root.isPartOf(parent));
REQUIRE(!parent.isPartOf(child));
REQUIRE(!unrelated.isPartOf(child));
REQUIRE(!child.isPartOf(unrelated));
REQUIRE(unrelated.isPartOf(Org));
}
TEST_CASE("DNS Messages", "[dnsmessage]") {