simple_parser6_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2016 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 <cc/data.h>
  8. #include <dhcp6/simple_parser6.h>
  9. #include <gtest/gtest.h>
  10. using namespace isc;
  11. using namespace isc::data;
  12. using namespace isc::dhcp;
  13. namespace {
  14. /// @brief DHCP Parser test fixture class
  15. class SimpleParser6Test : public ::testing::Test {
  16. public:
  17. /// @brief Checks if specified map has an integer parameter with expected value
  18. ///
  19. /// @param map map to be checked
  20. /// @param param_name name of the parameter to be checked
  21. /// @param exp_value expected value of the parameter.
  22. void checkIntegerValue(const ConstElementPtr& map, const std::string& param_name,
  23. int64_t exp_value) {
  24. // First check if the passed element is a map.
  25. ASSERT_EQ(Element::map, map->getType());
  26. // Now try to get the element being checked
  27. ConstElementPtr elem = map->get(param_name);
  28. ASSERT_TRUE(elem);
  29. // Now check if it's indeed integer
  30. ASSERT_EQ(Element::integer, elem->getType());
  31. // Finally, check if its value meets expectation.
  32. EXPECT_EQ(exp_value, elem->intValue());
  33. }
  34. };
  35. // This test checks if global defaults are properly set for DHCPv6.
  36. TEST_F(SimpleParser6Test, globalDefaults6) {
  37. ElementPtr empty = Element::fromJSON("{ }");
  38. size_t num = 0;
  39. EXPECT_NO_THROW(num = SimpleParser6::setAllDefaults(empty));
  40. // We expect at least 4 parameters to be inserted.
  41. EXPECT_TRUE(num >= 4);
  42. checkIntegerValue(empty, "valid-lifetime", 7200);
  43. checkIntegerValue(empty, "preferred-lifetime", 3600);
  44. checkIntegerValue(empty, "rebind-timer", 1800);
  45. checkIntegerValue(empty, "renew-timer", 900);
  46. }
  47. // This test checks if the parameters can be inherited from the global
  48. // scope to the subnet scope.
  49. TEST_F(SimpleParser6Test, inheritGlobalToSubnet6) {
  50. ElementPtr global = Element::fromJSON("{ \"renew-timer\": 1,"
  51. " \"rebind-timer\": 2,"
  52. " \"preferred-lifetime\": 3,"
  53. " \"valid-lifetime\": 4"
  54. "}");
  55. ElementPtr subnet = Element::fromJSON("{ \"renew-timer\": 100 }");
  56. // we should inherit 3 parameters. Renew-timer should remain intact,
  57. // as it was already defined in the subnet scope.
  58. size_t num;
  59. EXPECT_NO_THROW(num = SimpleParser::deriveParams(global, subnet,
  60. SimpleParser6::INHERIT_GLOBAL_TO_SUBNET6));
  61. EXPECT_EQ(3, num);
  62. // Check the values. 3 of them are inherited, while the fourth one
  63. // was already defined in the subnet, so should not be inherited.
  64. checkIntegerValue(subnet, "renew-timer", 100);
  65. checkIntegerValue(subnet, "rebind-timer", 2);
  66. checkIntegerValue(subnet, "preferred-lifetime", 3);
  67. checkIntegerValue(subnet, "valid-lifetime", 4);
  68. }
  69. };