From: Francis Dupont Date: Thu, 15 Feb 2018 23:26:21 +0000 (+0100) Subject: [5532] Reported trac5531 changes X-Git-Tag: trac5533_base~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b0bbc56c6a8bde9612b9f6c22845ec5fcfe6486;p=thirdparty%2Fkea.git [5532] Reported trac5531 changes --- diff --git a/premium b/premium index 78aaaded3b..d970735ad4 160000 --- a/premium +++ b/premium @@ -1 +1 @@ -Subproject commit 78aaaded3b633f0060339e8427efadf368dfbbda +Subproject commit d970735ad4f02de4cb736dc47669061048610412 diff --git a/src/lib/dhcpsrv/tests/host_data_source_factory_unittest.cc b/src/lib/dhcpsrv/tests/host_data_source_factory_unittest.cc index 0ff2a34e2e..6d401b6d48 100644 --- a/src/lib/dhcpsrv/tests/host_data_source_factory_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_data_source_factory_unittest.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -17,122 +18,51 @@ using namespace std; using namespace isc; using namespace isc::dhcp; +using namespace isc::dhcp::test; namespace { -// @brief Names of test backends -enum Names { FOOBAR, FOO, BAR }; - -// @brief Convert a name to a string -string toString(Names name) { - switch (name) { - case FOOBAR: - return ("foobar"); - case FOO: - return ("foo"); - default: - return ("bar"); - } +// @brief Register memFactory +bool registerFactory() { + return (HostDataSourceFactory::registerFactory("mem", memFactory)); } -// A host data source -template -class FakeHostDataSource : public BaseHostDataSource { +// @brief Derive mem1 class +class Mem1HostDataSource : public MemHostDataSource { public: - // @brief Constructor - FakeHostDataSource(const DatabaseConnection::ParameterMap&) { } - - virtual ~FakeHostDataSource() { } - - // The base class is abstract so we need to define methods - - virtual ConstHostCollection - getAll(const HWAddrPtr&, const DuidPtr&) const { - return (ConstHostCollection()); - } - - virtual ConstHostCollection - getAll(const Host::IdentifierType&, const uint8_t*, const size_t) const { - return (ConstHostCollection()); - } - - virtual ConstHostCollection - getAll4(const asiolink::IOAddress&) const { - return (ConstHostCollection()); - } - - virtual ConstHostPtr - get4(const SubnetID&, const HWAddrPtr&, const DuidPtr&) const { - return (ConstHostPtr()); - } - - virtual ConstHostPtr - get4(const SubnetID&, const Host::IdentifierType&, - const uint8_t*, const size_t) const { - return (ConstHostPtr()); - } - - virtual ConstHostPtr - get4(const SubnetID&, const asiolink::IOAddress&) const { - return (ConstHostPtr()); - } - - virtual ConstHostPtr - get6(const SubnetID&, const DuidPtr&, const HWAddrPtr&) const { - return (ConstHostPtr()); - } - - virtual ConstHostPtr - get6(const SubnetID&, const Host::IdentifierType&, - const uint8_t*, const size_t) const { - return (ConstHostPtr()); - } - - virtual ConstHostPtr - get6(const asiolink::IOAddress&, const uint8_t) const { - return (ConstHostPtr()); - } - - virtual ConstHostPtr - get6(const SubnetID&, const asiolink::IOAddress&) const { - return (ConstHostPtr()); - } - - virtual bool add(const HostPtr&) { - return (false); - } - - virtual bool del(const SubnetID&, const asiolink::IOAddress&) { - return (false); + virtual string getType() const { + return ("mem1"); } +}; - virtual bool del4(const SubnetID&, const Host::IdentifierType&, - const uint8_t*, const size_t) { - return (false); - } +// @brief Factory of mem1 +BaseHostDataSource* +mem1Factory(const DatabaseConnection::ParameterMap&) { + return (new Mem1HostDataSource()); +} - virtual bool del6(const SubnetID&, const Host::IdentifierType&, - const uint8_t*, const size_t) { - return (false); - } +// @brief Register mem1Factory +bool registerFactory1() { + return (HostDataSourceFactory::registerFactory("mem1", mem1Factory)); +} +// @brief Derive mem2 class +class Mem2HostDataSource : public MemHostDataSource { +public: virtual string getType() const { - return (toString(NAME)); + return ("mem2"); } }; -// @brief Factory function template -template +// @brief Factory of mem2 BaseHostDataSource* -factory(const DatabaseConnection::ParameterMap& parameters) { - return (new FakeHostDataSource(parameters)); +mem2Factory(const DatabaseConnection::ParameterMap&) { + return (new Mem2HostDataSource()); } -// @brief Register factory template -template -bool registerFactory() { - return (HostDataSourceFactory::registerFactory(toString(NAME), - factory)); +// @brief Register mem2Factory +bool registerFactory2() { + return (HostDataSourceFactory::registerFactory("mem2", mem2Factory)); } // @brief Factory function returning 0 @@ -150,9 +80,9 @@ private: // @brief Cleans up after the test. virtual void TearDown() { sources_.clear(); - HostDataSourceFactory::deregisterFactory("foobar"); - HostDataSourceFactory::deregisterFactory("foo"); - HostDataSourceFactory::deregisterFactory("bar"); + HostDataSourceFactory::deregisterFactory("mem"); + HostDataSourceFactory::deregisterFactory("mem1"); + HostDataSourceFactory::deregisterFactory("mem2"); } public: HostDataSourceList sources_; @@ -160,89 +90,89 @@ public: // Verify a factory can be registered and only once. TEST_F(HostDataSourceFactoryTest, registerFactory) { - EXPECT_TRUE(registerFactory()); + EXPECT_TRUE(registerFactory()); // Only once - EXPECT_FALSE(registerFactory()); + EXPECT_FALSE(registerFactory()); } // Verify a factory can be registered and deregistered TEST_F(HostDataSourceFactoryTest, deregisterFactory) { // Does not exist at the beginning - EXPECT_FALSE(HostDataSourceFactory::deregisterFactory("foobar")); + EXPECT_FALSE(HostDataSourceFactory::deregisterFactory("mem")); // Register and deregister - EXPECT_TRUE(registerFactory()); - EXPECT_TRUE(HostDataSourceFactory::deregisterFactory("foobar")); + EXPECT_TRUE(registerFactory()); + EXPECT_TRUE(HostDataSourceFactory::deregisterFactory("mem")); // No longer exists - EXPECT_FALSE(HostDataSourceFactory::deregisterFactory("foobar")); + EXPECT_FALSE(HostDataSourceFactory::deregisterFactory("mem")); } // Verify a registered factory can be called TEST_F(HostDataSourceFactoryTest, add) { - EXPECT_TRUE(registerFactory()); - EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=foobar")); + EXPECT_TRUE(registerFactory()); + EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=mem")); ASSERT_EQ(1, sources_.size()); - EXPECT_EQ("foobar", sources_[0]->getType()); + EXPECT_EQ("mem", sources_[0]->getType()); } // Verify that type is required TEST_F(HostDataSourceFactoryTest, notype) { - EXPECT_THROW(HostDataSourceFactory::add(sources_, "tp=foobar"), + EXPECT_THROW(HostDataSourceFactory::add(sources_, "tp=mem"), InvalidParameter); - EXPECT_THROW(HostDataSourceFactory::add(sources_, "type=foobar"), + EXPECT_THROW(HostDataSourceFactory::add(sources_, "type=mem"), InvalidType); } // Verify that factory must not return NULL TEST_F(HostDataSourceFactoryTest, null) { - EXPECT_TRUE(HostDataSourceFactory::registerFactory("foobar", factory0)); - EXPECT_THROW(HostDataSourceFactory::add(sources_, "type=foobar"), + EXPECT_TRUE(HostDataSourceFactory::registerFactory("mem", factory0)); + EXPECT_THROW(HostDataSourceFactory::add(sources_, "type=mem"), Unexpected); } // Verify del class method TEST_F(HostDataSourceFactoryTest, del) { // No sources at the beginning - EXPECT_FALSE(HostDataSourceFactory::del(sources_, "foobar")); + EXPECT_FALSE(HostDataSourceFactory::del(sources_, "mem")); - // Add foobar - EXPECT_TRUE(registerFactory()); - EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=foobar")); + // Add mem + EXPECT_TRUE(registerFactory()); + EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=mem")); ASSERT_EQ(1, sources_.size()); // Delete another EXPECT_FALSE(HostDataSourceFactory::del(sources_, "another")); - // Delete foobar - EXPECT_TRUE(HostDataSourceFactory::del(sources_, "foobar")); + // Delete mem + EXPECT_TRUE(HostDataSourceFactory::del(sources_, "mem")); - // No longer foobar in sources - EXPECT_FALSE(HostDataSourceFactory::del(sources_, "foobar")); + // No longer mem in sources + EXPECT_FALSE(HostDataSourceFactory::del(sources_, "mem")); } // Verify add and del class method on multiple backends TEST_F(HostDataSourceFactoryTest, multiple) { // Add foo twice - EXPECT_TRUE(registerFactory()); - EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=foo")); - EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=foo")); + EXPECT_TRUE(registerFactory1()); + EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=mem1")); + EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=mem1")); - // Add bar once - EXPECT_TRUE(registerFactory()); - EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=bar")); + // Add mem2 once + EXPECT_TRUE(registerFactory2()); + EXPECT_NO_THROW(HostDataSourceFactory::add(sources_, "type=mem2")); // Delete them - EXPECT_TRUE(HostDataSourceFactory::del(sources_, "foo")); - EXPECT_TRUE(HostDataSourceFactory::del(sources_, "bar")); - // Second instance of foo - EXPECT_TRUE(HostDataSourceFactory::del(sources_, "foo")); + EXPECT_TRUE(HostDataSourceFactory::del(sources_, "mem1")); + EXPECT_TRUE(HostDataSourceFactory::del(sources_, "mem2")); + // Second instance of mem1 + EXPECT_TRUE(HostDataSourceFactory::del(sources_, "mem1")); // No more sources EXPECT_EQ(0, sources_.size()); - EXPECT_FALSE(HostDataSourceFactory::del(sources_, "foo")); - EXPECT_FALSE(HostDataSourceFactory::del(sources_, "bar")); + EXPECT_FALSE(HostDataSourceFactory::del(sources_, "mem1")); + EXPECT_FALSE(HostDataSourceFactory::del(sources_, "mem2")); } }; // end of anonymous namespace diff --git a/src/lib/dhcpsrv/testutils/Makefile.am b/src/lib/dhcpsrv/testutils/Makefile.am index de4b15a898..dfdaebd93b 100644 --- a/src/lib/dhcpsrv/testutils/Makefile.am +++ b/src/lib/dhcpsrv/testutils/Makefile.am @@ -14,6 +14,7 @@ noinst_LTLIBRARIES = libdhcpsrvtest.la libdhcpsrvtest_la_SOURCES = config_result_check.cc config_result_check.h libdhcpsrvtest_la_SOURCES += dhcp4o6_test_ipc.cc dhcp4o6_test_ipc.h +libdhcpsrvtest_la_SOURCES += memory_host_data_source.cc memory_host_data_source.h libdhcpsrvtest_la_SOURCES += schema.cc schema.h