NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
hybi13.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_13_processor
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
35 
41 
44 
45 struct stub_config {
48 
53 
55 
58  };
59 
62 
63  static const size_t max_message_size = 16000000;
64  static const bool enable_extensions = false;
65 };
66 
70 
75 
77 
80  };
81 
84 
85  static const size_t max_message_size = 16000000;
86  static const bool enable_extensions = true;
87 };
88 
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 {
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;
105 };
106 
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;
118 };
119 
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 
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 
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 
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 
155  BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
157 }
158 
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 
167  BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
169 }
170 
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 
179  BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
181 }
182 
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 
191  BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
193 }
194 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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();
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 
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 
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 
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 
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 
564 }
565 
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 
573 }
574 
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 
582 }
583 
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 
591 }
592 
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 
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 
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 
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 
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 
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 
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 
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 
678  BOOST_CHECK_EQUAL( subps.size(), 0 );
679 }
680 
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 
bool is_control(value v)
Check if an opcode is for a control frame.
Definition: frame.hpp:139
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
stub_config::request_type req
Definition: hybi00.cpp:59
lib::error_code client_handshake_request(request_type &, uri_ptr, std::vector< std::string > const &) const
Fill in a set of request headers for a client connection request.
Definition: hybi00.hpp:158
websocketpp::extensions::permessage_deflate::enabled< permessage_deflate_config > permessage_deflate_type
Definition: hybi13.cpp:83
stub_config::message_type::ptr message_ptr
Definition: hybi13.cpp:90
bool is_websocket_handshake(request_type &r)
Determine whether or not a generic HTTP request is a WebSocket handshake.
Definition: processor.hpp:68
Clients may not send unmasked frames.
Definition: base.hpp:102
void set_max_message_size(size_t new_value)
Set maximum message size.
Definition: processor.hpp:203
core::con_msg_manager_type con_msg_manager_type
Definition: connection.cpp:72
con_msg_manager_type::ptr msg_manager
Definition: hybi13.cpp:100
websocketpp::processor::hybi13< stub_config > p
Definition: hybi13.cpp:104
websocketpp::http::parser::response response_type
Definition: hybi13.cpp:69
websocketpp::random::none::int_generator< uint32_t > rng_type
Definition: hybi13.cpp:76
lib::error_code validate_handshake(request_type const &r) const
validate a WebSocket handshake request for this version
Definition: hybi00.hpp:77
core::rng_type rng_type
Definition: connection.cpp:78
websocketpp::message_buffer::message< websocketpp::message_buffer::alloc::con_msg_manager > message_type
Definition: hybi13.cpp:72
int get_websocket_version(request_type &r)
Extract the version from a WebSocket handshake request.
Definition: processor.hpp:107
stub_config::request_type req
Definition: hybi13.cpp:115
Illegal use of reserved bit.
Definition: base.hpp:93
lib::error_code extract_subprotocols(request_type const &req, std::vector< std::string > &subprotocol_list)
Extracts requested subprotocols from a handshake request.
Definition: hybi00.hpp:198
bool ready() const
Checks if there is a message ready.
Definition: hybi00.hpp:309
stub_config::response_type res
Definition: hybi13.cpp:116
stub_config::con_msg_manager_type con_msg_manager_type
Definition: hybi13.cpp:89
websocketpp::processor::hybi13< stub_config_ext > p
Definition: hybi13.cpp:117
websocketpp::message_buffer::alloc::con_msg_manager< message_type > con_msg_manager_type
Definition: hybi13.cpp:74
int get_version() const
Get the protocol version of this processor.
Definition: hybi00.hpp:73
lib::error_code validate_server_handshake_response(request_type const &, response_type &) const
Validate the server&#39;s response to an outgoing handshake request.
Definition: hybi00.hpp:173
bool invalid(value code)
Test whether a close code is invalid on the wire.
Definition: close.hpp:199
websocketpp::extensions::permessage_deflate::disabled< permessage_deflate_config > permessage_deflate_type
Definition: hybi13.cpp:61
websocketpp::random::none::int_generator< uint32_t > rng_type
Definition: hybi13.cpp:54
Continuation without message.
Definition: base.hpp:99
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:57
Stub class for use when disabling permessage_deflate extension.
Definition: disabled.hpp:53
stub_config::con_msg_manager_type::ptr msg_manager
Definition: hybi00.cpp:58
processor_setup_ext(bool server)
Definition: hybi13.cpp:108
Extension related operation was ignored because extensions are disabled.
Definition: base.hpp:153
err_str_pair negotiate_extensions(request_type const &request)
Initializes extensions based on the Sec-WebSocket-Extensions header.
Definition: hybi13.hpp:88
websocketpp::http::parser::response response_type
Definition: hybi13.cpp:47
message_ptr get_message()
Retrieves the most recently processed message.
Definition: hybi00.hpp:317
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
Definition: base.hpp:244
stub_config::rng_type rng
Definition: hybi13.cpp:101
size_t consume(uint8_t *buf, size_t len, lib::error_code &ec)
Process new websocket connection bytes.
Definition: hybi00.hpp:252
websocketpp::lib::error_code ec
Definition: hybi13.cpp:112
Thread safe stub "random" integer generator.
Definition: none.hpp:46
virtual err_str_pair negotiate_extensions(request_type const &)
Initializes extensions based on the Sec-WebSocket-Extensions header.
Definition: processor.hpp:225
Represents a buffer for a single WebSocket message.
Definition: message.hpp:84
stub_config::response_type res
Definition: hybi00.cpp:60
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:352
stub_config::request_type request_type
Definition: hybi13.cpp:57
websocketpp::http::parser::request request_type
Definition: hybi13.cpp:46
Servers may not send masked frames.
Definition: base.hpp:105
Fragmented control message.
Definition: base.hpp:96
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
Definition: hybi00.hpp:217
Processor encountered a message that was too large.
Definition: base.hpp:78
websocketpp::processor::hybi00< stub_config > p
Definition: hybi00.cpp:61
stub_config_ext::request_type request_type
Definition: hybi13.cpp:79
core::request_type request_type
Definition: connection.cpp:68
Extension specific config.
Definition: hybi07.cpp:61
BOOST_AUTO_TEST_CASE(exact_match)
Definition: hybi13.cpp:120
static const size_t max_message_size
Definition: hybi00.cpp:49
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out)
Prepare a message for writing.
Definition: hybi00.hpp:329
stub_config::rng_type rng
Definition: hybi13.cpp:114
Opcode was invalid for requested operation.
Definition: base.hpp:87
websocketpp::lib::error_code ec
Definition: hybi00.cpp:57
core::response_type response_type
Definition: connection.cpp:69
websocketpp::message_buffer::alloc::con_msg_manager< message_type > con_msg_manager_type
Definition: hybi13.cpp:52
con_msg_manager_type::ptr msg_manager
Definition: hybi13.cpp:113
static const bool enable_extensions
Extension related config.
Definition: hybi07.cpp:56
websocketpp::http::parser::request request_type
Definition: hybi13.cpp:68
static uint16_t const uri_default_port
Default port for ws://.
Definition: uri.hpp:45
A connection message manager that allocates a new message for each request.
Definition: alloc.hpp:41
processor_setup(bool server)
Definition: hybi13.cpp:95
The processor method was called with invalid arguments.
Definition: base.hpp:84
websocketpp::message_buffer::message< websocketpp::message_buffer::alloc::con_msg_manager > message_type
Definition: hybi13.cpp:50
lib::error_code process_handshake(request_type const &req, std::string const &subprotocol, response_type &res) const
Calculate the appropriate response for this websocket request.
Definition: hybi00.hpp:100