From: JINMEI Tatuya Date: Tue, 14 Aug 2012 04:34:42 +0000 (-0700) Subject: [2097] provide const version of getDataBuf(), added some test cases for it. X-Git-Tag: trac2351_base~113^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2fd528da04c3a154ca0b787c7f1b537d4bd869db;p=thirdparty%2Fkea.git [2097] provide const version of getDataBuf(), added some test cases for it. --- diff --git a/src/lib/datasrc/memory/rdataset.h b/src/lib/datasrc/memory/rdataset.h index c9fcc975ca..3254199fee 100644 --- a/src/lib/datasrc/memory/rdataset.h +++ b/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(this)); + } + const void* getDataBuf() const { + return (getDataBuf(this)); } private: @@ -92,6 +91,14 @@ private: const uint16_t* getExtSIGCountBuf() const { return (reinterpret_cast(this + 1)); } + template + 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); diff --git a/src/lib/datasrc/memory/tests/rdataset_unittest.cc b/src/lib/datasrc/memory/tests/rdataset_unittest.cc index 27d1756fec..e8832da343 100644 --- a/src/lib/datasrc/memory/tests/rdataset_unittest.cc +++ b/src/lib/datasrc/memory/tests/rdataset_unittest.cc @@ -18,12 +18,14 @@ #include #include +#include #include #include #include #include #include +#include #include #include @@ -35,6 +37,7 @@ #include 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( + 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); }