]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4297] New test library added.
authorTomek Mrugalski <tomasz@isc.org>
Wed, 9 Mar 2016 17:57:00 +0000 (18:57 +0100)
committerTomek Mrugalski <tomasz@isc.org>
Wed, 9 Mar 2016 17:57:00 +0000 (18:57 +0100)
src/lib/dhcpsrv/tests/Makefile.am
src/lib/dhcpsrv/tests/callout_params_library.cc [new file with mode: 0644]
src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc
src/lib/dhcpsrv/tests/test_libraries.h.in

index 86890aa6d43a0562631b8faecaf92c01c08811dc..1541e3fc036ab45111dda6b4485b5709363027a3 100755 (executable)
@@ -40,7 +40,7 @@ if HAVE_GTEST
 # to unexpected errors. For this reason, the --enable-static-link option is
 # ignored for unit tests built here.
 
-noinst_LTLIBRARIES = libco1.la libco2.la
+noinst_LTLIBRARIES = libco1.la libco2.la libco3.la
 
 # -rpath /nowhere is a hack to trigger libtool to not create a
 # convenience archive, resulting in shared modules
@@ -55,6 +55,11 @@ libco2_la_CXXFLAGS = $(AM_CXXFLAGS)
 libco2_la_CPPFLAGS = $(AM_CPPFLAGS)
 libco2_la_LDFLAGS = -avoid-version -export-dynamic -module -rpath /nowhere
 
+libco3_la_SOURCES  = callout_params_library.cc
+libco3_la_CXXFLAGS = $(AM_CXXFLAGS)
+libco3_la_CPPFLAGS = $(AM_CPPFLAGS)
+libco3_la_LDFLAGS = -avoid-version -export-dynamic -module -rpath /nowhere
+
 TESTS += libdhcpsrv_unittests
 
 libdhcpsrv_unittests_SOURCES  = run_unittests.cc
diff --git a/src/lib/dhcpsrv/tests/callout_params_library.cc b/src/lib/dhcpsrv/tests/callout_params_library.cc
new file mode 100644 (file)
index 0000000..4e19ed1
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright (C) 2016 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/.
+
+/// @file
+/// @brief Callout Library
+///
+/// This is the source of a test library for the DHCP parser tests that
+/// specify parameters. It will attempt to obtain its own parameters.
+
+#include <config.h>
+
+#include <hooks/hooks.h>
+
+extern "C" {
+
+// Framework functions
+int
+version() {
+    return (KEA_HOOKS_VERSION);
+}
+
+};
index 31fde24a38b4741d1be4ccb5ea27262dc4813f25..620c4ac69e1116f2f576bc39777be81dbe544a3a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2016 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
@@ -996,7 +996,7 @@ TEST_F(ParseConfigTest, noHooksLibraries) {
     ASSERT_TRUE(rcode == 0) << error_text_;
 
     // Check that the parser recorded nothing.
-    std::vector<std::string> libraries;
+    isc::hooks::HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_FALSE(changed);
@@ -1021,12 +1021,12 @@ TEST_F(ParseConfigTest, oneHooksLibrary) {
     ASSERT_TRUE(rcode == 0) << error_text_;
 
     // Check that the parser recorded a single library.
-    std::vector<std::string> libraries;
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_TRUE(changed);
     ASSERT_EQ(1, libraries.size());
-    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0]);
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
 
     // Check that the change was propagated to the hooks manager.
     hooks_libraries = HooksManager::getLibraryNames();
@@ -1049,13 +1049,13 @@ TEST_F(ParseConfigTest, twoHooksLibraries) {
     ASSERT_TRUE(rcode == 0) << error_text_;
 
     // Check that the parser recorded two libraries in the expected order.
-    std::vector<std::string> libraries;
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_TRUE(changed);
     ASSERT_EQ(2, libraries.size());
-    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0]);
-    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1]);
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
+    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1].first);
 
     // Verify that the change was propagated to the hooks manager.
     hooks_libraries = HooksManager::getLibraryNames();
@@ -1086,15 +1086,16 @@ TEST_F(ParseConfigTest, reconfigureSameHooksLibraries) {
     rcode = parseConfiguration(config);
     ASSERT_TRUE(rcode == 0) << error_text_;
 
-    // The list has not changed between the two parse operations and this is
-    // what we should see.
-    std::vector<std::string> libraries;
+    // The list has not changed between the two parse operations. However,
+    // the paramters (or the files they could point to) could have
+    // changed, so the libraries are reloaded anyway.
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
-    EXPECT_FALSE(changed);
+    EXPECT_TRUE(changed);
     ASSERT_EQ(2, libraries.size());
-    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0]);
-    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1]);
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
+    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1].first);
 
     // ... and check that the same two libraries are still loaded in the
     // HooksManager.
@@ -1129,13 +1130,13 @@ TEST_F(ParseConfigTest, reconfigureReverseHooksLibraries) {
     ASSERT_TRUE(rcode == 0) << error_text_;
 
     // The list has changed, and this is what we should see.
-    std::vector<std::string> libraries;
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_TRUE(changed);
     ASSERT_EQ(2, libraries.size());
-    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[0]);
-    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[1]);
+    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[0].first);
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[1].first);
 
     // ... and check that this was propagated to the HooksManager.
     hooks_libraries = HooksManager::getLibraryNames();
@@ -1168,7 +1169,7 @@ TEST_F(ParseConfigTest, reconfigureZeroHooksLibraries) {
     ASSERT_TRUE(rcode == 0) << error_text_;
 
     // The list has changed, and this is what we should see.
-    std::vector<std::string> libraries;
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_TRUE(changed);
@@ -1202,14 +1203,14 @@ TEST_F(ParseConfigTest, invalidHooksLibraries) {
 
     // Check that the parser recorded the names but, as they were in error,
     // does not flag them as changed.
-    vector<string> libraries;
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_FALSE(changed);
     ASSERT_EQ(3, libraries.size());
-    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0]);
-    EXPECT_EQ(NOT_PRESENT_LIBRARY, libraries[1]);
-    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[2]);
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
+    EXPECT_EQ(NOT_PRESENT_LIBRARY, libraries[1].first);
+    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[2].first);
 
     // ...and check it did not alter the libraries in the hooks manager.
     hooks_libraries = HooksManager::getLibraryNames();
@@ -1246,14 +1247,14 @@ TEST_F(ParseConfigTest, reconfigureInvalidHooksLibraries) {
 
     // Check that the parser recorded the names but, as the library set was
     // incorrect, did not mark the configuration as changed.
-    vector<string> libraries;
+    HookLibsCollection libraries;
     bool changed;
     hooks_libraries_parser_->getLibraries(libraries, changed);
     EXPECT_FALSE(changed);
     ASSERT_EQ(3, libraries.size());
-    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0]);
-    EXPECT_EQ(NOT_PRESENT_LIBRARY, libraries[1]);
-    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[2]);
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
+    EXPECT_EQ(NOT_PRESENT_LIBRARY, libraries[1].first);
+    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[2].first);
 
     // ... but check that the hooks manager was not updated with the incorrect
     // names.
@@ -1836,8 +1837,8 @@ public:
 
 
         // Allocate container for hooks libraries and add one library name.
-        ctx.hooks_libraries_.reset(new std::vector<std::string>());
-        ctx.hooks_libraries_->push_back("library1");
+        ctx.hooks_libraries_.reset(new std::vector<HookLibInfo>());
+        ctx.hooks_libraries_->push_back(make_pair("library1", ConstElementPtr()));
 
         // We will use ctx_new to assign another context to it or copy
         // construct.
@@ -1918,7 +1919,7 @@ public:
         ASSERT_TRUE(ctx_new->hooks_libraries_);
         {
             ASSERT_EQ(1, ctx_new->hooks_libraries_->size());
-            EXPECT_EQ("library1", (*ctx_new->hooks_libraries_)[0]);
+            EXPECT_EQ("library1", (*ctx_new->hooks_libraries_)[0].first);
         }
 
         // New context has the same universe.
@@ -2032,9 +2033,9 @@ public:
         // Change the list of libraries. this should not affect the list in the
         // new context.
         ctx.hooks_libraries_->clear();
-        ctx.hooks_libraries_->push_back("library2");
+        ctx.hooks_libraries_->push_back(make_pair("library2", ConstElementPtr()));
         ASSERT_EQ(1, ctx_new->hooks_libraries_->size());
-        EXPECT_EQ("library1", (*ctx_new->hooks_libraries_)[0]);
+        EXPECT_EQ("library1", (*ctx_new->hooks_libraries_)[0].first);
 
         // Change the universe. This should not affect the universe value in the
         // new context.
@@ -2155,6 +2156,72 @@ TEST_F(ParseConfigTest, validRelayInfo6) {
     EXPECT_THROW(parser->build(json_bogus2), DhcpConfigError);
 }
 
+// Check that some parameters may have configuration parameters configured.
+TEST_F(ParseConfigTest, HooksLibrariesParameters) {
+    // Check that no libraries are currently loaded
+    vector<string> hooks_libraries = HooksManager::getLibraryNames();
+    EXPECT_TRUE(hooks_libraries.empty());
+
+    // Configuration string.  This contains an invalid library which should
+    // trigger an error in the "build" stage.
+    const std::string config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1,
+                                                       CALLOUT_LIBRARY_2,
+                                                       CALLOUT_PARAMS_LIBRARY);
+
+    // Verify that the configuration fails to parse. (Syntactically it's OK,
+    // but the library is invalid).
+    const int rcode = parseConfiguration(config);
+    ASSERT_EQ(0, rcode);
+
+    // Check that the parser recorded the names.
+    HookLibsCollection libraries;
+    bool changed;
+    hooks_libraries_parser_->getLibraries(libraries, changed);
+    EXPECT_TRUE(changed);
+    ASSERT_EQ(3, libraries.size());
+    EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
+    EXPECT_EQ(CALLOUT_LIBRARY_2, libraries[1].first);
+    EXPECT_EQ(CALLOUT_PARAMS_LIBRARY, libraries[2].first);
+
+    // Also, check that the third library has its parameters specified.
+    // They were set by setHooksLibrariesConfig. The first has no
+    // no parameters, the second one has an empty map and the third
+    // one has actual parameters.
+    EXPECT_FALSE(libraries[0].second);
+    EXPECT_TRUE(libraries[1].second);
+    ASSERT_TRUE(libraries[2].second);
+
+    // Ok, get the partameter for the third library.
+    ConstElementPtr params = libraries[2].second;
+
+    // It must be a map.
+    ASSERT_EQ(Element::map, params->getType());
+
+    // This map should have 3 parameters:
+    // - svalue (and will expect its value to be "string value")
+    // - ivalue (and will expect its value to be 42)
+    // - bvalue (and will expect its value to be true)
+    ConstElementPtr svalue = params->get("svalue");
+    ConstElementPtr ivalue = params->get("ivalue");
+    ConstElementPtr bvalue = params->get("bvalue");
+
+    // There should be no extra parameters.
+    EXPECT_FALSE(params->get("nonexistent"));
+
+    ASSERT_TRUE(svalue);
+    ASSERT_TRUE(ivalue);
+    ASSERT_TRUE(bvalue);
+
+    ASSERT_EQ(Element::string, svalue->getType());
+    ASSERT_EQ(Element::integer, ivalue->getType());
+    ASSERT_EQ(Element::boolean, bvalue->getType());
+
+    EXPECT_EQ("string value", svalue->stringValue());
+    EXPECT_EQ(42, ivalue->intValue());
+    EXPECT_EQ(true, bvalue->boolValue());
+}
+
+
 // There's no test for ControlSocketParser, as it is tested in the DHCPv4 code
 // (see CtrlDhcpv4SrvTest.commandSocketBasic in
 // src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc).
index 67d9c819dd41aacd9496a4b2f3208c1dfe3f4ff6..82e514b5efd9a8464724e128865b67ed3ae8f1c5 100644 (file)
@@ -20,6 +20,12 @@ namespace {
 static const char* CALLOUT_LIBRARY_1 = "@abs_builddir@/.libs/libco1.so";
 static const char* CALLOUT_LIBRARY_2 = "@abs_builddir@/.libs/libco2.so";
 
+// This library will try to get the following parameters:
+// - svalue (and will expect its value to be "string value")
+// - ivalue (and will expect its value to be 42)
+// - bvalue (and will expect its value to be true)
+static const char* CALLOUT_PARAMS_LIBRARY = "@abs_builddir@/.libs/libco3.so";
+
 // Name of a library which is not present.
 static const char* NOT_PRESENT_LIBRARY = "@abs_builddir@/.libs/libnothere.so";