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-01 12:03:35 +02:00
|
|
|
#include <exceptions/exceptions.h>
|
2011-08-01 11:47:37 +02:00
|
|
|
|
|
|
|
#include <datasrc/database.h>
|
2011-08-04 14:32:53 +02:00
|
|
|
#include <datasrc/zone.h>
|
|
|
|
#include <datasrc/data_source.h>
|
|
|
|
|
|
|
|
#include <map>
|
2011-08-01 11:47:37 +02:00
|
|
|
|
|
|
|
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:
|
2011-08-04 14:32:53 +02:00
|
|
|
MockConnection() { fillData(); }
|
|
|
|
|
2011-08-01 11:47:37 +02:00
|
|
|
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));
|
|
|
|
} else {
|
|
|
|
return (std::pair<bool, int>(false, 0));
|
|
|
|
}
|
|
|
|
}
|
2011-08-04 14:32:53 +02:00
|
|
|
|
|
|
|
virtual void searchForRecords(int zone_id, const std::string& name) {
|
|
|
|
// we're not aiming for efficiency in this test, simply
|
|
|
|
// copy the relevant vector from records
|
|
|
|
cur_record = 0;
|
|
|
|
|
|
|
|
if (zone_id == 42) {
|
|
|
|
if (records.count(name) > 0) {
|
|
|
|
cur_name = records.find(name)->second;
|
|
|
|
} else {
|
|
|
|
cur_name.clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cur_name.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual bool getNextRecord(std::vector<std::string>& columns) {
|
|
|
|
if (cur_record < cur_name.size()) {
|
|
|
|
columns = cur_name[cur_record++];
|
2011-08-08 01:41:39 -07:00
|
|
|
return (true);
|
2011-08-04 14:32:53 +02:00
|
|
|
} else {
|
2011-08-08 01:41:39 -07:00
|
|
|
return (false);
|
2011-08-04 14:32:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<std::string, std::vector< std::vector<std::string> > > records;
|
|
|
|
// used as internal index for getNextRecord()
|
|
|
|
size_t cur_record;
|
|
|
|
// used as temporary storage after searchForRecord() and during
|
|
|
|
// getNextRecord() calls, as well as during the building of the
|
|
|
|
// fake data
|
|
|
|
std::vector< std::vector<std::string> > cur_name;
|
|
|
|
|
2011-08-04 22:12:04 +02:00
|
|
|
// Adds one record to the current name in the database
|
|
|
|
// The actual data will not be added to 'records' until
|
|
|
|
// addCurName() is called
|
2011-08-04 14:32:53 +02:00
|
|
|
void addRecord(const std::string& name,
|
|
|
|
const std::string& type,
|
|
|
|
const std::string& sigtype,
|
|
|
|
const std::string& rdata) {
|
|
|
|
std::vector<std::string> columns;
|
|
|
|
columns.push_back(name);
|
|
|
|
columns.push_back(type);
|
|
|
|
columns.push_back(sigtype);
|
|
|
|
columns.push_back(rdata);
|
|
|
|
cur_name.push_back(columns);
|
|
|
|
}
|
|
|
|
|
2011-08-04 22:12:04 +02:00
|
|
|
// Adds all records we just built with calls to addRecords
|
|
|
|
// to the actual fake database. This will clear cur_name,
|
|
|
|
// so we can immediately start adding new records.
|
2011-08-04 14:32:53 +02:00
|
|
|
void addCurName(const std::string& name) {
|
2011-08-04 22:12:04 +02:00
|
|
|
ASSERT_EQ(0, records.count(name));
|
2011-08-04 14:32:53 +02:00
|
|
|
records[name] = cur_name;
|
|
|
|
cur_name.clear();
|
|
|
|
}
|
|
|
|
|
2011-08-04 22:12:04 +02:00
|
|
|
// Fills the database with zone data.
|
|
|
|
// This method constructs a number of resource records (with addRecord),
|
|
|
|
// which will all be added for one domain name to the fake database
|
|
|
|
// (with addCurName). So for instance the first set of calls create
|
|
|
|
// data for the name 'www.example.org', which will consist of one A RRset
|
|
|
|
// of one record, and one AAAA RRset of two records.
|
|
|
|
// The order in which they are added is the order in which getNextRecord()
|
|
|
|
// will return them (so we can test whether find() etc. support data that
|
|
|
|
// might not come in 'normal' order)
|
|
|
|
// It shall immediately fail if you try to add the same name twice.
|
2011-08-04 14:32:53 +02:00
|
|
|
void fillData() {
|
2011-08-04 21:27:38 +02:00
|
|
|
// some plain data
|
2011-08-04 14:32:53 +02:00
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addRecord("AAAA", "3600", "", "2001:db8::1");
|
|
|
|
addRecord("AAAA", "3600", "", "2001:db8::2");
|
|
|
|
addCurName("www.example.org.");
|
2011-08-04 22:12:04 +02:00
|
|
|
|
2011-08-04 14:32:53 +02:00
|
|
|
addRecord("CNAME", "3600", "", "www.example.org.");
|
|
|
|
addCurName("cname.example.org.");
|
|
|
|
|
2011-08-04 21:27:38 +02:00
|
|
|
// some DNSSEC-'signed' data
|
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
2011-08-05 18:02:33 +02:00
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
|
2011-08-04 21:27:38 +02:00
|
|
|
addRecord("AAAA", "3600", "", "2001:db8::1");
|
|
|
|
addRecord("AAAA", "3600", "", "2001:db8::2");
|
|
|
|
addRecord("RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addCurName("signed1.example.org.");
|
2011-08-04 21:57:16 +02:00
|
|
|
addRecord("CNAME", "3600", "", "www.example.org.");
|
|
|
|
addRecord("RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addCurName("signedcname1.example.org.");
|
2011-08-05 18:02:33 +02:00
|
|
|
// special case might fail; sig is for cname, which isn't there (should be ignored)
|
|
|
|
// (ignoring of 'normal' other type is done above by www.)
|
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addRecord("RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addCurName("acnamesig1.example.org.");
|
2011-08-04 21:27:38 +02:00
|
|
|
|
|
|
|
// let's pretend we have a database that is not careful
|
|
|
|
// about the order in which it returns data
|
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addRecord("AAAA", "3600", "", "2001:db8::2");
|
2011-08-05 18:02:33 +02:00
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
|
2011-08-04 21:27:38 +02:00
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addRecord("RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addRecord("AAAA", "3600", "", "2001:db8::1");
|
|
|
|
addCurName("signed2.example.org.");
|
|
|
|
addRecord("RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
2011-08-04 21:57:16 +02:00
|
|
|
addRecord("CNAME", "3600", "", "www.example.org.");
|
|
|
|
addCurName("signedcname2.example.org.");
|
2011-08-04 21:27:38 +02:00
|
|
|
|
2011-08-05 18:02:33 +02:00
|
|
|
addRecord("RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addCurName("acnamesig2.example.org.");
|
|
|
|
|
|
|
|
addRecord("RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addRecord("RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
|
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addCurName("acnamesig3.example.org.");
|
|
|
|
|
2011-08-04 14:32:53 +02:00
|
|
|
// also add some intentionally bad data
|
|
|
|
cur_name.push_back(std::vector<std::string>());
|
|
|
|
addCurName("emptyvector.example.org.");
|
|
|
|
addRecord("A", "3600", "", "192.0.2.1");
|
|
|
|
addRecord("CNAME", "3600", "", "www.example.org.");
|
|
|
|
addCurName("badcname.example.org.");
|
2011-08-05 18:02:33 +02:00
|
|
|
addRecord("A", "3600", "", "bad");
|
|
|
|
addCurName("badrdata.example.org.");
|
|
|
|
addRecord("BAD_TYPE", "3600", "", "192.0.2.1");
|
|
|
|
addCurName("badtype.example.org.");
|
|
|
|
addRecord("A", "badttl", "", "192.0.2.1");
|
|
|
|
addCurName("badttl.example.org.");
|
2011-08-04 14:32:53 +02:00
|
|
|
}
|
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() {
|
|
|
|
current_connection_ = new MockConnection();
|
2011-08-04 17:18:54 +02:00
|
|
|
client_.reset(new DatabaseClient(shared_ptr<DatabaseConnection>(
|
2011-08-01 11:47:37 +02:00
|
|
|
current_connection_)));
|
|
|
|
}
|
|
|
|
// Will be deleted by client_, just keep the current value for comparison.
|
|
|
|
MockConnection* current_connection_;
|
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
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2011-08-01 12:03:35 +02:00
|
|
|
TEST_F(DatabaseClientTest, noConnException) {
|
2011-08-04 17:18:54 +02:00
|
|
|
EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseConnection>()),
|
2011-08-01 12:03:35 +02:00
|
|
|
isc::InvalidParameter);
|
|
|
|
}
|
|
|
|
|
2011-08-04 21:57:16 +02:00
|
|
|
namespace {
|
|
|
|
void
|
|
|
|
doFindTest(shared_ptr<DatabaseClient::Finder> finder,
|
|
|
|
const isc::dns::Name& name,
|
|
|
|
const isc::dns::RRType& type,
|
|
|
|
const isc::dns::RRType& expected_type,
|
|
|
|
ZoneFinder::Result expected_result,
|
|
|
|
unsigned int expected_rdata_count,
|
|
|
|
unsigned int expected_signature_count)
|
|
|
|
{
|
|
|
|
ZoneFinder::FindResult result = finder->find(name, type,
|
|
|
|
NULL, ZoneFinder::FIND_DEFAULT);
|
|
|
|
ASSERT_EQ(expected_result, result.code) << name.toText() << " " << type.toText();
|
|
|
|
if (expected_rdata_count > 0) {
|
|
|
|
EXPECT_EQ(expected_rdata_count, result.rrset->getRdataCount());
|
|
|
|
EXPECT_EQ(expected_type, result.rrset->getType());
|
|
|
|
if (expected_signature_count > 0) {
|
|
|
|
EXPECT_EQ(expected_signature_count, result.rrset->getRRsig()->getRdataCount());
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset->getRRsig());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-08-04 14:32:53 +02:00
|
|
|
TEST_F(DatabaseClientTest, find) {
|
|
|
|
DataSourceClient::FindResult zone(client_->findZone(Name("example.org")));
|
|
|
|
ASSERT_EQ(result::SUCCESS, zone.code);
|
|
|
|
shared_ptr<DatabaseClient::Finder> finder(
|
|
|
|
dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
|
|
|
|
EXPECT_EQ(42, finder->zone_id());
|
2011-08-08 01:41:39 -07:00
|
|
|
const isc::dns::Name name("www.example.org.");
|
2011-08-04 14:32:53 +02:00
|
|
|
|
2011-08-04 21:57:16 +02:00
|
|
|
doFindTest(finder, isc::dns::Name("www.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
|
|
|
ZoneFinder::SUCCESS, 1, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("www.example.org."),
|
|
|
|
isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
|
|
|
|
ZoneFinder::SUCCESS, 2, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("www.example.org."),
|
|
|
|
isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
|
|
|
|
ZoneFinder::NXRRSET, 0, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("cname.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::CNAME(),
|
|
|
|
ZoneFinder::CNAME, 1, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("doesnotexist.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
|
|
|
ZoneFinder::NXDOMAIN, 0, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("signed1.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
2011-08-05 18:02:33 +02:00
|
|
|
ZoneFinder::SUCCESS, 1, 2);
|
2011-08-04 21:57:16 +02:00
|
|
|
doFindTest(finder, isc::dns::Name("signed1.example.org."),
|
|
|
|
isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
|
|
|
|
ZoneFinder::SUCCESS, 2, 1);
|
|
|
|
doFindTest(finder, isc::dns::Name("signed1.example.org."),
|
|
|
|
isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
|
|
|
|
ZoneFinder::NXRRSET, 0, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("signedcname1.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::CNAME(),
|
|
|
|
ZoneFinder::CNAME, 1, 1);
|
|
|
|
|
|
|
|
doFindTest(finder, isc::dns::Name("signed2.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
2011-08-05 18:02:33 +02:00
|
|
|
ZoneFinder::SUCCESS, 1, 2);
|
2011-08-04 21:57:16 +02:00
|
|
|
doFindTest(finder, isc::dns::Name("signed2.example.org."),
|
|
|
|
isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
|
|
|
|
ZoneFinder::SUCCESS, 2, 1);
|
|
|
|
doFindTest(finder, isc::dns::Name("signed2.example.org."),
|
|
|
|
isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
|
|
|
|
ZoneFinder::NXRRSET, 0, 0);
|
|
|
|
doFindTest(finder, isc::dns::Name("signedcname2.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::CNAME(),
|
|
|
|
ZoneFinder::CNAME, 1, 1);
|
2011-08-04 21:27:38 +02:00
|
|
|
|
2011-08-05 18:02:33 +02:00
|
|
|
doFindTest(finder, isc::dns::Name("acnamesig1.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
|
|
|
ZoneFinder::SUCCESS, 1, 1);
|
|
|
|
doFindTest(finder, isc::dns::Name("acnamesig2.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
|
|
|
ZoneFinder::SUCCESS, 1, 1);
|
|
|
|
doFindTest(finder, isc::dns::Name("acnamesig3.example.org."),
|
|
|
|
isc::dns::RRType::A(), isc::dns::RRType::A(),
|
|
|
|
ZoneFinder::SUCCESS, 1, 1);
|
|
|
|
|
2011-08-04 14:32:53 +02:00
|
|
|
EXPECT_THROW(finder->find(isc::dns::Name("emptyvector.example.org."),
|
|
|
|
isc::dns::RRType::A(),
|
|
|
|
NULL, ZoneFinder::FIND_DEFAULT),
|
|
|
|
DataSourceError);
|
|
|
|
EXPECT_THROW(finder->find(isc::dns::Name("badcname.example.org."),
|
|
|
|
isc::dns::RRType::A(),
|
|
|
|
NULL, ZoneFinder::FIND_DEFAULT),
|
|
|
|
DataSourceError);
|
2011-08-05 18:02:33 +02:00
|
|
|
EXPECT_THROW(finder->find(isc::dns::Name("badrdata.example.org."),
|
|
|
|
isc::dns::RRType::A(),
|
|
|
|
NULL, ZoneFinder::FIND_DEFAULT),
|
|
|
|
DataSourceError);
|
|
|
|
EXPECT_THROW(finder->find(isc::dns::Name("badtype.example.org."),
|
|
|
|
isc::dns::RRType::A(),
|
|
|
|
NULL, ZoneFinder::FIND_DEFAULT),
|
|
|
|
DataSourceError);
|
|
|
|
EXPECT_THROW(finder->find(isc::dns::Name("badttl.example.org."),
|
|
|
|
isc::dns::RRType::A(),
|
|
|
|
NULL, ZoneFinder::FIND_DEFAULT),
|
|
|
|
DataSourceError);
|
2011-08-04 14:32:53 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-01 11:47:37 +02:00
|
|
|
}
|