Initial commit from private 0.3 repository

This commit is contained in:
Peter Thorson
2013-01-06 06:09:15 -06:00
parent 5809440f1a
commit f10f8a4ce0
319 changed files with 16750 additions and 45341 deletions

View File

@@ -1,29 +0,0 @@
BOOST_LIB_PATH ?= /usr/local/lib
BOOST_INCLUDE_PATH ?= /usr/local/include
CFLAGS = -O2 -I$(BOOST_INCLUDE_PATH)
LDFLAGS = -L$(BOOST_LIB_PATH)
CXX ?= c++
SHARED ?= "1"
ifeq ($(SHARED), 1)
LDFLAGS := $(LDFLAGS) -lboost_system -lboost_date_time -lboost_regex -lboost_unit_test_framework -lwebsocketpp
else
LDFLAGS := $(LDFLAGS) $(BOOST_LIB_PATH)/libboost_system.a $(BOOST_LIB_PATH)/libboost_date_time.a $(BOOST_LIB_PATH)/libboost_regex.a $(BOOST_LIB_PATH)/libboost_unit_test_framework.a ../../libwebsocketpp.a
endif
tests: hybi_util.cpp
$(CXX) $(CFLAGS) $^ -o $@ $(LDFLAGS)
perf: uri_perf.cpp
$(CXX) $(CFLAGS) $^ -o $@ $(LDFLAGS)
%.o: %.cpp
$(CXX) -c $(CFLAGS) -o $@ $^
# cleanup by removing generated files
#
.PHONY: clean
clean:
rm -f *.o tests

View File

@@ -0,0 +1,23 @@
## connection 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','regex','system'],env) + [platform_libs]
objs = env.Object('connection_boost.o', ["connection.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_connection_boost', ["connection_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('connection_stl.o', ["connection.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_connection_stl', ["connection_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,143 @@
/*
* 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 connection
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <sstream>
// Test Environment:
// server, no TLS, no locks, iostream based transport
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::core> server;
class echo_handler : public server::handler {
bool validate(connection_ptr con) {
std::cout << "handler validate" << std::endl;
if (con->get_origin() != "http://www.example.com") {
con->set_status(websocketpp::http::status_code::FORBIDDEN);
return false;
}
return true;
}
void http(connection_ptr con) {}
void on_load(connection_ptr con, ptr old_handler) {}
void on_unload(connection_ptr con, ptr new_handler) {}
void on_open(connection_ptr con) {}
void on_fail(connection_ptr con) {}
void on_message(connection_ptr con, message_ptr msg) {
con->write(msg->get_payload());
}
void on_close(connection_ptr con) {}
};
std::string run_server_test(std::string input) {
server::handler::ptr h(new echo_handler());
server test_server(h);
server::connection_ptr con;
std::stringstream output;
test_server.register_ostream(&output);
con = test_server.get_connection();
con->start();
std::stringstream channel;
channel << input;
channel >> *con;
return output.str();
}
// NOTE: these tests currently test against hardcoded output values. I am not
// sure how problematic this will be. If issues arise like order of headers the
// output should be parsed by http::response and have values checked directly
BOOST_AUTO_TEST_CASE( basic_http_request ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 500 Internal Server Error\r\nServer: " +
std::string(websocketpp::user_agent)+"\r\n\r\n";
std::string o2 = run_server_test(input);
std::cout << "output: " << o2 << std::endl;
BOOST_CHECK(o2 == output);
}
/*
BOOST_AUTO_TEST_CASE( basic_websocket_request ) {
std::string input = "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\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: "+websocketpp::USER_AGENT+"\r\nUpgrade: websocket\r\n\r\n";
BOOST_CHECK(run_server_test(input) == output);
}
BOOST_AUTO_TEST_CASE( invalid_websocket_version ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: a\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 400 Bad Request\r\nServer: "+websocketpp::USER_AGENT+"\r\n\r\n";
BOOST_CHECK(run_server_test(input) == output);
}
BOOST_AUTO_TEST_CASE( unimplimented_websocket_version ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 14\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 400 Bad Request\r\nSec-WebSocket-Version: 0,7,8,13\r\nServer: "+websocketpp::USER_AGENT+"\r\n\r\n";
BOOST_CHECK(run_server_test(input) == output);
}
BOOST_AUTO_TEST_CASE( user_reject_origin ) {
std::string input = "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\nOrigin: http://www.example2.com\r\n\r\n";
std::string output = "HTTP/1.1 403 Forbidden\r\nServer: "+websocketpp::USER_AGENT+"\r\n\r\n";
BOOST_CHECK(run_server_test(input) == output);
}
BOOST_AUTO_TEST_CASE( basic_text_message ) {
std::string input = "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\nOrigin: http://www.example.com\r\n\r\n";
unsigned char frames[8] = {0x82,0x82,0xFF,0xFF,0xFF,0xFF,0xD5,0xD5};
input.append(reinterpret_cast<char*>(frames),8);
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: "+websocketpp::USER_AGENT+"\r\nUpgrade: websocket\r\n\r\n**";
BOOST_CHECK( run_server_test(input) == output);
}
*/

23
test/extension/SConscript Normal file
View File

@@ -0,0 +1,23 @@
## http 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','regex'],env) + [platform_libs]
objs = env.Object('extension_boost.o', ["extension.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_extension_boost', ["extension_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('extension_stl.o', ["extension.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_extension_stl', ["extension_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -24,17 +24,14 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE extension
#include <boost/test/unit_test.hpp>
#include "../../src/logger/logger.hpp"
#include <string>
int main () {
websocketpp::log::logger<websocketpp::log::elevel::value> log;
log.set_levels(websocketpp::log::elevel::DEVEL,websocketpp::log::elevel::ERROR);
log.at(websocketpp::log::elevel::DEVEL) << "devel: " << 5 << websocketpp::log::endl;
log.at(websocketpp::log::elevel::INFO) << "info: " << 5 << websocketpp::log::endl;
log.at(websocketpp::log::elevel::WARN) << "warn: " << 5 << websocketpp::log::endl;
log.at(websocketpp::log::elevel::ERROR) << "error: " << 5 << websocketpp::log::endl;
log.at(websocketpp::log::elevel::FATAL) << "fatal: " << 5 << websocketpp::log::endl;
#include <websocketpp/extensions/extension.hpp>
}
BOOST_AUTO_TEST_CASE( blank ) {
BOOST_CHECK( true );
}

23
test/http/SConscript Normal file
View File

@@ -0,0 +1,23 @@
## http 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','regex'],env) + [platform_libs]
objs = env.Object('parser_boost.o', ["parser.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_http_boost', ["parser_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('parser_stl.o', ["parser.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_http_stl', ["parser_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

BIN
test/http/a.out Executable file

Binary file not shown.

830
test/http/parser.cpp Normal file
View File

@@ -0,0 +1,830 @@
/*
* 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 http_parser
#include <boost/test/unit_test.hpp>
#include <string>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
BOOST_AUTO_TEST_CASE( is_token_char ) {
// Valid characters
// misc
BOOST_CHECK( websocketpp::http::is_token_char('!') );
BOOST_CHECK( websocketpp::http::is_token_char('#') );
BOOST_CHECK( websocketpp::http::is_token_char('$') );
BOOST_CHECK( websocketpp::http::is_token_char('%') );
BOOST_CHECK( websocketpp::http::is_token_char('&') );
BOOST_CHECK( websocketpp::http::is_token_char('\'') );
BOOST_CHECK( websocketpp::http::is_token_char('*') );
BOOST_CHECK( websocketpp::http::is_token_char('+') );
BOOST_CHECK( websocketpp::http::is_token_char('-') );
BOOST_CHECK( websocketpp::http::is_token_char('.') );
BOOST_CHECK( websocketpp::http::is_token_char('^') );
BOOST_CHECK( websocketpp::http::is_token_char('_') );
BOOST_CHECK( websocketpp::http::is_token_char('`') );
BOOST_CHECK( websocketpp::http::is_token_char('~') );
// numbers
for (int i = 0x30; i < 0x3a; i++) {
BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
}
// upper
for (int i = 0x41; i < 0x5b; i++) {
BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
}
// lower
for (int i = 0x61; i < 0x7b; i++) {
BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
}
// invalid characters
// lower unprintable
for (int i = 0; i < 33; i++) {
BOOST_CHECK( !websocketpp::http::is_token_char((unsigned char)(i)) );
}
// misc
BOOST_CHECK( !websocketpp::http::is_token_char('(') );
BOOST_CHECK( !websocketpp::http::is_token_char(')') );
BOOST_CHECK( !websocketpp::http::is_token_char('<') );
BOOST_CHECK( !websocketpp::http::is_token_char('>') );
BOOST_CHECK( !websocketpp::http::is_token_char('@') );
BOOST_CHECK( !websocketpp::http::is_token_char(',') );
BOOST_CHECK( !websocketpp::http::is_token_char(';') );
BOOST_CHECK( !websocketpp::http::is_token_char(':') );
BOOST_CHECK( !websocketpp::http::is_token_char('\\') );
BOOST_CHECK( !websocketpp::http::is_token_char('"') );
BOOST_CHECK( !websocketpp::http::is_token_char('/') );
BOOST_CHECK( !websocketpp::http::is_token_char('[') );
BOOST_CHECK( !websocketpp::http::is_token_char(']') );
BOOST_CHECK( !websocketpp::http::is_token_char('?') );
BOOST_CHECK( !websocketpp::http::is_token_char('=') );
BOOST_CHECK( !websocketpp::http::is_token_char('{') );
BOOST_CHECK( !websocketpp::http::is_token_char('}') );
// upper unprintable and out of ascii range
for (int i = 127; i < 256; i++) {
BOOST_CHECK( !websocketpp::http::is_token_char((unsigned char)(i)) );
}
// is not
BOOST_CHECK( !websocketpp::http::is_not_token_char('!') );
BOOST_CHECK( websocketpp::http::is_not_token_char('(') );
}
BOOST_AUTO_TEST_CASE( extract_token ) {
std::string d1 = "foo";
std::string d2 = " foo ";
std::pair<std::string,std::string::const_iterator> ret;
ret = websocketpp::http::parser::extract_token(d1.begin(),d1.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d1.begin()+3 );
ret = websocketpp::http::parser::extract_token(d2.begin(),d2.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.second == d2.begin()+0 );
ret = websocketpp::http::parser::extract_token(d2.begin()+1,d2.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d2.begin()+4 );
}
BOOST_AUTO_TEST_CASE( extract_quoted_string ) {
std::string d1 = "\"foo\"";
std::string d2 = "\"foo\\\"bar\\\"baz\"";
std::string d3 = "\"foo\" ";
std::string d4 = "";
std::string d5 = "foo";
std::pair<std::string,std::string::const_iterator> ret;
using websocketpp::http::parser::extract_quoted_string;
ret = extract_quoted_string(d1.begin(),d1.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d1.end() );
ret = extract_quoted_string(d2.begin(),d2.end());
BOOST_CHECK( ret.first == "foo\"bar\"baz" );
BOOST_CHECK( ret.second == d2.end() );
ret = extract_quoted_string(d3.begin(),d3.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d3.begin()+5 );
ret = extract_quoted_string(d4.begin(),d4.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.second == d4.begin() );
ret = extract_quoted_string(d5.begin(),d5.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.second == d5.begin() );
}
BOOST_AUTO_TEST_CASE( extract_all_lws ) {
std::string d1 = " foo bar";
d1.append(1,char(9));
d1.append("baz\r\n d\r\n \r\n e\r\nf");
std::string::const_iterator ret;
ret = websocketpp::http::parser::extract_all_lws(d1.begin(),d1.end());
BOOST_CHECK( ret == d1.begin()+1 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+1,d1.end());
BOOST_CHECK( ret == d1.begin()+1 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+4,d1.end());
BOOST_CHECK( ret == d1.begin()+9 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+12,d1.end());
BOOST_CHECK( ret == d1.begin()+13 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+16,d1.end());
BOOST_CHECK( ret == d1.begin()+19 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+20,d1.end());
BOOST_CHECK( ret == d1.begin()+28 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+29,d1.end());
BOOST_CHECK( ret == d1.begin()+29 );
}
BOOST_AUTO_TEST_CASE( extract_attributes_blank ) {
std::string s = "";
websocketpp::http::parser::attribute_list a;
std::string::const_iterator it;
it = websocketpp::http::parser::extract_attributes(s.begin(),s.end(),a);
BOOST_CHECK( it == s.begin() );
BOOST_CHECK_EQUAL( a.size(), 0 );
}
BOOST_AUTO_TEST_CASE( extract_attributes_simple ) {
std::string s = "foo";
websocketpp::http::parser::attribute_list a;
std::string::const_iterator it;
it = websocketpp::http::parser::extract_attributes(s.begin(),s.end(),a);
BOOST_CHECK( it == s.end() );
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("foo") != a.end() );
BOOST_CHECK_EQUAL( a.find("foo")->second, "" );
}
BOOST_AUTO_TEST_CASE( extract_parameters ) {
std::string s1 = "";
std::string s2 = "foo";
std::string s3 = " foo \r\nAbc";
std::string s4 = " \r\n foo ";
std::string s5 = "foo,bar";
std::string s6 = "foo;bar";
std::string s7 = "foo;baz,bar";
std::string s8 = "foo;bar;baz";
std::string s9 = "foo;bar=baz";
std::string s10 = "foo;bar=baz;boo";
std::string s11 = "foo;bar=baz;boo,bob";
std::string s12 = "foo;bar=\"a b c\"";
std::string s13 = "foo;bar=\"a \\\"b\\\" c\"";
std::string sx = "foo;bar=\"a \\\"b\\\" c\"";
websocketpp::http::parser::parameter_list p;
websocketpp::http::parser::attribute_list a;
std::string::const_iterator it;
using websocketpp::http::parser::extract_parameters;
it = extract_parameters(s1.begin(),s1.end(),p);
BOOST_CHECK( it == s1.begin() );
p.clear();
it = extract_parameters(s2.begin(),s2.end(),p);
BOOST_CHECK( it == s2.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
BOOST_CHECK( p.find("foo")->second.size() == 0 );
p.clear();
it = extract_parameters(s3.begin(),s3.end(),p);
BOOST_CHECK( it == s3.begin()+5 );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
BOOST_CHECK( p.find("foo")->second.size() == 0 );
p.clear();
it = extract_parameters(s4.begin(),s4.end(),p);
BOOST_CHECK( it == s4.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
BOOST_CHECK( p.find("foo")->second.size() == 0 );
p.clear();
it = extract_parameters(s5.begin(),s5.end(),p);
BOOST_CHECK( it == s5.end() );
BOOST_CHECK( p.size() == 2 );
BOOST_CHECK( p.find("foo") != p.end() );
BOOST_CHECK( p.find("foo")->second.size() == 0 );
BOOST_CHECK( p.find("bar") != p.end() );
BOOST_CHECK( p.find("bar")->second.size() == 0 );
p.clear();
it = extract_parameters(s6.begin(),s6.end(),p);
BOOST_CHECK( it == s6.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "" );
p.clear();
it = extract_parameters(s7.begin(),s7.end(),p);
BOOST_CHECK( it == s7.end() );
BOOST_CHECK( p.size() == 2 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 1 );
BOOST_CHECK( a.find("baz") != a.end() );
BOOST_CHECK( a.find("baz")->second == "" );
BOOST_CHECK( p.find("bar") != p.end() );
a = p.find("bar")->second;
BOOST_CHECK( a.size() == 0 );
p.clear();
it = extract_parameters(s8.begin(),s8.end(),p);
BOOST_CHECK( it == s8.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 2 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "" );
BOOST_CHECK( a.find("baz") != a.end() );
BOOST_CHECK( a.find("baz")->second == "" );
p.clear();
it = extract_parameters(s9.begin(),s9.end(),p);
BOOST_CHECK( it == s9.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "baz" );
p.clear();
it = extract_parameters(s10.begin(),s10.end(),p);
BOOST_CHECK( it == s10.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 2 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "baz" );
BOOST_CHECK( a.find("boo") != a.end() );
BOOST_CHECK( a.find("boo")->second == "" );
p.clear();
it = extract_parameters(s11.begin(),s11.end(),p);
BOOST_CHECK( it == s11.end() );
BOOST_CHECK( p.size() == 2 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 2 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "baz" );
BOOST_CHECK( a.find("boo") != a.end() );
BOOST_CHECK( a.find("boo")->second == "" );
a = p.find("bob")->second;
BOOST_CHECK( a.size() == 0 );
p.clear();
it = extract_parameters(s12.begin(),s12.end(),p);
BOOST_CHECK( it == s12.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "a b c" );
p.clear();
it = extract_parameters(s13.begin(),s13.end(),p);
BOOST_CHECK( it == s13.end() );
BOOST_CHECK( p.size() == 1 );
BOOST_CHECK( p.find("foo") != p.end() );
a = p.find("foo")->second;
BOOST_CHECK( a.size() == 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK( a.find("bar")->second == "a \"b\" c" );
}
BOOST_AUTO_TEST_CASE( blank_consume ) {
websocketpp::http::parser::request r;
std::string raw = "";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( r.ready() == false );
}
BOOST_AUTO_TEST_CASE( blank_request ) {
websocketpp::http::parser::request r;
std::string raw = "\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
BOOST_CHECK( r.ready() == false );
}
BOOST_AUTO_TEST_CASE( bad_request_no_host ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
BOOST_CHECK( r.ready() == false );
}
BOOST_AUTO_TEST_CASE( basic_request ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( trailing_body_characters ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\na";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( basic_split1 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\n";
std::string raw2 = "Host: www.example.com\r\n\r\na";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
pos += r.consume(raw2.c_str(),raw2.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( basic_split2 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r";
std::string raw2 = "\n\r\na";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
pos += r.consume(raw2.c_str(),raw2.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( max_header_len ) {
websocketpp::http::parser::request r;
std::string raw(websocketpp::http::MAX_HEADER_SIZE+1,'*');
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (const websocketpp::http::exception& e) {
if (e.m_error_code == websocketpp::http::status_code::REQUEST_HEADER_FIELDS_TOO_LARGE) {
exception = true;
}
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( max_header_len_split ) {
websocketpp::http::parser::request r;
std::string raw(websocketpp::http::MAX_HEADER_SIZE-1,'*');
std::string raw2(2,'*');
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
pos += r.consume(raw2.c_str(),raw2.size());
} catch (const websocketpp::http::exception& e) {
if (e.m_error_code == websocketpp::http::status_code::REQUEST_HEADER_FIELDS_TOO_LARGE) {
exception = true;
}
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( firefox_full_request ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 482 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "localhost:5000" );
BOOST_CHECK( r.get_header("User-Agent") == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0" );
BOOST_CHECK( r.get_header("Accept") == "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" );
BOOST_CHECK( r.get_header("Accept-Language") == "en-us,en;q=0.5" );
BOOST_CHECK( r.get_header("Accept-Encoding") == "gzip, deflate" );
BOOST_CHECK( r.get_header("Connection") == "keep-alive, Upgrade" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Version") == "8" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Origin") == "http://zaphoyd.com" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Key") == "pFik//FxwFk0riN4ZiPFjQ==" );
BOOST_CHECK( r.get_header("Pragma") == "no-cache" );
BOOST_CHECK( r.get_header("Cache-Control") == "no-cache" );
BOOST_CHECK( r.get_header("Upgrade") == "websocket" );
}
BOOST_AUTO_TEST_CASE( bad_method ) {
websocketpp::http::parser::request r;
std::string raw = "GE]T / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( bad_header_name ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHo]st: www.example.com\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.0" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( new_http_version1 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.12\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 42 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.12" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( new_http_version2 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/12.12\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 43 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/12.12" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
/* commented out due to not being implemented yet
BOOST_AUTO_TEST_CASE( new_http_version3 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTPS/12.12\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( header_whitespace1 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com \r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 43 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( header_whitespace2 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost:www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 40 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}*/
BOOST_AUTO_TEST_CASE( header_aggregation ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\nFoo: bar\r\nFoo: bat\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 61 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Foo") == "bar, bat" );
}
BOOST_AUTO_TEST_CASE( wikipedia_example_response ) {
websocketpp::http::parser::response r;
std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 159 );
BOOST_CHECK( r.headers_ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_status_code() == websocketpp::http::status_code::SWITCHING_PROTOCOLS );
BOOST_CHECK( r.get_status_msg() == "Switching Protocols" );
BOOST_CHECK( r.get_header("Upgrade") == "websocket" );
BOOST_CHECK( r.get_header("Connection") == "Upgrade" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Accept") == "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Protocol") == "chat" );
}
BOOST_AUTO_TEST_CASE( plain_http_response ) {
websocketpp::http::parser::response r;
std::string raw = "HTTP/1.1 200 OK\r\nDate: Thu, 10 May 2012 11:59:25 GMT\r\nServer: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch\r\nLast-Modified: Tue, 30 Mar 2010 17:41:28 GMT\r\nETag: \"16799d-55-4830823a78200\"\r\nAccept-Ranges: bytes\r\nContent-Length: 85\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 405 );
BOOST_CHECK( r.headers_ready() == true );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_status_code() == websocketpp::http::status_code::OK );
BOOST_CHECK( r.get_status_msg() == "OK" );
BOOST_CHECK( r.get_header("Date") == "Thu, 10 May 2012 11:59:25 GMT" );
BOOST_CHECK( r.get_header("Server") == "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch" );
BOOST_CHECK( r.get_header("Last-Modified") == "Tue, 30 Mar 2010 17:41:28 GMT" );
BOOST_CHECK( r.get_header("ETag") == "\"16799d-55-4830823a78200\"" );
BOOST_CHECK( r.get_header("Accept-Ranges") == "bytes" );
BOOST_CHECK( r.get_header("Content-Length") == "85" );
BOOST_CHECK( r.get_header("Vary") == "Accept-Encoding" );
BOOST_CHECK( r.get_header("Content-Type") == "text/html" );
BOOST_CHECK( r.get_body() == "<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>" );
}

141
test/http/parser_perf.cpp Normal file
View File

@@ -0,0 +1,141 @@
/*
* 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.
*
*/
#include <websocketpp/http/parser.hpp>
#include <chrono>
class scoped_timer {
public:
scoped_timer(std::string i) : m_id(i),m_start(std::chrono::steady_clock::now()) {
std::cout << "Clock " << i << ": ";
}
~scoped_timer() {
std::chrono::nanoseconds time_taken = std::chrono::steady_clock::now()-m_start;
//nanoseconds_per_test
//tests_per_second
//1000000000.0/(double(time_taken.count())/1000.0)
std::cout << 1000000000.0/(double(time_taken.count())/1000.0) << std::endl;
//std::cout << (1.0/double(time_taken.count())) * double(1000000000*1000) << std::endl;
}
private:
std::string m_id;
std::chrono::steady_clock::time_point m_start;
};
int main() {
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
std::string firefox = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
std::string firefox1 = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\n";
std::string firefox2 = "Accept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
{
scoped_timer timer("Simplest 1 chop");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
{
scoped_timer timer("FireFox, 1 chop, consume old");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume2(firefox.c_str(),firefox.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
{
scoped_timer timer("FireFox, 1 chop");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume(firefox.c_str(),firefox.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
{
scoped_timer timer("FireFox, 2 chop");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume(firefox1.c_str(),firefox1.size());
r.consume(firefox2.c_str(),firefox2.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
return 0;
}

BIN
test/http/perf.out Executable file

Binary file not shown.

BIN
test/http/test.out Executable file

Binary file not shown.

View File

@@ -0,0 +1,27 @@
## message_buffer 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('message_boost.o', ["message.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('alloc_boost.o', ["alloc.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_message_boost', ["message_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_alloc_boost', ["alloc_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('message_stl.o', ["message.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('alloc_stl.o', ["alloc.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_message_stl', ["message_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_alloc_stl', ["alloc_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2012, 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 message_buffer_alloc
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/message_buffer/alloc.hpp>
template <template <class> class con_msg_manager>
struct stub {
typedef websocketpp::lib::shared_ptr<stub> ptr;
typedef con_msg_manager<stub> con_msg_man_type;
typedef typename con_msg_man_type::ptr con_msg_man_ptr;
typedef typename con_msg_man_type::weak_ptr con_msg_man_weak_ptr;
stub(con_msg_man_ptr manager, websocketpp::frame::opcode::value op, size_t size = 128)
: m_opcode(op)
, m_manager(manager)
, m_size(size) {}
bool recycle() {
con_msg_man_ptr shared = m_manager.lock();
if (shared) {
return shared->recycle(this);
} else {
return false;
}
}
websocketpp::frame::opcode::value m_opcode;
con_msg_man_weak_ptr m_manager;
size_t m_size;
};
BOOST_AUTO_TEST_CASE( basic_get_message ) {
typedef stub<websocketpp::message_buffer::alloc::con_msg_manager>
message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_man_type;
con_msg_man_type::ptr manager(new con_msg_man_type());
message_type::ptr msg = manager->get_message(websocketpp::frame::opcode::TEXT,512);
BOOST_CHECK(msg);
BOOST_CHECK(msg->m_opcode == websocketpp::frame::opcode::TEXT);
BOOST_CHECK(msg->m_manager.lock() == manager);
BOOST_CHECK(msg->m_size == 512);
}
BOOST_AUTO_TEST_CASE( basic_get_manager ) {
typedef stub<websocketpp::message_buffer::alloc::con_msg_manager>
message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_man_type;
typedef websocketpp::message_buffer::alloc::endpoint_msg_manager
<con_msg_man_type> endpoint_manager_type;
endpoint_manager_type em;
con_msg_man_type::ptr manager = em.get_manager();
message_type::ptr msg = manager->get_message(websocketpp::frame::opcode::TEXT,512);
BOOST_CHECK(msg);
BOOST_CHECK(msg->m_opcode == websocketpp::frame::opcode::TEXT);
BOOST_CHECK(msg->m_manager.lock() == manager);
BOOST_CHECK(msg->m_size == 512);
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2012, 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 message
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/message_buffer/message.hpp>
template <typename message>
struct stub {
typedef websocketpp::lib::weak_ptr<stub> weak_ptr;
typedef websocketpp::lib::shared_ptr<stub> ptr;
stub() : recycled(false) {}
bool recycle(message * msg) {
this->recycled = true;
return false;
}
bool recycled;
};
BOOST_AUTO_TEST_CASE( basic_size_check ) {
typedef websocketpp::message_buffer::message<stub> message_type;
typedef stub<message_type> stub_type;
stub_type::ptr s(new stub_type());
message_type::ptr msg(new message_type(s,websocketpp::frame::opcode::TEXT,500));
BOOST_CHECK(msg->get_payload().capacity() >= 500);
}
BOOST_AUTO_TEST_CASE( recycle ) {
typedef websocketpp::message_buffer::message<stub> message_type;
typedef stub<message_type> stub_type;
stub_type::ptr s(new stub_type());
message_type::ptr msg(new message_type(s,websocketpp::frame::opcode::TEXT,500));
BOOST_CHECK(s->recycled == false);
BOOST_CHECK(msg->recycle() == false);
BOOST_CHECK(s->recycled == true);
}

View File

@@ -0,0 +1,156 @@
/*
* 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_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>
BOOST_AUTO_TEST_CASE( exact_match ) {
websocketpp::http::parser::request r;
websocketpp::http::parser::response response;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","WjN}|M(6");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(p.validate_handshake(r));
websocketpp::uri_ptr u;
bool exception;
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == false);
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-Origin") == "http://example.com");
BOOST_CHECK(response.get_header("Sec-WebSocket-Location") == "ws://www.example.com/");
BOOST_CHECK(response.get_header("Sec-WebSocket-Key3") == "n`9eBk9z$R8pOtVb");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( bad_host ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
websocketpp::uri_ptr u;
bool exception = false;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == true);
}

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','regex','system'],env) + [platform_libs]
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]
# 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) 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 extension_permessage_compress
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/extensions/permessage_compress/enabled.hpp>
struct config {
typedef websocketpp::http::parser::request request_type;
};
typedef websocketpp::extensions::permessage_compress::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_compress::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;
*/
}

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

@@ -0,0 +1,185 @@
/*
* 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_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;
};
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;
websocketpp::processor::hybi00<stub_config> p(false,true,msg_manager);
websocketpp::lib::error_code ec;
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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","WjN}|M(6");
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;
bool exception = false;
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == false);
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-Origin") == "http://example.com");
BOOST_CHECK(response.get_header("Sec-WebSocket-Location") == "ws://www.example.com/");
BOOST_CHECK(response.get_header("Sec-WebSocket-Key3") == "n`9eBk9z$R8pOtVb");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
stub_config::request_type r;
stub_config::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi00<stub_config> p(false,true,msg_manager);
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-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi00<stub_config> p(false,true,msg_manager);
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-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi00<stub_config> p(false,true,msg_manager);
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-Key1: 3e6b263 4 17 80\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi00<stub_config> p(false,true,msg_manager);
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-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi00<stub_config> p(false,true,msg_manager);
websocketpp::uri_ptr u;
bool exception = false;
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\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";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
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 );
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == true);
}

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

@@ -0,0 +1,195 @@
/*
* 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_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_compress/disabled.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;
/// Extension related config
static const bool enable_extensions = false;
/// Extension specific config
/// permessage_compress_config
struct permessage_compress_config {
typedef request_type request_type;
};
typedef websocketpp::extensions::permessage_compress::disabled
<permessage_compress_config> permessage_compress_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;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager);
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;
bool exception = false;
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == false);
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager);
websocketpp::uri_ptr u;
bool exception = false;
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 );
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == true);
}

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

@@ -0,0 +1,196 @@
/*
* 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_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_compress/disabled.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;
/// Extension related config
static const bool enable_extensions = false;
/// Extension specific config
/// permessage_compress_config
struct permessage_compress_config {
typedef request_type request_type;
};
typedef websocketpp::extensions::permessage_compress::disabled
<permessage_compress_config> permessage_compress_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;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager);
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;
bool exception = false;
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == false);
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager);
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;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager);
websocketpp::uri_ptr u;
bool exception = false;
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 );
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == true);
}

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

@@ -0,0 +1,594 @@
/*
* 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_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/extensions/permessage_compress/disabled.hpp>
#include <websocketpp/extensions/permessage_compress/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;
struct permessage_compress_config {
typedef request_type request_type;
};
typedef websocketpp::extensions::permessage_compress::disabled
<permessage_compress_config> permessage_compress_type;
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;
struct permessage_compress_config {
typedef request_type request_type;
};
typedef websocketpp::extensions::permessage_compress::enabled
<permessage_compress_config> permessage_compress_type;
static const bool enable_extensions = false;
};
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;
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
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: 13\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;
bool exception = false;
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == false);
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::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
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: 13\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;
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
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: 13\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;
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
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: 13\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;
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
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: 13\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;
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
websocketpp::uri_ptr u;
bool exception = false;
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: 13\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 );
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == true);
}
// 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 ) {
typedef stub_config::con_msg_manager_type con_msg_manager_type;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
uint8_t frame[2] = {0x82, 0x00};
websocketpp::lib::error_code ec;
// all in one chunk
websocketpp::processor::hybi13<stub_config> p1(false,false,msg_manager);
size_t ret1 = p1.consume(frame,2,ec);
BOOST_CHECK( ret1 == 2 );
BOOST_CHECK( !ec );
BOOST_CHECK( p1.ready() == true );
// two separate chunks
websocketpp::processor::hybi13<stub_config> p2(false,false,msg_manager);
BOOST_CHECK( p2.consume(frame,1,ec) == 1 );
BOOST_CHECK( !ec );
BOOST_CHECK( p2.ready() == false );
BOOST_CHECK( p2.consume(frame+1,1,ec) == 1 );
BOOST_CHECK( !ec );
BOOST_CHECK( p2.ready() == true );
}
BOOST_AUTO_TEST_CASE( frame_small_binary_unmasked ) {
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
uint8_t frame[4] = {0x82, 0x02, 0x2A, 0x2A};
websocketpp::lib::error_code ec;
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,4,ec) == 4 );
BOOST_CHECK( !ec );
BOOST_CHECK( p.ready() == true );
message_ptr foo = p.get_message();
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( foo->get_payload() == "**" );
}
BOOST_AUTO_TEST_CASE( frame_extended_binary_unmasked ) {
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
websocketpp::lib::error_code ec;
frame[0] = 0x82;
frame[1] = 0x7E;
frame[2] = 0x00;
frame[3] = 0x7E;
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,130,ec) == 130 );
BOOST_CHECK( !ec );
BOOST_CHECK( p.ready() == true );
message_ptr foo = p.get_message();
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( foo->get_payload().size() == 126 );
}
BOOST_AUTO_TEST_CASE( frame_jumbo_binary_unmasked ) {
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
websocketpp::lib::error_code ec;
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,130,ec) == 130 );
BOOST_CHECK( !ec );
BOOST_CHECK( p.ready() == true );
message_ptr foo = p.get_message();
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( foo->get_payload().size() == 126 );
}
BOOST_AUTO_TEST_CASE( control_frame_too_large ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
uint8_t frame[130] = {0x88, 0x7E, 0x00, 0x7E};
websocketpp::lib::error_code ec;
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,130,ec) > 0 );
BOOST_CHECK( ec == websocketpp::processor::error::control_too_big );
BOOST_CHECK( p.ready() == false );
}
BOOST_AUTO_TEST_CASE( rsv_bits_used ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
uint8_t frame[3][2] = {{0x90, 0x00},
{0xA0, 0x00},
{0xC0, 0x00}};
websocketpp::lib::error_code ec;
for (int i = 0; i < 3; i++) {
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame[i],2,ec) > 0 );
BOOST_CHECK( ec == websocketpp::processor::error::invalid_rsv_bit );
BOOST_CHECK( p.ready() == false );
}
}
BOOST_AUTO_TEST_CASE( reserved_opcode_used ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
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}};
websocketpp::lib::error_code ec;
for (int i = 0; i < 10; i++) {
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame[i],2,ec) > 0 );
BOOST_CHECK( ec == websocketpp::processor::error::invalid_opcode );
BOOST_CHECK( p.ready() == false );
}
}
BOOST_AUTO_TEST_CASE( fragmented_control_message ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
uint8_t frame[2] = {0x08, 0x00};
websocketpp::lib::error_code ec;
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,2,ec) > 0 );
BOOST_CHECK( ec == websocketpp::processor::error::fragmented_control );
BOOST_CHECK( p.ready() == false );
}
BOOST_AUTO_TEST_CASE( fragmented_binary_message ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p0(false,false,msg_manager);
websocketpp::processor::hybi13<stub_config> p1(false,false,msg_manager);
uint8_t frame0[6] = {0x02, 0x01, 0x2A, 0x80, 0x01, 0x2A};
uint8_t frame1[8] = {0x02, 0x01, 0x2A, 0x89, 0x00, 0x80, 0x01, 0x2A};
websocketpp::lib::error_code ec;
// read fragmented message in one chunk
BOOST_CHECK( p0.get_message() == message_ptr() );
BOOST_CHECK( p0.consume(frame0,6,ec) == 6 );
BOOST_CHECK( !ec );
BOOST_CHECK( p0.ready() == true );
BOOST_CHECK( p0.get_message()->get_payload() == "**" );
// read fragmented message in two chunks
BOOST_CHECK( p0.get_message() == message_ptr() );
BOOST_CHECK( p0.consume(frame0,3,ec) == 3 );
BOOST_CHECK( !ec );
BOOST_CHECK( p0.ready() == false );
BOOST_CHECK( p0.consume(frame0+3,3,ec) == 3 );
BOOST_CHECK( !ec );
BOOST_CHECK( p0.ready() == true );
BOOST_CHECK( p0.get_message()->get_payload() == "**" );
// read fragmented message with control message in between
BOOST_CHECK( p0.get_message() == message_ptr() );
BOOST_CHECK( p0.consume(frame1,8,ec) == 5 );
BOOST_CHECK( !ec );
BOOST_CHECK( p0.ready() == true );
BOOST_CHECK( p0.get_message()->get_opcode() == websocketpp::frame::opcode::PING);
BOOST_CHECK( p0.consume(frame1+5,3,ec) == 3 );
BOOST_CHECK( !ec );
BOOST_CHECK( p0.ready() == true );
BOOST_CHECK( p0.get_message()->get_payload() == "**" );
// read lone continuation frame
BOOST_CHECK( p0.get_message() == message_ptr() );
BOOST_CHECK( p0.consume(frame0+3,3,ec) > 0);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_continuation );
// read two start frames in a row
BOOST_CHECK( p1.get_message() == message_ptr() );
BOOST_CHECK( p1.consume(frame0,3,ec) == 3);
BOOST_CHECK( !ec );
BOOST_CHECK( p1.consume(frame0,3,ec) > 0);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_continuation );
}
BOOST_AUTO_TEST_CASE( unmasked_client_frame ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
uint8_t frame[2] = {0x82, 0x00};
websocketpp::lib::error_code ec;
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,2,ec) > 0 );
BOOST_CHECK( ec == websocketpp::processor::error::masking_required );
BOOST_CHECK( p.ready() == false );
}
BOOST_AUTO_TEST_CASE( masked_server_frame ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,false,msg_manager);
uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
websocketpp::lib::error_code ec;
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,8,ec) > 0 );
BOOST_CHECK( ec == websocketpp::processor::error::masking_forbidden );
BOOST_CHECK( p.ready() == false );
}
BOOST_AUTO_TEST_CASE( frame_small_binary_masked ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
websocketpp::lib::error_code ec;
BOOST_CHECK( p.get_message() == message_ptr() );
BOOST_CHECK( p.consume(frame,8,ec) == 8 );
BOOST_CHECK( !ec );
BOOST_CHECK( p.ready() == true );
BOOST_CHECK( p.get_message()->get_payload() == "**" );
}
BOOST_AUTO_TEST_CASE( masked_fragmented_binary_message ) {
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p0(false,true,msg_manager);
uint8_t frame0[14] = {0x02, 0x81, 0xAB, 0x23, 0x98, 0x45, 0x81,
0x80, 0x81, 0xB8, 0x34, 0x12, 0xFF, 0x92};
websocketpp::lib::error_code ec;
// read fragmented message in one chunk
BOOST_CHECK( p0.get_message() == message_ptr() );
BOOST_CHECK( p0.consume(frame0,14,ec) == 14 );
BOOST_CHECK( !ec );
BOOST_CHECK( p0.ready() == true );
BOOST_CHECK( p0.get_message()->get_payload() == "**" );
}
BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
// setup
using namespace websocketpp::processor;
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
con_msg_manager_type::ptr msg_manager(new con_msg_manager_type());
websocketpp::processor::hybi13<stub_config> p(false,true,msg_manager);
// test
websocketpp::lib::error_code e;
message_ptr in = msg_manager->get_message();
message_ptr out = msg_manager->get_message();
message_ptr invalid;
// empty pointers arguements should return sane error
e = p.prepare_data_frame(invalid,invalid);
BOOST_CHECK( e == websocketpp::processor::error::invalid_arguments );
e = p.prepare_data_frame(in,invalid);
BOOST_CHECK( e == websocketpp::processor::error::invalid_arguments );
e = p.prepare_data_frame(invalid,out);
BOOST_CHECK( e == 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));
e = p.prepare_data_frame(in,out);
if (websocketpp::frame::opcode::is_control(in->get_opcode())) {
BOOST_CHECK( e == websocketpp::processor::error::invalid_opcode );
} else {
BOOST_CHECK( e != websocketpp::processor::error::invalid_opcode );
}
}
//in.set_payload("foo");
//e = prepare_data_frame(in,out);
}

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);
}

View File

@@ -0,0 +1,25 @@
## asio 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('base_boost.o', ["base.cpp"], LIBS = BOOST_LIBS)
#objs += env.Object('utilities_boost.o', ["utilities.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_base_boost', ["base_boost.o"], LIBS = BOOST_LIBS)
#prgs += env.Program('test_utility_boost', ["utilities_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('base_stl.o', ["base.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_base_stl', ["base_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -24,28 +24,26 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_asio_base
#include <boost/test/unit_test.hpp>
#include <iostream>
#include "../../src/uri.hpp"
#include <websocketpp/transport/asio/base.hpp>
int main() {
boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
long m = 100000;
long n = 3;
for (long i = 0; i < m; i++) {
websocketpp::uri uri1("wss://thor-websocket.zaphoyd.net:9002/foo/bar/baz?a=b&c=d");
websocketpp::uri uri2("ws://[::1]");
websocketpp::uri uri3("ws://localhost:9000/chat");
}
boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_period period(start,end);
int ms = period.length().total_milliseconds();
std::cout << "Created " << m*n << " URIs in " << ms << "ms" << " (" << (m*n)/(ms/1000.0) << "/s)" << std::endl;
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::generic;
websocketpp::lib::error_code ec = make_error_code(generic);
BOOST_CHECK( ec == generic );
BOOST_CHECK( ec.value() == 1 );
}

32
test/utility/SConscript Normal file
View File

@@ -0,0 +1,32 @@
## utility 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','regex','system'],env) + [platform_libs]
objs = env.Object('uri_boost.o', ["uri.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('utilities_boost.o', ["utilities.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('close_boost.o', ["close.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_uri_boost', ["uri_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_utility_boost', ["utilities_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_frame', ["frame.cpp"], LIBS = BOOST_LIBS)
prgs += env.Program('test_close_boost', ["close_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('utilities_stl.o', ["utilities.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('uri_stl.o', ["uri.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('close_stl.o', ["close.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_utility_stl', ["utilities_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_uri_stl', ["uri_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_close_stl', ["close_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

127
test/utility/close.cpp Normal file
View File

@@ -0,0 +1,127 @@
/*
* Copyright (c) 2012, 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 close
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/close.hpp>
#include <websocketpp/utilities.hpp>
using namespace websocketpp;
BOOST_AUTO_TEST_CASE( reserved_values ) {
BOOST_CHECK( !close::status::reserved(999) );
BOOST_CHECK( close::status::reserved(1004) );
BOOST_CHECK( close::status::reserved(1012) );
BOOST_CHECK( close::status::reserved(1013) );
BOOST_CHECK( close::status::reserved(1014) );
BOOST_CHECK( close::status::reserved(1016) );
BOOST_CHECK( close::status::reserved(2999) );
BOOST_CHECK( !close::status::reserved(1000) );
}
BOOST_AUTO_TEST_CASE( invalid_values ) {
BOOST_CHECK( close::status::invalid(0) );
BOOST_CHECK( close::status::invalid(999) );
BOOST_CHECK( !close::status::invalid(1000) );
BOOST_CHECK( close::status::invalid(1005) );
BOOST_CHECK( close::status::invalid(1006) );
BOOST_CHECK( close::status::invalid(1015) );
BOOST_CHECK( !close::status::invalid(2999) );
BOOST_CHECK( !close::status::invalid(3000) );
BOOST_CHECK( close::status::invalid(5000) );
}
BOOST_AUTO_TEST_CASE( value_extraction ) {
lib::error_code ec;
std::string payload = "oo";
// Value = 1000
payload[0] = 0x03;
payload[1] = 0xe8;
BOOST_CHECK( close::extract_code(payload,ec) == close::status::normal );
BOOST_CHECK( !ec );
// Value = 1004
payload[0] = 0x03;
payload[1] = 0xec;
BOOST_CHECK( close::extract_code(payload,ec) == 1004 );
BOOST_CHECK( ec == error::reserved_close_code );
// Value = 1005
payload[0] = 0x03;
payload[1] = 0xed;
BOOST_CHECK( close::extract_code(payload,ec) == close::status::no_status );
BOOST_CHECK( ec == error::invalid_close_code );
// Value = 3000
payload[0] = 0x0b;
payload[1] = 0xb8;
BOOST_CHECK( close::extract_code(payload,ec) == 3000 );
BOOST_CHECK( !ec );
}
BOOST_AUTO_TEST_CASE( extract_empty ) {
lib::error_code ec;
std::string payload = "";
BOOST_CHECK( close::extract_code(payload,ec) == close::status::no_status );
BOOST_CHECK( !ec );
}
BOOST_AUTO_TEST_CASE( extract_short ) {
lib::error_code ec;
std::string payload = "0";
BOOST_CHECK( close::extract_code(payload,ec) == close::status::protocol_error );
BOOST_CHECK( ec == error::bad_close_code );
}
BOOST_AUTO_TEST_CASE( extract_reason ) {
lib::error_code ec;
std::string payload = "00Foo";
BOOST_CHECK( close::extract_reason(payload,ec) == "Foo" );
BOOST_CHECK( !ec );
payload = "";
BOOST_CHECK( close::extract_reason(payload,ec) == "" );
BOOST_CHECK( !ec );
payload = "00";
BOOST_CHECK( close::extract_reason(payload,ec) == "" );
BOOST_CHECK( !ec );
payload = "000";
payload[2] = 0xFF;
close::extract_reason(payload,ec);
BOOST_CHECK( ec == error::invalid_utf8 );
}

465
test/utility/frame.cpp Normal file
View File

@@ -0,0 +1,465 @@
/*
* 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 frame
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/frame.hpp>
#include <websocketpp/utilities.hpp>
using namespace websocketpp;
BOOST_AUTO_TEST_CASE( basic_bits ) {
frame::basic_header h1(0x00,0x00); // all false
frame::basic_header h2(0xF0,0x80); // all true
// Read Values
BOOST_CHECK( frame::get_fin(h1) == false );
BOOST_CHECK( frame::get_rsv1(h1) == false );
BOOST_CHECK( frame::get_rsv2(h1) == false );
BOOST_CHECK( frame::get_rsv3(h1) == false );
BOOST_CHECK( frame::get_masked(h1) == false );
BOOST_CHECK( frame::get_fin(h2) == true );
BOOST_CHECK( frame::get_rsv1(h2) == true );
BOOST_CHECK( frame::get_rsv2(h2) == true );
BOOST_CHECK( frame::get_rsv3(h2) == true );
BOOST_CHECK( frame::get_masked(h2) == true );
// Set Values
frame::set_fin(h1,true);
BOOST_CHECK( h1.b0 == 0x80 );
frame::set_rsv1(h1,true);
BOOST_CHECK( h1.b0 == 0xC0 );
frame::set_rsv2(h1,true);
BOOST_CHECK( h1.b0 == 0xE0 );
frame::set_rsv3(h1,true);
BOOST_CHECK( h1.b0 == 0xF0 );
frame::set_masked(h1,true);
BOOST_CHECK( h1.b1 == 0x80 );
}
BOOST_AUTO_TEST_CASE( basic_constructors ) {
// Read Values
frame::basic_header h1(frame::opcode::TEXT,12,true,false);
BOOST_CHECK( frame::get_opcode(h1) == frame::opcode::TEXT );
BOOST_CHECK( frame::get_basic_size(h1) == 12 );
BOOST_CHECK( frame::get_fin(h1) == true );
BOOST_CHECK( frame::get_rsv1(h1) == false );
BOOST_CHECK( frame::get_rsv2(h1) == false );
BOOST_CHECK( frame::get_rsv3(h1) == false );
BOOST_CHECK( frame::get_masked(h1) == false );
frame::basic_header h2(frame::opcode::BINARY,0,false,false,false,true);
BOOST_CHECK( frame::get_opcode(h2) == frame::opcode::BINARY );
BOOST_CHECK( frame::get_basic_size(h2) == 0 );
BOOST_CHECK( frame::get_fin(h2) == false );
BOOST_CHECK( frame::get_rsv1(h2) == false );
BOOST_CHECK( frame::get_rsv2(h2) == true );
BOOST_CHECK( frame::get_rsv3(h2) == false );
BOOST_CHECK( frame::get_masked(h2) == false );
}
BOOST_AUTO_TEST_CASE( basic_size ) {
frame::basic_header h1(0x00,0x00); // length 0
frame::basic_header h2(0x00,0x01); // length 1
frame::basic_header h3(0x00,0x7D); // length 125
frame::basic_header h4(0x00,0x7E); // length 126
frame::basic_header h5(0x00,0x7F); // length 127
frame::basic_header h6(0x00,0x80); // length 0, mask bit set
BOOST_CHECK( frame::get_basic_size(h1) == 0 );
BOOST_CHECK( frame::get_basic_size(h2) == 1 );
BOOST_CHECK( frame::get_basic_size(h3) == 125 );
BOOST_CHECK( frame::get_basic_size(h4) == 126 );
BOOST_CHECK( frame::get_basic_size(h5) == 127 );
BOOST_CHECK( frame::get_basic_size(h6) == 0 );
/*frame::set_basic_size(h1,1);
BOOST_CHECK( h1.b1 == 0x01 );
frame::set_basic_size(h1,125);
BOOST_CHECK( h1.b1 == 0x7D );
frame::set_basic_size(h1,126);
BOOST_CHECK( h1.b1 == 0x7E );
frame::set_basic_size(h1,127);
BOOST_CHECK( h1.b1 == 0x7F );
frame::set_basic_size(h1,0);
BOOST_CHECK( h1.b1 == 0x00 );*/
}
BOOST_AUTO_TEST_CASE( basic_header_length ) {
frame::basic_header h1(0x82,0x00); // short binary frame, unmasked
frame::basic_header h2(0x82,0x80); // short binary frame, masked
frame::basic_header h3(0x82,0x7E); // medium binary frame, unmasked
frame::basic_header h4(0x82,0xFE); // medium binary frame, masked
frame::basic_header h5(0x82,0x7F); // jumbo binary frame, unmasked
frame::basic_header h6(0x82,0xFF); // jumbo binary frame, masked
BOOST_CHECK( frame::get_header_len(h1) == 2);
BOOST_CHECK( frame::get_header_len(h2) == 6);
BOOST_CHECK( frame::get_header_len(h3) == 4);
BOOST_CHECK( frame::get_header_len(h4) == 8);
BOOST_CHECK( frame::get_header_len(h5) == 10);
BOOST_CHECK( frame::get_header_len(h6) == 14);
}
BOOST_AUTO_TEST_CASE( basic_opcode ) {
frame::basic_header h1(0x00,0x00);
BOOST_CHECK( is_control(frame::opcode::CONTINUATION) == false);
BOOST_CHECK( is_control(frame::opcode::TEXT) == false);
BOOST_CHECK( is_control(frame::opcode::BINARY) == false);
BOOST_CHECK( is_control(frame::opcode::CLOSE) == true);
BOOST_CHECK( is_control(frame::opcode::PING) == true);
BOOST_CHECK( is_control(frame::opcode::PONG) == true);
BOOST_CHECK( frame::get_opcode(h1) == frame::opcode::CONTINUATION );
}
BOOST_AUTO_TEST_CASE( extended_header_basics ) {
frame::extended_header h1;
uint8_t h1_solution[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
frame::extended_header h2(uint16_t(255));
uint8_t h2_solution[12] = {0x00, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
frame::extended_header h3(uint16_t(256),htonl(0x8040201));
uint8_t h3_solution[12] = {0x01, 0x00, 0x08, 0x04, 0x02, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
frame::extended_header h4(uint64_t(0x0807060504030201LL));
uint8_t h4_solution[12] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
0x02, 0x01, 0x00, 0x00, 0x00, 0x00};
frame::extended_header h5(uint64_t(0x0807060504030201LL),htonl(0x8040201));
uint8_t h5_solution[12] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
0x02, 0x01, 0x08, 0x04, 0x02, 0x01};
BOOST_CHECK( std::equal(h1_solution,h1_solution+12,h1.bytes) );
BOOST_CHECK( std::equal(h2_solution,h2_solution+12,h2.bytes) );
BOOST_CHECK( std::equal(h3_solution,h3_solution+12,h3.bytes) );
BOOST_CHECK( std::equal(h4_solution,h4_solution+12,h4.bytes) );
BOOST_CHECK( std::equal(h5_solution,h5_solution+12,h5.bytes) );
}
BOOST_AUTO_TEST_CASE( extended_header_extractors ) {
frame::basic_header h1(0x00,0x7E);
frame::extended_header e1(uint16_t(255));
BOOST_CHECK( get_extended_size(e1) == 255 );
BOOST_CHECK( get_payload_size(h1,e1) == 255 );
BOOST_CHECK( get_masking_key_offset(h1) == 2 );
BOOST_CHECK( get_masking_key(h1,e1).i == 0 );
frame::basic_header h2(0x00,0x7F);
frame::extended_header e2(uint64_t(0x0807060504030201LL));
BOOST_CHECK( get_jumbo_size(e2) == 0x0807060504030201LL );
BOOST_CHECK( get_payload_size(h2,e2) == 0x0807060504030201LL );
BOOST_CHECK( get_masking_key_offset(h2) == 8 );
BOOST_CHECK( get_masking_key(h2,e2).i == 0 );
frame::basic_header h3(0x00,0xFE);
frame::extended_header e3(uint16_t(255),0x08040201);
BOOST_CHECK( get_extended_size(e3) == 255 );
BOOST_CHECK( get_payload_size(h3,e3) == 255 );
BOOST_CHECK( get_masking_key_offset(h3) == 2 );
BOOST_CHECK( get_masking_key(h3,e3).i == 0x08040201 );
frame::basic_header h4(0x00,0xFF);
frame::extended_header e4(uint64_t(0x0807060504030201LL),0x08040201);
BOOST_CHECK( get_jumbo_size(e4) == 0x0807060504030201LL );
BOOST_CHECK( get_payload_size(h4,e4) == 0x0807060504030201LL );
BOOST_CHECK( get_masking_key_offset(h4) == 8 );
BOOST_CHECK( get_masking_key(h4,e4).i == 0x08040201 );
frame::basic_header h5(0x00,0x7D);
frame::extended_header e5;
BOOST_CHECK( get_payload_size(h5,e5) == 125 );
}
BOOST_AUTO_TEST_CASE( header_preparation ) {
frame::basic_header h1(0x81,0xFF); //
frame::extended_header e1(uint64_t(0xFFFFFLL),htonl(0xD5FB70EE));
std::string p1 = prepare_header(h1, e1);
uint8_t s1[14] = {0x81, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF,
0xD5, 0xFB, 0x70, 0xEE};
BOOST_CHECK( p1.size() == 14);
BOOST_CHECK( std::equal(p1.begin(),p1.end(),reinterpret_cast<char*>(s1)) );
frame::basic_header h2(0x81,0x7E); //
frame::extended_header e2(uint16_t(255));
std::string p2 = prepare_header(h2, e2);
uint8_t s2[4] = {0x81, 0x7E, 0x00, 0xFF};
BOOST_CHECK( p2.size() == 4);
BOOST_CHECK( std::equal(p2.begin(),p2.end(),reinterpret_cast<char*>(s2)) );
}
BOOST_AUTO_TEST_CASE( prepare_masking_key ) {
frame::masking_key_type key;
key.i = htonl(0x12345678);
if (sizeof(size_t) == 8) {
BOOST_CHECK(
frame::prepare_masking_key(key) == utility::htonll(0x1234567812345678)
);
} else {
BOOST_CHECK( frame::prepare_masking_key(key) == htonl(0x12345678) );
}
}
BOOST_AUTO_TEST_CASE( prepare_masking_key2 ) {
frame::masking_key_type key;
key.i = htonl(0xD5FB70EE);
// One call
if (sizeof(size_t) == 8) {
BOOST_CHECK(
frame::prepare_masking_key(key) == utility::htonll(0xD5FB70EED5FB70EE)
);
} else {
BOOST_CHECK( frame::prepare_masking_key(key) == htonl(0xD5FB70EE) );
}
}
// TODO: figure out a way to run/test both 4 and 8 byte versions.
BOOST_AUTO_TEST_CASE( circshift ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
BOOST_CHECK( frame::circshift_prepared_key(test,0) == 0x0123456789abcdef);
BOOST_CHECK( frame::circshift_prepared_key(test,1) == 0xef0123456789abcd);
BOOST_CHECK( frame::circshift_prepared_key(test,2) == 0xcdef0123456789ab);
BOOST_CHECK( frame::circshift_prepared_key(test,3) == 0xabcdef0123456789);
} else {
size_t test = 0x01234567;
BOOST_CHECK( frame::circshift_prepared_key(test,0) == 0x01234567);
BOOST_CHECK( frame::circshift_prepared_key(test,1) == 0x67012345);
BOOST_CHECK( frame::circshift_prepared_key(test,2) == 0x45670123);
BOOST_CHECK( frame::circshift_prepared_key(test,3) == 0x23456701);
}
}
BOOST_AUTO_TEST_CASE( block_byte_mask ) {
uint8_t input[15] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
uint8_t output[15];
uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02};
frame::masking_key_type key;
key.c[0] = 0x00;
key.c[1] = 0x01;
key.c[2] = 0x02;
key.c[3] = 0x03;
byte_mask(input,input+15,output,key);
BOOST_CHECK( std::equal(output,output+15,masked) );
}
BOOST_AUTO_TEST_CASE( block_byte_mask_inplace ) {
uint8_t buffer[15] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02};
frame::masking_key_type key;
key.c[0] = 0x00;
key.c[1] = 0x01;
key.c[2] = 0x02;
key.c[3] = 0x03;
byte_mask(buffer,buffer+15,key);
BOOST_CHECK( std::equal(buffer,buffer+15,masked) );
}
BOOST_AUTO_TEST_CASE( block_word_mask ) {
uint8_t input[15] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
uint8_t output[15];
uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02};
frame::masking_key_type key;
key.c[0] = 0x00;
key.c[1] = 0x01;
key.c[2] = 0x02;
key.c[3] = 0x03;
word_mask_exact(input,output,15,key);
BOOST_CHECK( std::equal(output,output+15,masked) );
}
BOOST_AUTO_TEST_CASE( block_word_mask_inplace ) {
uint8_t buffer[15] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
uint8_t masked[15] = {0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02};
frame::masking_key_type key;
key.c[0] = 0x00;
key.c[1] = 0x01;
key.c[2] = 0x02;
key.c[3] = 0x03;
word_mask_exact(buffer,15,key);
BOOST_CHECK( std::equal(buffer,buffer+15,masked) );
}
BOOST_AUTO_TEST_CASE( continuous_word_mask ) {
uint8_t input[16];
uint8_t output[16];
uint8_t masked[16] = {0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x00};
frame::masking_key_type key;
key.c[0] = 0x00;
key.c[1] = 0x01;
key.c[2] = 0x02;
key.c[3] = 0x03;
// One call
size_t pkey,pkey_temp;
pkey = frame::prepare_masking_key(key);
std::fill_n(input,16,0x00);
frame::word_mask_circ(input,output,15,pkey);
BOOST_CHECK( std::equal(output,output+16,masked) );
// calls not split on word boundaries
pkey = frame::prepare_masking_key(key);
std::fill_n(input,16,0x00);
pkey_temp = frame::word_mask_circ(input,output,7,pkey);
BOOST_CHECK( std::equal(output,output+7,masked) );
BOOST_CHECK( pkey_temp == frame::circshift_prepared_key(pkey,3) );
pkey_temp = frame::word_mask_circ(input+7,output+7,8,pkey_temp);
BOOST_CHECK( std::equal(output,output+16,masked) );
BOOST_CHECK( pkey_temp == frame::circshift_prepared_key(pkey,3) );
}
BOOST_AUTO_TEST_CASE( continuous_word_mask_inplace ) {
uint8_t buffer[16];
uint8_t masked[16] = {0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x00};
frame::masking_key_type key;
key.c[0] = 0x00;
key.c[1] = 0x01;
key.c[2] = 0x02;
key.c[3] = 0x03;
// One call
size_t pkey,pkey_temp;
pkey = frame::prepare_masking_key(key);
std::fill_n(buffer,16,0x00);
frame::word_mask_circ(buffer,15,pkey);
BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
// calls not split on word boundaries
pkey = frame::prepare_masking_key(key);
std::fill_n(buffer,16,0x00);
pkey_temp = frame::word_mask_circ(buffer,7,pkey);
BOOST_CHECK( std::equal(buffer,buffer+7,masked) );
BOOST_CHECK( pkey_temp == frame::circshift_prepared_key(pkey,3) );
pkey_temp = frame::word_mask_circ(buffer+7,8,pkey_temp);
BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
BOOST_CHECK( pkey_temp == frame::circshift_prepared_key(pkey,3) );
}
BOOST_AUTO_TEST_CASE( continuous_word_mask2 ) {
uint8_t buffer[12] = {0xA6, 0x15, 0x97, 0xB9,
0x81, 0x50, 0xAC, 0xBA,
0x9C, 0x1C, 0x9F, 0xF4};
uint8_t unmasked[12] = {0x48, 0x65, 0x6C, 0x6C,
0x6F, 0x20, 0x57, 0x6F,
0x72, 0x6C, 0x64, 0x21};
frame::masking_key_type key;
key.c[0] = 0xEE;
key.c[1] = 0x70;
key.c[2] = 0xFB;
key.c[3] = 0xD5;
// One call
size_t pkey;
pkey = frame::prepare_masking_key(key);
frame::word_mask_circ(buffer,12,pkey);
BOOST_CHECK( std::equal(buffer,buffer+12,unmasked) );
}

View File

@@ -29,8 +29,9 @@
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include "../../src/uri.hpp"
#include <websocketpp/uri.hpp>
// Test a regular valid ws URI
BOOST_AUTO_TEST_CASE( uri_valid ) {
@@ -42,7 +43,7 @@ BOOST_AUTO_TEST_CASE( uri_valid ) {
BOOST_CHECK( uri.get_host() == "localhost");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/chat" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -59,7 +60,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_no_port_unsecure ) {
BOOST_CHECK( uri.get_host() == "localhost");
BOOST_CHECK( uri.get_port() == 80 );
BOOST_CHECK( uri.get_resource() == "/chat" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -96,7 +97,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_no_resource ) {
BOOST_CHECK( uri.get_host() == "localhost");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -113,7 +114,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_ipv6_literal ) {
BOOST_CHECK( uri.get_host() == "::1");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/chat" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -132,7 +133,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_2 ) {
BOOST_CHECK( uri.get_host() == "thor-websocket.zaphoyd.net");
BOOST_CHECK( uri.get_port() == 88 );
BOOST_CHECK( uri.get_resource() == "/" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -145,7 +146,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_long_port ) {
bool exception = false;
try {
websocketpp::uri uri("wss://localhost:900000/chat");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -157,7 +158,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_http ) {
bool exception = false;
try {
websocketpp::uri uri("http://localhost:9000/chat");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -174,7 +175,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_ipv4_literal ) {
BOOST_CHECK( uri.get_host() == "127.0.0.1");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/chat" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -191,7 +192,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_3 ) {
BOOST_CHECK( uri.get_host() == "localhost");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/chat/foo/bar" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -203,7 +204,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_method_separator ) {
bool exception = false;
try {
websocketpp::uri uri("wss:/localhost:9000/chat");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -215,7 +216,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_gt_16_bit_port ) {
bool exception = false;
try {
websocketpp::uri uri("wss:/localhost:70000/chat");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -227,7 +228,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_fragment ) {
bool exception = false;
try {
websocketpp::uri uri("wss:/localhost:70000/chat#foo");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -239,7 +240,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_bad_v6_literal_1 ) {
bool exception = false;
try {
websocketpp::uri uri("wss://::1/chat");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -251,7 +252,7 @@ BOOST_AUTO_TEST_CASE( uri_invalid_bad_v6_literal_2 ) {
bool exception = false;
try {
websocketpp::uri uri("wss://::1:2009/chat");
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -268,7 +269,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_4 ) {
BOOST_CHECK( uri.get_host() == "localhost");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/chat/foo/bar?foo=bar" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -285,7 +286,7 @@ BOOST_AUTO_TEST_CASE( uri_valid_v4_mapped ) {
BOOST_CHECK( uri.get_host() == "0000:0000:0000:0000:0000:0000:192.168.1.1");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
@@ -302,11 +303,11 @@ BOOST_AUTO_TEST_CASE( uri_valid_v6_mixed_case ) {
BOOST_CHECK( uri.get_host() == "::10aB");
BOOST_CHECK( uri.get_port() == 9000 );
BOOST_CHECK( uri.get_resource() == "/" );
} catch (websocketpp::uri_exception& e) {
} catch (websocketpp::uri_exception&) {
exception = true;
}
BOOST_CHECK( exception == false);
}
// TODO: tests for the other two constructors
// TODO: tests for the other two constructors

View File

@@ -0,0 +1,59 @@
/*
* 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 utility
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/utilities.hpp>
BOOST_AUTO_TEST_SUITE ( utility )
BOOST_AUTO_TEST_CASE( substr_found ) {
std::string haystack = "abc123";
std::string needle = "abc";
BOOST_CHECK(websocketpp::utility::ci_find_substr(haystack,needle) ==haystack.begin());
}
BOOST_AUTO_TEST_CASE( substr_found_ci ) {
std::string haystack = "abc123";
std::string needle = "aBc";
BOOST_CHECK(websocketpp::utility::ci_find_substr(haystack,needle) ==haystack.begin());
}
BOOST_AUTO_TEST_CASE( substr_not_found ) {
std::string haystack = "abd123";
std::string needle = "abcd";
BOOST_CHECK(websocketpp::utility::ci_find_substr(haystack,needle) == haystack.end());
}
BOOST_AUTO_TEST_SUITE_END()