Browse Source

[2491] Added a function to write fqdn into a buffer.

Marcin Siodelski 12 years ago
parent
commit
5990336237

+ 13 - 8
src/lib/dhcp/option_custom.cc

@@ -101,7 +101,7 @@ OptionCustom::createBuffers() {
 
 
             if (data_size == 0 &&
             if (data_size == 0 &&
                 *field == OPT_FQDN_TYPE) {
                 *field == OPT_FQDN_TYPE) {
-                    OptionDataTypeUtil::writeFqdn(".", buf);
+                OptionDataTypeUtil::writeFqdn(".", buf);
             } else {
             } else {
                 buf.resize(data_size);
                 buf.resize(data_size);
             }
             }
@@ -114,8 +114,9 @@ OptionCustom::createBuffers() {
         if (data_size == 0 &&
         if (data_size == 0 &&
             data_type == OPT_FQDN_TYPE) {
             data_type == OPT_FQDN_TYPE) {
             OptionDataTypeUtil::writeFqdn(".", buf);
             OptionDataTypeUtil::writeFqdn(".", buf);
+        } else {
+            buf.resize(data_size);
         }
         }
-        buf.resize(data_size);
         buffers.push_back(buf);
         buffers.push_back(buf);
     }
     }
     std::swap(buffers, buffers_);
     std::swap(buffers, buffers_);
@@ -414,12 +415,16 @@ OptionCustom::writeBoolean(const bool value, const uint32_t index) {
 std::string
 std::string
 OptionCustom::readFqdn(const uint32_t index) const {
 OptionCustom::readFqdn(const uint32_t index) const {
     checkIndex(index);
     checkIndex(index);
-    try {
-        size_t len = 0;
-        return (OptionDataTypeUtil::readFqdn(buffers_[index], len));
-    } catch (const Exception& ex) {
-        isc_throw(BadDataTypeCast, ex.what());
-    }
+    size_t len = 0;
+    return (OptionDataTypeUtil::readFqdn(buffers_[index], len));
+}
+
+void
+OptionCustom::writeFqdn(const std::string& fqdn, const uint32_t index) {
+    checkIndex(index);
+
+    buffers_[index].clear();
+    OptionDataTypeUtil::writeFqdn(fqdn, buffers_[index]);
 }
 }
 
 
 std::string
 std::string

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

@@ -151,6 +151,14 @@ public:
     /// @return string representation if FQDN.
     /// @return string representation if FQDN.
     std::string readFqdn(const uint32_t index = 0) const;
     std::string readFqdn(const uint32_t index = 0) const;
 
 
+    /// @brief Write an FQDN into a buffer.
+    ///
+    /// @param fqdn text representation of FQDN.
+    /// @param index buffer index.
+    ///
+    /// @throw isc::OutOfRange if index is out of range.
+    void writeFqdn(const std::string& fqdn, const uint32_t index = 0);
+
     /// @brief Read a buffer as integer value.
     /// @brief Read a buffer as integer value.
     ///
     ///
     /// @param index buffer index.
     /// @param index buffer index.

+ 1 - 1
src/lib/dhcp/option_data_types.cc

@@ -216,7 +216,7 @@ OptionDataTypeUtil::readFqdn(const std::vector<uint8_t>& buf,
     // If buffer is empty emit an error.
     // If buffer is empty emit an error.
     if (buf.empty()) {
     if (buf.empty()) {
         isc_throw(BadDataTypeCast, "unable to read FQDN from a buffer."
         isc_throw(BadDataTypeCast, "unable to read FQDN from a buffer."
-                  << " The buffer is empty");
+                  << " The buffer is empty.");
     }
     }
     // Copy the data from a buffer to InputBuffer so as we can use
     // Copy the data from a buffer to InputBuffer so as we can use
     // isc::dns::Name object to get the FQDN. This is not the most
     // isc::dns::Name object to get the FQDN. This is not the most

+ 25 - 1
src/lib/dhcp/tests/option_custom_unittest.cc

@@ -944,12 +944,36 @@ TEST_F(OptionCustomTest, setStringData) {
     // By default the string data field is empty.
     // By default the string data field is empty.
     EXPECT_TRUE(value.empty());
     EXPECT_TRUE(value.empty());
     // Write some text to this field.
     // Write some text to this field.
-    EXPECT_NO_THROW(option->writeString("hello world"));
+    ASSERT_NO_THROW(option->writeString("hello world"));
     // Check that it has been actually written.
     // Check that it has been actually written.
     EXPECT_NO_THROW(value = option->readString());
     EXPECT_NO_THROW(value = option->readString());
     EXPECT_EQ("hello world", value);
     EXPECT_EQ("hello world", value);
 }
 }
 
 
+/// The purpose of this test is to verify that an option comprising
+/// a default FQDN value can be created and that this value can be
+/// overriden after the option has been created.
+TEST_F(OptionCustomTest, setFqdnData) {
+    OptionDefinition opt_def("OPTION_FOO", 1000, "fqdn");
+
+    // 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);
+    // Read a default FQDN value from the option.
+    std::string fqdn;
+    ASSERT_NO_THROW(fqdn = option->readFqdn());
+    EXPECT_EQ(".", fqdn);
+    // Try override the default FQDN value.
+    ASSERT_NO_THROW(option->writeFqdn("example.com"));
+    // Check that the value has been actually overriden.
+    ASSERT_NO_THROW(fqdn = option->readFqdn());
+    EXPECT_EQ("example.com.", fqdn);
+}
+
 // The purpose of this test is to verify that pack function for
 // The purpose of this test is to verify that pack function for
 // DHCPv4 custom option works correctly.
 // DHCPv4 custom option works correctly.
 TEST_F(OptionCustomTest, pack4) {
 TEST_F(OptionCustomTest, pack4) {