Parcourir la source

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

Marcin Siodelski il y a 12 ans
Parent
commit
1dc0f0871d

+ 7 - 0
src/lib/dhcp/option_custom.cc

@@ -390,6 +390,13 @@ OptionCustom::readBinary(const uint32_t index) const {
     return (buffers_[index]);
     return (buffers_[index]);
 }
 }
 
 
+void
+OptionCustom::writeBinary(const OptionBuffer& buf,
+                          const uint32_t index) {
+    checkIndex(index);
+    buffers_[index] = buf;
+}
+
 bool
 bool
 OptionCustom::readBoolean(const uint32_t index) const {
 OptionCustom::readBoolean(const uint32_t index) const {
     checkIndex(index);
     checkIndex(index);

+ 6 - 0
src/lib/dhcp/option_custom.h

@@ -118,6 +118,12 @@ public:
     /// @return read buffer holding binary data.
     /// @return read buffer holding binary data.
     const OptionBuffer& readBinary(const uint32_t index = 0) const;
     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.
     /// @brief Read a buffer as boolean value.
     ///
     ///
     /// @param index buffer index.
     /// @param index buffer index.

+ 34 - 0
src/lib/dhcp/tests/option_custom_unittest.cc

@@ -789,6 +789,40 @@ TEST_F(OptionCustomTest, recordDataTruncated) {
 }
 }
 
 
 // The purpose of this test is to verify that an option comprising
 // 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
 // single boolean data field can be created and that its default
 // value can be overriden by a new value.
 // value can be overriden by a new value.
 TEST_F(OptionCustomTest, setBooleanData) {
 TEST_F(OptionCustomTest, setBooleanData) {