Debian Codegraph @

crt/aws-c-mqtt/tests/v3/packet_framing_tests.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/testing/aws_test_harness.h>
7
8 #include <aws/mqtt/private/fixed_header.h>
9 #include <aws/mqtt/private/mqtt311_decoder.h>
10 #include <aws/mqtt/private/packets.h>
11
12 struct mqtt_311_decoding_test_context {
13 struct aws_allocator *allocator;
14 struct aws_mqtt311_decoder decoder;
15
16 void *expected_packet;
17 size_t packet_count[16];
18 };
19
20 static int s_compare_fixed_header(
21 struct aws_mqtt_fixed_header *expected_header,
22 struct aws_mqtt_fixed_header *actual_header) {
23 ASSERT_INT_EQUALS(expected_header->packet_type, actual_header->packet_type);
24 ASSERT_INT_EQUALS(expected_header->remaining_length, actual_header->remaining_length);
25 ASSERT_INT_EQUALS(expected_header->flags, actual_header->flags);
26
27 return AWS_OP_SUCCESS;
28 }
29
30 static int s_decoding_test_handle_publish(struct aws_byte_cursor message_cursor, void *user_data) {
31
32 struct mqtt_311_decoding_test_context *context = user_data;
33 (void)context;
34
35 struct aws_mqtt_packet_publish publish;
36 if (aws_mqtt_packet_publish_decode(&message_cursor, &publish)) {
37 return AWS_OP_ERR;
38 }
39
40 struct aws_mqtt_packet_publish *expected_publish = context->expected_packet;
41
42 ASSERT_SUCCESS(s_compare_fixed_header(&expected_publish->fixed_header, &publish.fixed_header));
43
44 ASSERT_INT_EQUALS(expected_publish->packet_identifier, publish.packet_identifier);
45
46 ASSERT_BIN_ARRAYS_EQUALS(
47 expected_publish->topic_name.ptr,
48 expected_publish->topic_name.len,
49 publish.topic_name.ptr,
50 publish.topic_name.len);
51 ASSERT_BIN_ARRAYS_EQUALS(
52 expected_publish->payload.ptr, expected_publish->payload.len, publish.payload.ptr, publish.payload.len);
53
54 ++context->packet_count[AWS_MQTT_PACKET_PUBLISH];
55
56 return AWS_OP_SUCCESS;
57 }
58
59 static int s_decoding_test_handle_suback(struct aws_byte_cursor message_cursor, void *user_data) {
60
61 struct mqtt_311_decoding_test_context *context = user_data;
62 (void)context;
63
64 struct aws_mqtt_packet_suback suback;
65 if (aws_mqtt_packet_suback_init(&suback, context->allocator, 0 /* fake packet_id */)) {
66 return AWS_OP_ERR;
67 }
68
69 int result = AWS_OP_ERR;
70 if (aws_mqtt_packet_suback_decode(&message_cursor, &suback)) {
71 goto done;
72 }
73
74 struct aws_mqtt_packet_suback *expected_suback = context->expected_packet;
75
76 ASSERT_INT_EQUALS(expected_suback->packet_identifier, suback.packet_identifier);
77
78 size_t expected_ack_count = aws_array_list_length(&expected_suback->return_codes);
79 size_t actual_ack_count = aws_array_list_length(&suback.return_codes);
80 ASSERT_INT_EQUALS(expected_ack_count, actual_ack_count);
81
82 for (size_t i = 0; i < expected_ack_count; ++i) {
83 uint8_t expected_return_code = 0;
84 aws_array_list_get_at(&expected_suback->return_codes, &expected_return_code, i);
85
86 uint8_t actual_return_code = 0;
87 aws_array_list_get_at(&suback.return_codes, &actual_return_code, i);
88
89 ASSERT_INT_EQUALS(expected_return_code, actual_return_code);
90 }
91
92 ++context->packet_count[AWS_MQTT_PACKET_SUBACK];
93
94 result = AWS_OP_SUCCESS;
95
96 done:
97
98 aws_mqtt_packet_suback_clean_up(&suback);
99
100 return result;
101 }
102
103 static int s_decoding_test_handle_unsuback(struct aws_byte_cursor message_cursor, void *user_data) {
104
105 struct mqtt_311_decoding_test_context *context = user_data;
106 (void)context;
107
108 struct aws_mqtt_packet_ack unsuback;
109 if (aws_mqtt_packet_unsuback_init(&unsuback, 0 /* fake packet_id */)) {
110 return AWS_OP_ERR;
111 }
112
113 if (aws_mqtt_packet_ack_decode(&message_cursor, &unsuback)) {
114 return AWS_OP_ERR;
115 }
116
117 struct aws_mqtt_packet_ack *expected_unsuback = context->expected_packet;
118
119 ASSERT_INT_EQUALS(expected_unsuback->packet_identifier, unsuback.packet_identifier);
120
121 ++context->packet_count[AWS_MQTT_PACKET_UNSUBACK];
122
123 return AWS_OP_SUCCESS;
124 }
125
126 static int s_decoding_test_handle_puback(struct aws_byte_cursor message_cursor, void *user_data) {
127
128 struct mqtt_311_decoding_test_context *context = user_data;
129 (void)context;
130
131 struct aws_mqtt_packet_ack puback;
132 if (aws_mqtt_packet_puback_init(&puback, 0 /* fake packet_id */)) {
133 return AWS_OP_ERR;
134 }
135
136 if (aws_mqtt_packet_ack_decode(&message_cursor, &puback)) {
137 return AWS_OP_ERR;
138 }
139
140 struct aws_mqtt_packet_ack *expected_puback = context->expected_packet;
141
142 ASSERT_INT_EQUALS(expected_puback->packet_identifier, puback.packet_identifier);
143
144 ++context->packet_count[AWS_MQTT_PACKET_PUBACK];
145
146 return AWS_OP_SUCCESS;
147 }
148
149 static int s_decoding_test_handle_pingresp(struct aws_byte_cursor message_cursor, void *user_data) {
150
151 struct mqtt_311_decoding_test_context *context = user_data;
152 (void)context;
153
154 struct aws_mqtt_packet_connection pingresp;
155 if (aws_mqtt_packet_pingresp_init(&pingresp)) {
156 return AWS_OP_ERR;
157 }
158
159 if (aws_mqtt_packet_connection_decode(&message_cursor, &pingresp)) {
160 return AWS_OP_ERR;
161 }
162
163 ++context->packet_count[AWS_MQTT_PACKET_PINGRESP];
164
165 return AWS_OP_SUCCESS;
166 }
167
168 static int s_decoding_test_handle_connack(struct aws_byte_cursor message_cursor, void *user_data) {
169
170 struct mqtt_311_decoding_test_context *context = user_data;
171 (void)context;
172
173 struct aws_mqtt_packet_connack connack;
174 if (aws_mqtt_packet_connack_init(&connack, false, 0)) {
175 return AWS_OP_ERR;
176 }
177
178 if (aws_mqtt_packet_connack_decode(&message_cursor, &connack)) {
179 return AWS_OP_ERR;
180 }
181
182 struct aws_mqtt_packet_connack *expected_connack = context->expected_packet;
183
184 ASSERT_INT_EQUALS(expected_connack->session_present, connack.session_present);
185 ASSERT_INT_EQUALS(expected_connack->connect_return_code, connack.connect_return_code);
186
187 ++context->packet_count[AWS_MQTT_PACKET_CONNACK];
188
189 return AWS_OP_SUCCESS;
190 }
191
192 static struct aws_mqtt_client_connection_packet_handlers s_decoding_test_packet_handlers = {
193 .handlers_by_packet_type = {
194 [AWS_MQTT_PACKET_PUBLISH] = &s_decoding_test_handle_publish,
195 [AWS_MQTT_PACKET_SUBACK] = &s_decoding_test_handle_suback,
196 [AWS_MQTT_PACKET_UNSUBACK] = &s_decoding_test_handle_unsuback,
197 [AWS_MQTT_PACKET_PUBACK] = &s_decoding_test_handle_puback,
198 [AWS_MQTT_PACKET_PINGRESP] = &s_decoding_test_handle_pingresp,
199 [AWS_MQTT_PACKET_CONNACK] = &s_decoding_test_handle_connack,
200 }};
201
202 static void s_init_decoding_test_context(
203 struct mqtt_311_decoding_test_context *context,
204 struct aws_allocator *allocator) {
205 AWS_ZERO_STRUCT(*context);
206
207 context->allocator = allocator;
208
209 struct aws_mqtt311_decoder_options config = {
210 .packet_handlers = &s_decoding_test_packet_handlers,
211 .handler_user_data = context,
212 };
213
214 aws_mqtt311_decoder_init(&context->decoder, allocator, &config);
215 }
216
217 static void s_clean_up_decoding_test_context(struct mqtt_311_decoding_test_context *context) {
218
219 aws_mqtt311_decoder_clean_up(&context->decoder);
220 }
221
222 #define TEST_ADJACENT_PACKET_COUNT 4
223
224 static int s_mqtt_frame_and_decode_publish_fn(struct aws_allocator *allocator, void *ctx) {
225 (void)ctx;
226
227 aws_mqtt_library_init(allocator);
228
229 /*
230 * For completeness, run the test with payload sizes that lead to a remaining length VLI encoding of 1, 2, 3, and
231 * 4 bytes.
232 */
233 size_t payload_sizes[] = {35, 1234, 1 << 16, 1 << 21};
234
235 for (size_t i = 0; i < AWS_ARRAY_SIZE(payload_sizes); ++i) {
236 struct mqtt_311_decoding_test_context test_context;
237 s_init_decoding_test_context(&test_context, allocator);
238
239 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
240
241 size_t publish_payload_size = payload_sizes[i];
242
243 /* Intentionally don't initialize so we have lots of garbage */
244 uint8_t *raw_payload = aws_mem_acquire(allocator, publish_payload_size);
245
246 struct aws_mqtt_packet_publish publish_packet;
247 ASSERT_SUCCESS(aws_mqtt_packet_publish_init(
248 &publish_packet,
249 true,
250 AWS_MQTT_QOS_AT_LEAST_ONCE,
251 false,
252 aws_byte_cursor_from_c_str("Hello/World"),
253 12,
254 aws_byte_cursor_from_array(raw_payload, publish_payload_size)));
255
256 test_context.expected_packet = &publish_packet;
257
258 struct aws_byte_buf encoded_buffer;
259 aws_byte_buf_init(&encoded_buffer, allocator, (publish_payload_size + 100) * TEST_ADJACENT_PACKET_COUNT);
260
261 for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
262 ASSERT_SUCCESS(aws_mqtt_packet_publish_encode(&encoded_buffer, &publish_packet));
263 }
264
265 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23, 37, 67, 131};
266
267 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
268 size_t fragment_length = fragment_lengths[j];
269
270 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
271 while (packet_cursor.len > 0) {
272 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
273 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
274
275 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
276 }
277 }
278
279 ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_PUBLISH]);
280
281 aws_byte_buf_clean_up(&encoded_buffer);
282 aws_mem_release(allocator, raw_payload);
283
284 s_clean_up_decoding_test_context(&test_context);
285 }
286
287 aws_mqtt_library_clean_up();
288
289 return AWS_OP_SUCCESS;
290 }
291
292 AWS_TEST_CASE(mqtt_frame_and_decode_publish, s_mqtt_frame_and_decode_publish_fn)
293
294 static int s_mqtt_frame_and_decode_suback_fn(struct aws_allocator *allocator, void *ctx) {
295 (void)ctx;
296
297 aws_mqtt_library_init(allocator);
298
299 struct mqtt_311_decoding_test_context test_context;
300 s_init_decoding_test_context(&test_context, allocator);
301
302 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
303
304 struct aws_mqtt_packet_suback suback_packet;
305 ASSERT_SUCCESS(aws_mqtt_packet_suback_init(&suback_packet, allocator, 1234));
306
307 uint8_t sample_return_codes[] = {0x00, 0x01, 0x02, 0x80, 0x01};
308 for (size_t i = 0; i < AWS_ARRAY_SIZE(sample_return_codes); ++i) {
309 aws_mqtt_packet_suback_add_return_code(&suback_packet, sample_return_codes[i]);
310 }
311
312 test_context.expected_packet = &suback_packet;
313
314 struct aws_byte_buf encoded_buffer;
315 aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);
316
317 for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
318 ASSERT_SUCCESS(aws_mqtt_packet_suback_encode(&encoded_buffer, &suback_packet));
319 }
320
321 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23, 37, 67, 143};
322
323 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
324 size_t fragment_length = fragment_lengths[j];
325
326 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
327 while (packet_cursor.len > 0) {
328 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
329 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
330
331 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
332 }
333 }
334
335 ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_SUBACK]);
336
337 aws_mqtt_packet_suback_clean_up(&suback_packet);
338 aws_byte_buf_clean_up(&encoded_buffer);
339
340 s_clean_up_decoding_test_context(&test_context);
341
342 aws_mqtt_library_clean_up();
343
344 return AWS_OP_SUCCESS;
345 }
346
347 AWS_TEST_CASE(mqtt_frame_and_decode_suback, s_mqtt_frame_and_decode_suback_fn)
348
349 static int s_mqtt_frame_and_decode_unsuback_fn(struct aws_allocator *allocator, void *ctx) {
350 (void)ctx;
351
352 aws_mqtt_library_init(allocator);
353
354 struct mqtt_311_decoding_test_context test_context;
355 s_init_decoding_test_context(&test_context, allocator);
356
357 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
358
359 struct aws_mqtt_packet_ack unsuback_packet;
360 ASSERT_SUCCESS(aws_mqtt_packet_unsuback_init(&unsuback_packet, 1234));
361
362 test_context.expected_packet = &unsuback_packet;
363
364 struct aws_byte_buf encoded_buffer;
365 aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);
366
367 for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
368 ASSERT_SUCCESS(aws_mqtt_packet_ack_encode(&encoded_buffer, &unsuback_packet));
369 }
370
371 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};
372
373 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
374 size_t fragment_length = fragment_lengths[j];
375
376 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
377 while (packet_cursor.len > 0) {
378 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
379 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
380
381 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
382 }
383 }
384
385 ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_UNSUBACK]);
386
387 aws_byte_buf_clean_up(&encoded_buffer);
388
389 s_clean_up_decoding_test_context(&test_context);
390
391 aws_mqtt_library_clean_up();
392
393 return AWS_OP_SUCCESS;
394 }
395
396 AWS_TEST_CASE(mqtt_frame_and_decode_unsuback, s_mqtt_frame_and_decode_unsuback_fn)
397
398 static int s_mqtt_frame_and_decode_puback_fn(struct aws_allocator *allocator, void *ctx) {
399 (void)ctx;
400
401 aws_mqtt_library_init(allocator);
402
403 struct mqtt_311_decoding_test_context test_context;
404 s_init_decoding_test_context(&test_context, allocator);
405
406 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
407
408 struct aws_mqtt_packet_ack puback_packet;
409 ASSERT_SUCCESS(aws_mqtt_packet_puback_init(&puback_packet, 1234));
410
411 test_context.expected_packet = &puback_packet;
412
413 struct aws_byte_buf encoded_buffer;
414 aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);
415
416 for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
417 ASSERT_SUCCESS(aws_mqtt_packet_ack_encode(&encoded_buffer, &puback_packet));
418 }
419
420 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};
421
422 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
423 size_t fragment_length = fragment_lengths[j];
424
425 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
426 while (packet_cursor.len > 0) {
427 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
428 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
429
430 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
431 }
432 }
433
434 ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_PUBACK]);
435
436 aws_byte_buf_clean_up(&encoded_buffer);
437
438 s_clean_up_decoding_test_context(&test_context);
439
440 aws_mqtt_library_clean_up();
441
442 return AWS_OP_SUCCESS;
443 }
444
445 AWS_TEST_CASE(mqtt_frame_and_decode_puback, s_mqtt_frame_and_decode_puback_fn)
446
447 static int s_mqtt_frame_and_decode_pingresp_fn(struct aws_allocator *allocator, void *ctx) {
448 (void)ctx;
449
450 aws_mqtt_library_init(allocator);
451
452 struct mqtt_311_decoding_test_context test_context;
453 s_init_decoding_test_context(&test_context, allocator);
454
455 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
456
457 struct aws_mqtt_packet_connection pingresp_packet;
458 ASSERT_SUCCESS(aws_mqtt_packet_pingresp_init(&pingresp_packet));
459
460 test_context.expected_packet = &pingresp_packet;
461
462 struct aws_byte_buf encoded_buffer;
463 aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);
464
465 for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
466 ASSERT_SUCCESS(aws_mqtt_packet_connection_encode(&encoded_buffer, &pingresp_packet));
467 }
468
469 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11};
470
471 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
472 size_t fragment_length = fragment_lengths[j];
473
474 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
475 while (packet_cursor.len > 0) {
476 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
477 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
478
479 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
480 }
481 }
482
483 ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_PINGRESP]);
484
485 aws_byte_buf_clean_up(&encoded_buffer);
486
487 s_clean_up_decoding_test_context(&test_context);
488
489 aws_mqtt_library_clean_up();
490
491 return AWS_OP_SUCCESS;
492 }
493
494 AWS_TEST_CASE(mqtt_frame_and_decode_pingresp, s_mqtt_frame_and_decode_pingresp_fn)
495
496 static int s_mqtt_frame_and_decode_connack_fn(struct aws_allocator *allocator, void *ctx) {
497 (void)ctx;
498
499 aws_mqtt_library_init(allocator);
500
501 struct mqtt_311_decoding_test_context test_context;
502 s_init_decoding_test_context(&test_context, allocator);
503
504 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
505
506 struct aws_mqtt_packet_connack connack_packet;
507 ASSERT_SUCCESS(aws_mqtt_packet_connack_init(&connack_packet, true, AWS_MQTT_CONNECT_NOT_AUTHORIZED));
508
509 test_context.expected_packet = &connack_packet;
510
511 struct aws_byte_buf encoded_buffer;
512 aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);
513
514 for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
515 ASSERT_SUCCESS(aws_mqtt_packet_connack_encode(&encoded_buffer, &connack_packet));
516 }
517
518 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};
519
520 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
521 size_t fragment_length = fragment_lengths[j];
522
523 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
524 while (packet_cursor.len > 0) {
525 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
526 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
527
528 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
529 }
530 }
531
532 ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_CONNACK]);
533
534 aws_byte_buf_clean_up(&encoded_buffer);
535
536 s_clean_up_decoding_test_context(&test_context);
537
538 aws_mqtt_library_clean_up();
539
540 return AWS_OP_SUCCESS;
541 }
542
543 AWS_TEST_CASE(mqtt_frame_and_decode_connack, s_mqtt_frame_and_decode_connack_fn)
544
545 static int s_mqtt_frame_and_decode_bad_remaining_length_fn(struct aws_allocator *allocator, void *ctx) {
546 (void)ctx;
547
548 aws_mqtt_library_init(allocator);
549
550 struct mqtt_311_decoding_test_context test_context;
551 s_init_decoding_test_context(&test_context, allocator);
552
553 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
554
555 /* QoS 0 Publish "Packet" data where the remaining length vli-encoding is illegal */
556 uint8_t bad_packet_data[] = {0x30, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00};
557
558 size_t fragment_lengths[] = {1, 2, 3, 5, 13};
559
560 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
561 aws_mqtt311_decoder_reset_for_new_connection(decoder);
562
563 size_t fragment_length = fragment_lengths[j];
564 struct aws_byte_cursor packet_cursor =
565 aws_byte_cursor_from_array(bad_packet_data, AWS_ARRAY_SIZE(bad_packet_data));
566 while (packet_cursor.len > 0) {
567 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
568 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
569
570 /* If this or a previous call contains the final 0x80 of the invalid vli encoding, then decode must fail */
571 bool should_fail = (fragment_cursor.ptr + fragment_cursor.len) - bad_packet_data > 4;
572 if (should_fail) {
573 ASSERT_FAILS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
574 } else {
575 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
576 }
577 }
578 }
579
580 s_clean_up_decoding_test_context(&test_context);
581
582 aws_mqtt_library_clean_up();
583
584 return AWS_OP_SUCCESS;
585 }
586
587 AWS_TEST_CASE(mqtt_frame_and_decode_bad_remaining_length, s_mqtt_frame_and_decode_bad_remaining_length_fn)
588
589 static int s_mqtt_frame_and_decode_unsupported_packet_type_fn(struct aws_allocator *allocator, void *ctx) {
590 (void)ctx;
591
592 aws_mqtt_library_init(allocator);
593
594 struct mqtt_311_decoding_test_context test_context;
595 s_init_decoding_test_context(&test_context, allocator);
596
597 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
598
599 /* Pingreq packet, no handler installed */
600 uint8_t pingreq_packet_data[] = {192, 0};
601
602 size_t fragment_lengths[] = {1, 2};
603
604 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
605 aws_mqtt311_decoder_reset_for_new_connection(decoder);
606
607 size_t fragment_length = fragment_lengths[j];
608 struct aws_byte_cursor packet_cursor =
609 aws_byte_cursor_from_array(pingreq_packet_data, AWS_ARRAY_SIZE(pingreq_packet_data));
610 while (packet_cursor.len > 0) {
611 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
612 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
613
614 /* If this is the final call, it should fail-but-not-crash as there's no handler */
615 bool should_fail = packet_cursor.len == 0;
616 if (should_fail) {
617 ASSERT_FAILS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
618 } else {
619 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
620 }
621 }
622 }
623
624 s_clean_up_decoding_test_context(&test_context);
625
626 aws_mqtt_library_clean_up();
627
628 return AWS_OP_SUCCESS;
629 }
630
631 AWS_TEST_CASE(mqtt_frame_and_decode_unsupported_packet_type, s_mqtt_frame_and_decode_unsupported_packet_type_fn)
632
633 static int s_mqtt_frame_and_decode_bad_flags_for_packet_type_fn(struct aws_allocator *allocator, void *ctx) {
634 (void)ctx;
635
636 aws_mqtt_library_init(allocator);
637
638 struct mqtt_311_decoding_test_context test_context;
639 s_init_decoding_test_context(&test_context, allocator);
640
641 struct aws_mqtt311_decoder *decoder = &test_context.decoder;
642
643 /* start with a valid suback */
644 struct aws_mqtt_packet_suback suback_packet;
645 ASSERT_SUCCESS(aws_mqtt_packet_suback_init(&suback_packet, allocator, 1234));
646
647 uint8_t sample_return_codes[] = {0x00, 0x01, 0x02, 0x80, 0x01};
648 for (size_t i = 0; i < AWS_ARRAY_SIZE(sample_return_codes); ++i) {
649 aws_mqtt_packet_suback_add_return_code(&suback_packet, sample_return_codes[i]);
650 }
651
652 /* encode it */
653 struct aws_byte_buf encoded_buffer;
654 aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);
655
656 ASSERT_SUCCESS(aws_mqtt_packet_suback_encode(&encoded_buffer, &suback_packet));
657
658 /* suback flags should be zero; mess that up */
659 encoded_buffer.buffer[0] |= 0x05;
660
661 size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};
662
663 for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
664 size_t fragment_length = fragment_lengths[j];
665
666 aws_mqtt311_decoder_reset_for_new_connection(decoder);
667
668 struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
669 while (packet_cursor.len > 0) {
670 size_t advance = aws_min_size(packet_cursor.len, fragment_length);
671 struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);
672
673 /* If this is the final call, it should fail-but-not-crash as the full decode should fail */
674 bool should_fail = packet_cursor.len == 0;
675 if (should_fail) {
676 ASSERT_FAILS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
677 } else {
678 ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
679 }
680 }
681 }
682
683 aws_mqtt_packet_suback_clean_up(&suback_packet);
684 aws_byte_buf_clean_up(&encoded_buffer);
685
686 s_clean_up_decoding_test_context(&test_context);
687
688 aws_mqtt_library_clean_up();
689
690 return AWS_OP_SUCCESS;
691 }
692
693 AWS_TEST_CASE(mqtt_frame_and_decode_bad_flags_for_packet_type, s_mqtt_frame_and_decode_bad_flags_for_packet_type_fn)