From: Marcin Siodelski Date: Tue, 4 Dec 2012 11:06:18 +0000 (+0100) Subject: [2491] Added some missing unit tests. X-Git-Tag: bind10-1.0.0-beta-release~19^2~15^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b8747917cc300904783b403a64c2a11ebdb5ec0;p=thirdparty%2Fkea.git [2491] Added some missing unit tests. --- diff --git a/src/lib/dhcp/tests/option_data_types_unittest.cc b/src/lib/dhcp/tests/option_data_types_unittest.cc index de815908b4..73e226e2c7 100644 --- a/src/lib/dhcp/tests/option_data_types_unittest.cc +++ b/src/lib/dhcp/tests/option_data_types_unittest.cc @@ -116,6 +116,173 @@ TEST_F(OptionDataTypesTest, writeAddress) { EXPECT_TRUE(std::equal(buf_in.begin(), buf_in.end(), buf_out.begin())); } +// The purpose of this test is to verify that binary data represented +// as a string of hexadecimal digits can be written to a buffer. +TEST_F(OptionDataTypesTest, writeBinary) { + // Prepare the reference data. + const char data[] = { + 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, + 0x6, 0x7, 0x8, 0x9, 0xA, 0xB + }; + std::vector buf_ref(data, data + sizeof(data)); + // Create empty vector where binary data will be written to. + std::vector buf; + ASSERT_NO_THROW( + OptionDataTypeUtil::writeBinary("000102030405060708090A0B", buf) + ); + // Verify that the buffer contains valid data. + ASSERT_EQ(buf_ref.size(), buf.size()); + EXPECT_TRUE(std::equal(buf_ref.begin(), buf_ref.end(), buf.begin())); +} + +// The purpose of this test is to verify that the boolean value stored +// in a buffer is correctly read from this buffer. +TEST_F(OptionDataTypesTest, readBool) { + // Create an input buffer. + std::vector buf; + // 'true' value is encoded as 1 ('false' is encoded as 0) + buf.push_back(1); + + // Read the value from the buffer. + bool value = false; + ASSERT_NO_THROW( + value = OptionDataTypeUtil::readBool(buf); + ); + // Verify the value. + EXPECT_TRUE(value); + // Check if 'false' is read correctly either. + buf[0] = 0; + ASSERT_NO_THROW( + value = OptionDataTypeUtil::readBool(buf); + ); + EXPECT_FALSE(value); + + // Check that invalid value causes exception. + buf[0] = 5; + ASSERT_THROW( + OptionDataTypeUtil::readBool(buf), + isc::dhcp::BadDataTypeCast + ); +} + +// The purpose of this test is to verify that boolean values +// are correctly encoded in a buffer as '1' for 'true' and +// '0' for 'false' values. +TEST_F(OptionDataTypesTest, writeBool) { + // Create a buffer we will write to. + std::vector buf; + // Write the 'true' value to the buffer. + ASSERT_NO_THROW(OptionDataTypeUtil::writeBool(true, buf)); + // We should now have 'true' value stored in a buffer. + ASSERT_EQ(1, buf.size()); + EXPECT_EQ(buf[0], 1); + // Let's append another value to make sure that it is not always + // 'true' value being written. + ASSERT_NO_THROW(OptionDataTypeUtil::writeBool(false, buf)); + ASSERT_EQ(2, buf.size()); + EXPECT_EQ(buf[1], 0); +} + +// The purpose of this test is to verify that the integer values +// of different types are correctly read from a buffer. +TEST_F(OptionDataTypesTest, readInt) { + std::vector buf; + + // Write an 8-bit unsigned integer value to the buffer. + writeInt(129, buf); + uint8_t valueUint8 = 0; + // Read the value and check that it is valid. + ASSERT_NO_THROW( + valueUint8 = OptionDataTypeUtil::readInt(buf); + ); + EXPECT_EQ(129, valueUint8); + // Clear the buffer for the next check we are going to do. + buf.clear(); + + // Test uint16_t value. + writeInt(1234, buf); + uint16_t valueUint16 = 0; + ASSERT_NO_THROW( + valueUint16 = OptionDataTypeUtil::readInt(buf); + ); + EXPECT_EQ(1234, valueUint16); + buf.clear(); + + // Test uint32_t value. + writeInt(56789, buf); + uint32_t valueUint32 = 0; + ASSERT_NO_THROW( + valueUint32 = OptionDataTypeUtil::readInt(buf); + ); + EXPECT_EQ(56789, valueUint32); + buf.clear(); + + // Test int8_t value. + writeInt(-65, buf); + int8_t valueInt8 = 0; + ASSERT_NO_THROW( + valueInt8 = OptionDataTypeUtil::readInt(buf); + ); + EXPECT_EQ(-65, valueInt8); + buf.clear(); + + // Test int16_t value. + writeInt(2345, buf); + int32_t valueInt16 = 0; + ASSERT_NO_THROW( + valueInt16 = OptionDataTypeUtil::readInt(buf); + ); + EXPECT_EQ(2345, valueInt16); + buf.clear(); + + // Test int32_t value. + writeInt(-16543, buf); + int32_t valueInt32 = 0; + ASSERT_NO_THROW( + valueInt32 = OptionDataTypeUtil::readInt(buf); + ); + EXPECT_EQ(-16543, valueInt32); + buf.clear(); +} + +// The purpose of this test is to verify that integer values of different +// types are correctly written to a buffer. +TEST_F(OptionDataTypesTest, writeInt) { + // Prepare the reference buffer. + const char data[] = { + 0x7F, // 127 + 0x03, 0xFF, // 1023 + 0x00, 0x00, 0x10, 0x00, // 4096 + 0xFF, 0xFF, 0xFC, 0x00, // -1024 + 0x02, 0x00, // 512 + 0x81 // -127 + }; + std::vector buf_ref(data, data + sizeof(data)); + + // Fill in the buffer with data. Each write operation appends an + // integer value. Eventually the buffer holds all values and should + // match with the reference buffer. + std::vector buf; + // Write uint8_t + ASSERT_NO_THROW(OptionDataTypeUtil::writeInt(127, buf)); + // Write uint16_t + ASSERT_NO_THROW(OptionDataTypeUtil::writeInt(1023, buf)); + // Write uint32_t + ASSERT_NO_THROW(OptionDataTypeUtil::writeInt(4096, buf)); + // Write int32_t + ASSERT_NO_THROW(OptionDataTypeUtil::writeInt(-1024, buf)); + // Write int16_t + ASSERT_NO_THROW(OptionDataTypeUtil::writeInt(512, buf)); + // Write int8_t + ASSERT_NO_THROW(OptionDataTypeUtil::writeInt(-127, buf)); + + // Make sure that the buffer has the same size as the reference + // buffer. + ASSERT_EQ(buf_ref.size(), buf.size()); + // Compare buffers. + EXPECT_TRUE(std::equal(buf_ref.begin(), buf_ref.end(), buf.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. @@ -229,4 +396,34 @@ TEST_F(OptionDataTypesTest, writeFqdn) { ); } +// The purpose of this test is to verify that the string +// can be read from a buffer correctly. +TEST_F(OptionDataTypesTest, readString) { + // Prepare a buffer with some string in it. + std::vector buf; + writeString("hello world", buf); + + // Read the string from the buffer. + std::string value; + ASSERT_NO_THROW( + value = OptionDataTypeUtil::readString(buf); + ); + // Check that it is valid. + EXPECT_EQ("hello world", value); +} + +// The purpose of this test is to verify that a string can be +// stored in a buffer correctly. +TEST_F(OptionDataTypesTest, writeString) { + // Prepare a buffer with a reference data. + std::vector buf_ref; + writeString("hello world!", buf_ref); + // Create empty buffer we will write to. + std::vector buf; + ASSERT_NO_THROW(OptionDataTypeUtil::writeString("hello world!", buf)); + // Compare two buffers. + ASSERT_EQ(buf_ref.size(), buf.size()); + EXPECT_TRUE(std::equal(buf_ref.begin(), buf_ref.end(), buf.begin())); +} + } // anonymous namespace