From: Marcin Siodelski Date: Tue, 11 Dec 2012 10:04:31 +0000 (+0100) Subject: [2526] Added unit tests for V4 option holding an array of integers. X-Git-Tag: bind10-1.0.0-beta-release~19^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=92e23241bb54197ec11ed79691bf678cd042de6d;p=thirdparty%2Fkea.git [2526] Added unit tests for V4 option holding an array of integers. --- diff --git a/src/lib/dhcp/option_int.h b/src/lib/dhcp/option_int.h index 21e2997322..ebb16415c7 100644 --- a/src/lib/dhcp/option_int.h +++ b/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 diff --git a/src/lib/dhcp/option_int_array.h b/src/lib/dhcp/option_int_array.h index a97f437807..500415233f 100644 --- a/src/lib/dhcp/option_int_array.h +++ b/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(); diff --git a/src/lib/dhcp/tests/option_int_array_unittest.cc b/src/lib/dhcp/tests/option_int_array_unittest.cc index 8c1d31266f..1aeb5841c4 100644 --- a/src/lib/dhcp/tests/option_int_array_unittest.cc +++ b/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 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 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 out_data; ASSERT_NO_THROW(out.readVector(out_data, opt_len)); @@ -290,26 +322,50 @@ TEST_F(OptionIntArrayTest, useInvalidType) { } +TEST_F(OptionIntArrayTest, bufferToUint8V4) { + bufferToIntTest8(Option::V4); +} + TEST_F(OptionIntArrayTest, bufferToUint8V6) { bufferToIntTest8(Option::V6); } +TEST_F(OptionIntArrayTest, bufferToInt8V4) { + bufferToIntTest8(Option::V4); +} + TEST_F(OptionIntArrayTest, bufferToInt8V6) { bufferToIntTest8(Option::V6); } +TEST_F(OptionIntArrayTest, bufferToUint16V4) { + bufferToIntTest16(Option::V4); +} + TEST_F(OptionIntArrayTest, bufferToUint16V6) { bufferToIntTest16(Option::V6); } +TEST_F(OptionIntArrayTest, bufferToInt16V4) { + bufferToIntTest16(Option::V4); +} + TEST_F(OptionIntArrayTest, bufferToInt16V6) { bufferToIntTest16(Option::V6); } +TEST_F(OptionIntArrayTest, bufferToUint32V4) { + bufferToIntTest32(Option::V4); +} + TEST_F(OptionIntArrayTest, bufferToUint32V6) { bufferToIntTest32(Option::V6); } +TEST_F(OptionIntArrayTest, bufferToInt32V4) { + bufferToIntTest32(Option::V4); +} + TEST_F(OptionIntArrayTest, bufferToInt32V6) { bufferToIntTest32(Option::V6); }