Browse Source

[3316] Implemented function to check if specified tuple is in VC option.

Marcin Siodelski 11 years ago
parent
commit
a0eb24bea3

+ 1 - 1
src/lib/dhcp/opaque_data_tuple.cc

@@ -89,7 +89,7 @@ OpaqueDataTuple::operator=(const std::string& other) {
 }
 
 bool
-OpaqueDataTuple::operator==(const std::string& other) {
+OpaqueDataTuple::operator==(const std::string& other) const {
     return (equals(other));
 }
 

+ 1 - 1
src/lib/dhcp/opaque_data_tuple.h

@@ -261,7 +261,7 @@ public:
     ///
     /// @param other String to compare the tuple against.
     /// @return true if data carried in the tuple is equal to the string.
-    bool operator==(const std::string& other);
+    bool operator==(const std::string& other) const;
 
     /// @brief Inequality operator.
     ///

+ 14 - 0
src/lib/dhcp/option_vendor_class.cc

@@ -130,6 +130,20 @@ OptionVendorClass::getTuple(const size_t at) const {
     return (tuples_[at]);
 }
 
+bool
+OptionVendorClass::hasTuple(const std::string& tuple_str) const {
+    // Iterate over existing tuples (there shouldn't be many of them),
+    // and try to match the searched one.
+    for (TuplesCollection::const_iterator it = tuples_.begin();
+         it != tuples_.end(); ++it) {
+        if (*it == tuple_str) {
+            return (true);
+        }
+    }
+    return (false);
+}
+
+
 uint16_t
 OptionVendorClass::len() {
     // The option starts with the header and enterprise id.

+ 7 - 0
src/lib/dhcp/option_vendor_class.h

@@ -128,6 +128,13 @@ public:
         return (tuples_);
     }
 
+    /// @brief Checks if the Vendor Class holds the opaque data tuple with the
+    /// specified string.
+    ///
+    /// @param tuple_str String representation of the tuple being searched.
+    /// @return true if the specified tuple exists for this option.
+    bool hasTuple(const std::string& tuple_str) const;
+
     /// @brief Returns the full length of the option, including option header.
     virtual uint16_t len();
 

+ 5 - 0
src/lib/dhcp/tests/option_vendor_class_unittest.cc

@@ -74,6 +74,11 @@ TEST(OptionVendorClass, addTuple) {
     EXPECT_EQ("xyz", vendor_class.getTuple(0).getText());
     EXPECT_EQ("abc", vendor_class.getTuple(1).getText());
 
+    // Check that hasTuple correctly identifies existing tuples.
+    EXPECT_TRUE(vendor_class.hasTuple("xyz"));
+    EXPECT_TRUE(vendor_class.hasTuple("abc"));
+    EXPECT_FALSE(vendor_class.hasTuple("other"));
+
     // Attempt to add the tuple with 1 byte long length field should fail
     // for DHCPv6 option.
     OpaqueDataTuple tuple2(OpaqueDataTuple::LENGTH_1_BYTE);