BIND 10 trac3316, updated. 282416056d98ce14b30998ae730c2ce1a4358dd0 [3316] Implemented function to return text representation of VC option

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Feb 13 16:46:46 UTC 2014


The branch, trac3316 has been updated
       via  282416056d98ce14b30998ae730c2ce1a4358dd0 (commit)
      from  87f58c5dd3b8555dc0ecb449f1f39e2cc0198806 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 282416056d98ce14b30998ae730c2ce1a4358dd0
Author: Marcin Siodelski <marcin at isc.org>
Date:   Thu Feb 13 17:46:36 2014 +0100

    [3316] Implemented function to return text representation of VC option

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dhcp/opaque_data_tuple.h                   |    8 ++--
 src/lib/dhcp/option_vendor_class.cc                |   24 ++++++++++
 src/lib/dhcp/option_vendor_class.h                 |    6 +++
 src/lib/dhcp/tests/option_vendor_class_unittest.cc |   50 ++++++++++++++++++++
 4 files changed, 85 insertions(+), 3 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dhcp/opaque_data_tuple.h b/src/lib/dhcp/opaque_data_tuple.h
index 82ea1ad..d1f9075 100644
--- a/src/lib/dhcp/opaque_data_tuple.h
+++ b/src/lib/dhcp/opaque_data_tuple.h
@@ -70,8 +70,8 @@ public:
 
     /// @brief Default constructor.
     ///
-    /// @param length_field_size Length of the field which holds the size of
-    /// the tuple.
+    /// @param length_field_type Indicates a length of the field which holds
+    /// the size of the tuple.
     OpaqueDataTuple(LengthFieldType length_field_type = LENGTH_2_BYTES);
 
     /// @brief Constructor
@@ -79,11 +79,13 @@ public:
     /// Creates a tuple from on-wire data. It calls @c OpaqueDataTuple::unpack
     /// internally.
     ///
+    /// @param length_field_type Indicates the length of the field holding the
+    /// opaque data size.
     /// @param begin Iterator pointing to the begining of the buffer holding
     /// wire data.
     /// @param end Iterator pointing to the end of the buffer holding wire data.
     /// @tparam InputIterator Type of the iterators passed to this function.
-    /// @throw It may throw an exception if the @unpack throws.
+    /// @throw It may throw an exception if the @c unpack throws.
     template<typename InputIterator>
     OpaqueDataTuple(LengthFieldType length_field_type, InputIterator begin,
                     InputIterator end)
diff --git a/src/lib/dhcp/option_vendor_class.cc b/src/lib/dhcp/option_vendor_class.cc
index c211b2f..343384f 100644
--- a/src/lib/dhcp/option_vendor_class.cc
+++ b/src/lib/dhcp/option_vendor_class.cc
@@ -15,6 +15,7 @@
 #include <exceptions/exceptions.h>
 #include <dhcp/opaque_data_tuple.h>
 #include <dhcp/option_vendor_class.h>
+#include <sstream>
 
 namespace isc {
 namespace dhcp {
@@ -163,5 +164,28 @@ OptionVendorClass::len() {
     return (length);
 }
 
+std::string
+OptionVendorClass::toText(int indent) {
+    std::ostringstream s;
+
+    // Apply indentation
+    s << std::string(indent, ' ');
+    // Print type, length and first occurence of enterprise id.
+    s << "type=" << getType() << ", len=" << len() - getHeaderLen() << ", "
+        " enterprise id=0x" << std::hex << getVendorId() << std::dec;
+    // Iterate over all tuples and print their size and contents.
+    for (int i = 0; i < getTuplesNum(); ++i) {
+        // The DHCPv4 V-I Vendor Class has enterprise id before every tuple.
+        if ((getUniverse() == V4) && (i > 0)) {
+            s << ", enterprise id=0x" << std::hex << getVendorId() << std::dec;
+        }
+        // Print the tuple.
+        s << ", data-len" << i << "=" << getTuple(i).getLength();
+        s << ", vendor-class-data" << i << "='" << getTuple(i) << "'";
+    }
+
+    return (s.str());
+}
+
 } // namespace isc::dhcp
 } // namespace isc
diff --git a/src/lib/dhcp/option_vendor_class.h b/src/lib/dhcp/option_vendor_class.h
index 1d43078..407c4b3 100644
--- a/src/lib/dhcp/option_vendor_class.h
+++ b/src/lib/dhcp/option_vendor_class.h
@@ -138,6 +138,12 @@ public:
     /// @brief Returns the full length of the option, including option header.
     virtual uint16_t len();
 
+    /// @brief Returns text representation of the option.
+    ///
+    /// @param indent Number of space characters before text.
+    /// @return Text representation of the option.
+    virtual std::string toText(int indent = 0);
+
 private:
 
     /// @brief Returns option code appropriate for the specified universe.
diff --git a/src/lib/dhcp/tests/option_vendor_class_unittest.cc b/src/lib/dhcp/tests/option_vendor_class_unittest.cc
index 2315e1e..eb99ad1 100644
--- a/src/lib/dhcp/tests/option_vendor_class_unittest.cc
+++ b/src/lib/dhcp/tests/option_vendor_class_unittest.cc
@@ -394,5 +394,55 @@ TEST(OptionVendorClass, unpack6NoTuple) {
     EXPECT_EQ(0, vendor_class->getTuplesNum());
 }
 
+// Verifies correctness of the text representation of the DHCPv4 option.
+TEST(OptionVendorClass, toText4) {
+    OptionVendorClass vendor_class(Option::V4, 1234);
+    ASSERT_EQ(1, vendor_class.getTuplesNum());
+    // By default, there is an empty tuple in the option. Let's replace
+    // it with the tuple with some data.
+    OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
+    tuple = "Hello world";
+    vendor_class.setTuple(0, tuple);
+    // And add another tuple so as resulting option is a bit more complex.
+    tuple = "foo";
+    vendor_class.addTuple(tuple);
+    // Check that the text representation of the option is as expected.
+    EXPECT_EQ("type=124, len=24,  enterprise id=0x4d2,"
+              " data-len0=11, vendor-class-data0='Hello world',"
+              " enterprise id=0x4d2, data-len1=3, vendor-class-data1='foo'",
+              vendor_class.toText());
+
+    // Check that indentation works.
+    EXPECT_EQ("   type=124, len=24,  enterprise id=0x4d2,"
+              " data-len0=11, vendor-class-data0='Hello world',"
+              " enterprise id=0x4d2, data-len1=3, vendor-class-data1='foo'",
+              vendor_class.toText(3));
+}
+
+// Verifies correctness of the text representation of the DHCPv4 option.
+TEST(OptionVendorClass, toText6) {
+    OptionVendorClass vendor_class(Option::V6, 1234);
+    ASSERT_EQ(0, vendor_class.getTuplesNum());
+    // By default, there is an empty tuple in the option. Let's replace
+    // it with the tuple with some data.
+    OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
+    tuple = "Hello world";
+    vendor_class.addTuple(tuple);
+    // And add another tuple so as resulting option is a bit more complex.
+    tuple = "foo";
+    vendor_class.addTuple(tuple);
+    // Check that the text representation of the option is as expected.
+    EXPECT_EQ("type=16, len=22,  enterprise id=0x4d2,"
+              " data-len0=11, vendor-class-data0='Hello world',"
+              " data-len1=3, vendor-class-data1='foo'",
+              vendor_class.toText());
+
+    // Check that indentation works.
+    EXPECT_EQ("  type=16, len=22,  enterprise id=0x4d2,"
+              " data-len0=11, vendor-class-data0='Hello world',"
+              " data-len1=3, vendor-class-data1='foo'",
+              vendor_class.toText(2));
+}
+
 } // end of anonymous namespace
 



More information about the bind10-changes mailing list