Browse Source

[2097] provide const version of getDataBuf(), added some test cases for it.

JINMEI Tatuya 12 years ago
parent
commit
2fd528da04
2 changed files with 58 additions and 17 deletions
  1. 12 5
      src/lib/datasrc/memory/rdataset.h
  2. 46 12
      src/lib/datasrc/memory/tests/rdataset_unittest.cc

+ 12 - 5
src/lib/datasrc/memory/rdataset.h

@@ -78,11 +78,10 @@ public:
     }
     const void* getTTLData() const { return (&ttl_); }
     void* getDataBuf() {
-        if (sig_rdata_count_ < MANY_RRSIG_COUNT) {
-            return (this + 1);
-        } else {
-            return (getExtSIGCountBuf() + 1);
-        }
+        return (getDataBuf<void, RdataSet>(this));
+    }
+    const void* getDataBuf() const {
+        return (getDataBuf<const void, const RdataSet>(this));
     }
 
 private:
@@ -92,6 +91,14 @@ private:
     const uint16_t* getExtSIGCountBuf() const {
         return (reinterpret_cast<const uint16_t*>(this + 1));
     }
+    template <typename RetType, typename ThisType>
+    static RetType* getDataBuf(ThisType* rdataset) {
+        if (rdataset->sig_rdata_count_ < MANY_RRSIG_COUNT) {
+            return (rdataset + 1);
+        } else {
+            return (rdataset->getExtSIGCountBuf() + 1);
+        }
+    }
 
     RdataSet(dns::RRType type, size_t rdata_count, size_t sig_rdata_count,
              dns::RRTTL ttl);

+ 46 - 12
src/lib/datasrc/memory/tests/rdataset_unittest.cc

@@ -18,12 +18,14 @@
 #include <util/memory_segment_local.h>
 
 #include <dns/rdata.h>
+#include <dns/rdataclass.h>
 #include <dns/rrset.h>
 #include <dns/rrclass.h>
 #include <dns/rrtype.h>
 #include <dns/rrttl.h>
 
 #include <datasrc/memory/rdata_encoder.h>
+#include <datasrc/memory/rdata_reader.h>
 #include <datasrc/memory/rdataset.h>
 
 #include <testutils/dnsmessage_test.h>
@@ -35,6 +37,7 @@
 #include <string>
 
 using namespace isc::dns;
+using namespace isc::dns::rdata;
 using namespace isc::datasrc::memory;
 using namespace isc::testutils;
 using boost::lexical_cast;
@@ -68,20 +71,53 @@ restoreTTL(const void* ttl_data) {
     return (RRTTL(b));
 }
 
+// A helper callback for checkRdataSet.  This confirms the given data
+// is the expected in::A RDATA (the value is taken from the RdataSetTest
+// constructor).
+void
+checkData(const uint8_t* data, size_t size) {
+    isc::util::InputBuffer b(data, size);
+    EXPECT_EQ(0, in::A(b, size).compare(in::A("192.0.2.1")));
+}
+
+// This is a set of checks for an RdataSet created with some simple
+// conditions.  with_rrset/with_rrsig is true iff the RdataSet is supposed to
+// contain normal/RRSIG RDATA.
+void
+checkRdataSet(const RdataSet& rdataset, bool with_rrset, bool with_rrsig) {
+    EXPECT_FALSE(rdataset.next); // by default the next pointer should be NULL
+    EXPECT_EQ(RRType::A(), rdataset.type);
+    // See the RdataSetTest constructor for the magic number.
+    EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset.getTTLData()));
+    EXPECT_EQ(with_rrset ? 1 : 0, rdataset.getRdataCount());
+    EXPECT_EQ(with_rrsig ? 1 : 0, rdataset.getSigRdataCount());
+
+    // A simple test for the data content.  Details tests for the encoder/
+    // reader should be basically sufficient for various cases of the data,
+    // and the fact that this test doesn't detect memory leak should be
+    // reasonably sufficient that the implementation handles the data region
+    // correctly.  Here we check one simple case for a simple form of RDATA,
+    // mainly for checking the behavior of getDataBuf().
+    RdataReader reader(RRClass::IN(), RRType::A(),
+                       reinterpret_cast<const uint8_t*>(
+                           rdataset.getDataBuf()),
+                       rdataset.getRdataCount(), rdataset.getSigRdataCount(),
+                       &RdataReader::emptyNameAction, checkData);
+    reader.iterate();
+}
+
 TEST_F(RdataSetTest, create) {
     // A simple case of creating an RdataSet.  Confirming the resulting
     // fields have the expected values, and then destroying it (TearDown()
     // would detect any memory leak)
     RdataSet* rdataset = RdataSet::create(mem_sgmt_, encoder_, a_rrset_,
                                           ConstRRsetPtr());
-    EXPECT_FALSE(rdataset->next); // by default the next pointer should be NULL
-    EXPECT_EQ(RRType::A(), rdataset->type);
-    EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
-    EXPECT_EQ(1, rdataset->getRdataCount());
-    EXPECT_EQ(0, rdataset->getSigRdataCount());
+    checkRdataSet(*rdataset, true, false);
     RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
 }
 
+// A helper function to create an RRset containing the given number of
+// unique RDATAs.
 ConstRRsetPtr
 getRRsetWithRdataCount(size_t rdata_count) {
     RRsetPtr rrset(new RRset(Name("example.com"), RRClass::IN(), RRType::TXT(),
@@ -113,8 +149,7 @@ TEST_F(RdataSetTest, createWithRRSIG) {
     // Normal case.
     RdataSet* rdataset = RdataSet::create(mem_sgmt_, encoder_, a_rrset_,
                                           rrsig_rrset_);
-    EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
-    EXPECT_EQ(1, rdataset->getSigRdataCount());
+    checkRdataSet(*rdataset, true, true);
     RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
 
     // Unusual case: TTL doesn't match.  This implementation accepts that,
@@ -124,10 +159,12 @@ TEST_F(RdataSetTest, createWithRRSIG) {
                                    "A 5 2 3600 20120814220826 "
                                    "20120715220826 1234 example.com. FAKE"));
     rdataset = RdataSet::create(mem_sgmt_, encoder_, a_rrset_, rrsig_badttl);
-    EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
+    checkRdataSet(*rdataset, true, true);
     RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
 }
 
+// A helper function to create an RRSIG RRset containing the given number of
+// unique RDATAs.
 ConstRRsetPtr
 getRRSIGWithRdataCount(size_t sig_count) {
     RRsetPtr rrset(new RRset(Name("example.com"), RRClass::IN(),
@@ -175,10 +212,7 @@ TEST_F(RdataSetTest, createWithRRSIGOnly) {
     // RRSIG.
     RdataSet* rdataset = RdataSet::create(mem_sgmt_, encoder_, ConstRRsetPtr(),
                                           rrsig_rrset_);
-    EXPECT_EQ(RRType::A(), rdataset->type); // type covered is used as type
-    EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
-    EXPECT_EQ(0, rdataset->getRdataCount());
-    EXPECT_EQ(1, rdataset->getSigRdataCount());
+    checkRdataSet(*rdataset, false, true);
     RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
 }