Browse Source

[2524] Add ClientId::toText() functionality

Stephen Morris 12 years ago
parent
commit
be90b92cbe
3 changed files with 23 additions and 2 deletions
  1. 10 0
      src/lib/dhcp/duid.cc
  2. 3 0
      src/lib/dhcp/duid.h
  3. 10 2
      src/lib/dhcp/tests/duid_unittest.cc

+ 10 - 0
src/lib/dhcp/duid.cc

@@ -95,6 +95,16 @@ const std::vector<uint8_t> ClientId::getClientId() const {
     return (duid_);
 }
 
+// Returns the Client ID in text form
+std::string ClientId::toText() const {
+
+    // As DUID is a private base class of ClientId, we can't access
+    // its public toText() method through inheritance: instead we
+    // need the interface of a ClientId::toText() that calls the
+    // equivalent method in the base class.
+    return (DUID::toText());
+}
+
 // Compares two client-ids
 bool ClientId::operator==(const ClientId& other) const {
     return (this->duid_ == other.duid_);

+ 3 - 0
src/lib/dhcp/duid.h

@@ -107,6 +107,9 @@ public:
     /// @brief Returns reference to the client-id data
     const std::vector<uint8_t> getClientId() const;
 
+    /// @brief Returns textual representation of a DUID (e.g. 00:01:02:03:ff)
+    std::string toText() const;
+
     /// @brief Compares two client-ids for equality
     bool operator==(const ClientId& other) const;
 

+ 10 - 2
src/lib/dhcp/tests/duid_unittest.cc

@@ -108,6 +108,14 @@ TEST(DuidTest, getType) {
     EXPECT_EQ(DUID::DUID_UNKNOWN, duid_invalid->getType());
 }
 
+// Test checks if the toText() returns valid texual representation
+TEST(DuidTest, toText) {
+    uint8_t data1[] = {0, 1, 2, 3, 4, 0xff, 0xfe};
+
+    DUID duid(data1, sizeof(data1));
+    EXPECT_EQ("00:01:02:03:04:ff:fe", duid.toText());
+}
+
 // This test checks if the comparison operators are sane.
 TEST(DuidTest, operators) {
     uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6};
@@ -174,8 +182,8 @@ TEST(ClientIdTest, operators) {
 TEST(ClientIdTest, toText) {
     uint8_t data1[] = {0, 1, 2, 3, 4, 0xff, 0xfe};
 
-    DUID duid(data1, sizeof(data1));
-    EXPECT_EQ("00:01:02:03:04:ff:fe", duid.toText());
+    ClientId clientid(data1, sizeof(data1));
+    EXPECT_EQ("00:01:02:03:04:ff:fe", clientid.toText());
 }
 
 } // end of anonymous namespace