first commit

This commit is contained in:
sanya
2025-09-01 14:20:39 +00:00
committed by ExternPointer
commit 490fc11f6a
4328 changed files with 1796224 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
# Test close utilities
file (GLOB SOURCE close.cpp)
init_target (test_close)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test error utilities
file (GLOB SOURCE error.cpp)
init_target (test_error)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test frame utilities
file (GLOB SOURCE frame.cpp)
init_target (test_frame)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test sha1 utilities
file (GLOB SOURCE sha1.cpp)
init_target (test_sha1)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test uri utilities
file (GLOB SOURCE uri.cpp)
init_target (test_uri)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test misc utilities
file (GLOB SOURCE utilities.cpp)
init_target (test_utilities)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")

View File

@@ -0,0 +1,40 @@
## 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','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)
objs += env.Object('sha1_boost.o', ["sha1.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('error_boost.o', ["error.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)
prgs += env.Program('test_sha1_boost', ["sha1_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_error_boost', ["error_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)
objs += env_cpp11.Object('sha1_stl.o', ["sha1.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('error_stl.o', ["error.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)
prgs += env_cpp11.Program('test_sha1_stl', ["sha1_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_error_stl', ["error_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,124 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE 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(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] = char(0xe8);
BOOST_CHECK( close::extract_code(payload,ec) == close::status::normal );
BOOST_CHECK( !ec );
// Value = 1004
payload[0] = 0x03;
payload[1] = char(0xec);
BOOST_CHECK( close::extract_code(payload,ec) == 1004 );
BOOST_CHECK( ec == error::reserved_close_code );
// Value = 1005
payload[0] = 0x03;
payload[1] = char(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] = char(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.clear();
BOOST_CHECK( close::extract_reason(payload,ec).empty() );
BOOST_CHECK( !ec );
payload = "00";
BOOST_CHECK( close::extract_reason(payload,ec).empty() );
BOOST_CHECK( !ec );
payload = "000";
payload[2] = char(0xFF);
close::extract_reason(payload,ec);
BOOST_CHECK( ec == error::invalid_utf8 );
}

View File

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

View File

@@ -0,0 +1,538 @@
/*
* 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) == lib::net::_htonll(0x1234567812345678LL)
);
} 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) == lib::net::_htonll(0xD5FB70EED5FB70EELL)
);
} 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);
std::fill_n(output,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);
std::fill_n(output,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_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
}
BOOST_AUTO_TEST_CASE( continuous_byte_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);
std::fill_n(output,16,0x00);
frame::byte_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);
std::fill_n(output,16,0x00);
pkey_temp = frame::byte_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::byte_mask_circ(input+7,output+7,8,pkey_temp);
BOOST_CHECK( std::equal(output,output+16,masked) );
BOOST_CHECK_EQUAL( 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_EQUAL( 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_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
}
BOOST_AUTO_TEST_CASE( continuous_byte_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::byte_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::byte_mask_circ(buffer,7,pkey);
BOOST_CHECK( std::equal(buffer,buffer+7,masked) );
BOOST_CHECK_EQUAL( pkey_temp, frame::circshift_prepared_key(pkey,3) );
pkey_temp = frame::byte_mask_circ(buffer+7,8,pkey_temp);
BOOST_CHECK( std::equal(buffer,buffer+16,masked) );
BOOST_CHECK_EQUAL( 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

@@ -0,0 +1,81 @@
/*
* 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 sha1
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/sha1/sha1.hpp>
#include <websocketpp/utilities.hpp>
BOOST_AUTO_TEST_SUITE ( sha1 )
BOOST_AUTO_TEST_CASE( sha1_test_a ) {
unsigned char hash[20];
unsigned char reference[20] = {0xa9, 0x99, 0x3e, 0x36, 0x47,
0x06, 0x81, 0x6a, 0xba, 0x3e,
0x25, 0x71, 0x78, 0x50, 0xc2,
0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
websocketpp::sha1::calc("abc",3,hash);
BOOST_CHECK_EQUAL_COLLECTIONS(hash, hash+20, reference, reference+20);
}
BOOST_AUTO_TEST_CASE( sha1_test_b ) {
unsigned char hash[20];
unsigned char reference[20] = {0x84, 0x98, 0x3e, 0x44, 0x1c,
0x3b, 0xd2, 0x6e, 0xba, 0xae,
0x4a, 0xa1, 0xf9, 0x51, 0x29,
0xe5, 0xe5, 0x46, 0x70, 0xf1};
websocketpp::sha1::calc(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",56,hash);
BOOST_CHECK_EQUAL_COLLECTIONS(hash, hash+20, reference, reference+20);
}
BOOST_AUTO_TEST_CASE( sha1_test_c ) {
std::string input;
unsigned char hash[20];
unsigned char reference[20] = {0x34, 0xaa, 0x97, 0x3c, 0xd4,
0xc4, 0xda, 0xa4, 0xf6, 0x1e,
0xeb, 0x2b, 0xdb, 0xad, 0x27,
0x31, 0x65, 0x34, 0x01, 0x6f};
for (int i = 0; i < 1000000; i++) {
input += 'a';
}
websocketpp::sha1::calc(input.c_str(),input.size(),hash);
BOOST_CHECK_EQUAL_COLLECTIONS(hash, hash+20, reference, reference+20);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@@ -0,0 +1,246 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE uri
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/uri.hpp>
// Test a regular valid ws URI
BOOST_AUTO_TEST_CASE( uri_valid ) {
websocketpp::uri uri("ws://localhost:9000/chat");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( !uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "ws");
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
BOOST_CHECK_EQUAL( uri.get_query(), "" );
}
// Test a regular valid ws URI
BOOST_AUTO_TEST_CASE( uri_valid_no_port_unsecure ) {
websocketpp::uri uri("ws://localhost/chat");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( !uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "ws");
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 80 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
}
// Valid URI with no port (secure)
BOOST_AUTO_TEST_CASE( uri_valid_no_port_secure ) {
websocketpp::uri uri("wss://localhost/chat");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 443 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
}
// Valid URI with no resource
BOOST_AUTO_TEST_CASE( uri_valid_no_resource ) {
websocketpp::uri uri("wss://localhost:9000");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
}
// Valid URI IPv6 Literal
BOOST_AUTO_TEST_CASE( uri_valid_ipv6_literal ) {
websocketpp::uri uri("wss://[::1]:9000/chat");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
BOOST_CHECK_EQUAL( uri.get_host(), "::1");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
}
// Valid URI with more complicated host
BOOST_AUTO_TEST_CASE( uri_valid_2 ) {
websocketpp::uri uri("wss://thor-websocket.zaphoyd.net:88/");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
BOOST_CHECK_EQUAL( uri.get_host(), "thor-websocket.zaphoyd.net");
BOOST_CHECK_EQUAL( uri.get_port(), 88 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
}
// Invalid URI (port too long)
BOOST_AUTO_TEST_CASE( uri_invalid_long_port ) {
websocketpp::uri uri("wss://localhost:900000/chat");
BOOST_CHECK( !uri.get_valid() );
}
// Invalid URI (bogus scheme method)
BOOST_AUTO_TEST_CASE( uri_invalid_scheme ) {
websocketpp::uri uri("foo://localhost:9000/chat");
BOOST_CHECK( !uri.get_valid() );
}
// Valid URI (http method)
BOOST_AUTO_TEST_CASE( uri_http_scheme ) {
websocketpp::uri uri("http://localhost:9000/chat");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( !uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "http");
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
}
// Valid URI IPv4 literal
BOOST_AUTO_TEST_CASE( uri_valid_ipv4_literal ) {
websocketpp::uri uri("wss://127.0.0.1:9000/chat");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
BOOST_CHECK_EQUAL( uri.get_host(), "127.0.0.1");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
}
// Valid URI complicated resource path
BOOST_AUTO_TEST_CASE( uri_valid_3 ) {
websocketpp::uri uri("wss://localhost:9000/chat/foo/bar");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat/foo/bar" );
}
// Invalid URI broken method separator
BOOST_AUTO_TEST_CASE( uri_invalid_method_separator ) {
websocketpp::uri uri("wss:/localhost:9000/chat");
BOOST_CHECK( !uri.get_valid() );
}
// Invalid URI port > 65535
BOOST_AUTO_TEST_CASE( uri_invalid_gt_16_bit_port ) {
websocketpp::uri uri("wss:/localhost:70000/chat");
BOOST_CHECK( !uri.get_valid() );
}
// Invalid URI includes uri fragment
BOOST_AUTO_TEST_CASE( uri_invalid_fragment ) {
websocketpp::uri uri("wss:/localhost:70000/chat#foo");
BOOST_CHECK( !uri.get_valid() );
}
// Invalid URI with no brackets around IPv6 literal
BOOST_AUTO_TEST_CASE( uri_invalid_bad_v6_literal_1 ) {
websocketpp::uri uri("wss://::1/chat");
BOOST_CHECK( !uri.get_valid() );
}
// Invalid URI with port and no brackets around IPv6 literal
BOOST_AUTO_TEST_CASE( uri_invalid_bad_v6_literal_2 ) {
websocketpp::uri uri("wss://::1:2009/chat");
BOOST_CHECK( !uri.get_valid() );
}
// Valid URI complicated resource path with query
BOOST_AUTO_TEST_CASE( uri_valid_4 ) {
websocketpp::uri uri("wss://localhost:9000/chat/foo/bar?foo=bar");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss" );
BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/chat/foo/bar?foo=bar" );
BOOST_CHECK_EQUAL( uri.get_query(), "foo=bar" );
}
// Valid URI with a mapped v4 ipv6 literal
BOOST_AUTO_TEST_CASE( uri_valid_v4_mapped ) {
websocketpp::uri uri("wss://[0000:0000:0000:0000:0000:0000:192.168.1.1]:9000/");
BOOST_CHECK( uri.get_valid() );
BOOST_CHECK( uri.get_secure() );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss" );
BOOST_CHECK_EQUAL( uri.get_host(), "0000:0000:0000:0000:0000:0000:192.168.1.1");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
}
// Valid URI with a v6 address with mixed case
BOOST_AUTO_TEST_CASE( uri_valid_v6_mixed_case ) {
websocketpp::uri uri("wss://[::10aB]:9000/");
BOOST_CHECK( uri.get_valid() == true );
BOOST_CHECK( uri.get_secure() == true );
BOOST_CHECK_EQUAL( uri.get_scheme(), "wss" );
BOOST_CHECK_EQUAL( uri.get_host(), "::10aB");
BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
}
// Valid URI with a v6 address with mixed case
BOOST_AUTO_TEST_CASE( uri_invalid_no_scheme ) {
websocketpp::uri uri("myserver.com");
BOOST_CHECK( !uri.get_valid() );
}
// Invalid IPv6 literal
/*BOOST_AUTO_TEST_CASE( uri_invalid_v6_nonhex ) {
websocketpp::uri uri("wss://[g::1]:9000/");
BOOST_CHECK( uri.get_valid() == false );
}*/
// TODO: tests for the other two constructors

View File

@@ -0,0 +1,73 @@
/*
* 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_CASE( to_lower ) {
std::string in = "AbCd";
BOOST_CHECK_EQUAL(websocketpp::utility::to_lower(in), "abcd");
}
BOOST_AUTO_TEST_CASE( string_replace_all ) {
std::string source = "foo \"bar\" baz";
std::string dest = "foo \\\"bar\\\" baz";
using websocketpp::utility::string_replace_all;
BOOST_CHECK_EQUAL(string_replace_all(source,"\"","\\\""),dest);
}
BOOST_AUTO_TEST_SUITE_END()