2011-08-01 11:47:37 +02:00
|
|
|
// 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>
|
2011-08-05 15:18:00 +02:00
|
|
|
#include <dns/rrttl.h>
|
2011-08-01 12:03:35 +02:00
|
|
|
#include <exceptions/exceptions.h>
|
2011-08-01 11:47:37 +02:00
|
|
|
|
|
|
|
#include <datasrc/database.h>
|
2011-08-05 15:18:00 +02:00
|
|
|
#include <datasrc/data_source.h>
|
|
|
|
#include <datasrc/iterator.h>
|
2011-08-01 11:47:37 +02:00
|
|
|
|
|
|
|
using namespace isc::datasrc;
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
2011-08-05 15:18:00 +02:00
|
|
|
using namespace isc::dns;
|
2011-08-01 11:47:37 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/*
|
2011-08-05 15:18:00 +02:00
|
|
|
* A connection with minimum implementation, keeping the original
|
|
|
|
* "NotImplemented" methods.
|
2011-08-01 11:47:37 +02:00
|
|
|
*/
|
2011-08-08 12:48:09 +02:00
|
|
|
class NopAbstraction : public DatabaseAbstraction {
|
2011-08-01 11:47:37 +02:00
|
|
|
public:
|
|
|
|
virtual std::pair<bool, int> getZone(const Name& name) const {
|
2011-08-01 13:06:13 +02:00
|
|
|
if (name == Name("example.org")) {
|
2011-08-01 11:47:37 +02:00
|
|
|
return (std::pair<bool, int>(true, 42));
|
2011-08-05 15:18:00 +02:00
|
|
|
} else if (name == Name("null.example.org")) {
|
|
|
|
return (std::pair<bool, int>(true, 13));
|
|
|
|
} else if (name == Name("empty.example.org")) {
|
|
|
|
return (std::pair<bool, int>(true, 0));
|
|
|
|
} else if (name == Name("bad.example.org")) {
|
|
|
|
return (std::pair<bool, int>(true, -1));
|
2011-08-01 11:47:37 +02:00
|
|
|
} else {
|
|
|
|
return (std::pair<bool, int>(false, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-05 15:18:00 +02:00
|
|
|
/*
|
|
|
|
* A virtual database connection that pretends it contains single zone --
|
|
|
|
* example.org.
|
|
|
|
*
|
|
|
|
* It has the same getZone method as NopConnection, but it provides
|
|
|
|
* implementation of the optional functionality.
|
|
|
|
*/
|
2011-08-08 12:48:09 +02:00
|
|
|
class MockAbstraction : public NopAbstraction {
|
2011-08-05 15:18:00 +02:00
|
|
|
private:
|
|
|
|
class MockIteratorContext : public IteratorContext {
|
|
|
|
private:
|
|
|
|
int step;
|
|
|
|
public:
|
|
|
|
MockIteratorContext() :
|
|
|
|
step(0)
|
|
|
|
{ }
|
2011-08-11 19:27:35 +02:00
|
|
|
virtual bool getNext(string data[4]) {
|
2011-08-05 15:18:00 +02:00
|
|
|
switch (step ++) {
|
|
|
|
case 0:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "example.org";
|
|
|
|
data[1] = "SOA";
|
|
|
|
data[2] = "300";
|
|
|
|
data[3] = "ns1.example.org. admin.example.org. "
|
2011-08-05 15:18:00 +02:00
|
|
|
"1234 3600 1800 2419200 7200";
|
|
|
|
return (true);
|
|
|
|
case 1:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "x.example.org";
|
|
|
|
data[1] = "A";
|
|
|
|
data[2] = "300";
|
|
|
|
data[3] = "192.0.2.1";
|
2011-08-05 15:18:00 +02:00
|
|
|
return (true);
|
|
|
|
case 2:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "x.example.org";
|
|
|
|
data[1] = "A";
|
|
|
|
data[2] = "300";
|
|
|
|
data[3] = "192.0.2.2";
|
2011-08-05 15:18:00 +02:00
|
|
|
return (true);
|
|
|
|
case 3:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "x.example.org";
|
|
|
|
data[1] = "AAAA";
|
|
|
|
data[2] = "300";
|
|
|
|
data[3] = "2001:db8::1";
|
2011-08-05 15:18:00 +02:00
|
|
|
return (true);
|
|
|
|
case 4:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "x.example.org";
|
|
|
|
data[1] = "AAAA";
|
|
|
|
data[2] = "300";
|
|
|
|
data[3] = "2001:db8::2";
|
2011-08-05 15:18:00 +02:00
|
|
|
return (true);
|
|
|
|
default:
|
|
|
|
ADD_FAILURE() <<
|
|
|
|
"Request past the end of iterator context";
|
|
|
|
case 5:
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class EmptyIteratorContext : public IteratorContext {
|
|
|
|
public:
|
2011-08-11 19:27:35 +02:00
|
|
|
virtual bool getNext(string[4]) {
|
2011-08-05 15:18:00 +02:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class BadIteratorContext : public IteratorContext {
|
|
|
|
private:
|
|
|
|
int step;
|
|
|
|
public:
|
|
|
|
BadIteratorContext() :
|
|
|
|
step(0)
|
|
|
|
{ }
|
2011-08-11 19:27:35 +02:00
|
|
|
virtual bool getNext(string data[4]) {
|
2011-08-05 15:18:00 +02:00
|
|
|
switch (step ++) {
|
|
|
|
case 0:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "x.example.org";
|
|
|
|
data[1] = "A";
|
|
|
|
data[2] = "300";
|
|
|
|
data[3] = "192.0.2.1";
|
2011-08-05 15:18:00 +02:00
|
|
|
return (true);
|
|
|
|
case 1:
|
2011-08-11 19:27:35 +02:00
|
|
|
data[0] = "x.example.org";
|
|
|
|
data[1] = "A";
|
|
|
|
data[2] = "301";
|
|
|
|
data[3] = "192.0.2.2";
|
2011-08-05 15:18:00 +02:00
|
|
|
return (true);
|
|
|
|
default:
|
|
|
|
ADD_FAILURE() <<
|
|
|
|
"Request past the end of iterator context";
|
|
|
|
case 2:
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
virtual IteratorContextPtr getIteratorContext(const Name&, int id) const {
|
|
|
|
if (id == 42) {
|
|
|
|
return (IteratorContextPtr(new MockIteratorContext()));
|
|
|
|
} else if (id == 13) {
|
|
|
|
return (IteratorContextPtr());
|
|
|
|
} else if (id == 0) {
|
|
|
|
return (IteratorContextPtr(new EmptyIteratorContext()));
|
|
|
|
} else if (id == -1) {
|
|
|
|
return (IteratorContextPtr(new BadIteratorContext()));
|
|
|
|
} else {
|
|
|
|
isc_throw(isc::Unexpected, "Unknown zone ID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-05 12:52:13 +02:00
|
|
|
// This tests the default getIteratorContext behaviour, throwing NotImplemented
|
|
|
|
TEST(DatabaseConnectionTest, getIteratorContext) {
|
|
|
|
// The parameters don't matter
|
2011-08-08 12:48:09 +02:00
|
|
|
EXPECT_THROW(NopAbstraction().getIteratorContext(Name("."), 1),
|
2011-08-05 12:52:13 +02:00
|
|
|
isc::NotImplemented);
|
|
|
|
}
|
|
|
|
|
2011-08-01 11:47:37 +02:00
|
|
|
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() {
|
2011-08-07 12:26:36 +02:00
|
|
|
current_database_ = new MockAbstraction();
|
|
|
|
client_.reset(new DatabaseClient(shared_ptr<DatabaseAbstraction>(
|
|
|
|
current_database_)));
|
2011-08-01 11:47:37 +02:00
|
|
|
}
|
|
|
|
// Will be deleted by client_, just keep the current value for comparison.
|
2011-08-07 12:26:36 +02:00
|
|
|
MockAbstraction* current_database_;
|
2011-08-04 17:18:54 +02:00
|
|
|
shared_ptr<DatabaseClient> client_;
|
2011-08-01 11:47:37 +02:00
|
|
|
/**
|
|
|
|
* Check the zone finder is a valid one and references the zone ID and
|
2011-08-07 12:26:36 +02:00
|
|
|
* database available here.
|
2011-08-01 11:47:37 +02:00
|
|
|
*/
|
|
|
|
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());
|
2011-08-07 12:26:36 +02:00
|
|
|
EXPECT_EQ(current_database_, &finder->database());
|
2011-08-01 11:47:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-08-01 12:03:35 +02:00
|
|
|
TEST_F(DatabaseClientTest, noConnException) {
|
2011-08-07 12:26:36 +02:00
|
|
|
EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseAbstraction>()),
|
2011-08-01 12:03:35 +02:00
|
|
|
isc::InvalidParameter);
|
|
|
|
}
|
|
|
|
|
2011-08-05 15:18:00 +02:00
|
|
|
// If the zone doesn't exist, exception is thrown
|
|
|
|
TEST_F(DatabaseClientTest, noZoneIterator) {
|
|
|
|
EXPECT_THROW(client_->getIterator(Name("example.com")), DataSourceError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the zone doesn't exist and iteration is not implemented, it still throws
|
|
|
|
// the exception it doesn't exist
|
|
|
|
TEST_F(DatabaseClientTest, noZoneNotImplementedIterator) {
|
2011-08-08 12:48:09 +02:00
|
|
|
EXPECT_THROW(DatabaseClient(boost::shared_ptr<DatabaseAbstraction>(
|
|
|
|
new NopAbstraction())).getIterator(Name("example.com")),
|
2011-08-05 15:18:00 +02:00
|
|
|
DataSourceError);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DatabaseClientTest, notImplementedIterator) {
|
2011-08-08 12:48:09 +02:00
|
|
|
EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseAbstraction>(
|
|
|
|
new NopAbstraction())).getIterator(Name("example.org")),
|
2011-08-05 15:18:00 +02:00
|
|
|
isc::NotImplemented);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pretend a bug in the connection and pass NULL as the context
|
|
|
|
// Should not crash, but gracefully throw
|
|
|
|
TEST_F(DatabaseClientTest, nullIteratorContext) {
|
|
|
|
EXPECT_THROW(client_->getIterator(Name("null.example.org")),
|
|
|
|
isc::Unexpected);
|
|
|
|
}
|
|
|
|
|
|
|
|
// It doesn't crash or anything if the zone is completely empty
|
|
|
|
TEST_F(DatabaseClientTest, emptyIterator) {
|
|
|
|
ZoneIteratorPtr it(client_->getIterator(Name("empty.example.org")));
|
|
|
|
EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
|
|
|
|
// This is past the end, it should throw
|
|
|
|
EXPECT_THROW(it->getNextRRset(), isc::Unexpected);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate trough a zone
|
|
|
|
TEST_F(DatabaseClientTest, iterator) {
|
|
|
|
ZoneIteratorPtr it(client_->getIterator(Name("example.org")));
|
|
|
|
ConstRRsetPtr rrset(it->getNextRRset());
|
|
|
|
ASSERT_NE(ConstRRsetPtr(), rrset);
|
|
|
|
EXPECT_EQ(Name("example.org"), rrset->getName());
|
|
|
|
EXPECT_EQ(RRClass::IN(), rrset->getClass());
|
|
|
|
EXPECT_EQ(RRType::SOA(), rrset->getType());
|
|
|
|
EXPECT_EQ(RRTTL(300), rrset->getTTL());
|
|
|
|
RdataIteratorPtr rit(rrset->getRdataIterator());
|
|
|
|
ASSERT_FALSE(rit->isLast());
|
|
|
|
rit->next();
|
|
|
|
EXPECT_TRUE(rit->isLast());
|
|
|
|
|
|
|
|
rrset = it->getNextRRset();
|
|
|
|
ASSERT_NE(ConstRRsetPtr(), rrset);
|
|
|
|
EXPECT_EQ(Name("x.example.org"), rrset->getName());
|
|
|
|
EXPECT_EQ(RRClass::IN(), rrset->getClass());
|
|
|
|
EXPECT_EQ(RRType::A(), rrset->getType());
|
|
|
|
EXPECT_EQ(RRTTL(300), rrset->getTTL());
|
|
|
|
rit = rrset->getRdataIterator();
|
|
|
|
ASSERT_FALSE(rit->isLast());
|
|
|
|
EXPECT_EQ("192.0.2.1", rit->getCurrent().toText());
|
|
|
|
rit->next();
|
|
|
|
ASSERT_FALSE(rit->isLast());
|
|
|
|
EXPECT_EQ("192.0.2.2", rit->getCurrent().toText());
|
|
|
|
rit->next();
|
|
|
|
EXPECT_TRUE(rit->isLast());
|
|
|
|
|
|
|
|
rrset = it->getNextRRset();
|
|
|
|
ASSERT_NE(ConstRRsetPtr(), rrset);
|
|
|
|
EXPECT_EQ(Name("x.example.org"), rrset->getName());
|
|
|
|
EXPECT_EQ(RRClass::IN(), rrset->getClass());
|
|
|
|
EXPECT_EQ(RRType::AAAA(), rrset->getType());
|
|
|
|
EXPECT_EQ(RRTTL(300), rrset->getTTL());
|
|
|
|
EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
|
|
|
|
rit = rrset->getRdataIterator();
|
|
|
|
ASSERT_FALSE(rit->isLast());
|
|
|
|
EXPECT_EQ("2001:db8::1", rit->getCurrent().toText());
|
|
|
|
rit->next();
|
|
|
|
ASSERT_FALSE(rit->isLast());
|
|
|
|
EXPECT_EQ("2001:db8::2", rit->getCurrent().toText());
|
|
|
|
rit->next();
|
|
|
|
EXPECT_TRUE(rit->isLast());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This has inconsistent TTL in the set (the rest, like nonsense in
|
|
|
|
// the data is handled in rdata itself).
|
|
|
|
TEST_F(DatabaseClientTest, badIterator) {
|
|
|
|
ZoneIteratorPtr it(client_->getIterator(Name("bad.example.org")));
|
|
|
|
EXPECT_THROW(it->getNextRRset(), DataSourceError);
|
|
|
|
}
|
|
|
|
|
2011-08-01 11:47:37 +02:00
|
|
|
}
|