From: Tomek Mrugalski Date: Tue, 30 Oct 2012 17:22:50 +0000 (+0100) Subject: [2414] DUID toText() implemented. X-Git-Tag: trac2487_base~1^2~31^2~31 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bd3faa6a3d1d5628509b725ecb61ef91ee3c2bb0;p=thirdparty%2Fkea.git [2414] DUID toText() implemented. --- diff --git a/src/lib/dhcp/duid.cc b/src/lib/dhcp/duid.cc index db7ba25624..aa06e95a93 100644 --- a/src/lib/dhcp/duid.cc +++ b/src/lib/dhcp/duid.cc @@ -12,11 +12,13 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -#include #include #include #include #include +#include +#include +#include namespace isc { namespace dhcp { @@ -53,6 +55,22 @@ DUID::DUIDType DUID::getType() const { } } +std::string DUID::toText() const { + std::stringstream tmp; + + bool delim = false; + for (std::vector::const_iterator it = duid_.begin(); + it != duid_.end(); ++it) { + if (delim) { + tmp << ":"; + } + tmp.width(2); + tmp << std::hex << std::setfill('0') << static_cast(*it); + delim = true; + } + return (tmp.str()); +} + bool DUID::operator == (const DUID& other) const { return (this->duid_ == other.duid_); } diff --git a/src/lib/dhcp/duid.h b/src/lib/dhcp/duid.h index 5f8ad58f20..ef7e476600 100644 --- a/src/lib/dhcp/duid.h +++ b/src/lib/dhcp/duid.h @@ -61,10 +61,13 @@ class DUID { /// @brief returns DUID type DUIDType getType() const; - // compares two DUIDs + /// returns textual prepresentation (e.g. 00:01:02:03:ff) + std::string toText() const; + + /// compares two DUIDs bool operator == (const DUID& other) const; - // compares two DUIDs + /// compares two DUIDs bool operator != (const DUID& other) const; protected: diff --git a/src/lib/dhcp/tests/duid_unittest.cc b/src/lib/dhcp/tests/duid_unittest.cc index aaf6d910bc..6096acfa54 100644 --- a/src/lib/dhcp/tests/duid_unittest.cc +++ b/src/lib/dhcp/tests/duid_unittest.cc @@ -166,4 +166,12 @@ TEST(ClientIdTest, operators) { EXPECT_TRUE(*id1 != *id3); } +// Test checks if the toText() returns valid texual representation +TEST(ClientIdTest, toText) { + uint8_t data1[] = {0, 1, 2, 3, 4, 0xff, 0xfe}; + + scoped_ptr duid1(new DUID(data1, sizeof(data1))); + EXPECT_EQ("00:01:02:03:04:ff:fe", duid1->toText()); +} + } // end of anonymous namespace