fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / test / processors / hybi00.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_00_processor
29 #include <boost/test/unit_test.hpp>
30
31 #include <iostream>
32 #include <string>
33
34 #include <websocketpp/processors/hybi00.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
40 struct stub_config {
41     typedef websocketpp::http::parser::request request_type;
42     typedef websocketpp::http::parser::response response_type;
43
44     typedef websocketpp::message_buffer::message
45         <websocketpp::message_buffer::alloc::con_msg_manager> message_type;
46     typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
47         con_msg_manager_type;
48         
49     static const size_t max_message_size = 16000000;
50 };
51
52 struct processor_setup {
53     processor_setup(bool server)
54       : msg_manager(new stub_config::con_msg_manager_type())
55       , p(false,server,msg_manager) {}
56
57     websocketpp::lib::error_code ec;
58     stub_config::con_msg_manager_type::ptr msg_manager;
59     stub_config::request_type req;
60     stub_config::response_type res;
61     websocketpp::processor::hybi00<stub_config> p;
62 };
63
64 typedef stub_config::message_type::ptr message_ptr;
65
66 BOOST_AUTO_TEST_CASE( exact_match ) {
67     processor_setup env(true);
68
69     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";
70
71     env.req.consume(handshake.c_str(),handshake.size());
72     env.req.replace_header("Sec-WebSocket-Key3","WjN}|M(6");
73
74     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
75     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
76     env.ec = env.p.validate_handshake(env.req);
77     BOOST_CHECK(!env.ec);
78
79     websocketpp::uri_ptr u;
80
81     BOOST_CHECK_NO_THROW( u = env.p.get_uri(env.req) );
82
83     BOOST_CHECK_EQUAL(u->get_secure(), false);
84     BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
85     BOOST_CHECK_EQUAL(u->get_resource(), "/");
86     BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
87
88     env.p.process_handshake(env.req,"",env.res);
89
90     BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "Upgrade");
91     BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "WebSocket");
92     BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Origin"), "http://example.com");
93
94     BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Location"), "ws://www.example.com/");
95     BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Key3"), "n`9eBk9z$R8pOtVb");
96 }
97
98 BOOST_AUTO_TEST_CASE( non_get_method ) {
99     processor_setup env(true);
100
101     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";
102
103     env.req.consume(handshake.c_str(),handshake.size());
104     env.req.replace_header("Sec-WebSocket-Key3","janelle!");
105
106     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
107     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
108     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_method );
109 }
110
111 BOOST_AUTO_TEST_CASE( old_http_version ) {
112     processor_setup env(true);
113
114     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";
115
116     env.req.consume(handshake.c_str(),handshake.size());
117     env.req.replace_header("Sec-WebSocket-Key3","janelle!");
118
119     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
120     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
121     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_version );
122 }
123
124 BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
125     processor_setup env(true);
126
127     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";
128
129     env.req.consume(handshake.c_str(),handshake.size());
130     env.req.replace_header("Sec-WebSocket-Key3","janelle!");
131
132     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
133     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
134     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
135 }
136
137 BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
138     processor_setup env(true);
139
140     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";
141
142     env.req.consume(handshake.c_str(),handshake.size());
143     env.req.replace_header("Sec-WebSocket-Key3","janelle!");
144
145     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
146     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
147     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
148 }
149
150 BOOST_AUTO_TEST_CASE( bad_host ) {
151     processor_setup env(true);
152     websocketpp::uri_ptr u;
153
154     std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\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";
155
156     env.req.consume(handshake.c_str(),handshake.size());
157     env.req.replace_header("Sec-WebSocket-Key3","janelle!");
158
159     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
160     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
161     BOOST_CHECK( !env.p.validate_handshake(env.req) );
162
163     BOOST_CHECK( !env.p.get_uri(env.req)->get_valid() );
164 }
165
166 BOOST_AUTO_TEST_CASE( extract_subprotocols ) {
167     processor_setup env(true);
168
169     std::vector<std::string> subps;
170
171     BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
172     BOOST_CHECK_EQUAL( subps.size(), 0 );
173 }
174
175 BOOST_AUTO_TEST_CASE( prepare_data_frame_null ) {
176     processor_setup env(true);
177
178     message_ptr in = env.msg_manager->get_message();
179     message_ptr out = env.msg_manager->get_message();
180     message_ptr invalid;
181
182
183     // empty pointers arguements should return sane error
184     BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments );
185
186     BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments );
187
188     BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments );
189
190     // test valid opcodes
191     // text (1) should be the only valid opcode
192     for (int i = 0; i < 0xF; i++) {
193         in->set_opcode(websocketpp::frame::opcode::value(i));
194
195         env.ec = env.p.prepare_data_frame(in,out);
196
197         if (i != 1) {
198             BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
199         } else {
200             BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode );
201         }
202     }
203
204     /*
205      * TODO: tests for invalid UTF8
206     char buf[2] = {0x00, 0x00};
207
208     in->set_opcode(websocketpp::frame::opcode::text);
209     in->set_payload("foo");
210
211     env.ec = env.p.prepare_data_frame(in,out);
212     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_payload );
213     */
214 }
215
216 BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
217     processor_setup env(true);
218
219     message_ptr in = env.msg_manager->get_message();
220     message_ptr out = env.msg_manager->get_message();
221
222     in->set_opcode(websocketpp::frame::opcode::text);
223     in->set_payload("foo");
224
225     env.ec = env.p.prepare_data_frame(in,out);
226
227     unsigned char raw_header[1] = {0x00};
228     unsigned char raw_payload[4] = {0x66,0x6f,0x6f,0xff};
229
230     BOOST_CHECK( !env.ec );
231     BOOST_CHECK_EQUAL( out->get_header(), std::string(reinterpret_cast<char*>(raw_header),1) );
232     BOOST_CHECK_EQUAL( out->get_payload(), std::string(reinterpret_cast<char*>(raw_payload),4) );
233 }
234
235
236 BOOST_AUTO_TEST_CASE( empty_consume ) {
237     uint8_t frame[2] = {0x00,0x00};
238
239     processor_setup env(true);
240
241     size_t ret = env.p.consume(frame,0,env.ec);
242
243     BOOST_CHECK_EQUAL( ret, 0);
244     BOOST_CHECK( !env.ec );
245     BOOST_CHECK_EQUAL( env.p.ready(), false );
246 }
247
248 BOOST_AUTO_TEST_CASE( empty_frame ) {
249     uint8_t frame[2] = {0x00, 0xff};
250
251     processor_setup env(true);
252
253     size_t ret = env.p.consume(frame,2,env.ec);
254
255     BOOST_CHECK_EQUAL( ret, 2);
256     BOOST_CHECK( !env.ec );
257     BOOST_CHECK_EQUAL( env.p.ready(), true );
258     BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "" );
259     BOOST_CHECK_EQUAL( env.p.ready(), false );
260 }
261
262 BOOST_AUTO_TEST_CASE( short_frame ) {
263     uint8_t frame[5] = {0x00, 0x66, 0x6f, 0x6f, 0xff};
264
265     processor_setup env(true);
266
267     size_t ret = env.p.consume(frame,5,env.ec);
268
269     BOOST_CHECK_EQUAL( ret, 5);
270     BOOST_CHECK( !env.ec );
271     BOOST_CHECK_EQUAL( env.p.ready(), true );
272     BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "foo" );
273     BOOST_CHECK_EQUAL( env.p.ready(), false );
274 }