Parcourir la source

Implement MemoryZone::load

And fix tests for it

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac451@4036 e5f2f494-b856-4b98-b285-d166d9295462
Michal Vaner il y a 14 ans
Parent
commit
29ef44f8e0

+ 48 - 0
src/lib/datasrc/memory_datasrc.cc

@@ -15,9 +15,11 @@
 #include <map>
 #include <cassert>
 #include <boost/shared_ptr.hpp>
+#include <boost/bind.hpp>
 
 #include <dns/name.h>
 #include <dns/rrclass.h>
+#include <dns/masterload.h>
 
 #include <datasrc/memory_datasrc.h>
 #include <datasrc/rbtree.h>
@@ -144,6 +146,41 @@ struct MemoryZone::MemoryZoneImpl {
             return (FindResult(NXRRSET, ConstRRsetPtr()));
         }
     }
+
+    struct Load {
+        Load(MemoryZone* zone) :
+            zone_(zone),
+            swap_(true)
+        {
+            tmp_tree_.swap(zone_->impl_->domains_);
+        }
+        ~ Load() {
+            if (swap_) {
+                tmp_tree_.swap(zone_->impl_->domains_);
+            }
+        }
+        void load(const string &filename) {
+            masterLoad(filename.c_str(), zone_->getOrigin(), zone_->getClass(),
+                boost::bind(&Load::add, this, _1));
+            swap_ = false;
+        }
+        void add(RRsetPtr set) {
+            switch (zone_->add(set)) {
+                case result::EXIST:
+                    isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
+                        set->toText());
+                case result::SUCCESS:
+                    return;
+                default:
+                    isc_throw(AssertError,
+                        "Unexpected result of MemoryZone::add");
+            }
+        }
+
+        MemoryZone *zone_;
+        DomainTree tmp_tree_;
+        bool swap_;
+    };
 };
 
 MemoryZone::MemoryZone(const RRClass& zone_class, const Name& origin) :
@@ -175,6 +212,17 @@ MemoryZone::add(const ConstRRsetPtr& rrset) {
     return (impl_->add(rrset));
 }
 
+void
+xxx(Zone*, RRsetPtr) {
+
+}
+
+void
+MemoryZone::load(const string& filename) {
+    MemoryZoneImpl::Load load(this);
+    load.load(filename);
+}
+
 /// Implementation details for \c MemoryDataSrc hidden from the public
 /// interface.
 ///

+ 16 - 7
src/lib/datasrc/tests/memory_datasrc_unittest.cc

@@ -185,15 +185,20 @@ public:
      * \param check_answer Should a check against equality of the answer be
      *     done?
      * \param answer The expected rrset, if any should be returned.
+     * \param zone Check different MemoryZone object than zone_ (if NULL,
+     *     uses zone_)
      */
     void findTest(const Name& name, const RRType& rrtype, Zone::Result result,
         bool check_answer = true,
-        const ConstRRsetPtr& answer = ConstRRsetPtr())
+        const ConstRRsetPtr& answer = ConstRRsetPtr(), MemoryZone *zone = NULL)
     {
+        if (!zone) {
+            zone = &zone_;
+        }
         // The whole block is inside, because we need to check the result and
         // we can't assign to FindResult
         EXPECT_NO_THROW({
-            Zone::FindResult find_result(zone_.find(name, rrtype));
+            Zone::FindResult find_result(zone->find(name, rrtype));
             // Check it returns correct answers
             EXPECT_EQ(result, find_result.code);
             if (check_answer) {
@@ -272,16 +277,20 @@ TEST_F(MemoryZoneTest, load) {
     // Create correct zone
     MemoryZone rootzone(class_, Name("."));
     // Try putting something inside
-    EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, zone_.add(rr_ns_aaaa_)));
+    EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, rootzone.add(rr_ns_aaaa_)));
     // Load the zone. It should overwrite/remove the above RRset
     EXPECT_NO_THROW(rootzone.load(TEST_DATA_DIR "/root.zone"));
 
     // Now see there are some rrsets (we don't look inside, though)
-    findTest(Name("."), RRType::SOA(), Zone::SUCCESS, false);
-    findTest(Name("."), RRType::NS(), Zone::SUCCESS, false);
-    findTest(Name("a.root-servers.net."), RRType::A(), Zone::SUCCESS, false);
+    findTest(Name("."), RRType::SOA(), Zone::SUCCESS, false, ConstRRsetPtr(),
+        &rootzone);
+    findTest(Name("."), RRType::NS(), Zone::SUCCESS, false, ConstRRsetPtr(),
+        &rootzone);
+    findTest(Name("a.root-servers.net."), RRType::A(), Zone::SUCCESS, false,
+        ConstRRsetPtr(), &rootzone);
     // But this should no longer be here
-    findTest(ns_name_, RRType::AAAA(), Zone::NXDOMAIN, false);
+    findTest(ns_name_, RRType::AAAA(), Zone::NXDOMAIN, true, ConstRRsetPtr(),
+        &rootzone);
 }
 
 }