From: Kees Monshouwer Date: Wed, 11 Mar 2015 13:32:56 +0000 (+0100) Subject: fix for root in DNSName isParent() and some tests X-Git-Tag: dnsdist-1.0.0-alpha1~248^2~88^2~54^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9e98d926b44340726a97f0440f2bdadb377682e8;p=pdns fix for root in DNSName isParent() and some tests --- diff --git a/pdns/dnsname.cc b/pdns/dnsname.cc index 78df576b8..3dc801110 100644 --- a/pdns/dnsname.cc +++ b/pdns/dnsname.cc @@ -75,18 +75,20 @@ std::string DNSName::toDNSString() const // are WE part of parent bool DNSName::isPartOf(const DNSName& parent) const { - if(parent.d_storage.size() > d_storage.size()) + if(parent.d_storage.empty()) + return true; + if(parent.d_storage.size() > d_storage.size()) return false; // this is slightly complicated since we can't start from the end, since we can't see where a label begins/ends then for(auto us=d_storage.cbegin(); us= (unsigned int)parent.d_storage.size(); us+=*us+1) { if (d_storage.cend()-us == (unsigned int)parent.d_storage.size()) { auto p = parent.d_storage.cbegin(); - for(; us != d_storage.cend() && p != parent.d_storage.cend(); ++us, ++p) { + for(; us != d_storage.cend(); ++us, ++p) { if(tolower(*p) != tolower(*us)) - break; + return false; } - return (p==parent.d_storage.end()); + return true; } } return false; diff --git a/pdns/test-dnsname_cc.cc b/pdns/test-dnsname_cc.cc index f1122e006..e15a5e84c 100644 --- a/pdns/test-dnsname_cc.cc +++ b/pdns/test-dnsname_cc.cc @@ -35,6 +35,50 @@ BOOST_AUTO_TEST_CASE(test_basic) { BOOST_CHECK(DNSName("www.ds9a.nl.").toString() == "www.ds9a.nl."); + + { // Check root part of root + DNSName name; + DNSName parent; + BOOST_CHECK(name.isPartOf(parent)); + } + + { // Check name part of root + DNSName name("a."); + DNSName parent; + BOOST_CHECK(name.isPartOf(parent)); + } + + { // Label boundary + DNSName name("a\002bb."); + DNSName parent("bb."); + BOOST_CHECK(!name.isPartOf(parent)); + } + + { // Multi label parent + DNSName name("a.bb.ccc.dddd."); + DNSName parent("ccc.dddd."); + BOOST_CHECK(name.isPartOf(parent)); + } + + { // Last char diff + DNSName name("a.bb.ccc.dddd."); + DNSName parent("ccc.dddx."); + BOOST_CHECK(!name.isPartOf(parent)); + } + + { // Equal length identical + DNSName name("aaaa.bbb.cc.d."); + DNSName parent("aaaa.bbb.cc.d."); + BOOST_CHECK(name.isPartOf(parent)); + } + + { // Equal length first char diff + DNSName name("xaaa.bbb.cc.d."); + DNSName parent("aaaa.bbb.cc.d."); + BOOST_CHECK(!name.isPartOf(parent)); + } + + DNSName left("ds9a.nl."); left.prependRawLabel("www"); BOOST_CHECK( left == DNSName("WwW.Ds9A.Nl."));