Lesson 35 - Get Compute Auth Token Working

This commit is contained in:
Norman Lansing
2026-02-28 12:32:28 -05:00
parent 1d477ee42a
commit 4fde462bce
7743 changed files with 1397833 additions and 18 deletions

View File

@@ -0,0 +1,71 @@
if (OPENSSL_FOUND)
# Test transport integration
file (GLOB SOURCE integration.cpp)
init_target (test_transport)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport asio timers
file (GLOB SOURCE asio/timers.cpp)
init_target (test_transport_asio_timers)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport asio security
file (GLOB SOURCE asio/security.cpp)
init_target (test_transport_asio_security)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
endif()
# Test transport iostream base
file (GLOB SOURCE iostream/base.cpp)
init_target (test_transport_iostream_base)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport iostream endpoint
file (GLOB SOURCE iostream/endpoint.cpp)
init_target (test_transport_iostream_endpoint)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport iostream connection
file (GLOB SOURCE iostream/connection.cpp)
init_target (test_transport_iostream_connection)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport asio base
file (GLOB SOURCE asio/base.cpp)
init_target (test_transport_asio_base)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")

View File

@@ -0,0 +1,24 @@
## transport integration tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system','thread','random','chrono'],env) + [platform_libs] + [tls_libs]
objs = env.Object('boost_integration.o', ["integration.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_boost_integration', ["boost_integration.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
objs += env_cpp11.Object('stl_integration.o', ["integration.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_stl_integration', ["stl_integration.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,32 @@
## asio transport unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system','thread','chrono'],env) + [platform_libs] + [tls_libs]
objs = env.Object('base_boost.o', ["base.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('timers_boost.o', ["timers.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('security_boost.o', ["security.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_base_boost', ["base_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_timers_boost', ["timers_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_security_boost', ["security_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
objs += env_cpp11.Object('base_stl.o', ["base.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('timers_stl.o', ["timers.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('security_stl.o', ["security.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_base_stl', ["base_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_timers_stl', ["timers_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_security_stl', ["security_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_asio_base
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/transport/asio/base.hpp>
BOOST_AUTO_TEST_CASE( blank_error ) {
websocketpp::lib::error_code ec;
BOOST_CHECK( !ec );
}
BOOST_AUTO_TEST_CASE( asio_error ) {
using websocketpp::transport::asio::error::make_error_code;
using websocketpp::transport::asio::error::general;
websocketpp::lib::error_code ec = make_error_code(general);
BOOST_CHECK( ec == general );
BOOST_CHECK( ec.value() == 1 );
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2015, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_asio_base
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/common/type_traits.hpp>
#include <websocketpp/transport/asio/security/none.hpp>
#include <websocketpp/transport/asio/security/tls.hpp>
template <typename base>
struct dummy_con : public base {
websocketpp::lib::error_code test() {
return this->translate_ec(websocketpp::lib::asio::error_code());
}
};
BOOST_AUTO_TEST_CASE( translated_ec_none ) {
dummy_con<websocketpp::transport::asio::basic_socket::connection> tscon;
// If the current configuration settings result in the library error type and the asio
// error type being the same, then the code should pass through natively. Otherwise
// we should get a generic pass through error.
if(websocketpp::lib::is_same<websocketpp::lib::error_code,websocketpp::lib::asio::error_code>::value) {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::lib::error_code() );
} else {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::transport::error::make_error_code(websocketpp::transport::error::pass_through) );
}
}
BOOST_AUTO_TEST_CASE( translated_ec_tls ) {
dummy_con<websocketpp::transport::asio::tls_socket::connection> tscon;
// If the current configuration settings result in the library error type and the asio
// error type being the same, then the code should pass through natively. Otherwise
// we should get a generic pass through error.
if(websocketpp::lib::is_same<websocketpp::lib::error_code,websocketpp::lib::asio::error_code>::value) {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::lib::error_code() );
} else {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::transport::error::make_error_code(websocketpp::transport::error::pass_through) );
}
}

View File

@@ -0,0 +1,194 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_asio_timers
#include <boost/test/unit_test.hpp>
#include <exception>
#include <iostream>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/transport/asio/endpoint.hpp>
#include <websocketpp/transport/asio/security/tls.hpp>
// Concurrency
#include <websocketpp/concurrency/none.hpp>
// HTTP
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
// Loggers
#include <websocketpp/logger/stub.hpp>
//#include <websocketpp/logger/basic.hpp>
#include <boost/asio.hpp>
// Accept a connection, read data, and discard until EOF
void run_dummy_server(int port) {
using boost::asio::ip::tcp;
try {
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v6(), port));
tcp::socket socket(io_service);
acceptor.accept(socket);
for (;;) {
char data[512];
boost::system::error_code ec;
socket.read_some(boost::asio::buffer(data), ec);
if (ec == boost::asio::error::eof) {
break;
} else if (ec) {
// other error
throw ec;
}
}
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
} catch (boost::system::error_code & ec) {
std::cout << ec.message() << std::endl;
}
}
// Wait for the specified time period then fail the test
void run_test_timer(long value) {
boost::asio::io_service ios;
boost::asio::deadline_timer t(ios,boost::posix_time::milliseconds(value));
boost::system::error_code ec;
t.wait(ec);
BOOST_FAIL( "Test timed out" );
}
struct config {
typedef websocketpp::concurrency::none concurrency_type;
//typedef websocketpp::log::basic<concurrency_type,websocketpp::log::alevel> alog_type;
typedef websocketpp::log::stub alog_type;
typedef websocketpp::log::stub elog_type;
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::transport::asio::tls_socket::endpoint socket_type;
static const bool enable_multithreading = true;
static const long timeout_socket_pre_init = 1000;
static const long timeout_proxy = 1000;
static const long timeout_socket_post_init = 1000;
static const long timeout_dns_resolve = 1000;
static const long timeout_connect = 1000;
static const long timeout_socket_shutdown = 1000;
};
// Mock context that does no validation
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
context_ptr on_tls_init(websocketpp::connection_hdl) {
return context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
}
// Mock connection
struct mock_con: public websocketpp::transport::asio::connection<config> {
typedef websocketpp::transport::asio::connection<config> base;
mock_con(bool a, const websocketpp::lib::shared_ptr<config::alog_type>& b,
const websocketpp::lib::shared_ptr<config::elog_type>& c)
: base(a,b,c) {}
void start() {
base::init(websocketpp::lib::bind(&mock_con::handle_start,this,
websocketpp::lib::placeholders::_1));
}
void handle_start(const websocketpp::lib::error_code& ec) {
using websocketpp::transport::asio::socket::make_error_code;
using websocketpp::transport::asio::socket::error::tls_handshake_timeout;
BOOST_CHECK_EQUAL( ec, make_error_code(tls_handshake_timeout) );
base::cancel_socket();
}
};
typedef websocketpp::transport::asio::connection<config> con_type;
typedef websocketpp::lib::shared_ptr<mock_con> connection_ptr;
struct mock_endpoint : public websocketpp::transport::asio::endpoint<config> {
typedef websocketpp::transport::asio::endpoint<config> base;
mock_endpoint()
: alog(websocketpp::lib::make_shared<config::alog_type>())
, elog(websocketpp::lib::make_shared<config::elog_type>())
{
alog->set_channels(websocketpp::log::alevel::all);
base::init_logging(alog,elog);
init_asio();
}
void connect(std::string u) {
m_con.reset(new mock_con(false,alog,elog));
websocketpp::uri_ptr uri(new websocketpp::uri(u));
BOOST_CHECK( uri->get_valid() );
BOOST_CHECK_EQUAL( base::init(m_con), websocketpp::lib::error_code() );
base::async_connect(
m_con,
uri,
websocketpp::lib::bind(
&mock_endpoint::handle_connect,
this,
m_con,
websocketpp::lib::placeholders::_1
)
);
}
void handle_connect(connection_ptr con, websocketpp::lib::error_code const & ec)
{
BOOST_CHECK( !ec );
con->start();
}
connection_ptr m_con;
websocketpp::lib::shared_ptr<config::alog_type> alog;
websocketpp::lib::shared_ptr<config::elog_type> elog;
};
BOOST_AUTO_TEST_CASE( tls_handshake_timeout ) {
websocketpp::lib::thread dummy_server(websocketpp::lib::bind(&run_dummy_server,9005));
websocketpp::lib::thread timer(websocketpp::lib::bind(&run_test_timer,5000));
dummy_server.detach();
timer.detach();
sleep(1); // give the server thread some time to start
mock_endpoint endpoint;
endpoint.set_tls_init_handler(&on_tls_init);
endpoint.connect("wss://localhost:9005");
endpoint.run();
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_util
#include <boost/test/unit_test.hpp>
#include <iostream>
#include "../../src/processors/hybi_util.hpp"
#include "../../src/network_utilities.hpp"
BOOST_AUTO_TEST_CASE( circshift_0 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,0);
BOOST_CHECK( test == 0x0123456789abcdef);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,0);
BOOST_CHECK( test == 0x01234567);
}
}
BOOST_AUTO_TEST_CASE( circshift_1 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,1);
BOOST_CHECK( test == 0xef0123456789abcd);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,1);
BOOST_CHECK( test == 0x67012345);
}
}
BOOST_AUTO_TEST_CASE( circshift_2 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,2);
BOOST_CHECK( test == 0xcdef0123456789ab);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,2);
BOOST_CHECK( test == 0x45670123);
}
}
BOOST_AUTO_TEST_CASE( circshift_3 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,3);
BOOST_CHECK( test == 0xabcdef0123456789);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,3);
BOOST_CHECK( test == 0x23456701);
}
}

View File

@@ -0,0 +1,651 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_integration
#include <boost/test/unit_test.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/config/core.hpp>
#include <websocketpp/config/core_client.hpp>
#include <websocketpp/config/asio.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/config/debug_asio.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/client.hpp>
struct config : public websocketpp::config::asio_client {
typedef config type;
typedef websocketpp::config::asio base;
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
typedef base::response_type response_type;
typedef base::message_type message_type;
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef base::alog_type alog_type;
typedef base::elog_type elog_type;
typedef base::rng_type rng_type;
struct transport_config : public base::transport_config {
typedef type::concurrency_type concurrency_type;
typedef type::alog_type alog_type;
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::basic_socket::endpoint
socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
//static const websocketpp::log::level elog_level = websocketpp::log::elevel::all;
//static const websocketpp::log::level alog_level = websocketpp::log::alevel::all;
/// Length of time before an opening handshake is aborted
static const long timeout_open_handshake = 500;
/// Length of time before a closing handshake is aborted
static const long timeout_close_handshake = 500;
/// Length of time to wait for a pong after a ping
static const long timeout_pong = 500;
};
struct config_tls : public websocketpp::config::asio_tls_client {
typedef config type;
typedef websocketpp::config::asio base;
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
typedef base::response_type response_type;
typedef base::message_type message_type;
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef base::alog_type alog_type;
typedef base::elog_type elog_type;
typedef base::rng_type rng_type;
struct transport_config : public base::transport_config {
typedef type::concurrency_type concurrency_type;
typedef type::alog_type alog_type;
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::basic_socket::endpoint
socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
//static const websocketpp::log::level elog_level = websocketpp::log::elevel::all;
//static const websocketpp::log::level alog_level = websocketpp::log::alevel::all;
/// Length of time before an opening handshake is aborted
static const long timeout_open_handshake = 500;
/// Length of time before a closing handshake is aborted
static const long timeout_close_handshake = 500;
/// Length of time to wait for a pong after a ping
static const long timeout_pong = 500;
};
typedef websocketpp::server<config> server;
typedef websocketpp::client<config> client;
typedef websocketpp::server<config_tls> server_tls;
typedef websocketpp::client<config_tls> client_tls;
typedef websocketpp::server<websocketpp::config::core> iostream_server;
typedef websocketpp::client<websocketpp::config::core_client> iostream_client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
template <typename T>
void close_after_timeout(T & e, websocketpp::connection_hdl hdl, long timeout) {
sleep(timeout);
websocketpp::lib::error_code ec;
e.close(hdl,websocketpp::close::status::normal,"",ec);
BOOST_CHECK(!ec);
}
void run_server(server * s, int port, bool log = false) {
if (log) {
s->set_access_channels(websocketpp::log::alevel::all);
s->set_error_channels(websocketpp::log::elevel::all);
} else {
s->clear_access_channels(websocketpp::log::alevel::all);
s->clear_error_channels(websocketpp::log::elevel::all);
}
s->init_asio();
s->set_reuse_addr(true);
s->listen(port);
s->start_accept();
s->run();
}
void run_client(client & c, std::string uri, bool log = false) {
if (log) {
c.set_access_channels(websocketpp::log::alevel::all);
c.set_error_channels(websocketpp::log::elevel::all);
} else {
c.clear_access_channels(websocketpp::log::alevel::all);
c.clear_error_channels(websocketpp::log::elevel::all);
}
websocketpp::lib::error_code ec;
c.init_asio(ec);
c.set_reuse_addr(true);
BOOST_CHECK(!ec);
client::connection_ptr con = c.get_connection(uri,ec);
BOOST_CHECK( !ec );
c.connect(con);
c.run();
}
void run_client_and_mark(client * c, bool * flag, websocketpp::lib::mutex * mutex) {
c->run();
BOOST_CHECK( true );
websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(*mutex);
*flag = true;
BOOST_CHECK( true );
}
void run_time_limited_client(client & c, std::string uri, long timeout,
bool log)
{
if (log) {
c.set_access_channels(websocketpp::log::alevel::all);
c.set_error_channels(websocketpp::log::elevel::all);
} else {
c.clear_access_channels(websocketpp::log::alevel::all);
c.clear_error_channels(websocketpp::log::elevel::all);
}
c.init_asio();
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri,ec);
BOOST_CHECK( !ec );
c.connect(con);
websocketpp::lib::thread tthread(websocketpp::lib::bind(
&close_after_timeout<client>,
websocketpp::lib::ref(c),
con->get_handle(),
timeout
));
tthread.detach();
c.run();
}
void run_dummy_server(int port) {
using boost::asio::ip::tcp;
try {
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v6(), port));
tcp::socket socket(io_service);
acceptor.accept(socket);
for (;;) {
char data[512];
boost::system::error_code ec;
socket.read_some(boost::asio::buffer(data), ec);
if (ec == boost::asio::error::eof) {
break;
} else if (ec) {
// other error
throw ec;
}
}
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
} catch (boost::system::error_code & ec) {
std::cout << ec.message() << std::endl;
}
}
void run_dummy_client(std::string port) {
using boost::asio::ip::tcp;
try {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("localhost", port);
tcp::resolver::iterator iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, iterator);
for (;;) {
char data[512];
boost::system::error_code ec;
socket.read_some(boost::asio::buffer(data), ec);
if (ec == boost::asio::error::eof) {
break;
} else if (ec) {
// other error
throw ec;
}
}
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
} catch (boost::system::error_code & ec) {
std::cout << ec.message() << std::endl;
}
}
bool on_ping(server * s, websocketpp::connection_hdl, std::string) {
s->get_alog().write(websocketpp::log::alevel::app,"got ping");
return false;
}
void cancel_on_open(server * s, websocketpp::connection_hdl) {
s->stop_listening();
}
void stop_on_close(server * s, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
//BOOST_CHECK_EQUAL( con->get_local_close_code(), websocketpp::close::status::normal );
//BOOST_CHECK_EQUAL( con->get_remote_close_code(), websocketpp::close::status::normal );
s->stop();
}
template <typename T>
void ping_on_open(T * c, std::string payload, websocketpp::connection_hdl hdl) {
typename T::connection_ptr con = c->get_con_from_hdl(hdl);
websocketpp::lib::error_code ec;
con->ping(payload,ec);
BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
}
void fail_on_pong(websocketpp::connection_hdl, std::string) {
BOOST_FAIL( "expected no pong handler" );
}
void fail_on_pong_timeout(websocketpp::connection_hdl, std::string) {
BOOST_FAIL( "expected no pong timeout" );
}
void req_pong(std::string expected_payload, websocketpp::connection_hdl,
std::string payload)
{
BOOST_CHECK_EQUAL( expected_payload, payload );
}
void fail_on_open(websocketpp::connection_hdl) {
BOOST_FAIL( "expected no open handler" );
}
void delay(websocketpp::connection_hdl, long duration) {
sleep(duration);
}
template <typename T>
void check_ec(T * c, websocketpp::lib::error_code ec,
websocketpp::connection_hdl hdl)
{
typename T::connection_ptr con = c->get_con_from_hdl(hdl);
BOOST_CHECK_EQUAL( con->get_ec(), ec );
//BOOST_CHECK_EQUAL( con->get_local_close_code(), websocketpp::close::status::normal );
//BOOST_CHECK_EQUAL( con->get_remote_close_code(), websocketpp::close::status::normal );
}
template <typename T>
void check_ec_and_stop(T * e, websocketpp::lib::error_code ec,
websocketpp::connection_hdl hdl)
{
typename T::connection_ptr con = e->get_con_from_hdl(hdl);
BOOST_CHECK_EQUAL( con->get_ec(), ec );
//BOOST_CHECK_EQUAL( con->get_local_close_code(), websocketpp::close::status::normal );
//BOOST_CHECK_EQUAL( con->get_remote_close_code(), websocketpp::close::status::normal );
e->stop();
}
template <typename T>
void req_pong_timeout(T * c, std::string expected_payload,
websocketpp::connection_hdl hdl, std::string payload)
{
typename T::connection_ptr con = c->get_con_from_hdl(hdl);
BOOST_CHECK_EQUAL( payload, expected_payload );
con->close(websocketpp::close::status::normal,"");
}
template <typename T>
void close(T * e, websocketpp::connection_hdl hdl) {
e->get_con_from_hdl(hdl)->close(websocketpp::close::status::normal,"");
}
class test_deadline_timer
{
public:
test_deadline_timer(int seconds)
: m_timer(m_io_service, boost::posix_time::seconds(seconds))
{
m_timer.async_wait(bind(&test_deadline_timer::expired, this, ::_1));
std::size_t (boost::asio::io_service::*run)() = &boost::asio::io_service::run;
m_timer_thread = websocketpp::lib::thread(websocketpp::lib::bind(run, &m_io_service));
}
~test_deadline_timer()
{
m_timer.cancel();
m_timer_thread.join();
}
private:
void expired(const boost::system::error_code & ec)
{
if (ec == boost::asio::error::operation_aborted)
return;
BOOST_CHECK(!ec);
BOOST_FAIL("Test timed out");
}
boost::asio::io_service m_io_service;
boost::asio::deadline_timer m_timer;
websocketpp::lib::thread m_timer_thread;
};
BOOST_AUTO_TEST_CASE( pong_no_timeout ) {
server s;
client c;
s.set_close_handler(bind(&stop_on_close,&s,::_1));
// send a ping when the connection is open
c.set_open_handler(bind(&ping_on_open<client>,&c,"foo",::_1));
// require that a pong with matching payload is received
c.set_pong_handler(bind(&req_pong,"foo",::_1,::_2));
// require that a pong timeout is NOT received
c.set_pong_timeout_handler(bind(&fail_on_pong_timeout,::_1,::_2));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
sleep(1); // give the server thread some time to start
// Run a client that closes the connection after 1 seconds
run_time_limited_client(c, "http://localhost:9005", 1, false);
sthread.join();
}
BOOST_AUTO_TEST_CASE( pong_timeout ) {
server s;
client c;
s.set_ping_handler(bind(&on_ping, &s,::_1,::_2));
s.set_close_handler(bind(&stop_on_close,&s,::_1));
c.set_fail_handler(bind(&check_ec<client>,&c,
websocketpp::lib::error_code(),::_1));
c.set_pong_handler(bind(&fail_on_pong,::_1,::_2));
c.set_open_handler(bind(&ping_on_open<client>,&c,"foo",::_1));
c.set_pong_timeout_handler(bind(&req_pong_timeout<client>,&c,"foo",::_1,::_2));
c.set_close_handler(bind(&check_ec<client>,&c,
websocketpp::lib::error_code(),::_1));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
sleep(1); // give the server thread some time to start
test_deadline_timer deadline(10);
run_client(c, "http://localhost:9005",false);
sthread.join();
}
BOOST_AUTO_TEST_CASE( client_open_handshake_timeout ) {
client c;
// set open handler to fail test
c.set_open_handler(bind(&fail_on_open,::_1));
// set fail hander to test for the right fail error code
c.set_fail_handler(bind(&check_ec<client>,&c,
websocketpp::error::open_handshake_timeout,::_1));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_dummy_server,9005));
sthread.detach();
sleep(1); // give the server thread some time to start
test_deadline_timer deadline(10);
run_client(c, "http://localhost:9005");
}
BOOST_AUTO_TEST_CASE( server_open_handshake_timeout ) {
server s;
// set open handler to fail test
s.set_open_handler(bind(&fail_on_open,::_1));
// set fail hander to test for the right fail error code
s.set_fail_handler(bind(&check_ec_and_stop<server>,&s,
websocketpp::error::open_handshake_timeout,::_1));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
test_deadline_timer deadline(10);
sleep(1); // give the server thread some time to start
run_dummy_client("9005");
sthread.join();
}
BOOST_AUTO_TEST_CASE( client_self_initiated_close_handshake_timeout ) {
server s;
client c;
// on open server sleeps for longer than the timeout
// on open client sends close handshake
// client handshake timer should be triggered
s.set_open_handler(bind(&delay,::_1,1));
s.set_close_handler(bind(&stop_on_close,&s,::_1));
c.set_open_handler(bind(&close<client>,&c,::_1));
c.set_close_handler(bind(&check_ec<client>,&c,
websocketpp::error::close_handshake_timeout,::_1));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
test_deadline_timer deadline(10);
sleep(1); // give the server thread some time to start
run_client(c, "http://localhost:9005", false);
sthread.join();
}
BOOST_AUTO_TEST_CASE( client_peer_initiated_close_handshake_timeout ) {
// on open server sends close
// client should ack normally and then wait
// server leaves TCP connection open
// client handshake timer should be triggered
// TODO: how to make a mock server that leaves the TCP connection open?
}
BOOST_AUTO_TEST_CASE( server_self_initiated_close_handshake_timeout ) {
server s;
client c;
// on open server sends close
// on open client sleeps for longer than the timeout
// server handshake timer should be triggered
s.set_open_handler(bind(&close<server>,&s,::_1));
s.set_close_handler(bind(&check_ec_and_stop<server>,&s,
websocketpp::error::close_handshake_timeout,::_1));
c.set_open_handler(bind(&delay,::_1,1));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
test_deadline_timer deadline(10);
sleep(1); // give the server thread some time to start
run_client(c, "http://localhost:9005", false);
sthread.join();
}
BOOST_AUTO_TEST_CASE( client_runs_out_of_work ) {
client c;
test_deadline_timer deadline(3);
websocketpp::lib::error_code ec;
c.init_asio(ec);
BOOST_CHECK(!ec);
c.run();
// This test checks that an io_service with no work ends immediately.
BOOST_CHECK(true);
}
BOOST_AUTO_TEST_CASE( client_is_perpetual ) {
client c;
bool flag = false;
websocketpp::lib::mutex mutex;
websocketpp::lib::error_code ec;
c.init_asio(ec);
BOOST_CHECK(!ec);
c.start_perpetual();
websocketpp::lib::thread cthread(websocketpp::lib::bind(&run_client_and_mark,&c,&flag,&mutex));
sleep(1);
{
// Checks that the thread hasn't exited yet
websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(mutex);
BOOST_CHECK( !flag );
}
c.stop_perpetual();
sleep(1);
{
// Checks that the thread has exited
websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(mutex);
BOOST_CHECK( flag );
}
cthread.join();
}
BOOST_AUTO_TEST_CASE( client_failed_connection ) {
client c;
run_time_limited_client(c,"http://localhost:9005", 5, false);
}
BOOST_AUTO_TEST_CASE( stop_listening ) {
server s;
client c;
// the first connection stops the server from listening
s.set_open_handler(bind(&cancel_on_open,&s,::_1));
// client immediately closes after opening a connection
c.set_open_handler(bind(&close<client>,&c,::_1));
websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
test_deadline_timer deadline(5);
sleep(1); // give the server thread some time to start
run_client(c, "http://localhost:9005", false);
sthread.join();
}
BOOST_AUTO_TEST_CASE( pause_reading ) {
iostream_server s;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
char buffer[2] = { char(0x81), char(0x80) };
// suppress output (it needs a place to go to avoid error but we don't care what it is)
std::stringstream null_output;
s.register_ostream(&null_output);
iostream_server::connection_ptr con = s.get_connection();
con->start();
// read handshake, should work
BOOST_CHECK_EQUAL( con->read_some(handshake.data(), handshake.length()), handshake.length());
// pause reading and try again. The first read should work, the second should return 0
// the first read was queued already after the handshake so it will go through because
// reading wasn't paused when it was queued. The byte it reads wont be enough to
// complete the frame so another read will be requested. This one wont actually happen
// because the connection is paused now.
con->pause_reading();
BOOST_CHECK_EQUAL( con->read_some(buffer, 1), 1);
BOOST_CHECK_EQUAL( con->read_some(buffer+1, 1), 0);
// resume reading and try again. Should work this time because the resume should have
// re-queued a read.
con->resume_reading();
BOOST_CHECK_EQUAL( con->read_some(buffer+1, 1), 1);
}
BOOST_AUTO_TEST_CASE( server_connection_cleanup ) {
server_tls s;
}
#ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
BOOST_AUTO_TEST_CASE( move_construct_transport ) {
server s1;
server s2(std::move(s1));
}
#endif // _WEBSOCKETPP_MOVE_SEMANTICS_

View File

@@ -0,0 +1,31 @@
## iostream transport unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs]
objs = env.Object('iostream_base_boost.o', ["base.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('iostream_connection_boost.o', ["connection.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('iostream_endpoint_boost.o', ["endpoint.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_iostream_base_boost', ["iostream_base_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_iostream_connection_boost', ["iostream_connection_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_iostream_endpoint_boost', ["iostream_endpoint_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('iostream_base_stl.o', ["base.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('iostream_connection_stl.o', ["connection.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('iostream_endpoint_stl.o', ["endpoint.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_iostream_base_stl', ["iostream_base_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_iostream_connection_stl', ["iostream_connection_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_iostream_endpoint_stl', ["iostream_endpoint_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_iostream_base
#include <boost/test/unit_test.hpp>
#include <iostream>
BOOST_AUTO_TEST_CASE( placeholder ) {}

View File

@@ -0,0 +1,619 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_iostream_connection
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <cstring>
#include <string>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/error.hpp>
#include <websocketpp/transport/iostream/connection.hpp>
// Policies
#include <websocketpp/concurrency/basic.hpp>
#include <websocketpp/logger/basic.hpp>
struct config {
typedef websocketpp::concurrency::basic concurrency_type;
typedef websocketpp::log::basic<concurrency_type,
websocketpp::log::elevel> elog_type;
typedef websocketpp::log::basic<concurrency_type,
websocketpp::log::alevel> alog_type;
};
typedef websocketpp::transport::iostream::connection<config> iostream_con;
using websocketpp::transport::iostream::error::make_error_code;
struct stub_con : public iostream_con {
typedef stub_con type;
typedef websocketpp::lib::shared_ptr<type> ptr;
typedef iostream_con::timer_ptr timer_ptr;
stub_con(bool is_server, const websocketpp::lib::shared_ptr<config::alog_type>& a, const websocketpp::lib::shared_ptr<config::elog_type>& e)
: iostream_con(is_server,a,e)
// Set the error to a known code that is unused by the library
// This way we can easily confirm that the handler was run at all.
, ec(websocketpp::error::make_error_code(websocketpp::error::test))
, indef_read_total(0)
{}
/// Get a shared pointer to this component
ptr get_shared() {
return websocketpp::lib::static_pointer_cast<type>(iostream_con::get_shared());
}
void write(std::string msg) {
iostream_con::async_write(
msg.data(),
msg.size(),
websocketpp::lib::bind(
&stub_con::handle_op,
type::get_shared(),
websocketpp::lib::placeholders::_1
)
);
}
void write(std::vector<websocketpp::transport::buffer> & bufs) {
iostream_con::async_write(
bufs,
websocketpp::lib::bind(
&stub_con::handle_op,
type::get_shared(),
websocketpp::lib::placeholders::_1
)
);
}
void async_read_at_least(size_t num_bytes, char *buf, size_t len)
{
iostream_con::async_read_at_least(
num_bytes,
buf,
len,
websocketpp::lib::bind(
&stub_con::handle_op,
type::get_shared(),
websocketpp::lib::placeholders::_1
)
);
}
void handle_op(websocketpp::lib::error_code const & e) {
ec = e;
}
void async_read_indef(size_t num_bytes, char *buf, size_t len)
{
indef_read_size = num_bytes;
indef_read_buf = buf;
indef_read_len = len;
indef_read();
}
void indef_read() {
iostream_con::async_read_at_least(
indef_read_size,
indef_read_buf,
indef_read_len,
websocketpp::lib::bind(
&stub_con::handle_indef,
type::get_shared(),
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2
)
);
}
void handle_indef(websocketpp::lib::error_code const & e, size_t amt_read) {
ec = e;
indef_read_total += amt_read;
indef_read();
}
void shutdown() {
iostream_con::async_shutdown(
websocketpp::lib::bind(
&stub_con::handle_async_shutdown,
type::get_shared(),
websocketpp::lib::placeholders::_1
)
);
}
void handle_async_shutdown(websocketpp::lib::error_code const & e) {
ec = e;
}
websocketpp::lib::error_code ec;
size_t indef_read_size;
char * indef_read_buf;
size_t indef_read_len;
size_t indef_read_total;
};
// Stubs
websocketpp::lib::shared_ptr<config::alog_type> alogger = websocketpp::lib::make_shared<config::alog_type>();
websocketpp::lib::shared_ptr<config::elog_type> elogger = websocketpp::lib::make_shared<config::elog_type>();
BOOST_AUTO_TEST_CASE( const_methods ) {
iostream_con::ptr con(new iostream_con(true,alogger,elogger));
BOOST_CHECK( !con->is_secure() );
BOOST_CHECK_EQUAL( con->get_remote_endpoint(), "iostream transport" );
}
BOOST_AUTO_TEST_CASE( write_before_output_method_set ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->write("foo");
BOOST_CHECK( con->ec == make_error_code(websocketpp::transport::iostream::error::output_stream_required) );
std::vector<websocketpp::transport::buffer> bufs;
con->write(bufs);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::iostream::error::output_stream_required) );
}
BOOST_AUTO_TEST_CASE( async_write_ostream ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
std::stringstream output;
con->register_ostream(&output);
con->write("foo");
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output.str(), "foo" );
}
websocketpp::lib::error_code write_handler(std::string & o, websocketpp::connection_hdl, char const * buf, size_t len) {
o += std::string(buf,len);
return websocketpp::lib::error_code();
}
websocketpp::lib::error_code vector_write_handler(std::string & o, websocketpp::connection_hdl, std::vector<websocketpp::transport::buffer> const & bufs) {
std::vector<websocketpp::transport::buffer>::const_iterator it;
for (it = bufs.begin(); it != bufs.end(); it++) {
o += std::string((*it).buf, (*it).len);
}
return websocketpp::lib::error_code();
}
websocketpp::lib::error_code write_handler_error(websocketpp::connection_hdl, char const *, size_t) {
return make_error_code(websocketpp::transport::error::general);
}
BOOST_AUTO_TEST_CASE( async_write_handler ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
std::string output;
con->set_write_handler(websocketpp::lib::bind(
&write_handler,
websocketpp::lib::ref(output),
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2,
websocketpp::lib::placeholders::_3
));
con->write("foo");
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL(output, "foo");
}
BOOST_AUTO_TEST_CASE( async_write_handler_error ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_write_handler(&write_handler_error);
con->write("foo");
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::error::general) );
}
BOOST_AUTO_TEST_CASE( async_write_vector_0_ostream ) {
std::stringstream output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->register_ostream(&output);
std::vector<websocketpp::transport::buffer> bufs;
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output.str(), "" );
}
BOOST_AUTO_TEST_CASE( async_write_vector_0_write_handler ) {
std::string output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_write_handler(websocketpp::lib::bind(
&write_handler,
websocketpp::lib::ref(output),
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2,
websocketpp::lib::placeholders::_3
));
std::vector<websocketpp::transport::buffer> bufs;
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output, "" );
}
BOOST_AUTO_TEST_CASE( async_write_vector_1_ostream ) {
std::stringstream output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->register_ostream(&output);
std::vector<websocketpp::transport::buffer> bufs;
std::string foo = "foo";
bufs.push_back(websocketpp::transport::buffer(foo.data(),foo.size()));
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output.str(), "foo" );
}
BOOST_AUTO_TEST_CASE( async_write_vector_1_write_handler ) {
std::string output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_write_handler(websocketpp::lib::bind(
&write_handler,
websocketpp::lib::ref(output),
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2,
websocketpp::lib::placeholders::_3
));
std::vector<websocketpp::transport::buffer> bufs;
std::string foo = "foo";
bufs.push_back(websocketpp::transport::buffer(foo.data(),foo.size()));
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output, "foo" );
}
BOOST_AUTO_TEST_CASE( async_write_vector_2_ostream ) {
std::stringstream output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->register_ostream(&output);
std::vector<websocketpp::transport::buffer> bufs;
std::string foo = "foo";
std::string bar = "bar";
bufs.push_back(websocketpp::transport::buffer(foo.data(),foo.size()));
bufs.push_back(websocketpp::transport::buffer(bar.data(),bar.size()));
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output.str(), "foobar" );
}
BOOST_AUTO_TEST_CASE( async_write_vector_2_write_handler ) {
std::string output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_write_handler(websocketpp::lib::bind(
&write_handler,
websocketpp::lib::ref(output),
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2,
websocketpp::lib::placeholders::_3
));
std::vector<websocketpp::transport::buffer> bufs;
std::string foo = "foo";
std::string bar = "bar";
bufs.push_back(websocketpp::transport::buffer(foo.data(),foo.size()));
bufs.push_back(websocketpp::transport::buffer(bar.data(),bar.size()));
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output, "foobar" );
}
BOOST_AUTO_TEST_CASE( async_write_vector_2_vector_write_handler ) {
std::string output;
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_vector_write_handler(websocketpp::lib::bind(
&vector_write_handler,
websocketpp::lib::ref(output),
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2
));
std::vector<websocketpp::transport::buffer> bufs;
std::string foo = "foo";
std::string bar = "bar";
bufs.push_back(websocketpp::transport::buffer(foo.data(),foo.size()));
bufs.push_back(websocketpp::transport::buffer(bar.data(),bar.size()));
con->write(bufs);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( output, "foobar" );
}
BOOST_AUTO_TEST_CASE( async_read_at_least_too_much ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
con->async_read_at_least(11,buf,10);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::iostream::error::invalid_num_bytes) );
}
BOOST_AUTO_TEST_CASE( async_read_at_least_double_read ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
con->async_read_at_least(5,buf,10);
con->async_read_at_least(5,buf,10);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::iostream::error::double_read) );
}
BOOST_AUTO_TEST_CASE( async_read_at_least ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
memset(buf,'x',10);
con->async_read_at_least(5,buf,10);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
std::stringstream channel;
channel << "abcd";
channel >> *con;
BOOST_CHECK_EQUAL( channel.tellg(), -1 );
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
std::stringstream channel2;
channel2 << "e";
channel2 >> *con;
BOOST_CHECK_EQUAL( channel2.tellg(), -1 );
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdexxxxx" );
std::stringstream channel3;
channel3 << "f";
channel3 >> *con;
BOOST_CHECK_EQUAL( channel3.tellg(), 0 );
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdexxxxx" );
con->async_read_at_least(1,buf+5,5);
channel3 >> *con;
BOOST_CHECK_EQUAL( channel3.tellg(), -1 );
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdefxxxx" );
}
BOOST_AUTO_TEST_CASE( async_read_at_least2 ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
memset(buf,'x',10);
con->async_read_at_least(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
std::stringstream channel;
channel << "abcdefg";
channel >> *con;
BOOST_CHECK_EQUAL( channel.tellg(), 5 );
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdexxxxx" );
con->async_read_at_least(1,buf+5,5);
channel >> *con;
BOOST_CHECK_EQUAL( channel.tellg(), -1 );
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdefgxxx" );
}
void timer_callback_stub(websocketpp::lib::error_code const &) {}
BOOST_AUTO_TEST_CASE( set_timer ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
stub_con::timer_ptr tp = con->set_timer(1000,timer_callback_stub);
BOOST_CHECK( !tp );
}
BOOST_AUTO_TEST_CASE( async_read_at_least_read_some ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
memset(buf,'x',10);
con->async_read_at_least(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
char input[10] = "abcdefg";
BOOST_CHECK_EQUAL(con->read_some(input,5), 5);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdexxxxx" );
BOOST_CHECK_EQUAL(con->read_some(input+5,2), 0);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdexxxxx" );
con->async_read_at_least(1,buf+5,5);
BOOST_CHECK_EQUAL(con->read_some(input+5,2), 2);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "abcdefgxxx" );
}
BOOST_AUTO_TEST_CASE( async_read_at_least_read_some_indef ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[20];
memset(buf,'x',20);
con->async_read_indef(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
// here we expect to return early from read some because the outstanding
// read was for 5 bytes and we were called with 10.
char input[11] = "aaaaabbbbb";
BOOST_CHECK_EQUAL(con->read_some(input,10), 5);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "aaaaaxxxxx" );
BOOST_CHECK_EQUAL( con->indef_read_total, 5 );
// A subsequent read should read 5 more because the indef read refreshes
// itself. The new read will start again at the beginning of the buffer.
BOOST_CHECK_EQUAL(con->read_some(input+5,5), 5);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "bbbbbxxxxx" );
BOOST_CHECK_EQUAL( con->indef_read_total, 10 );
}
BOOST_AUTO_TEST_CASE( async_read_at_least_read_all ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[20];
memset(buf,'x',20);
con->async_read_indef(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
char input[11] = "aaaaabbbbb";
BOOST_CHECK_EQUAL(con->read_all(input,10), 10);
BOOST_CHECK( !con->ec );
BOOST_CHECK_EQUAL( std::string(buf,10), "bbbbbxxxxx" );
BOOST_CHECK_EQUAL( con->indef_read_total, 10 );
}
BOOST_AUTO_TEST_CASE( eof_flag ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
con->async_read_at_least(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
con->eof();
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::error::eof) );
}
BOOST_AUTO_TEST_CASE( fatal_error_flag ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
char buf[10];
con->async_read_at_least(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
con->fatal_error();
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::error::pass_through) );
}
BOOST_AUTO_TEST_CASE( shutdown ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
con->shutdown();
BOOST_CHECK_EQUAL( con->ec, websocketpp::lib::error_code() );
}
websocketpp::lib::error_code sd_handler(websocketpp::connection_hdl) {
return make_error_code(websocketpp::transport::error::general);
}
BOOST_AUTO_TEST_CASE( shutdown_handler ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_shutdown_handler(&sd_handler);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
con->shutdown();
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::error::general) );
}
BOOST_AUTO_TEST_CASE( clear_handler ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
con->set_shutdown_handler(&sd_handler);
con->set_shutdown_handler(NULL);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
con->shutdown();
BOOST_CHECK_EQUAL( con->ec, websocketpp::lib::error_code() );
}
BOOST_AUTO_TEST_CASE( shared_pointer_memory_cleanup ) {
stub_con::ptr con(new stub_con(true,alogger,elogger));
BOOST_CHECK_EQUAL(con.use_count(), 1);
char buf[10];
memset(buf,'x',10);
con->async_read_at_least(5,buf,5);
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::error::test) );
BOOST_CHECK_EQUAL(con.use_count(), 2);
char input[10] = "foo";
con->read_some(input,3);
BOOST_CHECK_EQUAL(con.use_count(), 2);
con->read_some(input,2);
BOOST_CHECK_EQUAL( std::string(buf,10), "foofoxxxxx" );
BOOST_CHECK_EQUAL(con.use_count(), 1);
con->async_read_at_least(5,buf,5);
BOOST_CHECK_EQUAL(con.use_count(), 2);
con->eof();
BOOST_CHECK_EQUAL( con->ec, make_error_code(websocketpp::transport::error::eof) );
BOOST_CHECK_EQUAL(con.use_count(), 1);
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_iostream_endpoint
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/transport/iostream/endpoint.hpp>
BOOST_AUTO_TEST_CASE( placeholder ) {}
/*BOOST_AUTO_TEST_CASE( blank_error ) {
websocketpp::lib::error_code ec;
BOOST_CHECK( !ec );
}*/