Browse Source

[2414] DUID toText() implemented.

Tomek Mrugalski 12 years ago
parent
commit
bd3faa6a3d
3 changed files with 32 additions and 3 deletions
  1. 19 1
      src/lib/dhcp/duid.cc
  2. 5 2
      src/lib/dhcp/duid.h
  3. 8 0
      src/lib/dhcp/tests/duid_unittest.cc

+ 19 - 1
src/lib/dhcp/duid.cc

@@ -12,11 +12,13 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
-#include <vector>
 #include <exceptions/exceptions.h>
 #include <exceptions/exceptions.h>
 #include <stdint.h>
 #include <stdint.h>
 #include <util/io_utilities.h>
 #include <util/io_utilities.h>
 #include <dhcp/duid.h>
 #include <dhcp/duid.h>
+#include <vector>
+#include <sstream>
+#include <iomanip>
 
 
 namespace isc {
 namespace isc {
 namespace dhcp {
 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 {
 bool DUID::operator == (const DUID& other) const {
     return (this->duid_ == other.duid_);
     return (this->duid_ == other.duid_);
 }
 }

+ 5 - 2
src/lib/dhcp/duid.h

@@ -61,10 +61,13 @@ class DUID {
     /// @brief returns DUID type
     /// @brief returns DUID type
     DUIDType getType() const;
     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;
     bool operator == (const DUID& other) const;
 
 
-    // compares two DUIDs
+    /// compares two DUIDs
     bool operator != (const DUID& other) const;
     bool operator != (const DUID& other) const;
 
 
  protected:
  protected:

+ 8 - 0
src/lib/dhcp/tests/duid_unittest.cc

@@ -166,4 +166,12 @@ TEST(ClientIdTest, operators) {
     EXPECT_TRUE(*id1 != *id3);
     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
 } // end of anonymous namespace