From: Marcin Siodelski Date: Wed, 19 Dec 2018 20:26:24 +0000 (+0100) Subject: [#99,!176] Added deletion of vendor options from the configuration. X-Git-Tag: 380-unexpected-boost-include-capture_base~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bea0353e28c04c2464a8abe25c97078aae279bf6;p=thirdparty%2Fkea.git [#99,!176] Added deletion of vendor options from the configuration. --- diff --git a/src/lib/dhcpsrv/cfg_option.cc b/src/lib/dhcpsrv/cfg_option.cc index 0125709a68..71a4d5a66d 100644 --- a/src/lib/dhcpsrv/cfg_option.cc +++ b/src/lib/dhcpsrv/cfg_option.cc @@ -226,6 +226,19 @@ CfgOption::del(const std::string& option_space, const uint16_t option_code) { return (idx.erase(option_code)); } +size_t +CfgOption::del(const uint32_t vendor_id, const uint16_t option_code) { + // Check for presence of options. + OptionContainerPtr vendor_options = getAll(vendor_id); + if (!vendor_options || vendor_options->empty()) { + // There are no options, so there is nothing to do. + return (0); + } + + auto& idx = vendor_options->get<1>(); + return (idx.erase(option_code)); +} + ElementPtr CfgOption::toElement() const { // option-data value is a list of maps diff --git a/src/lib/dhcpsrv/cfg_option.h b/src/lib/dhcpsrv/cfg_option.h index a618076c6d..92a6c4ee92 100644 --- a/src/lib/dhcpsrv/cfg_option.h +++ b/src/lib/dhcpsrv/cfg_option.h @@ -420,11 +420,19 @@ public: /// it is also deleted from all option instances encapsulating this /// option space. /// - /// @param key Option space name or vendor identifier. + /// @param option_space Option space name. /// @param option_code Code of the option to be returned. /// /// @return Number of deleted options. - size_t del(const std::string& key, const uint16_t option_code); + size_t del(const std::string& option_space, const uint16_t option_code); + + /// @brief Delets vendor option for the specified vendor id. + /// + /// @param vendor_id Vendor identifier. + /// @param option_code Option code. + /// + /// @return Number of deleted options. + size_t del(const uint32_t vendor_id, const uint16_t option_code); /// @brief Returns a list of configured option space names. /// diff --git a/src/lib/dhcpsrv/tests/cfg_option_unittest.cc b/src/lib/dhcpsrv/tests/cfg_option_unittest.cc index 0a619d2931..563928286d 100644 --- a/src/lib/dhcpsrv/tests/cfg_option_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_option_unittest.cc @@ -400,7 +400,7 @@ TEST_F(CfgOptionTest, encapsulate) { } // This test verifies that an option can be deleted from the configuration. -TEST_F(CfgOptionTest, del) { +TEST_F(CfgOptionTest, delFromOptionSpace) { CfgOption cfg; generateEncapsulatedOptions(cfg); @@ -438,9 +438,36 @@ TEST_F(CfgOptionTest, del) { ASSERT_NO_THROW(top_level_option = cfg.get(DHCP6_OPTION_SPACE, code)); ASSERT_TRUE(top_level_option.option_); EXPECT_FALSE(top_level_option.option_->getOption(5)); + // Other options should remain. + EXPECT_TRUE(top_level_option.option_->getOption(1)); } } +// This test verifies that a vendor option can be deleted from the configuration. +TEST_F(CfgOptionTest, delVendorOption) { + CfgOption cfg; + + // Create multiple vendor options for vendor id 123. + for (uint16_t code = 100; code < 110; ++code) { + OptionPtr option(new Option(Option::V6, code, OptionBuffer(1, 0xFF))); + ASSERT_NO_THROW(cfg.add(option, false, "vendor-123")); + } + + // Make sure that the option we're trying to delete is there. + ASSERT_TRUE(cfg.get(123, 105).option_); + + // Delete the option. + uint64_t deleted_num; + ASSERT_NO_THROW(deleted_num = cfg.del(123, 105)); + EXPECT_EQ(1, deleted_num); + + // Make sure the option is gone. + EXPECT_FALSE(cfg.get(123, 105).option_); + + // Other options, like 107, shouldn't be gone. + EXPECT_TRUE(cfg.get(123, 107).option_); +} + // This test verifies that single option can be retrieved from the configuration // using option code and option space. TEST_F(CfgOptionTest, get) {