Debian Codegraph @

crt/aws-c-http/tests/test_websocket_handler.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/http/private/websocket_impl.h>
7
8 #include <aws/http/private/websocket_decoder.h>
9 #include <aws/http/private/websocket_encoder.h>
10 #include <aws/io/logging.h>
11 #include <aws/testing/io_testing_channel.h>
12
13 #ifdef _MSC_VER
14 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
15 #endif
16
17 /* Use small window so that we can observe it opening in tests.
18 * Channel may wait until the window is small before issuing the increment command. */
19 static const size_t s_default_initial_window_size = 256;
20
21 #define TEST_CASE(NAME) \
22 AWS_TEST_CASE(NAME, s_test_##NAME); \
23 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
24
25 struct written_frame {
26 struct aws_websocket_frame def;
27 struct aws_byte_buf payload;
28 bool is_complete;
29 };
30
31 struct incoming_frame {
32 struct aws_websocket_incoming_frame def;
33 struct aws_byte_buf payload;
34 size_t on_payload_count;
35 int on_complete_error_code;
36 bool has_begun;
37 bool is_complete;
38 };
39
40 static struct tester_options {
41 bool manual_window_update;
42 } s_tester_options;
43
44 struct tester {
45 struct aws_allocator *alloc;
46 struct aws_logger logger;
47 void *specific_test_data;
48
49 struct testing_channel testing_channel;
50 struct aws_websocket *websocket;
51 bool is_midchannel_handler;
52
53 size_t on_send_complete_count;
54
55 /* To make the written output of the websocket-handler easier to check,
56 * we translate the written bytes back into `written_frames` using a websocket-decoder.
57 * We're not testing the decoder here, just using it as a tool (decoder tests go in test_websocket_decoder.c). */
58 struct written_frame written_frames[100];
59 size_t num_written_frames;
60 size_t num_written_io_messages;
61 struct aws_websocket_decoder written_frame_decoder;
62
63 /* Frames reported via the websocket's on_incoming_frame callbacks are recorded here */
64 struct incoming_frame incoming_frames[100];
65 size_t num_incoming_frames;
66 size_t fail_on_incoming_frame_begin_n; /* If set, return false on Nth incoming_frame_begin callback */
67 size_t fail_on_incoming_frame_payload_n; /* If set, return false on Nth incoming_frame_payload callback */
68 size_t fail_on_incoming_frame_complete_n; /* If set, return false on Nth incoming_frame_complete callback */
69
70 /* For pushing messages downstream, to be read by websocket handler.
71 * readpush_frame is for tests to define websocket frames to be pushed downstream.
72 * An encoder is used to turn these into proper bits */
73 struct readpush_frame *readpush_frames;
74 size_t num_readpush_frames;
75 size_t readpush_frame_index;
76 struct aws_websocket_encoder readpush_encoder;
77
78 /* For pushing messages upstream, to test a websocket that's been converted to midchannel handler. */
79 size_t num_writepush_messages;
80 struct aws_byte_buf all_writepush_data; /* All data that's been writepushed, concatenated together */
81 };
82
83 /* Helps track the progress of a frame being sent. */
84 struct send_tester {
85 struct aws_websocket_send_frame_options def; /* some properties are autoconfigured */
86 struct aws_byte_cursor payload;
87
88 size_t delay_ticks; /* Don't send anything the first N ticks */
89 size_t bytes_per_tick; /* Don't send more than N bytes per tick */
90 size_t send_wrong_payload_amount;
91
92 /* Everything below this line is auto-configured */
93 struct tester *owner;
94
95 struct aws_byte_cursor cursor; /* iterates as payload is written */
96 size_t on_payload_count;
97 size_t fail_on_nth_payload; /* If set, returns false on Nth callback (1 is first callback)*/
98
99 size_t on_complete_count;
100 size_t on_complete_order; /* Order that frame sent, amongst all frames sent this test */
101 int on_complete_error_code;
102 bool fail_on_complete; /* If true, return false from on_complete callback */
103 };
104
105 struct readpush_frame {
106 struct aws_websocket_frame def;
107 struct aws_byte_cursor payload;
108
109 /* Everything below this is auto-configured */
110 struct aws_byte_cursor cursor; /* advances as payload is written */
111 };
112
113 /* Run loop that keeps the websocket-handler chugging. We need this because:
114 * 1) The websocket-handler won't write the next aws_io_message until the preceding one is processed.
115 * 2) The websocket-handler won't finish shutdown until it can write a CLOSE frame.
116 *
117 * Repeat until no more work is being done:
118 * - Drain task queue.
119 * - Decode written aws_io_messages from raw bytes into tester->written_frames[].
120 * - Mark aws_io_messages completed.
121 */
122 static int s_drain_written_messages(struct tester *tester) {
123 struct aws_linked_list *io_msgs = testing_channel_get_written_message_queue(&tester->testing_channel);
124 bool still_draining;
125 do {
126 still_draining = false;
127 testing_channel_drain_queued_tasks(&tester->testing_channel);
128
129 while (!aws_linked_list_empty(io_msgs)) {
130 struct aws_linked_list_node *node = aws_linked_list_pop_front(io_msgs);
131 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
132
133 tester->num_written_io_messages++;
134
135 struct aws_byte_cursor msg_cursor = aws_byte_cursor_from_buf(&msg->message_data);
136 while (msg_cursor.len) {
137 /* Make sure our arbitrarily sized buffer hasn't overflowed. */
138 ASSERT_TRUE(tester->num_written_frames < AWS_ARRAY_SIZE(tester->written_frames));
139
140 bool frame_complete;
141 ASSERT_SUCCESS(
142 aws_websocket_decoder_process(&tester->written_frame_decoder, &msg_cursor, &frame_complete));
143
144 if (frame_complete) {
145 tester->written_frames[tester->num_written_frames].is_complete = true;
146 tester->num_written_frames++;
147 }
148 }
149
150 if (msg->on_completion) {
151 msg->on_completion(tester->testing_channel.channel, msg, 0, msg->user_data);
152 still_draining = true;
153 }
154
155 aws_mem_release(msg->allocator, msg);
156 }
157 } while (still_draining);
158
159 return AWS_OP_SUCCESS;
160 }
161
162 static int s_on_written_frame(const struct aws_websocket_frame *frame, void *user_data) {
163 struct tester *tester = user_data;
164 struct written_frame *written = &tester->written_frames[tester->num_written_frames];
165 written->def = *frame;
166 if (frame->payload_length) {
167 AWS_FATAL_ASSERT(frame->payload_length <= SIZE_MAX);
168 ASSERT_SUCCESS(aws_byte_buf_init(&written->payload, tester->alloc, (size_t)frame->payload_length));
169 }
170 return AWS_OP_SUCCESS;
171 }
172
173 static int s_on_written_frame_payload(struct aws_byte_cursor data, void *user_data) {
174 struct tester *tester = user_data;
175 struct written_frame *written = &tester->written_frames[tester->num_written_frames];
176 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&written->payload, data));
177 return AWS_OP_SUCCESS;
178 }
179
180 static bool s_on_incoming_frame_begin(
181 struct aws_websocket *websocket,
182 const struct aws_websocket_incoming_frame *frame,
183 void *user_data) {
184
185 (void)websocket;
186 struct tester *tester = user_data;
187
188 /* Make sure our arbitrarily-sized testing buffer hasn't overflowed */
189 AWS_FATAL_ASSERT(tester->num_incoming_frames < AWS_ARRAY_SIZE(tester->incoming_frames));
190
191 if (tester->num_incoming_frames > 0) {
192 /* Make sure previous frame was marked complete */
193 AWS_FATAL_ASSERT(tester->incoming_frames[tester->num_incoming_frames - 1].is_complete);
194 }
195
196 struct incoming_frame *incoming_frame = &tester->incoming_frames[tester->num_incoming_frames];
197
198 AWS_FATAL_ASSERT(!incoming_frame->has_begun);
199 incoming_frame->has_begun = true;
200 incoming_frame->def = *frame;
201
202 AWS_FATAL_ASSERT(frame->payload_length <= SIZE_MAX);
203 int err = aws_byte_buf_init(&incoming_frame->payload, tester->alloc, (size_t)frame->payload_length);
204 AWS_FATAL_ASSERT(!err);
205
206 if (tester->fail_on_incoming_frame_begin_n) {
207 AWS_FATAL_ASSERT(tester->num_incoming_frames < tester->fail_on_incoming_frame_begin_n);
208
209 if ((tester->num_incoming_frames + 1) == tester->fail_on_incoming_frame_begin_n) {
210 return false;
211 }
212 }
213 return true;
214 }
215
216 static bool s_on_incoming_frame_payload(
217 struct aws_websocket *websocket,
218 const struct aws_websocket_incoming_frame *frame,
219 struct aws_byte_cursor data,
220 void *user_data) {
221
222 (void)websocket;
223 (void)frame;
224 struct tester *tester = user_data;
225 struct incoming_frame *incoming_frame = &tester->incoming_frames[tester->num_incoming_frames];
226 AWS_FATAL_ASSERT(incoming_frame->has_begun);
227 AWS_FATAL_ASSERT(!incoming_frame->is_complete);
228
229 /* buffer was allocated to exact payload length, so write should succeed */
230 AWS_FATAL_ASSERT(aws_byte_buf_write_from_whole_cursor(&incoming_frame->payload, data));
231
232 incoming_frame->on_payload_count++;
233
234 if (tester->fail_on_incoming_frame_payload_n) {
235 AWS_FATAL_ASSERT(incoming_frame->on_payload_count <= tester->fail_on_incoming_frame_payload_n);
236
237 if (incoming_frame->on_payload_count == tester->fail_on_incoming_frame_payload_n) {
238 return false;
239 }
240 }
241 return true;
242 }
243
244 static bool s_on_incoming_frame_complete(
245 struct aws_websocket *websocket,
246 const struct aws_websocket_incoming_frame *frame,
247 int error_code,
248 void *user_data) {
249
250 (void)websocket;
251 (void)frame;
252 struct tester *tester = user_data;
253 struct incoming_frame *incoming_frame = &tester->incoming_frames[tester->num_incoming_frames++];
254 AWS_FATAL_ASSERT(incoming_frame->has_begun);
255 AWS_FATAL_ASSERT(!incoming_frame->is_complete);
256
257 incoming_frame->is_complete = true;
258 incoming_frame->on_complete_error_code = error_code;
259 if (error_code == AWS_ERROR_SUCCESS) {
260 AWS_FATAL_ASSERT(incoming_frame->payload.len == incoming_frame->def.payload_length);
261 }
262
263 if (tester->fail_on_incoming_frame_complete_n) {
264 AWS_FATAL_ASSERT(tester->num_incoming_frames <= tester->fail_on_incoming_frame_complete_n);
265
266 if (tester->num_incoming_frames == tester->fail_on_incoming_frame_complete_n) {
267 return false;
268 }
269 }
270 return true;
271 }
272
273 static void s_set_readpush_frames(struct tester *tester, struct readpush_frame *frames, size_t num_frames) {
274 tester->readpush_frames = frames;
275 tester->num_readpush_frames = num_frames;
276 tester->readpush_frame_index = 0;
277 for (size_t i = 0; i < num_frames; ++i) {
278 struct readpush_frame *frame = &frames[i];
279 frame->cursor = frame->payload;
280 frame->def.payload_length = frame->payload.len;
281 }
282 }
283
284 static int s_stream_readpush_payload(struct aws_byte_buf *out_buf, void *user_data) {
285 struct tester *tester = user_data;
286
287 struct readpush_frame *frame = &tester->readpush_frames[tester->readpush_frame_index];
288 size_t available_bytes = out_buf->capacity - out_buf->len;
289 size_t sending_bytes = available_bytes < frame->cursor.len ? available_bytes : frame->cursor.len;
290 struct aws_byte_cursor sending_cursor = aws_byte_cursor_advance(&frame->cursor, sending_bytes);
291 AWS_FATAL_ASSERT(sending_cursor.len > 0);
292
293 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(out_buf, sending_cursor));
294 return AWS_OP_SUCCESS;
295 }
296
297 /* Options for pushing readpush_frames. Anything set to 0 is treated as "unlimited" */
298 struct readpush_options {
299 size_t num_frames; /* Stop after pushing this many frames. */
300 size_t num_bytes; /* Stop after pushing this many total bytes of aws_io_messages */
301 size_t num_messages; /* Stop after pushing this many aws_io_messages */
302 size_t message_size; /* Force fragmentation by limiting amount packed into each aws_io_message */
303 };
304
305 /* Encode readpush_frames into aws_io_messages and push those to websocket-handler. */
306 static int s_do_readpush(struct tester *tester, struct readpush_options options) {
307 const size_t max_frames = options.num_frames ? options.num_frames : SIZE_MAX;
308 const size_t max_bytes = options.num_bytes ? options.num_bytes : SIZE_MAX;
309 const size_t max_messages = options.num_messages ? options.num_messages : SIZE_MAX;
310 const size_t message_size = options.message_size ? options.message_size : (16 * 1024);
311
312 size_t sum_frames = 0;
313 size_t sum_bytes = 0;
314 size_t sum_messages = 0;
315
316 bool done = tester->readpush_frame_index >= tester->num_readpush_frames;
317 while (!done) {
318 size_t remaining_bytes = max_bytes - sum_bytes;
319 size_t request_bytes = remaining_bytes < message_size ? remaining_bytes : message_size;
320 struct aws_io_message *msg = aws_channel_acquire_message_from_pool(
321 tester->testing_channel.channel, AWS_IO_MESSAGE_APPLICATION_DATA, request_bytes);
322 ASSERT_NOT_NULL(msg);
323
324 while (!done && (msg->message_data.len < msg->message_data.capacity)) {
325
326 if (!aws_websocket_encoder_is_frame_in_progress(&tester->readpush_encoder)) {
327 ASSERT_SUCCESS(aws_websocket_encoder_start_frame(
328 &tester->readpush_encoder, &tester->readpush_frames[tester->readpush_frame_index].def));
329 }
330
331 ASSERT_SUCCESS(aws_websocket_encoder_process(&tester->readpush_encoder, &msg->message_data));
332
333 if (aws_websocket_encoder_is_frame_in_progress(&tester->readpush_encoder)) {
334 /* This function doesn't expect encoder to stop until frame is done or buffer is full */
335 ASSERT_UINT_EQUALS(msg->message_data.len, msg->message_data.capacity);
336 } else {
337 /* Frame done */
338 if (++tester->readpush_frame_index >= tester->num_readpush_frames) {
339 done = true;
340 }
341
342 if (++sum_frames >= max_frames) {
343 done = true;
344 }
345 }
346 }
347
348 sum_bytes += msg->message_data.len;
349 if (sum_bytes >= max_bytes) {
350 done = true;
351 }
352
353 if (++sum_messages >= max_messages) {
354 done = true;
355 }
356
357 ASSERT_SUCCESS(testing_channel_push_read_message(&tester->testing_channel, msg));
358 }
359
360 return AWS_OP_SUCCESS;
361 }
362
363 static int s_do_readpush_all(struct tester *tester) {
364 struct readpush_options options;
365 AWS_ZERO_STRUCT(options);
366 return s_do_readpush(tester, options);
367 }
368
369 /* Check that a readpush_frame was received by websocket */
370 static int s_readpush_check(struct tester *tester, size_t frame_i, int expected_error_code) {
371 ASSERT_TRUE(frame_i < tester->num_readpush_frames);
372 struct readpush_frame *pushed = &tester->readpush_frames[frame_i];
373 struct incoming_frame *received = &tester->incoming_frames[frame_i];
374
375 ASSERT_TRUE(received->has_begun);
376 ASSERT_TRUE(received->is_complete);
377 ASSERT_INT_EQUALS(expected_error_code, received->on_complete_error_code);
378
379 ASSERT_UINT_EQUALS(pushed->def.payload_length, received->def.payload_length);
380 ASSERT_UINT_EQUALS(pushed->def.opcode, received->def.opcode);
381 ASSERT_INT_EQUALS(pushed->def.fin, received->def.fin);
382
383 if (received->on_complete_error_code == AWS_ERROR_SUCCESS) {
384 ASSERT_UINT_EQUALS(received->def.payload_length, received->payload.len);
385 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&pushed->payload, &received->payload));
386 }
387
388 return AWS_OP_SUCCESS;
389 }
390
391 /* Check that a readpush_frame's payload was passed to the next handler downstream */
392 static int s_readpush_midchannel_check(struct tester *tester, size_t frame_i) {
393 ASSERT_TRUE(frame_i < tester->num_readpush_frames);
394 struct readpush_frame *pushed = &tester->readpush_frames[frame_i];
395 struct aws_byte_cursor payload = pushed->payload;
396
397 struct aws_linked_list *downstream_messages = testing_channel_get_read_message_queue(&tester->testing_channel);
398
399 while (payload.len > 0) {
400 ASSERT_FALSE(aws_linked_list_empty(downstream_messages));
401 struct aws_linked_list_node *message_node = aws_linked_list_front(downstream_messages);
402 struct aws_io_message *message = AWS_CONTAINER_OF(message_node, struct aws_io_message, queueing_handle);
403
404 /* This function might be called multiple times, the copy_mark is used to track where the last check ended */
405 size_t message_remainder = message->message_data.len - message->copy_mark;
406 size_t compare_bytes = message_remainder < payload.len ? message_remainder : payload.len;
407 struct aws_byte_cursor message_chunk =
408 aws_byte_cursor_from_array(message->message_data.buffer + message->copy_mark, compare_bytes);
409
410 struct aws_byte_cursor payload_chunk = aws_byte_cursor_advance(&payload, compare_bytes);
411 ASSERT_TRUE(aws_byte_cursor_eq(&message_chunk, &payload_chunk));
412 message->copy_mark += compare_bytes;
413 if (message->copy_mark == message->message_data.len) {
414 aws_linked_list_pop_front(downstream_messages);
415 aws_mem_release(message->allocator, message);
416 }
417 }
418
419 return AWS_OP_SUCCESS;
420 }
421
422 static int s_writepush(struct tester *tester, struct aws_byte_cursor data) {
423 if (!tester->all_writepush_data.allocator) {
424 ASSERT_SUCCESS(aws_byte_buf_init(&tester->all_writepush_data, tester->alloc, data.len));
425 }
426
427 while (data.len) {
428 /* Ask for slightly more data than we need so that capacity != length.
429 * This is to repro a bug where capacity and length were confused */
430 size_t size_hint = data.len + 1;
431
432 struct aws_io_message *msg = aws_channel_acquire_message_from_pool(
433 tester->testing_channel.channel, AWS_IO_MESSAGE_APPLICATION_DATA, size_hint);
434 ASSERT_NOT_NULL(msg);
435 size_t chunk_size = msg->message_data.capacity < data.len ? msg->message_data.capacity : data.len;
436 struct aws_byte_cursor chunk = aws_byte_cursor_advance(&data, chunk_size);
437 ASSERT_NOT_NULL(chunk.ptr);
438 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&msg->message_data, chunk));
439 ASSERT_SUCCESS(testing_channel_push_write_message(&tester->testing_channel, msg));
440
441 /* Update tracking data in tester */
442 tester->num_writepush_messages++;
443 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&tester->all_writepush_data, &chunk));
444 }
445 return AWS_OP_SUCCESS;
446 }
447
448 /* Scan all written_frames, and ensure that payloads of the binary frames match data */
449 static int s_writepush_check(struct tester *tester, size_t ignore_n_written_frames) {
450 struct aws_byte_cursor expected_cursor = aws_byte_cursor_from_buf(&tester->all_writepush_data);
451 for (size_t i = ignore_n_written_frames; i < tester->num_written_frames; ++i) {
452 struct written_frame *frame_i = &tester->written_frames[i];
453 if (aws_websocket_is_data_frame(frame_i->def.opcode)) {
454 ASSERT_UINT_EQUALS(AWS_WEBSOCKET_OPCODE_BINARY, frame_i->def.opcode);
455 struct aws_byte_cursor expected_i =
456 aws_byte_cursor_advance(&expected_cursor, (size_t)frame_i->def.payload_length);
457 ASSERT_TRUE(expected_i.len > 0);
458 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&expected_i, &frame_i->payload));
459 }
460 }
461 ASSERT_UINT_EQUALS(0, expected_cursor.len);
462 return AWS_OP_SUCCESS;
463 }
464
465 static int s_tester_init(struct tester *tester, struct aws_allocator *alloc) {
466 aws_http_library_init(alloc);
467
468 AWS_ZERO_STRUCT(*tester);
469 tester->alloc = alloc;
470
471 struct aws_logger_standard_options logger_options = {
472 .level = AWS_LOG_LEVEL_TRACE,
473 .file = stderr,
474 };
475 ASSERT_SUCCESS(aws_logger_init_standard(&tester->logger, tester->alloc, &logger_options));
476 aws_logger_set(&tester->logger);
477
478 struct aws_testing_channel_options test_channel_options = {.clock_fn = aws_high_res_clock_get_ticks};
479 ASSERT_SUCCESS(testing_channel_init(&tester->testing_channel, alloc, &test_channel_options));
480
481 struct aws_websocket_handler_options ws_options = {
482 .allocator = alloc,
483 .channel = tester->testing_channel.channel,
484 .initial_window_size = s_default_initial_window_size,
485 .user_data = tester,
486 .on_incoming_frame_begin = s_on_incoming_frame_begin,
487 .on_incoming_frame_payload = s_on_incoming_frame_payload,
488 .on_incoming_frame_complete = s_on_incoming_frame_complete,
489 .manual_window_update = s_tester_options.manual_window_update,
490 };
491 tester->websocket = aws_websocket_handler_new(&ws_options);
492 ASSERT_NOT_NULL(tester->websocket);
493 testing_channel_drain_queued_tasks(&tester->testing_channel);
494
495 aws_websocket_decoder_init(
496 &tester->written_frame_decoder, alloc, s_on_written_frame, s_on_written_frame_payload, tester);
497 aws_websocket_encoder_init(&tester->readpush_encoder, s_stream_readpush_payload, tester);
498
499 return AWS_OP_SUCCESS;
500 }
501
502 static int s_tester_clean_up(struct tester *tester) {
503 aws_websocket_release(tester->websocket);
504 ASSERT_SUCCESS(s_drain_written_messages(tester));
505
506 ASSERT_SUCCESS(testing_channel_clean_up(&tester->testing_channel));
507
508 for (size_t i = 0; i < AWS_ARRAY_SIZE(tester->written_frames); ++i) {
509 aws_byte_buf_clean_up(&tester->written_frames[i].payload);
510 }
511
512 for (size_t i = 0; i < AWS_ARRAY_SIZE(tester->incoming_frames); ++i) {
513 aws_byte_buf_clean_up(&tester->incoming_frames[i].payload);
514 }
515
516 aws_byte_buf_clean_up(&tester->all_writepush_data);
517
518 aws_websocket_decoder_clean_up(&tester->written_frame_decoder);
519 aws_http_library_clean_up();
520 aws_logger_clean_up(&tester->logger);
521 return AWS_OP_SUCCESS;
522 }
523
524 static int s_install_downstream_handler(struct tester *tester, size_t initial_window) {
525
526 ASSERT_SUCCESS(aws_websocket_convert_to_midchannel_handler(tester->websocket));
527 tester->is_midchannel_handler = true;
528
529 ASSERT_SUCCESS(testing_channel_install_downstream_handler(&tester->testing_channel, initial_window));
530 testing_channel_drain_queued_tasks(&tester->testing_channel);
531
532 return AWS_OP_SUCCESS;
533 }
534
535 static bool s_on_stream_outgoing_payload(
536 struct aws_websocket *websocket,
537 struct aws_byte_buf *out_buf,
538 void *user_data) {
539
540 struct send_tester *send_tester = user_data;
541 AWS_FATAL_ASSERT(websocket == send_tester->owner->websocket);
542
543 /* If user wants frame to break websocket, write an extra byte */
544 if (send_tester->send_wrong_payload_amount && (send_tester->on_payload_count == 0)) {
545 aws_byte_buf_write_u8(out_buf, 'X');
546 }
547
548 send_tester->on_payload_count++;
549
550 size_t space_available = out_buf->capacity - out_buf->len;
551
552 size_t bytes_max = send_tester->cursor.len;
553 if (send_tester->delay_ticks > 0) {
554 bytes_max = 0;
555 send_tester->delay_ticks--;
556 } else if (send_tester->bytes_per_tick > 0) {
557 bytes_max = bytes_max < send_tester->bytes_per_tick ? bytes_max : send_tester->bytes_per_tick;
558 }
559
560 size_t amount_to_send = bytes_max < space_available ? bytes_max : space_available;
561 struct aws_byte_cursor send_cursor = aws_byte_cursor_advance(&send_tester->cursor, amount_to_send);
562 if (send_cursor.len) {
563 aws_byte_buf_write_from_whole_cursor(out_buf, send_cursor);
564 }
565
566 if (send_tester->fail_on_nth_payload) {
567 AWS_FATAL_ASSERT(send_tester->on_payload_count <= send_tester->fail_on_nth_payload);
568 if (send_tester->on_payload_count == send_tester->fail_on_nth_payload) {
569 return false;
570 }
571 }
572
573 return true;
574 }
575
576 static void s_on_outgoing_frame_complete(struct aws_websocket *websocket, int error_code, void *user_data) {
577 struct send_tester *send_tester = user_data;
578 AWS_FATAL_ASSERT(websocket == send_tester->owner->websocket);
579
580 send_tester->on_complete_error_code = error_code;
581 AWS_FATAL_ASSERT(send_tester->on_complete_count == 0);
582 send_tester->on_complete_count++;
583 send_tester->on_complete_order = send_tester->owner->on_send_complete_count;
584 send_tester->owner->on_send_complete_count++;
585 }
586
587 static int s_send_frame_ex(struct tester *tester, struct send_tester *send_tester, bool assert_on_error) {
588 send_tester->owner = tester;
589 send_tester->cursor = send_tester->payload;
590
591 send_tester->def.payload_length = send_tester->payload.len;
592 send_tester->def.stream_outgoing_payload = s_on_stream_outgoing_payload;
593 send_tester->def.on_complete = s_on_outgoing_frame_complete;
594 send_tester->def.user_data = send_tester;
595
596 if (assert_on_error) {
597 ASSERT_SUCCESS(aws_websocket_send_frame(tester->websocket, &send_tester->def));
598 return AWS_OP_SUCCESS;
599 } else {
600 return aws_websocket_send_frame(tester->websocket, &send_tester->def);
601 }
602 }
603
604 static int s_send_frame(struct tester *tester, struct send_tester *send_tester) {
605 return s_send_frame_ex(tester, send_tester, true);
606 }
607
608 static int s_send_frame_no_assert(struct tester *tester, struct send_tester *send_tester) {
609 return s_send_frame_ex(tester, send_tester, false);
610 }
611
612 static int s_check_written_message(struct send_tester *send, size_t expected_order) {
613 struct tester *tester = send->owner;
614
615 ASSERT_UINT_EQUALS(1, send->on_complete_count);
616 ASSERT_UINT_EQUALS(expected_order, send->on_complete_order);
617
618 ASSERT_TRUE(expected_order < tester->num_written_frames);
619 struct written_frame *written = &tester->written_frames[expected_order];
620
621 ASSERT_TRUE(written->is_complete);
622 ASSERT_UINT_EQUALS(send->def.opcode, written->def.opcode);
623 ASSERT_UINT_EQUALS(send->def.payload_length, written->def.payload_length);
624 ASSERT_INT_EQUALS(send->def.fin, written->def.fin);
625
626 /* All payloads sent from client should have been masked (assuming client is being tested here) */
627 ASSERT_TRUE(written->def.masked);
628 if (written->def.masked) {
629 bool valid_masking_key = false;
630 for (int i = 0; i < 4; i++) {
631 if (written->def.masking_key[i]) {
632 valid_masking_key = true;
633 }
634 }
635 ASSERT_TRUE(valid_masking_key);
636 }
637
638 /* If payload was masked, decoder already unmasked it for us, so we can directly compare contents here */
639 ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&send->payload, &written->payload));
640
641 return AWS_OP_SUCCESS;
642 }
643
644 TEST_CASE(websocket_handler_sanity_check) {
645 (void)ctx;
646 struct tester tester;
647 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
648 ASSERT_SUCCESS(s_tester_clean_up(&tester));
649 return AWS_OP_SUCCESS;
650 }
651
652 TEST_CASE(websocket_handler_refcounting) {
653 (void)ctx;
654 struct tester tester;
655 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
656
657 /* acquire() and then release() a refcount. The websocket should not shut down yet */
658 ASSERT_PTR_EQUALS(tester.websocket, aws_websocket_acquire(tester.websocket));
659 aws_websocket_release(tester.websocket);
660
661 testing_channel_drain_queued_tasks(&tester.testing_channel);
662 ASSERT_FALSE(tester.testing_channel.channel_shutdown_completed);
663
664 /* should be safe to call release() on NULL */
665 aws_websocket_release(NULL);
666
667 ASSERT_SUCCESS(s_tester_clean_up(&tester));
668 return AWS_OP_SUCCESS;
669 }
670
671 static int s_websocket_handler_send_frame_common(struct aws_allocator *allocator, bool on_thread) {
672 struct tester tester;
673 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
674
675 const char *payload = "Shall I come in to cut off your threads?";
676
677 struct send_tester send = {
678 .payload = aws_byte_cursor_from_c_str(payload),
679 .def =
680 {
681 .opcode = AWS_WEBSOCKET_OPCODE_PING,
682 .fin = true,
683 },
684 };
685
686 testing_channel_set_is_on_users_thread(&tester.testing_channel, on_thread);
687 ASSERT_SUCCESS(s_send_frame(&tester, &send));
688
689 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
690 ASSERT_SUCCESS(s_drain_written_messages(&tester));
691
692 ASSERT_SUCCESS(s_check_written_message(&send, 0));
693
694 ASSERT_SUCCESS(s_tester_clean_up(&tester));
695 return AWS_OP_SUCCESS;
696 }
697
698 TEST_CASE(websocket_handler_send_frame) {
699 (void)ctx;
700 return s_websocket_handler_send_frame_common(allocator, true);
701 }
702
703 TEST_CASE(websocket_handler_send_frame_off_thread) {
704 (void)ctx;
705 return s_websocket_handler_send_frame_common(allocator, false);
706 }
707
708 TEST_CASE(websocket_handler_send_multiple_frames) {
709 (void)ctx;
710 struct tester tester;
711 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
712
713 struct send_tester sending[] = {
714 {
715 .payload = aws_byte_cursor_from_c_str("Wee Willie Winkie runs through the town."),
716 .def =
717 {
718 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
719 .fin = false,
720 },
721 },
722 {
723 .payload = aws_byte_cursor_from_c_str("Upstairs and downstairs in his nightgown."),
724 .def =
725 {
726 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
727 .fin = false,
728 },
729 },
730 {
731 .payload = aws_byte_cursor_from_c_str("Rapping at the window, crying through the lock."),
732 .def =
733 {
734 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
735 .fin = false,
736 },
737 },
738 {
739 .def =
740 {
741 .opcode = AWS_WEBSOCKET_OPCODE_PING,
742 .fin = true,
743 },
744 },
745 {
746 .payload = aws_byte_cursor_from_c_str("Are the children all in bed, for now it's eight o'clock?"),
747 .def =
748 {
749 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
750 .fin = true,
751 },
752 },
753
754 };
755
756 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
757 ASSERT_SUCCESS(s_send_frame(&tester, &sending[i]));
758 }
759
760 ASSERT_SUCCESS(s_drain_written_messages(&tester));
761
762 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
763 ASSERT_SUCCESS(s_check_written_message(&sending[i], i));
764 }
765 ASSERT_SUCCESS(s_tester_clean_up(&tester));
766 return AWS_OP_SUCCESS;
767 }
768
769 TEST_CASE(websocket_handler_send_huge_frame) {
770 (void)ctx;
771 struct tester tester;
772 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
773
774 /* transmit giant buffer with random contents */
775 struct aws_byte_buf giant_buf;
776 ASSERT_SUCCESS(aws_byte_buf_init(&giant_buf, allocator, 100000));
777 while (aws_byte_buf_write_be32(&giant_buf, (uint32_t)rand())) {
778 }
779 while (aws_byte_buf_write_u8(&giant_buf, (uint8_t)rand())) {
780 }
781
782 struct send_tester sending[] = {
783 {
784 .payload = aws_byte_cursor_from_c_str("Little frame before big one."),
785 .def =
786 {
787 .opcode = AWS_WEBSOCKET_OPCODE_BINARY,
788 .fin = false,
789 },
790 },
791 {
792 .payload = aws_byte_cursor_from_buf(&giant_buf),
793 .def =
794 {
795 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
796 .fin = false,
797 },
798 },
799 {
800 .payload = aws_byte_cursor_from_c_str("Little frame after big one."),
801 .def =
802 {
803 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
804 .fin = true,
805 },
806 },
807 };
808
809 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
810 ASSERT_SUCCESS(s_send_frame(&tester, &sending[i]));
811 }
812
813 ASSERT_SUCCESS(s_drain_written_messages(&tester));
814
815 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
816 ASSERT_SUCCESS(s_check_written_message(&sending[i], i));
817 }
818
819 /* Ensure this was actually big enough to be split across aws_io_messages */
820 ASSERT_TRUE(sending[1].on_payload_count > 1);
821
822 aws_byte_buf_clean_up(&giant_buf);
823 ASSERT_SUCCESS(s_tester_clean_up(&tester));
824 return AWS_OP_SUCCESS;
825 }
826
827 TEST_CASE(websocket_handler_send_payload_slowly) {
828 (void)ctx;
829 struct tester tester;
830 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
831
832 struct send_tester sending[] = {
833 {
834 .payload = aws_byte_cursor_from_c_str("quick A."),
835 .def =
836 {
837 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
838 .fin = false,
839 },
840 },
841 {
842 .payload = aws_byte_cursor_from_c_str("s l o o w w w l l y B."),
843 .def =
844 {
845 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
846 .fin = false,
847 },
848 .bytes_per_tick = 1,
849 },
850 {
851 .payload = aws_byte_cursor_from_c_str("quick C."),
852 .def =
853 {
854 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
855 .fin = true,
856 },
857 },
858 };
859
860 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
861 ASSERT_SUCCESS(s_send_frame(&tester, &sending[i]));
862 }
863
864 ASSERT_SUCCESS(s_drain_written_messages(&tester));
865
866 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
867 ASSERT_SUCCESS(s_check_written_message(&sending[i], i));
868 }
869
870 /* Ensure this test really did send data over multiple callbacks */
871 ASSERT_TRUE(sending[1].on_payload_count > 1);
872
873 ASSERT_SUCCESS(s_tester_clean_up(&tester));
874 return AWS_OP_SUCCESS;
875 }
876
877 TEST_CASE(websocket_handler_send_payload_with_pauses) {
878 (void)ctx;
879 struct tester tester;
880 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
881
882 struct send_tester sending = {
883 .payload = aws_byte_cursor_from_c_str("delayed B."),
884 .def =
885 {
886 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
887 .fin = true,
888 },
889 .delay_ticks = 5,
890 };
891
892 ASSERT_SUCCESS(s_send_frame(&tester, &sending));
893
894 ASSERT_SUCCESS(s_drain_written_messages(&tester));
895
896 ASSERT_SUCCESS(s_check_written_message(&sending, 0));
897
898 /* Ensure this test really did send data over multiple callbacks */
899 ASSERT_TRUE(sending.on_payload_count > 1);
900
901 ASSERT_SUCCESS(s_tester_clean_up(&tester));
902 return AWS_OP_SUCCESS;
903 }
904
905 TEST_CASE(websocket_handler_sends_nothing_after_close_frame) {
906 (void)ctx;
907 struct tester tester;
908 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
909
910 struct send_tester sending[] = {
911 {
912 .payload = aws_byte_cursor_from_c_str("Last text frame"),
913 .def =
914 {
915 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
916 .fin = true,
917 },
918 },
919 {
920 .def =
921 {
922 .opcode = AWS_WEBSOCKET_OPCODE_CLOSE,
923 .fin = true,
924 },
925 },
926 {
927 .payload = aws_byte_cursor_from_c_str("Should not be sent."),
928 .def =
929 {
930 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
931 .fin = true,
932 },
933 },
934 };
935
936 /* Ensure these frames are queued and processed later */
937 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
938
939 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
940 ASSERT_SUCCESS(s_send_frame(&tester, &sending[i]));
941 }
942
943 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
944 ASSERT_SUCCESS(s_drain_written_messages(&tester));
945
946 /* Ensure that only 1st frame and CLOSE frame were written*/
947 ASSERT_UINT_EQUALS(2, tester.num_written_frames);
948 ASSERT_SUCCESS(s_check_written_message(&sending[0], 0));
949 ASSERT_SUCCESS(s_check_written_message(&sending[1], 1));
950
951 /* Ensure no more frames written during shutdown */
952 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
953 ASSERT_SUCCESS(s_drain_written_messages(&tester));
954 ASSERT_UINT_EQUALS(2, tester.num_written_frames);
955
956 /* Ensure 3rd frame completed with error code */
957 ASSERT_UINT_EQUALS(1, sending[2].on_complete_count);
958 ASSERT_TRUE(sending[2].on_complete_error_code != AWS_ERROR_SUCCESS);
959
960 ASSERT_SUCCESS(s_tester_clean_up(&tester));
961 return AWS_OP_SUCCESS;
962 }
963
964 /* Send a frame while the handler is in every conceivable state.
965 * Ensure that the completion callback always fires. */
966 TEST_CASE(websocket_handler_send_frames_always_complete) {
967 (void)ctx;
968 struct tester tester;
969 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
970
971 enum {
972 ON_THREAD_BEFORE_CLOSE,
973 OFF_THREAD_BEFORE_CLOSE,
974 CLOSE,
975 ON_THREAD_AFTER_CLOSE,
976 OFF_THREAD_AFTER_CLOSE,
977 ON_THREAD_DURING_SHUTDOWN,
978 OFF_THREAD_DURING_SHUTDOWN,
979 ON_THREAD_AFTER_SHUTDOWN,
980 OFF_THREAD_AFTER_SHUTDOWN,
981 COUNT,
982 };
983
984 struct send_tester sending[COUNT];
985 memset(sending, 0, sizeof(sending));
986 for (int i = 0; i < COUNT; ++i) {
987 struct send_tester *send = &sending[i];
988 send->def.opcode = (i == CLOSE) ? AWS_WEBSOCKET_OPCODE_CLOSE : AWS_WEBSOCKET_OPCODE_PING;
989 send->def.fin = true;
990 }
991
992 int sending_err[AWS_ARRAY_SIZE(sending)];
993
994 /* Start sending frames */
995 sending_err[ON_THREAD_BEFORE_CLOSE] = s_send_frame_no_assert(&tester, &sending[ON_THREAD_BEFORE_CLOSE]);
996 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
997 sending_err[OFF_THREAD_BEFORE_CLOSE] = s_send_frame_no_assert(&tester, &sending[OFF_THREAD_BEFORE_CLOSE]);
998 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
999
1000 /* Send close frame */
1001 sending_err[CLOSE] = s_send_frame_no_assert(&tester, &sending[CLOSE]);
1002
1003 sending_err[ON_THREAD_AFTER_CLOSE] = s_send_frame_no_assert(&tester, &sending[ON_THREAD_AFTER_CLOSE]);
1004 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
1005 sending_err[OFF_THREAD_AFTER_CLOSE] = s_send_frame_no_assert(&tester, &sending[OFF_THREAD_AFTER_CLOSE]);
1006 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
1007
1008 /* Issue channel shutdown */
1009 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
1010
1011 sending_err[ON_THREAD_DURING_SHUTDOWN] = s_send_frame_no_assert(&tester, &sending[ON_THREAD_DURING_SHUTDOWN]);
1012 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
1013 sending_err[OFF_THREAD_DURING_SHUTDOWN] = s_send_frame_no_assert(&tester, &sending[OFF_THREAD_DURING_SHUTDOWN]);
1014 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
1015
1016 /* Wait for shutdown to complete */
1017 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1018
1019 /* Try to send even more frames */
1020 sending_err[ON_THREAD_AFTER_SHUTDOWN] = s_send_frame_no_assert(&tester, &sending[ON_THREAD_AFTER_SHUTDOWN]);
1021 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
1022 sending_err[OFF_THREAD_AFTER_SHUTDOWN] = s_send_frame_no_assert(&tester, &sending[OFF_THREAD_AFTER_SHUTDOWN]);
1023 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
1024
1025 /* Check that each send() failed immediately, or had its completion callback invoked. */
1026 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1027 for (int i = 0; i < COUNT; ++i) {
1028 if (sending_err[i] == AWS_OP_SUCCESS) {
1029 ASSERT_UINT_EQUALS(1, sending[i].on_complete_count);
1030 }
1031 }
1032
1033 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1034 return AWS_OP_SUCCESS;
1035 }
1036
1037 TEST_CASE(websocket_handler_send_one_io_msg_at_a_time) {
1038 (void)ctx;
1039 struct tester tester;
1040 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1041
1042 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("bitter butter.");
1043
1044 const size_t count = 10000;
1045 struct send_tester *sending = aws_mem_acquire(allocator, sizeof(struct send_tester) * count);
1046 ASSERT_NOT_NULL(sending);
1047 memset(sending, 0, sizeof(struct send_tester) * count);
1048
1049 for (size_t i = 0; i < count; ++i) {
1050 struct send_tester *send = &sending[i];
1051 send->payload = payload;
1052 send->def.opcode = AWS_WEBSOCKET_OPCODE_TEXT;
1053 send->def.fin = true;
1054
1055 ASSERT_SUCCESS(s_send_frame(&tester, send));
1056 }
1057
1058 /* Turn off instant write completion */
1059 testing_channel_complete_written_messages_immediately(&tester.testing_channel, false, AWS_OP_SUCCESS);
1060
1061 /* Repeatedly drain event loop and ensure that only 1 aws_io_message is written */
1062 struct aws_linked_list *io_msgs = testing_channel_get_written_message_queue(&tester.testing_channel);
1063 size_t total_io_msg_count = 0;
1064 while (true) {
1065 testing_channel_drain_queued_tasks(&tester.testing_channel);
1066 if (aws_linked_list_empty(io_msgs)) {
1067 break;
1068 }
1069
1070 total_io_msg_count++;
1071 struct aws_linked_list_node *node = aws_linked_list_pop_front(io_msgs);
1072 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
1073
1074 ASSERT_TRUE(aws_linked_list_empty(io_msgs)); /* Only 1 aws_io_message should be in the channel at a time */
1075
1076 if (msg->on_completion) {
1077 msg->on_completion(tester.testing_channel.channel, msg, AWS_ERROR_SUCCESS, msg->user_data);
1078 }
1079 aws_mem_release(msg->allocator, msg);
1080 }
1081
1082 /* Assert that every frame sent */
1083 ASSERT_UINT_EQUALS(1, sending[count - 1].on_complete_count);
1084
1085 /* Assert this test actually actually involved several aws_io_messages */
1086 ASSERT_TRUE(total_io_msg_count >= 3);
1087
1088 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1089 aws_mem_release(allocator, sending);
1090 return AWS_OP_SUCCESS;
1091 }
1092
1093 /*
1094 * Verifies that the write completion callbacks for websocket frames are not invoked immediately after relaying
1095 * towards the left end (socket) of the channel
1096 */
1097 TEST_CASE(websocket_handler_delayed_write_completion) {
1098 (void)ctx;
1099 struct tester tester;
1100 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1101
1102 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("bitter butter.");
1103
1104 const size_t count = 2;
1105 struct send_tester *sending = aws_mem_acquire(allocator, sizeof(struct send_tester) * count);
1106 ASSERT_NOT_NULL(sending);
1107 memset(sending, 0, sizeof(struct send_tester) * count);
1108
1109 for (size_t i = 0; i < count; ++i) {
1110 struct send_tester *send = &sending[i];
1111 send->payload = payload;
1112 send->def.opcode = AWS_WEBSOCKET_OPCODE_TEXT;
1113 send->def.fin = true;
1114
1115 ASSERT_SUCCESS(s_send_frame(&tester, send));
1116 }
1117
1118 /* Turn off instant write completion and run the channel */
1119 testing_channel_complete_written_messages_immediately(&tester.testing_channel, false, AWS_OP_SUCCESS);
1120 testing_channel_drain_queued_tasks(&tester.testing_channel);
1121
1122 struct aws_linked_list *io_msgs = testing_channel_get_written_message_queue(&tester.testing_channel);
1123 ASSERT_FALSE(aws_linked_list_empty(io_msgs));
1124
1125 struct aws_linked_list_node *node = aws_linked_list_pop_front(io_msgs);
1126 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
1127
1128 /* we've relayed the frames, but no frame write completions should have been invoked */
1129 for (size_t i = 0; i < count; ++i) {
1130 struct send_tester *send = &sending[i];
1131 ASSERT_UINT_EQUALS(0, send->on_complete_count);
1132 }
1133
1134 /* manually invoke the write completion on the downstream io message */
1135 msg->on_completion(tester.testing_channel.channel, msg, AWS_ERROR_SUCCESS, msg->user_data);
1136 aws_mem_release(msg->allocator, msg);
1137
1138 /* now all frame write completions should have been invoked exactly once */
1139 for (size_t i = 0; i < count; ++i) {
1140 struct send_tester *send = &sending[i];
1141 ASSERT_UINT_EQUALS(1, send->on_complete_count);
1142 }
1143
1144 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1145 aws_mem_release(allocator, sending);
1146 return AWS_OP_SUCCESS;
1147 }
1148
1149 TEST_CASE(websocket_handler_send_halts_if_payload_fn_returns_false) {
1150 (void)ctx;
1151 struct tester tester;
1152 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1153
1154 struct send_tester sending[] = {
1155 {
1156 /* Sending should halt after 1st frame sends 1byte of payload */
1157 .payload = aws_byte_cursor_from_c_str("Stop"),
1158 .fail_on_nth_payload = 1,
1159 .bytes_per_tick = 1,
1160 .def =
1161 {
1162 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1163 .fin = true,
1164 },
1165 },
1166 {
1167 .payload = aws_byte_cursor_from_c_str("Should never send"),
1168 .def =
1169 {
1170 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1171 .fin = true,
1172 },
1173 },
1174 };
1175
1176 for (size_t i = 0; i < AWS_ARRAY_SIZE(sending); ++i) {
1177 ASSERT_SUCCESS(s_send_frame(&tester, &sending[i]));
1178 }
1179
1180 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1181
1182 /* Check that frame stopped processing */
1183 ASSERT_UINT_EQUALS(1, sending[0].on_payload_count);
1184 ASSERT_UINT_EQUALS(1, sending[0].on_complete_count);
1185 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CALLBACK_FAILURE, sending[0].on_complete_error_code);
1186
1187 /* The websocket should close when a callback returns false */
1188 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1189
1190 /* Other send should have been cancelled without it payload callback ever being invoked */
1191 ASSERT_UINT_EQUALS(0, sending[1].on_payload_count);
1192 ASSERT_UINT_EQUALS(1, sending[1].on_complete_count);
1193 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, sending[1].on_complete_error_code);
1194
1195 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1196 return AWS_OP_SUCCESS;
1197 }
1198
1199 TEST_CASE(websocket_handler_shutdown_automatically_sends_close_frame) {
1200 (void)ctx;
1201 struct tester tester;
1202 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1203
1204 /* Shutdown channel normally */
1205 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
1206 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1207
1208 /* Check that CLOSE frame written */
1209 ASSERT_UINT_EQUALS(AWS_WEBSOCKET_OPCODE_CLOSE, tester.written_frames[0].def.opcode);
1210 ASSERT_TRUE(tester.written_frames[0].is_complete);
1211
1212 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1213 return AWS_OP_SUCCESS;
1214 }
1215
1216 /* Ensure that, if user had queued their own CLOSE frame before shutdown,
1217 * The user frame is the only one that gets written. */
1218 TEST_CASE(websocket_handler_shutdown_handles_queued_close_frame) {
1219 (void)ctx;
1220 struct tester tester;
1221 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1222
1223 /* Try to make it so we issue channel-shutdown while user CLOSE frame is mid-send.
1224 * We use the "payload delay" feature in the `send_tester` struct */
1225 uint8_t payload_bytes[] = {0x01, 0x02};
1226 struct send_tester send = {
1227 .payload = aws_byte_cursor_from_array(payload_bytes, sizeof(payload_bytes)),
1228 .def =
1229 {
1230 .opcode = AWS_WEBSOCKET_OPCODE_CLOSE,
1231 .fin = true,
1232 },
1233 .delay_ticks = 5,
1234 };
1235
1236 ASSERT_SUCCESS(s_send_frame(&tester, &send));
1237
1238 /* Assert that test has one aws_io_message written, containing a partially sent frame */
1239 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
1240 ASSERT_TRUE(send.on_payload_count > 0);
1241 ASSERT_UINT_EQUALS(0, send.on_complete_count);
1242
1243 /* Shutdown channel normally */
1244 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
1245 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1246 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1247
1248 /* Check that user's CLOSE frame was written, and nothing further */
1249 ASSERT_SUCCESS(s_check_written_message(&send, 0));
1250 ASSERT_UINT_EQUALS(1, tester.num_written_frames);
1251
1252 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1253 return AWS_OP_SUCCESS;
1254 }
1255
1256 TEST_CASE(websocket_handler_shutdown_immediately_in_emergency) {
1257 (void)ctx;
1258 struct tester tester;
1259 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1260
1261 /* Try to make it so we issue channel-shutdown while a frame is mid-send.
1262 * We use the "payload delay" feature in the `send_tester` struct */
1263 struct send_tester send = {
1264 .payload = aws_byte_cursor_from_c_str("delayed payload"),
1265 .def =
1266 {
1267 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1268 .fin = true,
1269 },
1270 .delay_ticks = 15,
1271 };
1272
1273 ASSERT_SUCCESS(s_send_frame(&tester, &send));
1274
1275 /* Assert that test is issuing shutdown while frame is partially written */
1276 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
1277 ASSERT_TRUE(send.on_payload_count > 0);
1278 ASSERT_UINT_EQUALS(0, send.on_complete_count);
1279
1280 /* Shutdown channel with error code, which should result in IMMEDIATE style shutdown */
1281 aws_channel_shutdown(tester.testing_channel.channel, AWS_IO_SOCKET_CLOSED);
1282 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1283
1284 /* Ensure shutdown is complete at this point*/
1285 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1286
1287 /* Frame should not have sent completely, no CLOSE frame should have been sent either */
1288 ASSERT_UINT_EQUALS(1, send.on_complete_count);
1289 ASSERT_TRUE(send.on_complete_error_code != AWS_ERROR_SUCCESS);
1290
1291 ASSERT_UINT_EQUALS(0, tester.num_written_frames);
1292
1293 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1294 return AWS_OP_SUCCESS;
1295 }
1296
1297 /* During normal shutdown, the websocket delays until a CLOSE frame can be sent.
1298 * This test checks that, if unexpected errors occur during that waiting period, shutdown doesn't hang forever */
1299 TEST_CASE(websocket_handler_shutdown_handles_unexpected_write_error) {
1300 (void)ctx;
1301 struct tester tester;
1302 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1303
1304 /* Queue a frame that delays a while, and then breaks the websocket entirely. */
1305 struct send_tester send = {
1306 .payload = aws_byte_cursor_from_c_str("bad frame"),
1307 .def =
1308 {
1309 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1310 .fin = true,
1311 },
1312 .delay_ticks = 15,
1313 .send_wrong_payload_amount = 1,
1314 };
1315
1316 ASSERT_SUCCESS(s_send_frame(&tester, &send));
1317
1318 /* Assert that test is issuing shutdown while frame is partially written */
1319 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
1320 ASSERT_TRUE(send.on_payload_count > 0);
1321 ASSERT_UINT_EQUALS(0, send.on_complete_count);
1322
1323 /* Shutdown channel normally, which should cause the websocket to queue a CLOSE frame and wait until it's sent. */
1324 aws_channel_shutdown(tester.testing_channel.channel, AWS_IO_SOCKET_CLOSED);
1325
1326 /* Wait for shutdown to complete */
1327 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1328
1329 /* Assert that test did actually experience a write error */
1330 ASSERT_UINT_EQUALS(1, send.on_complete_count);
1331 ASSERT_TRUE(send.on_complete_error_code != AWS_ERROR_SUCCESS);
1332
1333 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1334 return AWS_OP_SUCCESS;
1335 }
1336
1337 TEST_CASE(websocket_handler_close_on_thread) {
1338 (void)ctx;
1339 struct tester tester;
1340 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1341
1342 aws_websocket_close(tester.websocket, false);
1343
1344 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1345 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1346
1347 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1348 return AWS_OP_SUCCESS;
1349 }
1350
1351 TEST_CASE(websocket_handler_close_off_thread) {
1352 (void)ctx;
1353 struct tester tester;
1354 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1355
1356 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
1357 aws_websocket_close(tester.websocket, false);
1358 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
1359
1360 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1361 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1362
1363 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1364 return AWS_OP_SUCCESS;
1365 }
1366
1367 TEST_CASE(websocket_handler_read_frame) {
1368 (void)ctx;
1369 struct tester tester;
1370 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1371
1372 struct readpush_frame pushing[] = {
1373 {
1374 .payload = aws_byte_cursor_from_c_str("guten morgen"),
1375 .def =
1376 {
1377 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1378 .fin = true,
1379 },
1380 },
1381 };
1382
1383 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1384 s_do_readpush_all(&tester);
1385
1386 testing_channel_drain_queued_tasks(&tester.testing_channel);
1387
1388 for (size_t i = 0; i < AWS_ARRAY_SIZE(pushing); ++i) {
1389 ASSERT_SUCCESS(s_readpush_check(&tester, i, AWS_ERROR_SUCCESS));
1390 }
1391
1392 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1393 return AWS_OP_SUCCESS;
1394 }
1395
1396 TEST_CASE(websocket_handler_read_multiple_frames) {
1397 (void)ctx;
1398 struct tester tester;
1399 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1400
1401 struct readpush_frame pushing[] = {
1402 {
1403 .payload = aws_byte_cursor_from_c_str("Uno."),
1404 .def =
1405 {
1406 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1407 .fin = false,
1408 },
1409 },
1410 {
1411 .payload = aws_byte_cursor_from_c_str("Dos."),
1412 .def =
1413 {
1414 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
1415 .fin = false,
1416 },
1417 },
1418 {
1419 .payload = aws_byte_cursor_from_c_str("Tres."),
1420 .def =
1421 {
1422 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
1423 .fin = true,
1424 },
1425 },
1426 };
1427
1428 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1429 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1430
1431 testing_channel_drain_queued_tasks(&tester.testing_channel);
1432
1433 for (size_t i = 0; i < AWS_ARRAY_SIZE(pushing); ++i) {
1434 ASSERT_SUCCESS(s_readpush_check(&tester, i, AWS_ERROR_SUCCESS));
1435 }
1436
1437 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1438 return AWS_OP_SUCCESS;
1439 }
1440
1441 TEST_CASE(websocket_handler_read_frames_split_across_io_messages) {
1442 (void)ctx;
1443 struct tester tester;
1444 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1445
1446 struct readpush_frame pushing[] = {
1447 {
1448 .payload = aws_byte_cursor_from_c_str("As dry leaves that before the wild hurricane fly,"),
1449 .def =
1450 {
1451 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1452 .fin = false,
1453 },
1454 },
1455 {
1456 .payload = aws_byte_cursor_from_c_str("when they meet with an obstacle,"),
1457 .def =
1458 {
1459 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
1460 .fin = false,
1461 },
1462 },
1463 {
1464 .payload = aws_byte_cursor_from_c_str("mount to the sky"),
1465 .def =
1466 {
1467 .opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION,
1468 .fin = true,
1469 },
1470 },
1471 };
1472
1473 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1474
1475 /* Send 1 byte at a time to ensure we can tolerate frames split across multiple aws_io_messages */
1476 struct readpush_options options = {.message_size = 1};
1477 ASSERT_SUCCESS(s_do_readpush(&tester, options));
1478
1479 testing_channel_drain_queued_tasks(&tester.testing_channel);
1480
1481 for (size_t i = 0; i < AWS_ARRAY_SIZE(pushing); ++i) {
1482 ASSERT_SUCCESS(s_readpush_check(&tester, i, AWS_ERROR_SUCCESS));
1483 }
1484
1485 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1486 return AWS_OP_SUCCESS;
1487 }
1488
1489 TEST_CASE(websocket_handler_read_frames_complete_on_shutdown) {
1490 (void)ctx;
1491 struct tester tester;
1492 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1493
1494 struct readpush_frame pushing[] = {
1495 {
1496 .payload = aws_byte_cursor_from_c_str("This frame will not be completely sent."),
1497 .def =
1498 {
1499 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1500 .fin = true,
1501 },
1502 },
1503 };
1504
1505 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1506
1507 /* Push most, but not all, of a frame */
1508 struct readpush_options options = {
1509 .num_bytes = (size_t)(aws_websocket_frame_encoded_size(&pushing[0].def) - 1),
1510 };
1511 s_do_readpush(&tester, options);
1512
1513 /* Shut down channel */
1514 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
1515 s_drain_written_messages(&tester);
1516
1517 /* Check that completion callbacks fired */
1518 ASSERT_SUCCESS(s_readpush_check(&tester, 0, AWS_ERROR_HTTP_CONNECTION_CLOSED));
1519
1520 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1521 return AWS_OP_SUCCESS;
1522 }
1523
1524 TEST_CASE(websocket_handler_read_halts_if_begin_fn_returns_false) {
1525 (void)ctx;
1526 struct tester tester;
1527 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1528
1529 struct readpush_frame pushing[] = {
1530 {
1531 .payload = aws_byte_cursor_from_c_str("Fail on frame begin."),
1532 .def =
1533 {
1534 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1535 .fin = true,
1536 },
1537 },
1538 {
1539 .payload = aws_byte_cursor_from_c_str("This frame should never get read."),
1540 .def =
1541 {
1542 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1543 .fin = true,
1544 },
1545 },
1546 };
1547
1548 tester.fail_on_incoming_frame_begin_n = 1;
1549
1550 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1551 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1552
1553 s_drain_written_messages(&tester);
1554
1555 /* First frame should have completed immediately with an error */
1556 ASSERT_SUCCESS(s_readpush_check(&tester, 0, AWS_ERROR_HTTP_CALLBACK_FAILURE));
1557 ASSERT_UINT_EQUALS(0, tester.incoming_frames[0].on_payload_count);
1558
1559 /* No further frames should have been read */
1560 ASSERT_UINT_EQUALS(1, tester.num_incoming_frames);
1561
1562 /* Callback failure should have caused connection to close */
1563 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1564
1565 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1566 return AWS_OP_SUCCESS;
1567 }
1568
1569 TEST_CASE(websocket_handler_read_halts_if_payload_fn_returns_false) {
1570 (void)ctx;
1571 struct tester tester;
1572 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1573
1574 struct readpush_frame pushing[] = {
1575 {
1576 .payload = aws_byte_cursor_from_c_str("Fail on payload."),
1577 .def =
1578 {
1579 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1580 .fin = true,
1581 },
1582 },
1583 {
1584 .payload = aws_byte_cursor_from_c_str("This frame should never get read."),
1585 .def =
1586 {
1587 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1588 .fin = true,
1589 },
1590 },
1591 };
1592
1593 /* Return false from 1st on_payload callback. */
1594 tester.fail_on_incoming_frame_payload_n = 1;
1595
1596 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1597 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1598
1599 s_drain_written_messages(&tester);
1600
1601 /* First frame should complete with error */
1602 ASSERT_SUCCESS(s_readpush_check(&tester, 0, AWS_ERROR_HTTP_CALLBACK_FAILURE));
1603 ASSERT_UINT_EQUALS(1, tester.incoming_frames[0].on_payload_count);
1604
1605 /* No further frames should have been read */
1606 ASSERT_UINT_EQUALS(1, tester.num_incoming_frames);
1607
1608 /* Callback failure should have caused connection to close */
1609 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1610
1611 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1612 return AWS_OP_SUCCESS;
1613 }
1614
1615 TEST_CASE(websocket_handler_read_halts_if_complete_fn_returns_false) {
1616 (void)ctx;
1617 struct tester tester;
1618 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1619
1620 struct readpush_frame pushing[] = {
1621 {
1622 .payload = aws_byte_cursor_from_c_str("Fail on completion."),
1623 .def =
1624 {
1625 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1626 .fin = true,
1627 },
1628 },
1629 {
1630 .payload = aws_byte_cursor_from_c_str("This frame should never get read."),
1631 .def =
1632 {
1633 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1634 .fin = true,
1635 },
1636 },
1637 };
1638
1639 /* Return false when 1st frame's on_complete callback */
1640 tester.fail_on_incoming_frame_complete_n = 1;
1641
1642 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1643 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1644
1645 s_drain_written_messages(&tester);
1646
1647 /* First frame should have succeeded */
1648 ASSERT_SUCCESS(s_readpush_check(&tester, 0, AWS_ERROR_SUCCESS));
1649
1650 /* No further frames should have been read */
1651 ASSERT_UINT_EQUALS(1, tester.num_incoming_frames);
1652
1653 /* Callback failure should have caused connection to close */
1654 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1655
1656 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1657 return AWS_OP_SUCCESS;
1658 }
1659
1660 static int s_window_manual_increment_common(struct aws_allocator *allocator, bool on_thread) {
1661 struct tester tester;
1662 s_tester_options.manual_window_update = true;
1663 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1664
1665 /* Push "data" frame to websocket */
1666 struct readpush_frame pushing = {
1667 .payload = aws_byte_cursor_from_c_str("Shrink, then open"),
1668 .def =
1669 {
1670 .opcode = AWS_WEBSOCKET_OPCODE_TEXT,
1671 .fin = true,
1672 },
1673 };
1674
1675 s_set_readpush_frames(&tester, &pushing, 1);
1676 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1677 testing_channel_drain_queued_tasks(&tester.testing_channel);
1678
1679 /* Assert that window did not fully re-open*/
1680 uint64_t frame_minus_payload_size = aws_websocket_frame_encoded_size(&pushing.def) - pushing.def.payload_length;
1681
1682 ASSERT_UINT_EQUALS(frame_minus_payload_size, testing_channel_last_window_update(&tester.testing_channel));
1683
1684 /* Manually increment window */
1685 testing_channel_set_is_on_users_thread(&tester.testing_channel, on_thread);
1686 aws_websocket_increment_read_window(tester.websocket, (size_t)pushing.def.payload_length);
1687
1688 /* Assert it re-opened that much */
1689 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
1690 testing_channel_drain_queued_tasks(&tester.testing_channel);
1691 ASSERT_UINT_EQUALS(pushing.def.payload_length, testing_channel_last_window_update(&tester.testing_channel));
1692
1693 /* Now push a control frame, and ensure the window automatically re-opens by the whole amount */
1694 struct readpush_frame pushing_control_frame = {
1695 .payload = aws_byte_cursor_from_c_str("free data"),
1696 .def = {.opcode = AWS_WEBSOCKET_OPCODE_PONG, .fin = true},
1697 };
1698
1699 s_set_readpush_frames(&tester, &pushing_control_frame, 1);
1700 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1701 testing_channel_drain_queued_tasks(&tester.testing_channel);
1702
1703 ASSERT_UINT_EQUALS(
1704 aws_websocket_frame_encoded_size(&pushing_control_frame.def),
1705 testing_channel_last_window_update(&tester.testing_channel));
1706
1707 /* Done */
1708 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1709 return AWS_OP_SUCCESS;
1710 }
1711
1712 TEST_CASE(websocket_handler_window_manual_increment) {
1713 (void)ctx;
1714 return s_window_manual_increment_common(allocator, true);
1715 }
1716
1717 TEST_CASE(websocket_handler_window_manual_increment_off_thread) {
1718 (void)ctx;
1719 return s_window_manual_increment_common(allocator, false);
1720 }
1721
1722 TEST_CASE(websocket_midchannel_sanity_check) {
1723 (void)ctx;
1724 struct tester tester;
1725 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1726 ASSERT_SUCCESS(s_install_downstream_handler(&tester, s_default_initial_window_size));
1727 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1728 return AWS_OP_SUCCESS;
1729 }
1730
1731 TEST_CASE(websocket_midchannel_write_message) {
1732 (void)ctx;
1733 struct tester tester;
1734 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1735 ASSERT_SUCCESS(s_install_downstream_handler(&tester, s_default_initial_window_size));
1736
1737 /* Write data */
1738 struct aws_byte_cursor writing = aws_byte_cursor_from_c_str("My hat it has three corners");
1739 ASSERT_SUCCESS(s_writepush(&tester, writing));
1740
1741 /* Compare results */
1742 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1743 ASSERT_SUCCESS(s_writepush_check(&tester, 0));
1744
1745 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1746 return AWS_OP_SUCCESS;
1747 }
1748
1749 TEST_CASE(websocket_midchannel_write_multiple_messages) {
1750 (void)ctx;
1751 struct tester tester;
1752 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1753 ASSERT_SUCCESS(s_install_downstream_handler(&tester, s_default_initial_window_size));
1754
1755 struct aws_byte_cursor writing[] = {
1756 aws_byte_cursor_from_c_str("My hat it has three corners."),
1757 aws_byte_cursor_from_c_str("Three corners has my hat."),
1758 aws_byte_cursor_from_c_str("And had it not three corners, it would not be my hat."),
1759 };
1760
1761 /* Write data */
1762 for (size_t i = 0; i < AWS_ARRAY_SIZE(writing); ++i) {
1763 ASSERT_SUCCESS(s_writepush(&tester, writing[i]));
1764 }
1765
1766 /* Compare results */
1767 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1768 ASSERT_SUCCESS(s_writepush_check(&tester, 0));
1769
1770 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1771 return AWS_OP_SUCCESS;
1772 }
1773
1774 TEST_CASE(websocket_midchannel_write_huge_message) {
1775 (void)ctx;
1776 struct tester tester;
1777 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1778 ASSERT_SUCCESS(s_install_downstream_handler(&tester, s_default_initial_window_size));
1779
1780 /* Fill big buffer with random data */
1781 struct aws_byte_buf writing;
1782 ASSERT_SUCCESS(aws_byte_buf_init(&writing, allocator, 1000000));
1783 while (aws_byte_buf_write_be32(&writing, (uint32_t)rand())) {
1784 }
1785 while (aws_byte_buf_write_u8(&writing, (uint8_t)rand())) {
1786 }
1787
1788 /* Send as multiple aws_io_messages that are as full as they can be */
1789 ASSERT_SUCCESS(s_writepush(&tester, aws_byte_cursor_from_buf(&writing)));
1790
1791 /* Compare results */
1792 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1793 ASSERT_TRUE(tester.num_written_io_messages > 1); /* Assert that message was huge enough to stress limits */
1794 ASSERT_SUCCESS(s_writepush_check(&tester, 0));
1795
1796 aws_byte_buf_clean_up(&writing);
1797 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1798 return AWS_OP_SUCCESS;
1799 }
1800
1801 TEST_CASE(websocket_handler_sends_pong_automatically) {
1802 (void)ctx;
1803 struct tester tester;
1804 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1805
1806 /* Read PING with a payload */
1807 struct readpush_frame read_ping_with_payload = {
1808 .def =
1809 {
1810 .opcode = AWS_WEBSOCKET_OPCODE_PING,
1811 .fin = true,
1812 },
1813 .payload = aws_byte_cursor_from_c_str("echo me pls"),
1814 };
1815 s_set_readpush_frames(&tester, &read_ping_with_payload, 1);
1816 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1817
1818 /* Check that PONG is automatically written, with payload echoing the PING */
1819 s_drain_written_messages(&tester);
1820 const struct written_frame *written_frame = &tester.written_frames[0];
1821 ASSERT_UINT_EQUALS(AWS_WEBSOCKET_OPCODE_PONG, written_frame->def.opcode);
1822 ASSERT_TRUE(written_frame->is_complete);
1823 ASSERT_BIN_ARRAYS_EQUALS(
1824 read_ping_with_payload.payload.ptr,
1825 read_ping_with_payload.payload.len,
1826 written_frame->payload.buffer,
1827 written_frame->payload.len);
1828
1829 /* Read PING without empty payload */
1830 struct readpush_frame read_ping_with_empty_payload = {
1831 .def =
1832 {
1833 .opcode = AWS_WEBSOCKET_OPCODE_PING,
1834 .fin = true,
1835 },
1836 };
1837 s_set_readpush_frames(&tester, &read_ping_with_empty_payload, 1);
1838 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1839
1840 /* Check that PONG with empty payload is automatically written */
1841 s_drain_written_messages(&tester);
1842 written_frame = &tester.written_frames[1];
1843 ASSERT_UINT_EQUALS(AWS_WEBSOCKET_OPCODE_PONG, written_frame->def.opcode);
1844 ASSERT_TRUE(written_frame->is_complete);
1845 ASSERT_UINT_EQUALS(0, written_frame->payload.len);
1846
1847 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1848 return AWS_OP_SUCCESS;
1849 }
1850
1851 TEST_CASE(websocket_handler_wont_send_pong_after_close_frame) {
1852 (void)ctx;
1853 struct tester tester;
1854 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1855
1856 /* Send a CLOSE frame */
1857 struct send_tester send_close = {
1858 .def =
1859 {
1860 .opcode = AWS_WEBSOCKET_OPCODE_CLOSE,
1861 .fin = true,
1862 },
1863 };
1864 ASSERT_SUCCESS(s_send_frame(&tester, &send_close));
1865
1866 /* Now have the websocket read a PING */
1867 struct readpush_frame read_ping = {
1868 .def =
1869 {
1870 .opcode = AWS_WEBSOCKET_OPCODE_PING,
1871 .fin = true,
1872 },
1873 };
1874 s_set_readpush_frames(&tester, &read_ping, 1);
1875 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1876
1877 /* Check that PONG is NOT sent automatically, because a CLOSE was sent before it */
1878 s_drain_written_messages(&tester);
1879 ASSERT_TRUE(tester.num_written_frames == 1);
1880 ASSERT_INT_EQUALS(AWS_WEBSOCKET_OPCODE_CLOSE, tester.written_frames[0].def.opcode);
1881
1882 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1883 return AWS_OP_SUCCESS;
1884 }
1885
1886 /* This is a regression test. If aws_websocket_close() leads to shutdown,
1887 * then subsequent calls to aws_websocket_send_frame() should fail. */
1888 TEST_CASE(websocket_handler_send_frame_fails_if_websocket_closed) {
1889 (void)ctx;
1890 (void)ctx;
1891 struct tester tester;
1892 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1893
1894 /* Call aws_websocket_close() and wait for shutdown to complete */
1895 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
1896 aws_websocket_close(tester.websocket, false);
1897 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
1898
1899 ASSERT_SUCCESS(s_drain_written_messages(&tester));
1900 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
1901
1902 /* aws_websocket_send_frame() should fail */
1903 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("bitter butter.");
1904 struct send_tester send = {
1905 .payload = payload,
1906 .def =
1907 {
1908 .opcode = AWS_WEBSOCKET_OPCODE_PING,
1909 .fin = true,
1910 },
1911 };
1912 ASSERT_FAILS(s_send_frame(&tester, &send));
1913 ASSERT_UINT_EQUALS(AWS_ERROR_HTTP_WEBSOCKET_CLOSE_FRAME_SENT, aws_last_error());
1914 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1915 return AWS_OP_SUCCESS;
1916 }
1917
1918 TEST_CASE(websocket_midchannel_read_message) {
1919 (void)ctx;
1920 struct tester tester;
1921 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1922 ASSERT_SUCCESS(s_install_downstream_handler(&tester, s_default_initial_window_size));
1923
1924 struct readpush_frame pushing = {
1925 .payload = aws_byte_cursor_from_c_str("Hello hello can you hear me Joe?"),
1926 .def = {.opcode = AWS_WEBSOCKET_OPCODE_BINARY, .fin = true},
1927 };
1928
1929 s_set_readpush_frames(&tester, &pushing, 1);
1930 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1931 testing_channel_drain_queued_tasks(&tester.testing_channel);
1932
1933 ASSERT_SUCCESS(s_readpush_midchannel_check(&tester, 0));
1934
1935 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1936 return AWS_OP_SUCCESS;
1937 }
1938
1939 TEST_CASE(websocket_midchannel_read_multiple_messages) {
1940 (void)ctx;
1941 struct tester tester;
1942 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1943 ASSERT_SUCCESS(s_install_downstream_handler(&tester, s_default_initial_window_size));
1944
1945 /* Read a mix of different frame types, most of which shouldn't get passed along to next handler. */
1946 struct readpush_frame pushing[] = {
1947 {
1948 .payload = aws_byte_cursor_from_c_str("Message 1."),
1949 .def = {.opcode = AWS_WEBSOCKET_OPCODE_BINARY, .fin = true},
1950 },
1951 {
1952 .payload = aws_byte_cursor_from_c_str("Ignore ping frame"),
1953 .def = {.opcode = AWS_WEBSOCKET_OPCODE_PING, .fin = true},
1954 },
1955 {
1956 .payload = aws_byte_cursor_from_c_str("Ignore text frame"),
1957 .def = {.opcode = AWS_WEBSOCKET_OPCODE_TEXT, .fin = false},
1958 },
1959 {
1960 .payload = aws_byte_cursor_from_c_str("Ignore continuation of text frame"),
1961 .def = {.opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION, .fin = true},
1962 },
1963 {
1964 .payload = aws_byte_cursor_from_c_str("Message 2 fragment 1/3."),
1965 .def = {.opcode = AWS_WEBSOCKET_OPCODE_BINARY, .fin = false},
1966 },
1967 {
1968 .payload = aws_byte_cursor_from_c_str("Message 2 fragment 2/3"),
1969 .def = {.opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION, .fin = false},
1970 },
1971 {
1972 .payload = aws_byte_cursor_from_c_str("Ignore ping frame"),
1973 .def = {.opcode = AWS_WEBSOCKET_OPCODE_PING, .fin = true},
1974 },
1975 {
1976 .payload = aws_byte_cursor_from_c_str("Message 2 fragment 3/3."),
1977 .def = {.opcode = AWS_WEBSOCKET_OPCODE_CONTINUATION, .fin = true},
1978 },
1979 };
1980
1981 s_set_readpush_frames(&tester, pushing, AWS_ARRAY_SIZE(pushing));
1982 ASSERT_SUCCESS(s_do_readpush_all(&tester));
1983 testing_channel_drain_queued_tasks(&tester.testing_channel);
1984
1985 /* Check that only BINARY (and continuation of BINARY) frames passed through */
1986 ASSERT_SUCCESS(s_readpush_midchannel_check(&tester, 0));
1987 ASSERT_SUCCESS(s_readpush_midchannel_check(&tester, 4));
1988 ASSERT_SUCCESS(s_readpush_midchannel_check(&tester, 5));
1989 ASSERT_SUCCESS(s_readpush_midchannel_check(&tester, 7));
1990
1991 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1992 return AWS_OP_SUCCESS;
1993 }