BIND 10 trac2207, updated. c64356e9fdf3be868bb2d624200219d68aca45e8 [2207] Add missing test file

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Oct 11 17:54:27 UTC 2012


The branch, trac2207 has been updated
       via  c64356e9fdf3be868bb2d624200219d68aca45e8 (commit)
       via  e5ad36f38f6066c50059ea02e06245d7bc5c7efd (commit)
      from  e70da63e4aa5a075928c5cdef4fed11d15eeaa5a (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit c64356e9fdf3be868bb2d624200219d68aca45e8
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Thu Oct 11 19:52:59 2012 +0200

    [2207] Add missing test file
    
    Someone O:-) forgot to add it in the ^^ commit.

commit e5ad36f38f6066c50059ea02e06245d7bc5c7efd
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Thu Oct 11 19:52:12 2012 +0200

    [2207] Implementation of the zone updater
    
    The interface was slightly updated, to suit the needs (added some more
    parameters). Some of it might be wrong, but I'm not sure about it.

-----------------------------------------------------------------------

Summary of changes:
 src/lib/datasrc/memory/zone_updater.cc             |   75 +++++++
 src/lib/datasrc/memory/zone_updater.h              |   20 +-
 .../datasrc/tests/memory/zone_updater_unittest.cc  |  236 ++++++++++++++++++++
 3 files changed, 330 insertions(+), 1 deletion(-)
 create mode 100644 src/lib/datasrc/tests/memory/zone_updater_unittest.cc

-----------------------------------------------------------------------
diff --git a/src/lib/datasrc/memory/zone_updater.cc b/src/lib/datasrc/memory/zone_updater.cc
index 03c8ece..40d55ee 100644
--- a/src/lib/datasrc/memory/zone_updater.cc
+++ b/src/lib/datasrc/memory/zone_updater.cc
@@ -13,3 +13,78 @@
 // PERFORMANCE OF THIS SOFTWARE.
 
 #include "zone_updater.h"
+#include "zone_data.h"
+#include "zone_table_segment.h"
+
+#include <memory>
+
+using std::auto_ptr;
+
+namespace isc {
+namespace datasrc {
+namespace memory {
+
+ZoneUpdaterLocal::ZoneUpdaterLocal(ZoneTableSegment* segment,
+                                   const LoadAction& load_action,
+                                   const InstallAction& install_action,
+                                   const dns::Name& origin,
+                                   const dns::RRClass& rrclass) :
+    segment_(segment),
+    load_action_(load_action),
+    install_action_(install_action),
+    origin_(origin),
+    rrclass_(rrclass),
+    zone_data_(NULL),
+    loaded_(false),
+    data_ready_(false)
+{}
+
+ZoneUpdaterLocal::~ZoneUpdaterLocal() {
+    // Clean up everything there might be left if someone forgot, just
+    // in case. Or should we assert instead?
+    cleanup();
+}
+
+void
+ZoneUpdaterLocal::load() {
+    if (loaded_) {
+        isc_throw(isc::Unexpected, "Trying to load twice");
+    }
+    loaded_ = true;
+
+    zone_data_ = ZoneData::create(segment_->getMemorySegment(), origin_);
+
+    load_action_(zone_data_);
+
+    data_ready_ = true;
+}
+
+void
+ZoneUpdaterLocal::install() {
+    if (!data_ready_) {
+        isc_throw(isc::Unexpected, "No data to install");
+    }
+
+    data_ready_ = false;
+    auto_ptr<ZoneSegment> zone_segment(new ZoneSegment(zone_data_));
+
+    zone_data_ = install_action_(ZoneSegmentID(), zone_segment.get());
+
+    // The ownership was passed to the callback, no need to clear it now.
+    zone_segment.release();
+}
+
+void
+ZoneUpdaterLocal::cleanup() {
+    // We eat the data (if any) now.
+    data_ready_ = false;
+
+    if (zone_data_ != NULL) {
+        ZoneData::destroy(segment_->getMemorySegment(), zone_data_, rrclass_);
+        zone_data_ = NULL;
+    }
+}
+
+}
+}
+}
diff --git a/src/lib/datasrc/memory/zone_updater.h b/src/lib/datasrc/memory/zone_updater.h
index 9658866..5d445dd 100644
--- a/src/lib/datasrc/memory/zone_updater.h
+++ b/src/lib/datasrc/memory/zone_updater.h
@@ -12,6 +12,9 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
+#include <dns/name.h>
+#include <dns/rrclass.h>
+
 #include <boost/function.hpp>
 
 namespace isc {
@@ -130,8 +133,12 @@ public:
     /// \param segment The zone table segment to store the zone into.
     /// \param load_action The callback used to load data.
     /// \param install_action The callback used to install the loaded zone.
+    /// \param origin The origin name of the zone.
+    /// \param rrclass The class of the zone.
     ZoneUpdaterLocal(ZoneTableSegment* segment, const LoadAction& load_action,
-                     const InstallAction& install_action);
+                     const InstallAction& install_action,
+                     const dns::Name& origin,
+                     const dns::RRClass& rrclass);
     /// \brief Destructor
     ~ZoneUpdaterLocal();
     /// \brief Loads the data.
@@ -158,6 +165,17 @@ public:
     /// Cleans up the memory used by load()ed zone if not yet installed, or
     /// the old zone replaced by install().
     virtual void cleanup();
+private:
+    ZoneTableSegment* segment_;
+    LoadAction load_action_;
+    InstallAction install_action_;
+    dns::Name origin_;
+    dns::RRClass rrclass_;
+    ZoneData* zone_data_;
+    // The load was performed
+    bool loaded_;
+    // The data are ready to be installed
+    bool data_ready_;
 };
 
 }
diff --git a/src/lib/datasrc/tests/memory/zone_updater_unittest.cc b/src/lib/datasrc/tests/memory/zone_updater_unittest.cc
new file mode 100644
index 0000000..c32fb87
--- /dev/null
+++ b/src/lib/datasrc/tests/memory/zone_updater_unittest.cc
@@ -0,0 +1,236 @@
+// Copyright (C) 2012  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.
+
+#include <datasrc/memory/zone_updater.h>
+#include <datasrc/memory/zone_table_segment.h>
+#include <datasrc/memory/zone_data.h>
+
+#include <cc/data.h>
+#include <dns/rrclass.h>
+#include <dns/name.h>
+
+#include <gtest/gtest.h>
+
+#include <boost/scoped_ptr.hpp>
+#include <boost/bind.hpp>
+
+using boost::scoped_ptr;
+using boost::bind;
+using isc::dns::RRClass;
+using isc::dns::Name;
+using namespace isc::datasrc::memory;
+
+namespace {
+
+class TestException {};
+
+class ZoneUpdaterLocalTest : public ::testing::Test {
+public:
+    ZoneUpdaterLocalTest() :
+        // FIXME: The NullElement probably isn't the best one, but we don't
+        // know how the config will look, so it just fills the argument
+        // (which is currently ignored)
+        segment_(ZoneTableSegment::create(isc::data::NullElement())),
+        updater_(new
+            ZoneUpdaterLocal(segment_.get(),
+                             bind(&ZoneUpdaterLocalTest::loadAction, this, _1),
+                             bind(&ZoneUpdaterLocalTest::installAction, this,
+                                  _1, _2),
+                             Name("example.org"), RRClass::IN())),
+        load_called_(false),
+        install_called_(false),
+        load_throw_(false),
+        install_throw_(false)
+    {}
+    void TearDown() {
+        // Release the updater
+        updater_.reset();
+        // And check we freed all memory
+        EXPECT_TRUE(segment_->getMemorySegment().allMemoryDeallocated());
+    }
+protected:
+    scoped_ptr<ZoneTableSegment> segment_;
+    scoped_ptr<ZoneUpdaterLocal> updater_;
+    bool load_called_;
+    bool install_called_;
+    bool load_throw_;
+    bool install_throw_;
+private:
+    void loadAction(ZoneData* data) {
+        // Make sure we get something.
+        EXPECT_NE(static_cast<const ZoneData*>(NULL), data);
+        // We got called
+        load_called_ = true;
+        if (load_throw_) {
+            throw TestException();
+        }
+    }
+    ZoneData* installAction(const ZoneSegmentID&, ZoneSegment* segment) {
+        install_called_ = true;
+
+        // Check we got something
+        if (segment == NULL) {
+            ADD_FAILURE() << "Zone segment is NULL in install action";
+            return (NULL);
+        }
+        EXPECT_NE(static_cast<const ZoneData*>(NULL), segment->getZoneData());
+
+        if (install_throw_) {
+            // In case we throw, we do so before releasing the memory there,
+            // as in that case we don't claim the ownership of the data.
+            throw TestException();
+        }
+
+        // We received ownership of the parameters here. And we don't really need
+        // them in the tests, so we just release them.
+        ZoneData::destroy(segment_->getMemorySegment(), segment->getZoneData(),
+                          RRClass::IN());
+        delete segment;
+
+        // And we are supposed to pass the old version back. So we create
+        // new zone data and pass it.
+        return (ZoneData::create(segment_->getMemorySegment(), Name("exmaple.org")));
+    }
+};
+
+// We call it the way we are supposed to, check every callback is called in the
+// right moment.
+TEST_F(ZoneUpdaterLocalTest, correctCall) {
+    // Nothing called before we call it
+    EXPECT_FALSE(load_called_);
+    EXPECT_FALSE(install_called_);
+
+    // Just the load gets called now
+    EXPECT_NO_THROW(updater_->load());
+    EXPECT_TRUE(load_called_);
+    EXPECT_FALSE(install_called_);
+    load_called_ = false;
+
+    EXPECT_NO_THROW(updater_->install());
+    EXPECT_FALSE(load_called_);
+    EXPECT_TRUE(install_called_);
+
+    // We don't check explicitly how this works, but call it to free memory. If
+    // everything is freed should be checked inside the TearDown.
+    EXPECT_NO_THROW(updater_->cleanup());
+}
+
+TEST_F(ZoneUpdaterLocalTest, loadTwice) {
+    // Load it the first time
+    EXPECT_NO_THROW(updater_->load());
+    EXPECT_TRUE(load_called_);
+    EXPECT_FALSE(install_called_);
+    load_called_ = false;
+
+    // The second time, it should not be possible
+    EXPECT_THROW(updater_->load(), isc::Unexpected);
+    EXPECT_FALSE(load_called_);
+    EXPECT_FALSE(install_called_);
+
+    // The object should not be damaged, try installing and clearing now
+    EXPECT_NO_THROW(updater_->install());
+    EXPECT_FALSE(load_called_);
+    EXPECT_TRUE(install_called_);
+
+    // We don't check explicitly how this works, but call it to free memory. If
+    // everything is freed should be checked inside the TearDown.
+    EXPECT_NO_THROW(updater_->cleanup());
+}
+
+// Try loading after call to install and call to cleanup. Both is
+// forbidden.
+TEST_F(ZoneUpdaterLocalTest, loadLater) {
+    // Load first, so we can install
+    EXPECT_NO_THROW(updater_->load());
+    EXPECT_NO_THROW(updater_->install());
+    // Reset so we see nothing is called now
+    install_called_ = load_called_ = false;
+
+    EXPECT_THROW(updater_->load(), isc::Unexpected);
+    EXPECT_FALSE(load_called_);
+    EXPECT_FALSE(install_called_);
+
+    // Cleanup and try loading again. Still shouldn't work.
+    EXPECT_NO_THROW(updater_->cleanup());
+
+    EXPECT_THROW(updater_->load(), isc::Unexpected);
+    EXPECT_FALSE(load_called_);
+    EXPECT_FALSE(install_called_);
+}
+
+// Try calling install at various bad times
+TEST_F(ZoneUpdaterLocalTest, invalidInstall) {
+    // Nothing loaded yet
+    EXPECT_THROW(updater_->install(), isc::Unexpected);
+    EXPECT_FALSE(load_called_);
+    EXPECT_FALSE(install_called_);
+
+    EXPECT_NO_THROW(updater_->load());
+    load_called_ = false;
+    // This install is OK
+    EXPECT_NO_THROW(updater_->install());
+    install_called_ = false;
+    // But we can't call it second time now
+    EXPECT_THROW(updater_->install(), isc::Unexpected);
+    EXPECT_FALSE(load_called_);
+    EXPECT_FALSE(install_called_);
+}
+
+// We check we can clean without installing first and nothing bad
+// happens. We also misuse the testcase to check we can't install
+// after cleanup.
+TEST_F(ZoneUpdaterLocalTest, cleanWithoutInstall) {
+    EXPECT_NO_THROW(updater_->load());
+    EXPECT_NO_THROW(updater_->cleanup());
+
+    EXPECT_TRUE(load_called_);
+    EXPECT_FALSE(install_called_);
+
+    // We cleaned up, no way to install now
+    EXPECT_THROW(updater_->install(), isc::Unexpected);
+    EXPECT_FALSE(install_called_);
+}
+
+// Test the case when load callback throws
+TEST_F(ZoneUpdaterLocalTest, loadThrows) {
+    load_throw_ = true;
+    EXPECT_THROW(updater_->load(), TestException);
+
+    // We can't install now
+    EXPECT_THROW(updater_->install(), isc::Unexpected);
+    EXPECT_TRUE(load_called_);
+    EXPECT_FALSE(install_called_);
+
+    // But we can cleanup
+    EXPECT_NO_THROW(updater_->cleanup());
+}
+
+// Test we free all our memory even when we throw from install
+TEST_F(ZoneUpdaterLocalTest, installThrows) {
+    install_throw_ = true;
+    EXPECT_NO_THROW(updater_->load());
+
+    EXPECT_THROW(updater_->install(), TestException);
+    EXPECT_TRUE(load_called_);
+    EXPECT_TRUE(install_called_);
+
+    // We can't try again
+    install_throw_ = false;
+    EXPECT_THROW(updater_->install(), isc::Unexpected);
+
+    // But it is not completely broken
+    EXPECT_NO_THROW(updater_->cleanup());
+}
+
+}



More information about the bind10-changes mailing list