BIND 10 trac2491, updated. 7c0a4abbb4cce4460c8b0cfd9f1447e0cc37644d [2491] Added some more negative test cases to option_data_types_unittest.

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Dec 4 11:34:56 UTC 2012


The branch, trac2491 has been updated
       via  7c0a4abbb4cce4460c8b0cfd9f1447e0cc37644d (commit)
      from  9b8747917cc300904783b403a64c2a11ebdb5ec0 (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 7c0a4abbb4cce4460c8b0cfd9f1447e0cc37644d
Author: Marcin Siodelski <marcin at isc.org>
Date:   Tue Dec 4 12:34:47 2012 +0100

    [2491] Added some more negative test cases to option_data_types_unittest.

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

Summary of changes:
 src/lib/dhcp/option_data_types.h                 |    7 ++-
 src/lib/dhcp/tests/option_data_types_unittest.cc |   67 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dhcp/option_data_types.h b/src/lib/dhcp/option_data_types.h
index 58567cd..b52cc4d 100644
--- a/src/lib/dhcp/option_data_types.h
+++ b/src/lib/dhcp/option_data_types.h
@@ -276,7 +276,12 @@ public:
                       " by readInteger is unsupported integer type");
         }
 
-        assert(buf.size() == OptionDataTypeTraits<T>::len);
+        if (buf.size() < OptionDataTypeTraits<T>::len) {
+            isc_throw(isc::dhcp::BadDataTypeCast,
+                      "failed to read an integer value from a buffer"
+                      << " - buffer is truncated.");
+        }
+
         T value;
         switch (OptionDataTypeTraits<T>::len) {
         case 1:
diff --git a/src/lib/dhcp/tests/option_data_types_unittest.cc b/src/lib/dhcp/tests/option_data_types_unittest.cc
index 73e226e..0e3f7ec 100644
--- a/src/lib/dhcp/tests/option_data_types_unittest.cc
+++ b/src/lib/dhcp/tests/option_data_types_unittest.cc
@@ -88,6 +88,30 @@ TEST_F(OptionDataTypesTest, readAddress) {
     // Check that the read address matches address that
     // we used as input.
     EXPECT_EQ(address.toText(), address_out.toText());
+
+    // Check that an attempt to read the buffer as IPv6 address
+    // causes an error as the IPv6 address needs at least 16 bytes
+    // long buffer.
+    EXPECT_THROW(
+        OptionDataTypeUtil::readAddress(buf, AF_INET6),
+        isc::dhcp::BadDataTypeCast
+    );
+
+    buf.clear();
+
+    // Do another test like this for IPv6 address.
+    address = asiolink::IOAddress("2001:db8:1:0::1");
+    writeAddress(address, buf);
+    EXPECT_NO_THROW(address_out = OptionDataTypeUtil::readAddress(buf, AF_INET6));
+    EXPECT_EQ(address.toText(), address_out.toText());
+
+    // Truncate the buffer and expect an error to be reported when
+    // trying to read it.
+    buf.resize(buf.size() - 1);
+    EXPECT_THROW(
+        OptionDataTypeUtil::readAddress(buf, AF_INET6),
+        isc::dhcp::BadDataTypeCast
+    );
 }
 
 // The goal of this test is to verify that an IPv6 address
@@ -114,6 +138,18 @@ TEST_F(OptionDataTypesTest, writeAddress) {
     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()));
+
+    buf_out.clear();
+
+    // Do similar test for IPv4 address.
+    address = asiolink::IOAddress("192.168.0.1");
+    ASSERT_NO_THROW(OptionDataTypeUtil::writeAddress(address, buf_out));
+    ASSERT_EQ(4, buf_out.size());
+    // Verify that the IP address has been written correctly.
+    EXPECT_EQ(192, buf_out[0]);
+    EXPECT_EQ(168, buf_out[1]);
+    EXPECT_EQ(0, buf_out[2]);
+    EXPECT_EQ(1, buf_out[3]);
 }
 
 // The purpose of this test is to verify that binary data represented
@@ -196,6 +232,14 @@ TEST_F(OptionDataTypesTest, readInt) {
         valueUint8 = OptionDataTypeUtil::readInt<uint8_t>(buf);
     );
     EXPECT_EQ(129, valueUint8);
+
+    // Try to read 16-bit value from a buffer holding 8-bit value.
+    // This should result in an exception.
+    EXPECT_THROW(
+        OptionDataTypeUtil::readInt<uint16_t>(buf),
+        isc::dhcp::BadDataTypeCast
+    );
+
     // Clear the buffer for the next check we are going to do.
     buf.clear();
 
@@ -206,6 +250,14 @@ TEST_F(OptionDataTypesTest, readInt) {
         valueUint16 = OptionDataTypeUtil::readInt<uint16_t>(buf);
     );
     EXPECT_EQ(1234, valueUint16);
+
+    // Try to read 32-bit value from a buffer holding 16-bit value.
+    // This should result in an exception.
+    EXPECT_THROW(
+        OptionDataTypeUtil::readInt<uint32_t>(buf),
+        isc::dhcp::BadDataTypeCast
+    );
+
     buf.clear();
 
     // Test uint32_t value.
@@ -226,6 +278,13 @@ TEST_F(OptionDataTypesTest, readInt) {
     EXPECT_EQ(-65, valueInt8);
     buf.clear();
 
+    // Try to read 16-bit value from a buffer holding 8-bit value.
+    // This should result in an exception.
+    EXPECT_THROW(
+        OptionDataTypeUtil::readInt<int16_t>(buf),
+        isc::dhcp::BadDataTypeCast
+    );
+
     // Test int16_t value.
     writeInt<int16_t>(2345, buf);
     int32_t valueInt16 = 0;
@@ -235,6 +294,13 @@ TEST_F(OptionDataTypesTest, readInt) {
     EXPECT_EQ(2345, valueInt16);
     buf.clear();
 
+    // Try to read 32-bit value from a buffer holding 16-bit value.
+    // This should result in an exception.
+    EXPECT_THROW(
+        OptionDataTypeUtil::readInt<int32_t>(buf),
+        isc::dhcp::BadDataTypeCast
+    );
+
     // Test int32_t value.
     writeInt<int32_t>(-16543, buf);
     int32_t valueInt32 = 0;
@@ -242,6 +308,7 @@ TEST_F(OptionDataTypesTest, readInt) {
         valueInt32 = OptionDataTypeUtil::readInt<int32_t>(buf);
     );
     EXPECT_EQ(-16543, valueInt32);
+
     buf.clear();
 }
 



More information about the bind10-changes mailing list