command_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <cassert>
  15. #include <cstdlib>
  16. #include <string>
  17. #include <stdexcept>
  18. #include <boost/bind.hpp>
  19. #include <gtest/gtest.h>
  20. #include <dns/name.h>
  21. #include <dns/rrclass.h>
  22. #include <dns/rrtype.h>
  23. #include <cc/data.h>
  24. #include <config/ccsession.h>
  25. #include <datasrc/memory_datasrc.h>
  26. #include <auth/auth_srv.h>
  27. #include <auth/config.h>
  28. #include <auth/command.h>
  29. #include <asiolink/asiolink.h>
  30. #include <testutils/mockups.h>
  31. using namespace std;
  32. using namespace isc::dns;
  33. using namespace isc::data;
  34. using namespace isc::datasrc;
  35. using namespace isc::config;
  36. namespace {
  37. class AuthConmmandTest : public ::testing::Test {
  38. protected:
  39. AuthConmmandTest() : server(false, xfrout), rcode(-1) {
  40. server.setStatisticsSession(&statistics_session);
  41. }
  42. void checkAnswer(const int expected_code) {
  43. parseAnswer(rcode, result);
  44. EXPECT_EQ(expected_code, rcode);
  45. }
  46. MockSession statistics_session;
  47. MockXfroutClient xfrout;
  48. AuthSrv server;
  49. AuthSrv::ConstMemoryDataSrcPtr memory_datasrc;
  50. ConstElementPtr result;
  51. int rcode;
  52. public:
  53. void stopServer(); // need to be public for boost::bind
  54. };
  55. TEST_F(AuthConmmandTest, unknownCommand) {
  56. result = execAuthServerCommand(server, "no_such_command",
  57. ConstElementPtr());
  58. parseAnswer(rcode, result);
  59. EXPECT_EQ(1, rcode);
  60. }
  61. TEST_F(AuthConmmandTest, DISABLED_unexpectedException) {
  62. // execAuthServerCommand() won't catch standard exceptions.
  63. // Skip this test for now: ModuleCCSession doesn't seem to validate
  64. // commands.
  65. EXPECT_THROW(execAuthServerCommand(server, "_throw_exception",
  66. ConstElementPtr()),
  67. runtime_error);
  68. }
  69. TEST_F(AuthConmmandTest, sendStatistics) {
  70. result = execAuthServerCommand(server, "sendstats", ConstElementPtr());
  71. // Just check some message has been sent. Detailed tests specific to
  72. // statistics are done in its own tests.
  73. EXPECT_EQ("Stats", statistics_session.getMessageDest());
  74. checkAnswer(0);
  75. }
  76. void
  77. AuthConmmandTest::stopServer() {
  78. result = execAuthServerCommand(server, "shutdown", ConstElementPtr());
  79. parseAnswer(rcode, result);
  80. assert(rcode == 0); // make sure the test stops when something is wrong
  81. }
  82. TEST_F(AuthConmmandTest, shutdown) {
  83. asiolink::IntervalTimer itimer(server.getIOService());
  84. itimer.setup(boost::bind(&AuthConmmandTest::stopServer, this), 1);
  85. server.getIOService().run();
  86. EXPECT_EQ(0, rcode);
  87. }
  88. // A helper function commonly used for the "loadzone" command tests.
  89. // It configures the server with a memory data source containing two
  90. // zones, and checks the zones are correctly loaded.
  91. void
  92. zoneChecks(AuthSrv& server) {
  93. EXPECT_TRUE(server.getMemoryDataSrc(RRClass::IN()));
  94. EXPECT_EQ(Zone::SUCCESS, server.getMemoryDataSrc(RRClass::IN())->
  95. findZone(Name("ns.test1.example")).zone->
  96. find(Name("ns.test1.example"), RRType::A()).code);
  97. EXPECT_EQ(Zone::NXRRSET, server.getMemoryDataSrc(RRClass::IN())->
  98. findZone(Name("ns.test1.example")).zone->
  99. find(Name("ns.test1.example"), RRType::AAAA()).code);
  100. EXPECT_EQ(Zone::SUCCESS, server.getMemoryDataSrc(RRClass::IN())->
  101. findZone(Name("ns.test2.example")).zone->
  102. find(Name("ns.test2.example"), RRType::A()).code);
  103. EXPECT_EQ(Zone::NXRRSET, server.getMemoryDataSrc(RRClass::IN())->
  104. findZone(Name("ns.test2.example")).zone->
  105. find(Name("ns.test2.example"), RRType::AAAA()).code);
  106. }
  107. void
  108. configureZones(AuthSrv& server) {
  109. ASSERT_EQ(0, system(INSTALL_PROG " " TEST_DATA_DIR "/test1.zone.in "
  110. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  111. ASSERT_EQ(0, system(INSTALL_PROG " " TEST_DATA_DIR "/test2.zone.in "
  112. TEST_DATA_BUILDDIR "/test2.zone.copied"));
  113. configureAuthServer(server, Element::fromJSON(
  114. "{\"datasources\": "
  115. " [{\"type\": \"memory\","
  116. " \"zones\": "
  117. "[{\"origin\": \"test1.example\","
  118. " \"file\": \""
  119. TEST_DATA_BUILDDIR "/test1.zone.copied\"},"
  120. " {\"origin\": \"test2.example\","
  121. " \"file\": \""
  122. TEST_DATA_BUILDDIR "/test2.zone.copied\"}"
  123. "]}]}"));
  124. zoneChecks(server);
  125. }
  126. void
  127. newZoneChecks(AuthSrv& server) {
  128. EXPECT_TRUE(server.getMemoryDataSrc(RRClass::IN()));
  129. EXPECT_EQ(Zone::SUCCESS, server.getMemoryDataSrc(RRClass::IN())->
  130. findZone(Name("ns.test1.example")).zone->
  131. find(Name("ns.test1.example"), RRType::A()).code);
  132. // now test1.example should have ns/AAAA
  133. EXPECT_EQ(Zone::SUCCESS, server.getMemoryDataSrc(RRClass::IN())->
  134. findZone(Name("ns.test1.example")).zone->
  135. find(Name("ns.test1.example"), RRType::AAAA()).code);
  136. // test2.example shouldn't change
  137. EXPECT_EQ(Zone::SUCCESS, server.getMemoryDataSrc(RRClass::IN())->
  138. findZone(Name("ns.test2.example")).zone->
  139. find(Name("ns.test2.example"), RRType::A()).code);
  140. EXPECT_EQ(Zone::NXRRSET, server.getMemoryDataSrc(RRClass::IN())->
  141. findZone(Name("ns.test2.example")).zone->
  142. find(Name("ns.test2.example"), RRType::AAAA()).code);
  143. }
  144. TEST_F(AuthConmmandTest, loadZone) {
  145. configureZones(server);
  146. ASSERT_EQ(0, system(INSTALL_PROG " " TEST_DATA_DIR
  147. "/test1-new.zone.in "
  148. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  149. ASSERT_EQ(0, system(INSTALL_PROG " " TEST_DATA_DIR
  150. "/test2-new.zone.in "
  151. TEST_DATA_BUILDDIR "/test2.zone.copied"));
  152. result = execAuthServerCommand(server, "loadzone",
  153. Element::fromJSON(
  154. "{\"origin\": \"test1.example\"}"));
  155. checkAnswer(0);
  156. newZoneChecks(server);
  157. }
  158. TEST_F(AuthConmmandTest, loadBrokenZone) {
  159. configureZones(server);
  160. ASSERT_EQ(0, system(INSTALL_PROG " " TEST_DATA_DIR
  161. "/test1-broken.zone.in "
  162. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  163. result = execAuthServerCommand(server, "loadzone",
  164. Element::fromJSON(
  165. "{\"origin\": \"test1.example\"}"));
  166. checkAnswer(1);
  167. zoneChecks(server); // zone shouldn't be replaced
  168. }
  169. TEST_F(AuthConmmandTest, loadUnreadableZone) {
  170. configureZones(server);
  171. // install the zone file as unreadable
  172. ASSERT_EQ(0, system(INSTALL_PROG " -m 000 " TEST_DATA_DIR
  173. "/test1.zone.in "
  174. TEST_DATA_BUILDDIR "/test1.zone.copied"));
  175. result = execAuthServerCommand(server, "loadzone",
  176. Element::fromJSON(
  177. "{\"origin\": \"test1.example\"}"));
  178. checkAnswer(1);
  179. zoneChecks(server); // zone shouldn't be replaced
  180. }
  181. TEST_F(AuthConmmandTest, loadZoneWithoutDataSrc) {
  182. // try to execute load command without configuring the zone beforehand.
  183. // it should fail.
  184. result = execAuthServerCommand(server, "loadzone",
  185. Element::fromJSON(
  186. "{\"origin\": \"test1.example\"}"));
  187. checkAnswer(1);
  188. }
  189. TEST_F(AuthConmmandTest, loadSqlite3DataSrc) {
  190. // For sqlite3 data source we don't have to do anything (the data source
  191. // (re)loads itself automatically)
  192. result = execAuthServerCommand(server, "loadzone",
  193. Element::fromJSON(
  194. "{\"origin\": \"test1.example\","
  195. " \"datasrc\": \"sqlite3\"}"));
  196. checkAnswer(0);
  197. }
  198. TEST_F(AuthConmmandTest, loadZoneInvalidParams) {
  199. configureZones(server);
  200. // null arg
  201. result = execAuthServerCommand(server, "loadzone", ElementPtr());
  202. checkAnswer(1);
  203. // zone class is bogus
  204. result = execAuthServerCommand(server, "loadzone",
  205. Element::fromJSON(
  206. "{\"origin\": \"test1.example\","
  207. " \"class\": \"no_such_class\"}"));
  208. checkAnswer(1);
  209. result = execAuthServerCommand(server, "loadzone",
  210. Element::fromJSON(
  211. "{\"origin\": \"test1.example\","
  212. " \"class\": 1}"));
  213. checkAnswer(1);
  214. // unsupported zone class
  215. result = execAuthServerCommand(server, "loadzone",
  216. Element::fromJSON(
  217. "{\"origin\": \"test1.example\","
  218. " \"class\": \"CH\"}"));
  219. checkAnswer(1);
  220. // unsupported data source class
  221. result = execAuthServerCommand(server, "loadzone",
  222. Element::fromJSON(
  223. "{\"origin\": \"test1.example\","
  224. " \"datasrc\": \"not supported\"}"));
  225. checkAnswer(1);
  226. // data source is bogus
  227. result = execAuthServerCommand(server, "loadzone",
  228. Element::fromJSON(
  229. "{\"origin\": \"test1.example\","
  230. " \"datasrc\": 0}"));
  231. checkAnswer(1);
  232. // origin is missing
  233. result = execAuthServerCommand(server, "loadzone",
  234. Element::fromJSON("{}"));
  235. checkAnswer(1);
  236. // zone doesn't exist in the data source
  237. result = execAuthServerCommand(server, "loadzone",
  238. Element::fromJSON("{\"origin\": \"xx\"}"));
  239. checkAnswer(1);
  240. // origin is bogus
  241. result = execAuthServerCommand(server, "loadzone",
  242. Element::fromJSON(
  243. "{\"origin\": \"...\"}"));
  244. checkAnswer(1);
  245. result = execAuthServerCommand(server, "loadzone",
  246. Element::fromJSON("{\"origin\": 10}"));
  247. checkAnswer(1);
  248. }
  249. }