mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Squashed 'src/websocketpp/' content from commit 875d420
git-subtree-dir: src/websocketpp git-subtree-split: 875d420952d2c47d38de15952a401f00773cdffc
This commit is contained in:
27
test/message_buffer/SConscript
Normal file
27
test/message_buffer/SConscript
Normal 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')
|
||||
96
test/message_buffer/alloc.cpp
Normal file
96
test/message_buffer/alloc.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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 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);
|
||||
}
|
||||
|
||||
72
test/message_buffer/message.cpp
Normal file
72
test/message_buffer/message.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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 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 *) {
|
||||
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);
|
||||
}
|
||||
|
||||
156
test/message_buffer/pool.cpp
Normal file
156
test/message_buffer/pool.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user