BIND 10 trac2491, updated. 45c04beca6f7d07f1a2c779baf3a7a8ba26763a2 [2491] Unit test to verify that writting and reading IP address works.

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Dec 3 19:07:30 UTC 2012


The branch, trac2491 has been updated
       via  45c04beca6f7d07f1a2c779baf3a7a8ba26763a2 (commit)
       via  bc44ee8bf3c35cacaad05751d75e8789aa5c9108 (commit)
      from  b561ee0919676f6e9c34329151c2b46d60f1824b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 45c04beca6f7d07f1a2c779baf3a7a8ba26763a2
Author: Marcin Siodelski <marcin at isc.org>
Date:   Mon Dec 3 20:07:22 2012 +0100

    [2491] Unit test to verify that writting and reading IP address works.

commit bc44ee8bf3c35cacaad05751d75e8789aa5c9108
Author: Marcin Siodelski <marcin at isc.org>
Date:   Mon Dec 3 19:36:31 2012 +0100

    [2491] Use different constructor to pass empty buffer.

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dhcp/tests/option_custom_unittest.cc     |   16 ++--
 src/lib/dhcp/tests/option_data_types_unittest.cc |   86 ++++++++++++++++++++++
 2 files changed, 97 insertions(+), 5 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dhcp/tests/option_custom_unittest.cc b/src/lib/dhcp/tests/option_custom_unittest.cc
index 562336e..ec6b53d 100644
--- a/src/lib/dhcp/tests/option_custom_unittest.cc
+++ b/src/lib/dhcp/tests/option_custom_unittest.cc
@@ -124,9 +124,10 @@ TEST_F(OptionCustomTest, constructor) {
 TEST_F(OptionCustomTest, emptyData) {
     OptionDefinition opt_def("OPTION_FOO", 232, "empty");
 
+    OptionBuffer buf;
     boost::scoped_ptr<OptionCustom> option;
     ASSERT_NO_THROW(
-        option.reset(new OptionCustom(opt_def, Option::V4, OptionBuffer()));
+        option.reset(new OptionCustom(opt_def, Option::V4, buf.begin(), buf.end()));
     );
     ASSERT_TRUE(option);
 
@@ -170,8 +171,10 @@ TEST_F(OptionCustomTest, binaryData) {
     EXPECT_TRUE(std::equal(buf_in.begin(), buf_in.end(), buf_out.begin()));
 
     // Check that option with "no data" is rejected.
+    buf_in.clear();
     EXPECT_THROW(
-        option.reset(new OptionCustom(opt_def, Option::V4, OptionBuffer())),
+        option.reset(new OptionCustom(opt_def, Option::V4, buf_in.begin(),
+                                      buf_in.end())),
         isc::OutOfRange
     );
 }
@@ -208,8 +211,9 @@ TEST_F(OptionCustomTest, booleanData) {
     EXPECT_FALSE(value);
 
     // Check that the option with "no data" is rejected.
+    buf.clear();
     EXPECT_THROW(
-        option.reset(new OptionCustom(opt_def, Option::V6, OptionBuffer())),
+        option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.end())),
         isc::OutOfRange
     );
 }
@@ -410,8 +414,9 @@ TEST_F(OptionCustomTest, stringData) {
     EXPECT_EQ("hello world!", value);
 
     // Check that option will not be created if empty buffer is provided.
+    buf.clear();
     EXPECT_THROW(
-        option.reset(new OptionCustom(opt_def, Option::V6, OptionBuffer())),
+        option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.end())),
         isc::OutOfRange
     );
 }
@@ -464,8 +469,9 @@ TEST_F(OptionCustomTest, booleanDataArray) {
 
     // Check that empty buffer can't be used to create option holding
     // array of boolean values.
+    buf.clear();
     EXPECT_THROW(
-         option.reset(new OptionCustom(opt_def, Option::V6, OptionBuffer())),
+         option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.end())),
          isc::OutOfRange
     );
 }
diff --git a/src/lib/dhcp/tests/option_data_types_unittest.cc b/src/lib/dhcp/tests/option_data_types_unittest.cc
index 8b8ee48..de81590 100644
--- a/src/lib/dhcp/tests/option_data_types_unittest.cc
+++ b/src/lib/dhcp/tests/option_data_types_unittest.cc
@@ -28,8 +28,94 @@ public:
     /// @brief Constructor.
     OptionDataTypesTest() { }
 
+    /// @brief Write IP address into a buffer.
+    ///
+    /// @param address address to be written.
+    /// @param [out] buf output buffer.
+    void writeAddress(const asiolink::IOAddress& address,
+                      std::vector<uint8_t>& buf) {
+        short family = address.getFamily();
+        if (family == AF_INET) {
+            asio::ip::address_v4::bytes_type buf_addr =
+                address.getAddress().to_v4().to_bytes();
+            buf.insert(buf.end(), buf_addr.begin(), buf_addr.end());
+        } else if (family == AF_INET6) {
+            asio::ip::address_v6::bytes_type buf_addr =
+                address.getAddress().to_v6().to_bytes();
+            buf.insert(buf.end(), buf_addr.begin(), buf_addr.end());
+        }
+    }
+
+    /// @brief Write integer (signed or unsigned) into a buffer.
+    ///
+    /// @param value integer value.
+    /// @param [out] buf output buffer.
+    /// @tparam integer type.
+    template<typename T>
+    void writeInt(T value, std::vector<uint8_t>& buf) {
+        for (int i = 0; i < sizeof(T); ++i) {
+            buf.push_back(value >> ((sizeof(T) - i - 1) * 8) & 0xFF);
+        }
+    }
+
+    /// @brief Write a string into a buffer.
+    ///
+    /// @param value string to be written into a buffer.
+    /// @param buf output buffer.
+    void writeString(const std::string& value,
+                     std::vector<uint8_t>& buf) {
+        buf.resize(buf.size() + value.size());
+        std::copy_backward(value.c_str(), value.c_str() + value.size(),
+                           buf.end());
+    }
 };
 
+// The goal of this test is to verify that an IPv4 address being
+// stored in a buffer (wire format) can be read into IOAddress
+// object.
+TEST_F(OptionDataTypesTest, readAddress) {
+    // Create some IPv4 address.
+    asiolink::IOAddress address("192.168.0.1");
+    // And store it in a buffer in a wire format.
+    std::vector<uint8_t> buf;
+    writeAddress(address, buf);
+
+    // Now, try to read the IP address with a utility function
+    // being under test.
+    asiolink::IOAddress address_out("127.0.0.1");
+    EXPECT_NO_THROW(address_out = OptionDataTypeUtil::readAddress(buf, AF_INET));
+
+    // Check that the read address matches address that
+    // we used as input.
+    EXPECT_EQ(address.toText(), address_out.toText());
+}
+
+// The goal of this test is to verify that an IPv6 address
+// is properly converted to wire format and stored in a
+// buffer.
+TEST_F(OptionDataTypesTest, writeAddress) {
+    // Encode an IPv6 address 2001:db8:1::1 in wire format.
+    // This will be used as reference data to validate if
+    // an IPv6 address is stored in a buffer properly.
+    const char data[] = {
+        0x20, 0x01, 0x0d, 0xb8, 0x0, 0x1, 0x0, 0x0,
+        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
+    };
+    std::vector<uint8_t> buf_in(data, data + sizeof(data));
+
+    // Create IPv6 address object.
+    asiolink::IOAddress address("2001:db8:1::1");
+    // Define the output buffer to write IP address to.
+    std::vector<uint8_t> buf_out;
+    // Write the address to the buffer.
+    ASSERT_NO_THROW(OptionDataTypeUtil::writeAddress(address, buf_out));
+    // Make sure that input and output buffers have the same size
+    // so we can compare them.
+    ASSERT_EQ(buf_in.size(), buf_out.size());
+    // And finally compare them.
+    EXPECT_TRUE(std::equal(buf_in.begin(), buf_in.end(), buf_out.begin()));
+}
+
 // The purpose of this test is to verify that FQDN is read from
 // a buffer and returned as a text. The representation of the FQDN
 // in the buffer complies with RFC1035, section 3.1.



More information about the bind10-changes mailing list