Browse Source

[1607] test setup: created a test zone file, defined a param'ed test fixture.

JINMEI Tatuya 13 years ago
parent
commit
ff863e03a8

+ 2 - 0
src/lib/datasrc/tests/Makefile.am

@@ -56,6 +56,7 @@ run_unittests_SOURCES += database_unittest.cc
 run_unittests_SOURCES += sqlite3_unittest.cc
 run_unittests_SOURCES += sqlite3_accessor_unittest.cc
 run_unittests_SOURCES += memory_datasrc_unittest.cc
+run_unittests_SOURCES += zone_finder_context_unittest.cc
 
 # We need the actually module implementation in the tests (they are not part
 # of libdatasrc)
@@ -106,3 +107,4 @@ EXTRA_DIST += testdata/test.sqlite3
 EXTRA_DIST += testdata/test.sqlite3.nodiffs
 EXTRA_DIST += testdata/rwtest.sqlite3
 EXTRA_DIST += testdata/diffs.sqlite3
+EXTRA_DIST += testdata/contexttest.zone

+ 22 - 0
src/lib/datasrc/tests/testdata/contexttest.zone

@@ -0,0 +1,22 @@
+;; test zone file used for ZoneFinderContext tests.
+
+example.org. 3600 IN SOA	ns1.example.org. bugs.x.w.example.org. 9 3600 300 3600000 3600
+example.org.			      3600 IN NS	ns1.example.org.
+example.org.			      3600 IN NS	ns2.example.org.
+example.org.			      3600 IN MX	1 mx1.example.org.
+example.org.			      3600 IN MX	2 mx2.example.org.
+
+ns1.example.org.		      3600 IN A		192.0.2.1
+ns1.example.org.		      3600 IN AAAA	2001:db8::1
+ns2.example.org.		      3600 IN A		192.0.2.2
+
+mx1.example.org.		      3600 IN A		192.0.2.10
+mx2.example.org.		      3600 IN AAAA	2001:db8::10
+
+;; delegation
+a.example.org.			      3600 IN NS	ns1.a.example.org.
+a.example.org.			      3600 IN NS	ns2.a.example.org.
+
+ns1.a.example.org.		      3600 IN A		192.0.2.5
+ns2.a.example.org.		      3600 IN A		192.0.2.6
+ns2.a.example.org.		      3600 IN AAAA	2001:db8::6

+ 122 - 0
src/lib/datasrc/tests/zone_finder_context_unittest.cc

@@ -0,0 +1,122 @@
+// 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 <dns/masterload.h>
+#include <dns/name.h>
+#include <dns/rrclass.h>
+
+#include <datasrc/zone.h>
+#include <datasrc/memory_datasrc.h>
+#include <datasrc/database.h>
+#include <datasrc/sqlite3_accessor.h>
+
+#include <gtest/gtest.h>
+
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <cstdlib>
+
+using namespace std;
+using namespace isc::dns;
+using namespace isc::datasrc;
+using boost::shared_ptr;
+
+namespace {
+
+// Commonly used test zone file.
+const char* const TEST_ZONE_FILE = TEST_DATA_DIR "/contexttest.zone";
+
+// Convenient shortcut
+typedef shared_ptr<DataSourceClient> DataSourceClientPtr;
+
+// This is the type used as the test parameter.  Note that this is
+// intentionally a plain old type (i.e. a function pointer), not a class;
+// otherwise it could cause initialization fiasco at the instantiation time.
+typedef DataSourceClientPtr (*ClientCreator)(RRClass, const Name&);
+
+// Creator for the in-memory client to be tested
+DataSourceClientPtr
+createInMemoryClient(RRClass zclass, const Name& zname) {
+    shared_ptr<InMemoryClient> client(new InMemoryClient);
+
+    shared_ptr<InMemoryZoneFinder> finder(
+        new InMemoryZoneFinder(zclass, zname));
+    finder->load(TEST_ZONE_FILE);
+
+    client->addZone(finder);
+
+    return (client);
+}
+
+// Creator for the SQLite3 client to be tested.  addRRset() is a helper
+// subroutine.
+void
+addRRset(ZoneUpdaterPtr updater, ConstRRsetPtr rrset) {
+    updater->addRRset(*rrset);
+}
+
+DataSourceClientPtr
+createSQLite3Client(RRClass zclass, const Name& zname) {
+    // We always begin with an empty template SQLite3 DB file and install
+    // the zone data from the zone file to ensure both cases have the
+    // same test data.
+
+    const char* const install_cmd = INSTALL_PROG " " TEST_DATA_DIR
+        "/rwtest.sqlite3 " TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied";
+    if (system(install_cmd) != 0) {
+        isc_throw(isc::Unexpected,
+                  "Error setting up; command failed: " << install_cmd);
+    }
+
+    shared_ptr<SQLite3Accessor> accessor(
+        new SQLite3Accessor(TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied",
+                            zclass.toText()));
+    shared_ptr<DatabaseClient> client(new DatabaseClient(zclass, accessor));
+
+    ZoneUpdaterPtr updater = client->getUpdater(zname, true);
+    masterLoad(TEST_ZONE_FILE, zname, zclass, boost::bind(addRRset, updater,
+                                                          _1));
+    updater->commit();
+
+    return (client);
+}
+
+// The test class.  Its parameterized so we can share the test scnearios
+// for any concrete data source implementaitons.
+class ZoneFinderContextTest :
+        public ::testing::TestWithParam<ClientCreator>
+{
+protected:
+    ZoneFinderContextTest() : qclass_(RRClass::IN()), qzone_("example.org") {
+        client_ = (*GetParam())(qclass_, qzone_);
+    }
+
+    const RRClass qclass_;
+    const Name qzone_;
+    DataSourceClientPtr client_;
+    ZoneFinderPtr finder_;
+};
+
+// We test the in-memory and SQLite3 data source implementations.
+INSTANTIATE_TEST_CASE_P(, ZoneFinderContextTest,
+                        ::testing::Values(createInMemoryClient,
+                                          createSQLite3Client));
+
+TEST_P(ZoneFinderContextTest, getAdditional) {
+    finder_ = client_->findZone(Name("www.a.example.org")).zone_finder;
+    ASSERT_TRUE(finder_);
+}
+
+}