|
@@ -0,0 +1,93 @@
|
|
|
+// Copyright (C) 2011 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 <gtest/gtest.h>
|
|
|
+
|
|
|
+#include <dns/name.h>
|
|
|
+
|
|
|
+#include <datasrc/database.h>
|
|
|
+
|
|
|
+using namespace isc::datasrc;
|
|
|
+using namespace std;
|
|
|
+using namespace boost;
|
|
|
+using isc::dns::Name;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+/*
|
|
|
+ * A virtual database connection that pretends it contains single zone --
|
|
|
+ * example.org.
|
|
|
+ */
|
|
|
+class MockConnection : public DatabaseConnection {
|
|
|
+public:
|
|
|
+ virtual std::pair<bool, int> getZone(const Name& name) const {
|
|
|
+ if (name == Name("zone.example.org")) {
|
|
|
+ return (std::pair<bool, int>(true, 42));
|
|
|
+ } else {
|
|
|
+ return (std::pair<bool, int>(false, 0));
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+class DatabaseClientTest : public ::testing::Test {
|
|
|
+public:
|
|
|
+ DatabaseClientTest() {
|
|
|
+ createClient();
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * We initialize the client from a function, so we can call it multiple
|
|
|
+ * times per test.
|
|
|
+ */
|
|
|
+ void createClient() {
|
|
|
+ current_connection_ = new MockConnection();
|
|
|
+ client_.reset(new DatabaseClient(auto_ptr<DatabaseConnection>(
|
|
|
+ current_connection_)));
|
|
|
+ }
|
|
|
+ // Will be deleted by client_, just keep the current value for comparison.
|
|
|
+ MockConnection* current_connection_;
|
|
|
+ auto_ptr<DatabaseClient> client_;
|
|
|
+ /**
|
|
|
+ * Check the zone finder is a valid one and references the zone ID and
|
|
|
+ * connection available here.
|
|
|
+ */
|
|
|
+ void checkZoneFinder(const DataSourceClient::FindResult& zone) {
|
|
|
+ ASSERT_NE(ZoneFinderPtr(), zone.zone_finder) << "No zone finder";
|
|
|
+ shared_ptr<DatabaseClient::Finder> finder(
|
|
|
+ dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
|
|
|
+ ASSERT_NE(shared_ptr<DatabaseClient::Finder>(), finder) <<
|
|
|
+ "Wrong type of finder";
|
|
|
+ EXPECT_EQ(42, finder->zone_id());
|
|
|
+ EXPECT_EQ(current_connection_, &finder->connection());
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+TEST_F(DatabaseClientTest, zoneNotFound) {
|
|
|
+ DataSourceClient::FindResult zone(client_->findZone(Name("example.com")));
|
|
|
+ EXPECT_EQ(result::NOTFOUND, zone.code);
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(DatabaseClientTest, exactZone) {
|
|
|
+ DataSourceClient::FindResult zone(client_->findZone(Name("example.org")));
|
|
|
+ EXPECT_EQ(result::SUCCESS, zone.code);
|
|
|
+ checkZoneFinder(zone);
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(DatabaseClientTest, superZone) {
|
|
|
+ DataSourceClient::FindResult zone(client_->findZone(Name(
|
|
|
+ "sub.example.org")));
|
|
|
+ EXPECT_EQ(result::PARTIALMATCH, zone.code);
|
|
|
+ checkZoneFinder(zone);
|
|
|
+}
|
|
|
+
|
|
|
+}
|