Browse Source

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

Marcin Siodelski 11 years ago
parent
commit
282416056d

+ 5 - 3
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)

+ 24 - 0
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

+ 6 - 0
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.

+ 50 - 0
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