2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
27 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE hybi_07_processor
29 #include <boost/test/unit_test.hpp>
34 #include <websocketpp/processors/hybi07.hpp>
35 #include <websocketpp/http/request.hpp>
36 #include <websocketpp/http/response.hpp>
37 #include <websocketpp/message_buffer/message.hpp>
38 #include <websocketpp/message_buffer/alloc.hpp>
39 #include <websocketpp/extensions/permessage_deflate/disabled.hpp>
40 #include <websocketpp/random/none.hpp>
43 typedef websocketpp::http::parser::request request_type;
44 typedef websocketpp::http::parser::response response_type;
46 typedef websocketpp::message_buffer::message
47 <websocketpp::message_buffer::alloc::con_msg_manager> message_type;
48 typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
51 typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
53 static const size_t max_message_size = 16000000;
55 /// Extension related config
56 static const bool enable_extensions = false;
58 /// Extension specific config
60 /// permessage_compress_config
61 struct permessage_deflate_config {
62 typedef stub_config::request_type request_type;
65 typedef websocketpp::extensions::permessage_deflate::disabled
66 <permessage_deflate_config> permessage_deflate_type;
69 BOOST_AUTO_TEST_CASE( exact_match ) {
70 stub_config::request_type r;
71 stub_config::response_type response;
72 stub_config::con_msg_manager_type::ptr msg_manager;
73 stub_config::rng_type rng;
74 websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
75 websocketpp::lib::error_code ec;
77 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";
79 r.consume(handshake.c_str(),handshake.size());
81 BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
82 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
83 ec = p.validate_handshake(r);
86 websocketpp::uri_ptr u;
90 BOOST_CHECK(u->get_valid());
91 BOOST_CHECK(!u->get_secure());
92 BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
93 BOOST_CHECK_EQUAL(u->get_resource(), "/");
94 BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
96 p.process_handshake(r,"",response);
98 BOOST_CHECK_EQUAL(response.get_header("Connection"), "upgrade");
99 BOOST_CHECK_EQUAL(response.get_header("Upgrade"), "websocket");
100 BOOST_CHECK_EQUAL(response.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
103 BOOST_AUTO_TEST_CASE( non_get_method ) {
104 stub_config::request_type r;
105 stub_config::response_type response;
106 stub_config::con_msg_manager_type::ptr msg_manager;
107 stub_config::rng_type rng;
108 websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
109 websocketpp::lib::error_code ec;
111 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";
113 r.consume(handshake.c_str(),handshake.size());
115 BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
116 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
117 ec = p.validate_handshake(r);
118 BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_method );
121 BOOST_AUTO_TEST_CASE( old_http_version ) {
122 stub_config::request_type r;
123 stub_config::response_type response;
124 stub_config::con_msg_manager_type::ptr msg_manager;
125 stub_config::rng_type rng;
126 websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
127 websocketpp::lib::error_code ec;
129 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";
131 r.consume(handshake.c_str(),handshake.size());
133 BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
134 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
135 ec = p.validate_handshake(r);
136 BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_version );
139 BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
140 stub_config::request_type r;
141 stub_config::response_type response;
142 stub_config::con_msg_manager_type::ptr msg_manager;
143 stub_config::rng_type rng;
144 websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
145 websocketpp::lib::error_code ec;
147 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";
149 r.consume(handshake.c_str(),handshake.size());
151 BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
152 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
153 ec = p.validate_handshake(r);
154 BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
157 BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
158 stub_config::request_type r;
159 stub_config::response_type response;
160 stub_config::con_msg_manager_type::ptr msg_manager;
161 stub_config::rng_type rng;
162 websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
163 websocketpp::lib::error_code ec;
165 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";
167 r.consume(handshake.c_str(),handshake.size());
169 BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
170 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
171 ec = p.validate_handshake(r);
172 BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
175 BOOST_AUTO_TEST_CASE( bad_host ) {
176 stub_config::request_type r;
177 stub_config::response_type response;
178 stub_config::con_msg_manager_type::ptr msg_manager;
179 stub_config::rng_type rng;
180 websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
181 websocketpp::lib::error_code ec;
183 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";
185 r.consume(handshake.c_str(),handshake.size());
187 BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
188 BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
189 ec = p.validate_handshake(r);
192 BOOST_CHECK( !p.get_uri(r)->get_valid() );