|
@@ -18,6 +18,8 @@
|
|
|
#include <arpa/inet.h>
|
|
|
#include <gtest/gtest.h>
|
|
|
#include <util/buffer.h>
|
|
|
+#include <dhcp/dhcp4.h>
|
|
|
+#include <dhcp/dhcp6.h>
|
|
|
#include <dhcp/libdhcp++.h>
|
|
|
#include "config.h"
|
|
|
|
|
@@ -31,6 +33,19 @@ class LibDhcpTest : public ::testing::Test {
|
|
|
public:
|
|
|
LibDhcpTest() {
|
|
|
}
|
|
|
+
|
|
|
+ /// @brief Generic factory function to create any option.
|
|
|
+ ///
|
|
|
+ /// Generic factory function to create any option.
|
|
|
+ ///
|
|
|
+ /// @param u universe (V4 or V6)
|
|
|
+ /// @param type option-type
|
|
|
+ /// @param buf option-buffer
|
|
|
+ static OptionPtr genericOptionFactory(Option::Universe u, uint16_t type,
|
|
|
+ const OptionBuffer& buf) {
|
|
|
+ Option* option = new Option(u, type, buf);
|
|
|
+ return OptionPtr(option);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
static const uint8_t packed[] = {
|
|
@@ -41,6 +56,54 @@ static const uint8_t packed[] = {
|
|
|
1, 1, 0, 1, 114 // opt5 (5 bytes)
|
|
|
};
|
|
|
|
|
|
+TEST(LibDhcpTest, optionFactory) {
|
|
|
+ OptionBuffer buf;
|
|
|
+ // Factory functions for specific options must be registered before
|
|
|
+ // they can be used to create options instances. Otherwise exception
|
|
|
+ // is rised.
|
|
|
+ EXPECT_THROW(LibDHCP::optionFactory(Option::V4, DHO_SUBNET_MASK, buf),
|
|
|
+ isc::BadValue);
|
|
|
+
|
|
|
+ // Let's register some factory functions (two v4 and one v6 function).
|
|
|
+ // Registration may trigger exception if function for the specified
|
|
|
+ // option has been registered already.
|
|
|
+ ASSERT_NO_THROW(
|
|
|
+ LibDHCP::OptionFactoryRegister(Option::V4, DHO_SUBNET_MASK,
|
|
|
+ &LibDhcpTest::genericOptionFactory);
|
|
|
+ );
|
|
|
+ ASSERT_NO_THROW(
|
|
|
+ LibDHCP::OptionFactoryRegister(Option::V4, DHO_TIME_OFFSET,
|
|
|
+ &LibDhcpTest::genericOptionFactory);
|
|
|
+ );
|
|
|
+ ASSERT_NO_THROW(
|
|
|
+ LibDHCP::OptionFactoryRegister(Option::V6, D6O_CLIENTID,
|
|
|
+ &LibDhcpTest::genericOptionFactory);
|
|
|
+ );
|
|
|
+
|
|
|
+ // Invoke factory functions for all options (check if registration
|
|
|
+ // was successful).
|
|
|
+ OptionPtr opt_subnet_mask;
|
|
|
+ opt_subnet_mask = LibDHCP::optionFactory(Option::V4,
|
|
|
+ DHO_SUBNET_MASK,
|
|
|
+ buf);
|
|
|
+ // Check if non-NULL DHO_SUBNET_MASK option pointer has been returned.
|
|
|
+ EXPECT_TRUE(opt_subnet_mask);
|
|
|
+
|
|
|
+ OptionPtr opt_time_offset;
|
|
|
+ opt_time_offset = LibDHCP::optionFactory(Option::V4,
|
|
|
+ DHO_TIME_OFFSET,
|
|
|
+ buf);
|
|
|
+ // Check if non-NULL DHO_TIME_OFFSET option pointer has been returned.
|
|
|
+ EXPECT_TRUE(opt_time_offset);
|
|
|
+
|
|
|
+ OptionPtr opt_clientid;
|
|
|
+ opt_clientid = LibDHCP::optionFactory(Option::V6,
|
|
|
+ D6O_CLIENTID,
|
|
|
+ buf);
|
|
|
+ // Check if non-NULL D6O_CLIENTID option pointer has been returned.
|
|
|
+ EXPECT_TRUE(opt_clientid);
|
|
|
+}
|
|
|
+
|
|
|
TEST(LibDhcpTest, packOptions6) {
|
|
|
OptionBuffer buf(512);
|
|
|
isc::dhcp::Option::OptionCollection opts; // list of options
|