simple_parser6_unittest.cc 3.2 KB

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