diff --git a/castor/tape/reactor/CMakeLists.txt b/castor/tape/reactor/CMakeLists.txt index de21e40253e806b57f32b8aefedf0329705a08c8..3acaa31e9caed8687668e9b54cca078e612710bd 100644 --- a/castor/tape/reactor/CMakeLists.txt +++ b/castor/tape/reactor/CMakeLists.txt @@ -23,6 +23,7 @@ cmake_minimum_required (VERSION 2.6) # Rules to build the reactor code that is common to both rmcd and tapeserverd ################################################################################ set (REACTOR_SRC_FILES + DummyZMQReactor.cpp PollEventHandler.cpp PollReactor.cpp PollReactorImpl.cpp diff --git a/castor/tape/reactor/DummyZMQReactor.cpp b/castor/tape/reactor/DummyZMQReactor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f5f7672744a35b6d21896c27ec4d6c61431c0fb --- /dev/null +++ b/castor/tape/reactor/DummyZMQReactor.cpp @@ -0,0 +1,51 @@ +/****************************************************************************** + * + * This file is part of the Castor project. + * See http://castor.web.cern.ch/castor + * + * Copyright (C) 2003 CERN + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * @author Castor Dev team, castor-dev@cern.ch + *****************************************************************************/ + +#include "castor/tape/reactor/DummyZMQReactor.hpp" + +//------------------------------------------------------------------------------ +// constructor +//------------------------------------------------------------------------------ +castor::tape::reactor::DummyZMQReactor::DummyZMQReactor(log::Logger& log) throw(): + ZMQReactor(log) { +} + +//------------------------------------------------------------------------------ +// clear +//------------------------------------------------------------------------------ +void castor::tape::reactor::DummyZMQReactor::clear() { + // Do nothing +} + +//------------------------------------------------------------------------------ +// registerHandler +//------------------------------------------------------------------------------ +void castor::tape::reactor::DummyZMQReactor::registerHandler( + ZMQPollEventHandler *const handler) { + // Do nothing +} + +//------------------------------------------------------------------------------ +// handleEvents +//------------------------------------------------------------------------------ +void castor::tape::reactor::DummyZMQReactor::handleEvents(const int timeout) { + // Do nothing +} diff --git a/castor/tape/reactor/DummyZMQReactor.hpp b/castor/tape/reactor/DummyZMQReactor.hpp new file mode 100644 index 0000000000000000000000000000000000000000..5cce3e5029dc70e1184744a0424816e38463929d --- /dev/null +++ b/castor/tape/reactor/DummyZMQReactor.hpp @@ -0,0 +1,72 @@ +/****************************************************************************** + * + * This file is part of the Castor project. + * See http://castor.web.cern.ch/castor + * + * Copyright (C) 2003 CERN + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * @author Castor Dev team, castor-dev@cern.ch + *****************************************************************************/ + +#pragma once + +#include "castor/tape/reactor/ZMQReactor.hpp" + +namespace castor { +namespace tape { +namespace reactor { + +/** + * This is a dummy ZmqReactor class that does nothing. The primary goal of + * this class is to facilitate unit testing. + */ +class DummyZMQReactor: public ZMQReactor { +public: + + /** + * Constructor. + * + * @param log Interface to the CASTOR logging system. + */ + DummyZMQReactor(log::Logger& log) throw(); + + /** + * Removes and deletes all of the event handlers registered with the reactor. + */ + void clear(); + + /** + * Registers the specified handler. + * + * Please note that the reactor takes ownership of the handler and will + * delete it as appropriate. + * + * @param handler The handler to be registered. Please note that the handler + * MUST be allocated on the heap because the reactor will own the handler + * and therefore delete it as needed. + */ + void registerHandler(ZMQPollEventHandler *const handler); + + /** + * Handles any pending events. + * + * @param timeout Timeout in milliseconds. + */ + void handleEvents(const int timeout); + +}; // class DummyZMQReactor + +} // namespace reactor +} // namespace tape +} // namespace castor diff --git a/castor/tape/reactor/ZMQReactor.hpp b/castor/tape/reactor/ZMQReactor.hpp index 377a8c5f593b4cc99ae2895bbc6a54e6725e2e98..1530419f7824e483c57e848dab6ec93321de5204 100644 --- a/castor/tape/reactor/ZMQReactor.hpp +++ b/castor/tape/reactor/ZMQReactor.hpp @@ -33,7 +33,7 @@ namespace tape { namespace reactor { /** - * This reactor wraps the poll() system call. + * This reactor wraps the zmq_poll() function. * * This class is part of an implementation of the Reactor architecture pattern * described in the following book: @@ -57,12 +57,12 @@ public: /** * Destructor. */ - ~ZMQReactor() throw(); + virtual ~ZMQReactor() throw(); /** * Removes and deletes all of the event handlers registered with the reactor. */ - void clear(); + virtual void clear(); /** * Registers the specified handler. @@ -74,14 +74,14 @@ public: * MUST be allocated on the heap because the reactor will own the handler * and therefore delete it as needed. */ - void registerHandler(ZMQPollEventHandler *const handler); + virtual void registerHandler(ZMQPollEventHandler *const handler); /** * Handles any pending events. * * @param timeout Timeout in milliseconds. */ - void handleEvents(const int timeout); + virtual void handleEvents(const int timeout); private: diff --git a/castor/tape/tapeserver/daemon/TestingVdqmConnectionHandler.hpp b/castor/tape/tapeserver/daemon/TestingVdqmConnectionHandler.hpp new file mode 100644 index 0000000000000000000000000000000000000000..875f8782a6458086d02f0a63a7627f5f161ea724 --- /dev/null +++ b/castor/tape/tapeserver/daemon/TestingVdqmConnectionHandler.hpp @@ -0,0 +1,76 @@ +/****************************************************************************** + * + * This file is part of the Castor project. + * See http://castor.web.cern.ch/castor + * + * Copyright (C) 2003 CERN + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * + * Interface to the CASTOR logging system + * + * @author Castor Dev team, castor-dev@cern.ch + *****************************************************************************/ + +#pragma once + +#include "castor/tape/tapeserver/daemon/VdqmConnectionHandler.hpp" + +namespace castor { +namespace tape { +namespace tapeserver { +namespace daemon { + +/** + * Class used to facilitate unit testing by making public one or more of the + * protected members of its super class. + */ +class TestingVdqmConnectionHandler: public VdqmConnectionHandler { +public: + + /** + * Constructor. + * + * @param fd The file descriptor of the connection with the vdqmd + * daemon. + * @param reactor The reactor with which this event handler is registered. + * @param log The object representing the API of the CASTOR logging system. + * @param driveCatalogue The catalogue of tape drives controlled by the tape + * server daemon. + * @param tapeDaemonConfig The CASTOR configuration parameters to be used by + * the tape daemon. + */ + TestingVdqmConnectionHandler( + const int fd, + reactor::ZMQReactor &reactor, + log::Logger &log, + Catalogue &driveCatalogue, + const TapeDaemonConfig &tapeDaemonConfig) throw(): + VdqmConnectionHandler( + fd, + reactor, + log, + driveCatalogue, + tapeDaemonConfig) { + } + + using VdqmConnectionHandler::connectionIsFromTrustedVdqmHost; + + using VdqmConnectionHandler::getPeerHostName; + +}; // class TestingVdqmConnectionHandler + +} // namespace daemon +} // namespace tapeserver +} // namespace tape +} // namespace castor diff --git a/castor/tape/tapeserver/daemon/VdqmConnectionHandler.hpp b/castor/tape/tapeserver/daemon/VdqmConnectionHandler.hpp index 2c14e95161e42003ae134bc556661de5e6d37cbb..b661e255f42670dccf8b23d3f6c70aa3a5969566 100644 --- a/castor/tape/tapeserver/daemon/VdqmConnectionHandler.hpp +++ b/castor/tape/tapeserver/daemon/VdqmConnectionHandler.hpp @@ -90,7 +90,7 @@ public: */ ~VdqmConnectionHandler() throw(); -private: +protected: /** * The file descriptor of the vdqm connection. diff --git a/castor/tape/tapeserver/daemon/VdqmConnectionHandlerTest.cpp b/castor/tape/tapeserver/daemon/VdqmConnectionHandlerTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72b04968fc870e618736e265a19ed1aada762f15 --- /dev/null +++ b/castor/tape/tapeserver/daemon/VdqmConnectionHandlerTest.cpp @@ -0,0 +1,125 @@ +/****************************************************************************** + * + * This file is part of the Castor project. + * See http://castor.web.cern.ch/castor + * + * Copyright (C) 2003 CERN + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * + * + * @author Castor Dev team, castor-dev@cern.ch + *****************************************************************************/ + +#include "castor/legacymsg/CupvProxyDummy.hpp" +#include "castor/legacymsg/VdqmProxyDummy.hpp" +#include "castor/legacymsg/VmgrProxyDummy.hpp" +#include "castor/log/DummyLogger.hpp" +#include "castor/tape/tapeserver/daemon/ProcessForkerProxyDummy.hpp" +#include "castor/tape/tapeserver/daemon/TestingVdqmConnectionHandler.hpp" +#include "castor/tape/reactor/DummyZMQReactor.hpp" +#include "castor/utils/SmartFd.hpp" + +#include <gtest/gtest.h> + +namespace unitTests { + +class castor_tape_tapeserver_daemon_VdqmConnectionHandlerTest: + public ::testing::Test { +protected: + + castor_tape_tapeserver_daemon_VdqmConnectionHandlerTest(): + m_log("unittests"), + m_reactor(m_log), + m_netTimeout(1), + m_isGrantedReturnValue(true), + m_cupv(m_isGrantedReturnValue), + m_catalogue( + m_netTimeout, + m_log, + m_processForker, + m_cupv, + m_vdqm, + m_vmgr, + "hostName", + m_catalogueConfig) { + } + + castor::log::DummyLogger m_log; + + castor::tape::reactor::DummyZMQReactor m_reactor; + + const int m_netTimeout; + + castor::tape::tapeserver::daemon::ProcessForkerProxyDummy m_processForker; + + const bool m_isGrantedReturnValue; + + castor::legacymsg::CupvProxyDummy m_cupv; + + castor::legacymsg::VdqmProxyDummy m_vdqm; + + castor::legacymsg::VmgrProxyDummy m_vmgr; + + castor::tape::tapeserver::daemon::CatalogueConfig m_catalogueConfig; + + castor::tape::tapeserver::daemon::Catalogue m_catalogue; + + castor::tape::tapeserver::daemon::TapeDaemonConfig m_config; + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(castor_tape_tapeserver_daemon_VdqmConnectionHandlerTest, getPeerHostName) { + using namespace castor::tape::tapeserver::daemon; + + int pair[2] = {-1, -1}; + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pair)); + castor::utils::SmartFd pairElement0(pair[0]); + castor::utils::SmartFd pairElement1(pair[1]); + + TestingVdqmConnectionHandler handler( + pairElement0.get(), + m_reactor, + m_log, + m_catalogue, + m_config); + + std::string peerHostName; + ASSERT_NO_THROW(peerHostName = handler.getPeerHostName(pairElement0.get())); + ASSERT_FALSE(peerHostName.empty()); +} + +TEST_F(castor_tape_tapeserver_daemon_VdqmConnectionHandlerTest, connectionIsFromTrustedVdqmHost) { + using namespace castor::tape::tapeserver::daemon; + + int pair[2] = {-1, -1}; + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pair)); + castor::utils::SmartFd pairElement0(pair[0]); + castor::utils::SmartFd pairElement1(pair[1]); + + TestingVdqmConnectionHandler handler( + pairElement0.get(), + m_reactor, + m_log, + m_catalogue, + m_config); + + ASSERT_NO_THROW(handler.connectionIsFromTrustedVdqmHost()); +} + +} // namespace unitTests diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5175c4497df44a2643a50db394a242ab2196aeb0..be827a2954dca0b3a3c7a3aca167e9daf8af76e5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -164,6 +164,7 @@ add_executable(castorUnitTests ../castor/tape/tapeserver/daemon/TapeDaemonTest.cpp ../castor/tape/tapeserver/daemon/VdqmAcceptHandler.cpp ../castor/tape/tapeserver/daemon/VdqmConnectionHandler.cpp + ../castor/tape/tapeserver/daemon/VdqmConnectionHandlerTest.cpp ../castor/tape/tapeserver/daemon/RecallTaskInjectorTest.cpp ../castor/tape/tapeserver/daemon/MigrationReportPackerTest.cpp ../castor/tape/tapeserver/daemon/RecallReportPackerTest.cpp