Debian Codegraph @

crt/aws-c-s3/tests/s3_checksum_context_test.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/s3/private/s3_checksum_context.h"
7 #include "aws/s3/private/s3_checksums.h"
8 #include <aws/common/byte_buf.h>
9 #include <aws/testing/aws_test_harness.h>
10
11 static int s_test_upload_request_checksum_context_get_checksum_cursor(struct aws_allocator *allocator, void *ctx) {
12 (void)ctx;
13
14 struct aws_s3_meta_request_checksum_config_storage config = {
15 .allocator = allocator,
16 .checksum_algorithm = AWS_SCA_CRC32,
17 .location = AWS_SCL_HEADER,
18 .has_full_object_checksum = false,
19 };
20 AWS_ZERO_STRUCT(config.full_object_checksum);
21
22 /* Test get checksum cursor with context that has no calculated checksum */
23 struct aws_s3_upload_request_checksum_context *context =
24 aws_s3_upload_request_checksum_context_new(allocator, &config, false);
25 ASSERT_NOT_NULL(context);
26
27 struct aws_byte_cursor cursor = aws_s3_upload_request_checksum_context_get_checksum_cursor(context);
28 ASSERT_TRUE(cursor.len == 0);
29 ASSERT_NULL(cursor.ptr);
30
31 aws_s3_upload_request_checksum_context_release(context);
32
33 /* Test get checksum cursor with context that has calculated checksum */
34 struct aws_byte_cursor existing_checksum = aws_byte_cursor_from_c_str("dGVzdA==");
35 context = aws_s3_upload_request_checksum_context_new_with_existing_base64_checksum(
36 allocator, &config, existing_checksum, false);
37 ASSERT_NOT_NULL(context);
38
39 cursor = aws_s3_upload_request_checksum_context_get_checksum_cursor(context);
40 ASSERT_TRUE(cursor.len == existing_checksum.len);
41 ASSERT_TRUE(aws_byte_cursor_eq(&cursor, &existing_checksum));
42
43 aws_s3_upload_request_checksum_context_release(context);
44
45 /* Test get checksum cursor with NULL context */
46 cursor = aws_s3_upload_request_checksum_context_get_checksum_cursor(NULL);
47 ASSERT_TRUE(cursor.len == 0);
48 ASSERT_NULL(cursor.ptr);
49
50 return AWS_OP_SUCCESS;
51 }
52
53 AWS_TEST_CASE(
54 test_upload_request_checksum_context_get_checksum_cursor,
55 s_test_upload_request_checksum_context_get_checksum_cursor)
56
57 static int s_test_upload_request_checksum_context_error_cases(struct aws_allocator *allocator, void *ctx) {
58 (void)ctx;
59
60 struct aws_s3_meta_request_checksum_config_storage config = {
61 .allocator = allocator,
62 .checksum_algorithm = AWS_SCA_CRC32,
63 .location = AWS_SCL_HEADER,
64 .has_full_object_checksum = false,
65 };
66 AWS_ZERO_STRUCT(config.full_object_checksum);
67
68 /* Test creation with mismatched checksum size */
69 struct aws_byte_cursor wrong_size_checksum = aws_byte_cursor_from_c_str("short");
70 struct aws_s3_upload_request_checksum_context *context =
71 aws_s3_upload_request_checksum_context_new_with_existing_base64_checksum(
72 allocator, &config, wrong_size_checksum, false);
73 ASSERT_NULL(context);
74
75 /* Test helper functions with NULL context */
76 ASSERT_FALSE(aws_s3_upload_request_checksum_context_should_calculate(NULL));
77 ASSERT_FALSE(aws_s3_upload_request_checksum_context_should_add_header(NULL));
78 ASSERT_FALSE(aws_s3_upload_request_checksum_context_should_add_trailer(NULL));
79
80 /* Test acquire/release with NULL context */
81 ASSERT_NULL(aws_s3_upload_request_checksum_context_acquire(NULL));
82 ASSERT_NULL(aws_s3_upload_request_checksum_context_release(NULL));
83
84 /* unknown algo */
85 struct aws_s3_meta_request_checksum_config_storage config2 = {
86 .allocator = allocator,
87 .checksum_algorithm = AWS_SCA_UNKNOWN,
88 .location = AWS_SCL_NONE,
89 .has_full_object_checksum = false,
90 };
91 struct aws_s3_upload_request_checksum_context *context2 =
92 aws_s3_upload_request_checksum_context_new(allocator, &config2, false);
93
94 ASSERT_NOT_NULL(context2);
95 ASSERT_FALSE(aws_s3_upload_request_checksum_context_should_calculate(context2));
96
97 /* unknown algo */
98 struct aws_s3_meta_request_checksum_config_storage config3 = {
99 .allocator = allocator,
100 .checksum_algorithm = AWS_SCA_CRC32,
101 .location = AWS_SCL_NONE,
102 .has_full_object_checksum = false,
103 };
104 struct aws_s3_upload_request_checksum_context *context3 =
105 aws_s3_upload_request_checksum_context_new(allocator, &config3, false);
106
107 ASSERT_NOT_NULL(context3);
108 ASSERT_FALSE(aws_s3_upload_request_checksum_context_should_calculate(context3));
109
110 aws_s3_upload_request_checksum_context_release(context2);
111 aws_s3_upload_request_checksum_context_release(context3);
112
113 return AWS_OP_SUCCESS;
114 }
115 AWS_TEST_CASE(test_upload_request_checksum_context_error_cases, s_test_upload_request_checksum_context_error_cases)
116
117 static int s_test_upload_request_checksum_context_different_algorithms(struct aws_allocator *allocator, void *ctx) {
118 (void)ctx;
119
120 /* Test different checksum algorithms */
121 enum aws_s3_checksum_algorithm algorithms[] = {
122 AWS_SCA_CRC32, AWS_SCA_CRC32C, AWS_SCA_SHA1, AWS_SCA_SHA256, AWS_SCA_CRC64NVME};
123
124 for (size_t i = 0; i < AWS_ARRAY_SIZE(algorithms); ++i) {
125 struct aws_s3_meta_request_checksum_config_storage config = {
126 .allocator = allocator,
127 .checksum_algorithm = algorithms[i],
128 .location = AWS_SCL_HEADER,
129 .has_full_object_checksum = false,
130 };
131 AWS_ZERO_STRUCT(config.full_object_checksum);
132
133 struct aws_s3_upload_request_checksum_context *context =
134 aws_s3_upload_request_checksum_context_new(allocator, &config, false);
135 ASSERT_NOT_NULL(context);
136 ASSERT_INT_EQUALS(algorithms[i], context->algorithm);
137 ASSERT_INT_EQUALS(AWS_SCL_HEADER, context->location);
138 ASSERT_TRUE(context->encoded_checksum_size > 0);
139 ASSERT_TRUE(aws_s3_upload_request_checksum_context_should_calculate(context));
140 ASSERT_TRUE(aws_s3_upload_request_checksum_context_should_add_header(context));
141 ASSERT_FALSE(aws_s3_upload_request_checksum_context_should_add_trailer(context));
142
143 aws_s3_upload_request_checksum_context_release(context);
144 }
145
146 return AWS_OP_SUCCESS;
147 }
148 AWS_TEST_CASE(
149 test_upload_request_checksum_context_different_algorithms,
150 s_test_upload_request_checksum_context_different_algorithms)