Browse Source

[2526] Added unit tests for V4 option holding an array of integers.

Marcin Siodelski 12 years ago
parent
commit
92e23241bb

+ 1 - 1
src/lib/dhcp/option_int.h

@@ -171,7 +171,7 @@ public:
     /// @return length of this option
     virtual uint16_t len() {
         // Calculate the length of the header.
-        uint16_t length = (universe_ == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
+        uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
         // The data length is equal to size of T.
         length += sizeof(T);;
         // length of all suboptions

+ 5 - 3
src/lib/dhcp/option_int_array.h

@@ -116,8 +116,9 @@ public:
     /// equal to 1, 2 or 4 bytes. The data type is not checked in this function
     /// because it is checked in a constructor.
     void pack(isc::util::OutputBuffer& buf) {
-        buf.writeUint16(type_);
-        buf.writeUint16(len() - OPTION6_HDR_LEN);
+        // Pack option header.
+        packHeader(buf);
+        // Pack option data.
         for (int i = 0; i < values_.size(); ++i) {
             // Depending on the data type length we use different utility functions
             // writeUint16 or writeUint32 which write the data in the network byte
@@ -211,7 +212,8 @@ public:
     ///
     /// @return length of this option
     virtual uint16_t len() {
-        uint16_t length = OPTION6_HDR_LEN + values_.size() * sizeof(T);
+        uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
+        length += values_.size() * sizeof(T);
         // length of all suboptions
         for (Option::OptionCollection::iterator it = options_.begin();
              it != options_.end();

+ 75 - 19
src/lib/dhcp/tests/option_int_array_unittest.cc

@@ -75,7 +75,7 @@ public:
                                                           buf_.begin() + opt_len))
         );
 
-        EXPECT_EQ(Option::V6, opt->getUniverse());
+        EXPECT_EQ(u, opt->getUniverse());
         EXPECT_EQ(opt_code, opt->getType());
         // Option should return the collection of int8_t or uint8_t values that
         // we can match with the buffer we used to create the option.
@@ -100,15 +100,26 @@ public:
         // Data length is 10 bytes.
         EXPECT_EQ(10, opt->len() - opt->getHeaderLen());
         EXPECT_EQ(opt_code, opt->getType());
-        // The total length is 10 bytes for data and 4 bytes for header.
-        ASSERT_EQ(14, out_buf_.getLength());
 
         // Check if pack worked properly:
         InputBuffer out(out_buf_.getData(), out_buf_.getLength());
-        // if option type is correct
-        EXPECT_EQ(opt_code, out.readUint16());
-        // if option length is correct
-        EXPECT_EQ(10, out.readUint16());
+
+        if (u == Option::V4) {
+            // The total length is 10 bytes for data and 2 bytes for a header.
+            ASSERT_EQ(12, out_buf_.getLength());
+            // if option type is correct
+            EXPECT_EQ(opt_code, out.readUint8());
+            // if option length is correct
+            EXPECT_EQ(10, out.readUint8());
+        } else {
+            // The total length is 10 bytes for data and 4 bytes for a header.
+            ASSERT_EQ(14, out_buf_.getLength());
+            // if option type is correct
+            EXPECT_EQ(opt_code, out.readUint16());
+            // if option length is correct
+            EXPECT_EQ(10, out.readUint16());
+        }
+
         // if data is correct
         std::vector<uint8_t> out_data;
         ASSERT_NO_THROW(out.readVector(out_data, opt_len));
@@ -172,15 +183,25 @@ public:
         // Data length is 20 bytes.
         EXPECT_EQ(20, opt->len() - opt->getHeaderLen());
         EXPECT_EQ(opt_code, opt->getType());
-        // The total length is 20 bytes for data and 4 bytes for header.
-        ASSERT_EQ(24, out_buf_.getLength());
 
         // Check if pack worked properly:
         InputBuffer out(out_buf_.getData(), out_buf_.getLength());
-        // if option type is correct
-        EXPECT_EQ(opt_code, out.readUint16());
-        // if option length is correct
-        EXPECT_EQ(20, out.readUint16());
+
+        if (u == Option::V4) {
+            // The total length is 20 bytes for data and 2 bytes for a header.
+            ASSERT_EQ(22, out_buf_.getLength());
+            // if option type is correct
+            EXPECT_EQ(opt_code, out.readUint8());
+            // if option length is correct
+            EXPECT_EQ(20, out.readUint8());
+        } else {
+            // The total length is 20 bytes for data and 4 bytes for a header.
+            ASSERT_EQ(24, out_buf_.getLength());
+            // if option type is correct
+            EXPECT_EQ(opt_code, out.readUint16());
+            // if option length is correct
+            EXPECT_EQ(20, out.readUint16());
+        }
         // if data is correct
         std::vector<uint8_t> out_data;
         ASSERT_NO_THROW(out.readVector(out_data, opt_len));
@@ -246,15 +267,26 @@ public:
         // Data length is 40 bytes.
         EXPECT_EQ(40, opt->len() - opt->getHeaderLen());
         EXPECT_EQ(opt_code, opt->getType());
-        // The total length is 40 bytes for data and 4 bytes for header.
-        ASSERT_EQ(44, out_buf_.getLength());
 
         // Check if pack worked properly:
         InputBuffer out(out_buf_.getData(), out_buf_.getLength());
-        // if option type is correct
-        EXPECT_EQ(opt_code, out.readUint16());
-        // if option length is correct
-        EXPECT_EQ(40, out.readUint16());
+
+        if (u == Option::V4) {
+            // The total length is 40 bytes for data and 2 bytes for a header.
+            ASSERT_EQ(42, out_buf_.getLength());
+            // if option type is correct
+            EXPECT_EQ(opt_code, out.readUint8());
+            // if option length is correct
+            EXPECT_EQ(40, out.readUint8());
+        } else {
+            // The total length is 40 bytes for data and 4 bytes for a header.
+            ASSERT_EQ(44, out_buf_.getLength());
+            // if option type is correct
+            EXPECT_EQ(opt_code, out.readUint16());
+            // if option length is correct
+            EXPECT_EQ(40, out.readUint16());
+        }
+
         // if data is correct
         std::vector<uint8_t> out_data;
         ASSERT_NO_THROW(out.readVector(out_data, opt_len));
@@ -290,26 +322,50 @@ TEST_F(OptionIntArrayTest, useInvalidType) {
 
 }
 
+TEST_F(OptionIntArrayTest, bufferToUint8V4) {
+    bufferToIntTest8<uint8_t>(Option::V4);
+}
+
 TEST_F(OptionIntArrayTest, bufferToUint8V6) {
     bufferToIntTest8<uint8_t>(Option::V6);
 }
 
+TEST_F(OptionIntArrayTest, bufferToInt8V4) {
+    bufferToIntTest8<int8_t>(Option::V4);
+}
+
 TEST_F(OptionIntArrayTest, bufferToInt8V6) {
     bufferToIntTest8<int8_t>(Option::V6);
 }
 
+TEST_F(OptionIntArrayTest, bufferToUint16V4) {
+    bufferToIntTest16<uint16_t>(Option::V4);
+}
+
 TEST_F(OptionIntArrayTest, bufferToUint16V6) {
     bufferToIntTest16<uint16_t>(Option::V6);
 }
 
+TEST_F(OptionIntArrayTest, bufferToInt16V4) {
+    bufferToIntTest16<int16_t>(Option::V4);
+}
+
 TEST_F(OptionIntArrayTest, bufferToInt16V6) {
     bufferToIntTest16<int16_t>(Option::V6);
 }
 
+TEST_F(OptionIntArrayTest, bufferToUint32V4) {
+    bufferToIntTest32<uint32_t>(Option::V4);
+}
+
 TEST_F(OptionIntArrayTest, bufferToUint32V6) {
     bufferToIntTest32<uint32_t>(Option::V6);
 }
 
+TEST_F(OptionIntArrayTest, bufferToInt32V4) {
+    bufferToIntTest32<int32_t>(Option::V4);
+}
+
 TEST_F(OptionIntArrayTest, bufferToInt32V6) {
     bufferToIntTest32<int32_t>(Option::V6);
 }