]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#451,!227] Added unit tests for createBinding/createTriplet methods.
authorMarcin Siodelski <marcin@isc.org>
Fri, 8 Feb 2019 11:58:27 +0000 (12:58 +0100)
committerMarcin Siodelski <marcin@isc.org>
Fri, 8 Feb 2019 13:25:07 +0000 (08:25 -0500)
As a result of a review.

src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
src/hooks/dhcp/mysql_cb/mysql_cb_impl.h
src/hooks/dhcp/mysql_cb/tests/Makefile.am
src/hooks/dhcp/mysql_cb/tests/mysql_cb_impl_unittest.cc [new file with mode: 0644]

index 2f3834b3d7cd82a66cca99b4927ba322bdf12063..39a2d719307876cacdace6e80807bfdef748a7e2 100644 (file)
@@ -87,7 +87,7 @@ MySqlConfigBackendImpl::~MySqlConfigBackendImpl() {
 }
 
 MySqlBindingPtr
-MySqlConfigBackendImpl::createBinding(const Triplet<uint32_t>& triplet) const {
+MySqlConfigBackendImpl::createBinding(const Triplet<uint32_t>& triplet) {
     if (triplet.unspecified()) {
         return (MySqlBinding::createNull());
     }
@@ -95,7 +95,7 @@ MySqlConfigBackendImpl::createBinding(const Triplet<uint32_t>& triplet) const {
 }
 
 Triplet<uint32_t>
-MySqlConfigBackendImpl::createTriplet(const MySqlBindingPtr& binding) const {
+MySqlConfigBackendImpl::createTriplet(const MySqlBindingPtr& binding) {
     if (!binding) {
         isc_throw(Unexpected, "MySQL configuration backend internal error: "
                   "binding pointer is NULL when creating a triplet value");
index 0d1171de3c5ea7b9d98562b8a48c4e0d30f2fc5a..f947251f69b168665d51c5f7af7fe5a23cc7c907 100644 (file)
@@ -112,7 +112,7 @@ public:
     /// @return Pointer to a null binding if the triplet is "unspecified" or
     /// a pointer to a binding representing 32-bit unsigned integer value
     /// otherwise.
-    db::MySqlBindingPtr createBinding(const Triplet<uint32_t>& triplet) const;
+    static db::MySqlBindingPtr createBinding(const Triplet<uint32_t>& triplet);
 
     /// @brief Creates @c Triplet object from MySQL binding.
     ///
@@ -123,7 +123,7 @@ public:
     /// @throw isc::Unexpected if the provided binding pointer is NULL.
     /// @throw isc::InvalidOperation if the binding does not represent
     /// a 32-bit unsigned integer.
-    Triplet<uint32_t> createTriplet(const db::MySqlBindingPtr& binding) const;
+    static Triplet<uint32_t> createTriplet(const db::MySqlBindingPtr& binding);
 
     /// @brief Returns server tags associated with the particular selector.
     ///
index 6ba01c6ecab5f67b80468c52f3138c67903da722..251575aeec7ce3f29048ba03f3d4d5c7e9fdd990 100644 (file)
@@ -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)
diff --git a/src/hooks/dhcp/mysql_cb/tests/mysql_cb_impl_unittest.cc b/src/hooks/dhcp/mysql_cb/tests/mysql_cb_impl_unittest.cc
new file mode 100644 (file)
index 0000000..b2d76be
--- /dev/null
@@ -0,0 +1,61 @@
+// 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);
+}
+
+
+} 
+