From: Marcin Siodelski Date: Mon, 2 Sep 2013 15:20:34 +0000 (+0200) Subject: [3035] Added functions to construct DHCID object from raw buffer. X-Git-Tag: bind10-1.2.0beta1-release~102^2~21^2~22 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ecd5a9bc58e0f15933f5508afec02a0d48ba342e;p=thirdparty%2Fkea.git [3035] Added functions to construct DHCID object from raw buffer. --- diff --git a/src/lib/dhcp_ddns/ncr_msg.cc b/src/lib/dhcp_ddns/ncr_msg.cc index 3c7a18ecea..d5a36f5887 100644 --- a/src/lib/dhcp_ddns/ncr_msg.cc +++ b/src/lib/dhcp_ddns/ncr_msg.cc @@ -34,6 +34,10 @@ D2Dhcid::D2Dhcid(const std::string& data) { fromStr(data); } +D2Dhcid::D2Dhcid(const std::vector& data) + : bytes_(data) { +} + D2Dhcid::D2Dhcid(const isc::dhcp::DUID& duid, const std::vector& wire_fqdn) { fromDUID(duid, wire_fqdn); @@ -55,6 +59,11 @@ D2Dhcid::toStr() const { return (isc::util::encode::encodeHex(bytes_)); } +void +D2Dhcid::fromBytes(const std::vector& data) { + bytes_ = data; +} + void D2Dhcid::fromDUID(const isc::dhcp::DUID& duid, const std::vector& wire_fqdn) { diff --git a/src/lib/dhcp_ddns/ncr_msg.h b/src/lib/dhcp_ddns/ncr_msg.h index ceb8715c19..db693dfb9e 100644 --- a/src/lib/dhcp_ddns/ncr_msg.h +++ b/src/lib/dhcp_ddns/ncr_msg.h @@ -78,6 +78,12 @@ public: /// or there is an odd number of digits. D2Dhcid(const std::string& data); + /// @brief Constructor, creates new instance of the @c D2Dhcid using a + /// vector holding raw DHCID. + /// + /// @param data An vector holding DHCID in the raw format. + D2Dhcid(const std::vector& data); + /// @brief Constructor, creates an instance of the @c D2Dhcid from the /// @c isc::dhcp::DUID. /// @@ -101,6 +107,11 @@ public: /// or there is an odd number of digits. void fromStr(const std::string& data); + /// @brief Sets the DHCID value represented by raw data. + /// + /// @param data Holds the raw bytes representing DHCID. + void fromBytes(const std::vector& data); + /// @brief Sets the DHCID value based on the DUID and FQDN. /// /// This function requires that the FQDN conforms to the section 3.5 diff --git a/src/lib/dhcp_ddns/tests/ncr_unittests.cc b/src/lib/dhcp_ddns/tests/ncr_unittests.cc index df1a50a69a..418658e1cc 100644 --- a/src/lib/dhcp_ddns/tests/ncr_unittests.cc +++ b/src/lib/dhcp_ddns/tests/ncr_unittests.cc @@ -387,6 +387,32 @@ TEST(NameChangeRequestTest, dhcidFromMaxDUID) { EXPECT_EQ(dhcid_ref, dhcid.toStr()); } +// Test that DHCID is correctly created from the buffer holding DHCID data +// in raw format. +TEST(NameChangeRequestTest, dhcidFromBytes) { + // Create a buffer holding raw DHCID data. + std::vector dhcid_data; + for (int i = 0; i < 64; ++i) { + dhcid_data.push_back(i); + } + // Construct new object and initialize it with the DHCID data. + D2Dhcid dhcid(dhcid_data); + + // Make sure that the DHCID is valid. + EXPECT_TRUE(std::equal(dhcid.getBytes().begin(), dhcid.getBytes().end(), + dhcid_data.begin())); + + // Modify the buffer and reinitialize DHCID with the new buffer. + for (int i = 64; i < 128; ++i) { + dhcid_data.push_back(i); + } + ASSERT_NO_THROW(dhcid.fromBytes(dhcid_data)); + + // Make sure that the DHCID is still valid. + EXPECT_TRUE(std::equal(dhcid.getBytes().begin(), dhcid.getBytes().end(), + dhcid_data.begin())); + +} /// @brief Verifies the fundamentals of converting from and to JSON. /// It verifies that: