Commit 159af7d5 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰

[2325] Option::equal() method implemented.

parent 00eee13b
......@@ -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() {
......
......@@ -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.
......
......@@ -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));
}
}
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment