revert Makefile change
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / test / processors / hybi08.cpp
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
34 #include <websocketpp/processors/hybi08.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>
41
42 struct stub_config {
43     typedef websocketpp::http::parser::request request_type;
44     typedef websocketpp::http::parser::response response_type;
45
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>
49         con_msg_manager_type;
50
51     typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
52
53     static const size_t max_message_size = 16000000;
54
55     /// Extension related config
56     static const bool enable_extensions = false;
57
58     /// Extension specific config
59
60     /// permessage_deflate_config
61     struct permessage_deflate_config {
62         typedef stub_config::request_type request_type;
63     };
64
65     typedef websocketpp::extensions::permessage_deflate::disabled
66         <permessage_deflate_config> permessage_deflate_type;
67 };
68
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::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
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);
84     BOOST_CHECK(!ec);
85
86     websocketpp::uri_ptr u;
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
103 BOOST_AUTO_TEST_CASE( non_get_method ) {
104     stub_config::request_type r;
105     stub_config::response_type response;
106     stub_config::rng_type rng;
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
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 );
119 }
120
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::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
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 );
137 }
138
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::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
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 );
155 }
156
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::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
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 );
173 }
174
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::hybi08<stub_config> p(false,true,msg_manager,rng);
181     websocketpp::uri_ptr u;
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
188     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
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