...
 
Commits (3)
......@@ -356,11 +356,11 @@ public:
std::string subnet_prefix = out_bindings[1]->getString();
auto prefix_pair = Subnet4::parsePrefix(subnet_prefix);
// renew_timer
uint32_t renew_timer = out_bindings[13]->getIntegerOrDefault<uint32_t>(0);
auto renew_timer = createTriplet(out_bindings[13]);
// rebind_timer
uint32_t rebind_timer = out_bindings[11]->getIntegerOrDefault<uint32_t>(0);
auto rebind_timer = createTriplet(out_bindings[11]);
// valid_lifetime
uint32_t valid_lifetime = out_bindings[19]->getIntegerOrDefault<uint32_t>(0);
auto valid_lifetime = createTriplet(out_bindings[19]);
// Create subnet with basic settings.
last_subnet.reset(new Subnet4(prefix_pair.first, prefix_pair.second,
......@@ -778,15 +778,15 @@ public:
MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(subnet->getMatchClientId())),
MySqlBinding::createTimestamp(subnet->getModificationTime()),
MySqlBinding::condCreateInteger<uint32_t>(subnet->getSiaddr().toUint32()),
MySqlBinding::createInteger<uint32_t>(subnet->getT2()),
createBinding(subnet->getT2()),
createInputRelayBinding(subnet),
MySqlBinding::createInteger<uint32_t>(subnet->getT1()),
createBinding(subnet->getT1()),
createInputRequiredClassesBinding(subnet),
MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(subnet->getHostReservationMode())),
MySqlBinding::condCreateString(subnet->getSname()),
shared_network_binding,
createInputContextBinding(subnet),
MySqlBinding::createInteger<uint32_t>(subnet->getValid())
createBinding(subnet->getValid())
};
MySqlTransaction transaction(conn_);
......@@ -1027,7 +1027,7 @@ public:
// rebind_timer
if (!out_bindings[6]->amNull()) {
last_network->setT2(out_bindings[6]->getInteger<uint32_t>());
last_network->setT2(createTriplet(out_bindings[6]));
}
// relay
......@@ -1048,7 +1048,7 @@ public:
// renew_timer
if (!out_bindings[8]->amNull()) {
last_network->setT1(out_bindings[8]->getInteger<uint32_t>());
last_network->setT1(createTriplet(out_bindings[8]));
}
// require_client_classes
......@@ -1080,7 +1080,7 @@ public:
// valid_lifetime
if (!out_bindings[12]->amNull()) {
last_network->setValid(out_bindings[12]->getInteger<uint32_t>());
last_network->setValid(createTriplet(out_bindings[12]));
}
shared_networks.push_back(last_network);
......@@ -1187,14 +1187,14 @@ public:
MySqlBinding::condCreateString(shared_network->getIface()),
MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>(shared_network->getMatchClientId())),
MySqlBinding::createTimestamp(shared_network->getModificationTime()),
MySqlBinding::condCreateInteger<uint32_t>(shared_network->getT2()),
createBinding(shared_network->getT2()),
createInputRelayBinding(shared_network),
MySqlBinding::condCreateInteger<uint32_t>(shared_network->getT1()),
createBinding(shared_network->getT1()),
createInputRequiredClassesBinding(shared_network),
MySqlBinding::createInteger<uint8_t>(static_cast<uint8_t>
(shared_network->getHostReservationMode())),
createInputContextBinding(shared_network),
MySqlBinding::condCreateInteger<uint32_t>(shared_network->getValid())
createBinding(shared_network->getValid())
};
MySqlTransaction transaction(conn_);
......
......@@ -86,6 +86,28 @@ MySqlConfigBackendImpl::~MySqlConfigBackendImpl() {
}
}
MySqlBindingPtr
MySqlConfigBackendImpl::createBinding(const Triplet<uint32_t>& triplet) {
if (triplet.unspecified()) {
return (MySqlBinding::createNull());
}
return (MySqlBinding::createInteger<uint32_t>(triplet.get()));
}
Triplet<uint32_t>
MySqlConfigBackendImpl::createTriplet(const MySqlBindingPtr& binding) {
if (!binding) {
isc_throw(Unexpected, "MySQL configuration backend internal error: "
"binding pointer is NULL when creating a triplet value");
}
if (binding->amNull()) {
return (Triplet<uint32_t>());
}
return (Triplet<uint32_t>(binding->getInteger<uint32_t>()));
}
void
MySqlConfigBackendImpl::createAuditRevision(const int index,
const ServerSelector& server_selector,
......
......@@ -106,6 +106,25 @@ public:
/// @brief Destructor.
~MySqlConfigBackendImpl();
/// @brief Creates MySQL binding from a @c Triplet.
///
/// @param triplet Triplet value from which the binding should be created.
/// @return Pointer to a null binding if the triplet is "unspecified" or
/// a pointer to a binding representing 32-bit unsigned integer value
/// otherwise.
static db::MySqlBindingPtr createBinding(const Triplet<uint32_t>& triplet);
/// @brief Creates @c Triplet object from MySQL binding.
///
/// @param binding Pointer to the MySQL binding.
/// @return Triplet value set to "unspecified" if the MySQL binding
/// represents a NULL value or a Triplet value encapsulating 32-bit
/// unsigned integer if the MySQL represents an integer.
/// @throw isc::Unexpected if the provided binding pointer is NULL.
/// @throw isc::InvalidOperation if the binding does not represent
/// a 32-bit unsigned integer.
static Triplet<uint32_t> createTriplet(const db::MySqlBindingPtr& binding);
/// @brief Returns server tags associated with the particular selector.
///
/// @param server_selector Server selector.
......
......@@ -2,7 +2,7 @@ SUBDIRS = .
AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
AM_CPPFLAGS += -I$(top_builddir)/src/hooks/dhcp/mysql_cb -I$(top_srcdir)/src/hooks/dhcp/mysql_cb
AM_CPPFLAGS += $(BOOST_INCLUDES)
AM_CPPFLAGS += $(BOOST_INCLUDES) $(MYSQL_CPPFLAGS)
AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\"
AM_CXXFLAGS = $(KEA_CXXFLAGS)
......@@ -25,6 +25,7 @@ TESTS += mysql_cb_unittests
mysql_cb_unittests_SOURCES = mysql_cb_dhcp4_unittest.cc
mysql_cb_unittests_SOURCES += mysql_cb_dhcp4_mgr_unittest.cc
mysql_cb_unittests_SOURCES += mysql_cb_impl_unittest.cc
mysql_cb_unittests_SOURCES += run_unittests.cc
mysql_cb_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES)
......
......@@ -152,6 +152,10 @@ public:
test_subnets_.push_back(subnet);
subnet.reset(new Subnet4(IOAddress("192.0.3.0"), 24, 20, 30, 40, 2048));
Triplet<uint32_t> null_timer;
subnet->setT1(null_timer);
subnet->setT2(null_timer);
subnet->setValid(null_timer);
test_subnets_.push_back(subnet);
subnet.reset(new Subnet4(IOAddress("192.0.4.0"), 24, 30, 40, 60, 4096));
......@@ -200,6 +204,10 @@ public:
// Add more shared networks.
shared_network.reset(new SharedNetwork4("level2"));
Triplet<uint32_t> null_timer;
shared_network->setT1(null_timer);
shared_network->setT2(null_timer);
shared_network->setValid(null_timer);
test_networks_.push_back(shared_network);
shared_network.reset(new SharedNetwork4("level3"));
......
// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <mysql_cb_impl.h>
#include <gtest/gtest.h>
using namespace isc::db;
using namespace isc::dhcp;
namespace {
// Test that the MySQL binding can be created from a triplet.
TEST(MySqlConfigBackendImplTest, createBindingFromTriplet) {
// Create a binding from an unspecified triplet.
auto binding = MySqlConfigBackendImpl::createBinding(Triplet<uint32_t>());
// The binding pointer should be non-null but the type of the binding
// should be null.
ASSERT_TRUE(binding);
EXPECT_TRUE(binding->amNull());
// This time create a triplet encapsulating a number.
binding = MySqlConfigBackendImpl::createBinding(Triplet<uint32_t>(123));
ASSERT_TRUE(binding);
// The binding type should be non-null.
ASSERT_FALSE(binding->amNull());
ASSERT_EQ(MYSQL_TYPE_LONG, binding->getType());
// Check that the correct value was stored in the binding.
EXPECT_EQ(123, binding->getInteger<uint32_t>());
}
// Test that the triplet can be created from the MySQL binding.
TEST(MySqlConfigBackendImplTest, createTripletFromBinding) {
// Create a binding encapsulating a number and try to create a triplet
// from this binding.
MySqlBindingPtr binding = MySqlBinding::createInteger<uint32_t>(234);
Triplet<uint32_t> triplet;
ASSERT_NO_THROW(triplet = MySqlConfigBackendImpl::createTriplet(binding));
// The triplet should be specified and equal to the value stored in the
// binding.
ASSERT_FALSE(triplet.unspecified());
EXPECT_EQ(234, triplet.get());
// Create a null binding and then use it to create a triplet.
binding = MySqlBinding::createNull();
ASSERT_NO_THROW(triplet = MySqlConfigBackendImpl::createTriplet(binding));
// This time the triplet should be unspecified.
EXPECT_TRUE(triplet.unspecified());
// Finally, make sure that the null binding pointer is rejected.
EXPECT_THROW(MySqlConfigBackendImpl::createTriplet(MySqlBindingPtr()),
isc::Unexpected);
}
}