diff --git a/src/lib/dhcp/duid.cc b/src/lib/dhcp/duid.cc index 91efe94ad3..f1c8866c1c 100644 --- a/src/lib/dhcp/duid.cc +++ b/src/lib/dhcp/duid.cc @@ -95,6 +95,16 @@ const std::vector 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_); diff --git a/src/lib/dhcp/duid.h b/src/lib/dhcp/duid.h index 60b9706df6..a4e32b3b05 100644 --- a/src/lib/dhcp/duid.h +++ b/src/lib/dhcp/duid.h @@ -107,6 +107,9 @@ public: /// @brief Returns reference to the client-id data const std::vector 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; diff --git a/src/lib/dhcp/tests/duid_unittest.cc b/src/lib/dhcp/tests/duid_unittest.cc index 9db4c352b3..de20e51060 100644 --- a/src/lib/dhcp/tests/duid_unittest.cc +++ b/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