From: Tomek Mrugalski Date: Thu, 29 Nov 2012 16:30:50 +0000 (+0100) Subject: [2325] Option::equal() method implemented. X-Git-Tag: bind10-1.0.0-beta-release~29^2~2^2^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=159af7d5de17a74269cd4af69ac3b569495c4a4e;p=thirdparty%2Fkea.git [2325] Option::equal() method implemented. --- diff --git a/src/lib/dhcp/option.cc b/src/lib/dhcp/option.cc index 4638025f03..2a53f0f247 100644 --- a/src/lib/dhcp/option.cc +++ b/src/lib/dhcp/option.cc @@ -314,6 +314,10 @@ void Option::setData(const OptionBufferConstIter first, std::copy(first, last, data_.begin()); } +bool Option::equal(const OptionPtr& other) const { + return ( (getType() == other->getType()) && + (getData() == other->getData()) ); +} Option::~Option() { diff --git a/src/lib/dhcp/option.h b/src/lib/dhcp/option.h index a6b062286e..29f0f01760 100644 --- a/src/lib/dhcp/option.h +++ b/src/lib/dhcp/option.h @@ -197,7 +197,7 @@ public: /// Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6) /// /// @return option type - uint16_t getType() { return (type_); } + uint16_t getType() const { return (type_); } /// Returns length of the complete option (data length + DHCPv4/DHCPv6 /// option header) @@ -219,7 +219,7 @@ public: /// /// @return pointer to actual data (or reference to an empty vector /// if there is no data) - virtual const OptionBuffer& getData() { return (data_); } + virtual const OptionBuffer& getData() const { return (data_); } /// Adds a sub-option. /// @@ -303,6 +303,19 @@ public: /// just to force that every option has virtual dtor virtual ~Option(); + /// @brief Checks if two options are equal + /// + /// Equality verifies option type and option content. Care should + /// be taken when using this method. Implementation for derived + /// classes should be provided when this method is expected to be + /// used. It is safe in general, as the first check (different types) + /// will detect differences between base Option and derived + /// objects. + /// + /// @param other the other option + /// @return true if both options are equal + virtual bool equal(const OptionPtr& other) const; + protected: /// Builds raw (over-wire) buffer of this option, including all /// defined suboptions. Version for building DHCPv4 options. diff --git a/src/lib/dhcp/tests/option_unittest.cc b/src/lib/dhcp/tests/option_unittest.cc index 50d143fe40..afa64d52b0 100644 --- a/src/lib/dhcp/tests/option_unittest.cc +++ b/src/lib/dhcp/tests/option_unittest.cc @@ -524,4 +524,27 @@ TEST_F(OptionTest, setData) { EXPECT_TRUE(0 == memcmp(&buf_[0], test_data + opt1->getHeaderLen(), buf_.size())); } + +// This test verifies that options can be compared using equal() method. +TEST_F(OptionTest, equal) { + + // five options with varying lengths + OptionPtr opt1(new Option(Option::V6, 258, buf_.begin(), buf_.begin() + 1)); + OptionPtr opt2(new Option(Option::V6, 258, buf_.begin(), buf_.begin() + 2)); + OptionPtr opt3(new Option(Option::V6, 258, buf_.begin(), buf_.begin() + 3)); + + // the same content as opt2, but different type + OptionPtr opt4(new Option(Option::V6, 1, buf_.begin(), buf_.begin() + 2)); + + // another instance with the same type and content as opt2 + OptionPtr opt5(new Option(Option::V6, 258, buf_.begin(), buf_.begin() + 2)); + + EXPECT_TRUE(opt1->equal(opt1)); + + EXPECT_FALSE(opt1->equal(opt2)); + EXPECT_FALSE(opt1->equal(opt3)); + EXPECT_FALSE(opt1->equal(opt4)); + + EXPECT_TRUE(opt2->equal(opt5)); +} } diff --git a/src/lib/dhcpsrv/subnet.h b/src/lib/dhcpsrv/subnet.h index aa1ef1fbad..c7d7ac7c73 100644 --- a/src/lib/dhcpsrv/subnet.h +++ b/src/lib/dhcpsrv/subnet.h @@ -172,7 +172,7 @@ public: // Use option type as the index key. The type is held // in OptionPtr object so we have to call Option::getType // to retrieve this key for each element. - boost::multi_index::mem_fun< + boost::multi_index::const_mem_fun< Option, uint16_t, &Option::getType