Browse Source

[3807] Fixed toText functions in the DHCPv6 options.

Marcin Siodelski 10 years ago
parent
commit
52c850f6fc

+ 8 - 2
src/lib/dhcp/option.cc

@@ -214,14 +214,20 @@ std::string Option::toText(int indent) {
 }
 
 std::string
-Option::headerToText(const int indent) {
+Option::headerToText(const int indent, const std::string& type_name) {
     std::stringstream output;
     for (int i = 0; i < indent; i++)
         output << " ";
 
     int field_len = (getUniverse() == V4 ? 3 : 5);
     output << "type=" << std::setw(field_len) << std::setfill('0')
-           << type_ << ", len=" << std::setw(field_len) << std::setfill('0')
+           << type_;
+
+    if (!type_name.empty()) {
+        output << "(" << type_name << ")";
+    }
+
+    output << ", len=" << std::setw(field_len) << std::setfill('0')
            << len()-getHeaderLen();
     return (output.str());
 }

+ 4 - 1
src/lib/dhcp/option.h

@@ -429,9 +429,12 @@ protected:
     /// their respective @c toText implementations.
     ///
     /// @param indent Number of spaces to insert before the text.
+    /// @param type_name Option type name. If empty, the option name
+    /// is omitted.
     ///
     /// @return Option header in the textual format.
-    std::string headerToText(const int indent = 0);
+    std::string headerToText(const int indent = 0,
+                             const std::string& type_name = "");
 
     /// @brief Returns collection of suboptions in the textual format.
     ///

+ 10 - 18
src/lib/dhcp/option6_ia.cc

@@ -84,32 +84,24 @@ void Option6IA::unpack(OptionBufferConstIter begin,
     unpackOptions(OptionBuffer(begin, end));
 }
 
-std::string Option6IA::toText(int indent /* = 0*/) {
-    stringstream tmp;
+std::string Option6IA::toText(int indent) {
+    stringstream output;
 
-    for (int i=0; i<indent; i++)
-        tmp << " ";
-    tmp << "type=" << type_;
-
-    switch (type_) {
+    switch(getType()) {
     case D6O_IA_NA:
-        tmp << "(IA_NA)";
+        output << headerToText(indent, "IA_NA");
         break;
     case D6O_IA_PD:
-        tmp << "(IA_PD)";
+        output << headerToText(indent, "IA_PD");
         break;
     default:
-        tmp << "(unknown)";
+        output << headerToText(indent);
     }
-    tmp << " iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_
-        << " " << options_.size() << " sub-options:" << endl;
 
-    for (OptionCollection::const_iterator opt=options_.begin();
-         opt!=options_.end();
-         ++opt) {
-        tmp << (*opt).second->toText(indent+2);
-    }
-    return tmp.str();
+    output << ": iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_
+           << suboptionsToText(indent + 2);
+
+    return (output.str());
 }
 
 uint16_t Option6IA::len() {

+ 9 - 15
src/lib/dhcp/option6_iaaddr.cc

@@ -89,21 +89,15 @@ void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
     unpackOptions(OptionBuffer(begin, end));
 }
 
-std::string Option6IAAddr::toText(int indent /* =0 */) {
-    stringstream tmp;
-    for (int i=0; i<indent; i++)
-        tmp << " ";
-
-    tmp << "type=" << type_ << "(IAADDR) addr=" << addr_
-        << ", preferred-lft=" << preferred_  << ", valid-lft="
-        << valid_ << endl;
-
-    for (OptionCollection::const_iterator opt=options_.begin();
-         opt!=options_.end();
-         ++opt) {
-        tmp << (*opt).second->toText(indent+2);
-    }
-    return tmp.str();
+std::string Option6IAAddr::toText(int indent) {
+    std::stringstream output;
+    output << headerToText(indent, "IAADDR") << ": "
+           << "address=" << addr_
+           << ", preferred-lft=" << preferred_
+           << ", valid-lft=" << valid_;
+
+    output << suboptionsToText(indent + 2);
+    return (output.str());
 }
 
 uint16_t Option6IAAddr::len() {

+ 9 - 15
src/lib/dhcp/option6_iaprefix.cc

@@ -97,21 +97,15 @@ void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
     unpackOptions(OptionBuffer(begin, end));
 }
 
-std::string Option6IAPrefix::toText(int indent /* =0 */) {
-    stringstream tmp;
-    for (int i=0; i<indent; i++)
-        tmp << " ";
-
-    tmp << "type=" << type_ << "(IAPREFIX) prefix=" << addr_ << "/"
-        << static_cast<int>(prefix_len_) << ", preferred-lft="
-        << preferred_ << ", valid-lft=" << valid_ << endl;
-
-    for (OptionCollection::const_iterator opt=options_.begin();
-         opt!=options_.end();
-         ++opt) {
-        tmp << (*opt).second->toText(indent + 2);
-    }
-    return tmp.str();
+std::string Option6IAPrefix::toText(int indent) {
+    std::stringstream output;
+    output << headerToText(indent, "IAPREFIX") << ": "
+           << "prefix=" << addr_ << "/" << static_cast<int>(prefix_len_)
+           << ", preferred-lft=" << preferred_
+           << ", valid-lft=" << valid_;
+
+    output << suboptionsToText(indent + 2);
+    return (output.str());
 }
 
 uint16_t Option6IAPrefix::len() {

+ 17 - 0
src/lib/dhcp/tests/option4_addrlst_unittest.cc

@@ -252,4 +252,21 @@ TEST_F(Option4AddrLstTest, setAddresses) {
     EXPECT_NO_THROW(opt.reset());
 }
 
+// This test checks that the option holding IPv4 address list can
+// be converted to textual format.
+TEST_F(Option4AddrLstTest, toText) {
+    Option4AddrLst opt(111);
+    // Generate a few IPv4 addresses.
+    Option4AddrLst::AddressContainer addresses;
+    for (int i = 2; i < 6; ++i) {
+        std::stringstream s;
+        s << "192.0.2." << i;
+        addresses.push_back(IOAddress(s.str()));
+    }
+    opt.setAddresses(addresses);
+
+    EXPECT_EQ("type=111, len=016: 192.0.2.2 192.0.2.3 192.0.2.4 192.0.2.5",
+              opt.toText());
+}
+
 } // namespace

+ 18 - 1
src/lib/dhcp/tests/option6_addrlst_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -234,4 +234,21 @@ TEST_F(Option6AddrLstTest, setAddress) {
     EXPECT_NO_THROW(opt1.reset());
 }
 
+// This test checks that the option holding IPv6 address list can
+// be converted to textual format.
+TEST_F(Option6AddrLstTest, toText) {
+    Option6AddrLst opt(1234, IOAddress("2001:db8:1::1"));
+    // Generate a few IPv6 addresses.
+    Option6AddrLst::AddressContainer addresses;
+    for (int i = 2; i < 6; ++i) {
+        std::stringstream s;
+        s << "2001:db8:1::" << i;
+        addresses.push_back(IOAddress(s.str()));
+    }
+    opt.setAddresses(addresses);
+
+    EXPECT_EQ("type=01234, len=00064: 2001:db8:1::2 2001:db8:1::3 "
+              "2001:db8:1::4 2001:db8:1::5", opt.toText());
+}
+
 } // namespace

+ 43 - 2
src/lib/dhcp/tests/option6_ia_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -258,7 +258,7 @@ TEST_F(Option6IATest, pdSuboptionsPack) {
 }
 
 // test if option can parse suboptions
-TEST_F(Option6IATest, suboptions_unpack) {
+TEST_F(Option6IATest, suboptionsUnpack) {
     // sizeof (expected) = 48 bytes
     const uint8_t expected[] = {
         D6O_IA_NA / 256, D6O_IA_NA % 256, // type
@@ -323,4 +323,45 @@ TEST_F(Option6IATest, suboptions_unpack) {
     EXPECT_NO_THROW(ia.reset());
 }
 
+// This test checks that the IA_NA option is correctly converted to the
+// textual format.
+TEST_F(Option6IATest, toTextNA) {
+    Option6IA ia(D6O_IA_NA, 1234);
+    ia.setT1(200);
+    ia.setT2(300);
+
+    ia.addOption(OptionPtr(new Option6IAAddr(D6O_IAADDR, IOAddress("2001:db8:1::1"),
+                                             500, 600)));
+    ia.addOption(OptionPtr(new Option6IAAddr(D6O_IAADDR, IOAddress("2001:db8:1::2"),
+                                             450, 550)));
+
+    EXPECT_EQ("type=00003(IA_NA), len=00068: iaid=1234, t1=200, t2=300,\n"
+              "options:\n"
+              "  type=00005(IAADDR), len=00024: address=2001:db8:1::1, "
+              "preferred-lft=500, valid-lft=600\n"
+              "  type=00005(IAADDR), len=00024: address=2001:db8:1::2, "
+              "preferred-lft=450, valid-lft=550", ia.toText());
+}
+
+// This test checks that the IA_PD option is correctly converted to the
+// textual format.
+TEST_F(Option6IATest, toTextPD) {
+    Option6IA ia(D6O_IA_PD, 2345);
+    ia.setT1(200);
+    ia.setT2(300);
+
+    ia.addOption(OptionPtr(new Option6IAPrefix(D6O_IAPREFIX, IOAddress("2001:db8:1::"),
+                                               72, 500, 600)));
+    ia.addOption(OptionPtr(new Option6IAPrefix(D6O_IAPREFIX, IOAddress("2001:db8:1::"),
+                                               64, 450, 550)));
+
+    EXPECT_EQ("type=00025(IA_PD), len=00070: iaid=2345, t1=200, t2=300,\n"
+              "options:\n"
+              "  type=00026(IAPREFIX), len=00025: prefix=2001:db8:1::/72, "
+              "preferred-lft=500, valid-lft=600\n"
+              "  type=00026(IAPREFIX), len=00025: prefix=2001:db8:1::/64, "
+              "preferred-lft=450, valid-lft=550",
+              ia.toText());
+}
+
 }

+ 23 - 1
src/lib/dhcp/tests/option6_iaaddr_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -16,6 +16,7 @@
 
 #include <dhcp/dhcp6.h>
 #include <dhcp/option.h>
+#include <dhcp/option_int.h>
 #include <dhcp/option6_iaaddr.h>
 #include <util/buffer.h>
 
@@ -29,6 +30,7 @@
 
 using namespace std;
 using namespace isc;
+using namespace isc::asiolink;
 using namespace isc::dhcp;
 using namespace isc::util;
 
@@ -121,4 +123,24 @@ TEST_F(Option6IAAddrTest, negative) {
                                1000, 2000), BadValue);
 }
 
+// Tests that option can be converted to textual format.
+TEST_F(Option6IAAddrTest, toText) {
+    // Create option without suboptions.
+    Option6IAAddr opt(D6O_IAADDR, IOAddress("2001:db8:1::1"), 300, 400);
+    EXPECT_EQ("type=00005(IAADDR), len=00024: address=2001:db8:1::1,"
+              " preferred-lft=300, valid-lft=400",
+              opt.toText());
+
+    // Add suboptions and make sure they are printed.
+    opt.addOption(OptionPtr(new OptionUint32(Option::V6, 123, 234)));
+    opt.addOption(OptionPtr(new OptionUint32(Option::V6, 222, 333)));
+
+    EXPECT_EQ("type=00005(IAADDR), len=00040: address=2001:db8:1::1,"
+              " preferred-lft=300, valid-lft=400,\noptions:\n"
+              "  type=00123, len=00004: 234 (uint32)\n"
+              "  type=00222, len=00004: 333 (uint32)",
+              opt.toText());
+
+}
+
 }

+ 21 - 1
src/lib/dhcp/tests/option6_iaprefix_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -16,6 +16,7 @@
 
 #include <dhcp/dhcp6.h>
 #include <dhcp/option.h>
+#include <dhcp/option_int.h>
 #include <dhcp/option6_iaprefix.h>
 #include <util/buffer.h>
 
@@ -256,4 +257,23 @@ TEST_F(Option6IAPrefixTest, negative) {
                  BadValue);
 }
 
+// Checks if the option is converted to textual format correctly.
+TEST_F(Option6IAPrefixTest, toText) {
+    // Create option without suboptions.
+    Option6IAPrefix opt(D6O_IAPREFIX, IOAddress("2001:db8:1::"), 64, 300, 400);
+    EXPECT_EQ("type=00026(IAPREFIX), len=00025: prefix=2001:db8:1::/64,"
+              " preferred-lft=300, valid-lft=400",
+              opt.toText());
+
+    // Add suboptions and make sure they are printed.
+    opt.addOption(OptionPtr(new OptionUint32(Option::V6, 123, 234)));
+    opt.addOption(OptionPtr(new OptionUint32(Option::V6, 222, 333)));
+
+    EXPECT_EQ("type=00026(IAPREFIX), len=00041: prefix=2001:db8:1::/64,"
+              " preferred-lft=300, valid-lft=400,\noptions:\n"
+              "  type=00123, len=00004: 234 (uint32)\n"
+              "  type=00222, len=00004: 333 (uint32)",
+              opt.toText());
+}
+
 }