From: Tomek Mrugalski Date: Wed, 9 Mar 2016 17:57:00 +0000 (+0100) Subject: [4297] New test library added. X-Git-Tag: trac4298_base~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=629dee19cf030342eaa6b071881653e8bf65c745;p=thirdparty%2Fkea.git [4297] New test library added. --- diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index 86890aa6d4..1541e3fc03 100755 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -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 index 0000000000..4e19ed103c --- /dev/null +++ b/src/lib/dhcpsrv/tests/callout_params_library.cc @@ -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 + +#include + +extern "C" { + +// Framework functions +int +version() { + return (KEA_HOOKS_VERSION); +} + +}; diff --git a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc index 31fde24a38..620c4ac69e 100644 --- a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc @@ -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 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 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 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 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 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 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 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 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()); - ctx.hooks_libraries_->push_back("library1"); + ctx.hooks_libraries_.reset(new std::vector()); + 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 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). diff --git a/src/lib/dhcpsrv/tests/test_libraries.h.in b/src/lib/dhcpsrv/tests/test_libraries.h.in index 67d9c819dd..82e514b5ef 100644 --- a/src/lib/dhcpsrv/tests/test_libraries.h.in +++ b/src/lib/dhcpsrv/tests/test_libraries.h.in @@ -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";