File: packet_framing_tests.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (693 lines) | stat: -rw-r--r-- 24,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/testing/aws_test_harness.h>

#include <aws/mqtt/private/fixed_header.h>
#include <aws/mqtt/private/mqtt311_decoder.h>
#include <aws/mqtt/private/packets.h>

struct mqtt_311_decoding_test_context {
    struct aws_allocator *allocator;
    struct aws_mqtt311_decoder decoder;

    void *expected_packet;
    size_t packet_count[16];
};

static int s_compare_fixed_header(
    struct aws_mqtt_fixed_header *expected_header,
    struct aws_mqtt_fixed_header *actual_header) {
    ASSERT_INT_EQUALS(expected_header->packet_type, actual_header->packet_type);
    ASSERT_INT_EQUALS(expected_header->remaining_length, actual_header->remaining_length);
    ASSERT_INT_EQUALS(expected_header->flags, actual_header->flags);

    return AWS_OP_SUCCESS;
}

static int s_decoding_test_handle_publish(struct aws_byte_cursor message_cursor, void *user_data) {

    struct mqtt_311_decoding_test_context *context = user_data;
    (void)context;

    struct aws_mqtt_packet_publish publish;
    if (aws_mqtt_packet_publish_decode(&message_cursor, &publish)) {
        return AWS_OP_ERR;
    }

    struct aws_mqtt_packet_publish *expected_publish = context->expected_packet;

    ASSERT_SUCCESS(s_compare_fixed_header(&expected_publish->fixed_header, &publish.fixed_header));

    ASSERT_INT_EQUALS(expected_publish->packet_identifier, publish.packet_identifier);

    ASSERT_BIN_ARRAYS_EQUALS(
        expected_publish->topic_name.ptr,
        expected_publish->topic_name.len,
        publish.topic_name.ptr,
        publish.topic_name.len);
    ASSERT_BIN_ARRAYS_EQUALS(
        expected_publish->payload.ptr, expected_publish->payload.len, publish.payload.ptr, publish.payload.len);

    ++context->packet_count[AWS_MQTT_PACKET_PUBLISH];

    return AWS_OP_SUCCESS;
}

static int s_decoding_test_handle_suback(struct aws_byte_cursor message_cursor, void *user_data) {

    struct mqtt_311_decoding_test_context *context = user_data;
    (void)context;

    struct aws_mqtt_packet_suback suback;
    if (aws_mqtt_packet_suback_init(&suback, context->allocator, 0 /* fake packet_id */)) {
        return AWS_OP_ERR;
    }

    int result = AWS_OP_ERR;
    if (aws_mqtt_packet_suback_decode(&message_cursor, &suback)) {
        goto done;
    }

    struct aws_mqtt_packet_suback *expected_suback = context->expected_packet;

    ASSERT_INT_EQUALS(expected_suback->packet_identifier, suback.packet_identifier);

    size_t expected_ack_count = aws_array_list_length(&expected_suback->return_codes);
    size_t actual_ack_count = aws_array_list_length(&suback.return_codes);
    ASSERT_INT_EQUALS(expected_ack_count, actual_ack_count);

    for (size_t i = 0; i < expected_ack_count; ++i) {
        uint8_t expected_return_code = 0;
        aws_array_list_get_at(&expected_suback->return_codes, &expected_return_code, i);

        uint8_t actual_return_code = 0;
        aws_array_list_get_at(&suback.return_codes, &actual_return_code, i);

        ASSERT_INT_EQUALS(expected_return_code, actual_return_code);
    }

    ++context->packet_count[AWS_MQTT_PACKET_SUBACK];

    result = AWS_OP_SUCCESS;

done:

    aws_mqtt_packet_suback_clean_up(&suback);

    return result;
}

static int s_decoding_test_handle_unsuback(struct aws_byte_cursor message_cursor, void *user_data) {

    struct mqtt_311_decoding_test_context *context = user_data;
    (void)context;

    struct aws_mqtt_packet_ack unsuback;
    if (aws_mqtt_packet_unsuback_init(&unsuback, 0 /* fake packet_id */)) {
        return AWS_OP_ERR;
    }

    if (aws_mqtt_packet_ack_decode(&message_cursor, &unsuback)) {
        return AWS_OP_ERR;
    }

    struct aws_mqtt_packet_ack *expected_unsuback = context->expected_packet;

    ASSERT_INT_EQUALS(expected_unsuback->packet_identifier, unsuback.packet_identifier);

    ++context->packet_count[AWS_MQTT_PACKET_UNSUBACK];

    return AWS_OP_SUCCESS;
}

static int s_decoding_test_handle_puback(struct aws_byte_cursor message_cursor, void *user_data) {

    struct mqtt_311_decoding_test_context *context = user_data;
    (void)context;

    struct aws_mqtt_packet_ack puback;
    if (aws_mqtt_packet_puback_init(&puback, 0 /* fake packet_id */)) {
        return AWS_OP_ERR;
    }

    if (aws_mqtt_packet_ack_decode(&message_cursor, &puback)) {
        return AWS_OP_ERR;
    }

    struct aws_mqtt_packet_ack *expected_puback = context->expected_packet;

    ASSERT_INT_EQUALS(expected_puback->packet_identifier, puback.packet_identifier);

    ++context->packet_count[AWS_MQTT_PACKET_PUBACK];

    return AWS_OP_SUCCESS;
}

static int s_decoding_test_handle_pingresp(struct aws_byte_cursor message_cursor, void *user_data) {

    struct mqtt_311_decoding_test_context *context = user_data;
    (void)context;

    struct aws_mqtt_packet_connection pingresp;
    if (aws_mqtt_packet_pingresp_init(&pingresp)) {
        return AWS_OP_ERR;
    }

    if (aws_mqtt_packet_connection_decode(&message_cursor, &pingresp)) {
        return AWS_OP_ERR;
    }

    ++context->packet_count[AWS_MQTT_PACKET_PINGRESP];

    return AWS_OP_SUCCESS;
}

static int s_decoding_test_handle_connack(struct aws_byte_cursor message_cursor, void *user_data) {

    struct mqtt_311_decoding_test_context *context = user_data;
    (void)context;

    struct aws_mqtt_packet_connack connack;
    if (aws_mqtt_packet_connack_init(&connack, false, 0)) {
        return AWS_OP_ERR;
    }

    if (aws_mqtt_packet_connack_decode(&message_cursor, &connack)) {
        return AWS_OP_ERR;
    }

    struct aws_mqtt_packet_connack *expected_connack = context->expected_packet;

    ASSERT_INT_EQUALS(expected_connack->session_present, connack.session_present);
    ASSERT_INT_EQUALS(expected_connack->connect_return_code, connack.connect_return_code);

    ++context->packet_count[AWS_MQTT_PACKET_CONNACK];

    return AWS_OP_SUCCESS;
}

static struct aws_mqtt_client_connection_packet_handlers s_decoding_test_packet_handlers = {
    .handlers_by_packet_type = {
        [AWS_MQTT_PACKET_PUBLISH] = &s_decoding_test_handle_publish,
        [AWS_MQTT_PACKET_SUBACK] = &s_decoding_test_handle_suback,
        [AWS_MQTT_PACKET_UNSUBACK] = &s_decoding_test_handle_unsuback,
        [AWS_MQTT_PACKET_PUBACK] = &s_decoding_test_handle_puback,
        [AWS_MQTT_PACKET_PINGRESP] = &s_decoding_test_handle_pingresp,
        [AWS_MQTT_PACKET_CONNACK] = &s_decoding_test_handle_connack,
    }};

static void s_init_decoding_test_context(
    struct mqtt_311_decoding_test_context *context,
    struct aws_allocator *allocator) {
    AWS_ZERO_STRUCT(*context);

    context->allocator = allocator;

    struct aws_mqtt311_decoder_options config = {
        .packet_handlers = &s_decoding_test_packet_handlers,
        .handler_user_data = context,
    };

    aws_mqtt311_decoder_init(&context->decoder, allocator, &config);
}

static void s_clean_up_decoding_test_context(struct mqtt_311_decoding_test_context *context) {

    aws_mqtt311_decoder_clean_up(&context->decoder);
}

#define TEST_ADJACENT_PACKET_COUNT 4

static int s_mqtt_frame_and_decode_publish_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    /*
     * For completeness, run the test with payload sizes that lead to a remaining length VLI encoding of 1, 2, 3, and
     * 4 bytes.
     */
    size_t payload_sizes[] = {35, 1234, 1 << 16, 1 << 21};

    for (size_t i = 0; i < AWS_ARRAY_SIZE(payload_sizes); ++i) {
        struct mqtt_311_decoding_test_context test_context;
        s_init_decoding_test_context(&test_context, allocator);

        struct aws_mqtt311_decoder *decoder = &test_context.decoder;

        size_t publish_payload_size = payload_sizes[i];

        /* Intentionally don't initialize so we have lots of garbage */
        uint8_t *raw_payload = aws_mem_acquire(allocator, publish_payload_size);

        struct aws_mqtt_packet_publish publish_packet;
        ASSERT_SUCCESS(aws_mqtt_packet_publish_init(
            &publish_packet,
            true,
            AWS_MQTT_QOS_AT_LEAST_ONCE,
            false,
            aws_byte_cursor_from_c_str("Hello/World"),
            12,
            aws_byte_cursor_from_array(raw_payload, publish_payload_size)));

        test_context.expected_packet = &publish_packet;

        struct aws_byte_buf encoded_buffer;
        aws_byte_buf_init(&encoded_buffer, allocator, (publish_payload_size + 100) * TEST_ADJACENT_PACKET_COUNT);

        for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
            ASSERT_SUCCESS(aws_mqtt_packet_publish_encode(&encoded_buffer, &publish_packet));
        }

        size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23, 37, 67, 131};

        for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
            size_t fragment_length = fragment_lengths[j];

            struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
            while (packet_cursor.len > 0) {
                size_t advance = aws_min_size(packet_cursor.len, fragment_length);
                struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

                ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            }
        }

        ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_PUBLISH]);

        aws_byte_buf_clean_up(&encoded_buffer);
        aws_mem_release(allocator, raw_payload);

        s_clean_up_decoding_test_context(&test_context);
    }

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_publish, s_mqtt_frame_and_decode_publish_fn)

static int s_mqtt_frame_and_decode_suback_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    struct aws_mqtt_packet_suback suback_packet;
    ASSERT_SUCCESS(aws_mqtt_packet_suback_init(&suback_packet, allocator, 1234));

    uint8_t sample_return_codes[] = {0x00, 0x01, 0x02, 0x80, 0x01};
    for (size_t i = 0; i < AWS_ARRAY_SIZE(sample_return_codes); ++i) {
        aws_mqtt_packet_suback_add_return_code(&suback_packet, sample_return_codes[i]);
    }

    test_context.expected_packet = &suback_packet;

    struct aws_byte_buf encoded_buffer;
    aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);

    for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
        ASSERT_SUCCESS(aws_mqtt_packet_suback_encode(&encoded_buffer, &suback_packet));
    }

    size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23, 37, 67, 143};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        size_t fragment_length = fragment_lengths[j];

        struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
        }
    }

    ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_SUBACK]);

    aws_mqtt_packet_suback_clean_up(&suback_packet);
    aws_byte_buf_clean_up(&encoded_buffer);

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_suback, s_mqtt_frame_and_decode_suback_fn)

static int s_mqtt_frame_and_decode_unsuback_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    struct aws_mqtt_packet_ack unsuback_packet;
    ASSERT_SUCCESS(aws_mqtt_packet_unsuback_init(&unsuback_packet, 1234));

    test_context.expected_packet = &unsuback_packet;

    struct aws_byte_buf encoded_buffer;
    aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);

    for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
        ASSERT_SUCCESS(aws_mqtt_packet_ack_encode(&encoded_buffer, &unsuback_packet));
    }

    size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        size_t fragment_length = fragment_lengths[j];

        struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
        }
    }

    ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_UNSUBACK]);

    aws_byte_buf_clean_up(&encoded_buffer);

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_unsuback, s_mqtt_frame_and_decode_unsuback_fn)

static int s_mqtt_frame_and_decode_puback_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    struct aws_mqtt_packet_ack puback_packet;
    ASSERT_SUCCESS(aws_mqtt_packet_puback_init(&puback_packet, 1234));

    test_context.expected_packet = &puback_packet;

    struct aws_byte_buf encoded_buffer;
    aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);

    for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
        ASSERT_SUCCESS(aws_mqtt_packet_ack_encode(&encoded_buffer, &puback_packet));
    }

    size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        size_t fragment_length = fragment_lengths[j];

        struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
        }
    }

    ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_PUBACK]);

    aws_byte_buf_clean_up(&encoded_buffer);

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_puback, s_mqtt_frame_and_decode_puback_fn)

static int s_mqtt_frame_and_decode_pingresp_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    struct aws_mqtt_packet_connection pingresp_packet;
    ASSERT_SUCCESS(aws_mqtt_packet_pingresp_init(&pingresp_packet));

    test_context.expected_packet = &pingresp_packet;

    struct aws_byte_buf encoded_buffer;
    aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);

    for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
        ASSERT_SUCCESS(aws_mqtt_packet_connection_encode(&encoded_buffer, &pingresp_packet));
    }

    size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        size_t fragment_length = fragment_lengths[j];

        struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
        }
    }

    ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_PINGRESP]);

    aws_byte_buf_clean_up(&encoded_buffer);

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_pingresp, s_mqtt_frame_and_decode_pingresp_fn)

static int s_mqtt_frame_and_decode_connack_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    struct aws_mqtt_packet_connack connack_packet;
    ASSERT_SUCCESS(aws_mqtt_packet_connack_init(&connack_packet, true, AWS_MQTT_CONNECT_NOT_AUTHORIZED));

    test_context.expected_packet = &connack_packet;

    struct aws_byte_buf encoded_buffer;
    aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);

    for (size_t j = 0; j < TEST_ADJACENT_PACKET_COUNT; ++j) {
        ASSERT_SUCCESS(aws_mqtt_packet_connack_encode(&encoded_buffer, &connack_packet));
    }

    size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        size_t fragment_length = fragment_lengths[j];

        struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
        }
    }

    ASSERT_INT_EQUALS(4 * AWS_ARRAY_SIZE(fragment_lengths), test_context.packet_count[AWS_MQTT_PACKET_CONNACK]);

    aws_byte_buf_clean_up(&encoded_buffer);

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_connack, s_mqtt_frame_and_decode_connack_fn)

static int s_mqtt_frame_and_decode_bad_remaining_length_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    /* QoS 0 Publish "Packet" data where the remaining length vli-encoding is illegal */
    uint8_t bad_packet_data[] = {0x30, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00};

    size_t fragment_lengths[] = {1, 2, 3, 5, 13};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        aws_mqtt311_decoder_reset_for_new_connection(decoder);

        size_t fragment_length = fragment_lengths[j];
        struct aws_byte_cursor packet_cursor =
            aws_byte_cursor_from_array(bad_packet_data, AWS_ARRAY_SIZE(bad_packet_data));
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            /* If this or a previous call contains the final 0x80 of the invalid vli encoding, then decode must fail */
            bool should_fail = (fragment_cursor.ptr + fragment_cursor.len) - bad_packet_data > 4;
            if (should_fail) {
                ASSERT_FAILS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            } else {
                ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            }
        }
    }

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_bad_remaining_length, s_mqtt_frame_and_decode_bad_remaining_length_fn)

static int s_mqtt_frame_and_decode_unsupported_packet_type_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    /* Pingreq packet, no handler installed */
    uint8_t pingreq_packet_data[] = {192, 0};

    size_t fragment_lengths[] = {1, 2};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        aws_mqtt311_decoder_reset_for_new_connection(decoder);

        size_t fragment_length = fragment_lengths[j];
        struct aws_byte_cursor packet_cursor =
            aws_byte_cursor_from_array(pingreq_packet_data, AWS_ARRAY_SIZE(pingreq_packet_data));
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            /* If this is the final call, it should fail-but-not-crash as there's no handler */
            bool should_fail = packet_cursor.len == 0;
            if (should_fail) {
                ASSERT_FAILS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            } else {
                ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            }
        }
    }

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_unsupported_packet_type, s_mqtt_frame_and_decode_unsupported_packet_type_fn)

static int s_mqtt_frame_and_decode_bad_flags_for_packet_type_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    aws_mqtt_library_init(allocator);

    struct mqtt_311_decoding_test_context test_context;
    s_init_decoding_test_context(&test_context, allocator);

    struct aws_mqtt311_decoder *decoder = &test_context.decoder;

    /* start with a valid suback */
    struct aws_mqtt_packet_suback suback_packet;
    ASSERT_SUCCESS(aws_mqtt_packet_suback_init(&suback_packet, allocator, 1234));

    uint8_t sample_return_codes[] = {0x00, 0x01, 0x02, 0x80, 0x01};
    for (size_t i = 0; i < AWS_ARRAY_SIZE(sample_return_codes); ++i) {
        aws_mqtt_packet_suback_add_return_code(&suback_packet, sample_return_codes[i]);
    }

    /* encode it */
    struct aws_byte_buf encoded_buffer;
    aws_byte_buf_init(&encoded_buffer, allocator, 100 * TEST_ADJACENT_PACKET_COUNT);

    ASSERT_SUCCESS(aws_mqtt_packet_suback_encode(&encoded_buffer, &suback_packet));

    /* suback flags should be zero; mess that up */
    encoded_buffer.buffer[0] |= 0x05;

    size_t fragment_lengths[] = {1, 2, 3, 5, 7, 11, 23};

    for (size_t j = 0; j < AWS_ARRAY_SIZE(fragment_lengths); ++j) {
        size_t fragment_length = fragment_lengths[j];

        aws_mqtt311_decoder_reset_for_new_connection(decoder);

        struct aws_byte_cursor packet_cursor = aws_byte_cursor_from_buf(&encoded_buffer);
        while (packet_cursor.len > 0) {
            size_t advance = aws_min_size(packet_cursor.len, fragment_length);
            struct aws_byte_cursor fragment_cursor = aws_byte_cursor_advance(&packet_cursor, advance);

            /* If this is the final call, it should fail-but-not-crash as the full decode should fail */
            bool should_fail = packet_cursor.len == 0;
            if (should_fail) {
                ASSERT_FAILS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            } else {
                ASSERT_SUCCESS(aws_mqtt311_decoder_on_bytes_received(decoder, fragment_cursor));
            }
        }
    }

    aws_mqtt_packet_suback_clean_up(&suback_packet);
    aws_byte_buf_clean_up(&encoded_buffer);

    s_clean_up_decoding_test_context(&test_context);

    aws_mqtt_library_clean_up();

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt_frame_and_decode_bad_flags_for_packet_type, s_mqtt_frame_and_decode_bad_flags_for_packet_type_fn)