| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/private/websocket_impl.h> |
| 7 |
|
| 8 |
#include <aws/common/atomics.h> |
| 9 |
#include <aws/http/connection.h> |
| 10 |
#include <aws/http/request_response.h> |
| 11 |
#include <aws/io/uri.h> |
| 12 |
#include <aws/testing/aws_test_harness.h> |
| 13 |
|
| 14 |
#ifdef _MSC_VER |
| 15 |
# pragma warning(disable : 4204) |
| 16 |
#endif |
| 17 |
|
| 18 |
#define TEST_CASE(NAME) \ |
| 19 |
AWS_TEST_CASE(NAME, s_test_##NAME); \ |
| 20 |
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) |
| 21 |
|
| 22 |
static int s_mock_http_client_connect(const struct aws_http_client_connection_options *options); |
| 23 |
static void s_mock_http_connection_release(struct aws_http_connection *connection); |
| 24 |
static void s_mock_http_connection_close(struct aws_http_connection *connection); |
| 25 |
static struct aws_channel *s_mock_http_connection_get_channel(struct aws_http_connection *connection); |
| 26 |
static struct aws_http_stream *s_mock_http_connection_make_request( |
| 27 |
struct aws_http_connection *client_connection, |
| 28 |
const struct aws_http_make_request_options *options); |
| 29 |
static int s_mock_http_stream_activate(struct aws_http_stream *stream); |
| 30 |
static void s_mock_http_stream_release(struct aws_http_stream *stream); |
| 31 |
static struct aws_http_connection *s_mock_http_stream_get_connection(const struct aws_http_stream *stream); |
| 32 |
static void s_mock_http_stream_update_window(struct aws_http_stream *stream, size_t increment_size); |
| 33 |
static int s_mock_http_stream_get_incoming_response_status(const struct aws_http_stream *stream, int *out_status); |
| 34 |
static struct aws_websocket *s_mock_websocket_handler_new(const struct aws_websocket_handler_options *options); |
| 35 |
|
| 36 |
static const struct aws_websocket_client_bootstrap_system_vtable s_mock_system_vtable = { |
| 37 |
.aws_http_client_connect = s_mock_http_client_connect, |
| 38 |
.aws_http_connection_release = s_mock_http_connection_release, |
| 39 |
.aws_http_connection_close = s_mock_http_connection_close, |
| 40 |
.aws_http_connection_get_channel = s_mock_http_connection_get_channel, |
| 41 |
.aws_http_connection_make_request = s_mock_http_connection_make_request, |
| 42 |
.aws_http_stream_activate = s_mock_http_stream_activate, |
| 43 |
.aws_http_stream_release = s_mock_http_stream_release, |
| 44 |
.aws_http_stream_get_connection = s_mock_http_stream_get_connection, |
| 45 |
.aws_http_stream_update_window = s_mock_http_stream_update_window, |
| 46 |
.aws_http_stream_get_incoming_response_status = s_mock_http_stream_get_incoming_response_status, |
| 47 |
.aws_websocket_handler_new = s_mock_websocket_handler_new, |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
static const char *s_sec_websocket_key_value = "dGhlIHNhbXBsZSBub25jZQ=="; |
| 52 |
|
| 53 |
struct test_response { |
| 54 |
int status_code; |
| 55 |
struct aws_http_header headers[10]; |
| 56 |
const char *body; |
| 57 |
}; |
| 58 |
|
| 59 |
static const struct test_response s_accepted_response = { |
| 60 |
.status_code = 101, |
| 61 |
.headers = |
| 62 |
{ |
| 63 |
{ |
| 64 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 65 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 66 |
}, |
| 67 |
{ |
| 68 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 69 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 70 |
}, |
| 71 |
{ |
| 72 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 73 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 74 |
}, |
| 75 |
}, |
| 76 |
}; |
| 77 |
|
| 78 |
static const struct test_response s_rejected_response = { |
| 79 |
.status_code = 403, |
| 80 |
.headers = |
| 81 |
{ |
| 82 |
{ |
| 83 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"), |
| 84 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("43"), |
| 85 |
}, |
| 86 |
}, |
| 87 |
.body = "your request is bad and you should feel bad", |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
enum boot_step { |
| 93 |
BOOT_STEP_HTTP_CONNECT = 0x4000000, |
| 94 |
BOOT_STEP_HTTP_CONNECT_COMPLETE, |
| 95 |
BOOT_STEP_REQUEST_NEW, |
| 96 |
BOOT_STEP_REQUEST_ACTIVATE, |
| 97 |
BOOT_STEP_BEFORE_HEADERS, |
| 98 |
BOOT_STEP_BEFORE_HEADERS_DONE, |
| 99 |
BOOT_STEP_BEFORE_REJECTION_BODY, |
| 100 |
BOOT_STEP_BEFORE_REJECTION_STREAM_COMPLETE, |
| 101 |
BOOT_STEP_WEBSOCKET_NEW, |
| 102 |
BOOT_STEP_HTTP_SHUTDOWN, |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
static struct tester { |
| 107 |
|
| 108 |
struct aws_allocator *alloc; |
| 109 |
enum boot_step fail_at_step; |
| 110 |
|
| 111 |
struct aws_http_header *extra_handshake_request_header_array; |
| 112 |
size_t num_extra_handshake_request_headers; |
| 113 |
struct aws_http_message *handshake_request; |
| 114 |
|
| 115 |
const struct test_response *handshake_response; |
| 116 |
size_t num_handshake_response_headers; |
| 117 |
|
| 118 |
|
| 119 |
bool http_connect_called_successfully; |
| 120 |
|
| 121 |
aws_http_on_client_connection_setup_fn *http_connect_setup_callback; |
| 122 |
aws_http_on_client_connection_shutdown_fn *http_connect_shutdown_callback; |
| 123 |
void *http_connect_user_data; |
| 124 |
|
| 125 |
bool http_connection_release_called; |
| 126 |
bool http_connection_close_called; |
| 127 |
|
| 128 |
bool http_stream_new_called_successfully; |
| 129 |
aws_http_on_incoming_headers_fn *http_stream_on_response_headers; |
| 130 |
aws_http_on_incoming_header_block_done_fn *http_stream_on_response_header_block_done; |
| 131 |
aws_http_on_incoming_body_fn *http_stream_on_response_body; |
| 132 |
aws_http_on_stream_complete_fn *http_stream_on_complete; |
| 133 |
void *http_stream_user_data; |
| 134 |
|
| 135 |
bool http_stream_on_complete_invoked; |
| 136 |
|
| 137 |
bool websocket_new_called_successfully; |
| 138 |
|
| 139 |
bool http_stream_release_called; |
| 140 |
bool http_stream_activate_called_successfully; |
| 141 |
|
| 142 |
bool websocket_setup_invoked; |
| 143 |
int websocket_setup_error_code; |
| 144 |
bool websocket_setup_had_response_status; |
| 145 |
bool websocket_setup_had_response_headers; |
| 146 |
bool websocket_setup_had_response_body; |
| 147 |
|
| 148 |
bool websocket_shutdown_invoked; |
| 149 |
int websocket_shutdown_error_code; |
| 150 |
|
| 151 |
|
| 152 |
size_t window_increment_total; |
| 153 |
} s_tester; |
| 154 |
|
| 155 |
static int s_tester_init(struct aws_allocator *alloc) { |
| 156 |
aws_http_library_init(alloc); |
| 157 |
|
| 158 |
aws_websocket_client_bootstrap_set_system_vtable(&s_mock_system_vtable); |
| 159 |
|
| 160 |
|
| 161 |
s_tester.alloc = alloc; |
| 162 |
|
| 163 |
if (!s_tester.handshake_response) { |
| 164 |
s_tester.handshake_response = &s_accepted_response; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_tester.handshake_response->headers); ++i) { |
| 169 |
if (s_tester.handshake_response->headers[i].name.len == 0) { |
| 170 |
break; |
| 171 |
} |
| 172 |
s_tester.num_handshake_response_headers = i + 1; |
| 173 |
} |
| 174 |
|
| 175 |
return AWS_OP_SUCCESS; |
| 176 |
} |
| 177 |
|
| 178 |
static int s_tester_clean_up(void) { |
| 179 |
aws_http_library_clean_up(); |
| 180 |
return AWS_OP_SUCCESS; |
| 181 |
} |
| 182 |
|
| 183 |
static bool s_headers_eq( |
| 184 |
const struct aws_http_header *headers_a, |
| 185 |
size_t num_headers_a, |
| 186 |
const struct aws_http_header *headers_b, |
| 187 |
size_t num_headers_b) { |
| 188 |
|
| 189 |
if (num_headers_a != num_headers_b) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
for (size_t i = 0; i < num_headers_a; ++i) { |
| 194 |
struct aws_http_header a = headers_a[i]; |
| 195 |
struct aws_http_header b = headers_b[i]; |
| 196 |
|
| 197 |
if (!aws_byte_cursor_eq_ignore_case(&a.name, &b.name) || !aws_byte_cursor_eq(&a.value, &b.value)) { |
| 198 |
printf( |
| 199 |
"Header did not match '" PRInSTR ": " PRInSTR "'\n", |
| 200 |
AWS_BYTE_CURSOR_PRI(a.name), |
| 201 |
AWS_BYTE_CURSOR_PRI(a.value)); |
| 202 |
return false; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
static bool s_request_eq(const struct aws_http_message *request_a, const struct aws_http_message *request_b) { |
| 210 |
|
| 211 |
const size_t num_headers_a = aws_http_message_get_header_count(request_a); |
| 212 |
const size_t num_headers_b = aws_http_message_get_header_count(request_b); |
| 213 |
|
| 214 |
if (num_headers_a != num_headers_b) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
for (size_t a_i = 0; a_i < num_headers_a; ++a_i) { |
| 219 |
struct aws_http_header a; |
| 220 |
aws_http_message_get_header(request_a, &a, a_i); |
| 221 |
|
| 222 |
bool found_match = false; |
| 223 |
|
| 224 |
for (size_t b_i = 0; b_i < num_headers_b; ++b_i) { |
| 225 |
struct aws_http_header b; |
| 226 |
aws_http_message_get_header(request_b, &b, b_i); |
| 227 |
|
| 228 |
if (aws_byte_cursor_eq_ignore_case(&a.name, &b.name) && |
| 229 |
aws_byte_cursor_eq_ignore_case(&a.value, &b.value)) { |
| 230 |
|
| 231 |
found_match = true; |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (!found_match) { |
| 237 |
printf( |
| 238 |
"Failed to find header '" PRInSTR ": " PRInSTR "'\n", |
| 239 |
AWS_BYTE_CURSOR_PRI(a.name), |
| 240 |
AWS_BYTE_CURSOR_PRI(a.value)); |
| 241 |
return false; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
static struct aws_http_connection *s_mock_http_connection = (void *)"http connection"; |
| 250 |
static struct aws_http_stream *s_mock_stream = (void *)"stream"; |
| 251 |
static struct aws_channel *s_mock_channel = (void *)"channel"; |
| 252 |
static struct aws_websocket *s_mock_websocket = (void *)"websocket"; |
| 253 |
|
| 254 |
static int s_mock_http_client_connect(const struct aws_http_client_connection_options *options) { |
| 255 |
AWS_FATAL_ASSERT(options); |
| 256 |
AWS_FATAL_ASSERT(!s_tester.http_connect_called_successfully); |
| 257 |
|
| 258 |
if (s_tester.fail_at_step == BOOT_STEP_HTTP_CONNECT) { |
| 259 |
return aws_raise_error(BOOT_STEP_HTTP_CONNECT); |
| 260 |
} |
| 261 |
|
| 262 |
s_tester.http_connect_called_successfully = true; |
| 263 |
s_tester.http_connect_setup_callback = options->on_setup; |
| 264 |
s_tester.http_connect_shutdown_callback = options->on_shutdown; |
| 265 |
s_tester.http_connect_user_data = options->user_data; |
| 266 |
return AWS_OP_SUCCESS; |
| 267 |
} |
| 268 |
|
| 269 |
static void s_mock_http_connection_release(struct aws_http_connection *connection) { |
| 270 |
if (connection == NULL) { |
| 271 |
return; |
| 272 |
} |
| 273 |
AWS_FATAL_ASSERT(connection == s_mock_http_connection); |
| 274 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 275 |
s_tester.http_connection_release_called = true; |
| 276 |
} |
| 277 |
|
| 278 |
static void s_mock_http_connection_close(struct aws_http_connection *connection) { |
| 279 |
AWS_FATAL_ASSERT(connection == s_mock_http_connection); |
| 280 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 281 |
s_tester.http_connection_close_called = true; |
| 282 |
} |
| 283 |
|
| 284 |
static struct aws_channel *s_mock_http_connection_get_channel(struct aws_http_connection *connection) { |
| 285 |
AWS_FATAL_ASSERT(connection == s_mock_http_connection); |
| 286 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 287 |
return s_mock_channel; |
| 288 |
} |
| 289 |
|
| 290 |
static struct aws_http_stream *s_mock_http_connection_make_request( |
| 291 |
struct aws_http_connection *client_connection, |
| 292 |
const struct aws_http_make_request_options *options) { |
| 293 |
|
| 294 |
AWS_FATAL_ASSERT(client_connection); |
| 295 |
AWS_FATAL_ASSERT(options); |
| 296 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 297 |
AWS_FATAL_ASSERT(!s_tester.http_stream_new_called_successfully); |
| 298 |
|
| 299 |
if (s_tester.fail_at_step == BOOT_STEP_REQUEST_NEW) { |
| 300 |
aws_raise_error(BOOT_STEP_REQUEST_NEW); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
AWS_FATAL_ASSERT(s_request_eq(s_tester.handshake_request, options->request)); |
| 306 |
|
| 307 |
s_tester.http_stream_new_called_successfully = true; |
| 308 |
s_tester.http_stream_on_response_headers = options->on_response_headers; |
| 309 |
s_tester.http_stream_on_response_header_block_done = options->on_response_header_block_done; |
| 310 |
s_tester.http_stream_on_response_body = options->on_response_body; |
| 311 |
s_tester.http_stream_on_complete = options->on_complete; |
| 312 |
s_tester.http_stream_user_data = options->user_data; |
| 313 |
return s_mock_stream; |
| 314 |
} |
| 315 |
|
| 316 |
static int s_mock_http_stream_activate(struct aws_http_stream *stream) { |
| 317 |
AWS_FATAL_ASSERT(stream == s_mock_stream); |
| 318 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 319 |
AWS_FATAL_ASSERT(!s_tester.http_stream_release_called); |
| 320 |
|
| 321 |
if (s_tester.fail_at_step == BOOT_STEP_REQUEST_ACTIVATE) { |
| 322 |
return aws_raise_error(BOOT_STEP_REQUEST_ACTIVATE); |
| 323 |
} |
| 324 |
|
| 325 |
s_tester.http_stream_activate_called_successfully = true; |
| 326 |
return AWS_OP_SUCCESS; |
| 327 |
} |
| 328 |
|
| 329 |
static void s_mock_http_stream_release(struct aws_http_stream *stream) { |
| 330 |
if (stream == NULL) { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
AWS_FATAL_ASSERT(stream == s_mock_stream); |
| 335 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 336 |
AWS_FATAL_ASSERT(!s_tester.http_stream_release_called); |
| 337 |
s_tester.http_stream_release_called = true; |
| 338 |
} |
| 339 |
|
| 340 |
static struct aws_http_connection *s_mock_http_stream_get_connection(const struct aws_http_stream *stream) { |
| 341 |
AWS_FATAL_ASSERT(stream == s_mock_stream); |
| 342 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 343 |
AWS_FATAL_ASSERT(!s_tester.http_stream_release_called); |
| 344 |
return s_mock_http_connection; |
| 345 |
} |
| 346 |
|
| 347 |
static void s_mock_http_stream_update_window(struct aws_http_stream *stream, size_t increment_size) { |
| 348 |
AWS_FATAL_ASSERT(stream == s_mock_stream); |
| 349 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 350 |
AWS_FATAL_ASSERT(!s_tester.http_stream_release_called); |
| 351 |
s_tester.window_increment_total += increment_size; |
| 352 |
} |
| 353 |
|
| 354 |
static int s_mock_http_stream_get_incoming_response_status(const struct aws_http_stream *stream, int *out_status) { |
| 355 |
AWS_FATAL_ASSERT(stream == s_mock_stream); |
| 356 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 357 |
AWS_FATAL_ASSERT(!s_tester.http_stream_release_called); |
| 358 |
AWS_FATAL_ASSERT(out_status); |
| 359 |
|
| 360 |
*out_status = s_tester.handshake_response->status_code; |
| 361 |
return AWS_OP_SUCCESS; |
| 362 |
} |
| 363 |
|
| 364 |
static struct aws_websocket *s_mock_websocket_handler_new(const struct aws_websocket_handler_options *options) { |
| 365 |
AWS_FATAL_ASSERT(options); |
| 366 |
AWS_FATAL_ASSERT(!s_tester.http_connection_release_called); |
| 367 |
AWS_FATAL_ASSERT(!s_tester.websocket_new_called_successfully); |
| 368 |
|
| 369 |
if (s_tester.fail_at_step == BOOT_STEP_WEBSOCKET_NEW) { |
| 370 |
aws_raise_error(BOOT_STEP_WEBSOCKET_NEW); |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
|
| 374 |
s_tester.websocket_new_called_successfully = true; |
| 375 |
return s_mock_websocket; |
| 376 |
} |
| 377 |
|
| 378 |
static void s_on_websocket_setup(const struct aws_websocket_on_connection_setup_data *setup, void *user_data) { |
| 379 |
|
| 380 |
|
| 381 |
AWS_FATAL_ASSERT((setup->error_code != 0) ^ (setup->websocket != NULL)); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
if (setup->handshake_response_status) { |
| 387 |
s_tester.websocket_setup_had_response_status = true; |
| 388 |
AWS_FATAL_ASSERT(*setup->handshake_response_status == s_tester.handshake_response->status_code); |
| 389 |
|
| 390 |
|
| 391 |
AWS_FATAL_ASSERT(setup->handshake_response_header_array != NULL); |
| 392 |
} |
| 393 |
|
| 394 |
if (setup->handshake_response_header_array) { |
| 395 |
s_tester.websocket_setup_had_response_headers = true; |
| 396 |
AWS_FATAL_ASSERT(s_headers_eq( |
| 397 |
s_tester.handshake_response->headers, |
| 398 |
s_tester.num_handshake_response_headers, |
| 399 |
setup->handshake_response_header_array, |
| 400 |
setup->num_handshake_response_headers)); |
| 401 |
|
| 402 |
|
| 403 |
AWS_FATAL_ASSERT(setup->handshake_response_status != NULL); |
| 404 |
} |
| 405 |
|
| 406 |
if (setup->handshake_response_body) { |
| 407 |
s_tester.websocket_setup_had_response_body = true; |
| 408 |
AWS_FATAL_ASSERT(aws_byte_cursor_eq_c_str(setup->handshake_response_body, s_tester.handshake_response->body)); |
| 409 |
|
| 410 |
|
| 411 |
AWS_FATAL_ASSERT(setup->handshake_response_status != NULL); |
| 412 |
AWS_FATAL_ASSERT(setup->handshake_response_header_array != NULL); |
| 413 |
} |
| 414 |
|
| 415 |
AWS_FATAL_ASSERT(user_data == &s_tester); |
| 416 |
|
| 417 |
s_tester.websocket_setup_invoked = true; |
| 418 |
s_tester.websocket_setup_error_code = setup->error_code; |
| 419 |
|
| 420 |
|
| 421 |
aws_http_message_destroy(s_tester.handshake_request); |
| 422 |
s_tester.handshake_request = NULL; |
| 423 |
} |
| 424 |
|
| 425 |
static void s_on_websocket_shutdown(struct aws_websocket *websocket, int error_code, void *user_data) { |
| 426 |
AWS_FATAL_ASSERT(websocket == s_mock_websocket); |
| 427 |
AWS_FATAL_ASSERT(user_data == &s_tester); |
| 428 |
|
| 429 |
s_tester.websocket_shutdown_invoked = true; |
| 430 |
s_tester.websocket_shutdown_error_code = error_code; |
| 431 |
} |
| 432 |
|
| 433 |
static void s_complete_http_stream_and_connection(int error_code) { |
| 434 |
if (s_tester.http_stream_activate_called_successfully && !s_tester.http_stream_on_complete_invoked) { |
| 435 |
s_tester.http_stream_on_complete(s_mock_stream, error_code, s_tester.http_stream_user_data); |
| 436 |
} |
| 437 |
s_tester.http_connect_shutdown_callback(s_mock_http_connection, error_code, s_tester.http_stream_user_data); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
static int s_drive_websocket_connect(int *out_error_code) { |
| 443 |
ASSERT_NOT_NULL(out_error_code); |
| 444 |
|
| 445 |
bool websocket_connect_called_successfully = false; |
| 446 |
bool http_connect_setup_reported_success = false; |
| 447 |
|
| 448 |
|
| 449 |
static struct aws_byte_cursor path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/"); |
| 450 |
static const struct aws_byte_cursor host = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("server.example.com"); |
| 451 |
|
| 452 |
s_tester.handshake_request = aws_http_message_new_websocket_handshake_request(s_tester.alloc, path, host); |
| 453 |
if (!s_tester.handshake_request) { |
| 454 |
goto finishing_checks; |
| 455 |
} |
| 456 |
|
| 457 |
struct aws_http_headers *request_headers = aws_http_message_get_headers(s_tester.handshake_request); |
| 458 |
ASSERT_SUCCESS(aws_http_headers_set( |
| 459 |
request_headers, |
| 460 |
aws_byte_cursor_from_c_str("Sec-WebSocket-Key"), |
| 461 |
aws_byte_cursor_from_c_str(s_sec_websocket_key_value))); |
| 462 |
|
| 463 |
for (size_t i = 0; i < s_tester.num_extra_handshake_request_headers; ++i) { |
| 464 |
ASSERT_SUCCESS(aws_http_headers_add_header(request_headers, &s_tester.extra_handshake_request_header_array[i])); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
struct aws_websocket_client_connection_options ws_options = { |
| 469 |
.allocator = s_tester.alloc, |
| 470 |
.bootstrap = (void *)"client channel bootstrap", |
| 471 |
.socket_options = (void *)"socket options", |
| 472 |
.host = host, |
| 473 |
.handshake_request = s_tester.handshake_request, |
| 474 |
.user_data = &s_tester, |
| 475 |
.on_connection_setup = s_on_websocket_setup, |
| 476 |
.on_connection_shutdown = s_on_websocket_shutdown, |
| 477 |
}; |
| 478 |
|
| 479 |
int err = aws_websocket_client_connect(&ws_options); |
| 480 |
|
| 481 |
if (err) { |
| 482 |
goto finishing_checks; |
| 483 |
} |
| 484 |
websocket_connect_called_successfully = true; |
| 485 |
|
| 486 |
|
| 487 |
ASSERT_TRUE(s_tester.http_connect_called_successfully); |
| 488 |
|
| 489 |
|
| 490 |
if (s_tester.fail_at_step == BOOT_STEP_HTTP_CONNECT_COMPLETE) { |
| 491 |
s_tester.http_connect_setup_callback(NULL, BOOT_STEP_HTTP_CONNECT_COMPLETE, s_tester.http_connect_user_data); |
| 492 |
goto finishing_checks; |
| 493 |
} |
| 494 |
|
| 495 |
http_connect_setup_reported_success = true; |
| 496 |
s_tester.http_connect_setup_callback(s_mock_http_connection, AWS_ERROR_SUCCESS, s_tester.http_connect_user_data); |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
if (s_tester.http_connection_close_called) { |
| 502 |
s_tester.http_connect_shutdown_callback( |
| 503 |
s_mock_http_connection, AWS_ERROR_SUCCESS, s_tester.http_connect_user_data); |
| 504 |
goto finishing_checks; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
ASSERT_TRUE(s_tester.http_stream_new_called_successfully); |
| 509 |
ASSERT_TRUE(s_tester.http_stream_activate_called_successfully); |
| 510 |
|
| 511 |
|
| 512 |
if (s_tester.fail_at_step == BOOT_STEP_BEFORE_HEADERS) { |
| 513 |
s_complete_http_stream_and_connection(BOOT_STEP_BEFORE_HEADERS); |
| 514 |
goto finishing_checks; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
enum aws_http_header_block header_block = s_tester.handshake_response->status_code / 100 == 1 |
| 519 |
? AWS_HTTP_HEADER_BLOCK_INFORMATIONAL |
| 520 |
: AWS_HTTP_HEADER_BLOCK_MAIN; |
| 521 |
if (s_tester.http_stream_on_response_headers( |
| 522 |
s_mock_stream, |
| 523 |
header_block, |
| 524 |
s_tester.handshake_response->headers, |
| 525 |
s_tester.num_handshake_response_headers, |
| 526 |
s_tester.http_stream_user_data)) { |
| 527 |
|
| 528 |
s_complete_http_stream_and_connection(aws_last_error()); |
| 529 |
goto finishing_checks; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
if (s_tester.fail_at_step == BOOT_STEP_BEFORE_HEADERS_DONE) { |
| 534 |
s_complete_http_stream_and_connection(BOOT_STEP_BEFORE_HEADERS_DONE); |
| 535 |
goto finishing_checks; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
if (s_tester.http_stream_on_response_header_block_done( |
| 540 |
s_mock_stream, header_block, s_tester.http_stream_user_data)) { |
| 541 |
s_complete_http_stream_and_connection(aws_last_error()); |
| 542 |
goto finishing_checks; |
| 543 |
} |
| 544 |
|
| 545 |
if (s_tester.http_connection_close_called) { |
| 546 |
s_complete_http_stream_and_connection(AWS_ERROR_SUCCESS); |
| 547 |
goto finishing_checks; |
| 548 |
} |
| 549 |
|
| 550 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 551 |
|
| 552 |
struct aws_byte_cursor body = aws_byte_cursor_from_c_str(s_tester.handshake_response->body); |
| 553 |
|
| 554 |
|
| 555 |
if (s_tester.fail_at_step == BOOT_STEP_BEFORE_REJECTION_BODY) { |
| 556 |
s_complete_http_stream_and_connection(BOOT_STEP_BEFORE_REJECTION_BODY); |
| 557 |
goto finishing_checks; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
if (body.len > 0) { |
| 562 |
|
| 563 |
|
| 564 |
if (s_tester.fail_at_step == BOOT_STEP_BEFORE_REJECTION_STREAM_COMPLETE) { |
| 565 |
body.len = 1; |
| 566 |
} |
| 567 |
|
| 568 |
if (s_tester.http_stream_on_response_body(s_mock_stream, &body, s_tester.http_stream_user_data)) { |
| 569 |
s_complete_http_stream_and_connection(aws_last_error()); |
| 570 |
goto finishing_checks; |
| 571 |
} |
| 572 |
|
| 573 |
if (s_tester.http_connection_close_called) { |
| 574 |
s_complete_http_stream_and_connection(AWS_ERROR_SUCCESS); |
| 575 |
goto finishing_checks; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
if (s_tester.fail_at_step == BOOT_STEP_BEFORE_REJECTION_STREAM_COMPLETE) { |
| 581 |
s_complete_http_stream_and_connection(BOOT_STEP_BEFORE_REJECTION_STREAM_COMPLETE); |
| 582 |
goto finishing_checks; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
s_tester.http_stream_on_complete(s_mock_stream, AWS_ERROR_SUCCESS, s_tester.http_stream_user_data); |
| 587 |
s_tester.http_stream_on_complete_invoked = true; |
| 588 |
|
| 589 |
|
| 590 |
ASSERT_TRUE(s_tester.http_connection_close_called); |
| 591 |
s_tester.http_connect_shutdown_callback( |
| 592 |
s_mock_http_connection, AWS_ERROR_SUCCESS, s_tester.http_stream_user_data); |
| 593 |
goto finishing_checks; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
ASSERT_TRUE(s_tester.websocket_new_called_successfully); |
| 598 |
|
| 599 |
|
| 600 |
ASSERT_TRUE(s_tester.websocket_setup_invoked); |
| 601 |
if (s_tester.websocket_setup_error_code) { |
| 602 |
goto finishing_checks; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
if (s_tester.fail_at_step == BOOT_STEP_HTTP_SHUTDOWN) { |
| 607 |
s_complete_http_stream_and_connection(BOOT_STEP_HTTP_SHUTDOWN); |
| 608 |
goto finishing_checks; |
| 609 |
} |
| 610 |
|
| 611 |
s_complete_http_stream_and_connection(AWS_ERROR_SUCCESS); |
| 612 |
|
| 613 |
finishing_checks: |
| 614 |
|
| 615 |
|
| 616 |
if (s_tester.handshake_request) { |
| 617 |
aws_http_message_destroy(s_tester.handshake_request); |
| 618 |
s_tester.handshake_request = NULL; |
| 619 |
} |
| 620 |
|
| 621 |
if (!websocket_connect_called_successfully) { |
| 622 |
|
| 623 |
*out_error_code = aws_last_error(); |
| 624 |
ASSERT_FALSE(s_tester.websocket_setup_invoked); |
| 625 |
ASSERT_FALSE(s_tester.websocket_shutdown_invoked); |
| 626 |
} else { |
| 627 |
|
| 628 |
ASSERT_TRUE(s_tester.websocket_setup_invoked); |
| 629 |
if (s_tester.websocket_setup_error_code) { |
| 630 |
*out_error_code = s_tester.websocket_setup_error_code; |
| 631 |
|
| 632 |
|
| 633 |
ASSERT_FALSE(s_tester.websocket_shutdown_invoked); |
| 634 |
} else { |
| 635 |
*out_error_code = s_tester.websocket_shutdown_error_code; |
| 636 |
|
| 637 |
|
| 638 |
ASSERT_TRUE(s_tester.websocket_shutdown_invoked); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
if (s_tester.http_stream_new_called_successfully) { |
| 644 |
ASSERT_TRUE(s_tester.http_stream_release_called); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
if (http_connect_setup_reported_success) { |
| 649 |
ASSERT_TRUE(s_tester.http_connection_release_called); |
| 650 |
} |
| 651 |
|
| 652 |
return AWS_OP_SUCCESS; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
TEST_CASE(websocket_boot_sanity_check) { |
| 657 |
(void)ctx; |
| 658 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 659 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 660 |
return AWS_OP_SUCCESS; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
TEST_CASE(websocket_boot_golden_path) { |
| 665 |
(void)ctx; |
| 666 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 667 |
|
| 668 |
int websocket_connect_error_code; |
| 669 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 670 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, websocket_connect_error_code); |
| 671 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_status); |
| 672 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_headers); |
| 673 |
ASSERT_FALSE(s_tester.websocket_setup_had_response_body); |
| 674 |
|
| 675 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 676 |
return AWS_OP_SUCCESS; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
static int s_websocket_boot_fail_at_step_test(struct aws_allocator *alloc, void *ctx, enum boot_step fail_at_step) { |
| 681 |
(void)ctx; |
| 682 |
s_tester.fail_at_step = fail_at_step; |
| 683 |
ASSERT_SUCCESS(s_tester_init(alloc)); |
| 684 |
|
| 685 |
int websocket_connect_error_code; |
| 686 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 687 |
ASSERT_INT_EQUALS(fail_at_step, websocket_connect_error_code); |
| 688 |
|
| 689 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 690 |
return AWS_OP_SUCCESS; |
| 691 |
} |
| 692 |
|
| 693 |
TEST_CASE(websocket_boot_fail_at_http_connect) { |
| 694 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_HTTP_CONNECT); |
| 695 |
} |
| 696 |
|
| 697 |
TEST_CASE(websocket_boot_fail_at_http_connect_error) { |
| 698 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_HTTP_CONNECT_COMPLETE); |
| 699 |
} |
| 700 |
|
| 701 |
TEST_CASE(websocket_boot_fail_at_new_request) { |
| 702 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_REQUEST_NEW); |
| 703 |
} |
| 704 |
|
| 705 |
TEST_CASE(websocket_boot_fail_at_activate_request) { |
| 706 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_REQUEST_ACTIVATE); |
| 707 |
} |
| 708 |
|
| 709 |
TEST_CASE(websocket_boot_fail_before_response_headers) { |
| 710 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_BEFORE_HEADERS); |
| 711 |
} |
| 712 |
|
| 713 |
TEST_CASE(websocket_boot_fail_before_response_headers_done) { |
| 714 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_BEFORE_HEADERS_DONE); |
| 715 |
} |
| 716 |
|
| 717 |
TEST_CASE(websocket_boot_fail_at_new_handler) { |
| 718 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_WEBSOCKET_NEW); |
| 719 |
} |
| 720 |
|
| 721 |
TEST_CASE(websocket_boot_report_unexpected_http_shutdown) { |
| 722 |
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_HTTP_SHUTDOWN); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
TEST_CASE(websocket_boot_fail_from_handshake_rejection) { |
| 729 |
(void)ctx; |
| 730 |
s_tester.handshake_response = &s_rejected_response; |
| 731 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 732 |
|
| 733 |
int websocket_connect_error_code; |
| 734 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 735 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE, websocket_connect_error_code); |
| 736 |
|
| 737 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 738 |
return AWS_OP_SUCCESS; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
TEST_CASE(websocket_boot_fail_before_handshake_rejection_body) { |
| 744 |
(void)ctx; |
| 745 |
s_tester.handshake_response = &s_rejected_response; |
| 746 |
s_tester.fail_at_step = BOOT_STEP_BEFORE_REJECTION_BODY; |
| 747 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 748 |
|
| 749 |
int websocket_connect_error_code; |
| 750 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE, websocket_connect_error_code); |
| 757 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_status); |
| 758 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_headers); |
| 759 |
ASSERT_FALSE(s_tester.websocket_setup_had_response_body); |
| 760 |
|
| 761 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 762 |
return AWS_OP_SUCCESS; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
TEST_CASE(websocket_boot_fail_before_handshake_rejection_stream_complete) { |
| 768 |
(void)ctx; |
| 769 |
s_tester.handshake_response = &s_rejected_response; |
| 770 |
s_tester.fail_at_step = BOOT_STEP_BEFORE_REJECTION_STREAM_COMPLETE; |
| 771 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 772 |
|
| 773 |
int websocket_connect_error_code; |
| 774 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE, websocket_connect_error_code); |
| 781 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_status); |
| 782 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_headers); |
| 783 |
ASSERT_FALSE(s_tester.websocket_setup_had_response_body); |
| 784 |
|
| 785 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 786 |
return AWS_OP_SUCCESS; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
static int s_websocket_boot_fail_from_bad_101_response( |
| 791 |
struct aws_allocator *alloc, |
| 792 |
const struct test_response *bad_response) { |
| 793 |
|
| 794 |
ASSERT_INT_EQUALS(101, bad_response->status_code, "This helper function is only for bad 101 responses"); |
| 795 |
|
| 796 |
s_tester.handshake_response = bad_response; |
| 797 |
ASSERT_SUCCESS(s_tester_init(alloc)); |
| 798 |
|
| 799 |
int websocket_connect_error_code; |
| 800 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 801 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE, websocket_connect_error_code); |
| 802 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_status); |
| 803 |
ASSERT_TRUE(s_tester.websocket_setup_had_response_headers); |
| 804 |
ASSERT_FALSE(s_tester.websocket_setup_had_response_body); |
| 805 |
|
| 806 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 807 |
return AWS_OP_SUCCESS; |
| 808 |
} |
| 809 |
|
| 810 |
TEST_CASE(websocket_boot_fail_from_invalid_upgrade_header) { |
| 811 |
(void)ctx; |
| 812 |
struct test_response bad_response = { |
| 813 |
.status_code = 101, |
| 814 |
.headers = |
| 815 |
{ |
| 816 |
{ |
| 817 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 818 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/9000"), |
| 819 |
}, |
| 820 |
{ |
| 821 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 822 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 823 |
}, |
| 824 |
{ |
| 825 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 826 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 827 |
}, |
| 828 |
}, |
| 829 |
}; |
| 830 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 831 |
} |
| 832 |
|
| 833 |
TEST_CASE(websocket_boot_fail_from_missing_upgrade_header) { |
| 834 |
(void)ctx; |
| 835 |
struct test_response bad_response = { |
| 836 |
.status_code = 101, |
| 837 |
.headers = |
| 838 |
{ |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
{ |
| 846 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 847 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 848 |
}, |
| 849 |
{ |
| 850 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 851 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 852 |
}, |
| 853 |
}, |
| 854 |
}; |
| 855 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 856 |
} |
| 857 |
|
| 858 |
TEST_CASE(websocket_boot_fail_from_invalid_connection_header) { |
| 859 |
(void)ctx; |
| 860 |
struct test_response bad_response = { |
| 861 |
.status_code = 101, |
| 862 |
.headers = |
| 863 |
{ |
| 864 |
{ |
| 865 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 866 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 867 |
}, |
| 868 |
{ |
| 869 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 870 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HeartToHeart"), |
| 871 |
}, |
| 872 |
{ |
| 873 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 874 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 875 |
}, |
| 876 |
}, |
| 877 |
}; |
| 878 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 879 |
} |
| 880 |
|
| 881 |
TEST_CASE(websocket_boot_fail_from_invalid_sec_websocket_accept_header) { |
| 882 |
(void)ctx; |
| 883 |
struct test_response bad_response = { |
| 884 |
.status_code = 101, |
| 885 |
.headers = |
| 886 |
{ |
| 887 |
{ |
| 888 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 889 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 890 |
}, |
| 891 |
{ |
| 892 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 893 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 894 |
}, |
| 895 |
{ |
| 896 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 897 |
|
| 898 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("S3PPLMBITXAQ9KYGZZHZRBK+XOO="), |
| 899 |
}, |
| 900 |
}, |
| 901 |
}; |
| 902 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 903 |
} |
| 904 |
|
| 905 |
TEST_CASE(websocket_boot_fail_from_unsupported_sec_websocket_extensions_in_request) { |
| 906 |
(void)ctx; |
| 907 |
struct aws_http_header extra_request_headers[] = { |
| 908 |
|
| 909 |
{ |
| 910 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Extensions"), |
| 911 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("permessage-deflate"), |
| 912 |
}, |
| 913 |
}; |
| 914 |
s_tester.extra_handshake_request_header_array = extra_request_headers; |
| 915 |
s_tester.num_extra_handshake_request_headers = AWS_ARRAY_SIZE(extra_request_headers); |
| 916 |
|
| 917 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 918 |
|
| 919 |
int websocket_connect_error_code; |
| 920 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 921 |
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, websocket_connect_error_code); |
| 922 |
ASSERT_FALSE(s_tester.websocket_setup_invoked); |
| 923 |
|
| 924 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 925 |
return AWS_OP_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
TEST_CASE(websocket_boot_fail_from_unsupported_sec_websocket_extensions_in_response) { |
| 929 |
(void)ctx; |
| 930 |
struct test_response bad_response = { |
| 931 |
.status_code = 101, |
| 932 |
.headers = |
| 933 |
{ |
| 934 |
{ |
| 935 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 936 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 937 |
}, |
| 938 |
{ |
| 939 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 940 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 941 |
}, |
| 942 |
{ |
| 943 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 944 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 945 |
}, |
| 946 |
{ |
| 947 |
|
| 948 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Extensions"), |
| 949 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("permessage-deflate"), |
| 950 |
}, |
| 951 |
}, |
| 952 |
}; |
| 953 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
TEST_CASE(websocket_boot_ok_with_sec_websocket_protocol_header) { |
| 958 |
(void)ctx; |
| 959 |
struct aws_http_header extra_request_headers[] = { |
| 960 |
{ |
| 961 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 962 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt"), |
| 963 |
}, |
| 964 |
}; |
| 965 |
s_tester.extra_handshake_request_header_array = extra_request_headers; |
| 966 |
s_tester.num_extra_handshake_request_headers = AWS_ARRAY_SIZE(extra_request_headers); |
| 967 |
|
| 968 |
struct test_response response = { |
| 969 |
.status_code = 101, |
| 970 |
.headers = |
| 971 |
{ |
| 972 |
{ |
| 973 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 974 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 975 |
}, |
| 976 |
{ |
| 977 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 978 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 979 |
}, |
| 980 |
{ |
| 981 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 982 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 983 |
}, |
| 984 |
{ |
| 985 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 986 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt"), |
| 987 |
}, |
| 988 |
}, |
| 989 |
}; |
| 990 |
s_tester.handshake_response = &response; |
| 991 |
|
| 992 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 993 |
|
| 994 |
int websocket_connect_error_code; |
| 995 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 996 |
ASSERT_INT_EQUALS(0, websocket_connect_error_code); |
| 997 |
|
| 998 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 999 |
return AWS_OP_SUCCESS; |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
TEST_CASE(websocket_boot_ok_with_sec_websocket_protocol_split_across_headers) { |
| 1004 |
(void)ctx; |
| 1005 |
struct aws_http_header extra_request_headers[] = { |
| 1006 |
{ |
| 1007 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1008 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("http/1.1, http/2"), |
| 1009 |
}, |
| 1010 |
{ |
| 1011 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1012 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt, mqtt5, mqtt6"), |
| 1013 |
}, |
| 1014 |
{ |
| 1015 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1016 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("klingon, esperanto"), |
| 1017 |
}, |
| 1018 |
}; |
| 1019 |
s_tester.extra_handshake_request_header_array = extra_request_headers; |
| 1020 |
s_tester.num_extra_handshake_request_headers = AWS_ARRAY_SIZE(extra_request_headers); |
| 1021 |
|
| 1022 |
struct test_response response = { |
| 1023 |
.status_code = 101, |
| 1024 |
.headers = |
| 1025 |
{ |
| 1026 |
{ |
| 1027 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 1028 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 1029 |
}, |
| 1030 |
{ |
| 1031 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 1032 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 1033 |
}, |
| 1034 |
{ |
| 1035 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 1036 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 1037 |
}, |
| 1038 |
{ |
| 1039 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1040 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt5"), |
| 1041 |
}, |
| 1042 |
}, |
| 1043 |
}; |
| 1044 |
s_tester.handshake_response = &response; |
| 1045 |
|
| 1046 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1047 |
|
| 1048 |
int websocket_connect_error_code; |
| 1049 |
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code)); |
| 1050 |
ASSERT_INT_EQUALS(0, websocket_connect_error_code); |
| 1051 |
|
| 1052 |
ASSERT_SUCCESS(s_tester_clean_up()); |
| 1053 |
return AWS_OP_SUCCESS; |
| 1054 |
} |
| 1055 |
|
| 1056 |
TEST_CASE(websocket_boot_fail_from_missing_sec_websocket_protocol_header) { |
| 1057 |
(void)ctx; |
| 1058 |
struct aws_http_header extra_request_headers[] = { |
| 1059 |
{ |
| 1060 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1061 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt, mqtt5"), |
| 1062 |
}, |
| 1063 |
}; |
| 1064 |
s_tester.extra_handshake_request_header_array = extra_request_headers; |
| 1065 |
s_tester.num_extra_handshake_request_headers = AWS_ARRAY_SIZE(extra_request_headers); |
| 1066 |
|
| 1067 |
struct test_response bad_response = { |
| 1068 |
.status_code = 101, |
| 1069 |
.headers = |
| 1070 |
{ |
| 1071 |
{ |
| 1072 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 1073 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 1074 |
}, |
| 1075 |
{ |
| 1076 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 1077 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 1078 |
}, |
| 1079 |
{ |
| 1080 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 1081 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 1082 |
}, |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
}, |
| 1090 |
}; |
| 1091 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 1092 |
} |
| 1093 |
|
| 1094 |
TEST_CASE(websocket_boot_fail_from_invalid_sec_websocket_protocol_header) { |
| 1095 |
(void)ctx; |
| 1096 |
struct aws_http_header extra_request_headers[] = { |
| 1097 |
{ |
| 1098 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1099 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt, mqtt5"), |
| 1100 |
}, |
| 1101 |
}; |
| 1102 |
s_tester.extra_handshake_request_header_array = extra_request_headers; |
| 1103 |
s_tester.num_extra_handshake_request_headers = AWS_ARRAY_SIZE(extra_request_headers); |
| 1104 |
|
| 1105 |
struct test_response bad_response = { |
| 1106 |
.status_code = 101, |
| 1107 |
.headers = |
| 1108 |
{ |
| 1109 |
{ |
| 1110 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 1111 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("websocket"), |
| 1112 |
}, |
| 1113 |
{ |
| 1114 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 1115 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Upgrade"), |
| 1116 |
}, |
| 1117 |
{ |
| 1118 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Accept"), |
| 1119 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3pPLMBiTxaQ9kYGzzhZRbK+xOo="), |
| 1120 |
}, |
| 1121 |
{ |
| 1122 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Sec-WebSocket-Protocol"), |
| 1123 |
|
| 1124 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("mqtt, mqtt5"), |
| 1125 |
}, |
| 1126 |
}, |
| 1127 |
}; |
| 1128 |
return s_websocket_boot_fail_from_bad_101_response(allocator, &bad_response); |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
TEST_CASE(websocket_handshake_key_max_length) { |
| 1133 |
(void)allocator; |
| 1134 |
(void)ctx; |
| 1135 |
|
| 1136 |
uint8_t small_buf_storage[AWS_WEBSOCKET_MAX_HANDSHAKE_KEY_LENGTH]; |
| 1137 |
for (size_t i = 0; i < 100; ++i) { |
| 1138 |
struct aws_byte_buf small_buf = aws_byte_buf_from_empty_array(small_buf_storage, sizeof(small_buf_storage)); |
| 1139 |
ASSERT_SUCCESS(aws_websocket_random_handshake_key(&small_buf)); |
| 1140 |
} |
| 1141 |
|
| 1142 |
return AWS_OP_SUCCESS; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
TEST_CASE(websocket_handshake_key_randomness) { |
| 1147 |
(void)ctx; |
| 1148 |
|
| 1149 |
enum { count = 100 }; |
| 1150 |
struct aws_byte_buf keys[count]; |
| 1151 |
|
| 1152 |
for (int i = 0; i < count; ++i) { |
| 1153 |
struct aws_byte_buf *key = &keys[i]; |
| 1154 |
ASSERT_SUCCESS(aws_byte_buf_init(key, allocator, AWS_WEBSOCKET_MAX_HANDSHAKE_KEY_LENGTH)); |
| 1155 |
ASSERT_SUCCESS(aws_websocket_random_handshake_key(key)); |
| 1156 |
for (int existing_i = 0; existing_i < i; ++existing_i) { |
| 1157 |
struct aws_byte_buf *existing = &keys[existing_i]; |
| 1158 |
ASSERT_FALSE(aws_byte_buf_eq(key, existing)); |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
for (int i = 0; i < count; ++i) { |
| 1163 |
aws_byte_buf_clean_up(&keys[i]); |
| 1164 |
} |
| 1165 |
|
| 1166 |
return AWS_OP_SUCCESS; |
| 1167 |
} |