Squashed 'src/websocketpp/' content from commit 875d420

git-subtree-dir: src/websocketpp
git-subtree-split: 875d420952d2c47d38de15952a401f00773cdffc
This commit is contained in:
Vinnie Falco
2015-01-20 09:35:00 -08:00
commit 02855d7fed
209 changed files with 37062 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
## processor 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] + ['z']
objs = env.Object('test_processor_boost.o', ["processor.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi13_boost.o', ["hybi13.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi08_boost.o', ["hybi08.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi07_boost.o', ["hybi07.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi00_boost.o', ["hybi00.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_extension_permessage_compress_boost.o', ["extension_permessage_compress.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_processor_boost', ["test_processor_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi13_boost', ["test_hybi13_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi08_boost', ["test_hybi08_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi07_boost', ["test_hybi07_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi00_boost', ["test_hybi00_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_extension_permessage_compress_boost', ["test_extension_permessage_compress_boost.o"], LIBS = BOOST_LIBS + ['z'])
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs] + ['z']
# no C++11 features are used in processor so there are no C++11 versions of
# these tests.
objs += env_cpp11.Object('test_processor_stl.o', ["processor.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi13_stl.o', ["hybi13.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi08_stl.o', ["hybi08.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi07_stl.o', ["hybi07.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi00_stl.o', ["hybi00.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_extension_permessage_compress_stl.o', ["extension_permessage_compress.cpp"], LIBS = BOOST_LIBS_CPP11 + ['z'])
prgs += env_cpp11.Program('test_processor_stl', ["test_processor_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi13_stl', ["test_hybi13_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi08_stl', ["test_hybi08_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi07_stl', ["test_hybi07_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi00_stl', ["test_hybi00_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_extension_permessage_compress_stl', ["test_extension_permessage_compress_stl.o"], LIBS = BOOST_LIBS_CPP11 + ['z'])
Return('prgs')

View File

@@ -0,0 +1,198 @@
/*
* 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 extension_permessage_deflate
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
struct config {
typedef websocketpp::http::parser::request request_type;
};
typedef websocketpp::extensions::permessage_deflate::enabled<config>
compressor_type;
using namespace websocketpp;
BOOST_AUTO_TEST_CASE( deflate_init ) {
/*compressor_type compressor;
websocketpp::http::parser::attribute_list attributes;
std::pair<lib::error_code,std::string> neg_ret;
neg_ret = compressor.negotiate(attributes);
BOOST_CHECK_EQUAL( neg_ret.first,
extensions::permessage_deflate::error::invalid_parameters );*/
/**
* Window size is primarily controlled by the writer. A stream can only be
* read by a window size equal to or greater than the one use to compress
* it initially. The default windows size is also the maximum window size.
* Thus:
*
* Outbound window size can be limited unilaterally under the assumption
* that the opposite end will be using the default (maximum size which can
* read anything)
*
* Inbound window size must be limited by asking the remote endpoint to
* do so and it agreeing.
*
* Context takeover is also primarily controlled by the writer. If the
* compressor does not clear its context between messages then the reader
* can't either.
*
* Outbound messages may clear context between messages unilaterally.
* Inbound messages must retain state unless the remote endpoint signals
* otherwise.
*
* Negotiation options:
* Client must choose from the following options:
* - whether or not to request an inbound window limit
* - whether or not to signal that it will honor an outbound window limit
* - whether or not to request that the server disallow context takeover
*
* Server must answer in the following ways
* - If client requested a window size limit, is the window size limit
* acceptable?
* - If client allows window limit requests, should we send one?
* - If client requested no context takeover, should we accept?
*
*
*
* All Defaults
* Req: permessage-compress; method=deflate
* Ans: permessage-compress; method=deflate
*
* # Client wants to limit the size of inbound windows from server
* permessage-compress; method="deflate; s2c_max_window_bits=8, deflate"
* Ans: permessage-compress; method="deflate; s2c_max_window_bits=8"
* OR
* Ans: permessage-compress; method=deflate
*
* # Server wants to limit the size of inbound windows from client
* Client:
* permessage-compress; method="deflate; c2s_max_window_bits, deflate"
*
* Server:
* permessage-compress; method="deflate; c2s_max_window_bits=8"
*
* # Client wants to
*
*
*
*
*
*
*/
/* processor::extensions::deflate_method d(true);
http::parser::attribute_list attributes;
lib::error_code ec;
attributes.push_back(http::parser::attribute("foo","bar"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::unknown_method_parameter);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","bar"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","7"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","16"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","9"));
ec = d.init(attributes);
BOOST_CHECK( !ec);
attributes.clear();
ec = d.init(attributes);
BOOST_CHECK( !ec);
processor::extensions::deflate_engine de;
unsigned char test_in[] = "HelloHelloHelloHello";
unsigned char test_out[30];
uLongf test_out_size = 30;
int ret;
ret = compress(test_out, &test_out_size, test_in, 20);
std::cout << ret << std::endl
<< websocketpp::utility::to_hex(test_in,20) << std::endl
<< websocketpp::utility::to_hex(test_out,test_out_size) << std::endl;
std::string input = "Hello";
std::string output = "";
ec = de.compress(input,output);
BOOST_CHECK( ec == processor::extensions::error::uninitialized );
//std::cout << ec.message() << websocketpp::utility::to_hex(output) << std::endl;
ec = de.init(15,15,Z_DEFAULT_COMPRESSION,8,Z_FIXED);
//ec = de.init();
BOOST_CHECK( !ec );
ec = de.compress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
output = "";
ec = de.compress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
input = output;
output = "";
ec = de.decompress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
*/
}

274
test/processors/hybi00.cpp Normal file
View File

@@ -0,0 +1,274 @@
/*
* 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 hybi_00_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi00.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
static const size_t max_message_size = 16000000;
};
struct processor_setup {
processor_setup(bool server)
: msg_manager(new stub_config::con_msg_manager_type())
, p(false,server,msg_manager) {}
websocketpp::lib::error_code ec;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::request_type req;
stub_config::response_type res;
websocketpp::processor::hybi00<stub_config> p;
};
typedef stub_config::message_type::ptr message_ptr;
BOOST_AUTO_TEST_CASE( exact_match ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nOrigin: http://example.com\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","WjN}|M(6");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
env.ec = env.p.validate_handshake(env.req);
BOOST_CHECK(!env.ec);
websocketpp::uri_ptr u;
BOOST_CHECK_NO_THROW( u = env.p.get_uri(env.req) );
BOOST_CHECK_EQUAL(u->get_secure(), false);
BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
BOOST_CHECK_EQUAL(u->get_resource(), "/");
BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
env.p.process_handshake(env.req,"",env.res);
BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "Upgrade");
BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "WebSocket");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Origin"), "http://example.com");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Location"), "ws://www.example.com/");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Key3"), "n`9eBk9z$R8pOtVb");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
processor_setup env(true);
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
processor_setup env(true);
websocketpp::uri_ptr u;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nOrigin: http://example.com\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK( !env.p.validate_handshake(env.req) );
BOOST_CHECK( !env.p.get_uri(env.req)->get_valid() );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols ) {
processor_setup env(true);
std::vector<std::string> subps;
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_CHECK_EQUAL( subps.size(), 0 );
}
BOOST_AUTO_TEST_CASE( prepare_data_frame_null ) {
processor_setup env(true);
message_ptr in = env.msg_manager->get_message();
message_ptr out = env.msg_manager->get_message();
message_ptr invalid;
// empty pointers arguements should return sane error
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments );
// test valid opcodes
// text (1) should be the only valid opcode
for (int i = 0; i < 0xF; i++) {
in->set_opcode(websocketpp::frame::opcode::value(i));
env.ec = env.p.prepare_data_frame(in,out);
if (i != 1) {
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
} else {
BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode );
}
}
/*
* TODO: tests for invalid UTF8
char buf[2] = {0x00, 0x00};
in->set_opcode(websocketpp::frame::opcode::text);
in->set_payload("foo");
env.ec = env.p.prepare_data_frame(in,out);
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_payload );
*/
}
BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
processor_setup env(true);
message_ptr in = env.msg_manager->get_message();
message_ptr out = env.msg_manager->get_message();
in->set_opcode(websocketpp::frame::opcode::text);
in->set_payload("foo");
env.ec = env.p.prepare_data_frame(in,out);
unsigned char raw_header[1] = {0x00};
unsigned char raw_payload[4] = {0x66,0x6f,0x6f,0xff};
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( out->get_header(), std::string(reinterpret_cast<char*>(raw_header),1) );
BOOST_CHECK_EQUAL( out->get_payload(), std::string(reinterpret_cast<char*>(raw_payload),4) );
}
BOOST_AUTO_TEST_CASE( empty_consume ) {
uint8_t frame[2] = {0x00,0x00};
processor_setup env(true);
size_t ret = env.p.consume(frame,0,env.ec);
BOOST_CHECK_EQUAL( ret, 0);
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( empty_frame ) {
uint8_t frame[2] = {0x00, 0xff};
processor_setup env(true);
size_t ret = env.p.consume(frame,2,env.ec);
BOOST_CHECK_EQUAL( ret, 2);
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "" );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( short_frame ) {
uint8_t frame[5] = {0x00, 0x66, 0x6f, 0x6f, 0xff};
processor_setup env(true);
size_t ret = env.p.consume(frame,5,env.ec);
BOOST_CHECK_EQUAL( ret, 5);
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "foo" );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}

193
test/processors/hybi07.cpp Normal file
View File

@@ -0,0 +1,193 @@
/*
* 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 hybi_07_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi07.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/random/none.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
static const size_t max_message_size = 16000000;
/// Extension related config
static const bool enable_extensions = false;
/// Extension specific config
/// permessage_compress_config
struct permessage_deflate_config {
typedef stub_config::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::disabled
<permessage_deflate_config> permessage_deflate_type;
};
BOOST_AUTO_TEST_CASE( exact_match ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK(!ec);
websocketpp::uri_ptr u;
u = p.get_uri(r);
BOOST_CHECK(u->get_valid());
BOOST_CHECK(!u->get_secure());
BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
BOOST_CHECK_EQUAL(u->get_resource(), "/");
BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
p.process_handshake(r,"",response);
BOOST_CHECK_EQUAL(response.get_header("Connection"), "upgrade");
BOOST_CHECK_EQUAL(response.get_header("Upgrade"), "websocket");
BOOST_CHECK_EQUAL(response.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( !ec );
BOOST_CHECK( !p.get_uri(r)->get_valid() );
}

197
test/processors/hybi08.cpp Normal file
View File

@@ -0,0 +1,197 @@
/*
* 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 hybi_08_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi08.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/random/none.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
static const size_t max_message_size = 16000000;
/// Extension related config
static const bool enable_extensions = false;
/// Extension specific config
/// permessage_deflate_config
struct permessage_deflate_config {
typedef stub_config::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::disabled
<permessage_deflate_config> permessage_deflate_type;
};
BOOST_AUTO_TEST_CASE( exact_match ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK(!ec);
websocketpp::uri_ptr u;
u = p.get_uri(r);
BOOST_CHECK(u->get_valid() == true);
BOOST_CHECK(u->get_secure() == false);
BOOST_CHECK(u->get_host() == "www.example.com");
BOOST_CHECK(u->get_resource() == "/");
BOOST_CHECK(u->get_port() == websocketpp::uri_default_port);
p.process_handshake(r,"",response);
BOOST_CHECK(response.get_header("Connection") == "upgrade");
BOOST_CHECK(response.get_header("Upgrade") == "websocket");
BOOST_CHECK(response.get_header("Sec-WebSocket-Accept") == "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::rng_type rng;
stub_config::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::uri_ptr u;
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( !ec );
u = p.get_uri(r);
BOOST_CHECK( !u->get_valid() );
}

693
test/processors/hybi13.cpp Normal file
View File

@@ -0,0 +1,693 @@
/*
* 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 hybi_13_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi13.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
#include <websocketpp/random/none.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
struct permessage_deflate_config {
typedef stub_config::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::disabled
<permessage_deflate_config> permessage_deflate_type;
static const size_t max_message_size = 16000000;
static const bool enable_extensions = false;
};
struct stub_config_ext {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
struct permessage_deflate_config {
typedef stub_config_ext::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
static const size_t max_message_size = 16000000;
static const bool enable_extensions = true;
};
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
// Set up a structure that constructs new copies of all of the support structure
// for using connection processors
struct processor_setup {
processor_setup(bool server)
: msg_manager(new con_msg_manager_type())
, p(false,server,msg_manager,rng) {}
websocketpp::lib::error_code ec;
con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
stub_config::request_type req;
stub_config::response_type res;
websocketpp::processor::hybi13<stub_config> p;
};
struct processor_setup_ext {
processor_setup_ext(bool server)
: msg_manager(new con_msg_manager_type())
, p(false,server,msg_manager,rng) {}
websocketpp::lib::error_code ec;
con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
stub_config::request_type req;
stub_config::response_type res;
websocketpp::processor::hybi13<stub_config_ext> p;
};
BOOST_AUTO_TEST_CASE( exact_match ) {
processor_setup env(true);
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";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK(!env.p.validate_handshake(env.req));
websocketpp::uri_ptr u;
BOOST_CHECK_NO_THROW( u = env.p.get_uri(env.req) );
BOOST_CHECK_EQUAL(u->get_secure(), false);
BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
BOOST_CHECK_EQUAL(u->get_resource(), "/");
BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
env.p.process_handshake(env.req,"",env.res);
BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "upgrade");
BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "websocket");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
processor_setup env(true);
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK( env.p.validate_handshake(env.req) == websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
BOOST_CHECK( !env.p.validate_handshake(env.req) );
BOOST_CHECK( !env.p.get_uri(env.req)->get_valid() );
}
// FRAME TESTS TO DO
//
// unmasked, 0 length, binary
// 0x82 0x00
//
// masked, 0 length, binary
// 0x82 0x80
//
// unmasked, 0 length, text
// 0x81 0x00
//
// masked, 0 length, text
// 0x81 0x80
BOOST_AUTO_TEST_CASE( frame_empty_binary_unmasked ) {
uint8_t frame[2] = {0x82, 0x00};
// all in one chunk
processor_setup env1(false);
size_t ret1 = env1.p.consume(frame,2,env1.ec);
BOOST_CHECK_EQUAL( ret1, 2 );
BOOST_CHECK( !env1.ec );
BOOST_CHECK_EQUAL( env1.p.ready(), true );
// two separate chunks
processor_setup env2(false);
BOOST_CHECK_EQUAL( env2.p.consume(frame,1,env2.ec), 1 );
BOOST_CHECK( !env2.ec );
BOOST_CHECK_EQUAL( env2.p.ready(), false );
BOOST_CHECK_EQUAL( env2.p.consume(frame+1,1,env2.ec), 1 );
BOOST_CHECK( !env2.ec );
BOOST_CHECK_EQUAL( env2.p.ready(), true );
}
BOOST_AUTO_TEST_CASE( frame_small_binary_unmasked ) {
processor_setup env(false);
uint8_t frame[4] = {0x82, 0x02, 0x2A, 0x2A};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,4,env.ec), 4 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
message_ptr foo = env.p.get_message();
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( foo->get_payload(), "**" );
}
BOOST_AUTO_TEST_CASE( frame_extended_binary_unmasked ) {
processor_setup env(false);
uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
frame[0] = 0x82;
frame[1] = 0x7E;
frame[2] = 0x00;
frame[3] = 0x7E;
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,130,env.ec), 130 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
message_ptr foo = env.p.get_message();
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( foo->get_payload().size(), 126 );
}
BOOST_AUTO_TEST_CASE( frame_jumbo_binary_unmasked ) {
processor_setup env(false);
uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,130,env.ec), 130 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
message_ptr foo = env.p.get_message();
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( foo->get_payload().size(), 126 );
}
BOOST_AUTO_TEST_CASE( control_frame_too_large ) {
processor_setup env(false);
uint8_t frame[130] = {0x88, 0x7E, 0x00, 0x7E};
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,130,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::control_too_big );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( rsv_bits_used ) {
uint8_t frame[3][2] = {{0x90, 0x00},
{0xA0, 0x00},
{0xC0, 0x00}};
for (int i = 0; i < 3; i++) {
processor_setup env(false);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame[i],2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_rsv_bit );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
}
BOOST_AUTO_TEST_CASE( reserved_opcode_used ) {
uint8_t frame[10][2] = {{0x83, 0x00},
{0x84, 0x00},
{0x85, 0x00},
{0x86, 0x00},
{0x87, 0x00},
{0x8B, 0x00},
{0x8C, 0x00},
{0x8D, 0x00},
{0x8E, 0x00},
{0x8F, 0x00}};
for (int i = 0; i < 10; i++) {
processor_setup env(false);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame[i],2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
}
BOOST_AUTO_TEST_CASE( fragmented_control_message ) {
processor_setup env(false);
uint8_t frame[2] = {0x08, 0x00};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::fragmented_control );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( fragmented_binary_message ) {
processor_setup env0(false);
processor_setup env1(false);
uint8_t frame0[6] = {0x02, 0x01, 0x2A, 0x80, 0x01, 0x2A};
uint8_t frame1[8] = {0x02, 0x01, 0x2A, 0x89, 0x00, 0x80, 0x01, 0x2A};
// read fragmented message in one chunk
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env0.p.consume(frame0,6,env0.ec), 6 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
// read fragmented message in two chunks
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env0.p.consume(frame0,3,env0.ec), 3 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), false );
BOOST_CHECK_EQUAL( env0.p.consume(frame0+3,3,env0.ec), 3 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
// read fragmented message with control message in between
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env0.p.consume(frame1,8,env0.ec), 5 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_opcode(), websocketpp::frame::opcode::PING);
BOOST_CHECK_EQUAL( env0.p.consume(frame1+5,3,env0.ec), 3 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
// read lone continuation frame
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env0.p.consume(frame0+3,3,env0.ec), 0);
BOOST_CHECK_EQUAL( env0.ec, websocketpp::processor::error::invalid_continuation );
// read two start frames in a row
BOOST_CHECK_EQUAL( env1.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env1.p.consume(frame0,3,env1.ec), 3);
BOOST_CHECK( !env1.ec );
BOOST_CHECK_GT( env1.p.consume(frame0,3,env1.ec), 0);
BOOST_CHECK_EQUAL( env1.ec, websocketpp::processor::error::invalid_continuation );
}
BOOST_AUTO_TEST_CASE( unmasked_client_frame ) {
processor_setup env(true);
uint8_t frame[2] = {0x82, 0x00};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::masking_required );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( masked_server_frame ) {
processor_setup env(false);
uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,8,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::masking_forbidden );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( frame_small_binary_masked ) {
processor_setup env(true);
uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,8,env.ec), 8 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "**" );
}
BOOST_AUTO_TEST_CASE( masked_fragmented_binary_message ) {
processor_setup env(true);
uint8_t frame0[14] = {0x02, 0x81, 0xAB, 0x23, 0x98, 0x45, 0x81,
0x80, 0x81, 0xB8, 0x34, 0x12, 0xFF, 0x92};
// read fragmented message in one chunk
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame0,14,env.ec), 14 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "**" );
}
BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
processor_setup env(true);
message_ptr in = env.msg_manager->get_message();
message_ptr out = env.msg_manager->get_message();
message_ptr invalid;
// empty pointers arguements should return sane error
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments );
// test valid opcodes
// control opcodes should return an error, data ones shouldn't
for (int i = 0; i < 0xF; i++) {
in->set_opcode(websocketpp::frame::opcode::value(i));
env.ec = env.p.prepare_data_frame(in,out);
if (websocketpp::frame::opcode::is_control(in->get_opcode())) {
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
} else {
BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode );
}
}
//in.set_payload("foo");
//e = prepare_data_frame(in,out);
}
BOOST_AUTO_TEST_CASE( single_frame_message_too_large ) {
processor_setup env(true);
env.p.set_max_message_size(3);
uint8_t frame0[10] = {0x82, 0x84, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01};
// read message that is one byte too large
BOOST_CHECK_EQUAL( env.p.consume(frame0,10,env.ec), 6 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::message_too_big );
}
BOOST_AUTO_TEST_CASE( multiple_frame_message_too_large ) {
processor_setup env(true);
env.p.set_max_message_size(4);
uint8_t frame0[8] = {0x02, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01};
uint8_t frame1[9] = {0x80, 0x83, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01};
// read first message frame with size under the limit
BOOST_CHECK_EQUAL( env.p.consume(frame0,8,env.ec), 8 );
BOOST_CHECK( !env.ec );
// read second message frame that puts the size over the limit
BOOST_CHECK_EQUAL( env.p.consume(frame1,9,env.ec), 6 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::message_too_big );
}
BOOST_AUTO_TEST_CASE( client_handshake_request ) {
processor_setup env(false);
websocketpp::uri_ptr u(new websocketpp::uri("ws://localhost/"));
env.p.client_handshake_request(env.req,u, std::vector<std::string>());
BOOST_CHECK_EQUAL( env.req.get_method(), "GET" );
BOOST_CHECK_EQUAL( env.req.get_version(), "HTTP/1.1");
BOOST_CHECK_EQUAL( env.req.get_uri(), "/");
BOOST_CHECK_EQUAL( env.req.get_header("Host"), "localhost");
BOOST_CHECK_EQUAL( env.req.get_header("Sec-WebSocket-Version"), "13");
BOOST_CHECK_EQUAL( env.req.get_header("Connection"), "Upgrade");
BOOST_CHECK_EQUAL( env.req.get_header("Upgrade"), "websocket");
}
// TODO:
// test cases
// - adding headers
// - adding Upgrade header
// - adding Connection header
// - adding Sec-WebSocket-Version, Sec-WebSocket-Key, or Host header
// - other Sec* headers?
// - User Agent header?
// Origin support
// Subprotocol requests
//websocketpp::uri_ptr u(new websocketpp::uri("ws://localhost/"));
//env.p.client_handshake_request(env.req,u);
BOOST_AUTO_TEST_CASE( client_handshake_response_404 ) {
processor_setup env(false);
std::string res = "HTTP/1.1 404 Not Found\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::invalid_http_status );
}
BOOST_AUTO_TEST_CASE( client_handshake_response_no_upgrade ) {
processor_setup env(false);
std::string res = "HTTP/1.1 101 Switching Protocols\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( client_handshake_response_no_connection ) {
processor_setup env(false);
std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( client_handshake_response_no_accept ) {
processor_setup env(false);
std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\nConnection: bar, UpGrAdE\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( client_handshake_response ) {
processor_setup env(false);
env.req.append_header("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\nConnection: bar, UpGrAdE\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK( !env.p.validate_server_handshake_response(env.req,env.res) );
}
BOOST_AUTO_TEST_CASE( extensions_disabled ) {
processor_setup env(true);
env.req.replace_header("Sec-WebSocket-Extensions","");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK_EQUAL( neg_results.first, websocketpp::processor::error::extensions_disabled );
BOOST_CHECK_EQUAL( neg_results.second, "" );
}
BOOST_AUTO_TEST_CASE( extension_negotiation_blank ) {
processor_setup_ext env(true);
env.req.replace_header("Sec-WebSocket-Extensions","");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK( !neg_results.first );
BOOST_CHECK_EQUAL( neg_results.second, "" );
}
BOOST_AUTO_TEST_CASE( extension_negotiation_unknown ) {
processor_setup_ext env(true);
env.req.replace_header("Sec-WebSocket-Extensions","foo");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK( !neg_results.first );
BOOST_CHECK_EQUAL( neg_results.second, "" );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_empty ) {
processor_setup env(true);
std::vector<std::string> subps;
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_CHECK_EQUAL( subps.size(), 0 );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_one ) {
processor_setup env(true);
std::vector<std::string> subps;
env.req.replace_header("Sec-WebSocket-Protocol","foo");
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_REQUIRE_EQUAL( subps.size(), 1 );
BOOST_CHECK_EQUAL( subps[0], "foo" );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_multiple ) {
processor_setup env(true);
std::vector<std::string> subps;
env.req.replace_header("Sec-WebSocket-Protocol","foo,bar");
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_REQUIRE_EQUAL( subps.size(), 2 );
BOOST_CHECK_EQUAL( subps[0], "foo" );
BOOST_CHECK_EQUAL( subps[1], "bar" );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_invalid) {
processor_setup env(true);
std::vector<std::string> subps;
env.req.replace_header("Sec-WebSocket-Protocol","foo,bar,,,,");
BOOST_CHECK_EQUAL( env.p.extract_subprotocols(env.req,subps), websocketpp::processor::error::make_error_code(websocketpp::processor::error::subprotocol_parse_error) );
BOOST_CHECK_EQUAL( subps.size(), 0 );
}
BOOST_AUTO_TEST_CASE( extension_negotiation_permessage_deflate ) {
processor_setup_ext env(true);
env.req.replace_header("Sec-WebSocket-Extensions",
"permessage-deflate; c2s_max_window_bits");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK( !neg_results.first );
BOOST_CHECK_EQUAL( neg_results.second, "permessage-deflate" );
}

View File

@@ -0,0 +1,135 @@
/*
* 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 processors
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/processor.hpp>
#include <websocketpp/http/request.hpp>
BOOST_AUTO_TEST_CASE( exact_match ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( non_match ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(!websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( ci_exact_match ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: UpGrAde\r\nUpgrade: WebSocket\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( non_exact_match1 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade,foo\r\nUpgrade: websocket,foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( non_exact_match2 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: keep-alive,Upgrade,foo\r\nUpgrade: foo,websocket,bar\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( version_blank ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 0);
}
BOOST_AUTO_TEST_CASE( version_7 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 7);
}
BOOST_AUTO_TEST_CASE( version_8 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 8);
}
BOOST_AUTO_TEST_CASE( version_13 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 13);
}
BOOST_AUTO_TEST_CASE( version_non_numeric ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: abc\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == -1);
}