From: Michal 'vorner' Vaner Date: Thu, 22 Dec 2011 10:57:56 +0000 (+0100) Subject: [805] Extract the testing socket requestor X-Git-Tag: trac2351_base~304^2~16^2~27 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=33a396ecf7eddfa82054f4056d471ce9ff0616e4;p=thirdparty%2Fkea.git [805] Extract the testing socket requestor * It'll be needed in other tests as well, putting it to testutils. * Needs documentation. * A header only, to avoid linking order problems. --- diff --git a/src/lib/server_common/tests/portconfig_unittest.cc b/src/lib/server_common/tests/portconfig_unittest.cc index e6c45a6b18..7e3ec1f2a1 100644 --- a/src/lib/server_common/tests/portconfig_unittest.cc +++ b/src/lib/server_common/tests/portconfig_unittest.cc @@ -13,7 +13,7 @@ // PERFORMANCE OF THIS SOFTWARE. #include -#include +#include #include #include @@ -22,7 +22,6 @@ #include #include -#include using namespace isc::server_common::portconfig; using namespace isc::server_common; @@ -33,15 +32,6 @@ using namespace isc::asiolink; using namespace isc::asiodns; using boost::lexical_cast; -// Access the private hidden flag -namespace isc { -namespace server_common { -namespace portconfig { -extern bool test_mode; -} -} -} - namespace { /// Testcase for parseAddresses call (struct, nobody cares about private here) @@ -140,11 +130,10 @@ TEST_F(ParseAddresses, invalid) { } // Test fixture for installListenAddresses -struct InstallListenAddresses : public ::testing::Test, - public SocketRequestor { +struct InstallListenAddresses : public testutils::TestSocketRequestor { InstallListenAddresses() : - dnss_(ios_, NULL, NULL, NULL), - last_token_(0), break_rollback_(false) + // The members aren't initialized yet, but we need to store refs only + TestSocketRequestor(dnss_, store_), dnss_(ios_, NULL, NULL, NULL) { valid_.push_back(AddressPair("127.0.0.1", 5288)); valid_.push_back(AddressPair("::1", 5288)); @@ -158,13 +147,6 @@ struct InstallListenAddresses : public ::testing::Test, AddressList valid_; // But this shouldn't work AddressList invalid_; - // Store the tokens as they go in and out - vector released_tokens_; - vector given_tokens_; - // Last token number and fd given out - size_t last_token_; - // Should we break the rollback? - bool break_rollback_; // Check that the store_ addresses are the same as expected void checkAddresses(const AddressList& expected, const string& name) { SCOPED_TRACE(name); @@ -178,63 +160,6 @@ struct InstallListenAddresses : public ::testing::Test, EXPECT_EQ(ei->second, si->second); } } - void releaseSocket(const string& token) { - released_tokens_.push_back(token); - } - SocketID requestSocket(Protocol protocol, const string& address, - uint16_t port, ShareMode mode, const string& name) - { - if (address == "192.0.2.2") { - isc_throw(SocketError, "This address is not allowed"); - } - if (address == "::1" && break_rollback_) { - // This is valid address, but in case we need to break the - // rollback, it needs to be busy or whatever - // - // We break the second address to see the first one was - // allocated and then returned - isc_throw(SocketError, - "This address is available, but not for you"); - } - const string proto(protocol == TCP ? "TCP" : "UDP"); - size_t number = ++ last_token_; - EXPECT_EQ(5288, port); - EXPECT_EQ(DONT_SHARE, mode); - EXPECT_EQ("dummy_app", name); - const string token(proto + ":" + address + ":" + - lexical_cast(port) + ":" + - lexical_cast(number)); - given_tokens_.push_back(token); - return (SocketID(number, token)); - } - void SetUp() { - // Prepare the requestor (us) for the test - SocketRequestor::initTest(this); - // Don't manipulate the real sockets - test_mode = true; - } - void TearDown() { - // Make sure no sockets are left inside - AddressList list; - installListenAddresses(list, store_, dnss_); - // Don't leave invalid pointers here - SocketRequestor::initTest(NULL); - // And return the mode - test_mode = false; - } - // This checks the set of tokens is the same - void checkTokens(const char** expected, const vector& real, - const char* scope) - { - SCOPED_TRACE(scope); - size_t position(0); - while (expected[position]) { - ASSERT_LT(position, real.size()); - EXPECT_EQ(expected[position], real[position]) << position; - position ++; - } - EXPECT_EQ(position, real.size()); - } }; // Try switching valid addresses diff --git a/src/lib/testutils/Makefile.am b/src/lib/testutils/Makefile.am index a511d24be3..7a4c8d79bc 100644 --- a/src/lib/testutils/Makefile.am +++ b/src/lib/testutils/Makefile.am @@ -14,4 +14,4 @@ libtestutils_la_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) libtestutils_la_LIBADD = $(top_builddir)/src/lib/asiolink/libasiolink.la endif -EXTRA_DIST = portconfig.h +EXTRA_DIST = portconfig.h socket_request.h diff --git a/src/lib/testutils/socket_request.h b/src/lib/testutils/socket_request.h new file mode 100644 index 0000000000..aea5667f8d --- /dev/null +++ b/src/lib/testutils/socket_request.h @@ -0,0 +1,118 @@ +// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#include +#include + +#include + +#include +#include + +#include +#include + +namespace isc { +namespace server_common { +namespace portconfig { +// Access the private hidden flag +extern bool test_mode; +} +} + +namespace testutils { + +// TODO Docs + +class TestSocketRequestor : public isc::server_common::SocketRequestor, + public ::testing::Test { +protected: + TestSocketRequestor(asiodns::DNSService& dnss, + server_common::portconfig::AddressList& store) : + last_token_(0), break_rollback_(false), dnss_(dnss), store_(store) + {} + // Store the tokens as they go in and out + std::vector released_tokens_; + std::vector given_tokens_; + // Last token number and fd given out + size_t last_token_; + // Should we break the rollback? + bool break_rollback_; + void releaseSocket(const std::string& token) { + released_tokens_.push_back(token); + } + SocketID requestSocket(Protocol protocol, const std::string& address, + uint16_t port, ShareMode mode, + const std::string& name) + { + if (address == "192.0.2.2") { + isc_throw(SocketError, "This address is not allowed"); + } + if (address == "::1" && break_rollback_) { + // This is valid address, but in case we need to break the + // rollback, it needs to be busy or whatever + // + // We break the second address to see the first one was + // allocated and then returned + isc_throw(SocketError, + "This address is available, but not for you"); + } + const std::string proto(protocol == TCP ? "TCP" : "UDP"); + size_t number = ++ last_token_; + EXPECT_EQ(5288, port); + EXPECT_EQ(DONT_SHARE, mode); + EXPECT_EQ("dummy_app", name); + const std::string token(proto + ":" + address + ":" + + boost::lexical_cast(port) + ":" + + boost::lexical_cast(number)); + given_tokens_.push_back(token); + return (SocketID(number, token)); + } + void SetUp() { + // Prepare the requestor (us) for the test + SocketRequestor::initTest(this); + // Don't manipulate the real sockets + server_common::portconfig::test_mode = true; + } + void TearDown() { + // Make sure no sockets are left inside (if installListenAddresses + // wasn't used, this is NOP, so it won't hurt). + server_common::portconfig::AddressList list; + server_common::portconfig::installListenAddresses(list, store_, dnss_); + // Don't leave invalid pointers here + SocketRequestor::initTest(NULL); + // And return the mode + server_common::portconfig::test_mode = false; + } + // This checks the set of tokens is the same + void checkTokens(const char** expected, + const std::vector& real, + const char* scope) + { + SCOPED_TRACE(scope); + size_t position(0); + while (expected[position]) { + ASSERT_LT(position, real.size()); + EXPECT_EQ(expected[position], real[position]) << position; + position ++; + } + EXPECT_EQ(position, real.size()); + } +private: + asiodns::DNSService& dnss_; + server_common::portconfig::AddressList& store_; +}; + +} +}