Skip to content

Commit

Permalink
Dockerised connections tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alamaison committed Jan 30, 2016
1 parent b6f38da commit 3987fe9
Show file tree
Hide file tree
Showing 10 changed files with 541 additions and 591 deletions.
173 changes: 76 additions & 97 deletions swish/connection/running_session.cpp
@@ -1,46 +1,24 @@
/** // Copyright 2008, 2009, 2010, 2011, 2013, 2016 Alexander Lamaison
@file
Libssh2 SSH and SFTP session management
@if license
Copyright (C) 2008, 2009, 2010, 2011, 2013
Alexander Lamaison <awl03@doc.ic.ac.uk>


This program is free software; you can redistribute it and/or modify // 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 // it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or // the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. // (at your option) any later version.


This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. // GNU General Public License for more details.


You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License
with this program; if not, write to the Free Software Foundation, Inc., // along with this program. If not, see <http://www.gnu.org/licenses/>.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
In addition, as a special exception, the the copyright holders give you
permission to combine this program with free software programs or the
OpenSSL project's "OpenSSL" library (or with modified versions of it,
with unchanged license). You may copy and distribute such a system
following the terms of the GNU GPL for this program and the licenses
of the other code concerned. The GNU General Public License gives
permission to release a modified version without this exception; this
exception also makes it possible to release a modified version which
carries forward this exception.
@endif
*/


#include "running_session.hpp" #include "running_session.hpp"


#include "swish/remotelimits.h" #include "swish/remotelimits.h"
#include "swish/debug.hpp" // Debug macros #include "swish/debug.hpp" // Debug macros
#include "swish/port_conversion.hpp" // port_to_string #include "swish/port_conversion.hpp" // port_to_string
#include "swish/utils.hpp" // WideStringToUtf8String #include "swish/utils.hpp" // WideStringToUtf8String


#include <ssh/session.hpp> #include <ssh/session.hpp>
#include <ssh/filesystem.hpp> // sftp_filesystem #include <ssh/filesystem.hpp> // sftp_filesystem
Expand Down Expand Up @@ -70,76 +48,77 @@ using boost::system::error_code;
using std::string; using std::string;
using std::wstring; using std::wstring;


namespace swish
{
namespace connection
{


namespace swish { namespace
namespace connection { {


namespace { /**

* Connect a socket to the given port on the given host.
/** *
* Connect a socket to the given port on the given host. * @throws A boost::system::system_error if there is a failure.
* */
* @throws A boost::system::system_error if there is a failure. void connect_socket_to_host(tcp::socket& socket, const wstring& host,
*/ unsigned int port, io_service& io)
void connect_socket_to_host( {
tcp::socket& socket, const wstring& host, unsigned int port, assert(!host.empty());
io_service& io) assert(host[0] != L'\0');
{
assert(!host.empty());
assert(host[0] != L'\0');

// Convert host address to a UTF-8 string
string host_name = WideStringToUtf8String(host);

tcp::resolver resolver(io);
typedef tcp::resolver::query Lookup;
Lookup query(host_name, port_to_string(port));

tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

error_code error = host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
BOOST_THROW_EXCEPTION(system_error(error));

assert(socket.is_open());
assert(socket.available() == 0);
}


// We have to have this weird function because Boost.ASIO doesn't // Convert host address to a UTF-8 string
// support Boost.Move rvalue-emulation. string host_name = WideStringToUtf8String(host);
//
// Ideally, connect_socket_to_host would return the connected sockets tcp::resolver resolver(io);
// so could be used in the running_session initialiser list below. And typedef tcp::resolver::query Lookup;
// the the initialiser for m_session would have a valid socket to use. Lookup query(host_name, port_to_string(port));
//
// But as we can't return the valid socket, we have to connect it *during* tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// the m_session initialisation, *after* the m_socket initialisation. Yuk! tcp::resolver::iterator end;
ssh::session session_on_socket(
tcp::socket& socket, const wstring& host, unsigned int port, error_code error = host_not_found;
io_service& io, const string& disconnection_message) while (error && endpoint_iterator != end)
{ {
connect_socket_to_host(socket, host, port, io); socket.close();
return ssh::session(socket.native(), disconnection_message); socket.connect(*endpoint_iterator++, error);
} }
if (error)
BOOST_THROW_EXCEPTION(system_error(error));
}

// We have to have this weird function because Boost.ASIO doesn't
// support Boost.Move rvalue-emulation.
//
// Ideally, connect_socket_to_host would return the connected sockets
// so could be used in the running_session initialiser list below. And
// the the initialiser for m_session would have a valid socket to use.
//
// But as we can't return the valid socket, we have to connect it *during*
// the m_session initialisation, *after* the m_socket initialisation. Yuk!
ssh::session session_on_socket(tcp::socket& socket, const wstring& host,
unsigned int port, io_service& io,
const string& disconnection_message)
{
connect_socket_to_host(socket, host, port, io);
return ssh::session(socket.native(), disconnection_message);
}
} }


running_session::running_session(const wstring& host, unsigned int port) running_session::running_session(const wstring& host, unsigned int port)
: : m_io(new io_service(0)),
m_io(new io_service(0)), m_socket(new tcp::socket(*m_io)), m_socket(new tcp::socket(*m_io)),
m_session( m_session(session_on_socket(*m_socket, host, port, *m_io,
session_on_socket(*m_socket, host, port, *m_io, "Swish says goodbye.")) "Swish says goodbye."))
{} {
}


running_session::running_session(BOOST_RV_REF(running_session) other) running_session::running_session(BOOST_RV_REF(running_session) other)
: : m_io(move(other.m_io)),
m_io(move(other.m_io)), m_socket(move(other.m_socket)),
m_socket(move(other.m_socket)), m_session(move(other.m_session)) {} m_session(move(other.m_session))
{
}


running_session& running_session::operator=(BOOST_RV_REF(running_session) other) running_session& running_session::operator=(BOOST_RV_REF(running_session) other)
{ {
Expand Down Expand Up @@ -172,5 +151,5 @@ void swap(running_session& lhs, running_session& rhs)
boost::swap(lhs.m_socket, rhs.m_socket); boost::swap(lhs.m_socket, rhs.m_socket);
boost::swap(lhs.m_session, rhs.m_session); boost::swap(lhs.m_session, rhs.m_session);
} }

}
}} // namespace swish::connection } // namespace swish::connection
21 changes: 14 additions & 7 deletions test/connection/CMakeLists.txt
@@ -1,4 +1,4 @@
# Copyright (C) 2015 Alexander Lamaison <swish@lammy.co.uk> # Copyright (C) 2015, 2016 Alexander Lamaison
# #
# This program is free software: you can redistribute it and/or modify it under # 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 # the terms of the GNU General Public License as published by the Free Software
Expand All @@ -13,17 +13,24 @@
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>. # this program. If not, see <http://www.gnu.org/licenses/>.


set(SOURCES set(UNIT_TESTS
connection_spec_test.cpp)

set(INTEGRATION_TESTS
authenticated_session_test.cpp authenticated_session_test.cpp
connection_spec_test.cpp connection_spec_create_session_test.cpp
running_session_test.cpp running_session_test.cpp
session_manager_test.cpp session_manager_test.cpp
session_pool_test.cpp) session_pool_test.cpp)


swish_test_suite( swish_test_suite(
SUBJECT connection SUBJECT connection VARIANT unit
SOURCES ${SOURCES} SOURCES ${UNIT_TESTS}
LIBRARIES ${Boost_LIBRARIES} LIBRARIES ${Boost_LIBRARIES}
LABELS integration) LABELS unit)


swish_copy_fixture_files(test-connection) swish_test_suite(
SUBJECT connection VARIANT integration
SOURCES ${INTEGRATION_TESTS}
LIBRARIES ${Boost_LIBRARIES} openssh_fixture
LABELS integration)

0 comments on commit 3987fe9

Please sign in to comment.