config_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <gtest/gtest.h>
  15. #include <exceptions/exceptions.h>
  16. #include <dns/rrclass.h>
  17. #include <cc/data.h>
  18. #include <datasrc/memory_datasrc.h>
  19. #include <xfr/xfrout_client.h>
  20. #include <auth/auth_srv.h>
  21. #include <auth/config.h>
  22. #include <testutils/mockups.h>
  23. using namespace isc::dns;
  24. using namespace isc::data;
  25. using namespace isc::datasrc;
  26. namespace {
  27. class AuthConfigTest : public ::testing::Test {
  28. protected:
  29. AuthConfigTest() : rrclass(RRClass::IN()), server(true, xfrout) {}
  30. const RRClass rrclass;
  31. MockXfroutClient xfrout;
  32. AuthSrv server;
  33. };
  34. TEST_F(AuthConfigTest, datasourceConfig) {
  35. // By default, we don't have any in-memory data source.
  36. EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  37. configureAuthServer(server, Element::fromJSON(
  38. "{\"datasources\": [{\"type\": \"memory\"}]}"));
  39. // after successful configuration, we should have one (with empty zoneset).
  40. ASSERT_NE(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  41. EXPECT_EQ(0, server.getMemoryDataSrc(rrclass)->getZoneCount());
  42. }
  43. TEST_F(AuthConfigTest, databaseConfig) {
  44. // right now, "database_file" is handled separately, so the parser
  45. // doesn't recognize it, but it shouldn't throw an exception due to that.
  46. EXPECT_NO_THROW(configureAuthServer(
  47. server,
  48. Element::fromJSON(
  49. "{\"database_file\": \"should_be_ignored\"}")));
  50. }
  51. TEST_F(AuthConfigTest, exceptionGuarantee) {
  52. EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  53. // This configuration contains an invalid item, which will trigger
  54. // an exception.
  55. EXPECT_THROW(configureAuthServer(
  56. server,
  57. Element::fromJSON(
  58. "{\"datasources\": [{\"type\": \"memory\"}], "
  59. " \"no_such_config_var\": 1}")),
  60. AuthConfigError);
  61. // The server state shouldn't change
  62. EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  63. }
  64. TEST_F(AuthConfigTest, exceptionConversion) {
  65. // This configuration contains a bogus RR class, which will trigger an
  66. // exception from libdns++. configureAuthServer() should convert this
  67. // to AuthConfigError and rethrow the converted one.
  68. EXPECT_THROW(configureAuthServer(
  69. server,
  70. Element::fromJSON(
  71. "{\"datasources\": "
  72. " [{\"type\": \"memory\","
  73. " \"class\": \"BADCLASS\","
  74. " \"zones\": [{\"origin\": \"example.com\","
  75. " \"file\": \"example.zone\"}]}]}")),
  76. AuthConfigError);
  77. }
  78. TEST_F(AuthConfigTest, badConfig) {
  79. // These should normally not happen, but should be handled to avoid
  80. // an unexpected crash due to a bug of the caller.
  81. EXPECT_THROW(configureAuthServer(server, ElementPtr()), AuthConfigError);
  82. EXPECT_THROW(configureAuthServer(server, Element::fromJSON("[]")),
  83. AuthConfigError);
  84. }
  85. TEST_F(AuthConfigTest, unknownConfigVar) {
  86. EXPECT_THROW(createAuthConfigParser(server, "no_such_config_var"),
  87. AuthConfigError);
  88. }
  89. class MemoryDatasrcConfigTest : public AuthConfigTest {
  90. protected:
  91. MemoryDatasrcConfigTest() :
  92. parser(createAuthConfigParser(server, "datasources"))
  93. {}
  94. ~MemoryDatasrcConfigTest() {
  95. delete parser;
  96. }
  97. AuthConfigParser* parser;
  98. };
  99. TEST_F(MemoryDatasrcConfigTest, addZeroDataSrc) {
  100. parser->build(Element::fromJSON("[]"));
  101. parser->commit();
  102. EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  103. }
  104. TEST_F(MemoryDatasrcConfigTest, addEmpty) {
  105. // By default, we don't have any in-memory data source.
  106. EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  107. parser->build(Element::fromJSON("[{\"type\": \"memory\"}]"));
  108. parser->commit();
  109. EXPECT_EQ(0, server.getMemoryDataSrc(rrclass)->getZoneCount());
  110. }
  111. TEST_F(MemoryDatasrcConfigTest, addZeroZone) {
  112. parser->build(Element::fromJSON("[{\"type\": \"memory\","
  113. " \"zones\": []}]"));
  114. parser->commit();
  115. EXPECT_EQ(0, server.getMemoryDataSrc(rrclass)->getZoneCount());
  116. }
  117. TEST_F(MemoryDatasrcConfigTest, addOneZone) {
  118. parser->build(Element::fromJSON(
  119. "[{\"type\": \"memory\","
  120. " \"zones\": [{\"origin\": \"example.com\","
  121. " \"file\": \"example.zone\"}]}]"));
  122. parser->commit();
  123. EXPECT_EQ(1, server.getMemoryDataSrc(rrclass)->getZoneCount());
  124. }
  125. TEST_F(MemoryDatasrcConfigTest, addMultiZones) {
  126. parser->build(Element::fromJSON(
  127. "[{\"type\": \"memory\","
  128. " \"zones\": [{\"origin\": \"example.com\","
  129. " \"file\": \"example.zone\"},"
  130. " {\"origin\": \"example.org\","
  131. " \"file\": \"example.org.zone\"},"
  132. " {\"origin\": \"example.net\","
  133. " \"file\": \"example.net.zone\"}]}]"));
  134. parser->commit();
  135. EXPECT_EQ(3, server.getMemoryDataSrc(rrclass)->getZoneCount());
  136. }
  137. TEST_F(MemoryDatasrcConfigTest, replace) {
  138. parser->build(Element::fromJSON(
  139. "[{\"type\": \"memory\","
  140. " \"zones\": [{\"origin\": \"example.com\","
  141. " \"file\": \"example.zone\"}]}]"));
  142. parser->commit();
  143. EXPECT_EQ(1, server.getMemoryDataSrc(rrclass)->getZoneCount());
  144. EXPECT_EQ(isc::datasrc::result::SUCCESS,
  145. server.getMemoryDataSrc(rrclass)->findZone(
  146. Name("example.com")).code);
  147. // create a new parser, and install a new set of configuration. It
  148. // should replace the old one.
  149. delete parser;
  150. parser = createAuthConfigParser(server, "datasources");
  151. parser->build(Element::fromJSON(
  152. "[{\"type\": \"memory\","
  153. " \"zones\": [{\"origin\": \"example.org\","
  154. " \"file\": \"example.org.zone\"},"
  155. " {\"origin\": \"example.net\","
  156. " \"file\": \"example.net.zone\"}]}]"));
  157. parser->commit();
  158. EXPECT_EQ(2, server.getMemoryDataSrc(rrclass)->getZoneCount());
  159. EXPECT_EQ(isc::datasrc::result::NOTFOUND,
  160. server.getMemoryDataSrc(rrclass)->findZone(
  161. Name("example.com")).code);
  162. }
  163. TEST_F(MemoryDatasrcConfigTest, remove) {
  164. parser->build(Element::fromJSON(
  165. "[{\"type\": \"memory\","
  166. " \"zones\": [{\"origin\": \"example.com\","
  167. " \"file\": \"example.zone\"}]}]"));
  168. parser->commit();
  169. EXPECT_EQ(1, server.getMemoryDataSrc(rrclass)->getZoneCount());
  170. delete parser;
  171. parser = createAuthConfigParser(server, "datasources");
  172. parser->build(Element::fromJSON("[]"));
  173. parser->commit();
  174. EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
  175. }
  176. TEST_F(MemoryDatasrcConfigTest, adDuplicateZones) {
  177. EXPECT_THROW(parser->build(
  178. Element::fromJSON(
  179. "[{\"type\": \"memory\","
  180. " \"zones\": [{\"origin\": \"example.com\","
  181. " \"file\": \"example.zone\"},"
  182. " {\"origin\": \"example.com\","
  183. " \"file\": \"example.com.zone\"}]}]")),
  184. AuthConfigError);
  185. }
  186. TEST_F(MemoryDatasrcConfigTest, addBadZone) {
  187. // origin is missing
  188. EXPECT_THROW(parser->build(
  189. Element::fromJSON(
  190. "[{\"type\": \"memory\","
  191. " \"zones\": [{\"file\": \"example.zone\"}]}]")),
  192. AuthConfigError);
  193. // missing zone file
  194. EXPECT_THROW(parser->build(
  195. Element::fromJSON(
  196. "[{\"type\": \"memory\","
  197. " \"zones\": [{\"origin\": \"example.com\"}]}]")),
  198. AuthConfigError);
  199. // bogus origin name
  200. EXPECT_THROW(parser->build(Element::fromJSON(
  201. "[{\"type\": \"memory\","
  202. " \"zones\": [{\"origin\": \"example..com\","
  203. " \"file\": \"example.zone\"}]}]")),
  204. EmptyLabel);
  205. // bogus RR class name
  206. EXPECT_THROW(parser->build(
  207. Element::fromJSON(
  208. "[{\"type\": \"memory\","
  209. " \"class\": \"BADCLASS\","
  210. " \"zones\": [{\"origin\": \"example.com\","
  211. " \"file\": \"example.zone\"}]}]")),
  212. InvalidRRClass);
  213. // valid RR class, but not currently supported
  214. EXPECT_THROW(parser->build(
  215. Element::fromJSON(
  216. "[{\"type\": \"memory\","
  217. " \"class\": \"CH\","
  218. " \"zones\": [{\"origin\": \"example.com\","
  219. " \"file\": \"example.zone\"}]}]")),
  220. isc::InvalidParameter);
  221. }
  222. TEST_F(MemoryDatasrcConfigTest, badDatasrcType) {
  223. EXPECT_THROW(parser->build(Element::fromJSON("[{\"type\": \"badsrc\"}]")),
  224. AuthConfigError);
  225. EXPECT_THROW(parser->build(Element::fromJSON("[{\"notype\": \"memory\"}]")),
  226. AuthConfigError);
  227. EXPECT_THROW(parser->build(Element::fromJSON("[{\"type\": 1}]")),
  228. isc::data::TypeError);
  229. EXPECT_THROW(parser->build(Element::fromJSON("[{\"type\": \"memory\"},"
  230. " {\"type\": \"memory\"}]")),
  231. AuthConfigError);
  232. }
  233. }