kea_controller_unittest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright (C) 2014 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 <config/ccsession.h>
  16. #include <dhcp/dhcp4.h>
  17. #include <dhcp4/ctrl_dhcp4_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 NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv {
  36. // "Naked" DHCPv4 server, exposes internal fields
  37. public:
  38. NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(0) { }
  39. };
  40. /// @brief test class for Kea configuration backend
  41. ///
  42. /// This class is used for testing Kea configuration backend.
  43. /// It is very simple and currently focuses on reading
  44. /// config file from disk. It is expected to be expanded in the
  45. /// near future.
  46. class JSONFileBackendTest : public ::testing::Test {
  47. public:
  48. JSONFileBackendTest() {
  49. }
  50. ~JSONFileBackendTest() {
  51. isc::log::setDefaultLoggingOutput();
  52. static_cast<void>(unlink(TEST_FILE));
  53. };
  54. /// @brief writes specified content to a well known file
  55. ///
  56. /// Writes specified content to TEST_FILE. Tests will
  57. /// attempt to read that file.
  58. ///
  59. /// @param content content to be written to file
  60. void writeFile(const std::string& content) {
  61. static_cast<void>(unlink(TEST_FILE));
  62. ofstream out(TEST_FILE, ios::trunc);
  63. EXPECT_TRUE(out.is_open());
  64. out << content;
  65. out.close();
  66. }
  67. /// Name of a config file used during tests
  68. static const char* TEST_FILE;
  69. };
  70. const char* JSONFileBackendTest::TEST_FILE = "test-config.json";
  71. // This test checks if configuration can be read from a JSON file.
  72. TEST_F(JSONFileBackendTest, jsonFile) {
  73. // Prepare configuration file.
  74. string config = "{ \"Dhcp4\": { \"interfaces\": [ \"*\" ],"
  75. "\"rebind-timer\": 2000, "
  76. "\"renew-timer\": 1000, "
  77. "\"subnet4\": [ { "
  78. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  79. " \"subnet\": \"192.0.2.0/24\" "
  80. " },"
  81. " {"
  82. " \"pools\": [ { \"pool\": \"192.0.3.101 - 192.0.3.150\" } ],"
  83. " \"subnet\": \"192.0.3.0/24\", "
  84. " \"id\": 0 "
  85. " },"
  86. " {"
  87. " \"pools\": [ { \"pool\": \"192.0.4.101 - 192.0.4.150\" } ],"
  88. " \"subnet\": \"192.0.4.0/24\" "
  89. " } ],"
  90. "\"valid-lifetime\": 4000 }"
  91. "}";
  92. writeFile(config);
  93. // Now initialize the server
  94. boost::scoped_ptr<ControlledDhcpv4Srv> srv;
  95. ASSERT_NO_THROW(
  96. srv.reset(new ControlledDhcpv4Srv(0))
  97. );
  98. // And configure it using the config file.
  99. EXPECT_NO_THROW(srv->init(TEST_FILE));
  100. // Now check if the configuration has been applied correctly.
  101. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  102. ASSERT_TRUE(subnets);
  103. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets.
  104. // Check subnet 1.
  105. EXPECT_EQ("192.0.2.0", subnets->at(0)->get().first.toText());
  106. EXPECT_EQ(24, subnets->at(0)->get().second);
  107. // Check pools in the first subnet.
  108. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_V4);
  109. ASSERT_EQ(1, pools1.size());
  110. EXPECT_EQ("192.0.2.1", pools1.at(0)->getFirstAddress().toText());
  111. EXPECT_EQ("192.0.2.100", pools1.at(0)->getLastAddress().toText());
  112. EXPECT_EQ(Lease::TYPE_V4, pools1.at(0)->getType());
  113. // Check subnet 2.
  114. EXPECT_EQ("192.0.3.0", subnets->at(1)->get().first.toText());
  115. EXPECT_EQ(24, subnets->at(1)->get().second);
  116. // Check pools in the second subnet.
  117. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_V4);
  118. ASSERT_EQ(1, pools2.size());
  119. EXPECT_EQ("192.0.3.101", pools2.at(0)->getFirstAddress().toText());
  120. EXPECT_EQ("192.0.3.150", pools2.at(0)->getLastAddress().toText());
  121. EXPECT_EQ(Lease::TYPE_V4, pools2.at(0)->getType());
  122. // And finally check subnet 3.
  123. EXPECT_EQ("192.0.4.0", subnets->at(2)->get().first.toText());
  124. EXPECT_EQ(24, subnets->at(2)->get().second);
  125. // ... and it's only pool.
  126. const PoolCollection& pools3 = subnets->at(2)->getPools(Lease::TYPE_V4);
  127. EXPECT_EQ("192.0.4.101", pools3.at(0)->getFirstAddress().toText());
  128. EXPECT_EQ("192.0.4.150", pools3.at(0)->getLastAddress().toText());
  129. EXPECT_EQ(Lease::TYPE_V4, pools3.at(0)->getType());
  130. }
  131. // This test checks if configuration can be read from a JSON file.
  132. TEST_F(JSONFileBackendTest, comments) {
  133. string config_hash_comments = "# This is a comment. It should be \n"
  134. "#ignored. Real config starts in line below\n"
  135. "{ \"Dhcp4\": { \"interfaces\": [ \"*\" ],"
  136. "\"rebind-timer\": 2000, "
  137. "\"renew-timer\": 1000, \n"
  138. "# comments in the middle should be ignored, too\n"
  139. "\"subnet4\": [ { "
  140. " \"pools\": [ { \"pool\": \"192.0.2.0/24\" } ],"
  141. " \"subnet\": \"192.0.2.0/22\" "
  142. " } ],"
  143. "\"valid-lifetime\": 4000 }"
  144. "}";
  145. /// @todo: Implement C++-style (// ...) comments
  146. /// @todo: Implement C-style (/* ... */) comments
  147. writeFile(config_hash_comments);
  148. // Now initialize the server
  149. boost::scoped_ptr<ControlledDhcpv4Srv> srv;
  150. ASSERT_NO_THROW(
  151. srv.reset(new ControlledDhcpv4Srv(0))
  152. );
  153. // And configure it using config with comments.
  154. EXPECT_NO_THROW(srv->init(TEST_FILE));
  155. // Now check if the configuration has been applied correctly.
  156. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  157. ASSERT_TRUE(subnets);
  158. ASSERT_EQ(1, subnets->size());
  159. // Check subnet 1.
  160. EXPECT_EQ("192.0.2.0", subnets->at(0)->get().first.toText());
  161. EXPECT_EQ(22, subnets->at(0)->get().second);
  162. // Check pools in the first subnet.
  163. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_V4);
  164. ASSERT_EQ(1, pools1.size());
  165. EXPECT_EQ("192.0.2.0", pools1.at(0)->getFirstAddress().toText());
  166. EXPECT_EQ("192.0.2.255", pools1.at(0)->getLastAddress().toText());
  167. EXPECT_EQ(Lease::TYPE_V4, pools1.at(0)->getType());
  168. }
  169. // This test checks if configuration detects failure when trying:
  170. // - empty file
  171. // - empty filename
  172. // - no Dhcp4 element
  173. // - Config file that contains Dhcp4 but has a content error
  174. TEST_F(JSONFileBackendTest, configBroken) {
  175. // Empty config is not allowed, because Dhcp4 element is missing
  176. string config_empty = "";
  177. // This config does not have mandatory Dhcp4 element
  178. string config_v4 = "{ \"Dhcp6\": { \"interfaces\": [ \"*\" ],"
  179. "\"preferred-lifetime\": 3000,"
  180. "\"rebind-timer\": 2000, "
  181. "\"renew-timer\": 1000, "
  182. "\"subnet4\": [ { "
  183. " \"pool\": [ \"2001:db8::/80\" ],"
  184. " \"subnet\": \"2001:db8::/64\" "
  185. " } ]}";
  186. // This has Dhcp4 element, but it's utter nonsense
  187. string config_nonsense = "{ \"Dhcp4\": { \"reviews\": \"are so much fun\" } }";
  188. // Now initialize the server
  189. boost::scoped_ptr<ControlledDhcpv4Srv> srv;
  190. ASSERT_NO_THROW(
  191. srv.reset(new ControlledDhcpv4Srv(0))
  192. );
  193. // Try to configure without filename. Should fail.
  194. EXPECT_THROW(srv->init(""), BadValue);
  195. // Try to configure it using empty file. Should fail.
  196. writeFile(config_empty);
  197. EXPECT_THROW(srv->init(TEST_FILE), BadValue);
  198. // Now try to load a config that does not have Dhcp4 component.
  199. writeFile(config_v4);
  200. EXPECT_THROW(srv->init(TEST_FILE), BadValue);
  201. // Now try to load a config with Dhcp4 full of nonsense.
  202. writeFile(config_nonsense);
  203. EXPECT_THROW(srv->init(TEST_FILE), BadValue);
  204. }
  205. /// This unit-test reads all files enumerated in configs-test.txt file, loads
  206. /// each of them and verify that they can be loaded.
  207. ///
  208. /// @todo: Unfortunately, we have this test disabled, because all loaded
  209. /// configs use memfile, which attempts to create lease file in
  210. /// /usr/local/var/kea/kea-leases4.csv. We have couple options here:
  211. /// a) disable persistence in example configs - a very bad thing to do
  212. /// as users will forget to reenable it and then will be surprised when their
  213. /// leases disappear
  214. /// b) change configs to store lease file in /tmp. It's almost as bad as the
  215. /// previous one. Users will then be displeased when all their leases are
  216. /// wiped. (most systems wipe /tmp during boot)
  217. /// c) read each config and rewrite it on the fly, so persistence is disabled.
  218. /// This is probably the way to go, but this is a work for a dedicated ticket.
  219. ///
  220. /// Hence I'm leaving the test in, but it is disabled.
  221. TEST_F(JSONFileBackendTest, DISABLED_loadAllConfigs) {
  222. // Create server first
  223. boost::scoped_ptr<ControlledDhcpv4Srv> srv;
  224. ASSERT_NO_THROW(
  225. srv.reset(new ControlledDhcpv4Srv(0))
  226. );
  227. const char* configs_list = "configs-list.txt";
  228. fstream configs(configs_list, ios::in);
  229. ASSERT_TRUE(configs.is_open());
  230. std::string config_name;
  231. while (std::getline(configs, config_name)) {
  232. // Ignore empty and commented lines
  233. if (config_name.empty() || config_name[0] == '#') {
  234. continue;
  235. }
  236. // Unit-tests usually do not print out anything, but in this case I
  237. // think printing out tests configs is warranted.
  238. std::cout << "Loading config file " << config_name << std::endl;
  239. try {
  240. srv->init(config_name);
  241. } catch (const std::exception& ex) {
  242. ADD_FAILURE() << "Exception thrown" << ex.what() << endl;
  243. }
  244. }
  245. }
  246. } // End of anonymous namespace