cfg_host_operations_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <dhcp/dhcp6.h>
  8. #include <dhcpsrv/cfg_host_operations.h>
  9. #include <dhcpsrv/host.h>
  10. #include <testutils/test_to_element.h>
  11. #include <gtest/gtest.h>
  12. #include <algorithm>
  13. #include <iterator>
  14. using namespace isc;
  15. using namespace isc::dhcp;
  16. using namespace isc::test;
  17. namespace {
  18. /// @brief Checks if specified identifier is present.
  19. ///
  20. /// @param cfg Object holding current configuration.
  21. /// @param id Identifier type which presence should be checked.
  22. /// @return true if specified identifier is present.
  23. bool
  24. identifierPresent(const CfgHostOperations& cfg, const Host::IdentifierType& id) {
  25. CfgHostOperations::IdentifierTypes types = cfg.getIdentifierTypes();
  26. return (std::find(types.begin(), types.end(), id) != types.end());
  27. }
  28. /// @brief Checks if specified identifier is at specified position.
  29. ///
  30. /// @param cfg Object holding current configuration.
  31. /// @param id Identifier type which presence should be checked.
  32. /// @param pos Position at which the identifier is expected on the list.
  33. /// @return true if specified identifier exists at specified position.
  34. bool
  35. identifierAtPosition(const CfgHostOperations& cfg, const Host::IdentifierType& id,
  36. const size_t pos) {
  37. CfgHostOperations::IdentifierTypes types = cfg.getIdentifierTypes();
  38. if (types.size() > pos) {
  39. CfgHostOperations::IdentifierTypes::const_iterator type = types.begin();
  40. std::advance(type, pos);
  41. return (*type == id);
  42. }
  43. return (false);
  44. }
  45. // This test checks that the list of identifiers is initially
  46. // empty.
  47. TEST(CfgHostOperationsTest, defaults) {
  48. CfgHostOperations cfg;
  49. EXPECT_TRUE(cfg.getIdentifierTypes().empty());
  50. runToElementTest<CfgHostOperations>("[ ]", cfg);
  51. }
  52. // This test verifies that identifier types can be added into an
  53. // ordered collection and then removed.
  54. TEST(CfgHostOperationsTest, addIdentifier) {
  55. CfgHostOperations cfg;
  56. // Only HW address added.
  57. ASSERT_NO_THROW(cfg.addIdentifierType("hw-address"));
  58. EXPECT_TRUE(identifierAtPosition(cfg, Host::IDENT_HWADDR, 0));
  59. EXPECT_FALSE(identifierPresent(cfg, Host::IDENT_DUID));
  60. EXPECT_FALSE(identifierPresent(cfg, Host::IDENT_CIRCUIT_ID));
  61. // HW address and DUID should be present.
  62. ASSERT_NO_THROW(cfg.addIdentifierType("duid"));
  63. EXPECT_TRUE(identifierAtPosition(cfg, Host::IDENT_HWADDR, 0));
  64. EXPECT_TRUE(identifierAtPosition(cfg, Host::IDENT_DUID, 1));
  65. EXPECT_FALSE(identifierPresent(cfg, Host::IDENT_CIRCUIT_ID));
  66. // All three identifiers should be present now.
  67. ASSERT_NO_THROW(cfg.addIdentifierType("circuit-id"));
  68. EXPECT_TRUE(identifierAtPosition(cfg, Host::IDENT_HWADDR, 0));
  69. EXPECT_TRUE(identifierAtPosition(cfg, Host::IDENT_DUID, 1));
  70. EXPECT_TRUE(identifierAtPosition(cfg, Host::IDENT_CIRCUIT_ID, 2));
  71. // Check unparse
  72. std::string ids = "[ \"hw-address\", \"duid\", \"circuit-id\" ]";
  73. runToElementTest<CfgHostOperations>(ids, cfg);
  74. // Let's clear and make sure no identifiers are present.
  75. ASSERT_NO_THROW(cfg.clearIdentifierTypes());
  76. EXPECT_TRUE(cfg.getIdentifierTypes().empty());
  77. runToElementTest<CfgHostOperations>("[ ]", cfg);
  78. }
  79. // This test verifies that the default DHCPv4 configuration is created
  80. // as expected.
  81. TEST(CfgHostOperationsTest, createConfig4) {
  82. CfgHostOperationsPtr cfg = CfgHostOperations::createConfig4();
  83. EXPECT_TRUE(identifierAtPosition(*cfg, Host::IDENT_HWADDR, 0));
  84. EXPECT_TRUE(identifierAtPosition(*cfg, Host::IDENT_DUID, 1));
  85. EXPECT_TRUE(identifierAtPosition(*cfg, Host::IDENT_CIRCUIT_ID, 2));
  86. }
  87. // This test verifies that the default DHCPv6 configuration is created
  88. // as expected.
  89. TEST(CfgHostOperationsTest, createConfig6) {
  90. CfgHostOperationsPtr cfg = CfgHostOperations::createConfig6();
  91. EXPECT_TRUE(identifierAtPosition(*cfg, Host::IDENT_HWADDR, 0));
  92. EXPECT_TRUE(identifierAtPosition(*cfg, Host::IDENT_DUID, 1));
  93. EXPECT_FALSE(identifierPresent(*cfg, Host::IDENT_CIRCUIT_ID));
  94. }
  95. } // end of anonymous namespace