command_unittest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include "datasrc_util.h"
  16. #include <auth/auth_srv.h>
  17. #include <auth/auth_config.h>
  18. #include <auth/command.h>
  19. #include <dns/name.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. #include <dns/rrttl.h>
  23. #include <cc/data.h>
  24. #include <config/ccsession.h>
  25. #include <datasrc/memory_datasrc.h>
  26. #include <asiolink/asiolink.h>
  27. #include <util/unittests/mock_socketsession.h>
  28. #include <testutils/mockups.h>
  29. #include <cassert>
  30. #include <cstdlib>
  31. #include <string>
  32. #include <stdexcept>
  33. #include <boost/bind.hpp>
  34. #include <gtest/gtest.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. using namespace std;
  38. using namespace isc::dns;
  39. using namespace isc::data;
  40. using namespace isc::datasrc;
  41. using namespace isc::config;
  42. using namespace isc::util::unittests;
  43. using namespace isc::testutils;
  44. using namespace isc::auth::unittest;
  45. namespace {
  46. class AuthCommandTest : public ::testing::Test {
  47. protected:
  48. AuthCommandTest() :
  49. server_(false, xfrout_, ddns_forwarder_),
  50. rcode_(-1),
  51. expect_rcode_(0),
  52. itimer_(server_.getIOService())
  53. {
  54. server_.setStatisticsSession(&statistics_session_);
  55. }
  56. void checkAnswer(const int expected_code) {
  57. parseAnswer(rcode_, result_);
  58. EXPECT_EQ(expected_code, rcode_) << result_->str();
  59. }
  60. MockSession statistics_session_;
  61. MockXfroutClient xfrout_;
  62. MockSocketSessionForwarder ddns_forwarder_;
  63. AuthSrv server_;
  64. ConstElementPtr result_;
  65. // The shutdown command parameter
  66. ConstElementPtr param_;
  67. int rcode_, expect_rcode_;
  68. isc::asiolink::IntervalTimer itimer_;
  69. public:
  70. void stopServer(); // need to be public for boost::bind
  71. void dontStopServer(); // need to be public for boost::bind
  72. };
  73. TEST_F(AuthCommandTest, unknownCommand) {
  74. result_ = execAuthServerCommand(server_, "no_such_command",
  75. ConstElementPtr());
  76. parseAnswer(rcode_, result_);
  77. EXPECT_EQ(1, rcode_);
  78. }
  79. TEST_F(AuthCommandTest, DISABLED_unexpectedException) {
  80. // execAuthServerCommand() won't catch standard exceptions.
  81. // Skip this test for now: ModuleCCSession doesn't seem to validate
  82. // commands.
  83. EXPECT_THROW(execAuthServerCommand(server_, "_throw_exception",
  84. ConstElementPtr()),
  85. runtime_error);
  86. }
  87. TEST_F(AuthCommandTest, sendStatistics) {
  88. result_ = execAuthServerCommand(server_, "sendstats", ConstElementPtr());
  89. // Just check some message has been sent. Detailed tests specific to
  90. // statistics are done in its own tests.
  91. EXPECT_EQ("Stats", statistics_session_.getMessageDest());
  92. checkAnswer(0);
  93. }
  94. void
  95. AuthCommandTest::stopServer() {
  96. result_ = execAuthServerCommand(server_, "shutdown", param_);
  97. parseAnswer(rcode_, result_);
  98. assert(rcode_ == 0); // make sure the test stops when something is wrong
  99. }
  100. TEST_F(AuthCommandTest, shutdown) {
  101. // Param defaults to empty/null pointer on creation
  102. itimer_.setup(boost::bind(&AuthCommandTest::stopServer, this), 1);
  103. server_.getIOService().run();
  104. EXPECT_EQ(0, rcode_);
  105. }
  106. TEST_F(AuthCommandTest, shutdownCorrectPID) {
  107. // Put the pid parameter there
  108. const pid_t pid(getpid());
  109. ElementPtr param(new isc::data::MapElement());
  110. param->set("pid", ConstElementPtr(new isc::data::IntElement(pid)));
  111. param_ = param;
  112. // With the correct PID, it should act exactly the same as in case
  113. // of no parameter
  114. itimer_.setup(boost::bind(&AuthCommandTest::stopServer, this), 1);
  115. server_.getIOService().run();
  116. EXPECT_EQ(0, rcode_);
  117. }
  118. // This is like stopServer, but the server should not stop after the
  119. // command, it should be running
  120. void
  121. AuthCommandTest::dontStopServer() {
  122. result_ = execAuthServerCommand(server_, "shutdown", param_);
  123. parseAnswer(rcode_, result_);
  124. EXPECT_EQ(expect_rcode_, rcode_);
  125. rcode_ = -1;
  126. // We run the stopServer now, to really stop the server.
  127. // If it had stopped already, it won't be run and the rcode -1 will
  128. // be left here.
  129. param_ = ConstElementPtr();
  130. itimer_.cancel();
  131. itimer_.setup(boost::bind(&AuthCommandTest::stopServer, this), 1);
  132. }
  133. // If we provide something not an int, the PID is not really specified, so
  134. // act as if nothing came.
  135. TEST_F(AuthCommandTest, shutdownNotInt) {
  136. // Put the pid parameter there
  137. ElementPtr param(new isc::data::MapElement());
  138. param->set("pid", ConstElementPtr(new isc::data::StringElement("pid")));
  139. param_ = param;
  140. expect_rcode_ = 1;
  141. // It should reject to stop if the PID is not an int.
  142. itimer_.setup(boost::bind(&AuthCommandTest::dontStopServer, this), 1);
  143. server_.getIOService().run();
  144. EXPECT_EQ(0, rcode_);
  145. }
  146. TEST_F(AuthCommandTest, shutdownIncorrectPID) {
  147. // The PID = 0 should be taken by init, so we are not init and the
  148. // PID should be different
  149. param_ = Element::fromJSON("{\"pid\": 0}");
  150. itimer_.setup(boost::bind(&AuthCommandTest::dontStopServer, this), 1);
  151. server_.getIOService().run();
  152. EXPECT_EQ(0, rcode_);
  153. }
  154. // A helper function commonly used for the "loadzone" command tests.
  155. // It configures the server with a memory data source containing two
  156. // zones, and checks the zones are correctly loaded.
  157. void
  158. zoneChecks(AuthSrv& server) {
  159. EXPECT_TRUE(server.getInMemoryClient(RRClass::IN()));
  160. EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
  161. findZone(Name("ns.test1.example")).zone_finder->
  162. find(Name("ns.test1.example"), RRType::A())->code);
  163. EXPECT_EQ(ZoneFinder::NXRRSET, server.getInMemoryClient(RRClass::IN())->
  164. findZone(Name("ns.test1.example")).zone_finder->
  165. find(Name("ns.test1.example"), RRType::AAAA())->code);
  166. EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
  167. findZone(Name("ns.test2.example")).zone_finder->
  168. find(Name("ns.test2.example"), RRType::A())->code);
  169. EXPECT_EQ(ZoneFinder::NXRRSET, server.getInMemoryClient(RRClass::IN())->
  170. findZone(Name("ns.test2.example")).zone_finder->
  171. find(Name("ns.test2.example"), RRType::AAAA())->code);
  172. }
  173. void
  174. configureZones(AuthSrv& server) {
  175. ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR "/test1.zone.in "
  176. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  177. ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR "/test2.zone.in "
  178. TEST_DATA_BUILDDIR "/test2.zone.copied"));
  179. configureAuthServer(server, Element::fromJSON(
  180. "{\"datasources\": "
  181. " [{\"type\": \"memory\","
  182. " \"zones\": "
  183. "[{\"origin\": \"test1.example\","
  184. " \"file\": \""
  185. TEST_DATA_BUILDDIR "/test1.zone.copied\"},"
  186. " {\"origin\": \"test2.example\","
  187. " \"file\": \""
  188. TEST_DATA_BUILDDIR "/test2.zone.copied\"}"
  189. "]}]}"));
  190. zoneChecks(server);
  191. }
  192. void
  193. newZoneChecks(AuthSrv& server) {
  194. EXPECT_TRUE(server.getInMemoryClient(RRClass::IN()));
  195. EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
  196. findZone(Name("ns.test1.example")).zone_finder->
  197. find(Name("ns.test1.example"), RRType::A())->code);
  198. // now test1.example should have ns/AAAA
  199. EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
  200. findZone(Name("ns.test1.example")).zone_finder->
  201. find(Name("ns.test1.example"), RRType::AAAA())->code);
  202. // test2.example shouldn't change
  203. EXPECT_EQ(ZoneFinder::SUCCESS, server.getInMemoryClient(RRClass::IN())->
  204. findZone(Name("ns.test2.example")).zone_finder->
  205. find(Name("ns.test2.example"), RRType::A())->code);
  206. EXPECT_EQ(ZoneFinder::NXRRSET, server.getInMemoryClient(RRClass::IN())->
  207. findZone(Name("ns.test2.example")).zone_finder->
  208. find(Name("ns.test2.example"), RRType::AAAA())->code);
  209. }
  210. TEST_F(AuthCommandTest,
  211. #ifdef USE_STATIC_LINK
  212. DISABLED_loadZone
  213. #else
  214. loadZone
  215. #endif
  216. )
  217. {
  218. configureZones(server_);
  219. ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR
  220. "/test1-new.zone.in "
  221. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  222. ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR
  223. "/test2-new.zone.in "
  224. TEST_DATA_BUILDDIR "/test2.zone.copied"));
  225. result_ = execAuthServerCommand(server_, "loadzone",
  226. Element::fromJSON(
  227. "{\"origin\": \"test1.example\"}"));
  228. checkAnswer(0);
  229. newZoneChecks(server_);
  230. }
  231. TEST_F(AuthCommandTest,
  232. #ifdef USE_STATIC_LINK
  233. DISABLED_loadZoneSQLite3
  234. #else
  235. loadZoneSQLite3
  236. #endif
  237. )
  238. {
  239. const char* const SPEC_FILE = AUTH_OBJ_DIR "/auth.spec";
  240. // Prepare the database first
  241. const string test_db = TEST_DATA_BUILDDIR "/auth_test.sqlite3.copied";
  242. const string bad_db = TEST_DATA_BUILDDIR "/does-not-exist.sqlite3";
  243. stringstream ss("example.org. 3600 IN SOA . . 0 0 0 0 0\n");
  244. createSQLite3DB(RRClass::IN(), Name("example.org"), test_db.c_str(), ss);
  245. // Then store a config of the zone to the auth server
  246. // This omits many config options of the auth server, but these are
  247. // not read now.
  248. isc::testutils::MockSession session;
  249. // The session should not take care of anything or start anything, we
  250. // need it only to hold the config we're going to put into it.
  251. ModuleCCSession module_session(SPEC_FILE, session, NULL, NULL, false,
  252. false);
  253. // This describes the data source in the configuration
  254. const ElementPtr
  255. map(Element::fromJSON("{\"datasources\": ["
  256. " {"
  257. " \"type\": \"memory\","
  258. " \"zones\": ["
  259. " {"
  260. " \"origin\": \"example.org\","
  261. " \"file\": \"" + test_db + "\","
  262. " \"filetype\": \"sqlite3\""
  263. " }"
  264. " ]"
  265. " }"
  266. "],"
  267. " \"database_file\": \"" + test_db + "\""
  268. "}"));
  269. module_session.setLocalConfig(map);
  270. server_.setConfigSession(&module_session);
  271. server_.updateConfig(map);
  272. // Check that the A record at www.example.org does not exist
  273. ASSERT_TRUE(server_.hasInMemoryClient());
  274. EXPECT_EQ(ZoneFinder::NXDOMAIN, server_.getInMemoryClient(RRClass::IN())->
  275. findZone(Name("example.org")).zone_finder->
  276. find(Name("www.example.org"), RRType::A())->code);
  277. // Add the record to the underlying sqlite database, by loading
  278. // it as a separate datasource, and updating it
  279. ConstElementPtr sql_cfg = Element::fromJSON("{ \"type\": \"sqlite3\","
  280. "\"database_file\": \""
  281. + test_db + "\"}");
  282. DataSourceClientContainer sql_ds("sqlite3", sql_cfg);
  283. ZoneUpdaterPtr sql_updater =
  284. sql_ds.getInstance().getUpdater(Name("example.org"), false);
  285. RRsetPtr rrset(new RRset(Name("www.example.org."), RRClass::IN(),
  286. RRType::A(), RRTTL(60)));
  287. rrset->addRdata(rdata::createRdata(rrset->getType(),
  288. rrset->getClass(),
  289. "192.0.2.1"));
  290. sql_updater->addRRset(*rrset);
  291. sql_updater->commit();
  292. // This new record is in the database now, but should not be in the
  293. // memory-datasource yet, so check again
  294. EXPECT_EQ(ZoneFinder::NXDOMAIN, server_.getInMemoryClient(RRClass::IN())->
  295. findZone(Name("example.org")).zone_finder->
  296. find(Name("www.example.org"), RRType::A())->code);
  297. // Now send the command to reload it
  298. result_ = execAuthServerCommand(server_, "loadzone",
  299. Element::fromJSON(
  300. "{\"origin\": \"example.org\"}"));
  301. checkAnswer(0);
  302. // And now it should be present too.
  303. EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
  304. findZone(Name("example.org")).zone_finder->
  305. find(Name("www.example.org"), RRType::A())->code);
  306. // Some error cases. First, the zone has no configuration. (note .com here)
  307. result_ = execAuthServerCommand(server_, "loadzone",
  308. Element::fromJSON("{\"origin\": \"example.com\"}"));
  309. checkAnswer(1);
  310. // The previous zone is not hurt in any way
  311. EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
  312. findZone(Name("example.org")).zone_finder->
  313. find(Name("example.org"), RRType::SOA())->code);
  314. module_session.setLocalConfig(Element::fromJSON("{\"datasources\": []}"));
  315. result_ = execAuthServerCommand(server_, "loadzone",
  316. Element::fromJSON(
  317. "{\"origin\": \"example.org\"}"));
  318. checkAnswer(1);
  319. // The previous zone is not hurt in any way
  320. EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
  321. findZone(Name("example.org")).zone_finder->
  322. find(Name("example.org"), RRType::SOA())->code);
  323. // Configure an unreadable zone. Should fail, but leave the original zone
  324. // data there
  325. const ElementPtr
  326. mapBad(Element::fromJSON("{\"datasources\": ["
  327. " {"
  328. " \"type\": \"memory\","
  329. " \"zones\": ["
  330. " {"
  331. " \"origin\": \"example.org\","
  332. " \"file\": \"" + bad_db + "\","
  333. " \"filetype\": \"sqlite3\""
  334. " }"
  335. " ]"
  336. " }"
  337. "]}"));
  338. module_session.setLocalConfig(mapBad);
  339. result_ = execAuthServerCommand(server_, "loadzone",
  340. Element::fromJSON("{\"origin\": \"example.com\"}"));
  341. checkAnswer(1);
  342. // The previous zone is not hurt in any way
  343. EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
  344. findZone(Name("example.org")).zone_finder->
  345. find(Name("example.org"), RRType::SOA())->code);
  346. // Broken configuration (not valid against the spec)
  347. const ElementPtr
  348. broken(Element::fromJSON("{\"datasources\": ["
  349. " {"
  350. " \"type\": \"memory\","
  351. " \"zones\": [[]]"
  352. " }"
  353. "]}"));
  354. module_session.setLocalConfig(broken);
  355. checkAnswer(1);
  356. // The previous zone is not hurt in any way
  357. EXPECT_EQ(ZoneFinder::SUCCESS, server_.getInMemoryClient(RRClass::IN())->
  358. findZone(Name("example.org")).zone_finder->
  359. find(Name("example.org"), RRType::SOA())->code);
  360. }
  361. TEST_F(AuthCommandTest,
  362. #ifdef USE_STATIC_LINK
  363. DISABLED_loadBrokenZone
  364. #else
  365. loadBrokenZone
  366. #endif
  367. )
  368. {
  369. configureZones(server_);
  370. ASSERT_EQ(0, system(INSTALL_PROG " -c " TEST_DATA_DIR
  371. "/test1-broken.zone.in "
  372. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  373. result_ = execAuthServerCommand(server_, "loadzone",
  374. Element::fromJSON(
  375. "{\"origin\": \"test1.example\"}"));
  376. checkAnswer(1);
  377. zoneChecks(server_); // zone shouldn't be replaced
  378. }
  379. TEST_F(AuthCommandTest,
  380. #ifdef USE_STATIC_LINK
  381. DISABLED_loadUnreadableZone
  382. #else
  383. loadUnreadableZone
  384. #endif
  385. )
  386. {
  387. configureZones(server_);
  388. // install the zone file as unreadable
  389. ASSERT_EQ(0, system(INSTALL_PROG " -c -m 000 " TEST_DATA_DIR
  390. "/test1.zone.in "
  391. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  392. result_ = execAuthServerCommand(server_, "loadzone",
  393. Element::fromJSON(
  394. "{\"origin\": \"test1.example\"}"));
  395. checkAnswer(1);
  396. zoneChecks(server_); // zone shouldn't be replaced
  397. }
  398. TEST_F(AuthCommandTest, loadZoneWithoutDataSrc) {
  399. // try to execute load command without configuring the zone beforehand.
  400. // it should fail.
  401. result_ = execAuthServerCommand(server_, "loadzone",
  402. Element::fromJSON(
  403. "{\"origin\": \"test1.example\"}"));
  404. checkAnswer(1);
  405. }
  406. TEST_F(AuthCommandTest, loadSqlite3DataSrc) {
  407. // For sqlite3 data source we don't have to do anything (the data source
  408. // (re)loads itself automatically)
  409. result_ = execAuthServerCommand(server_, "loadzone",
  410. Element::fromJSON(
  411. "{\"origin\": \"test1.example\","
  412. " \"datasrc\": \"sqlite3\"}"));
  413. checkAnswer(0);
  414. }
  415. TEST_F(AuthCommandTest,
  416. #ifdef USE_STATIC_LINK
  417. DISABLED_loadZoneInvalidParams
  418. #else
  419. loadZoneInvalidParams
  420. #endif
  421. )
  422. {
  423. configureZones(server_);
  424. // null arg
  425. result_ = execAuthServerCommand(server_, "loadzone", ElementPtr());
  426. checkAnswer(1);
  427. // zone class is bogus
  428. result_ = execAuthServerCommand(server_, "loadzone",
  429. Element::fromJSON(
  430. "{\"origin\": \"test1.example\","
  431. " \"class\": \"no_such_class\"}"));
  432. checkAnswer(1);
  433. result_ = execAuthServerCommand(server_, "loadzone",
  434. Element::fromJSON(
  435. "{\"origin\": \"test1.example\","
  436. " \"class\": 1}"));
  437. checkAnswer(1);
  438. // unsupported zone class
  439. result_ = execAuthServerCommand(server_, "loadzone",
  440. Element::fromJSON(
  441. "{\"origin\": \"test1.example\","
  442. " \"class\": \"CH\"}"));
  443. checkAnswer(1);
  444. // unsupported data source class
  445. result_ = execAuthServerCommand(server_, "loadzone",
  446. Element::fromJSON(
  447. "{\"origin\": \"test1.example\","
  448. " \"datasrc\": \"not supported\"}"));
  449. checkAnswer(1);
  450. // data source is bogus
  451. result_ = execAuthServerCommand(server_, "loadzone",
  452. Element::fromJSON(
  453. "{\"origin\": \"test1.example\","
  454. " \"datasrc\": 0}"));
  455. checkAnswer(1);
  456. // origin is missing
  457. result_ = execAuthServerCommand(server_, "loadzone",
  458. Element::fromJSON("{}"));
  459. checkAnswer(1);
  460. // zone doesn't exist in the data source
  461. result_ = execAuthServerCommand(server_, "loadzone",
  462. Element::fromJSON("{\"origin\": \"xx\"}"));
  463. checkAnswer(1);
  464. // origin is bogus
  465. result_ = execAuthServerCommand(server_, "loadzone",
  466. Element::fromJSON(
  467. "{\"origin\": \"...\"}"));
  468. checkAnswer(1);
  469. result_ = execAuthServerCommand(server_, "loadzone",
  470. Element::fromJSON("{\"origin\": 10}"));
  471. checkAnswer(1);
  472. }
  473. }