]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
make it possible to compare pointers/references to DNSRecordContent. There is a gener...
authorbert hubert <bert.hubert@netherlabs.nl>
Mon, 12 Sep 2016 21:48:41 +0000 (23:48 +0200)
committerbert hubert <bert.hubert@powerdns.com>
Tue, 13 Sep 2016 10:53:30 +0000 (12:53 +0200)
Also make DNSPacket::addRecord() use this infrastructure

pdns/Makefile.am
pdns/dnsparser.hh
pdns/dnsrecords.hh
pdns/test-dnsrecordcontent.cc [new file with mode: 0644]

index 89fae83fc780d242dd37f0e19cd705b825e03862..ee4ea9126f89199c6a35eaafcdf0a410cc542d9a 100644 (file)
@@ -1136,6 +1136,7 @@ testrunner_SOURCES = \
        test-base64_cc.cc \
        test-bindparser_cc.cc \
        test-delaypipe_hh.cc \
+       test-dnsrecordcontent.cc \
        test-distributor_hh.cc \
        test-dns_random_hh.cc \
        test-dnsname_cc.cc \
index a3b0771f4474d752336857d2fa23f787479f4eca..e64985f62bafb12bf9eece8eb33b6507e9df9aba 100644 (file)
@@ -192,6 +192,11 @@ public:
     return record;
   }
 
+  virtual bool operator==(const DNSRecordContent& rhs) const
+  {
+    return typeid(*this)==typeid(rhs) && this->getZoneRepresentation() == rhs.getZoneRepresentation();
+  }
+  
   static shared_ptr<DNSRecordContent> unserialize(const DNSName& qname, uint16_t qtype, const string& serialized);
 
   void doRecordCheck(const struct DNSRecord&){}
@@ -318,13 +323,7 @@ struct DNSRecord
     if(d_type != rhs.d_type || d_class != rhs.d_class || d_name != rhs.d_name)
       return false;
     
-    string lzrp, rzrp;
-    if(d_content)
-      lzrp=toLower(d_content->getZoneRepresentation());
-    if(rhs.d_content)
-      rzrp=toLower(rhs.d_content->getZoneRepresentation());
-
-    return lzrp == rzrp;
+    return *d_content == *rhs.d_content;
   }
 };
 
index a597162fb6647ad41c52928ed7814a35417b8e1e..97c66d00524716191703f4b37de7cfe2b695f3c8 100644 (file)
@@ -62,6 +62,12 @@ public:
   includeboilerplate(A);
   void doRecordCheck(const DNSRecord& dr);
   ComboAddress getCA(int port=0) const;
+  bool operator==(const DNSRecordContent& rhs) const override
+  {
+    if(typeid(*this) != typeid(rhs))
+      return false;
+    return d_ip == dynamic_cast<const ARecordContent&>(rhs).d_ip;
+  }
 private:
   uint32_t d_ip;
 };
@@ -73,6 +79,12 @@ public:
   explicit AAAARecordContent(const ComboAddress& ca);
   includeboilerplate(AAAA);
   ComboAddress getCA(int port=0) const;
+  bool operator==(const DNSRecordContent& rhs) const override
+  {
+    if(typeid(*this) != typeid(rhs))
+      return false;
+    return d_ip6 == dynamic_cast<const decltype(this)>(&rhs)->d_ip6;
+  }
 private:
   string d_ip6; // why??
 };
@@ -86,6 +98,15 @@ public:
 
   uint16_t d_preference;
   DNSName d_mxname;
+
+  bool operator==(const DNSRecordContent& rhs) const override
+  {
+    if(typeid(*this) != typeid(rhs))
+      return false;
+    auto rrhs =dynamic_cast<const decltype(this)>(&rhs);
+    return std::tie(d_preference, d_mxname) == std::tie(rrhs->d_preference, rrhs->d_mxname);
+  }
+
 };
 
 class KXRecordContent : public DNSRecordContent
@@ -185,7 +206,15 @@ class NSRecordContent : public DNSRecordContent
 public:
   includeboilerplate(NS)
   explicit NSRecordContent(const DNSName& content) : d_content(content){}
-  const DNSName& getNS() const { return d_content; } 
+  const DNSName& getNS() const { return d_content; }
+  bool operator==(const DNSRecordContent& rhs) const override
+  {
+    if(typeid(*this) != typeid(rhs))
+      return false;
+    auto rrhs =dynamic_cast<const decltype(this)>(&rhs);
+    return d_content == rrhs->d_content;
+  }
+
 private:
   DNSName d_content;
 };
diff --git a/pdns/test-dnsrecordcontent.cc b/pdns/test-dnsrecordcontent.cc
new file mode 100644 (file)
index 0000000..9eb6829
--- /dev/null
@@ -0,0 +1,46 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_NO_MAIN
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <boost/test/unit_test.hpp>
+#include "dnsrecords.hh"
+#include "iputils.hh"
+
+BOOST_AUTO_TEST_SUITE(test_dnsrecordcontent)
+
+BOOST_AUTO_TEST_CASE(test_equality) {
+  reportAllTypes();
+  ComboAddress ip("1.2.3.4"), ip2("10.0.0.1"), ip6("::1");
+  ARecordContent a1(ip), a2(ip), a3(ip2);
+  AAAARecordContent aaaa(ip6), aaaa1(ip6);
+  
+  BOOST_CHECK(a1==a2);
+  BOOST_CHECK(!(a1==a3));
+
+  BOOST_CHECK(aaaa == aaaa1);
+
+
+  auto rec1=DNSRecordContent::makeunique(QType::A, 1, "192.168.0.1");
+  auto rec2=DNSRecordContent::makeunique(QType::A, 1, "192.168.222.222");
+  auto rec3=DNSRecordContent::makeunique(QType::AAAA, 1, "::1");
+  auto recMX=DNSRecordContent::makeunique(QType::MX, 1, "25 smtp.powerdns.com");
+  auto recMX2=DNSRecordContent::makeunique(QType::MX, 1, "26 smtp.powerdns.com");
+  auto recMX3=DNSRecordContent::makeunique(QType::MX, 1, "26 SMTP.powerdns.com");
+  BOOST_CHECK(!(*rec1==*rec2));
+  BOOST_CHECK(*rec1==*rec1);
+  BOOST_CHECK(*rec3==*rec3);
+
+  BOOST_CHECK(*recMX==*recMX);
+  BOOST_CHECK(*recMX2==*recMX3);
+  BOOST_CHECK(!(*recMX==*recMX3));
+  
+  
+  BOOST_CHECK(!(*rec1==*rec3));
+
+  NSRecordContent ns1(DNSName("ns1.powerdns.com")), ns2(DNSName("NS1.powerdns.COM")), ns3(DNSName("powerdns.net"));
+  BOOST_CHECK(ns1==ns2);
+  BOOST_CHECK(!(ns1==ns3));
+}
+
+BOOST_AUTO_TEST_SUITE_END()