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_13_processor
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
34 #include <websocketpp/processors/hybi13.hpp>
35 
36 #include <websocketpp/http/request.hpp>
37 #include <websocketpp/http/response.hpp>
38 #include <websocketpp/message_buffer/message.hpp>
39 #include <websocketpp/message_buffer/alloc.hpp>
40 #include <websocketpp/random/none.hpp>
41 
42 #include <websocketpp/extensions/permessage_deflate/disabled.hpp>
43 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
44 
45 struct stub_config {
46     typedef websocketpp::http::parser::request request_type;
47     typedef websocketpp::http::parser::response response_type;
48 
49     typedef websocketpp::message_buffer::message
50         <websocketpp::message_buffer::alloc::con_msg_manager> message_type;
51     typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
52         con_msg_manager_type;
53 
54     typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
55 
56     struct permessage_deflate_config {
57         typedef stub_config::request_type request_type;
58     };
59 
60     typedef websocketpp::extensions::permessage_deflate::disabled
61         <permessage_deflate_config> permessage_deflate_type;
62 
63     static const size_t max_message_size = 16000000;
64     static const bool enable_extensions = false;
65 };
66 
67 struct stub_config_ext {
68     typedef websocketpp::http::parser::request request_type;
69     typedef websocketpp::http::parser::response response_type;
70 
71     typedef websocketpp::message_buffer::message
72         <websocketpp::message_buffer::alloc::con_msg_manager> message_type;
73     typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
74         con_msg_manager_type;
75 
76     typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
77 
78     struct permessage_deflate_config {
79         typedef stub_config_ext::request_type request_type;
80     };
81 
82     typedef websocketpp::extensions::permessage_deflate::enabled
83         <permessage_deflate_config> permessage_deflate_type;
84 
85     static const size_t max_message_size = 16000000;
86     static const bool enable_extensions = true;
87 };
88 
89 typedef stub_config::con_msg_manager_type con_msg_manager_type;
90 typedef stub_config::message_type::ptr message_ptr;
91 
92 // Set up a structure that constructs new copies of all of the support structure
93 // for using connection processors
94 struct processor_setup {
processor_setupprocessor_setup95     processor_setup(bool server)
96       : msg_manager(new con_msg_manager_type())
97       , p(false,server,msg_manager,rng) {}
98 
99     websocketpp::lib::error_code ec;
100     con_msg_manager_type::ptr msg_manager;
101     stub_config::rng_type rng;
102     stub_config::request_type req;
103     stub_config::response_type res;
104     websocketpp::processor::hybi13<stub_config> p;
105 };
106 
107 struct processor_setup_ext {
processor_setup_extprocessor_setup_ext108     processor_setup_ext(bool server)
109       : msg_manager(new con_msg_manager_type())
110       , p(false,server,msg_manager,rng) {}
111 
112     websocketpp::lib::error_code ec;
113     con_msg_manager_type::ptr msg_manager;
114     stub_config::rng_type rng;
115     stub_config::request_type req;
116     stub_config::response_type res;
117     websocketpp::processor::hybi13<stub_config_ext> p;
118 };
119 
BOOST_AUTO_TEST_CASE(exact_match)120 BOOST_AUTO_TEST_CASE( exact_match ) {
121     processor_setup env(true);
122 
123     std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
124 
125     env.req.consume(handshake.c_str(),handshake.size());
126 
127     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
128     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
129     BOOST_CHECK(!env.p.validate_handshake(env.req));
130 
131     websocketpp::uri_ptr u;
132 
133     BOOST_CHECK_NO_THROW( u = env.p.get_uri(env.req) );
134 
135     BOOST_CHECK_EQUAL(u->get_secure(), false);
136     BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
137     BOOST_CHECK_EQUAL(u->get_resource(), "/");
138     BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
139 
140     env.p.process_handshake(env.req,"",env.res);
141 
142     BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "Upgrade");
143     BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "websocket");
144     BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
145 }
146 
BOOST_AUTO_TEST_CASE(non_get_method)147 BOOST_AUTO_TEST_CASE( non_get_method ) {
148     processor_setup env(true);
149 
150     std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
151 
152     env.req.consume(handshake.c_str(),handshake.size());
153 
154     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
155     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
156     BOOST_CHECK( env.p.validate_handshake(env.req) == websocketpp::processor::error::invalid_http_method );
157 }
158 
BOOST_AUTO_TEST_CASE(old_http_version)159 BOOST_AUTO_TEST_CASE( old_http_version ) {
160     processor_setup env(true);
161 
162     std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
163 
164     env.req.consume(handshake.c_str(),handshake.size());
165 
166     BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
167     BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
168     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_version );
169 }
170 
BOOST_AUTO_TEST_CASE(missing_handshake_key1)171 BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
172     processor_setup env(true);
173 
174     std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
175 
176     env.req.consume(handshake.c_str(),handshake.size());
177 
178     BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
179     BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
180     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
181 }
182 
BOOST_AUTO_TEST_CASE(missing_handshake_key2)183 BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
184     processor_setup env(true);
185 
186     std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
187 
188     env.req.consume(handshake.c_str(),handshake.size());
189 
190     BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
191     BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
192     BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
193 }
194 
BOOST_AUTO_TEST_CASE(bad_host)195 BOOST_AUTO_TEST_CASE( bad_host ) {
196     processor_setup env(true);
197 
198     std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
199 
200     env.req.consume(handshake.c_str(),handshake.size());
201 
202     BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
203     BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
204     BOOST_CHECK( !env.p.validate_handshake(env.req) );
205     BOOST_CHECK( !env.p.get_uri(env.req)->get_valid() );
206 }
207 
208 // FRAME TESTS TO DO
209 //
210 // unmasked, 0 length, binary
211 // 0x82 0x00
212 //
213 // masked, 0 length, binary
214 // 0x82 0x80
215 //
216 // unmasked, 0 length, text
217 // 0x81 0x00
218 //
219 // masked, 0 length, text
220 // 0x81 0x80
221 
BOOST_AUTO_TEST_CASE(frame_empty_binary_unmasked)222 BOOST_AUTO_TEST_CASE( frame_empty_binary_unmasked ) {
223     uint8_t frame[2] = {0x82, 0x00};
224 
225     // all in one chunk
226     processor_setup env1(false);
227 
228     size_t ret1 = env1.p.consume(frame,2,env1.ec);
229 
230     BOOST_CHECK_EQUAL( ret1, 2 );
231     BOOST_CHECK( !env1.ec );
232     BOOST_CHECK_EQUAL( env1.p.ready(), true );
233 
234     // two separate chunks
235     processor_setup env2(false);
236 
237     BOOST_CHECK_EQUAL( env2.p.consume(frame,1,env2.ec), 1 );
238     BOOST_CHECK( !env2.ec );
239     BOOST_CHECK_EQUAL( env2.p.ready(), false );
240 
241     BOOST_CHECK_EQUAL( env2.p.consume(frame+1,1,env2.ec), 1 );
242     BOOST_CHECK( !env2.ec );
243     BOOST_CHECK_EQUAL( env2.p.ready(), true );
244 }
245 
BOOST_AUTO_TEST_CASE(frame_small_binary_unmasked)246 BOOST_AUTO_TEST_CASE( frame_small_binary_unmasked ) {
247     processor_setup env(false);
248 
249     uint8_t frame[4] = {0x82, 0x02, 0x2A, 0x2A};
250 
251     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
252     BOOST_CHECK_EQUAL( env.p.consume(frame,4,env.ec), 4 );
253     BOOST_CHECK( !env.ec );
254     BOOST_CHECK_EQUAL( env.p.ready(), true );
255 
256     message_ptr foo = env.p.get_message();
257 
258     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
259     BOOST_CHECK_EQUAL( foo->get_payload(), "**" );
260 
261 }
262 
BOOST_AUTO_TEST_CASE(frame_extended_binary_unmasked)263 BOOST_AUTO_TEST_CASE( frame_extended_binary_unmasked ) {
264     processor_setup env(false);
265 
266     uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
267     frame[0] = 0x82;
268     frame[1] = 0x7E;
269     frame[2] = 0x00;
270     frame[3] = 0x7E;
271     std::fill_n(frame+4,126,0x2A);
272 
273     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
274     BOOST_CHECK_EQUAL( env.p.consume(frame,130,env.ec), 130 );
275     BOOST_CHECK( !env.ec );
276     BOOST_CHECK_EQUAL( env.p.ready(), true );
277 
278     message_ptr foo = env.p.get_message();
279 
280     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
281     BOOST_CHECK_EQUAL( foo->get_payload().size(), 126 );
282 }
283 
BOOST_AUTO_TEST_CASE(frame_jumbo_binary_unmasked)284 BOOST_AUTO_TEST_CASE( frame_jumbo_binary_unmasked ) {
285     processor_setup env(false);
286 
287     uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
288     std::fill_n(frame+4,126,0x2A);
289 
290     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
291     BOOST_CHECK_EQUAL( env.p.consume(frame,130,env.ec), 130 );
292     BOOST_CHECK( !env.ec );
293     BOOST_CHECK_EQUAL( env.p.ready(), true );
294 
295     message_ptr foo = env.p.get_message();
296 
297     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
298     BOOST_CHECK_EQUAL( foo->get_payload().size(), 126 );
299 }
300 
BOOST_AUTO_TEST_CASE(control_frame_too_large)301 BOOST_AUTO_TEST_CASE( control_frame_too_large ) {
302     processor_setup env(false);
303 
304     uint8_t frame[130] = {0x88, 0x7E, 0x00, 0x7E};
305     std::fill_n(frame+4,126,0x2A);
306 
307     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
308     BOOST_CHECK_GT( env.p.consume(frame,130,env.ec), 0 );
309     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::control_too_big  );
310     BOOST_CHECK_EQUAL( env.p.ready(), false );
311 }
312 
BOOST_AUTO_TEST_CASE(rsv_bits_used)313 BOOST_AUTO_TEST_CASE( rsv_bits_used ) {
314     uint8_t frame[3][2] = {{0x90, 0x00},
315                            {0xA0, 0x00},
316                            {0xC0, 0x00}};
317 
318     for (int i = 0; i < 3; i++) {
319         processor_setup env(false);
320 
321         BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
322         BOOST_CHECK_GT( env.p.consume(frame[i],2,env.ec), 0 );
323         BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_rsv_bit  );
324         BOOST_CHECK_EQUAL( env.p.ready(), false );
325     }
326 }
327 
328 
BOOST_AUTO_TEST_CASE(reserved_opcode_used)329 BOOST_AUTO_TEST_CASE( reserved_opcode_used ) {
330     uint8_t frame[10][2] = {{0x83, 0x00},
331                             {0x84, 0x00},
332                             {0x85, 0x00},
333                             {0x86, 0x00},
334                             {0x87, 0x00},
335                             {0x8B, 0x00},
336                             {0x8C, 0x00},
337                             {0x8D, 0x00},
338                             {0x8E, 0x00},
339                             {0x8F, 0x00}};
340 
341     for (int i = 0; i < 10; i++) {
342         processor_setup env(false);
343 
344         BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
345         BOOST_CHECK_GT( env.p.consume(frame[i],2,env.ec), 0 );
346         BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode  );
347         BOOST_CHECK_EQUAL( env.p.ready(), false );
348     }
349 }
350 
BOOST_AUTO_TEST_CASE(fragmented_control_message)351 BOOST_AUTO_TEST_CASE( fragmented_control_message ) {
352     processor_setup env(false);
353 
354     uint8_t frame[2] = {0x08, 0x00};
355 
356     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
357     BOOST_CHECK_GT( env.p.consume(frame,2,env.ec), 0 );
358     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::fragmented_control );
359     BOOST_CHECK_EQUAL( env.p.ready(), false );
360 }
361 
BOOST_AUTO_TEST_CASE(fragmented_binary_message)362 BOOST_AUTO_TEST_CASE( fragmented_binary_message ) {
363     processor_setup env0(false);
364     processor_setup env1(false);
365 
366     uint8_t frame0[6] = {0x02, 0x01, 0x2A, 0x80, 0x01, 0x2A};
367     uint8_t frame1[8] = {0x02, 0x01, 0x2A, 0x89, 0x00, 0x80, 0x01, 0x2A};
368 
369     // read fragmented message in one chunk
370     BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
371     BOOST_CHECK_EQUAL( env0.p.consume(frame0,6,env0.ec), 6 );
372     BOOST_CHECK( !env0.ec );
373     BOOST_CHECK_EQUAL( env0.p.ready(), true );
374     BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
375 
376     // read fragmented message in two chunks
377     BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
378     BOOST_CHECK_EQUAL( env0.p.consume(frame0,3,env0.ec), 3 );
379     BOOST_CHECK( !env0.ec );
380     BOOST_CHECK_EQUAL( env0.p.ready(), false );
381     BOOST_CHECK_EQUAL( env0.p.consume(frame0+3,3,env0.ec), 3 );
382     BOOST_CHECK( !env0.ec );
383     BOOST_CHECK_EQUAL( env0.p.ready(), true );
384     BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
385 
386     // read fragmented message with control message in between
387     BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
388     BOOST_CHECK_EQUAL( env0.p.consume(frame1,8,env0.ec), 5 );
389     BOOST_CHECK( !env0.ec );
390     BOOST_CHECK_EQUAL( env0.p.ready(), true );
391     BOOST_CHECK_EQUAL( env0.p.get_message()->get_opcode(), websocketpp::frame::opcode::PING);
392     BOOST_CHECK_EQUAL( env0.p.consume(frame1+5,3,env0.ec), 3 );
393     BOOST_CHECK( !env0.ec );
394     BOOST_CHECK_EQUAL( env0.p.ready(), true );
395     BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
396 
397     // read lone continuation frame
398     BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
399     BOOST_CHECK_GT( env0.p.consume(frame0+3,3,env0.ec), 0);
400     BOOST_CHECK_EQUAL( env0.ec, websocketpp::processor::error::invalid_continuation );
401 
402     // read two start frames in a row
403     BOOST_CHECK_EQUAL( env1.p.get_message(), message_ptr() );
404     BOOST_CHECK_EQUAL( env1.p.consume(frame0,3,env1.ec), 3);
405     BOOST_CHECK( !env1.ec );
406     BOOST_CHECK_GT( env1.p.consume(frame0,3,env1.ec), 0);
407     BOOST_CHECK_EQUAL( env1.ec, websocketpp::processor::error::invalid_continuation );
408 }
409 
BOOST_AUTO_TEST_CASE(unmasked_client_frame)410 BOOST_AUTO_TEST_CASE( unmasked_client_frame ) {
411     processor_setup env(true);
412 
413     uint8_t frame[2] = {0x82, 0x00};
414 
415     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
416     BOOST_CHECK_GT( env.p.consume(frame,2,env.ec), 0 );
417     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::masking_required );
418     BOOST_CHECK_EQUAL( env.p.ready(), false );
419 }
420 
BOOST_AUTO_TEST_CASE(masked_server_frame)421 BOOST_AUTO_TEST_CASE( masked_server_frame ) {
422     processor_setup env(false);
423 
424     uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
425 
426     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
427     BOOST_CHECK_GT( env.p.consume(frame,8,env.ec), 0 );
428     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::masking_forbidden );
429     BOOST_CHECK_EQUAL( env.p.ready(), false );
430 }
431 
BOOST_AUTO_TEST_CASE(frame_small_binary_masked)432 BOOST_AUTO_TEST_CASE( frame_small_binary_masked ) {
433     processor_setup env(true);
434 
435     uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
436 
437     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
438     BOOST_CHECK_EQUAL( env.p.consume(frame,8,env.ec), 8 );
439     BOOST_CHECK( !env.ec );
440     BOOST_CHECK_EQUAL( env.p.ready(), true );
441     BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "**" );
442 }
443 
BOOST_AUTO_TEST_CASE(masked_fragmented_binary_message)444 BOOST_AUTO_TEST_CASE( masked_fragmented_binary_message ) {
445     processor_setup env(true);
446 
447     uint8_t frame0[14] = {0x02, 0x81, 0xAB, 0x23, 0x98, 0x45, 0x81,
448                          0x80, 0x81, 0xB8, 0x34, 0x12, 0xFF, 0x92};
449 
450     // read fragmented message in one chunk
451     BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
452     BOOST_CHECK_EQUAL( env.p.consume(frame0,14,env.ec), 14 );
453     BOOST_CHECK( !env.ec );
454     BOOST_CHECK_EQUAL( env.p.ready(), true );
455     BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "**" );
456 }
457 
BOOST_AUTO_TEST_CASE(prepare_data_frame)458 BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
459     processor_setup env(true);
460 
461     message_ptr in = env.msg_manager->get_message();
462     message_ptr out = env.msg_manager->get_message();
463     message_ptr invalid;
464 
465     // empty pointers arguements should return sane error
466     BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments );
467 
468     BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments );
469 
470     BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments );
471 
472     // test valid opcodes
473     // control opcodes should return an error, data ones shouldn't
474     for (int i = 0; i < 0xF; i++) {
475         in->set_opcode(websocketpp::frame::opcode::value(i));
476 
477         env.ec = env.p.prepare_data_frame(in,out);
478 
479         if (websocketpp::frame::opcode::is_control(in->get_opcode())) {
480             BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
481         } else {
482             BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode );
483         }
484     }
485 
486 
487     //in.set_payload("foo");
488 
489     //e = prepare_data_frame(in,out);
490 
491 
492 }
493 
BOOST_AUTO_TEST_CASE(single_frame_message_too_large)494 BOOST_AUTO_TEST_CASE( single_frame_message_too_large ) {
495     processor_setup env(true);
496 
497     env.p.set_max_message_size(3);
498 
499     uint8_t frame0[10] = {0x82, 0x84, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01};
500 
501     // read message that is one byte too large
502     BOOST_CHECK_EQUAL( env.p.consume(frame0,10,env.ec), 6 );
503     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::message_too_big );
504 }
505 
BOOST_AUTO_TEST_CASE(multiple_frame_message_too_large)506 BOOST_AUTO_TEST_CASE( multiple_frame_message_too_large ) {
507     processor_setup env(true);
508 
509     env.p.set_max_message_size(4);
510 
511     uint8_t frame0[8] = {0x02, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01};
512     uint8_t frame1[9] = {0x80, 0x83, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01};
513 
514     // read first message frame with size under the limit
515     BOOST_CHECK_EQUAL( env.p.consume(frame0,8,env.ec), 8 );
516     BOOST_CHECK( !env.ec );
517 
518     // read second message frame that puts the size over the limit
519     BOOST_CHECK_EQUAL( env.p.consume(frame1,9,env.ec), 6 );
520     BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::message_too_big );
521 }
522 
523 
524 
BOOST_AUTO_TEST_CASE(client_handshake_request)525 BOOST_AUTO_TEST_CASE( client_handshake_request ) {
526     processor_setup env(false);
527 
528     websocketpp::uri_ptr u(new websocketpp::uri("ws://localhost/"));
529 
530     env.p.client_handshake_request(env.req,u, std::vector<std::string>());
531 
532     BOOST_CHECK_EQUAL( env.req.get_method(), "GET" );
533     BOOST_CHECK_EQUAL( env.req.get_version(), "HTTP/1.1");
534     BOOST_CHECK_EQUAL( env.req.get_uri(), "/");
535 
536     BOOST_CHECK_EQUAL( env.req.get_header("Host"), "localhost");
537     BOOST_CHECK_EQUAL( env.req.get_header("Sec-WebSocket-Version"), "13");
538     BOOST_CHECK_EQUAL( env.req.get_header("Connection"), "Upgrade");
539     BOOST_CHECK_EQUAL( env.req.get_header("Upgrade"), "websocket");
540 }
541 
542 // TODO:
543 // test cases
544 // - adding headers
545 // - adding Upgrade header
546 // - adding Connection header
547 // - adding Sec-WebSocket-Version, Sec-WebSocket-Key, or Host header
548 // - other Sec* headers?
549 // - User Agent header?
550 
551 // Origin support
552 // Subprotocol requests
553 
554 //websocketpp::uri_ptr u(new websocketpp::uri("ws://localhost/"));
555     //env.p.client_handshake_request(env.req,u);
556 
BOOST_AUTO_TEST_CASE(client_handshake_response_404)557 BOOST_AUTO_TEST_CASE( client_handshake_response_404 ) {
558     processor_setup env(false);
559 
560     std::string res = "HTTP/1.1 404 Not Found\r\n\r\n";
561     env.res.consume(res.data(),res.size());
562 
563     BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::invalid_http_status );
564 }
565 
BOOST_AUTO_TEST_CASE(client_handshake_response_no_upgrade)566 BOOST_AUTO_TEST_CASE( client_handshake_response_no_upgrade ) {
567     processor_setup env(false);
568 
569     std::string res = "HTTP/1.1 101 Switching Protocols\r\n\r\n";
570     env.res.consume(res.data(),res.size());
571 
572     BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
573 }
574 
BOOST_AUTO_TEST_CASE(client_handshake_response_no_connection)575 BOOST_AUTO_TEST_CASE( client_handshake_response_no_connection ) {
576     processor_setup env(false);
577 
578     std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\n\r\n";
579     env.res.consume(res.data(),res.size());
580 
581     BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
582 }
583 
BOOST_AUTO_TEST_CASE(client_handshake_response_no_accept)584 BOOST_AUTO_TEST_CASE( client_handshake_response_no_accept ) {
585     processor_setup env(false);
586 
587     std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\nConnection: bar, UpGrAdE\r\n\r\n";
588     env.res.consume(res.data(),res.size());
589 
590     BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
591 }
592 
BOOST_AUTO_TEST_CASE(client_handshake_response)593 BOOST_AUTO_TEST_CASE( client_handshake_response ) {
594     processor_setup env(false);
595 
596     env.req.append_header("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
597 
598     std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\nConnection: bar, UpGrAdE\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n";
599     env.res.consume(res.data(),res.size());
600 
601     BOOST_CHECK( !env.p.validate_server_handshake_response(env.req,env.res) );
602 }
603 
BOOST_AUTO_TEST_CASE(extensions_disabled)604 BOOST_AUTO_TEST_CASE( extensions_disabled ) {
605     processor_setup env(true);
606 
607     env.req.replace_header("Sec-WebSocket-Extensions","");
608 
609     std::pair<websocketpp::lib::error_code,std::string> neg_results;
610     neg_results = env.p.negotiate_extensions(env.req);
611 
612     BOOST_CHECK_EQUAL( neg_results.first, websocketpp::processor::error::extensions_disabled );
613     BOOST_CHECK_EQUAL( neg_results.second, "" );
614 }
615 
BOOST_AUTO_TEST_CASE(extension_negotiation_blank)616 BOOST_AUTO_TEST_CASE( extension_negotiation_blank ) {
617     processor_setup_ext env(true);
618 
619     env.req.replace_header("Sec-WebSocket-Extensions","");
620 
621     std::pair<websocketpp::lib::error_code,std::string> neg_results;
622     neg_results = env.p.negotiate_extensions(env.req);
623 
624     BOOST_CHECK( !neg_results.first );
625     BOOST_CHECK_EQUAL( neg_results.second, "" );
626 }
627 
BOOST_AUTO_TEST_CASE(extension_negotiation_unknown)628 BOOST_AUTO_TEST_CASE( extension_negotiation_unknown ) {
629     processor_setup_ext env(true);
630 
631     env.req.replace_header("Sec-WebSocket-Extensions","foo");
632 
633     std::pair<websocketpp::lib::error_code,std::string> neg_results;
634     neg_results = env.p.negotiate_extensions(env.req);
635 
636     BOOST_CHECK( !neg_results.first );
637     BOOST_CHECK_EQUAL( neg_results.second, "" );
638 }
639 
BOOST_AUTO_TEST_CASE(extract_subprotocols_empty)640 BOOST_AUTO_TEST_CASE( extract_subprotocols_empty ) {
641     processor_setup env(true);
642     std::vector<std::string> subps;
643 
644     BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
645     BOOST_CHECK_EQUAL( subps.size(), 0 );
646 }
647 
BOOST_AUTO_TEST_CASE(extract_subprotocols_one)648 BOOST_AUTO_TEST_CASE( extract_subprotocols_one ) {
649     processor_setup env(true);
650     std::vector<std::string> subps;
651 
652     env.req.replace_header("Sec-WebSocket-Protocol","foo");
653 
654     BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
655     BOOST_REQUIRE_EQUAL( subps.size(), 1 );
656     BOOST_CHECK_EQUAL( subps[0], "foo" );
657 }
658 
BOOST_AUTO_TEST_CASE(extract_subprotocols_multiple)659 BOOST_AUTO_TEST_CASE( extract_subprotocols_multiple ) {
660     processor_setup env(true);
661     std::vector<std::string> subps;
662 
663     env.req.replace_header("Sec-WebSocket-Protocol","foo,bar");
664 
665     BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
666     BOOST_REQUIRE_EQUAL( subps.size(), 2 );
667     BOOST_CHECK_EQUAL( subps[0], "foo" );
668     BOOST_CHECK_EQUAL( subps[1], "bar" );
669 }
670 
BOOST_AUTO_TEST_CASE(extract_subprotocols_invalid)671 BOOST_AUTO_TEST_CASE( extract_subprotocols_invalid) {
672     processor_setup env(true);
673     std::vector<std::string> subps;
674 
675     env.req.replace_header("Sec-WebSocket-Protocol","foo,bar,,,,");
676 
677     BOOST_CHECK_EQUAL( env.p.extract_subprotocols(env.req,subps), websocketpp::processor::error::make_error_code(websocketpp::processor::error::subprotocol_parse_error) );
678     BOOST_CHECK_EQUAL( subps.size(), 0 );
679 }
680 
BOOST_AUTO_TEST_CASE(extension_negotiation_permessage_deflate)681 BOOST_AUTO_TEST_CASE( extension_negotiation_permessage_deflate ) {
682     processor_setup_ext env(true);
683 
684     env.req.replace_header("Sec-WebSocket-Extensions",
685         "permessage-deflate; client_max_window_bits");
686 
687     std::pair<websocketpp::lib::error_code,std::string> neg_results;
688     neg_results = env.p.negotiate_extensions(env.req);
689 
690     BOOST_CHECK( !neg_results.first );
691     BOOST_CHECK_EQUAL( neg_results.second, "permessage-deflate" );
692 }
693 
694