Commit 1dc0f087 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[2491] Add a function to set binary data for a data field.

parent 1bbe1315
......@@ -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);
......
......@@ -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.
......
......@@ -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<OptionCustom> 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.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment