diff --git a/src/lib/dhcp/option_custom.h b/src/lib/dhcp/option_custom.h index 5c792cbb10..10b2b7920b 100644 --- a/src/lib/dhcp/option_custom.h +++ b/src/lib/dhcp/option_custom.h @@ -64,6 +64,11 @@ public: OptionCustom(const OptionDefinition& def, Universe u, OptionBufferConstIter first, OptionBufferConstIter last); + /// @brief Return a number of the data fields. + /// + /// @return number of data fields held by the option. + uint32_t getDataFieldsNum() const { return (buffers_.size()); } + /// @brief Read a buffer as IP address. /// /// @param index buffer index. diff --git a/src/lib/dhcp/tests/option_custom_unittest.cc b/src/lib/dhcp/tests/option_custom_unittest.cc index 39a46868de..bfde9e4716 100644 --- a/src/lib/dhcp/tests/option_custom_unittest.cc +++ b/src/lib/dhcp/tests/option_custom_unittest.cc @@ -118,6 +118,9 @@ TEST_F(OptionCustomTest, emptyData) { option.reset(new OptionCustom(opt_def, Option::V4, OptionBuffer())); ); ASSERT_TRUE(option); + + // Option is 'empty' so no data fields are expected. + EXPECT_EQ(0, option->getDataFieldsNum()); } // The purpose of this test is to verify that the option definition comprising @@ -142,6 +145,9 @@ TEST_F(OptionCustomTest, binaryData) { ); ASSERT_TRUE(option); + // We should have just one data field. + ASSERT_EQ(1, option->getDataFieldsNum()); + // The custom option should hold just one buffer that can be // accessed using index 0. OptionBuffer buf_out; @@ -172,6 +178,9 @@ TEST_F(OptionCustomTest, booleanData) { ); ASSERT_TRUE(option); + // We should have just one data field. + ASSERT_EQ(1, option->getDataFieldsNum()); + // Initialize the value to true because we want to make sure // that it is modified to 'false' by readBoolean below. bool value = true; @@ -198,6 +207,9 @@ TEST_F(OptionCustomTest, int16Data) { ); ASSERT_TRUE(option); + // We should have just one data field. + ASSERT_EQ(1, option->getDataFieldsNum()); + // Initialize value to 0 explicitely to make sure that is // modified by readInteger function to expected -234. int16_t value = 0; @@ -221,6 +233,9 @@ TEST_F(OptionCustomTest, ipv4AddressData) { ); ASSERT_TRUE(option); + // We should have just one data field. + ASSERT_EQ(1, option->getDataFieldsNum()); + IOAddress address("127.0.0.1"); // Read IPv4 address from using index 0. ASSERT_NO_THROW(option->readAddress(0, address)); @@ -244,6 +259,9 @@ TEST_F(OptionCustomTest, ipv6AddressData) { ); ASSERT_TRUE(option); + // We should have just one data field. + ASSERT_EQ(1, option->getDataFieldsNum()); + // Custom option should comprise exactly one buffer that represents // IPv6 address. IOAddress address("::1"); @@ -269,6 +287,9 @@ TEST_F(OptionCustomTest, stringData) { ); ASSERT_TRUE(option); + // We should have just one data field. + ASSERT_EQ(1, option->getDataFieldsNum()); + // Custom option should now comprise single string value that // can be accessed using index 0. std::string value; @@ -298,6 +319,9 @@ TEST_F(OptionCustomTest, booleanDataArray) { ); ASSERT_TRUE(option); + // We should have 5 data fields. + ASSERT_EQ(5, option->getDataFieldsNum()); + // Read values from custom option using indexes 0..4 and // check that they are valid. bool value0 = false; @@ -350,6 +374,9 @@ TEST_F(OptionCustomTest, uint32DataArray) { ); ASSERT_TRUE(option); + // We should have 3 data fields. + ASSERT_EQ(3, option->getDataFieldsNum()); + // Expect only 3 values. for (int i = 0; i < 3; ++i) { uint32_t value = 0; @@ -382,6 +409,9 @@ TEST_F(OptionCustomTest, ipv4AddressDataArray) { ); ASSERT_TRUE(option); + // We should have 3 data fields. + ASSERT_EQ(3, option->getDataFieldsNum()); + // We expect 3 IPv4 addresses being stored in the option. for (int i = 0; i < 3; ++i) { IOAddress address("10.10.10.10"); @@ -410,10 +440,13 @@ TEST_F(OptionCustomTest, ipv6AddressDataArray) { // Use the input buffer to create custom option. boost::scoped_ptr option; ASSERT_NO_THROW( - option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.begin() + 70)); + option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.begin() + 50)); ); ASSERT_TRUE(option); + // We should have 3 data fields. + ASSERT_EQ(3, option->getDataFieldsNum()); + // We expect 3 IPv6 addresses being stored in the option. for (int i = 0; i < 3; ++i) { IOAddress address("fe80::4"); @@ -453,6 +486,9 @@ TEST_F(OptionCustomTest, recordData) { ); ASSERT_TRUE(option); + // We should have 5 data fields. + ASSERT_EQ(5, option->getDataFieldsNum()); + // Verify value in the field 0. uint16_t value0 = 0; ASSERT_NO_THROW(value0 = option->readInteger(0)); @@ -571,6 +607,9 @@ TEST_F(OptionCustomTest, unpack) { ); ASSERT_TRUE(option); + // We should have 3 data fields. + ASSERT_EQ(3, option->getDataFieldsNum()); + // We expect 3 IPv4 addresses being stored in the option. for (int i = 0; i < 3; ++i) { IOAddress address("10.10.10.10"); @@ -595,6 +634,9 @@ TEST_F(OptionCustomTest, unpack) { // Perform 'unpack'. ASSERT_NO_THROW(option->unpack(buf.begin(), buf.end())); + // Now we should have only 2 data fields. + ASSERT_EQ(2, option->getDataFieldsNum()); + // Verify that the addresses have been overwritten. for (int i = 0; i < 2; ++i) { IOAddress address("10.10.10.10"); @@ -624,10 +666,13 @@ TEST_F(OptionCustomTest, setData) // Use the input buffer to create custom option. boost::scoped_ptr option; ASSERT_NO_THROW( - option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.begin() + 70)); + option.reset(new OptionCustom(opt_def, Option::V6, buf.begin(), buf.begin() + 50)); ); ASSERT_TRUE(option); + // We should have 3 data fields. + ASSERT_EQ(3, option->getDataFieldsNum()); + // We expect 3 IPv6 addresses being stored in the option. for (int i = 0; i < 3; ++i) { IOAddress address("fe80::4"); @@ -651,6 +696,9 @@ TEST_F(OptionCustomTest, setData) // Replace the option data. ASSERT_NO_THROW(option->setData(buf.begin(), buf.end())); + // Now we should have only 2 data fields. + ASSERT_EQ(2, option->getDataFieldsNum()); + // Check that it has been replaced. for (int i = 0; i < 2; ++i) { IOAddress address("10.10.10.10");