Commit d7fa2817 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[2786] Implemented unit tests for OptionString.

parent 2f808b5a
......@@ -33,6 +33,7 @@ libb10_dhcp___la_SOURCES += option_custom.cc option_custom.h
libb10_dhcp___la_SOURCES += option_data_types.cc option_data_types.h
libb10_dhcp___la_SOURCES += option_definition.cc option_definition.h
libb10_dhcp___la_SOURCES += option_space.cc option_space.h
libb10_dhcp___la_SOURCES += option_string.cc option_string.h
libb10_dhcp___la_SOURCES += pkt6.cc pkt6.h
libb10_dhcp___la_SOURCES += pkt4.cc pkt4.h
libb10_dhcp___la_SOURCES += pkt_filter.h
......
// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <dhcp/option_string.h>
namespace isc {
namespace dhcp {
OptionString::OptionString(const Option::Universe u, const uint16_t type,
const std::string& value)
: Option(u, type), value_(value) {
}
OptionString::OptionString(const Option::Universe u, const uint16_t type,
OptionBufferConstIter begin,
OptionBufferConstIter end)
: Option(u, type) {
// Decode the data. This will throw exception if the buffer is
// truncated.
unpack(begin, end);
}
void
OptionString::pack(isc::util::OutputBuffer&/* buf*/) {
}
void
OptionString::unpack(OptionBufferConstIter/* begin */,
OptionBufferConstIter/* end*/) {
}
} // end of isc::dhcp namespace
} // end of isc namespace
// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#ifndef OPTION_STRING_H
#define OPTION_STRING_H
#include <dhcp/option.h>
#include <util/buffer.h>
#include <string>
namespace isc {
namespace dhcp {
/// @brief Class which represents an option carrying a single string value.
///
/// This class represents an option carrying a single string value.
/// Currently this class imposes that the minimal length of the carried
/// string is 1.
class OptionString : public Option {
public:
/// @brief Constructor, used to create options to be sent.
///
/// This constructor creates an instance of option which carries a
/// string value specified as constructor's parameter. This constructor
/// is most often used to create an instance of an option which will
/// be sent in the outgoing packet.
///
/// @param u universe (V4 or V6).
/// @param type option code.
/// @param value a string value to be carried by the option.
///
/// @throw isc::OutOfRange if provided string is empty.
OptionString(const Option::Universe u, const uint16_t type,
const std::string& value);
/// @brief Constructor, used for receiving options.
///
/// This constructor creates an instance of the option from the provided
/// chunk of buffer. This buffer may hold the data received on the wire.
///
/// @param u universe (V4 or V6).
/// @param type option code.
/// @param begin iterator pointing to the first byte of the buffer chunk.
/// @param end iterator pointing to the last byte of the buffer chunk.
///
/// @throw isc::OutOfRange if provided buffer is truncated.
OptionString(const Option::Universe u, const uint16_t type,
OptionBufferConstIter begin, OptionBufferConstIter end);
/// @brief Returns the string value held by the option.
///
/// @return string value held by the option.
std::string getValue() const {
return (value_);
}
/// @brief Sets the string value to be held by the option.
void setValue(const std::string& value) {
value_ = value;
}
/// @brief Creates on-wire format of the option.
///
/// This function creates on-wire format of the option and appends it to
/// the data existing in the provided buffer. The internal buffer's pointer
/// is moved to the end of stored data.
///
/// @param [out] buf output buffer where the option will be stored.
virtual void pack(isc::util::OutputBuffer& buf);
/// @brief Decodes option data from the provided buffer.
///
/// This function decodes option data from the provided buffer. Note that
/// it does not decode the option code and length, so the iterators must
/// point to the begining and end of the option payload respectively.
/// The size of the decoded payload must be at least 1 byte.
///
/// @param begin the iterator pointing to the option payload.
/// @param end the iterator pointing to the end of the option payload.
///
/// @throw isc::OutOfRange if provided buffer is truncated.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end);
private:
/// String value being held by the option.
std::string value_;
};
} // namespace isc::dhcp
} // namespace isc
#endif // OPTION_STRING_H
......@@ -84,7 +84,7 @@ const OptionDefParams OPTION_DEF_PARAMS4[] = {
{ "host-name", DHO_HOST_NAME, OPT_STRING_TYPE, false, NO_RECORD_DEF, "" },
{ "boot-size", DHO_BOOT_SIZE, OPT_UINT16_TYPE, false, NO_RECORD_DEF, "" },
{ "merit-dump", DHO_MERIT_DUMP, OPT_STRING_TYPE, false, NO_RECORD_DEF, "" },
{ "domain-name", DHO_DOMAIN_NAME, OPT_FQDN_TYPE, false, NO_RECORD_DEF, "" },
{ "domain-name", DHO_DOMAIN_NAME, OPT_STRING_TYPE, false, NO_RECORD_DEF, "" },
{ "swap-server", DHO_SWAP_SERVER, OPT_IPV4_ADDRESS_TYPE, false, NO_RECORD_DEF, "" },
{ "root-path", DHO_ROOT_PATH, OPT_STRING_TYPE, false, NO_RECORD_DEF, "" },
{ "extensions-path", DHO_EXTENSIONS_PATH, OPT_STRING_TYPE,
......
......@@ -41,6 +41,7 @@ libdhcp___unittests_SOURCES += option_definition_unittest.cc
libdhcp___unittests_SOURCES += option_custom_unittest.cc
libdhcp___unittests_SOURCES += option_unittest.cc
libdhcp___unittests_SOURCES += option_space_unittest.cc
libdhcp___unittests_SOURCES += option_string_unittest.cc
libdhcp___unittests_SOURCES += pkt4_unittest.cc
libdhcp___unittests_SOURCES += pkt6_unittest.cc
libdhcp___unittests_SOURCES += duid_unittest.cc
......
// Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <config.h>
#include <dhcp/option_string.h>
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
using namespace isc;
using namespace isc::dhcp;
using namespace isc::util;
namespace {
/// @brief OptionString test class.
class OptionStringTest : public ::testing::Test {
public:
/// @brief Constructor.
///
/// Initializes the test buffer with some data.
OptionStringTest() {
std::string test_string("This is a test string");
buf_.assign(test_string.begin(), test_string.end());
}
OptionBuffer buf_;
};
// This test verifies that the constructor which creates an option instance
// from a string value will create it properly.
TEST_F(OptionStringTest, constructorFromString) {
OptionString optv4(Option::V4, 123, "some option");
EXPECT_EQ(Option::V4, optv4.getUniverse());
EXPECT_EQ(123, optv4.getType());
EXPECT_EQ("some option", optv4.getValue());
// Do another test with the same constructor to make sure that
// different set of parameters would initialize the class members
// to different values.
OptionString optv6(Option::V6, 234, "other option");
EXPECT_EQ(Option::V6, optv6.getUniverse());
EXPECT_EQ(234, optv6.getType());
EXPECT_EQ("other option", optv6.getValue());
}
// This test verifies that the constructor which creates an option instance
// from a buffer, holding option payload, will create it properly.
// This function calls unpack() internally thus test test is considered
// to cover testing of unpack() functionality.
TEST_F(OptionStringTest, constructorFromBuffer) {
// Attempt to create an option using empty buffer should result in
// an exception.
EXPECT_THROW(
OptionString(Option::V4, 234, buf_.begin(), buf_.begin()),
isc::OutOfRange
);
// Declare option as a scoped pointer here so as its scope is
// function wide. The initialization (constructor invocation)
// is pushed to the ASSERT_NO_THROW macro below, as it may
// throw exception if buffer is truncated.
boost::scoped_ptr<OptionString> optv6;
ASSERT_NO_THROW(
optv6.reset(new OptionString(Option::V6, 123, buf_.begin(), buf_.end()));
);
// Make sure that it has been initialized to non-NULL value.
ASSERT_TRUE(optv6);
// Test the instance of the created option.
EXPECT_EQ(Option::V6, optv6->getUniverse());
EXPECT_EQ(123, optv6->getType());
EXPECT_EQ("This is a test string", optv6->getValue());
}
// This test verifies that the current option value can be overriden
// with new value, using setValue method.
TEST_F(OptionStringTest, setValue) {
// Create an instance of the option and set some initial value.
OptionString optv4(Option::V4, 123, "some option");
EXPECT_EQ("some option", optv4.getValue());
// Replace the value with the new one, and make sure it has
// been successful.
EXPECT_NO_THROW(optv4.setValue("new option value"));
EXPECT_EQ("new option value", optv4.getValue());
}
// This test verifies that the pack function encodes the option in
// a on-wire format properly.
TEST_F(OptionStringTest, pack) {
// Create an instance of the option.
std::string option_value("sample option value");
OptionString optv4(Option::V4, 123, option_value);
// Encode the option in on-wire format.
OutputBuffer buf(Option::OPTION4_HDR_LEN);
EXPECT_NO_THROW(optv4.pack(buf));
// Sanity check the length of the buffer.
ASSERT_EQ(Option::OPTION4_HDR_LEN + option_value.length(),
buf.getLength());
// Copy the contents of the OutputBuffer to InputBuffer because
// the latter has API to read data from it.
InputBuffer test_buf(buf.getData(), buf.getLength());
// First byte holds option code.
EXPECT_EQ(123, test_buf.readUint8());
// Second byte holds option length.
EXPECT_EQ(option_value.size(), test_buf.readUint8());
// Read the option data.
std::vector<uint8_t> data;
test_buf.readVector(data, test_buf.getLength() - test_buf.getPosition());
// And create a string from it.
std::string test_string(data.begin(), data.end());
// This string should be equal to the string used to create
// option's instance.
EXPECT_TRUE(option_value == test_string);
}
} // anonymous namespace
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