mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-02 16:26:48 +00:00
adds lots of iostream transport unit tests
This commit is contained in:
@@ -171,7 +171,7 @@ Export('polyfill_libs')
|
||||
## TARGETS:
|
||||
|
||||
# Unit tests, add test folders with SConscript files to to_test list.
|
||||
to_test = ['utility','http','logger','random','processors','message_buffer','extension','transport/asio','endpoint','connection'] #,'http','processors','connection'
|
||||
to_test = ['utility','http','logger','random','processors','message_buffer','extension','transport/iostream','transport/asio','endpoint','connection'] #,'http','processors','connection'
|
||||
|
||||
for t in to_test:
|
||||
new_tests = SConscript('#/test/'+t+'/SConscript',variant_dir = testdir + t, duplicate = 0)
|
||||
|
||||
31
test/transport/iostream/SConscript
Normal file
31
test/transport/iostream/SConscript
Normal 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')
|
||||
33
test/transport/iostream/base.cpp
Normal file
33
test/transport/iostream/base.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 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 ) {}
|
||||
270
test/transport/iostream/connection.cpp
Normal file
270
test/transport/iostream/connection.cpp
Normal file
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 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/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 {
|
||||
stub_con(bool is_server, config::alog_type &a, 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))
|
||||
{}
|
||||
|
||||
void write(std::string msg) {
|
||||
iostream_con::async_write(
|
||||
msg.data(),
|
||||
msg.size(),
|
||||
websocketpp::lib::bind(
|
||||
&stub_con::handle_op,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void write(std::vector<websocketpp::transport::buffer> & bufs) {
|
||||
iostream_con::async_write(
|
||||
bufs,
|
||||
websocketpp::lib::bind(
|
||||
&stub_con::handle_op,
|
||||
this,
|
||||
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,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void handle_op(const websocketpp::lib::error_code& e) {
|
||||
ec = e;
|
||||
}
|
||||
|
||||
websocketpp::lib::error_code ec;
|
||||
};
|
||||
|
||||
// Stubs
|
||||
config::alog_type a;
|
||||
config::elog_type e;
|
||||
|
||||
BOOST_AUTO_TEST_CASE( const_methods ) {
|
||||
iostream_con con(true,a,e);
|
||||
|
||||
BOOST_CHECK( con.is_secure() == false );
|
||||
BOOST_CHECK( con.get_remote_endpoint() == "iostream" );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( write_before_ostream_set ) {
|
||||
stub_con con(true,a,e);
|
||||
|
||||
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( con.ec == make_error_code(websocketpp::transport::iostream::error::output_stream_required) );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_write ) {
|
||||
stub_con con(true,a,e);
|
||||
|
||||
std::stringstream output;
|
||||
|
||||
con.register_ostream(&output);
|
||||
|
||||
con.write("foo");
|
||||
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( output.str() == "foo" );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_write_vector_0 ) {
|
||||
std::stringstream output;
|
||||
|
||||
stub_con con(true,a,e);
|
||||
con.register_ostream(&output);
|
||||
|
||||
std::vector<websocketpp::transport::buffer> bufs;
|
||||
|
||||
con.write(bufs);
|
||||
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( output.str() == "" );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_write_vector_1 ) {
|
||||
std::stringstream output;
|
||||
|
||||
stub_con con(true,a,e);
|
||||
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( output.str() == "foo" );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_write_vector_2 ) {
|
||||
std::stringstream output;
|
||||
|
||||
stub_con con(true,a,e);
|
||||
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( output.str() == "foobar" );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_read_at_least_too_much ) {
|
||||
stub_con con(true,a,e);
|
||||
|
||||
char buf[10];
|
||||
|
||||
con.async_read_at_least(11,buf,10);
|
||||
BOOST_CHECK( con.ec == make_error_code(websocketpp::transport::iostream::error::invalid_num_bytes) );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_read_at_least_double_read ) {
|
||||
stub_con con(true,a,e);
|
||||
|
||||
char buf[10];
|
||||
|
||||
con.async_read_at_least(5,buf,10);
|
||||
con.async_read_at_least(5,buf,10);
|
||||
BOOST_CHECK( con.ec == make_error_code(websocketpp::transport::iostream::error::double_read) );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_read_at_least ) {
|
||||
stub_con con(true,a,e);
|
||||
|
||||
char buf[10];
|
||||
|
||||
memset(buf,'x',10);
|
||||
|
||||
con.async_read_at_least(5,buf,10);
|
||||
BOOST_CHECK( con.ec == make_error_code(websocketpp::error::test) );
|
||||
|
||||
std::stringstream channel;
|
||||
channel << "abcd";
|
||||
channel >> con;
|
||||
BOOST_CHECK( channel.tellg() == -1 );
|
||||
BOOST_CHECK( con.ec == make_error_code(websocketpp::error::test) );
|
||||
|
||||
std::stringstream channel2;
|
||||
channel2 << "e";
|
||||
channel2 >> con;
|
||||
BOOST_CHECK( channel2.tellg() == -1 );
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( std::string(buf,10) == "abcdexxxxx" );
|
||||
|
||||
std::stringstream channel3;
|
||||
channel3 << "f";
|
||||
channel3 >> con;
|
||||
BOOST_CHECK( channel3.tellg() == 0 );
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( std::string(buf,10) == "abcdexxxxx" );
|
||||
con.async_read_at_least(1,buf+5,5);
|
||||
channel3 >> con;
|
||||
BOOST_CHECK( channel3.tellg() == -1 );
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( std::string(buf,10) == "abcdefxxxx" );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( async_read_at_least2 ) {
|
||||
stub_con con(true,a,e);
|
||||
|
||||
char buf[10];
|
||||
|
||||
memset(buf,'x',10);
|
||||
|
||||
con.async_read_at_least(5,buf,5);
|
||||
BOOST_CHECK( con.ec == make_error_code(websocketpp::error::test) );
|
||||
|
||||
std::stringstream channel;
|
||||
channel << "abcdefg";
|
||||
channel >> con;
|
||||
BOOST_CHECK( channel.tellg() == 5 );
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( std::string(buf,10) == "abcdexxxxx" );
|
||||
|
||||
con.async_read_at_least(1,buf+5,5);
|
||||
channel >> con;
|
||||
BOOST_CHECK( channel.tellg() == -1 );
|
||||
BOOST_CHECK( !con.ec );
|
||||
BOOST_CHECK( std::string(buf,10) == "abcdefgxxx" );
|
||||
}
|
||||
41
test/transport/iostream/endpoint.cpp
Normal file
41
test/transport/iostream/endpoint.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 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 );
|
||||
}*/
|
||||
Reference in New Issue
Block a user