NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
hybi00.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_00_processor
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
39 
40 struct stub_config {
43 
48 
49  static const size_t max_message_size = 16000000;
50 };
51 
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;
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 
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 
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 
107  BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
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 
120  BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_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 
133  BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
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 
146  BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
148 }
149 
150 BOOST_AUTO_TEST_CASE( bad_host ) {
151  processor_setup env(true);
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 
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();
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 }
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
stub_config::request_type req
Definition: hybi00.cpp:59
BOOST_AUTO_TEST_CASE(exact_match)
Definition: hybi00.cpp:66
bool is_websocket_handshake(request_type &r)
Determine whether or not a generic HTTP request is a WebSocket handshake.
Definition: processor.hpp:68
lib::error_code validate_handshake(request_type const &r) const
validate a WebSocket handshake request for this version
Definition: hybi00.hpp:77
int get_websocket_version(request_type &r)
Extract the version from a WebSocket handshake request.
Definition: processor.hpp:107
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::con_msg_manager_type con_msg_manager_type
Definition: hybi13.cpp:89
int get_version() const
Get the protocol version of this processor.
Definition: hybi00.hpp:73
bool invalid(value code)
Test whether a close code is invalid on the wire.
Definition: close.hpp:199
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:57
stub_config::con_msg_manager_type::ptr msg_manager
Definition: hybi00.cpp:58
websocketpp::http::parser::response response_type
Definition: hybi00.cpp:42
message_ptr get_message()
Retrieves the most recently processed message.
Definition: hybi00.hpp:317
size_t consume(uint8_t *buf, size_t len, lib::error_code &ec)
Process new websocket connection bytes.
Definition: hybi00.hpp:252
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
websocketpp::http::parser::request request_type
Definition: hybi00.cpp:41
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
Definition: hybi00.hpp:217
websocketpp::processor::hybi00< stub_config > p
Definition: hybi00.cpp:61
core::request_type request_type
Definition: connection.cpp:68
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
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: hybi00.cpp:47
stub_config::message_type::ptr message_ptr
Definition: hybi00.cpp:64
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: hybi00.cpp:53
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: hybi00.cpp:45
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