123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- // Copyright (C) 2010 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 <config.h>
- #include "datasrc_util.h"
- #include <auth/auth_srv.h>
- #include <auth/auth_config.h>
- #include <auth/command.h>
- #include <dns/name.h>
- #include <dns/rrclass.h>
- #include <dns/rrtype.h>
- #include <dns/rrttl.h>
- #include <cc/data.h>
- #include <config/ccsession.h>
- #include <datasrc/memory_datasrc.h>
- #include <asiolink/asiolink.h>
- #include <util/unittests/mock_socketsession.h>
- #include <testutils/mockups.h>
- #include <cassert>
- #include <cstdlib>
- #include <string>
- #include <stdexcept>
- #include <boost/bind.hpp>
- #include <gtest/gtest.h>
- #include <sys/types.h>
- #include <unistd.h>
- using namespace std;
- using namespace isc::dns;
- using namespace isc::data;
- using namespace isc::datasrc;
- using namespace isc::config;
- using namespace isc::util::unittests;
- using namespace isc::testutils;
- using namespace isc::auth::unittest;
- namespace {
- class AuthCommandTest : public ::testing::Test {
- protected:
- AuthCommandTest() :
- server_(false, xfrout_, ddns_forwarder_),
- rcode_(-1),
- expect_rcode_(0),
- itimer_(server_.getIOService())
- {
- server_.setStatisticsSession(&statistics_session_);
- }
- void checkAnswer(const int expected_code) {
- parseAnswer(rcode_, result_);
- EXPECT_EQ(expected_code, rcode_) << result_->str();
- }
- MockSession statistics_session_;
- MockXfroutClient xfrout_;
- MockSocketSessionForwarder ddns_forwarder_;
- AuthSrv server_;
- ConstElementPtr result_;
- // The shutdown command parameter
- ConstElementPtr param_;
- int rcode_, expect_rcode_;
- isc::asiolink::IntervalTimer itimer_;
- public:
- void stopServer(); // need to be public for boost::bind
- void dontStopServer(); // need to be public for boost::bind
- };
- TEST_F(AuthCommandTest, unknownCommand) {
- result_ = execAuthServerCommand(server_, "no_such_command",
- ConstElementPtr());
- parseAnswer(rcode_, result_);
- EXPECT_EQ(1, rcode_);
- }
- TEST_F(AuthCommandTest, DISABLED_unexpectedException) {
- // execAuthServerCommand() won't catch standard exceptions.
- // Skip this test for now: ModuleCCSession doesn't seem to validate
- // commands.
- EXPECT_THROW(execAuthServerCommand(server_, "_throw_exception",
- ConstElementPtr()),
- runtime_error);
- }
- TEST_F(AuthCommandTest, sendStatistics) {
- result_ = execAuthServerCommand(server_, "sendstats", ConstElementPtr());
- // Just check some message has been sent. Detailed tests specific to
- // statistics are done in its own tests.
- EXPECT_EQ("Stats", statistics_session_.getMessageDest());
- checkAnswer(0);
- }
- void
- AuthCommandTest::stopServer() {
- result_ = execAuthServerCommand(server_, "shutdown", param_);
- parseAnswer(rcode_, result_);
- assert(rcode_ == 0); // make sure the test stops when something is wrong
- }
- TEST_F(AuthCommandTest, shutdown) {
- // Param defaults to empty/null pointer on creation
- itimer_.setup(boost::bind(&AuthCommandTest::stopServer, this), 1);
- server_.getIOService().run();
- EXPECT_EQ(0, rcode_);
- }
- TEST_F(AuthCommandTest, shutdownCorrectPID) {
- // Put the pid parameter there
- const pid_t pid(getpid());
- ElementPtr param(new isc::data::MapElement());
- param->set("pid", ConstElementPtr(new isc::data::IntElement(pid)));
- param_ = param;
- // With the correct PID, it should act exactly the same as in case
- // of no parameter
- itimer_.setup(boost::bind(&AuthCommandTest::stopServer, this), 1);
- server_.getIOService().run();
- EXPECT_EQ(0, rcode_);
- }
- // This is like stopServer, but the server should not stop after the
- // command, it should be running
- void
- AuthCommandTest::dontStopServer() {
- result_ = execAuthServerCommand(server_, "shutdown", param_);
- parseAnswer(rcode_, result_);
- EXPECT_EQ(expect_rcode_, rcode_);
- rcode_ = -1;
- // We run the stopServer now, to really stop the server.
- // If it had stopped already, it won't be run and the rcode -1 will
- // be left here.
- param_ = ConstElementPtr();
- itimer_.cancel();
- itimer_.setup(boost::bind(&AuthCommandTest::stopServer, this), 1);
- }
- // If we provide something not an int, the PID is not really specified, so
- // act as if nothing came.
- TEST_F(AuthCommandTest, shutdownNotInt) {
- // Put the pid parameter there
- ElementPtr param(new isc::data::MapElement());
- param->set("pid", ConstElementPtr(new isc::data::StringElement("pid")));
- param_ = param;
- expect_rcode_ = 1;
- // It should reject to stop if the PID is not an int.
- itimer_.setup(boost::bind(&AuthCommandTest::dontStopServer, this), 1);
- server_.getIOService().run();
- EXPECT_EQ(0, rcode_);
- }
- TEST_F(AuthCommandTest, shutdownIncorrectPID) {
- // The PID = 0 should be taken by init, so we are not init and the
- // PID should be different
- param_ = Element::fromJSON("{\"pid\": 0}");
- itimer_.setup(boost::bind(&AuthCommandTest::dontStopServer, this), 1);
- server_.getIOService().run();
- EXPECT_EQ(0, rcode_);
- }
- // A helper function commonly used for the "loadzone" command tests.
- // It configures the server with a memory data source containing two
- // zones, and checks the zones are correctly loaded.
- void
- zoneChecks(AuthSrv& server) {
- EXPECT_TRUE(server.getInMemoryClient(RRClass::IN()));
- EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test1.example")).zone_finder->
- find(Name("ns.test1.example"), RRType::A())->code);
- EXPECT_EQ(ZoneFinder::NXRRSET, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test1.example")).zone_finder->
- find(Name("ns.test1.example"), RRType::AAAA())->code);
- EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test2.example")).zone_finder->
- find(Name("ns.test2.example"), RRType::A())->code);
- EXPECT_EQ(ZoneFinder::NXRRSET, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test2.example")).zone_finder->
- find(Name("ns.test2.example"), RRType::AAAA())->code);
- }
- void
- configureZones(AuthSrv& server) {
- ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR "/test1.zone.in "
- TEST_DATA_BUILDDIR "/test1.zone.copied"));
- ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR "/test2.zone.in "
- TEST_DATA_BUILDDIR "/test2.zone.copied"));
- configureAuthServer(server, Element::fromJSON(
- "{\"datasources\": "
- " [{\"type\": \"memory\","
- " \"zones\": "
- "[{\"origin\": \"test1.example\","
- " \"file\": \""
- TEST_DATA_BUILDDIR "/test1.zone.copied\"},"
- " {\"origin\": \"test2.example\","
- " \"file\": \""
- TEST_DATA_BUILDDIR "/test2.zone.copied\"}"
- "]}]}"));
- zoneChecks(server);
- }
- void
- newZoneChecks(AuthSrv& server) {
- EXPECT_TRUE(server.getInMemoryClient(RRClass::IN()));
- EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test1.example")).zone_finder->
- find(Name("ns.test1.example"), RRType::A())->code);
- // now test1.example should have ns/AAAA
- EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test1.example")).zone_finder->
- find(Name("ns.test1.example"), RRType::AAAA())->code);
- // test2.example shouldn't change
- EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test2.example")).zone_finder->
- find(Name("ns.test2.example"), RRType::A())->code);
- EXPECT_EQ(ZoneFinder::NXRRSET, server.getInMemoryClient(RRClass::IN())->
- findZone(Name("ns.test2.example")).zone_finder->
- find(Name("ns.test2.example"), RRType::AAAA())->code);
- }
- TEST_F(AuthCommandTest,
- #ifdef USE_STATIC_LINK
- DISABLED_loadZone
- #else
- loadZone
- #endif
- )
- {
- configureZones(server_);
- ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR
- "/test1-new.zone.in "
- TEST_DATA_BUILDDIR "/test1.zone.copied"));
- ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR
- "/test2-new.zone.in "
- TEST_DATA_BUILDDIR "/test2.zone.copied"));
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\"}"));
- checkAnswer(0);
- newZoneChecks(server_);
- }
- TEST_F(AuthCommandTest,
- #ifdef USE_STATIC_LINK
- DISABLED_loadZoneSQLite3
- #else
- loadZoneSQLite3
- #endif
- )
- {
- const char* const SPEC_FILE = AUTH_OBJ_DIR "/auth.spec";
- // Prepare the database first
- const string test_db = TEST_DATA_BUILDDIR "/auth_test.sqlite3.copied";
- const string bad_db = TEST_DATA_BUILDDIR "/does-not-exist.sqlite3";
- stringstream ss("example.org. 3600 IN SOA . . 0 0 0 0 0\n");
- createSQLite3DB(RRClass::IN(), Name("example.org"), test_db.c_str(), ss);
- // Then store a config of the zone to the auth server
- // This omits many config options of the auth server, but these are
- // not read now.
- isc::testutils::MockSession session;
- // The session should not take care of anything or start anything, we
- // need it only to hold the config we're going to put into it.
- ModuleCCSession module_session(SPEC_FILE, session, NULL, NULL, false,
- false);
- // This describes the data source in the configuration
- const ElementPtr
- map(Element::fromJSON("{\"datasources\": ["
- " {"
- " \"type\": \"memory\","
- " \"zones\": ["
- " {"
- " \"origin\": \"example.org\","
- " \"file\": \"" + test_db + "\","
- " \"filetype\": \"sqlite3\""
- " }"
- " ]"
- " }"
- "],"
- " \"database_file\": \"" + test_db + "\""
- "}"));
- module_session.setLocalConfig(map);
- server_.setConfigSession(&module_session);
- server_.updateConfig(map);
- // Check that the A record at www.example.org does not exist
- ASSERT_TRUE(server_.hasInMemoryClient());
- EXPECT_EQ(ZoneFinder::NXDOMAIN, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("www.example.org"), RRType::A())->code);
- // Add the record to the underlying sqlite database, by loading
- // it as a separate datasource, and updating it
- ConstElementPtr sql_cfg = Element::fromJSON("{ \"type\": \"sqlite3\","
- "\"database_file\": \""
- + test_db + "\"}");
- DataSourceClientContainer sql_ds("sqlite3", sql_cfg);
- ZoneUpdaterPtr sql_updater =
- sql_ds.getInstance().getUpdater(Name("example.org"), false);
- RRsetPtr rrset(new RRset(Name("www.example.org."), RRClass::IN(),
- RRType::A(), RRTTL(60)));
- rrset->addRdata(rdata::createRdata(rrset->getType(),
- rrset->getClass(),
- "192.0.2.1"));
- sql_updater->addRRset(*rrset);
- sql_updater->commit();
- // This new record is in the database now, but should not be in the
- // memory-datasource yet, so check again
- EXPECT_EQ(ZoneFinder::NXDOMAIN, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("www.example.org"), RRType::A())->code);
- // Now send the command to reload it
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"example.org\"}"));
- checkAnswer(0);
- // And now it should be present too.
- EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("www.example.org"), RRType::A())->code);
- // Some error cases. First, the zone has no configuration. (note .com here)
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON("{\"origin\": \"example.com\"}"));
- checkAnswer(1);
- // The previous zone is not hurt in any way
- EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("example.org"), RRType::SOA())->code);
- module_session.setLocalConfig(Element::fromJSON("{\"datasources\": []}"));
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"example.org\"}"));
- checkAnswer(1);
- // The previous zone is not hurt in any way
- EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("example.org"), RRType::SOA())->code);
- // Configure an unreadable zone. Should fail, but leave the original zone
- // data there
- const ElementPtr
- mapBad(Element::fromJSON("{\"datasources\": ["
- " {"
- " \"type\": \"memory\","
- " \"zones\": ["
- " {"
- " \"origin\": \"example.org\","
- " \"file\": \"" + bad_db + "\","
- " \"filetype\": \"sqlite3\""
- " }"
- " ]"
- " }"
- "]}"));
- module_session.setLocalConfig(mapBad);
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON("{\"origin\": \"example.com\"}"));
- checkAnswer(1);
- // The previous zone is not hurt in any way
- EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("example.org"), RRType::SOA())->code);
- // Broken configuration (not valid against the spec)
- const ElementPtr
- broken(Element::fromJSON("{\"datasources\": ["
- " {"
- " \"type\": \"memory\","
- " \"zones\": [[]]"
- " }"
- "]}"));
- module_session.setLocalConfig(broken);
- checkAnswer(1);
- // The previous zone is not hurt in any way
- EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
- findZone(Name("example.org")).zone_finder->
- find(Name("example.org"), RRType::SOA())->code);
- }
- TEST_F(AuthCommandTest,
- #ifdef USE_STATIC_LINK
- DISABLED_loadBrokenZone
- #else
- loadBrokenZone
- #endif
- )
- {
- configureZones(server_);
- ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR
- "/test1-broken.zone.in "
- TEST_DATA_BUILDDIR "/test1.zone.copied"));
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\"}"));
- checkAnswer(1);
- zoneChecks(server_); // zone shouldn't be replaced
- }
- TEST_F(AuthCommandTest,
- #ifdef USE_STATIC_LINK
- DISABLED_loadUnreadableZone
- #else
- loadUnreadableZone
- #endif
- )
- {
- configureZones(server_);
- // install the zone file as unreadable
- ASSERT_EQ(0, system(INSTALL_PROG " -c -m 000 " TEST_DATA_DIR
- "/test1.zone.in "
- TEST_DATA_BUILDDIR "/test1.zone.copied"));
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\"}"));
- checkAnswer(1);
- zoneChecks(server_); // zone shouldn't be replaced
- }
- TEST_F(AuthCommandTest, loadZoneWithoutDataSrc) {
- // try to execute load command without configuring the zone beforehand.
- // it should fail.
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\"}"));
- checkAnswer(1);
- }
- TEST_F(AuthCommandTest, loadSqlite3DataSrc) {
- // For sqlite3 data source we don't have to do anything (the data source
- // (re)loads itself automatically)
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\","
- " \"datasrc\": \"sqlite3\"}"));
- checkAnswer(0);
- }
- TEST_F(AuthCommandTest,
- #ifdef USE_STATIC_LINK
- DISABLED_loadZoneInvalidParams
- #else
- loadZoneInvalidParams
- #endif
- )
- {
- configureZones(server_);
- // null arg
- result_ = execAuthServerCommand(server_, "loadzone", ElementPtr());
- checkAnswer(1);
- // zone class is bogus
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\","
- " \"class\": \"no_such_class\"}"));
- checkAnswer(1);
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\","
- " \"class\": 1}"));
- checkAnswer(1);
- // unsupported zone class
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\","
- " \"class\": \"CH\"}"));
- checkAnswer(1);
- // unsupported data source class
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\","
- " \"datasrc\": \"not supported\"}"));
- checkAnswer(1);
- // data source is bogus
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"test1.example\","
- " \"datasrc\": 0}"));
- checkAnswer(1);
- // origin is missing
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON("{}"));
- checkAnswer(1);
- // zone doesn't exist in the data source
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON("{\"origin\": \"xx\"}"));
- checkAnswer(1);
- // origin is bogus
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON(
- "{\"origin\": \"...\"}"));
- checkAnswer(1);
- result_ = execAuthServerCommand(server_, "loadzone",
- Element::fromJSON("{\"origin\": 10}"));
- checkAnswer(1);
- }
- }
|