kea_controller_unittest.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (C) 2012-2015 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 <cc/command_interpreter.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp6/ctrl_dhcp6_srv.h>
  18. #include <dhcpsrv/cfgmgr.h>
  19. #include <log/logger_support.h>
  20. #include <boost/scoped_ptr.hpp>
  21. #include <gtest/gtest.h>
  22. #include <fstream>
  23. #include <iostream>
  24. #include <sstream>
  25. #include <arpa/inet.h>
  26. #include <unistd.h>
  27. using namespace std;
  28. using namespace isc;
  29. using namespace isc::asiolink;
  30. using namespace isc::config;
  31. using namespace isc::data;
  32. using namespace isc::dhcp;
  33. using namespace isc::hooks;
  34. namespace {
  35. class NakedControlledDhcpv6Srv: public ControlledDhcpv6Srv {
  36. // "Naked" DHCPv6 server, exposes internal fields
  37. public:
  38. NakedControlledDhcpv6Srv():ControlledDhcpv6Srv(0) { }
  39. };
  40. class JSONFileBackendTest : public ::testing::Test {
  41. public:
  42. JSONFileBackendTest() {
  43. }
  44. ~JSONFileBackendTest() {
  45. isc::log::setDefaultLoggingOutput();
  46. static_cast<void>(remove(TEST_FILE));
  47. };
  48. void writeFile(const std::string& file_name, const std::string& content) {
  49. static_cast<void>(remove(file_name.c_str()));
  50. ofstream out(file_name.c_str(), ios::trunc);
  51. EXPECT_TRUE(out.is_open());
  52. out << content;
  53. out.close();
  54. }
  55. static const char* TEST_FILE;
  56. };
  57. const char* JSONFileBackendTest::TEST_FILE = "test-config.json";
  58. // This test checks if configuration can be read from a JSON file.
  59. TEST_F(JSONFileBackendTest, jsonFile) {
  60. // Prepare configuration file.
  61. string config = "{ \"Dhcp6\": {"
  62. "\"interfaces-config\": {"
  63. " \"interfaces\": [ \"*\" ]"
  64. "},"
  65. "\"preferred-lifetime\": 3000,"
  66. "\"rebind-timer\": 2000, "
  67. "\"renew-timer\": 1000, "
  68. "\"subnet6\": [ { "
  69. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  70. " \"subnet\": \"2001:db8:1::/64\" "
  71. " },"
  72. " {"
  73. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  74. " \"subnet\": \"2001:db8:2::/64\", "
  75. " \"id\": 0"
  76. " },"
  77. " {"
  78. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  79. " \"subnet\": \"2001:db8:3::/64\" "
  80. " } ],"
  81. "\"valid-lifetime\": 4000 }"
  82. "}";
  83. writeFile(TEST_FILE, config);
  84. // Now initialize the server
  85. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  86. ASSERT_NO_THROW(
  87. srv.reset(new ControlledDhcpv6Srv(0))
  88. );
  89. // And configure it using the config file.
  90. EXPECT_NO_THROW(srv->init(TEST_FILE));
  91. // Now check if the configuration has been applied correctly.
  92. const Subnet6Collection* subnets =
  93. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  94. ASSERT_TRUE(subnets);
  95. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets.
  96. // Check subnet 1.
  97. EXPECT_EQ("2001:db8:1::", subnets->at(0)->get().first.toText());
  98. EXPECT_EQ(64, subnets->at(0)->get().second);
  99. // Check pools in the first subnet.
  100. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_NA);
  101. ASSERT_EQ(1, pools1.size());
  102. EXPECT_EQ("2001:db8:1::", pools1.at(0)->getFirstAddress().toText());
  103. EXPECT_EQ("2001:db8:1::ffff:ffff:ffff", pools1.at(0)->getLastAddress().toText());
  104. EXPECT_EQ(Lease::TYPE_NA, pools1.at(0)->getType());
  105. // Check subnet 2.
  106. EXPECT_EQ("2001:db8:2::", subnets->at(1)->get().first.toText());
  107. EXPECT_EQ(64, subnets->at(1)->get().second);
  108. // Check pools in the second subnet.
  109. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_NA);
  110. ASSERT_EQ(1, pools2.size());
  111. EXPECT_EQ("2001:db8:2::", pools2.at(0)->getFirstAddress().toText());
  112. EXPECT_EQ("2001:db8:2::ffff:ffff:ffff", pools2.at(0)->getLastAddress().toText());
  113. EXPECT_EQ(Lease::TYPE_NA, pools2.at(0)->getType());
  114. // And finally check subnet 3.
  115. EXPECT_EQ("2001:db8:3::", subnets->at(2)->get().first.toText());
  116. EXPECT_EQ(64, subnets->at(2)->get().second);
  117. // ... and it's only pool.
  118. const PoolCollection& pools3 = subnets->at(2)->getPools(Lease::TYPE_NA);
  119. EXPECT_EQ("2001:db8:3::", pools3.at(0)->getFirstAddress().toText());
  120. EXPECT_EQ("2001:db8:3::ffff:ffff:ffff", pools3.at(0)->getLastAddress().toText());
  121. EXPECT_EQ(Lease::TYPE_NA, pools3.at(0)->getType());
  122. }
  123. // This test checks if configuration can be read from a JSON file.
  124. TEST_F(JSONFileBackendTest, comments) {
  125. string config_hash_comments = "# This is a comment. It should be \n"
  126. "#ignored. Real config starts in line below\n"
  127. "{ \"Dhcp6\": {"
  128. "\"interfaces-config\": {"
  129. " \"interfaces\": [ \"*\" ]"
  130. "},"
  131. "\"preferred-lifetime\": 3000,"
  132. "\"rebind-timer\": 2000, "
  133. "\"renew-timer\": 1000, \n"
  134. "# comments in the middle should be ignored, too\n"
  135. "\"subnet6\": [ { "
  136. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  137. " \"subnet\": \"2001:db8:1::/64\" "
  138. " } ],"
  139. "\"valid-lifetime\": 4000 }"
  140. "}";
  141. /// @todo: Implement C++-style (// ...) comments
  142. /// @todo: Implement C-style (/* ... */) comments
  143. writeFile(TEST_FILE, config_hash_comments);
  144. // Now initialize the server
  145. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  146. ASSERT_NO_THROW(
  147. srv.reset(new ControlledDhcpv6Srv(0))
  148. );
  149. // And configure it using config without
  150. EXPECT_NO_THROW(srv->init(TEST_FILE));
  151. // Now check if the configuration has been applied correctly.
  152. const Subnet6Collection* subnets =
  153. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  154. ASSERT_TRUE(subnets);
  155. ASSERT_EQ(1, subnets->size());
  156. // Check subnet 1.
  157. EXPECT_EQ("2001:db8:1::", subnets->at(0)->get().first.toText());
  158. EXPECT_EQ(64, subnets->at(0)->get().second);
  159. // Check pools in the first subnet.
  160. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_NA);
  161. ASSERT_EQ(1, pools1.size());
  162. EXPECT_EQ("2001:db8:1::", pools1.at(0)->getFirstAddress().toText());
  163. EXPECT_EQ("2001:db8:1::ffff:ffff:ffff", pools1.at(0)->getLastAddress().toText());
  164. EXPECT_EQ(Lease::TYPE_NA, pools1.at(0)->getType());
  165. }
  166. // This test checks if configuration detects failure when trying:
  167. // - empty file
  168. // - empty filename
  169. // - no Dhcp6 element
  170. // - Config file that contains Dhcp6 but has a content error
  171. TEST_F(JSONFileBackendTest, configBroken) {
  172. // Empty config is not allowed, because Dhcp6 element is missing
  173. string config_empty = "";
  174. // This config does not have mandatory Dhcp6 element
  175. string config_v4 = "{ \"Dhcp4\": { \"interfaces\": [ \"*\" ],"
  176. "\"preferred-lifetime\": 3000,"
  177. "\"rebind-timer\": 2000, "
  178. "\"renew-timer\": 1000, "
  179. "\"subnet4\": [ { "
  180. " \"pool\": [ \"192.0.2.0/24\" ],"
  181. " \"subnet\": \"192.0.2.0/24\" "
  182. " } ]}";
  183. // This has Dhcp6 element, but it's utter nonsense
  184. string config_nonsense = "{ \"Dhcp6\": { \"reviews\": \"are so much fun\" } }";
  185. // Now initialize the server
  186. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  187. ASSERT_NO_THROW(
  188. srv.reset(new ControlledDhcpv6Srv(0))
  189. );
  190. // Try to configure without filename. Should fail.
  191. EXPECT_THROW(srv->init(""), BadValue);
  192. // Try to configure it using empty file. Should fail.
  193. writeFile(TEST_FILE, config_empty);
  194. EXPECT_THROW(srv->init(TEST_FILE), BadValue);
  195. // Now try to load a config that does not have Dhcp6 component.
  196. writeFile(TEST_FILE, config_v4);
  197. EXPECT_THROW(srv->init(TEST_FILE), BadValue);
  198. // Now try to load a config with Dhcp6 full of nonsense.
  199. writeFile(TEST_FILE, config_nonsense);
  200. EXPECT_THROW(srv->init(TEST_FILE), BadValue);
  201. }
  202. } // End of anonymous namespace