Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
connection.cpp File Reference
#include <boost/test/unit_test.hpp>
#include "connection_tu2.hpp"
#include <websocketpp/transport/debug/endpoint.hpp>
Include dependency graph for connection.cpp:

Go to the source code of this file.

Classes

struct  connection_extension
 
struct  stub_config
 
struct  debug_config_client
 
struct  debug_config_client::transport_config
 
struct  connection_setup
 

Macros

#define BOOST_TEST_MODULE   connection
 

Typedefs

typedef websocketpp::client< debug_config_clientdebug_client
 
typedef websocketpp::server< debug_config_clientdebug_server
 

Functions

 BOOST_AUTO_TEST_CASE (basic_http_request)
 
void validate_func (server *s, websocketpp::connection_hdl hdl, message_ptr msg)
 
bool validate_set_ua (server *s, websocketpp::connection_hdl hdl)
 
void http_func (server *s, websocketpp::connection_hdl hdl)
 
void defer_http_func (server *s, bool *deferred, websocketpp::connection_hdl hdl)
 
void check_on_fail (server *s, websocketpp::lib::error_code ec, bool &called, websocketpp::connection_hdl hdl)
 
void on_open_print (server *s, websocketpp::connection_hdl hdl)
 
void fail_on_open (websocketpp::connection_hdl)
 
void fail_on_http (websocketpp::connection_hdl)
 
 BOOST_AUTO_TEST_CASE (connection_extensions)
 
 BOOST_AUTO_TEST_CASE (basic_websocket_request)
 
 BOOST_AUTO_TEST_CASE (http_request)
 
 BOOST_AUTO_TEST_CASE (deferred_http_request)
 
 BOOST_AUTO_TEST_CASE (request_no_server_header)
 
 BOOST_AUTO_TEST_CASE (request_no_server_header_override)
 
 BOOST_AUTO_TEST_CASE (basic_client_websocket)
 
 BOOST_AUTO_TEST_CASE (set_max_message_size)
 
 BOOST_AUTO_TEST_CASE (websocket_fail_parse_error)
 
 BOOST_AUTO_TEST_CASE (websocket_fail_invalid_version)
 
 BOOST_AUTO_TEST_CASE (websocket_fail_unsupported_version)
 
 BOOST_AUTO_TEST_CASE (websocket_fail_upgrade_required)
 
 BOOST_AUTO_TEST_CASE (client_handshake_timeout_race1)
 
 BOOST_AUTO_TEST_CASE (client_handshake_timeout_race2)
 
 BOOST_AUTO_TEST_CASE (server_handshake_timeout_race1)
 
 BOOST_AUTO_TEST_CASE (server_handshake_timeout_race2)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   connection

Definition at line 28 of file connection.cpp.

Typedef Documentation

◆ debug_client

◆ debug_server

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/17]

BOOST_AUTO_TEST_CASE ( basic_client_websocket )

Definition at line 301 of file connection.cpp.

301 {
302 std::string uri = "ws://localhost";
303
304 //std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
305
306 std::string ref = "GET / HTTP/1.1\r\nConnection: Upgrade\r\nFoo: Bar\r\nHost: localhost\r\nSec-WebSocket-Key: AAAAAAAAAAAAAAAAAAAAAA==\r\nSec-WebSocket-Version: 13\r\nUpgrade: websocket\r\nUser-Agent: foo\r\n\r\n";
307
308 std::stringstream output;
309
310 client e;
313 e.set_user_agent("foo");
314 e.register_ostream(&output);
315
317 websocketpp::lib::error_code ec;
318 con = e.get_connection(uri, ec);
319 con->append_header("Foo","Bar");
320 e.connect(con);
321
322 BOOST_CHECK_EQUAL(ref, output.str());
323}
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
Definition endpoint.hpp:196
void set_access_channels(log::level channels)
Set Access logging channel.
Definition endpoint.hpp:220
void set_error_channels(log::level channels)
Set Error logging channel.
Definition endpoint.hpp:242
static level const none
Special aggregate value representing "no levels".
Definition levels.hpp:114
static level const none
Special aggregate value representing "no levels".
Definition levels.hpp:61
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/17]

BOOST_AUTO_TEST_CASE ( basic_http_request )

Definition at line 41 of file connection.cpp.

41 {
42 std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
43 std::string output = "HTTP/1.1 426 Upgrade Required\r\nServer: " +
44 std::string(websocketpp::user_agent)+"\r\n\r\n";
45
46 std::string o2 = run_server_test(input);
47
48 BOOST_CHECK(o2 == output);
49}
std::string run_server_test(std::string input, bool log)
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/17]

BOOST_AUTO_TEST_CASE ( basic_websocket_request )

Definition at line 216 of file connection.cpp.

216 {
217 std::string input = "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\nOrigin: http://www.example.com\r\n\r\n";
218 std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: ";
219 output+=websocketpp::user_agent;
220 output+="\r\nUpgrade: websocket\r\n\r\n";
221
222 server s;
223 s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
224
225 BOOST_CHECK(run_server_test(s,input) == output);
226}
void set_message_handler(message_handler h)
Definition endpoint.hpp:322
void echo_func(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
char * s
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/17]

BOOST_AUTO_TEST_CASE ( client_handshake_timeout_race1 )

Definition at line 451 of file connection.cpp.

451 {
452 debug_client c;
453
454 websocketpp::lib::error_code ec;
455 debug_client::connection_ptr con = c.get_connection("ws://localhost:9002", ec);
456
457 BOOST_CHECK(!ec);
458
459 // This test the case where a handshake times out immediately before the
460 // handler that would have completed it gets invoked. This situation happens
461 // when clients are connecting to overloaded servers and on servers that are
462 // overloaded.
463 c.connect(con);
464
465 con->expire_timer(websocketpp::lib::error_code());
466 // Fullfil the write to simulate the write completing immediately after
467 // timer expires
468 con->fullfil_write();
469
470 BOOST_CHECK_EQUAL(con->get_ec(), make_error_code(websocketpp::error::open_handshake_timeout));
471}
@ open_handshake_timeout
WebSocket opening handshake timed out.
Definition error.hpp:114
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [5/17]

BOOST_AUTO_TEST_CASE ( client_handshake_timeout_race2 )

Definition at line 473 of file connection.cpp.

473 {
474 debug_client c;
475
476 websocketpp::lib::error_code ec;
477 debug_client::connection_ptr con = c.get_connection("ws://localhost:9002", ec);
478
479 BOOST_CHECK(!ec);
480
481 std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: ICX+Yqv66kxgM0FcWaLWlFLwTAI=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
482
483 // This test the case where a handshake times out immediately before the
484 // handler that would have completed it gets invoked. This situation happens
485 // when clients are connecting to overloaded servers and on servers that are
486 // overloaded.
487 c.connect(con);
488 con->fullfil_write();
489
490 con->expire_timer(websocketpp::lib::error_code());
491 // Read valid handshake to simulate receiving the handshake response
492 // immediately after the timer expires
493 con->read_all(output.data(),output.size());
494
495 BOOST_CHECK_EQUAL(con->get_ec(), make_error_code(websocketpp::error::open_handshake_timeout));
496}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [6/17]

BOOST_AUTO_TEST_CASE ( connection_extensions )

Definition at line 207 of file connection.cpp.

207 {
208 connection_setup env(true);
209
210 BOOST_CHECK( env.c.extension_value == 5 );
211 BOOST_CHECK( env.c.extension_method() == 5 );
212
213 BOOST_CHECK( env.c.is_server() == true );
214}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [7/17]

BOOST_AUTO_TEST_CASE ( deferred_http_request )

Definition at line 240 of file connection.cpp.

240 {
241 std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
242 std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";
243 output+=websocketpp::user_agent;
244 output+="\r\n\r\n/foo/bar";
245
246 server s;
248 bool deferred = false;
249 s.set_http_handler(bind(&defer_http_func,&s, &deferred,::_1));
250
251 s.clear_access_channels(websocketpp::log::alevel::all);
252 s.clear_error_channels(websocketpp::log::elevel::all);
253
254 std::stringstream ostream;
255 s.register_ostream(&ostream);
256
257 con = s.get_connection();
258 con->start();
259
260 BOOST_CHECK(!deferred);
261 BOOST_CHECK_EQUAL(ostream.str(), "");
262 con->read_some(input.data(),input.size());
263 BOOST_CHECK(deferred);
264 BOOST_CHECK_EQUAL(ostream.str(), "");
265
266 con->set_body(con->get_resource());
267 con->set_status(websocketpp::http::status_code::ok);
268
269 websocketpp::lib::error_code ec;
270 s.send_http_response(con->get_handle(),ec);
271 BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
272 BOOST_CHECK_EQUAL(ostream.str(), output);
273 con->send_http_response(ec);
274 BOOST_CHECK_EQUAL(ec, make_error_code(websocketpp::error::invalid_state));
275
276}
void defer_http_func(server *s, bool *deferred, websocketpp::connection_hdl hdl)
@ invalid_state
The connection was in the wrong state for this operation.
Definition error.hpp:74
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:80
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [8/17]

BOOST_AUTO_TEST_CASE ( http_request )

Definition at line 228 of file connection.cpp.

228 {
229 std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
230 std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";
231 output+=websocketpp::user_agent;
232 output+="\r\n\r\n/foo/bar";
233
234 server s;
235 s.set_http_handler(bind(&http_func,&s,::_1));
236
237 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
238}
void set_http_handler(http_handler h)
Definition endpoint.hpp:312
void http_func(server *s, websocketpp::connection_hdl hdl)
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [9/17]

BOOST_AUTO_TEST_CASE ( request_no_server_header )

Definition at line 278 of file connection.cpp.

278 {
279 std::string input = "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\nOrigin: http://www.example.com\r\n\r\n";
280 std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nUpgrade: websocket\r\n\r\n";
281
282 server s;
283 s.set_user_agent("");
284 s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
285
286 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
287}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [10/17]

BOOST_AUTO_TEST_CASE ( request_no_server_header_override )

Definition at line 289 of file connection.cpp.

289 {
290 std::string input = "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\nOrigin: http://www.example.com\r\n\r\n";
291 std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
292
293 server s;
294 s.set_user_agent("");
295 s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
296 s.set_validate_handler(bind(&validate_set_ua,&s,::_1));
297
298 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
299}
bool validate_set_ua(server *s, websocketpp::connection_hdl hdl)
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [11/17]

BOOST_AUTO_TEST_CASE ( server_handshake_timeout_race1 )

Definition at line 498 of file connection.cpp.

498 {
500
501 std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: AAAAAAAAAAAAAAAAAAAAAA==\r\n\r\n";
502
504 con->start();
505
506 con->expire_timer(websocketpp::lib::error_code());
507 // Read handshake immediately after timer expire
508 con->read_all(input.data(), input.size());
509
510 BOOST_CHECK_EQUAL(con->get_ec(), make_error_code(websocketpp::error::open_handshake_timeout));
511}
connection_ptr get_connection()
Create and initialize a new connection.
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [12/17]

BOOST_AUTO_TEST_CASE ( server_handshake_timeout_race2 )

Definition at line 513 of file connection.cpp.

513 {
515
516 std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: AAAAAAAAAAAAAAAAAAAAAA==\r\n\r\n";
517
519 con->start();
520
521 con->read_all(input.data(), input.size());
522
523 con->expire_timer(websocketpp::lib::error_code());
524 // Complete write immediately after timer expire
525 con->fullfil_write();
526
527 BOOST_CHECK_EQUAL(con->get_ec(), make_error_code(websocketpp::error::open_handshake_timeout));
528}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [13/17]

BOOST_AUTO_TEST_CASE ( set_max_message_size )

Definition at line 325 of file connection.cpp.

325 {
326 std::string input = "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";
327
328 // After the handshake, add a single frame with a message that is too long.
329 char frame0[10] = {char(0x82), char(0x83), 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01};
330 input.append(frame0, 10);
331
332 std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
333
334 // After the handshake, add a single frame with a close message with message too big
335 // error code.
336 char frame1[4] = {char(0x88), 0x19, 0x03, char(0xf1)};
337 output.append(frame1, 4);
338 output.append("A message was too large");
339
340 server s;
341 s.set_user_agent("");
342 s.set_validate_handler(bind(&validate_set_ua,&s,::_1));
343 s.set_max_message_size(2);
344
345 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
346}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [14/17]

BOOST_AUTO_TEST_CASE ( websocket_fail_invalid_version )

Definition at line 360 of file connection.cpp.

360 {
361 std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: foo\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
362
363 server s;
364 websocketpp::lib::error_code ec = make_error_code(websocketpp::error::invalid_version);
365 bool called = false;
366 s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
367
368 run_server_test(s,input,false);
369 BOOST_CHECK(called);
370}
void check_on_fail(server *s, websocketpp::lib::error_code ec, bool &called, websocketpp::connection_hdl hdl)
@ invalid_version
Invalid WebSocket protocol version.
Definition error.hpp:137
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [15/17]

BOOST_AUTO_TEST_CASE ( websocket_fail_parse_error )

Definition at line 348 of file connection.cpp.

348 {
349 std::string input = "asdf\r\n\r\n";
350
351 server s;
352 websocketpp::lib::error_code ec = make_error_code(websocketpp::error::http_parse_error);
353 bool called = false;
354 s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
355
356 run_server_test(s,input,false);
357 BOOST_CHECK(called);
358}
@ http_parse_error
HTTP parse error.
Definition error.hpp:143
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [16/17]

BOOST_AUTO_TEST_CASE ( websocket_fail_unsupported_version )

Definition at line 372 of file connection.cpp.

372 {
373 std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 12\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
374
375 server s;
376 websocketpp::lib::error_code ec = make_error_code(websocketpp::error::unsupported_version);
377 bool called = false;
378 s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
379
380 run_server_test(s,input,false);
381 BOOST_CHECK(called);
382}
@ unsupported_version
Unsupported WebSocket protocol version.
Definition error.hpp:140
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [17/17]

BOOST_AUTO_TEST_CASE ( websocket_fail_upgrade_required )

Definition at line 410 of file connection.cpp.

410 {
411 std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
412
413 server s;
414 websocketpp::lib::error_code ec = make_error_code(websocketpp::error::upgrade_required);
415 bool called = false;
416 s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
417
418 run_server_test(s,input,false);
419 BOOST_CHECK(called);
420}
Here is the call graph for this function:

◆ check_on_fail()

void check_on_fail ( server * s,
websocketpp::lib::error_code ec,
bool & called,
websocketpp::connection_hdl hdl )

Definition at line 184 of file connection.cpp.

186{
187 server::connection_ptr con = s->get_con_from_hdl(hdl);
188
189 BOOST_CHECK_EQUAL(ec, con->get_ec());
190 called = true;
191}
Here is the caller graph for this function:

◆ defer_http_func()

void defer_http_func ( server * s,
bool * deferred,
websocketpp::connection_hdl hdl )

Definition at line 175 of file connection.cpp.

175 {
176 *deferred = true;
177
178 server::connection_ptr con = s->get_con_from_hdl(hdl);
179
180 websocketpp::lib::error_code ec = con->defer_http_response();
181 BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
182}
Here is the caller graph for this function:

◆ fail_on_http()

void fail_on_http ( websocketpp::connection_hdl )

Definition at line 203 of file connection.cpp.

203 {
204 BOOST_CHECK(false);
205}

◆ fail_on_open()

void fail_on_open ( websocketpp::connection_hdl )

Definition at line 200 of file connection.cpp.

200 {
201 BOOST_CHECK(false);
202}

◆ http_func()

void http_func ( server * s,
websocketpp::connection_hdl hdl )

Definition at line 161 of file connection.cpp.

161 {
162 using namespace websocketpp::http;
163
164 server::connection_ptr con = s->get_con_from_hdl(hdl);
165
166 std::string res = con->get_resource();
167
168 con->set_body(res);
169 con->set_status(status_code::ok);
170
171 BOOST_CHECK_EQUAL(con->get_response_code(), status_code::ok);
172 BOOST_CHECK_EQUAL(con->get_response_msg(), status_code::get_string(status_code::ok));
173}
HTTP handling support.
Definition constants.hpp:39
Here is the caller graph for this function:

◆ on_open_print()

void on_open_print ( server * s,
websocketpp::connection_hdl hdl )

Definition at line 193 of file connection.cpp.

194{
195 server::connection_ptr con = s->get_con_from_hdl(hdl);
196
197 std::cout << con->get_uri() << std::endl;
198}

◆ validate_func()

void validate_func ( server * s,
websocketpp::connection_hdl hdl,
message_ptr msg )

Definition at line 151 of file connection.cpp.

151 {
152 s->send(hdl, msg->get_payload(), msg->get_opcode());
153}
Here is the call graph for this function:

◆ validate_set_ua()

bool validate_set_ua ( server * s,
websocketpp::connection_hdl hdl )

Definition at line 155 of file connection.cpp.

155 {
156 server::connection_ptr con = s->get_con_from_hdl(hdl);
157 con->replace_header("Server","foo");
158 return true;
159}
Here is the caller graph for this function: