| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "websocket.h" |
| 9 |
#include "websocketprivate.h" |
| 10 |
|
| 11 |
#include "cockpitflow.h" |
| 12 |
#include "cockpitsocket.h" |
| 13 |
#include "testlib/mock-pressure.h" |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
typedef struct { |
| 18 |
WebSocketConnection *client; |
| 19 |
WebSocketConnection *server; |
| 20 |
} Test; |
| 21 |
|
| 22 |
static void |
| 23 |
null_log_handler (const gchar *log_domain, |
| 24 |
GLogLevelFlags log_level, |
| 25 |
const gchar *message, |
| 26 |
gpointer user_data) |
| 27 |
{ |
| 28 |
/* |
| 29 |
* HACK: Use g_test_expect_message() to quieten things down once this lands: |
| 30 |
* https://bugzilla.gnome.org/show_bug.cgi?id=710991 |
| 31 |
*/ |
| 32 |
} |
| 33 |
|
| 34 |
#define WAIT_UNTIL(cond) \ |
| 35 |
G_STMT_START \ |
| 36 |
while (!(cond)) g_main_context_iteration (NULL, TRUE); \ |
| 37 |
G_STMT_END |
| 38 |
|
| 39 |
static void |
| 40 |
test_parse_url (void) |
| 41 |
{ |
| 42 |
gboolean ret; |
| 43 |
GError *error = NULL; |
| 44 |
gchar *host; |
| 45 |
gchar *path; |
| 46 |
gchar *scheme; |
| 47 |
|
| 48 |
ret = _web_socket_util_parse_url ("scheme://host:port/path/part", |
| 49 |
&scheme, &host, &path, &error); |
| 50 |
g_assert_no_error (error); |
| 51 |
g_assert (ret == TRUE); |
| 52 |
g_assert_cmpstr (scheme, ==, "scheme"); |
| 53 |
g_assert_cmpstr (host, ==, "host:port"); |
| 54 |
g_assert_cmpstr (path, ==, "/path/part"); |
| 55 |
g_free (scheme); |
| 56 |
g_free (host); |
| 57 |
g_free (path); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
test_parse_url_no_out (void) |
| 62 |
{ |
| 63 |
gboolean ret; |
| 64 |
GError *error = NULL; |
| 65 |
|
| 66 |
ret = _web_socket_util_parse_url ("scheme://host:port/path/part", |
| 67 |
NULL, NULL, NULL, &error); |
| 68 |
g_assert_no_error (error); |
| 69 |
g_assert (ret == TRUE); |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
test_parse_url_bad (void) |
| 74 |
{ |
| 75 |
const gchar *bads[] = { |
| 76 |
"/host:port/path/part", |
| 77 |
"http://@/", |
| 78 |
"http:///", |
| 79 |
"http://", |
| 80 |
}; |
| 81 |
|
| 82 |
gboolean ret; |
| 83 |
GError *error = NULL; |
| 84 |
gint i; |
| 85 |
|
| 86 |
for (i = 0; i < G_N_ELEMENTS (bads); i++) |
| 87 |
{ |
| 88 |
ret = _web_socket_util_parse_url (bads[i], NULL, NULL, NULL, &error); |
| 89 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT); |
| 90 |
g_assert (ret == FALSE); |
| 91 |
g_clear_error (&error); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
test_parse_url_no_path (void) |
| 97 |
{ |
| 98 |
gboolean ret; |
| 99 |
gchar *path; |
| 100 |
|
| 101 |
ret = _web_socket_util_parse_url ("scheme://host:port", |
| 102 |
NULL, NULL, &path, NULL); |
| 103 |
g_assert (ret == TRUE); |
| 104 |
g_assert_cmpstr (path, ==, "/"); |
| 105 |
g_free (path); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
test_parse_url_with_user (void) |
| 110 |
{ |
| 111 |
gboolean ret; |
| 112 |
gchar *host; |
| 113 |
|
| 114 |
ret = _web_socket_util_parse_url ("scheme://user:password@host", |
| 115 |
NULL, &host, NULL, NULL); |
| 116 |
g_assert (ret == TRUE); |
| 117 |
g_assert_cmpstr (host, ==, "host"); |
| 118 |
g_free (host); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
test_parse_req (void) |
| 123 |
{ |
| 124 |
const gchar *reqs[] = { |
| 125 |
"GET /path/part HTTP/1.0\r\n ", |
| 126 |
"GET /path/part HTTP/1.0\n ", |
| 127 |
"GET /path/part HTTP/1.0 \r\n ", |
| 128 |
}; |
| 129 |
|
| 130 |
for (gint i = 0; i < G_N_ELEMENTS (reqs); i++) |
| 131 |
{ |
| 132 |
gchar *path; |
| 133 |
gchar *method; |
| 134 |
|
| 135 |
gssize ret = web_socket_util_parse_req_line (reqs[i], strlen (reqs[i]), &method, &path); |
| 136 |
g_assert_cmpint (ret, ==, strlen (reqs[i]) - 2); |
| 137 |
g_assert_cmpstr (method, ==, "GET"); |
| 138 |
g_assert_cmpstr (path, ==, "/path/part"); |
| 139 |
g_free (method); |
| 140 |
g_free (path); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
test_parse_req_no_out (void) |
| 146 |
{ |
| 147 |
const gchar *data = "GET /path/part HTTP/1.0\r\n "; |
| 148 |
gssize ret; |
| 149 |
|
| 150 |
ret = web_socket_util_parse_req_line (data, strlen (data), NULL, NULL); |
| 151 |
g_assert_cmpint (ret, ==, 25); |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
test_parse_req_not_enough (void) |
| 156 |
{ |
| 157 |
const gchar *data = "GET /path/par"; |
| 158 |
gssize ret; |
| 159 |
|
| 160 |
ret = web_socket_util_parse_req_line (data, strlen (data), NULL, NULL); |
| 161 |
g_assert_cmpint (ret, ==, 0); |
| 162 |
} |
| 163 |
|
| 164 |
static void |
| 165 |
test_parse_req_bad (void) |
| 166 |
{ |
| 167 |
const gchar *bads[] = { |
| 168 |
" GET /path/part HTTP/1.0\r\n ", |
| 169 |
"GET /path/part\r\n ", |
| 170 |
"GET /path/part HTTP/4.4\r\n ", |
| 171 |
"GET /path/part HTTP/1.0X\r\n ", |
| 172 |
"GET /path/part XXX/2\r\n ", |
| 173 |
"TESTONE\r\n ", |
| 174 |
}; |
| 175 |
|
| 176 |
gssize ret; |
| 177 |
gint i; |
| 178 |
|
| 179 |
for (i = 0; i < G_N_ELEMENTS (bads); i++) |
| 180 |
{ |
| 181 |
ret = web_socket_util_parse_req_line (bads[i], strlen (bads[i]), NULL, NULL); |
| 182 |
g_assert_cmpint (ret, ==, -1); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
test_parse_status (void) |
| 188 |
{ |
| 189 |
const gchar *lines[] = { |
| 190 |
"HTTP/1.0 101 Switching Protocols\r\n ", |
| 191 |
"HTTP/1.0 101 Switching Protocols\n ", |
| 192 |
"HTTP/1.1 101 Switching Protocols \r\n ", |
| 193 |
}; |
| 194 |
|
| 195 |
for (gint i = 0; i < G_N_ELEMENTS (lines); i++) |
| 196 |
{ |
| 197 |
guint status; |
| 198 |
gchar *reason; |
| 199 |
|
| 200 |
gssize ret = web_socket_util_parse_status_line (lines[i], strlen (lines[i]), NULL, &status, &reason); |
| 201 |
g_assert_cmpint (ret, ==, strlen (lines[i]) - 2); |
| 202 |
g_assert_cmpuint (status, ==, 101); |
| 203 |
g_assert_cmpstr (reason, ==, "Switching Protocols"); |
| 204 |
g_free (reason); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
static void |
| 209 |
test_parse_status_no_out (void) |
| 210 |
{ |
| 211 |
const gchar *line = "HTTP/1.0 101 Switching Protocols\r\n "; |
| 212 |
gssize ret; |
| 213 |
|
| 214 |
ret = web_socket_util_parse_status_line (line, strlen (line), NULL, NULL, NULL); |
| 215 |
g_assert_cmpint (ret, ==, strlen (line) - 2); |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
test_parse_status_not_enough (void) |
| 220 |
{ |
| 221 |
const gchar *data = "HTTP/"; |
| 222 |
gssize ret; |
| 223 |
|
| 224 |
ret = web_socket_util_parse_status_line (data, strlen (data), NULL, NULL, NULL); |
| 225 |
g_assert_cmpint (ret, ==, 0); |
| 226 |
} |
| 227 |
|
| 228 |
static void |
| 229 |
test_parse_status_bad (void) |
| 230 |
{ |
| 231 |
const gchar *lines[] = { |
| 232 |
" HTTP/1.0 101 Switching Protocols\r\n ", |
| 233 |
"HTTP/1.0 101\r\n ", |
| 234 |
"HTTP/1.1 1A01 Switching Protocols \r\n ", |
| 235 |
"TESTONE\r\n ", |
| 236 |
}; |
| 237 |
|
| 238 |
gssize ret; |
| 239 |
gint i; |
| 240 |
|
| 241 |
for (i = 0; i < G_N_ELEMENTS (lines); i++) |
| 242 |
{ |
| 243 |
ret = web_socket_util_parse_status_line (lines[i], strlen (lines[i]), NULL, NULL, NULL); |
| 244 |
g_assert_cmpint (ret, ==, -1); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
static void |
| 249 |
test_parse_version_1_0 (void) |
| 250 |
{ |
| 251 |
gchar *version; |
| 252 |
const gchar *line; |
| 253 |
gssize ret; |
| 254 |
|
| 255 |
line = "HTTP/1.0 101 Switching Protocols\r\n "; |
| 256 |
ret = web_socket_util_parse_status_line (line, strlen (line), &version, NULL, NULL); |
| 257 |
g_assert_cmpint (ret, ==, strlen (line) - 2); |
| 258 |
g_assert_cmpstr (version, ==, "HTTP/1.0"); |
| 259 |
g_free (version); |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
test_parse_version_1_1 (void) |
| 264 |
{ |
| 265 |
gchar *version; |
| 266 |
const gchar *line; |
| 267 |
gssize ret; |
| 268 |
|
| 269 |
line = "HTTP/1.1 101 Switching Protocols\r\n "; |
| 270 |
ret = web_socket_util_parse_status_line (line, strlen (line), &version, NULL, NULL); |
| 271 |
g_assert_cmpint (ret, ==, strlen (line) - 2); |
| 272 |
g_assert_cmpstr (version, ==, "HTTP/1.1"); |
| 273 |
g_free (version); |
| 274 |
} |
| 275 |
|
| 276 |
static void |
| 277 |
test_parse_headers (void) |
| 278 |
{ |
| 279 |
GHashTable *headers; |
| 280 |
gssize ret; |
| 281 |
|
| 282 |
const gchar *input = |
| 283 |
"Header1: value3\r\n" |
| 284 |
"Header2: field\r\n" |
| 285 |
"Head3: Another \r\n" |
| 286 |
"Host:https://cockpit-project.org\r\n" |
| 287 |
"Funny: a☺b\r\n" |
| 288 |
"\r\n" |
| 289 |
"BODY "; |
| 290 |
|
| 291 |
ret = web_socket_util_parse_headers (input, strlen (input), &headers); |
| 292 |
g_assert_cmpint (ret, ==, strlen (input) - 6); |
| 293 |
g_assert_cmpstr (g_hash_table_lookup (headers, "header1"), ==, "value3"); |
| 294 |
g_assert_cmpstr (g_hash_table_lookup (headers, "Header2"), ==, "field"); |
| 295 |
g_assert_cmpstr (g_hash_table_lookup (headers, "hEAD3"), ==, "Another"); |
| 296 |
g_assert_cmpstr (g_hash_table_lookup (headers, "Host"), ==, "https://cockpit-project.org"); |
| 297 |
g_assert_cmpstr (g_hash_table_lookup (headers, "Funny"), ==, "a☺b"); |
| 298 |
g_assert (g_hash_table_lookup (headers, "Something else") == NULL); |
| 299 |
g_hash_table_unref (headers); |
| 300 |
} |
| 301 |
|
| 302 |
static void |
| 303 |
test_parse_duplicate_headers (void) |
| 304 |
{ |
| 305 |
GHashTable *headers; |
| 306 |
gssize ret; |
| 307 |
|
| 308 |
const gchar *input = |
| 309 |
"header1: value2\r\n" |
| 310 |
"Header1: value3\r\n" |
| 311 |
"\r\n" |
| 312 |
"BODY "; |
| 313 |
|
| 314 |
ret = web_socket_util_parse_headers (input, strlen (input), &headers); |
| 315 |
g_assert_cmpint (ret, ==, strlen (input) - 6); |
| 316 |
g_assert_cmpstr (g_hash_table_lookup (headers, "header1"), ==, "value3"); |
| 317 |
g_assert (g_hash_table_lookup (headers, "Something else") == NULL); |
| 318 |
g_hash_table_unref (headers); |
| 319 |
} |
| 320 |
|
| 321 |
static void |
| 322 |
test_parse_headers_no_out (void) |
| 323 |
{ |
| 324 |
const gchar *input = |
| 325 |
"Header1: value3\r\n" |
| 326 |
"Header2: field\r\n" |
| 327 |
"Head3: Another \r\n" |
| 328 |
"\r\n" |
| 329 |
"BODY "; |
| 330 |
|
| 331 |
gssize ret; |
| 332 |
|
| 333 |
ret = web_socket_util_parse_headers (input, strlen (input), NULL); |
| 334 |
g_assert_cmpint (ret, ==, strlen (input) - 6); |
| 335 |
} |
| 336 |
|
| 337 |
static void |
| 338 |
test_parse_headers_not_enough (void) |
| 339 |
{ |
| 340 |
const gchar *input = |
| 341 |
"Header1: value3\r\n" |
| 342 |
"Header2: field\r\n" |
| 343 |
"Head3: Another"; |
| 344 |
|
| 345 |
gssize ret; |
| 346 |
|
| 347 |
ret = web_socket_util_parse_headers (input, strlen (input), NULL); |
| 348 |
g_assert_cmpint (ret, ==, 0); |
| 349 |
} |
| 350 |
|
| 351 |
static void |
| 352 |
test_parse_headers_bad (void) |
| 353 |
{ |
| 354 |
const gchar *input[] = { |
| 355 |
/* missing : */ |
| 356 |
"Header1 value3\r\n" |
| 357 |
"\r\n" |
| 358 |
"BODY ", |
| 359 |
|
| 360 |
/* binary garbage (not even UTF8) */ |
| 361 |
"Header1: a\xFF\x01b\r\n" |
| 362 |
"\r\n" |
| 363 |
"BODY ", |
| 364 |
}; |
| 365 |
|
| 366 |
gssize ret; |
| 367 |
gint i; |
| 368 |
|
| 369 |
for (i = 0; i < G_N_ELEMENTS (input); i++) |
| 370 |
{ |
| 371 |
ret = web_socket_util_parse_headers (input[i], strlen (input[i]), NULL); |
| 372 |
g_assert_cmpint (ret, ==, -1); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
test_header_equals (void) |
| 378 |
{ |
| 379 |
GHashTable *headers = web_socket_util_new_headers (); |
| 380 |
g_hash_table_insert (headers, g_strdup ("Blah"), g_strdup ("VALUE")); |
| 381 |
g_hash_table_insert (headers, g_strdup ("Funny"), g_strdup ("a☺b")); |
| 382 |
|
| 383 |
g_assert (_web_socket_util_header_equals (headers, "blah", "Value")); |
| 384 |
g_assert (_web_socket_util_header_equals (headers, "Funny", "a☺b")); |
| 385 |
|
| 386 |
g_test_expect_message ("WebSocket", G_LOG_LEVEL_MESSAGE, |
| 387 |
"received invalid or missing Blah header*"); |
| 388 |
g_assert (!_web_socket_util_header_equals (headers, "Blah", "test")); |
| 389 |
|
| 390 |
g_test_expect_message ("WebSocket", G_LOG_LEVEL_MESSAGE, |
| 391 |
"received invalid or missing Extra header*"); |
| 392 |
g_assert (!_web_socket_util_header_equals (headers, "Extra", "test")); |
| 393 |
g_hash_table_unref (headers); |
| 394 |
} |
| 395 |
|
| 396 |
static void |
| 397 |
test_header_contains (void) |
| 398 |
{ |
| 399 |
GHashTable *headers = web_socket_util_new_headers (); |
| 400 |
g_hash_table_insert (headers, g_strdup ("Blah"), g_strdup ("one two three")); |
| 401 |
|
| 402 |
g_assert (_web_socket_util_header_contains (headers, "blah", "one")); |
| 403 |
g_assert (_web_socket_util_header_contains (headers, "blah", "two")); |
| 404 |
g_assert (_web_socket_util_header_contains (headers, "blah", "three")); |
| 405 |
|
| 406 |
g_test_expect_message ("WebSocket", G_LOG_LEVEL_MESSAGE, |
| 407 |
"received invalid or missing Blah header*"); |
| 408 |
g_assert (!_web_socket_util_header_contains (headers, "Blah", "thre")); |
| 409 |
|
| 410 |
g_test_expect_message ("WebSocket", G_LOG_LEVEL_MESSAGE, |
| 411 |
"received invalid or missing Blah header*"); |
| 412 |
g_assert (!_web_socket_util_header_contains (headers, "Blah", "four")); |
| 413 |
|
| 414 |
g_test_expect_message ("WebSocket", G_LOG_LEVEL_MESSAGE, |
| 415 |
"received invalid or missing Extra header*"); |
| 416 |
g_assert (!_web_socket_util_header_contains (headers, "Extra", "test")); |
| 417 |
g_hash_table_unref (headers); |
| 418 |
} |
| 419 |
|
| 420 |
static void |
| 421 |
test_header_empty (void) |
| 422 |
{ |
| 423 |
GHashTable *headers = web_socket_util_new_headers (); |
| 424 |
g_hash_table_insert (headers, g_strdup ("Empty"), g_strdup ("")); |
| 425 |
g_hash_table_insert (headers, g_strdup ("Blah"), g_strdup ("value")); |
| 426 |
|
| 427 |
g_assert (_web_socket_util_header_empty (headers, "empty")); |
| 428 |
g_assert (_web_socket_util_header_empty (headers, "Another")); |
| 429 |
|
| 430 |
g_test_expect_message ("WebSocket", G_LOG_LEVEL_MESSAGE, |
| 431 |
"received unsupported Blah header*"); |
| 432 |
g_assert (!_web_socket_util_header_empty (headers, "Blah")); |
| 433 |
g_hash_table_unref (headers); |
| 434 |
} |
| 435 |
|
| 436 |
static gboolean |
| 437 |
on_error_not_reached (WebSocketConnection *ws, |
| 438 |
GError *error, |
| 439 |
gpointer user_data) |
| 440 |
{ |
| 441 |
/* At this point we know this will fail, but is informative */ |
| 442 |
g_assert_no_error (error); |
| 443 |
return TRUE; |
| 444 |
} |
| 445 |
|
| 446 |
static gboolean |
| 447 |
on_error_copy (WebSocketConnection *ws, |
| 448 |
GError *error, |
| 449 |
gpointer user_data) |
| 450 |
{ |
| 451 |
GError **copy = user_data; |
| 452 |
g_assert (*copy == NULL); |
| 453 |
*copy = g_error_copy (error); |
| 454 |
return TRUE; |
| 455 |
} |
| 456 |
|
| 457 |
static void |
| 458 |
setup_pair (Test *test, |
| 459 |
gconstpointer data) |
| 460 |
{ |
| 461 |
GIOStream *ioc; |
| 462 |
GIOStream *ios; |
| 463 |
|
| 464 |
cockpit_socket_streampair (&ioc, &ios); |
| 465 |
|
| 466 |
test->server = web_socket_server_new_for_stream (NULL, NULL, ios, NULL, NULL); |
| 467 |
test->client = web_socket_client_new_for_stream ("ws://localhost/unix", NULL, NULL, ioc); |
| 468 |
|
| 469 |
g_signal_connect (test->server, "error", G_CALLBACK (on_error_not_reached), NULL); |
| 470 |
|
| 471 |
g_object_unref (ioc); |
| 472 |
g_object_unref (ios); |
| 473 |
} |
| 474 |
|
| 475 |
static void |
| 476 |
teardown (Test *test, |
| 477 |
gconstpointer data) |
| 478 |
{ |
| 479 |
g_clear_object (&test->client); |
| 480 |
g_clear_object (&test->server); |
| 481 |
} |
| 482 |
|
| 483 |
static gboolean |
| 484 |
on_timeout_set_flag (gpointer user_data) |
| 485 |
{ |
| 486 |
gboolean *data = user_data; |
| 487 |
g_assert (user_data); |
| 488 |
g_assert (*data == FALSE); |
| 489 |
*data = TRUE; |
| 490 |
return FALSE; |
| 491 |
} |
| 492 |
|
| 493 |
static void |
| 494 |
on_text_message (WebSocketConnection *ws, |
| 495 |
WebSocketDataType type, |
| 496 |
GBytes *message, |
| 497 |
gpointer user_data) |
| 498 |
{ |
| 499 |
GBytes **receive = user_data; |
| 500 |
g_assert_cmpint (type, ==, WEB_SOCKET_DATA_TEXT); |
| 501 |
g_assert (*receive == NULL); |
| 502 |
g_assert (message != NULL); |
| 503 |
*receive = g_bytes_ref (message); |
| 504 |
} |
| 505 |
|
| 506 |
static void |
| 507 |
on_message_append (WebSocketConnection *ws, |
| 508 |
WebSocketDataType type, |
| 509 |
GBytes *message, |
| 510 |
gpointer user_data) |
| 511 |
{ |
| 512 |
GByteArray *received = user_data; |
| 513 |
g_assert (received != NULL); |
| 514 |
g_byte_array_append (received, g_bytes_get_data (message, NULL), g_bytes_get_size (message)); |
| 515 |
} |
| 516 |
|
| 517 |
static void |
| 518 |
on_close_set_flag (WebSocketConnection *ws, |
| 519 |
gpointer user_data) |
| 520 |
{ |
| 521 |
gboolean *flag = user_data; |
| 522 |
g_assert (*flag == FALSE); |
| 523 |
*flag = TRUE; |
| 524 |
} |
| 525 |
|
| 526 |
static void |
| 527 |
on_open_set_flag (WebSocketConnection *ws, |
| 528 |
gpointer user_data) |
| 529 |
{ |
| 530 |
gboolean *flag = user_data; |
| 531 |
g_assert (*flag == FALSE); |
| 532 |
*flag = TRUE; |
| 533 |
} |
| 534 |
|
| 535 |
static void |
| 536 |
test_handshake (Test *test, |
| 537 |
gconstpointer data) |
| 538 |
{ |
| 539 |
gboolean open_event_client = FALSE; |
| 540 |
gboolean open_event_server = FALSE; |
| 541 |
GHashTable *headers; |
| 542 |
|
| 543 |
g_signal_connect (test->client, "open", G_CALLBACK (on_open_set_flag), &open_event_client); |
| 544 |
g_signal_connect (test->server, "open", G_CALLBACK (on_open_set_flag), &open_event_server); |
| 545 |
|
| 546 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 547 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->client), ==, WEB_SOCKET_STATE_OPEN); |
| 548 |
|
| 549 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) != WEB_SOCKET_STATE_CONNECTING); |
| 550 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_OPEN); |
| 551 |
|
| 552 |
headers = web_socket_client_get_headers (WEB_SOCKET_CLIENT (test->client)); |
| 553 |
g_assert (headers != NULL); |
| 554 |
#if 0 |
| 555 |
GHashTableIter iter; |
| 556 |
gpointer key, value; |
| 557 |
g_hash_table_iter_init (&iter, headers); |
| 558 |
while (g_hash_table_iter_next (&iter, &key, &value)) |
| 559 |
g_printerr ("headers: %s %s\n", (gchar *)key, (gchar *)value); |
| 560 |
#endif |
| 561 |
g_assert_cmpstr (g_hash_table_lookup (headers, "connection"), ==, "Upgrade"); |
| 562 |
|
| 563 |
g_assert (open_event_client); |
| 564 |
g_assert (open_event_server); |
| 565 |
} |
| 566 |
|
| 567 |
static void |
| 568 |
test_send_client_to_server (Test *test, |
| 569 |
gconstpointer data) |
| 570 |
{ |
| 571 |
GBytes *sent = NULL; |
| 572 |
GBytes *received = NULL; |
| 573 |
const gchar *contents; |
| 574 |
gsize len; |
| 575 |
|
| 576 |
g_signal_connect (test->server, "message", G_CALLBACK (on_text_message), &received); |
| 577 |
|
| 578 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 579 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->client), ==, WEB_SOCKET_STATE_OPEN); |
| 580 |
|
| 581 |
sent = g_bytes_new ("this is a test", 14); |
| 582 |
web_socket_connection_send (test->client, WEB_SOCKET_DATA_TEXT, NULL, sent); |
| 583 |
|
| 584 |
WAIT_UNTIL (received != NULL); |
| 585 |
|
| 586 |
g_assert (g_bytes_equal (sent, received)); |
| 587 |
|
| 588 |
/* Received messages should be null terminated (outside of len) */ |
| 589 |
contents = g_bytes_get_data (received, &len); |
| 590 |
g_assert (contents[len] == '\0'); |
| 591 |
|
| 592 |
g_bytes_unref (sent); |
| 593 |
g_bytes_unref (received); |
| 594 |
} |
| 595 |
|
| 596 |
static void |
| 597 |
test_send_server_to_client (Test *test, |
| 598 |
gconstpointer data) |
| 599 |
{ |
| 600 |
GBytes *sent = NULL; |
| 601 |
GBytes *received = NULL; |
| 602 |
const gchar *contents; |
| 603 |
gsize len; |
| 604 |
|
| 605 |
g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received); |
| 606 |
|
| 607 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) != WEB_SOCKET_STATE_CONNECTING); |
| 608 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_OPEN); |
| 609 |
|
| 610 |
sent = g_bytes_new ("this is a test", 14); |
| 611 |
web_socket_connection_send (test->server, WEB_SOCKET_DATA_TEXT, NULL, sent); |
| 612 |
|
| 613 |
WAIT_UNTIL (received != NULL); |
| 614 |
|
| 615 |
g_assert (g_bytes_equal (sent, received)); |
| 616 |
|
| 617 |
/* Received messages should be null terminated (outside of len) */ |
| 618 |
contents = g_bytes_get_data (received, &len); |
| 619 |
g_assert (contents[len] == '\0'); |
| 620 |
|
| 621 |
g_bytes_unref (sent); |
| 622 |
g_bytes_unref (received); |
| 623 |
} |
| 624 |
|
| 625 |
static void |
| 626 |
test_send_big_packets (Test *test, |
| 627 |
gconstpointer data) |
| 628 |
{ |
| 629 |
GBytes *sent = NULL; |
| 630 |
GBytes *received = NULL; |
| 631 |
|
| 632 |
g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received); |
| 633 |
|
| 634 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) != WEB_SOCKET_STATE_CONNECTING); |
| 635 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_OPEN); |
| 636 |
|
| 637 |
sent = g_bytes_new_take (g_strnfill (400, '!'), 400); |
| 638 |
web_socket_connection_send (test->server, WEB_SOCKET_DATA_TEXT, NULL, sent); |
| 639 |
WAIT_UNTIL (received != NULL); |
| 640 |
g_assert (g_bytes_equal (sent, received)); |
| 641 |
g_bytes_unref (sent); |
| 642 |
g_bytes_unref (received); |
| 643 |
received = NULL; |
| 644 |
|
| 645 |
sent = g_bytes_new_take (g_strnfill (100 * 1000, '?'), 100 * 1000); |
| 646 |
web_socket_connection_send (test->server, WEB_SOCKET_DATA_TEXT, NULL, sent); |
| 647 |
WAIT_UNTIL (received != NULL); |
| 648 |
g_assert (g_bytes_equal (sent, received)); |
| 649 |
g_bytes_unref (sent); |
| 650 |
g_bytes_unref (received); |
| 651 |
} |
| 652 |
|
| 653 |
static void |
| 654 |
on_pressure_set_throttle (WebSocketConnection *socket, |
| 655 |
gboolean throttle, |
| 656 |
gpointer user_data) |
| 657 |
{ |
| 658 |
gint *data = user_data; |
| 659 |
g_assert (user_data != NULL); |
| 660 |
*data = throttle ? 1 : 0; |
| 661 |
} |
| 662 |
|
| 663 |
static void |
| 664 |
test_pressure_queue (Test *test, |
| 665 |
gconstpointer data) |
| 666 |
{ |
| 667 |
GBytes *sent = NULL; |
| 668 |
gint throttle = -1; |
| 669 |
gint i; |
| 670 |
|
| 671 |
g_signal_connect (test->server, "pressure", G_CALLBACK (on_pressure_set_throttle), &throttle); |
| 672 |
|
| 673 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) != WEB_SOCKET_STATE_CONNECTING); |
| 674 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_OPEN); |
| 675 |
|
| 676 |
sent = g_bytes_new_take (g_strnfill (10 * 1000, '!'), 10 * 1000); |
| 677 |
for (i = 0; i < 1000; i++) |
| 678 |
web_socket_connection_send (test->server, WEB_SOCKET_DATA_TEXT, NULL, sent); |
| 679 |
g_bytes_unref (sent); |
| 680 |
|
| 681 |
/* |
| 682 |
* This should have put way too much in the queue, and thus |
| 683 |
* emitted the back-pressure signal. This signal would normally |
| 684 |
* be used by others to slow down their queueing, but in this |
| 685 |
* case we just check that it was fired. |
| 686 |
*/ |
| 687 |
g_assert_cmpint (throttle, ==, 1); |
| 688 |
throttle = -1; |
| 689 |
|
| 690 |
/* |
| 691 |
* Now the queue is getting drained. At some point, it will be |
| 692 |
* signaled that back pressure has been turned off |
| 693 |
*/ |
| 694 |
while (throttle == -1) |
| 695 |
g_main_context_iteration (NULL, TRUE); |
| 696 |
|
| 697 |
g_assert_cmpint (throttle, ==, 0); |
| 698 |
} |
| 699 |
|
| 700 |
static void |
| 701 |
test_pressure_throttle (Test *test, |
| 702 |
gconstpointer data) |
| 703 |
{ |
| 704 |
CockpitFlow *pressure = mock_pressure_new (); |
| 705 |
GBytes *sent = NULL; |
| 706 |
GByteArray *received = NULL; |
| 707 |
gboolean timeout = FALSE; |
| 708 |
gsize length; |
| 709 |
gint i; |
| 710 |
|
| 711 |
received = g_byte_array_new (); |
| 712 |
cockpit_flow_throttle (COCKPIT_FLOW (test->client), pressure); |
| 713 |
g_signal_connect (test->client, "message", G_CALLBACK (on_message_append), received); |
| 714 |
|
| 715 |
while (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_CONNECTING) |
| 716 |
g_main_context_iteration (NULL, TRUE); |
| 717 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_OPEN); |
| 718 |
|
| 719 |
/* Send this a thousand times over the socket */ |
| 720 |
sent = g_bytes_new_take (g_strnfill (10 * 1000, '?'), 10 * 1000); |
| 721 |
for (i = 0; i < 1000; i++) |
| 722 |
web_socket_connection_send (test->server, WEB_SOCKET_DATA_TEXT, NULL, sent); |
| 723 |
g_bytes_unref (sent); |
| 724 |
|
| 725 |
/* |
| 726 |
* So we should start receiving the echoed data. But we apply |
| 727 |
* the throttle pressure after receiving some data, and the rest |
| 728 |
* just waits. |
| 729 |
*/ |
| 730 |
while (received->len == 0) |
| 731 |
g_main_context_iteration (NULL, TRUE); |
| 732 |
g_signal_emit_by_name (pressure, "pressure", TRUE); |
| 733 |
|
| 734 |
length = received->len; |
| 735 |
g_assert_cmpint (length, <, 10 * 1000 * 1000); |
| 736 |
|
| 737 |
/* Now remaining data input should wait, no further data received*/ |
| 738 |
g_timeout_add_seconds (2, on_timeout_set_flag, &timeout); |
| 739 |
while (timeout == FALSE) |
| 740 |
g_main_context_iteration (NULL, TRUE); |
| 741 |
g_assert_cmpint (length, ==, received->len); |
| 742 |
|
| 743 |
/* Remove the pressure, and we should get more data */ |
| 744 |
g_signal_emit_by_name (pressure, "pressure", FALSE); |
| 745 |
while (length < received->len) |
| 746 |
g_main_context_iteration (NULL, TRUE); |
| 747 |
|
| 748 |
/* Clearing the throttle should work too. This pressure signal has no effect */ |
| 749 |
cockpit_flow_throttle (COCKPIT_FLOW (test->client), NULL); |
| 750 |
g_signal_emit_by_name (pressure, "pressure", TRUE); |
| 751 |
|
| 752 |
/* Now wait for the remaining data */ |
| 753 |
while (received->len < 10 * 1000 * 1000) |
| 754 |
g_main_context_iteration (NULL, TRUE); |
| 755 |
|
| 756 |
g_byte_array_free (received, TRUE); |
| 757 |
g_object_unref (pressure); |
| 758 |
} |
| 759 |
|
| 760 |
static void |
| 761 |
test_send_prefixed (Test *test, |
| 762 |
gconstpointer data) |
| 763 |
{ |
| 764 |
GBytes *prefix = NULL; |
| 765 |
GBytes *payload = NULL; |
| 766 |
GBytes *received = NULL; |
| 767 |
|
| 768 |
g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received); |
| 769 |
|
| 770 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) != WEB_SOCKET_STATE_CONNECTING); |
| 771 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_OPEN); |
| 772 |
|
| 773 |
prefix = g_bytes_new_static ("funny ", 6); |
| 774 |
payload = g_bytes_new_static ("thing", 5); |
| 775 |
|
| 776 |
web_socket_connection_send (test->server, WEB_SOCKET_DATA_TEXT, prefix, payload); |
| 777 |
WAIT_UNTIL (received != NULL); |
| 778 |
g_assert_cmpstr (g_bytes_get_data (received, NULL), ==, "funny thing"); |
| 779 |
g_assert_cmpint (g_bytes_get_size (received), ==, 11); |
| 780 |
g_bytes_unref (payload); |
| 781 |
g_bytes_unref (prefix); |
| 782 |
g_bytes_unref (received); |
| 783 |
} |
| 784 |
|
| 785 |
static void |
| 786 |
test_send_bad_data (Test *test, |
| 787 |
gconstpointer unused) |
| 788 |
{ |
| 789 |
GError *error = NULL; |
| 790 |
GIOStream *io; |
| 791 |
gsize written; |
| 792 |
const gchar *frame; |
| 793 |
guint logid; |
| 794 |
|
| 795 |
g_signal_handlers_disconnect_by_func (test->server, on_error_not_reached, NULL); |
| 796 |
g_signal_connect (test->server, "error", G_CALLBACK (on_error_copy), &error); |
| 797 |
|
| 798 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 799 |
|
| 800 |
io = web_socket_connection_get_io_stream (test->client); |
| 801 |
|
| 802 |
logid = g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, null_log_handler, NULL); |
| 803 |
|
| 804 |
/* Bad UTF-8 raw frames */ |
| 805 |
frame = "\x81\x04\xEE\xEE\xEE\xEE"; |
| 806 |
|
| 807 |
if (!g_output_stream_write_all (g_io_stream_get_output_stream (io), |
| 808 |
frame, 6, &written, NULL, NULL)) |
| 809 |
g_assert_not_reached (); |
| 810 |
g_assert_cmpuint (written, ==, 6); |
| 811 |
|
| 812 |
WAIT_UNTIL (error != NULL); |
| 813 |
g_assert_error (error, WEB_SOCKET_ERROR, WEB_SOCKET_CLOSE_BAD_DATA); |
| 814 |
|
| 815 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_CLOSED); |
| 816 |
|
| 817 |
g_assert_cmpuint (web_socket_connection_get_close_code (test->client), ==, WEB_SOCKET_CLOSE_BAD_DATA); |
| 818 |
g_error_free (error); |
| 819 |
|
| 820 |
g_log_remove_handler (G_LOG_DOMAIN, logid); |
| 821 |
} |
| 822 |
|
| 823 |
static void |
| 824 |
test_protocol_negotiate (Test *test, |
| 825 |
gconstpointer unused) |
| 826 |
{ |
| 827 |
const gchar *server_protocols[] = { "aaa", "bbb", "ccc", NULL }; |
| 828 |
const gchar *client_protocols[] = { "bbb", "ccc", NULL }; |
| 829 |
|
| 830 |
g_object_set (test->server, "protocols", server_protocols, NULL); |
| 831 |
g_object_set (test->client, "protocols", client_protocols, NULL); |
| 832 |
|
| 833 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 834 |
g_assert_cmpstr (web_socket_connection_get_protocol (test->client), ==, "bbb"); |
| 835 |
g_assert_cmpstr (web_socket_connection_get_protocol (test->server), ==, "bbb"); |
| 836 |
} |
| 837 |
|
| 838 |
static void |
| 839 |
test_protocol_mismatch (Test *test, |
| 840 |
gconstpointer unused) |
| 841 |
{ |
| 842 |
GError *error = NULL; |
| 843 |
guint logid; |
| 844 |
|
| 845 |
const gchar *server_protocols[] = { "aaa", "bbb", "ccc", NULL }; |
| 846 |
const gchar *client_protocols[] = { "ddd", NULL }; |
| 847 |
|
| 848 |
g_signal_handlers_disconnect_by_func (test->client, on_error_not_reached, NULL); |
| 849 |
g_signal_handlers_disconnect_by_func (test->server, on_error_not_reached, NULL); |
| 850 |
g_signal_connect (test->client, "error", G_CALLBACK (on_error_copy), &error); |
| 851 |
|
| 852 |
logid = g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, null_log_handler, NULL); |
| 853 |
|
| 854 |
g_object_set (test->server, "protocols", server_protocols, NULL); |
| 855 |
g_object_set (test->client, "protocols", client_protocols, NULL); |
| 856 |
|
| 857 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 858 |
|
| 859 |
g_assert_error (error, WEB_SOCKET_ERROR, WEB_SOCKET_CLOSE_PROTOCOL); |
| 860 |
g_error_free (error); |
| 861 |
|
| 862 |
g_log_remove_handler (G_LOG_DOMAIN, logid); |
| 863 |
} |
| 864 |
|
| 865 |
static void |
| 866 |
test_protocol_server_any (Test *test, |
| 867 |
gconstpointer unused) |
| 868 |
{ |
| 869 |
GError *error = NULL; |
| 870 |
|
| 871 |
/* Server accepts any protocol */ |
| 872 |
const gchar *client_protocols[] = { "aaa", "bbb", "ccc", NULL }; |
| 873 |
|
| 874 |
g_signal_handlers_disconnect_by_func (test->client, on_error_not_reached, NULL); |
| 875 |
g_signal_connect (test->client, "error", G_CALLBACK (on_error_copy), &error); |
| 876 |
|
| 877 |
g_object_set (test->client, "protocols", client_protocols, NULL); |
| 878 |
|
| 879 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 880 |
|
| 881 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 882 |
g_assert_cmpstr (web_socket_connection_get_protocol (test->client), ==, "aaa"); |
| 883 |
g_assert_cmpstr (web_socket_connection_get_protocol (test->server), ==, "aaa"); |
| 884 |
|
| 885 |
g_clear_error (&error); |
| 886 |
} |
| 887 |
|
| 888 |
static void |
| 889 |
test_protocol_client_any (Test *test, |
| 890 |
gconstpointer unused) |
| 891 |
{ |
| 892 |
GError *error = NULL; |
| 893 |
|
| 894 |
/* Client accepts any protocol */ |
| 895 |
const gchar *server_protocols[] = { "aaa", "bbb", "ccc", NULL }; |
| 896 |
|
| 897 |
g_signal_handlers_disconnect_by_func (test->client, on_error_not_reached, NULL); |
| 898 |
g_signal_connect (test->client, "error", G_CALLBACK (on_error_copy), &error); |
| 899 |
|
| 900 |
g_object_set (test->server, "protocols", server_protocols, NULL); |
| 901 |
|
| 902 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 903 |
|
| 904 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) != WEB_SOCKET_STATE_CONNECTING); |
| 905 |
g_assert_cmpstr (web_socket_connection_get_protocol (test->client), ==, "aaa"); |
| 906 |
g_assert_cmpstr (web_socket_connection_get_protocol (test->server), ==, "aaa"); |
| 907 |
|
| 908 |
g_clear_error (&error); |
| 909 |
} |
| 910 |
|
| 911 |
static void |
| 912 |
test_close_clean_client (Test *test, |
| 913 |
gconstpointer data) |
| 914 |
{ |
| 915 |
gboolean close_event_client = FALSE; |
| 916 |
gboolean close_event_server = FALSE; |
| 917 |
|
| 918 |
g_signal_connect (test->client, "close", G_CALLBACK (on_close_set_flag), &close_event_client); |
| 919 |
g_signal_connect (test->server, "close", G_CALLBACK (on_close_set_flag), &close_event_server); |
| 920 |
|
| 921 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_OPEN); |
| 922 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_OPEN); |
| 923 |
|
| 924 |
web_socket_connection_close (test->client, WEB_SOCKET_CLOSE_GOING_AWAY, "give me a reason"); |
| 925 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->client), ==, WEB_SOCKET_STATE_CLOSING); |
| 926 |
|
| 927 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_CLOSED); |
| 928 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_CLOSED); |
| 929 |
|
| 930 |
g_assert (close_event_client); |
| 931 |
g_assert (close_event_server); |
| 932 |
|
| 933 |
g_assert_cmpint (web_socket_connection_get_close_code (test->client), ==, WEB_SOCKET_CLOSE_GOING_AWAY); |
| 934 |
g_assert_cmpint (web_socket_connection_get_close_code (test->server), ==, WEB_SOCKET_CLOSE_GOING_AWAY); |
| 935 |
g_assert_cmpstr (web_socket_connection_get_close_data (test->server), ==, "give me a reason"); |
| 936 |
} |
| 937 |
|
| 938 |
static void |
| 939 |
test_close_clean_server (Test *test, |
| 940 |
gconstpointer data) |
| 941 |
{ |
| 942 |
gboolean close_event_client = FALSE; |
| 943 |
gboolean close_event_server = FALSE; |
| 944 |
|
| 945 |
g_signal_connect (test->client, "close", G_CALLBACK (on_close_set_flag), &close_event_client); |
| 946 |
g_signal_connect (test->server, "close", G_CALLBACK (on_close_set_flag), &close_event_server); |
| 947 |
|
| 948 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_OPEN); |
| 949 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_OPEN); |
| 950 |
|
| 951 |
web_socket_connection_close (test->server, WEB_SOCKET_CLOSE_GOING_AWAY, "another reason"); |
| 952 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->server), ==, WEB_SOCKET_STATE_CLOSING); |
| 953 |
|
| 954 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_CLOSED); |
| 955 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_CLOSED); |
| 956 |
|
| 957 |
g_assert (close_event_client); |
| 958 |
g_assert (close_event_server); |
| 959 |
|
| 960 |
g_assert_cmpint (web_socket_connection_get_close_code (test->server), ==, WEB_SOCKET_CLOSE_GOING_AWAY); |
| 961 |
g_assert_cmpint (web_socket_connection_get_close_code (test->client), ==, WEB_SOCKET_CLOSE_GOING_AWAY); |
| 962 |
g_assert_cmpstr (web_socket_connection_get_close_data (test->client), ==, "another reason"); |
| 963 |
} |
| 964 |
|
| 965 |
static void |
| 966 |
test_close_immediately (void) |
| 967 |
{ |
| 968 |
WebSocketConnection *client; |
| 969 |
gboolean close_event = FALSE; |
| 970 |
|
| 971 |
client = web_socket_client_new ("ws://localhost/unix", NULL, NULL); |
| 972 |
g_signal_connect (client, "close", G_CALLBACK (on_close_set_flag), &close_event); |
| 973 |
g_assert_cmpint (web_socket_connection_get_ready_state (client), ==, WEB_SOCKET_STATE_CONNECTING); |
| 974 |
|
| 975 |
web_socket_connection_close (client, 0, NULL); |
| 976 |
g_assert_cmpint (web_socket_connection_get_ready_state (client), ==, WEB_SOCKET_STATE_CLOSED); |
| 977 |
g_assert (close_event == TRUE); |
| 978 |
|
| 979 |
g_object_unref (client); |
| 980 |
} |
| 981 |
|
| 982 |
static gboolean |
| 983 |
on_idle_real_close (gpointer data) |
| 984 |
{ |
| 985 |
WebSocketConnection *ws = data; |
| 986 |
web_socket_connection_close (ws, 0, NULL); |
| 987 |
return FALSE; |
| 988 |
} |
| 989 |
|
| 990 |
static gboolean |
| 991 |
on_closing_send_message (WebSocketConnection *ws, |
| 992 |
gpointer data) |
| 993 |
{ |
| 994 |
GBytes *message = data; |
| 995 |
web_socket_connection_send (ws, WEB_SOCKET_DATA_TEXT, NULL, message); |
| 996 |
g_signal_handlers_disconnect_by_func (ws, on_closing_send_message, data); |
| 997 |
g_idle_add (on_idle_real_close, ws); |
| 998 |
return TRUE; |
| 999 |
} |
| 1000 |
|
| 1001 |
static void |
| 1002 |
test_message_after_closing (Test *test, |
| 1003 |
gconstpointer data) |
| 1004 |
{ |
| 1005 |
gboolean close_event_client = FALSE; |
| 1006 |
gboolean close_event_server = FALSE; |
| 1007 |
GBytes *received = NULL; |
| 1008 |
GBytes *message; |
| 1009 |
|
| 1010 |
message = g_bytes_new ("another test because", 20); |
| 1011 |
g_signal_connect (test->client, "close", G_CALLBACK (on_close_set_flag), &close_event_client); |
| 1012 |
g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received); |
| 1013 |
g_signal_connect (test->server, "close", G_CALLBACK (on_close_set_flag), &close_event_server); |
| 1014 |
g_signal_connect (test->server, "closing", G_CALLBACK (on_closing_send_message), message); |
| 1015 |
|
| 1016 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_OPEN); |
| 1017 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_OPEN); |
| 1018 |
|
| 1019 |
web_socket_connection_close (test->client, WEB_SOCKET_CLOSE_GOING_AWAY, "another reason"); |
| 1020 |
g_assert_cmpint (web_socket_connection_get_ready_state (test->client), ==, WEB_SOCKET_STATE_CLOSING); |
| 1021 |
|
| 1022 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->server) == WEB_SOCKET_STATE_CLOSED); |
| 1023 |
WAIT_UNTIL (web_socket_connection_get_ready_state (test->client) == WEB_SOCKET_STATE_CLOSED); |
| 1024 |
|
| 1025 |
g_assert (close_event_client); |
| 1026 |
g_assert (close_event_server); |
| 1027 |
|
| 1028 |
g_assert (received != NULL); |
| 1029 |
g_assert (g_bytes_equal (message, received)); |
| 1030 |
|
| 1031 |
g_bytes_unref (received); |
| 1032 |
g_bytes_unref (message); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static void |
| 1036 |
mock_perform_handshake (GIOStream *io) |
| 1037 |
{ |
| 1038 |
GHashTable *headers; |
| 1039 |
gchar buffer[1024]; |
| 1040 |
gssize count; |
| 1041 |
gssize ret; |
| 1042 |
const gchar *key; |
| 1043 |
gchar *accept; |
| 1044 |
gsize written; |
| 1045 |
|
| 1046 |
/* Assumes client codes sends headers as a single write() */ |
| 1047 |
count = g_input_stream_read (g_io_stream_get_input_stream (io), |
| 1048 |
buffer, sizeof (buffer), NULL, NULL); |
| 1049 |
g_assert (count > 0); |
| 1050 |
|
| 1051 |
/* Parse the incoming request */ |
| 1052 |
ret = web_socket_util_parse_req_line (buffer, count, NULL, NULL); |
| 1053 |
g_assert_cmpint (ret, >, 0); |
| 1054 |
ret = web_socket_util_parse_headers (buffer + ret, count - ret, &headers); |
| 1055 |
g_assert_cmpint (ret, >, 0); |
| 1056 |
|
| 1057 |
key = g_hash_table_lookup (headers, "Sec-WebSocket-Key"); |
| 1058 |
accept = _web_socket_complete_accept_key_rfc6455 (key); |
| 1059 |
|
| 1060 |
count = g_snprintf (buffer, sizeof (buffer), |
| 1061 |
"HTTP/1.1 101 Switching Protocols\r\n" |
| 1062 |
"Upgrade: websocket\r\n" |
| 1063 |
"Connection: Upgrade\r\n" |
| 1064 |
"Sec-WebSocket-Accept: %s\r\n" |
| 1065 |
"\r\n", accept); |
| 1066 |
g_free (accept); |
| 1067 |
|
| 1068 |
if (!g_output_stream_write_all (g_io_stream_get_output_stream (io), |
| 1069 |
buffer, count, &written, NULL, NULL)) |
| 1070 |
g_assert_not_reached (); |
| 1071 |
g_assert_cmpuint (count, ==, written); |
| 1072 |
|
| 1073 |
g_hash_table_unref (headers); |
| 1074 |
} |
| 1075 |
|
| 1076 |
static gpointer |
| 1077 |
handshake_then_timeout_server_thread (gpointer user_data) |
| 1078 |
{ |
| 1079 |
GIOStream *io = user_data; |
| 1080 |
mock_perform_handshake (io); |
| 1081 |
return NULL; |
| 1082 |
} |
| 1083 |
|
| 1084 |
static void |
| 1085 |
test_close_after_timeout (void) |
| 1086 |
{ |
| 1087 |
WebSocketConnection *client; |
| 1088 |
gboolean close_event = FALSE; |
| 1089 |
GIOStream *io_a; |
| 1090 |
GIOStream *io_b; |
| 1091 |
GThread *thread; |
| 1092 |
|
| 1093 |
/* Note that no server is around in this test, so no close happens */ |
| 1094 |
cockpit_socket_streampair (&io_a, &io_b); |
| 1095 |
thread = g_thread_new ("timeout-thread", handshake_then_timeout_server_thread, io_a); |
| 1096 |
|
| 1097 |
client = web_socket_client_new_for_stream ("ws://localhost/unix", NULL, NULL, io_b); |
| 1098 |
|
| 1099 |
g_signal_connect (client, "close", G_CALLBACK (on_close_set_flag), &close_event); |
| 1100 |
g_signal_connect (client, "error", G_CALLBACK (on_error_not_reached), NULL); |
| 1101 |
WAIT_UNTIL (web_socket_connection_get_ready_state (client) == WEB_SOCKET_STATE_OPEN); |
| 1102 |
|
| 1103 |
/* Now try and close things */ |
| 1104 |
web_socket_connection_close (client, 0, NULL); |
| 1105 |
g_assert_cmpint (web_socket_connection_get_ready_state (client), ==, WEB_SOCKET_STATE_CLOSING); |
| 1106 |
|
| 1107 |
#if 0 |
| 1108 |
/* g_test_expect_message is pretty much incompatible with g_debug() */ |
| 1109 |
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "server did not close io when expected"); |
| 1110 |
#endif |
| 1111 |
WAIT_UNTIL (web_socket_connection_get_ready_state (client) == WEB_SOCKET_STATE_CLOSED); |
| 1112 |
|
| 1113 |
g_assert (close_event == TRUE); |
| 1114 |
g_object_unref (client); |
| 1115 |
|
| 1116 |
/* Now actually close the server side stream */ |
| 1117 |
g_thread_join (thread); |
| 1118 |
g_object_unref (io_a); |
| 1119 |
g_object_unref (io_b); |
| 1120 |
} |
| 1121 |
|
| 1122 |
static gpointer |
| 1123 |
send_fragments_server_thread (gpointer user_data) |
| 1124 |
{ |
| 1125 |
GIOStream *io = user_data; |
| 1126 |
gsize written; |
| 1127 |
|
| 1128 |
const gchar fragments[] = "\x01\x04""one " /* !fin | opcode */ |
| 1129 |
"\x00\x04""two " /* !fin | no opcode */ |
| 1130 |
"\x80\x05""three"; /* fin | no opcode */ |
| 1131 |
|
| 1132 |
mock_perform_handshake (io); |
| 1133 |
|
| 1134 |
if (!g_output_stream_write_all (g_io_stream_get_output_stream (io), |
| 1135 |
fragments, sizeof (fragments) -1, &written, NULL, NULL)) |
| 1136 |
g_assert_not_reached (); |
| 1137 |
g_assert_cmpuint (written, ==, sizeof (fragments) - 1); |
| 1138 |
|
| 1139 |
return NULL; |
| 1140 |
} |
| 1141 |
|
| 1142 |
static void |
| 1143 |
test_receive_fragmented (void) |
| 1144 |
{ |
| 1145 |
WebSocketConnection *client; |
| 1146 |
GIOStream *io_a; |
| 1147 |
GIOStream *io_b; |
| 1148 |
GThread *thread; |
| 1149 |
GBytes *received = NULL; |
| 1150 |
GBytes *expect; |
| 1151 |
|
| 1152 |
/* Note that no server is around in this test, so no close happens */ |
| 1153 |
cockpit_socket_streampair (&io_a, &io_b); |
| 1154 |
thread = g_thread_new ("fragment-thread", send_fragments_server_thread, io_a); |
| 1155 |
|
| 1156 |
client = web_socket_client_new_for_stream ("ws://localhost/unix", NULL, NULL, io_b); |
| 1157 |
g_signal_connect (client, "error", G_CALLBACK (on_error_not_reached), NULL); |
| 1158 |
g_signal_connect (client, "message", G_CALLBACK (on_text_message), &received); |
| 1159 |
|
| 1160 |
WAIT_UNTIL (received != NULL); |
| 1161 |
expect = g_bytes_new ("one two three", 13); |
| 1162 |
g_assert (g_bytes_equal (expect, received)); |
| 1163 |
g_bytes_unref (expect); |
| 1164 |
g_bytes_unref (received); |
| 1165 |
|
| 1166 |
g_thread_join (thread); |
| 1167 |
g_object_unref (client); |
| 1168 |
g_object_unref (io_a); |
| 1169 |
g_object_unref (io_b); |
| 1170 |
} |
| 1171 |
|
| 1172 |
static gpointer |
| 1173 |
client_thread (gpointer data) |
| 1174 |
{ |
| 1175 |
GIOStream *io = data; |
| 1176 |
GMainContext *context; |
| 1177 |
WebSocketConnection *client; |
| 1178 |
|
| 1179 |
context = g_main_context_new (); |
| 1180 |
g_main_context_push_thread_default (context); |
| 1181 |
|
| 1182 |
client = web_socket_client_new_for_stream ("ws://localhost/unix", NULL, NULL, io); |
| 1183 |
g_signal_connect (client, "error", G_CALLBACK (on_error_not_reached), NULL); |
| 1184 |
|
| 1185 |
while (web_socket_connection_get_ready_state (client) != WEB_SOCKET_STATE_CLOSED) |
| 1186 |
g_main_context_iteration (context, TRUE); |
| 1187 |
|
| 1188 |
g_main_context_pop_thread_default (context); |
| 1189 |
g_main_context_unref (context); |
| 1190 |
|
| 1191 |
g_object_unref (client); |
| 1192 |
return NULL; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static void |
| 1196 |
test_handshake_with_buffer_and_headers (void) |
| 1197 |
{ |
| 1198 |
WebSocketConnection *server; |
| 1199 |
GHashTable *headers; |
| 1200 |
GByteArray *input; |
| 1201 |
gchar buffer[1024]; |
| 1202 |
GIOStream *ioc; |
| 1203 |
GIOStream *ios; |
| 1204 |
gssize count; |
| 1205 |
gssize in1, in2; |
| 1206 |
GThread *thread; |
| 1207 |
|
| 1208 |
cockpit_socket_streampair (&ioc, &ios); |
| 1209 |
|
| 1210 |
thread = g_thread_new ("client-thread", client_thread, ioc); |
| 1211 |
|
| 1212 |
count = g_input_stream_read (g_io_stream_get_input_stream (ios), buffer, |
| 1213 |
sizeof (buffer), NULL, NULL); |
| 1214 |
g_assert_cmpint (count, >, 0); |
| 1215 |
|
| 1216 |
/* Parse the incoming request */ |
| 1217 |
in1 = web_socket_util_parse_req_line (buffer, count, NULL, NULL); |
| 1218 |
g_assert_cmpint (in1, >, 0); |
| 1219 |
in2 = web_socket_util_parse_headers (buffer + in1, count - in1, &headers); |
| 1220 |
g_assert_cmpint (in2, >, 0); |
| 1221 |
|
| 1222 |
/* Make a buffer for the rest */ |
| 1223 |
input = g_byte_array_new (); |
| 1224 |
g_byte_array_append (input, (guchar *)buffer + (in1 + in2), count - (in1 + in2)); |
| 1225 |
|
| 1226 |
server = web_socket_server_new_for_stream (NULL, NULL, ios, headers, input); |
| 1227 |
g_signal_connect (server, "error", G_CALLBACK (on_error_not_reached), NULL); |
| 1228 |
|
| 1229 |
WAIT_UNTIL (web_socket_connection_get_ready_state (server) != WEB_SOCKET_STATE_CONNECTING); |
| 1230 |
g_assert_cmpint (web_socket_connection_get_ready_state (server), ==, WEB_SOCKET_STATE_OPEN); |
| 1231 |
|
| 1232 |
web_socket_connection_close (server, 0, NULL); |
| 1233 |
WAIT_UNTIL (web_socket_connection_get_ready_state (server) == WEB_SOCKET_STATE_CLOSED); |
| 1234 |
|
| 1235 |
g_byte_array_unref (input); |
| 1236 |
g_hash_table_unref (headers); |
| 1237 |
g_object_unref (server); |
| 1238 |
g_thread_join (thread); |
| 1239 |
|
| 1240 |
g_object_unref (ioc); |
| 1241 |
g_object_unref (ios); |
| 1242 |
} |
| 1243 |
|
| 1244 |
int |
| 1245 |
main (int argc, |
| 1246 |
char *argv[]) |
| 1247 |
{ |
| 1248 |
gchar *name; |
| 1249 |
gint j; |
| 1250 |
|
| 1251 |
struct { |
| 1252 |
void (* func) (Test *, gconstpointer); |
| 1253 |
const gchar *name; |
| 1254 |
} tests_with_client_server_pair[] = { |
| 1255 |
{ test_handshake, "handshake" }, |
| 1256 |
{ test_send_client_to_server, "send-client-to-server" }, |
| 1257 |
{ test_send_server_to_client, "send-server-to-client" }, |
| 1258 |
{ test_send_big_packets, "send-big-packets" }, |
| 1259 |
{ test_send_prefixed, "send-prefixed" }, |
| 1260 |
{ test_send_bad_data, "send-bad-data" }, |
| 1261 |
{ test_pressure_queue, "pressure-queue" }, |
| 1262 |
{ test_pressure_throttle, "pressure-throttle" }, |
| 1263 |
{ test_protocol_negotiate, "protocol-negotiate" }, |
| 1264 |
{ test_protocol_mismatch, "protocol-mismatch" }, |
| 1265 |
{ test_protocol_server_any, "protocol-server-any" }, |
| 1266 |
{ test_protocol_client_any, "protocol-client-any" }, |
| 1267 |
{ test_close_clean_client, "close-clean-client" }, |
| 1268 |
{ test_close_clean_server, "close-clean-server" }, |
| 1269 |
}; |
| 1270 |
|
| 1271 |
signal (SIGPIPE, SIG_IGN); |
| 1272 |
g_assert (g_setenv ("GSETTINGS_BACKEND", "memory", TRUE)); |
| 1273 |
g_assert (g_setenv ("GIO_USE_PROXY_RESOLVER", "dummy", TRUE)); |
| 1274 |
g_assert (g_setenv ("GIO_USE_VFS", "local", TRUE)); |
| 1275 |
|
| 1276 |
g_set_prgname ("test-websocket"); |
| 1277 |
g_test_init (&argc, &argv, NULL); |
| 1278 |
|
| 1279 |
g_test_add_func ("/web-socket/parse-url", test_parse_url); |
| 1280 |
g_test_add_func ("/web-socket/parse-url-no-out", test_parse_url_no_out); |
| 1281 |
g_test_add_func ("/web-socket/parse-url-bad", test_parse_url_bad); |
| 1282 |
g_test_add_func ("/web-socket/parse-url-no-path", test_parse_url_no_path); |
| 1283 |
g_test_add_func ("/web-socket/parse-url-with-user", test_parse_url_with_user); |
| 1284 |
g_test_add_func ("/web-socket/parse-req", test_parse_req); |
| 1285 |
g_test_add_func ("/web-socket/parse-req-no-out", test_parse_req_no_out); |
| 1286 |
g_test_add_func ("/web-socket/parse-req-not-enough", test_parse_req_not_enough); |
| 1287 |
g_test_add_func ("/web-socket/parse-req-bad", test_parse_req_bad); |
| 1288 |
g_test_add_func ("/web-socket/parse-status", test_parse_status); |
| 1289 |
g_test_add_func ("/web-socket/parse-status-no-out", test_parse_status_no_out); |
| 1290 |
g_test_add_func ("/web-socket/parse-status-not-enough", test_parse_status_not_enough); |
| 1291 |
g_test_add_func ("/web-socket/parse-status-bad", test_parse_status_bad); |
| 1292 |
g_test_add_func ("/web-socket/parse-version-1-0", test_parse_version_1_0); |
| 1293 |
g_test_add_func ("/web-socket/parse-version-1-1", test_parse_version_1_1); |
| 1294 |
g_test_add_func ("/web-socket/parse-headers", test_parse_headers); |
| 1295 |
g_test_add_func ("/web-socket/parse-duplicate-headers", test_parse_duplicate_headers); |
| 1296 |
g_test_add_func ("/web-socket/parse-headers-no-out", test_parse_headers_no_out); |
| 1297 |
g_test_add_func ("/web-socket/parse-headers-bad", test_parse_headers_bad); |
| 1298 |
g_test_add_func ("/web-socket/parse-headers-not-enough", test_parse_headers_not_enough); |
| 1299 |
g_test_add_func ("/web-socket/header-equals", test_header_equals); |
| 1300 |
g_test_add_func ("/web-socket/header-contains", test_header_contains); |
| 1301 |
g_test_add_func ("/web-socket/header-empty", test_header_empty); |
| 1302 |
|
| 1303 |
for (j = 0; j < G_N_ELEMENTS (tests_with_client_server_pair); j++) |
| 1304 |
{ |
| 1305 |
name = g_strdup_printf ("/web-socket/%s", tests_with_client_server_pair[j].name); |
| 1306 |
g_test_add (name, Test, NULL, setup_pair, tests_with_client_server_pair[j].func, teardown); |
| 1307 |
g_free (name); |
| 1308 |
} |
| 1309 |
|
| 1310 |
g_test_add_func ("/web-socket/close-immediately", test_close_immediately); |
| 1311 |
if (g_test_slow ()) |
| 1312 |
g_test_add_func ("/web-socket/close-after-timeout", test_close_after_timeout); |
| 1313 |
g_test_add_func ("/web-socket/receive-fragmented", test_receive_fragmented); |
| 1314 |
g_test_add_func ("/web-socket/handshake-with-buffer-headers", test_handshake_with_buffer_and_headers); |
| 1315 |
|
| 1316 |
g_test_add ("/web-socket/message-after-closing", Test, NULL, setup_pair, test_message_after_closing, teardown); |
| 1317 |
|
| 1318 |
return g_test_run (); |
| 1319 |
} |