configuration_unittest.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <dhcpsrv/cfgmgr.h>
  16. #include <dhcpsrv/configuration.h>
  17. #include <dhcpsrv/subnet.h>
  18. #include <gtest/gtest.h>
  19. using namespace isc::asiolink;
  20. using namespace isc::dhcp;
  21. // Those are the tests for Configuration storage. Right now they are minimal,
  22. // but the number is expected to grow significantly once we migrate more
  23. // parameters from CfgMgr storage to Configuration storage.
  24. namespace {
  25. /// @brief Number of IPv4 and IPv6 subnets to be created for a test.
  26. const int TEST_SUBNETS_NUM = 3;
  27. /// @brief Test fixture class for testing configuration data storage.
  28. class ConfigurationTest : public ::testing::Test {
  29. public:
  30. /// @brief Constructor.
  31. ///
  32. /// Creates IPv4 and IPv6 subnets for unit test. The number of subnets
  33. /// is @c TEST_SUBNETS_NUM for IPv4 and IPv6 each.
  34. ConfigurationTest() {
  35. // Remove any subnets dangling from previous unit tests.
  36. clearSubnets();
  37. // Create IPv4 subnets.
  38. for (int i = 0; i < TEST_SUBNETS_NUM; ++i) {
  39. // Default triplet carried undefined value.
  40. Triplet<uint32_t> def_triplet;
  41. // Create a collection of subnets: 192.0.X.0/24 where X is
  42. // 0, 1, 2 etc.
  43. Subnet4Ptr subnet(new Subnet4(IOAddress(0xC0000000 | (i << 2)),
  44. 24, def_triplet, def_triplet,
  45. 4000));
  46. test_subnets4_.push_back(subnet);
  47. }
  48. // Create IPv6 subnets.
  49. for (int i = 0; i < TEST_SUBNETS_NUM; ++i) {
  50. // This is a base prefix. All other prefixes will be created by
  51. // modifying this one.
  52. IOAddress prefix("2001:db8:1::0");
  53. std::vector<uint8_t> prefix_bytes = prefix.toBytes();
  54. // Modify 5th byte of the prefix, so 2001:db8:1::0 becomes
  55. // 2001:db8:2::0 etc.
  56. ++prefix_bytes[5];
  57. prefix = IOAddress::fromBytes(prefix.getFamily(), &prefix_bytes[0]);
  58. Subnet6Ptr subnet(new Subnet6(prefix, 64, 1000, 2000, 3000, 4000));
  59. test_subnets6_.push_back(subnet);
  60. }
  61. }
  62. /// @brief Destructor.
  63. ///
  64. /// Removes any dangling configuration.
  65. virtual ~ConfigurationTest() {
  66. clearSubnets();
  67. }
  68. /// @brief Convenience function which adds IPv4 subnet to the configuration.
  69. ///
  70. /// @param index Index of the subnet in the @c test_subnets4_ collection
  71. /// which should be added to the configuration. The configuration is stored
  72. /// in the @ conf_ member. This value must be lower than
  73. /// @c TEST_SUBNETS_NUM.
  74. ///
  75. /// @todo Until the subnets configuration is migrated from the @c CfgMgr to
  76. /// the @c Configuration object, this function adds the subnet to the
  77. /// @c CfgMgr. Once, the subnet configuration is held in the
  78. /// @c Configuration this function must be modified to store the subnets in
  79. /// the @c conf_ object.
  80. void addSubnet4(const unsigned int index);
  81. /// @brief Convenience function which adds IPv6 subnet to the configuration.
  82. ///
  83. /// @param index Index of the subnet in the @c test_subnets6_ collection
  84. /// which should be added to the configuration. The configuration is stored
  85. /// in the @ conf_ member. This value must be lower than
  86. /// @c TEST_SUBNETS_NUM.
  87. ///
  88. /// @todo Until the subnets configuration is migrated from the @c CfgMgr to
  89. /// the @c Configuration object, this function adds the subnet to the
  90. /// @c CfgMgr. Once, the subnet configuration is held in the
  91. /// @c Configuration this function must be modified to store the subnets in
  92. /// @c conf_ object.
  93. void addSubnet6(const unsigned int index);
  94. /// @brief Removes all subnets from the configuration.
  95. ///
  96. /// @todo Modify this function once the subnet configuration is migrated
  97. /// from @c CfgMgr to @c Configuration.
  98. void clearSubnets();
  99. /// @brief Enable/disable DDNS.
  100. ///
  101. /// @param enable A boolean value indicating if the DDNS should be
  102. /// enabled (true) or disabled (false).
  103. void enableDDNS(const bool enable);
  104. /// @brief Stores configuration.
  105. Configuration conf_;
  106. /// @brief A collection of IPv4 subnets used by unit tests.
  107. Subnet4Collection test_subnets4_;
  108. /// @brief A collection of IPv6 subnets used by unit tests.
  109. Subnet6Collection test_subnets6_;
  110. };
  111. void
  112. ConfigurationTest::addSubnet4(const unsigned int index) {
  113. if (index >= TEST_SUBNETS_NUM) {
  114. FAIL() << "Subnet index " << index << "out of range (0.."
  115. << TEST_SUBNETS_NUM << "): " << "unable to add IPv4 subnet";
  116. }
  117. CfgMgr::instance().addSubnet4(test_subnets4_[index]);
  118. }
  119. void
  120. ConfigurationTest::addSubnet6(const unsigned int index) {
  121. if (index >= TEST_SUBNETS_NUM) {
  122. FAIL() << "Subnet index " << index << "out of range (0.."
  123. << TEST_SUBNETS_NUM << "): " << "unable to add IPv6 subnet";
  124. }
  125. CfgMgr::instance().addSubnet6(test_subnets6_[index]);
  126. }
  127. void
  128. ConfigurationTest::clearSubnets() {
  129. CfgMgr::instance().deleteSubnets4();
  130. CfgMgr::instance().deleteSubnets6();
  131. }
  132. void
  133. ConfigurationTest::enableDDNS(const bool enable) {
  134. // D2 configuration should always be non-NULL.
  135. CfgMgr::instance().getD2ClientConfig()->enableUpdates(enable);
  136. }
  137. // Check that by default there are no logging entries
  138. TEST_F(ConfigurationTest, basic) {
  139. EXPECT_TRUE(conf_.logging_info_.empty());
  140. }
  141. // Check that Configuration can store logging information.
  142. TEST_F(ConfigurationTest, loggingInfo) {
  143. LoggingInfo log1;
  144. log1.name_ = "foo";
  145. log1.severity_ = isc::log::WARN;
  146. log1.debuglevel_ = 77;
  147. LoggingDestination dest;
  148. dest.output_ = "some-logfile.txt";
  149. dest.maxver_ = 5;
  150. dest.maxsize_ = 2097152;
  151. log1.destinations_.push_back(dest);
  152. conf_.logging_info_.push_back(log1);
  153. EXPECT_EQ("foo", conf_.logging_info_[0].name_);
  154. EXPECT_EQ(isc::log::WARN, conf_.logging_info_[0].severity_);
  155. EXPECT_EQ(77, conf_.logging_info_[0].debuglevel_);
  156. EXPECT_EQ("some-logfile.txt", conf_.logging_info_[0].destinations_[0].output_);
  157. EXPECT_EQ(5, conf_.logging_info_[0].destinations_[0].maxver_);
  158. EXPECT_EQ(2097152, conf_.logging_info_[0].destinations_[0].maxsize_);
  159. }
  160. // Check that the configuration summary including information about the status
  161. // of DDNS is returned.
  162. TEST_F(ConfigurationTest, summaryDDNS) {
  163. EXPECT_EQ("DDNS: disabled",
  164. conf_.getConfigSummary(Configuration::CFGSEL_DDNS));
  165. enableDDNS(true);
  166. EXPECT_EQ("DDNS: enabled",
  167. conf_.getConfigSummary(Configuration::CFGSEL_DDNS));
  168. enableDDNS(false);
  169. EXPECT_EQ("no IPv4 subnets!; no IPv6 subnets!; DDNS: disabled",
  170. conf_.getConfigSummary(Configuration::CFGSEL_ALL));
  171. }
  172. // Check that the configuration summary including information about added
  173. // subnets is returned.
  174. TEST_F(ConfigurationTest, summarySubnets) {
  175. EXPECT_EQ("no config details available",
  176. conf_.getConfigSummary(Configuration::CFGSEL_NONE));
  177. // Initially, there are no subnets added but it should be explicitly
  178. // reported when we query for information about the subnets.
  179. EXPECT_EQ("no IPv4 subnets!; no IPv6 subnets!",
  180. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET));
  181. // If we just want information about IPv4 subnets, there should be no
  182. // mention of IPv6 subnets, even though there are none added.
  183. EXPECT_EQ("no IPv4 subnets!",
  184. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET4));
  185. // If we just want information about IPv6 subnets, there should be no
  186. // mention of IPv4 subnets, even though there are none added.
  187. EXPECT_EQ("no IPv6 subnets!",
  188. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET6));
  189. // Add IPv4 subnet and make sure it is reported.
  190. addSubnet4(0);
  191. EXPECT_EQ("added IPv4 subnets: 1",
  192. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET4));
  193. EXPECT_EQ("added IPv4 subnets: 1; no IPv6 subnets!",
  194. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET));
  195. // Add IPv6 subnet and make sure it is reported.
  196. addSubnet6(0);
  197. EXPECT_EQ("added IPv6 subnets: 1",
  198. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET6));
  199. EXPECT_EQ("added IPv4 subnets: 1; added IPv6 subnets: 1",
  200. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET));
  201. // Add one more subnet and make sure the bumped value is only
  202. // for IPv4, but not for IPv6.
  203. addSubnet4(1);
  204. EXPECT_EQ("added IPv4 subnets: 2; added IPv6 subnets: 1",
  205. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET));
  206. EXPECT_EQ("added IPv4 subnets: 2",
  207. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET4));
  208. addSubnet6(1);
  209. EXPECT_EQ("added IPv4 subnets: 2; added IPv6 subnets: 2",
  210. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET));
  211. // Remove all subnets and make sure that there are no reported subnets
  212. // back again.
  213. clearSubnets();
  214. EXPECT_EQ("no IPv4 subnets!; no IPv6 subnets!",
  215. conf_.getConfigSummary(Configuration::CFGSEL_SUBNET));
  216. }
  217. } // end of anonymous namespace