Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
hybi08.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27//#define BOOST_TEST_DYN_LINK
28#define BOOST_TEST_MODULE hybi_08_processor
29#include <boost/test/unit_test.hpp>
30
31#include <iostream>
32#include <string>
33
41
42struct stub_config {
45
50
52
53 static const size_t max_message_size = 16000000;
54
56 static const bool enable_extensions = false;
57
59
61 struct permessage_deflate_config {
63 };
64
66 <permessage_deflate_config> permessage_deflate_type;
67};
68
69BOOST_AUTO_TEST_CASE( exact_match ) {
72 stub_config::con_msg_manager_type::ptr msg_manager;
74 websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
75 websocketpp::lib::error_code ec;
76
77 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";
78
79 r.consume(handshake.c_str(),handshake.size());
80
82 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
83 ec = p.validate_handshake(r);
84 BOOST_CHECK(!ec);
85
87
88 u = p.get_uri(r);
89
90 BOOST_CHECK(u->get_valid() == true);
91 BOOST_CHECK(u->get_secure() == false);
92 BOOST_CHECK(u->get_host() == "www.example.com");
93 BOOST_CHECK(u->get_resource() == "/");
94 BOOST_CHECK(u->get_port() == websocketpp::uri_default_port);
95
96 p.process_handshake(r,"",response);
97
98 BOOST_CHECK(response.get_header("Connection") == "upgrade");
99 BOOST_CHECK(response.get_header("Upgrade") == "websocket");
100 BOOST_CHECK(response.get_header("Sec-WebSocket-Accept") == "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
101}
102
103BOOST_AUTO_TEST_CASE( non_get_method ) {
107 stub_config::con_msg_manager_type::ptr msg_manager;
108 websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
109 websocketpp::lib::error_code ec;
110
111 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";
112
113 r.consume(handshake.c_str(),handshake.size());
114
116 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
117 ec = p.validate_handshake(r);
119}
120
121BOOST_AUTO_TEST_CASE( old_http_version ) {
124 stub_config::con_msg_manager_type::ptr msg_manager;
126 websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
127 websocketpp::lib::error_code ec;
128
129 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";
130
131 r.consume(handshake.c_str(),handshake.size());
132
134 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
135 ec = p.validate_handshake(r);
137}
138
139BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
142 stub_config::con_msg_manager_type::ptr msg_manager;
144 websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
145 websocketpp::lib::error_code ec;
146
147 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";
148
149 r.consume(handshake.c_str(),handshake.size());
150
152 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
153 ec = p.validate_handshake(r);
155}
156
157BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
160 stub_config::con_msg_manager_type::ptr msg_manager;
162 websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
163 websocketpp::lib::error_code ec;
164
165 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";
166
167 r.consume(handshake.c_str(),handshake.size());
168
170 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
171 ec = p.validate_handshake(r);
173}
174
178 stub_config::con_msg_manager_type::ptr msg_manager;
180 websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
182 websocketpp::lib::error_code ec;
183
184 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";
185
186 r.consume(handshake.c_str(),handshake.size());
187
189 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
190 ec = p.validate_handshake(r);
191 BOOST_CHECK( !ec );
192
193 u = p.get_uri(r);
194
195 BOOST_CHECK( !u->get_valid() );
196}
197
const mie::Vuint & p
Definition bn.cpp:27
const mie::Vuint & r
Definition bn.cpp:28
Stub class for use when disabling permessage_deflate extension.
Definition disabled.hpp:52
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
Stores, parses, and manipulates HTTP responses.
Definition response.hpp:57
Represents a buffer for a single WebSocket message.
Definition pool.hpp:101
Processor for Hybi Draft version 08.
Definition hybi08.hpp:44
Thread safe stub "random" integer generator.
Definition none.hpp:46
BOOST_AUTO_TEST_CASE(exact_match)
Definition hybi08.cpp:69
@ missing_required_header
Missing Required Header.
Definition base.hpp:129
@ invalid_http_method
Invalid HTTP method.
Definition base.hpp:120
@ invalid_http_version
Invalid HTTP version.
Definition base.hpp:123
int get_websocket_version(request_type &r)
Extract the version from a WebSocket handshake request.
bool is_websocket_handshake(request_type &r)
Determine whether or not a generic HTTP request is a WebSocket handshake.
Definition processor.hpp:68
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:351
stub_config::request_type request_type
Definition hybi07.cpp:62
static const bool enable_extensions
Extension related config.
Definition hybi07.cpp:56
websocketpp::extensions::permessage_deflate::disabled< permessage_deflate_config > permessage_deflate_type
Definition hybi07.cpp:66
core::message_type message_type
core::rng_type rng_type
core::con_msg_manager_type con_msg_manager_type
core::request_type request_type
static const size_t max_message_size
Definition hybi00.cpp:49
core::response_type response_type