diff --git a/src/lib/dhcp/option_custom.cc b/src/lib/dhcp/option_custom.cc index ba845190f4..11e124c3eb 100644 --- a/src/lib/dhcp/option_custom.cc +++ b/src/lib/dhcp/option_custom.cc @@ -390,6 +390,13 @@ OptionCustom::readBinary(const uint32_t index) const { return (buffers_[index]); } +void +OptionCustom::writeBinary(const OptionBuffer& buf, + const uint32_t index) { + checkIndex(index); + buffers_[index] = buf; +} + bool OptionCustom::readBoolean(const uint32_t index) const { checkIndex(index); diff --git a/src/lib/dhcp/option_custom.h b/src/lib/dhcp/option_custom.h index 64d98869af..7be9ab57cc 100644 --- a/src/lib/dhcp/option_custom.h +++ b/src/lib/dhcp/option_custom.h @@ -118,6 +118,12 @@ public: /// @return read buffer holding binary data. const OptionBuffer& readBinary(const uint32_t index = 0) const; + /// @brief Write binary data into a buffer. + /// + /// @param buf buffer holding binary data to be written. + /// @param index buffer index. + void writeBinary(const OptionBuffer& buf, const uint32_t index = 0); + /// @brief Read a buffer as boolean value. /// /// @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 da5903f35c..16148eeecd 100644 --- a/src/lib/dhcp/tests/option_custom_unittest.cc +++ b/src/lib/dhcp/tests/option_custom_unittest.cc @@ -788,6 +788,40 @@ TEST_F(OptionCustomTest, recordDataTruncated) { ); } +// The purpose of this test is to verify that an option comprising +// single data field with binary data can be used and that this +// binary data is properly initialized to a default value. This +// test also checks that it is possible to override this default +// value. +TEST_F(OptionCustomTest, setBinaryData) { + OptionDefinition opt_def("OPTION_FOO", 1000, "binary"); + + // Create an option and let the data field be initialized + // to default value (do not provide any data buffer). + boost::scoped_ptr option; + ASSERT_NO_THROW( + option.reset(new OptionCustom(opt_def, Option::V6)); + ); + ASSERT_TRUE(option); + + // Get the default binary value. + OptionBuffer buf; + ASSERT_NO_THROW(option->readBinary()); + // The buffer is by default empty. + EXPECT_TRUE(buf.empty()); + // Prepare input buffer with some dummy data. + OptionBuffer buf_in(10); + for (int i = 0; i < buf_in.size(); ++i) { + buf_in[i] = i; + } + // Try to override the default binary buffer. + ASSERT_NO_THROW(option->writeBinary(buf_in)); + // And check that it has been actually overriden. + ASSERT_NO_THROW(buf = option->readBinary()); + ASSERT_EQ(buf_in.size(), buf.size()); + EXPECT_TRUE(std::equal(buf_in.begin(), buf_in.end(), buf.begin())); +} + // The purpose of this test is to verify that an option comprising // single boolean data field can be created and that its default // value can be overriden by a new value.