From: Marcin Siodelski Date: Thu, 13 Feb 2014 16:46:36 +0000 (+0100) Subject: [3316] Implemented function to return text representation of VC option X-Git-Tag: bind10-1.2.0beta1-release~35^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=282416056d98ce14b30998ae730c2ce1a4358dd0;p=thirdparty%2Fkea.git [3316] Implemented function to return text representation of VC option --- diff --git a/src/lib/dhcp/opaque_data_tuple.h b/src/lib/dhcp/opaque_data_tuple.h index 82ea1ad394..d1f907522c 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 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 c211b2f0fc..343384fd4a 100644 --- a/src/lib/dhcp/option_vendor_class.cc +++ b/src/lib/dhcp/option_vendor_class.cc @@ -15,6 +15,7 @@ #include #include #include +#include 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 1d43078bcc..407c4b3d16 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 2315e1e586..eb99ad12cf 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