From: Marcin Siodelski Date: Mon, 18 May 2015 08:24:29 +0000 (+0200) Subject: [3807] Fixed toText functions in the DHCPv6 options. X-Git-Tag: trac3732a_base~19^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=52c850f6fc611171e7dabbfb60a43536f479daa7;p=thirdparty%2Fkea.git [3807] Fixed toText functions in the DHCPv6 options. --- diff --git a/src/lib/dhcp/option.cc b/src/lib/dhcp/option.cc index dcf919960b..8a796c58b0 100644 --- a/src/lib/dhcp/option.cc +++ b/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()); } diff --git a/src/lib/dhcp/option.h b/src/lib/dhcp/option.h index 68e141da07..1b3022a0b8 100644 --- a/src/lib/dhcp/option.h +++ b/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. /// diff --git a/src/lib/dhcp/option6_ia.cc b/src/lib/dhcp/option6_ia.cc index 2079d0421d..0260d94ef2 100644 --- a/src/lib/dhcp/option6_ia.cc +++ b/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; itoText(indent+2); - } - return tmp.str(); + output << ": iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_ + << suboptionsToText(indent + 2); + + return (output.str()); } uint16_t Option6IA::len() { diff --git a/src/lib/dhcp/option6_iaaddr.cc b/src/lib/dhcp/option6_iaaddr.cc index 8474eb4923..f5e781ad27 100644 --- a/src/lib/dhcp/option6_iaaddr.cc +++ b/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(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(prefix_len_) + << ", preferred-lft=" << preferred_ + << ", valid-lft=" << valid_; + + output << suboptionsToText(indent + 2); + return (output.str()); } uint16_t Option6IAPrefix::len() { diff --git a/src/lib/dhcp/tests/option4_addrlst_unittest.cc b/src/lib/dhcp/tests/option4_addrlst_unittest.cc index 767d69011a..6777c7f262 100644 --- a/src/lib/dhcp/tests/option4_addrlst_unittest.cc +++ b/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 diff --git a/src/lib/dhcp/tests/option6_addrlst_unittest.cc b/src/lib/dhcp/tests/option6_addrlst_unittest.cc index 35a7829b03..b48aeed57d 100644 --- a/src/lib/dhcp/tests/option6_addrlst_unittest.cc +++ b/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 diff --git a/src/lib/dhcp/tests/option6_ia_unittest.cc b/src/lib/dhcp/tests/option6_ia_unittest.cc index 2a91505892..c9d472b7e1 100644 --- a/src/lib/dhcp/tests/option6_ia_unittest.cc +++ b/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()); +} + } diff --git a/src/lib/dhcp/tests/option6_iaaddr_unittest.cc b/src/lib/dhcp/tests/option6_iaaddr_unittest.cc index d2e6a156db..dba2b96acd 100644 --- a/src/lib/dhcp/tests/option6_iaaddr_unittest.cc +++ b/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 #include +#include #include #include @@ -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()); + +} + } diff --git a/src/lib/dhcp/tests/option6_iaprefix_unittest.cc b/src/lib/dhcp/tests/option6_iaprefix_unittest.cc index 2861c478ee..f4b5b3a71e 100644 --- a/src/lib/dhcp/tests/option6_iaprefix_unittest.cc +++ b/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 #include +#include #include #include @@ -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()); +} + }