From: Otto Date: Tue, 3 Aug 2021 08:32:57 +0000 (+0200) Subject: Add test case and comments X-Git-Tag: dnsdist-1.7.0-alpha1~74^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F10623%2Fhead;p=thirdparty%2Fpdns.git Add test case and comments --- diff --git a/pdns/recursordist/test-syncres_cc10.cc b/pdns/recursordist/test-syncres_cc10.cc index 3d6808c0ea..2397952941 100644 --- a/pdns/recursordist/test-syncres_cc10.cc +++ b/pdns/recursordist/test-syncres_cc10.cc @@ -1013,4 +1013,50 @@ BOOST_AUTO_TEST_CASE(test_dnssec_bogus_dnskey_loop) BOOST_CHECK_EQUAL(queriesCount, 5U); } +static auto createPID(std::string rem, int tcpsock, uint16_t type, std::string domain, int fd, uint16_t id) +{ + PacketID pid; + pid.remote = ComboAddress(rem); + pid.tcpsock = tcpsock; + pid.type = type; + pid.domain = DNSName(domain); + pid.fd = fd; + pid.id = id; + return std::make_shared(pid); +} + +BOOST_AUTO_TEST_CASE(test_PacketIDCompare) +{ + // Ordered by domain, but not by id + auto a = createPID("1.2.3.4", -1, 1, "powerdns.com", -1, 1000); + auto b = createPID("1.2.3.4", -1, 1, "powerdns.net", -1, 999); + + auto cmp = PacketIDCompare(); + auto bcmp = PacketIDBirthdayCompare(); + + bool r1 = cmp.operator()(a, b); + bool br1 = bcmp.operator()(a, b); + bool r2 = cmp.operator()(b, a); + bool br2 = bcmp.operator()(b, a); + + BOOST_CHECK(r1); + BOOST_CHECK(br1); + BOOST_CHECK(!r2); + BOOST_CHECK(!br2); + + // Ordered by domain, but not by fd + a = createPID("1.2.3.4", -1, 1, "powerdns.com", 1, 1000); + b = createPID("1.2.3.4", -1, 1, "powerdns.net", -1, 1000); + + r1 = cmp.operator()(a, b); + br1 = bcmp.operator()(a, b); + r2 = cmp.operator()(b, a); + br2 = bcmp.operator()(b, a); + + BOOST_CHECK(r1); + BOOST_CHECK(br1); + BOOST_CHECK(!r2); + BOOST_CHECK(!br2); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/pdns/syncres.hh b/pdns/syncres.hh index acca4f9951..a5f9caf4c0 100644 --- a/pdns/syncres.hh +++ b/pdns/syncres.hh @@ -967,7 +967,7 @@ struct PacketID bool operator<(const PacketID& b) const { - // We don't want explcit PacketID compare here, but always via predicate classes below + // We don't want explicit PacketID compare here, but always via predicate classes below assert(0); } }; @@ -983,6 +983,11 @@ inline ostream& operator<<(ostream & os, const shared_ptr& pid) return os << *pid; } +/* + * The two compare predicates below must be consistent! + * PacketIDBirthdayCompare can omit minor fields, but not change the or skip fields + * order! See boost docs on CompatibleCompare. + */ struct PacketIDCompare { bool operator()(const std::shared_ptr& a, const std::shared_ptr& b) const