simple_parser4_unittest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <gtest/gtest.h>
  8. #include <dhcpsrv/parsers/simple_parser4.h>
  9. #include <dhcp4/tests/dhcp4_test_utils.h>
  10. #include <cc/data.h>
  11. using namespace isc::data;
  12. namespace isc {
  13. namespace dhcp {
  14. namespace test {
  15. /// @brief DHCP Parser test fixture class
  16. class SimpleParser4Test : public ::testing::Test {
  17. public:
  18. /// @brief Checks if specified map has an integer parameter with expected value
  19. ///
  20. /// @param map map to be checked
  21. /// @param param_name name of the parameter to be checked
  22. /// @param exp_value expected value of the parameter.
  23. void checkIntegerValue(const ConstElementPtr& map, const std::string& param_name,
  24. int64_t exp_value) {
  25. // First check if the passed element is a map.
  26. ASSERT_EQ(Element::map, map->getType());
  27. // Now try to get the element being checked
  28. ConstElementPtr elem = map->get(param_name);
  29. ASSERT_TRUE(elem);
  30. // Now check if it's indeed integer
  31. ASSERT_EQ(Element::integer, elem->getType());
  32. // Finally, check if its value meets expectation.
  33. EXPECT_EQ(exp_value, elem->intValue());
  34. }
  35. /// @brief Checks if specified map has a string parameter with expected value
  36. ///
  37. /// @param map map to be checked
  38. /// @param param_name name of the parameter to be checked
  39. /// @param exp_value expected value of the parameter.
  40. void checkStringValue(const ConstElementPtr& map, const std::string& param_name,
  41. std::string exp_value) {
  42. // First check if the passed element is a map.
  43. ASSERT_EQ(Element::map, map->getType());
  44. // Now try to get the element being checked
  45. ConstElementPtr elem = map->get(param_name);
  46. ASSERT_TRUE(elem);
  47. // Now check if it's indeed integer
  48. ASSERT_EQ(Element::string, elem->getType());
  49. // Finally, check if its value meets expectation.
  50. EXPECT_EQ(exp_value, elem->stringValue());
  51. }
  52. /// @brief Checks if specified map has a boolean parameter with expected value
  53. ///
  54. /// @param map map to be checked
  55. /// @param param_name name of the parameter to be checked
  56. /// @param exp_value expected value of the parameter.
  57. void checkBoolValue(const ConstElementPtr& map, const std::string& param_name,
  58. bool exp_value) {
  59. // First check if the passed element is a map.
  60. ASSERT_EQ(Element::map, map->getType());
  61. // Now try to get the element being checked
  62. ConstElementPtr elem = map->get(param_name);
  63. ASSERT_TRUE(elem);
  64. // Now check if it's indeed integer
  65. ASSERT_EQ(Element::boolean, elem->getType());
  66. // Finally, check if its value meets expectation.
  67. EXPECT_EQ(exp_value, elem->boolValue());
  68. }
  69. };
  70. // This test checks if global defaults are properly set for DHCPv4.
  71. TEST_F(SimpleParser4Test, globalDefaults4) {
  72. ElementPtr empty = parseJSON("{ }");
  73. size_t num = 0;
  74. EXPECT_NO_THROW(num = SimpleParser4::setAllDefaults(empty));
  75. // We expect at least 3 parameters to be inserted.
  76. EXPECT_TRUE(num >= 3);
  77. checkIntegerValue(empty, "valid-lifetime", 7200);
  78. checkIntegerValue(empty, "rebind-timer", 1800);
  79. checkIntegerValue(empty, "renew-timer", 900);
  80. // Make sure that preferred-lifetime is not set for v4 (it's v6 only
  81. // parameter)
  82. EXPECT_FALSE(empty->get("preferred-lifetime"));
  83. }
  84. // This test checks if the parameters can be inherited from the global
  85. // scope to the subnet scope.
  86. TEST_F(SimpleParser4Test, inheritGlobalToSubnet4) {
  87. ElementPtr global = parseJSON("{ \"renew-timer\": 1,"
  88. " \"rebind-timer\": 2,"
  89. " \"valid-lifetime\": 4,"
  90. " \"subnet4\": [ { \"renew-timer\": 100 } ] "
  91. "}");
  92. ConstElementPtr subnets = global->find("subnet4");
  93. ASSERT_TRUE(subnets);
  94. ConstElementPtr subnet = subnets->get(0);
  95. ASSERT_TRUE(subnet);
  96. // we should inherit 3 parameters. Renew-timer should remain intact,
  97. // as it was already defined in the subnet scope.
  98. size_t num;
  99. EXPECT_NO_THROW(num = SimpleParser4::deriveParameters(global));
  100. EXPECT_EQ(2, num);
  101. // Check the values. 2 of them are inherited, while the third one
  102. // was already defined in the subnet, so should not be inherited.
  103. checkIntegerValue(subnet, "renew-timer", 100);
  104. checkIntegerValue(subnet, "rebind-timer", 2);
  105. checkIntegerValue(subnet, "valid-lifetime", 4);
  106. }
  107. // This test checks if the parameters in "subnet4" are assigned default values
  108. // if not explicitly specified.
  109. TEST_F(SimpleParser4Test, subnetDefaults4) {
  110. ElementPtr global = parseJSON("{ \"renew-timer\": 1,"
  111. " \"rebind-timer\": 2,"
  112. " \"valid-lifetime\": 4,"
  113. " \"subnet4\": [ { } ] "
  114. "}");
  115. size_t num = 0;
  116. EXPECT_NO_THROW(num = SimpleParser4::setAllDefaults(global));
  117. EXPECT_LE(1, num); // at least 1 parameter has to be modified
  118. ConstElementPtr subnets = global->find("subnet4");
  119. ASSERT_TRUE(subnets);
  120. ConstElementPtr subnet = subnets->get(0);
  121. ASSERT_TRUE(subnet);
  122. // we should have "id" parameter with the default value of 0 added for us.
  123. checkIntegerValue(subnet, "id", 0);
  124. }
  125. // This test checks if the parameters in option-data are assigned default values
  126. // if not explicitly specified.
  127. TEST_F(SimpleParser4Test, optionDataDefaults4) {
  128. ElementPtr global = parseJSON("{ \"renew-timer\": 1,"
  129. " \"rebind-timer\": 2,"
  130. " \"valid-lifetime\": 4,"
  131. " \"option-data\": [ { } ] "
  132. "}");
  133. size_t num = 0;
  134. EXPECT_NO_THROW(num = SimpleParser4::setAllDefaults(global));
  135. EXPECT_LE(1, num); // at least 1 parameter has to be modified
  136. ConstElementPtr options = global->find("option-data");
  137. ASSERT_TRUE(options);
  138. ConstElementPtr option = options->get(0);
  139. ASSERT_TRUE(option);
  140. // we should have appropriate default value set. See
  141. // SimpleParser4::OPTION4_DEFAULTS for a list of default values.
  142. checkStringValue(option, "space", "dhcp4");
  143. checkBoolValue(option, "csv-format", true);
  144. }
  145. // This test checks if the parameters in option-data are assigned default values
  146. // if not explicitly specified.
  147. TEST_F(SimpleParser4Test, optionDefDefaults4) {
  148. ElementPtr global = parseJSON("{ "
  149. " \"option-def\": [ { } ] "
  150. "}");
  151. size_t num = 0;
  152. EXPECT_NO_THROW(num = SimpleParser4::setAllDefaults(global));
  153. EXPECT_LE(1, num); // at least 1 parameter has to be modified
  154. ConstElementPtr defs = global->find("option-def");
  155. ASSERT_TRUE(defs);
  156. ASSERT_EQ(1, defs->size());
  157. ConstElementPtr def = defs->get(0);
  158. ASSERT_TRUE(def);
  159. // we should have appropriate default value set. See
  160. // SimpleParser4::OPTION4_DEFAULTS for a list of default values.
  161. checkStringValue(def, "record-types", "");
  162. checkStringValue(def, "space", "dhcp4");
  163. checkStringValue(def, "encapsulate", "");
  164. checkBoolValue(def, "array", false);
  165. }
  166. };
  167. };
  168. };