File: s3_util_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 (731 lines) | stat: -rw-r--r-- 27,374 bytes parent folder | download
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include "aws/s3/private/s3_client_impl.h"
#include "aws/s3/private/s3_meta_request_impl.h"
#include "aws/s3/private/s3_util.h"
#include "s3_tester.h"
#include <aws/common/byte_buf.h>
#include <aws/common/clock.h>
#include <aws/common/common.h>
#include <aws/common/environment.h>
#include <aws/common/ref_count.h>
#include <aws/http/request_response.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
#include <aws/io/host_resolver.h>
#include <aws/io/stream.h>
#include <aws/io/tls_channel_handler.h>
#include <aws/testing/aws_test_harness.h>
#include <inttypes.h>
#include <stdio.h>

AWS_TEST_CASE(test_s3_request_type_operation_name, s_test_s3_request_type_operation_name)
static int s_test_s3_request_type_operation_name(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    /* sanity check */
    ASSERT_STR_EQUALS("HeadObject", aws_s3_request_type_operation_name(AWS_S3_REQUEST_TYPE_HEAD_OBJECT));

    /* check that all valid enums give back valid strings */
    for (enum aws_s3_request_type type = AWS_S3_REQUEST_TYPE_UNKNOWN + 1; type < AWS_S3_REQUEST_TYPE_MAX; ++type) {
        const char *operation_name = aws_s3_request_type_operation_name(type);
        ASSERT_NOT_NULL(operation_name);
        ASSERT_TRUE(strlen(operation_name) > 1);
    }

    /* check that invalid enums give back empty strings */
    ASSERT_NOT_NULL(aws_s3_request_type_operation_name(AWS_S3_REQUEST_TYPE_UNKNOWN));
    ASSERT_STR_EQUALS("", aws_s3_request_type_operation_name(AWS_S3_REQUEST_TYPE_UNKNOWN));
    ASSERT_STR_EQUALS("", aws_s3_request_type_operation_name(AWS_S3_REQUEST_TYPE_MAX));
    ASSERT_STR_EQUALS("", aws_s3_request_type_operation_name(-1));

    return 0;
}

AWS_TEST_CASE(test_s3_replace_quote_entities, s_test_s3_replace_quote_entities)
static int s_test_s3_replace_quote_entities(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    struct test_case {
        struct aws_byte_cursor test_string;
        const char *expected_result;
    };

    struct test_case test_cases[] = {
        {
            .test_string = aws_byte_cursor_from_c_str("&quot;testtest"),
            .expected_result = "\"testtest",
        },
        {
            .test_string = aws_byte_cursor_from_c_str("testtest&quot;"),
            .expected_result = "testtest\"",
        },
        {
            .test_string = aws_byte_cursor_from_c_str("&quot;&quot;"),
            .expected_result = "\"\"",
        },
        {
            .test_string = aws_byte_cursor_from_c_str("testtest"),
            .expected_result = "testtest",
        },
        {
            .test_string = aws_byte_cursor_from_c_str(""),
            .expected_result = "",
        },
    };

    for (size_t i = 0; i < AWS_ARRAY_SIZE(test_cases); ++i) {
        struct test_case *test_case = &test_cases[i];

        struct aws_byte_buf result_byte_buf = aws_replace_quote_entities(allocator, test_case->test_string);

        struct aws_byte_cursor result_byte_cursor = aws_byte_cursor_from_buf(&result_byte_buf);

        ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result_byte_cursor, test_case->expected_result);

        aws_byte_buf_clean_up(&result_byte_buf);
    }

    return 0;
}

AWS_TEST_CASE(test_s3_strip_quotes, s_test_s3_strip_quotes)
static int s_test_s3_strip_quotes(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    struct test_case {
        struct aws_byte_cursor test_cursor;
        struct aws_byte_cursor expected_result;
    };

    struct test_case test_cases[] = {
        {
            .test_cursor = aws_byte_cursor_from_c_str("\"test\""),
            .expected_result = aws_byte_cursor_from_c_str("test"),
        },
        {
            .test_cursor = aws_byte_cursor_from_c_str("test\""),
            .expected_result = aws_byte_cursor_from_c_str("test\""),
        },
        {
            .test_cursor = aws_byte_cursor_from_c_str("\"test"),
            .expected_result = aws_byte_cursor_from_c_str("\"test"),
        },
        {
            .test_cursor = aws_byte_cursor_from_c_str("test"),
            .expected_result = aws_byte_cursor_from_c_str("test"),
        },
        {
            .test_cursor = aws_byte_cursor_from_c_str(""),
            .expected_result = aws_byte_cursor_from_c_str(""),
        },
    };

    for (size_t i = 0; i < AWS_ARRAY_SIZE(test_cases); ++i) {
        struct test_case *test_case = &test_cases[i];

        struct aws_byte_buf result_byte_buf;
        AWS_ZERO_STRUCT(result_byte_buf);

        struct aws_string *result = aws_strip_quotes(allocator, test_case->test_cursor);

        struct aws_byte_cursor result_byte_cursor = aws_byte_cursor_from_string(result);

        ASSERT_TRUE(aws_byte_cursor_eq(&test_case->expected_result, &result_byte_cursor));

        aws_byte_buf_clean_up(&result_byte_buf);
        aws_string_destroy(result);
    }

    return 0;
}

AWS_TEST_CASE(test_s3_parse_request_range_header, s_test_s3_parse_request_range_header)
static int s_test_s3_parse_request_range_header(struct aws_allocator *allocator, void *ctx) {

    (void)ctx;

    struct range_header_example {
        struct aws_byte_cursor header_value;

        bool has_start_range;
        bool has_end_range;
        uint64_t range_start;
        uint64_t range_end;
    };

    const struct range_header_example valid_range_examples[] = {
        {
            .header_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=5-10"),
            .has_start_range = true,
            .has_end_range = true,
            .range_start = 5,
            .range_end = 10,
        },
        {
            .header_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-0"),
            .has_start_range = true,
            .has_end_range = true,
            .range_start = 0,
            .range_end = 0,
        },
        {
            .header_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-"),
            .has_start_range = true,
            .has_end_range = false,
            .range_start = 0,
            .range_end = 0,
        },
        {
            .header_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=5-"),
            .has_start_range = true,
            .has_end_range = false,
            .range_start = 5,
            .range_end = 0,
        },
        {
            .header_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=-10"),
            .has_start_range = false,
            .has_end_range = true,
            .range_start = 0,
            .range_end = 10,
        },
    };

    const struct aws_byte_cursor invalid_range_header_values[] = {
        AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=-"),
        AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=10-5"),
        AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("byts=0-5"),
        AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("5-10"),
    };
    bool has_start_range = false;
    bool has_end_range = false;
    uint64_t range_start = 0;
    uint64_t range_end = 0;

    struct aws_http_headers *headers = aws_http_headers_new(allocator);
    /* Check that it fails if there is no Range header */
    ASSERT_FAILS(
        aws_s3_parse_request_range_header(headers, &has_start_range, &has_end_range, &range_start, &range_end));

    /* Check the valid test cases */
    for (size_t i = 0; i < AWS_ARRAY_SIZE(valid_range_examples); ++i) {
        printf("valid example [%zu]: " PRInSTR "\n", i, AWS_BYTE_CURSOR_PRI(valid_range_examples[i].header_value));

        aws_http_headers_set(headers, g_range_header_name, valid_range_examples[i].header_value);

        ASSERT_SUCCESS(
            aws_s3_parse_request_range_header(headers, &has_start_range, &has_end_range, &range_start, &range_end));

        ASSERT_INT_EQUALS(valid_range_examples[i].has_start_range, has_start_range);
        ASSERT_INT_EQUALS(valid_range_examples[i].has_end_range, has_end_range);
        ASSERT_INT_EQUALS(valid_range_examples[i].range_start, range_start);
        ASSERT_INT_EQUALS(valid_range_examples[i].range_end, range_end);
    }

    /* Check the invalid test cases */
    for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_range_header_values); ++i) {
        printf("invalid example [%zu]: " PRInSTR "\n", i, AWS_BYTE_CURSOR_PRI(invalid_range_header_values[i]));

        aws_http_headers_set(headers, g_range_header_name, invalid_range_header_values[i]);

        ASSERT_FAILS(
            aws_s3_parse_request_range_header(headers, &has_start_range, &has_end_range, &range_start, &range_end));
    }

    aws_http_headers_release(headers);

    return 0;
}

AWS_TEST_CASE(test_s3_parse_content_range_response_header, s_test_s3_parse_content_range_response_header)
static int s_test_s3_parse_content_range_response_header(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    const struct aws_http_header content_range_header = {
        .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Range"),
        .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes 55-100/12345"),
    };

    const struct aws_http_header invalid_content_range_header = {
        .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Range"),
        .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes 55-100/"),
    };

    struct aws_http_headers *response_headers = aws_http_headers_new(allocator);

    /* Try to parse a header that isn't there. */
    {
        uint64_t object_size = 0ULL;

        ASSERT_FAILS(aws_s3_parse_content_range_response_header(allocator, response_headers, NULL, NULL, &object_size));
        ASSERT_TRUE(aws_last_error() == AWS_ERROR_S3_MISSING_CONTENT_RANGE_HEADER);
    }

    aws_http_headers_add_header(response_headers, &content_range_header);

    /* Parse all of the data from a valid header. */
    {
        uint64_t object_size = 0ULL;
        uint64_t range_start = 0ULL;
        uint64_t range_end = 0ULL;

        ASSERT_SUCCESS(aws_s3_parse_content_range_response_header(
            allocator, response_headers, &range_start, &range_end, &object_size));
        ASSERT_TRUE(range_start == 55ULL);
        ASSERT_TRUE(range_end == 100ULL);
        ASSERT_TRUE(object_size == 12345ULL);
    }

    /* Range-end and range-start are optional output arguments. */
    {
        uint64_t object_size = 0ULL;

        ASSERT_SUCCESS(
            aws_s3_parse_content_range_response_header(allocator, response_headers, NULL, NULL, &object_size));
        ASSERT_TRUE(object_size == 12345ULL);
    }

    aws_http_headers_set(response_headers, invalid_content_range_header.name, invalid_content_range_header.value);

    /* Try to parse an invalid header. */
    {
        uint64_t object_size = 0ULL;
        ASSERT_FAILS(aws_s3_parse_content_range_response_header(allocator, response_headers, NULL, NULL, &object_size));
        ASSERT_TRUE(aws_last_error() == AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER);
    }

    aws_http_headers_release(response_headers);

    return 0;
}

AWS_TEST_CASE(test_s3_parse_content_length_response_header, s_test_s3_parse_content_length_response_header)
static int s_test_s3_parse_content_length_response_header(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;

    struct aws_http_headers *response_headers = aws_http_headers_new(allocator);

    const struct aws_http_header valid_content_length_header = {
        .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
        .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("12345"),
    };

    const struct aws_http_header invalid_content_length_header = {
        .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
        .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(""),
    };

    /* Try to parse a header that isn't there. */
    {
        uint64_t content_length = 0ULL;
        ASSERT_FAILS(aws_s3_parse_content_length_response_header(allocator, response_headers, &content_length));
        ASSERT_TRUE(aws_last_error() == AWS_ERROR_S3_MISSING_CONTENT_LENGTH_HEADER);
    }

    aws_http_headers_add_header(response_headers, &valid_content_length_header);

    /* Parse a valid header. */
    {
        uint64_t content_length = 0ULL;
        ASSERT_SUCCESS(aws_s3_parse_content_length_response_header(allocator, response_headers, &content_length));
        ASSERT_TRUE(content_length == 12345ULL);
    }

    aws_http_headers_set(response_headers, invalid_content_length_header.name, invalid_content_length_header.value);

    /* Try to parse an invalid header. */
    {
        uint64_t content_length = 0ULL;
        ASSERT_FAILS(aws_s3_parse_content_length_response_header(allocator, response_headers, &content_length));
        ASSERT_TRUE(aws_last_error() == AWS_ERROR_S3_INVALID_CONTENT_LENGTH_HEADER);
    }

    aws_http_headers_release(response_headers);

    return 0;
}

static int s_validate_part_ranges(
    uint64_t object_range_start,
    uint64_t object_range_end,
    size_t part_size,
    uint32_t num_parts,
    const uint64_t *part_ranges) {
    ASSERT_TRUE(part_ranges != NULL);
    uint64_t aligned_first_part_size = part_size - (object_range_start % part_size);
    for (uint32_t i = 0; i < num_parts; ++i) {
        uint64_t part_range_start = 0ULL;
        uint64_t part_range_end = 0ULL;

        aws_s3_calculate_auto_ranged_get_part_range(
            object_range_start,
            object_range_end,
            part_size,
            aligned_first_part_size,
            i + 1,
            &part_range_start,
            &part_range_end);

        ASSERT_TRUE(part_range_start == part_ranges[i * 2]);
        ASSERT_TRUE(part_range_end == part_ranges[i * 2 + 1]);
    }

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_s3_get_num_parts_and_get_part_range, s_test_s3_get_num_parts_and_get_part_range)
static int s_test_s3_get_num_parts_and_get_part_range(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    const size_t part_size = 16 * 1024;

    /* Empty File . */
    {
        const uint32_t expected_num_parts = 1;
        const uint64_t object_range_start = 0;
        const uint64_t object_range_end = 0;

        ASSERT_INT_EQUALS(
            aws_s3_calculate_auto_ranged_get_num_parts(part_size, 0, object_range_start, object_range_end),
            expected_num_parts);
    }

    /* first_part_size is < part_size . */
    {
        const uint32_t expected_num_parts = 2;
        const uint64_t first_part_size = 2;
        const uint64_t object_range_start = 0;
        const uint64_t object_range_end = 5;

        const uint64_t part_ranges[] = {
            0, /* first_part start */
            1, /* first_part end */

            2, /* second_part start */
            5, /* second_part end */
        };

        ASSERT_INT_EQUALS(
            aws_s3_calculate_auto_ranged_get_num_parts(
                part_size, first_part_size, object_range_start, object_range_end),
            expected_num_parts);

        uint64_t part_range_start, part_range_end;
        aws_s3_calculate_auto_ranged_get_part_range(
            object_range_start, object_range_end, part_size, first_part_size, 1, &part_range_start, &part_range_end);

        ASSERT_INT_EQUALS(part_range_start, part_ranges[0]);
        ASSERT_INT_EQUALS(part_range_end, part_ranges[1]);

        aws_s3_calculate_auto_ranged_get_part_range(
            object_range_start, object_range_end, part_size, first_part_size, 2, &part_range_start, &part_range_end);

        ASSERT_INT_EQUALS(part_range_start, part_ranges[2]);
        ASSERT_INT_EQUALS(part_range_end, part_ranges[3]);
    }

    /* Perfectly aligned on part boundaries. */
    {
        const uint32_t expected_num_parts = 2;
        const uint64_t object_range_start = 0;
        const uint64_t object_range_end =
            (object_range_start + (uint64_t)part_size * (uint64_t)expected_num_parts) - 1ULL;

        const uint64_t part_ranges[] = {
            0,
            (uint64_t)part_size - 1ULL,

            (uint64_t)part_size,
            (uint64_t)part_size * 2ULL - 1ULL,
        };

        ASSERT_TRUE(
            aws_s3_calculate_auto_ranged_get_num_parts(part_size, part_size, object_range_start, object_range_end) ==
            expected_num_parts);

        ASSERT_SUCCESS(
            s_validate_part_ranges(object_range_start, object_range_end, part_size, expected_num_parts, part_ranges));
    }

    /* Range-start unaligned on part boundaries, but range-end aligned. */
    {
        const uint32_t expected_num_parts = 3;
        const uint64_t half_part_size = part_size >> 1ULL;
        const uint64_t object_range_start = half_part_size;
        const uint64_t object_range_end = (object_range_start + half_part_size + (uint64_t)part_size * 2ULL) - 1ULL;
        uint64_t aligned_first_part_size = part_size - (object_range_start % part_size);

        const uint64_t part_ranges[] = {
            object_range_start,
            object_range_start + half_part_size - 1,

            object_range_start + half_part_size,
            object_range_start + half_part_size + (uint64_t)part_size - 1ULL,

            object_range_start + half_part_size + (uint64_t)part_size,
            object_range_start + half_part_size + (uint64_t)part_size * 2ULL - 1ULL,
        };

        ASSERT_TRUE(
            aws_s3_calculate_auto_ranged_get_num_parts(
                part_size, aligned_first_part_size, object_range_start, object_range_end) == expected_num_parts);

        ASSERT_SUCCESS(
            s_validate_part_ranges(object_range_start, object_range_end, part_size, expected_num_parts, part_ranges));
    }

    /* Range-start and range-end both unaligned on part boundaries. */
    {
        const uint32_t expected_num_parts = 4;
        const uint64_t half_part_size = part_size >> 1ULL;
        const uint64_t object_range_start = half_part_size;
        const uint64_t object_range_end =
            (object_range_start + half_part_size + (uint64_t)part_size * 2ULL + half_part_size) - 1ULL;
        uint64_t aligned_first_part_size = part_size - (object_range_start % part_size);

        const uint64_t part_ranges[] = {
            object_range_start,
            object_range_start + half_part_size - 1,

            object_range_start + half_part_size,
            object_range_start + half_part_size + (uint64_t)part_size - 1ULL,

            object_range_start + half_part_size + (uint64_t)part_size,
            object_range_start + half_part_size + (uint64_t)part_size * 2ULL - 1ULL,

            object_range_start + half_part_size + (uint64_t)part_size * 2ULL,
            object_range_start + half_part_size + (uint64_t)part_size * 2ULL + half_part_size - 1ULL,
        };

        ASSERT_TRUE(
            aws_s3_calculate_auto_ranged_get_num_parts(
                part_size, aligned_first_part_size, object_range_start, object_range_end) == expected_num_parts);

        ASSERT_SUCCESS(
            s_validate_part_ranges(object_range_start, object_range_end, part_size, expected_num_parts, part_ranges));
    }

    /* 1 byte range corner case. */
    {
        const uint32_t expected_num_parts = 1;
        const uint64_t object_range_start = 8;
        const uint64_t object_range_end = 8;
        uint64_t aligned_first_part_size = part_size - (object_range_start % part_size);

        const uint64_t part_ranges[] = {8, 8};

        ASSERT_TRUE(
            aws_s3_calculate_auto_ranged_get_num_parts(
                part_size, aligned_first_part_size, object_range_start, object_range_end) == expected_num_parts);

        ASSERT_SUCCESS(
            s_validate_part_ranges(object_range_start, object_range_end, part_size, expected_num_parts, part_ranges));
    }

    return 0;
}

struct s3_request_part_config_example {
    const char *name;
    uint64_t content_length;
    size_t client_part_size;
    uint64_t client_max_part_size;
    size_t expected_part_size;
    uint32_t expected_num_parts;
};

AWS_TEST_CASE(test_s3_mpu_get_part_size_and_num_parts, s_test_s3_mpu_get_part_size_and_num_parts)
static int s_test_s3_mpu_get_part_size_and_num_parts(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    uint64_t default_max_part_size = 5368709120ULL;

    const struct s3_request_part_config_example valid_request_part_config[] = {
        {
            .name = "simple case",
            .content_length = MB_TO_BYTES((uint64_t)10000),
            .client_part_size = MB_TO_BYTES(5),
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 5242880,
            .expected_num_parts = 2000,
        },
        {
            .name = "large content length with small part size",
            .content_length = MB_TO_BYTES((uint64_t)990000),
            .client_part_size = MB_TO_BYTES(5),
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 103809024,
            .expected_num_parts = 10000,
        },
        {

            .name = "large content length with large part size",
            .content_length = MB_TO_BYTES((uint64_t)1000000),
            .client_part_size = MB_TO_BYTES(500),
            .client_max_part_size = default_max_part_size,
            .expected_part_size = MB_TO_BYTES(500),
            .expected_num_parts = 2000,
        },
        {
            .name = "large odd content length",
            .content_length = 1044013645824,
            .client_part_size = 5242880,
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 104401365,
            .expected_num_parts = 10000,
        },
        {
            .name = "10k parts",
            .content_length = MB_TO_BYTES((uint64_t)50000),
            .client_part_size = MB_TO_BYTES(5),
            .client_max_part_size = default_max_part_size,
            .expected_part_size = MB_TO_BYTES(5),
            .expected_num_parts = 10000,
        },
        {
            .name = "10k - 1 parts",
            .content_length = 49995,
            .client_part_size = 5,
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 5,
            .expected_num_parts = 9999,
        },
        {
            .name = "10k with small last part",
            .content_length = 49998,
            .client_part_size = 5,
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 5,
            .expected_num_parts = 10000,
        },
        {
            .name = "10k + 1 parts",
            .content_length = 50001,
            .client_part_size = 5,
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 6,
            .expected_num_parts = 8334,

        },
        {
            .name = "bump content length",
            .content_length = 100000,
            .client_part_size = 5,
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 10,
            .expected_num_parts = 10000,
        },
        {
            .name = "bump content length with non-zero mod",
            .content_length = 999999,
            .client_part_size = 5,
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 100,
            .expected_num_parts = 10000,
        },
        {
            .name = "5 tb content length",
            .content_length = MB_TO_BYTES((uint64_t)5 * 1024 * 1024),
            .client_part_size = MB_TO_BYTES((uint64_t)5),
            .client_max_part_size = default_max_part_size,
            .expected_part_size = 549755814,
            .expected_num_parts = 10000,
        },
    };
    for (size_t i = 0; i < AWS_ARRAY_SIZE(valid_request_part_config); ++i) {
        AWS_LOGF_INFO(AWS_LS_S3_GENERAL, "valid example [%zu]: %s\n", i, valid_request_part_config[i].name);

        uint64_t content_length = valid_request_part_config[i].content_length;
        size_t part_size;
        uint32_t num_parts;

        ASSERT_SUCCESS(aws_s3_calculate_optimal_mpu_part_size_and_num_parts(
            content_length,
            valid_request_part_config[i].client_part_size,
            valid_request_part_config[i].client_max_part_size,
            &part_size,
            &num_parts));
        ASSERT_INT_EQUALS(valid_request_part_config[i].expected_part_size, part_size);
        ASSERT_INT_EQUALS(valid_request_part_config[i].expected_num_parts, num_parts);
    }

    /* Invalid cases */
    const struct s3_request_part_config_example invalid_request_part_config[] = {{
        .name = "max part < required part size",
        .content_length = 900000,
        .client_part_size = 5,
        .client_max_part_size = 10,
    }};

    for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_request_part_config); ++i) {
        printf("invalid example [%zu]: %s\n", i, invalid_request_part_config[i].name);
        size_t part_size;
        uint32_t num_parts;
        ASSERT_FAILS(aws_s3_calculate_optimal_mpu_part_size_and_num_parts(
            invalid_request_part_config[i].content_length,
            invalid_request_part_config[i].client_part_size,
            invalid_request_part_config[i].client_max_part_size,
            &part_size,
            &num_parts));
    }
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_s3_aws_xml_get_body_at_path, s_test_s3_aws_xml_get_body_at_path)
static int s_test_s3_aws_xml_get_body_at_path(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_byte_cursor example_error_body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        "<Error>\n"
        "<Code>AccessDenied</Code>\n"
        "<Message>Access Denied</Message>\n"
        "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
        "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
        "</Error>");

    /* Ensure we can successfully look up <Error><Code> */
    {
        struct aws_byte_cursor error_code = {0};
        const char *xml_path[] = {"Error", "Code", NULL};

        ASSERT_SUCCESS(aws_xml_get_body_at_path(allocator, example_error_body, xml_path, &error_code));
        ASSERT_CURSOR_VALUE_CSTRING_EQUALS(error_code, "AccessDenied");
    }

    /* Ensure we fail if the beginning of the path doesn't match */
    {
        struct aws_byte_cursor error_code = {0};
        const char *xml_path[] = {"ObviouslyInvalidName", "Code", NULL};
        ASSERT_ERROR(
            AWS_ERROR_STRING_MATCH_NOT_FOUND,
            aws_xml_get_body_at_path(allocator, example_error_body, xml_path, &error_code));
    }

    /* Ensure we fail if the end of the path doesn't match */
    {
        struct aws_byte_cursor error_code = {0};
        const char *xml_path[] = {"Error", "ObviouslyInvalidName", NULL};
        ASSERT_ERROR(
            AWS_ERROR_STRING_MATCH_NOT_FOUND,
            aws_xml_get_body_at_path(allocator, example_error_body, xml_path, &error_code));
    }

    /* Ensure we fail if the document isn't valid XML */
    {
        struct aws_byte_cursor error_code = {0};
        const char *xml_path[] = {"Error", "Code", NULL};
        ASSERT_ERROR(
            AWS_ERROR_INVALID_XML,
            aws_xml_get_body_at_path(
                allocator, aws_byte_cursor_from_c_str("Obviously invalid XML document"), xml_path, &error_code));
    }
    return AWS_OP_SUCCESS;
}