Commit 59903362 authored by Marcin Siodelski's avatar Marcin Siodelski

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

parent 1dc0f087
......@@ -101,7 +101,7 @@ OptionCustom::createBuffers() {
if (data_size == 0 &&
*field == OPT_FQDN_TYPE) {
OptionDataTypeUtil::writeFqdn(".", buf);
OptionDataTypeUtil::writeFqdn(".", buf);
} else {
buf.resize(data_size);
}
......@@ -114,8 +114,9 @@ OptionCustom::createBuffers() {
if (data_size == 0 &&
data_type == OPT_FQDN_TYPE) {
OptionDataTypeUtil::writeFqdn(".", buf);
} else {
buf.resize(data_size);
}
buf.resize(data_size);
buffers.push_back(buf);
}
std::swap(buffers, buffers_);
......@@ -414,12 +415,16 @@ OptionCustom::writeBoolean(const bool value, const uint32_t index) {
std::string
OptionCustom::readFqdn(const uint32_t index) const {
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
......
......@@ -151,6 +151,14 @@ public:
/// @return string representation if FQDN.
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.
///
/// @param index buffer index.
......
......@@ -216,7 +216,7 @@ OptionDataTypeUtil::readFqdn(const std::vector<uint8_t>& buf,
// If buffer is empty emit an error.
if (buf.empty()) {
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
// isc::dns::Name object to get the FQDN. This is not the most
......
......@@ -944,12 +944,36 @@ TEST_F(OptionCustomTest, setStringData) {
// By default the string data field is empty.
EXPECT_TRUE(value.empty());
// 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.
EXPECT_NO_THROW(value = option->readString());
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
// DHCPv4 custom option works correctly.
TEST_F(OptionCustomTest, pack4) {
......
Markdown is supported
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