]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2414] DUID toText() implemented.
authorTomek Mrugalski <tomasz@isc.org>
Tue, 30 Oct 2012 17:22:50 +0000 (18:22 +0100)
committerTomek Mrugalski <tomasz@isc.org>
Tue, 30 Oct 2012 17:22:50 +0000 (18:22 +0100)
src/lib/dhcp/duid.cc
src/lib/dhcp/duid.h
src/lib/dhcp/tests/duid_unittest.cc

index db7ba256240376f65eeb3a369733207a0a67bac9..aa06e95a93dbe4440ed8b02321e50dd829ec4655 100644 (file)
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#include <vector>
 #include <exceptions/exceptions.h>
 #include <stdint.h>
 #include <util/io_utilities.h>
 #include <dhcp/duid.h>
+#include <vector>
+#include <sstream>
+#include <iomanip>
 
 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<uint8_t>::const_iterator it = duid_.begin();
+         it != duid_.end(); ++it) {
+        if (delim) {
+            tmp << ":";
+        }
+        tmp.width(2);
+        tmp << std::hex << std::setfill('0') << static_cast<unsigned int>(*it);
+        delim = true;
+    }
+    return (tmp.str());
+}
+
 bool DUID::operator == (const DUID& other) const {
     return (this->duid_ == other.duid_);
 }
index 5f8ad58f20c53a71dbd5593580fb2745bbad6ea8..ef7e476600bd224b23f0827d922d27bbbda120e8 100644 (file)
@@ -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:
index aaf6d910bca9b2a15c4636c2b9716555e4db7c30..6096acfa547af63abaa2ac5a735831d41b9ef9e4 100644 (file)
@@ -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<DUID> duid1(new DUID(data1, sizeof(data1)));
+    EXPECT_EQ("00:01:02:03:04:ff:fe", duid1->toText());
+}
+
 } // end of anonymous namespace