Browse Source

Commit RRsetList to trunk (mutually written and reviewed, ping-pong
style, by Michael and me)


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@952 e5f2f494-b856-4b98-b285-d166d9295462

Evan Hunt 15 years ago
parent
commit
cb211a31b7

+ 2 - 0
src/lib/dns/cpp/Makefile.am

@@ -13,6 +13,7 @@ libdns_la_SOURCES += rrclass.h rrclass.cc rrtype.h rrtype.cc rrttl.h rrttl.cc
 libdns_la_SOURCES += rdata.h rdata.cc
 libdns_la_SOURCES += rdataclass.h rdataclass.cc
 libdns_la_SOURCES += rrset.h rrset.cc
+libdns_la_SOURCES += rrsetlist.h rrsetlist.cc
 libdns_la_SOURCES += question.h question.cc
 libdns_la_SOURCES += message.h message.cc
 libdns_la_SOURCES += base64.h base64.cc
@@ -33,6 +34,7 @@ run_unittests_SOURCES += rrclass_unittest.cc rrtype_unittest.cc
 run_unittests_SOURCES += rrttl_unittest.cc
 run_unittests_SOURCES += rdata_unittest.cc
 run_unittests_SOURCES += rrset_unittest.cc
+run_unittests_SOURCES += rrsetlist_unittest.cc
 run_unittests_SOURCES += question_unittest.cc
 run_unittests_SOURCES += rrparamregistry_unittest.cc
 run_unittests_SOURCES += message_unittest.cc

+ 67 - 0
src/lib/dns/cpp/rrsetlist.cc

@@ -0,0 +1,67 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <algorithm>
+#include <string>
+#include <vector>
+
+#include <boost/shared_ptr.hpp>
+#include <boost/foreach.hpp>
+
+#include "rrsetlist.h"
+
+using namespace std;
+using namespace isc::dns;
+
+namespace isc {
+namespace dns {
+
+void
+RRsetList::addRRset(const RRsetPtr rrsetptr)
+{
+    const RRsetPtr rrset_found = findRRset(rrsetptr->getType(),
+                                           rrsetptr->getClass());
+    if (rrset_found) {
+        dns_throw(DuplicateRRset, "");
+    }
+    rrsets_.push_back(rrsetptr);
+}
+
+const RRsetPtr
+RRsetList::findRRset(const RRsetPtr rrsetptr)
+{
+    BOOST_FOREACH(const RRsetPtr t, rrsets_) {
+        if (rrsetptr == t) {
+            return rrsetptr;
+        }
+    }
+    return RRsetPtr();
+}
+
+const RRsetPtr
+RRsetList::findRRset(const RRType& rrtype, const RRClass& rrclass)
+{
+    BOOST_FOREACH(const RRsetPtr rrsetptr, rrsets_) {
+        const AbstractRRset* rrset = rrsetptr.get();
+        if ((rrset->getClass() == rrclass) && (rrset->getType() == rrtype)) {
+            return rrsetptr;
+        }
+    }
+    return RRsetPtr();
+}
+
+}
+}

+ 68 - 0
src/lib/dns/cpp/rrsetlist.h

@@ -0,0 +1,68 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __RRSETLIST_H
+#define __RRSETLIST_H 1
+
+#include <iostream>
+#include <vector>
+#include <map>
+
+#include <boost/shared_ptr.hpp>
+
+#include "rrset.h"
+#include "rrclass.h"
+#include "rrtype.h"
+
+namespace isc {
+namespace dns {
+
+class DuplicateRRset : public Exception {
+public:
+    DuplicateRRset(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
+class RRsetList {
+public:
+    void addRRset(const RRsetPtr new_rrsetptr);
+    const RRsetPtr findRRset(const RRType& rrtype,
+                             const RRClass& rrclass = RRClass::IN());
+    const RRsetPtr findRRset(const RRsetPtr);
+
+    const RRsetPtr operator[](RRType t) { return (this->findRRset(t)); }
+
+    typedef std::vector<RRsetPtr>::const_iterator const_iterator;
+    const_iterator begin() const { return (rrsets_.begin()); }
+    const_iterator end() const { return (rrsets_.end)(); }
+
+    typedef std::vector<RRsetPtr>::iterator iterator;
+    iterator begin() { return (rrsets_.begin()); }
+    iterator end() { return (rrsets_.end)(); }
+
+    size_t size() const { return (rrsets_.size()); }
+
+private:
+    std::vector<RRsetPtr> rrsets_;
+};
+
+} // end of namespace dns
+} // end of namespace isc
+#endif  // __RRSETLIST_H
+
+// Local Variables: 
+// mode: c++
+// End: 

+ 177 - 0
src/lib/dns/cpp/rrsetlist_unittest.cc

@@ -0,0 +1,177 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id: rrtype_unittest.cc 476 2010-01-19 00:29:28Z jinmei $
+
+#include <vector>
+#include <boost/foreach.hpp>
+
+#include "rdata.h"
+#include "rdataclass.h"
+#include "rrclass.h"
+#include "rrtype.h"
+#include "rrsetlist.h"
+#include "rrset.h"
+#include "rrttl.h"
+
+#include <gtest/gtest.h>
+
+#include "unittest_util.h"
+
+using isc::UnitTestUtil;
+using namespace std;
+using namespace isc::dns;
+using namespace isc::dns::rdata;
+
+namespace {
+class RRsetListTest : public ::testing::Test {
+protected:
+    RRsetListTest() {}
+    void setupList(RRsetList& list);
+    static const in::A rdata_in_a;
+    static const in::AAAA rdata_in_aaaa;
+    static const generic::NS rdata_ns;
+    static const generic::SOA rdata_soa;
+    static const generic::CNAME rdata_cname;
+};
+
+const in::A RRsetListTest::rdata_in_a("192.0.2.1");
+const in::AAAA RRsetListTest::rdata_in_aaaa("2001:db8::1234");
+const generic::NS RRsetListTest::rdata_ns("ns.example.com");
+const generic::SOA RRsetListTest::rdata_soa(Name("ns.example.com"),
+                                            Name("root.example.com"),
+                                            2010012601, 3600, 300,
+                                            3600000, 1200);
+const generic::CNAME RRsetListTest::rdata_cname("target.example.com");
+
+void
+RRsetListTest::setupList(RRsetList& list) {
+    RRsetPtr a(new RRset(Name("example.com"), RRClass::IN(),
+                         RRType::A(), RRTTL(3600)));
+    RRsetPtr aaaa(new RRset(Name("example.com"), RRClass::IN(),
+                            RRType::AAAA(), RRTTL(3600)));
+    RRsetPtr ns(new RRset(Name("example.com"), RRClass::IN(),
+                          RRType::NS(), RRTTL(3600)));
+    RRsetPtr soa(new RRset(Name("example.com"), RRClass::IN(),
+                           RRType::SOA(), RRTTL(3600)));
+    RRsetPtr cname(new RRset(Name("example.com"), RRClass::IN(),
+                             RRType::CNAME(), RRTTL(3600)));
+
+    a->addRdata(rdata_in_a);
+    aaaa->addRdata(rdata_in_aaaa);
+    ns->addRdata(rdata_ns);
+    soa->addRdata(rdata_soa);
+    cname->addRdata(rdata_cname);
+
+    list.addRRset(a);
+    list.addRRset(aaaa);
+    list.addRRset(ns);
+    list.addRRset(soa);
+    list.addRRset(cname);
+}
+
+TEST_F(RRsetListTest, emptyOnInitialCreate) {
+    RRsetList list;
+    EXPECT_EQ(list.size(), 0);
+}
+
+TEST_F(RRsetListTest, addRRsets) {
+    RRsetList list;
+    setupList(list);
+    EXPECT_EQ(list.size(), 5);
+}
+
+TEST_F(RRsetListTest, extraRRset) {
+    RRsetList list;
+    setupList(list);
+    RRsetPtr cname(new RRset(Name("another.example.com"), RRClass::IN(),
+                             RRType::CNAME(), RRTTL(3600)));
+    EXPECT_THROW(list.addRRset(cname), DuplicateRRset);
+}
+
+TEST_F(RRsetListTest, randomAccess) {
+    RRsetList list;
+    setupList(list);
+
+    RRsetPtr p;
+
+    p = list[RRType::CNAME()];
+    EXPECT_TRUE(p->getType() == RRType::CNAME());
+
+    p = list[RRType::AAAA()];
+    EXPECT_TRUE(p->getType() == RRType::AAAA());
+
+    p = list[RRType::NS()];
+    EXPECT_TRUE(p->getType() == RRType::NS());
+
+    p = list[RRType::A()];
+    EXPECT_TRUE(p->getType() == RRType::A());
+
+    p = list[RRType::SOA()];
+    EXPECT_TRUE(p->getType() == RRType::SOA());
+}
+
+TEST_F(RRsetListTest, findRRset) {
+    RRsetList list;
+    setupList(list);
+    EXPECT_EQ(list[RRType::A()], list.findRRset(RRType::A(), RRClass::IN()));
+}
+
+TEST_F(RRsetListTest, checkData) {
+    RRsetList list;
+    RRsetPtr a(new RRset(Name("example.com"), RRClass::IN(),
+                         RRType::A(), RRTTL(3600)));
+    a->addRdata(rdata_in_a);
+    list.addRRset(a);
+
+    RdataIteratorPtr it = list[RRType::A()]->getRdataIterator();
+    it->first();
+    EXPECT_FALSE(it->isLast());
+    EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
+}
+
+TEST_F(RRsetListTest, iterate) {
+    RRsetList list;
+    setupList(list);
+
+    bool has_a, has_aaaa, has_ns, has_soa, has_cname;
+    int i = 0;
+    BOOST_FOREACH(RRsetPtr rrset, list) {
+        if (rrset->getType() == RRType::A()) {
+            has_a = true;
+        }
+        if (rrset->getType() == RRType::AAAA()) {
+            has_aaaa = true;
+        }
+        if (rrset->getType() == RRType::NS()) {
+            has_ns = true;
+        }
+        if (rrset->getType() == RRType::SOA()) {
+            has_soa = true;
+        }
+        if (rrset->getType() == RRType::CNAME()) {
+            has_cname = true;
+        }
+        ++i;
+    }
+    EXPECT_TRUE(has_a);
+    EXPECT_TRUE(has_aaaa);
+    EXPECT_TRUE(has_ns);
+    EXPECT_TRUE(has_soa);
+    EXPECT_TRUE(has_cname);
+    EXPECT_TRUE(i == 5);
+}
+
+}
+