| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "aws/s3/private/s3_checksum_context.h" |
| 7 |
#include "aws/s3/private/s3_checksums.h" |
| 8 |
#include "s3_tester.h" |
| 9 |
#include <aws/common/byte_buf.h> |
| 10 |
#include <aws/common/encoding.h> |
| 11 |
#include <aws/io/stream.h> |
| 12 |
#include <aws/testing/aws_test_harness.h> |
| 13 |
|
| 14 |
static int compare_checksum_stream(struct aws_allocator *allocator, struct aws_byte_cursor *input, size_t buffer_size) { |
| 15 |
struct aws_byte_buf compute_checksum_output; |
| 16 |
struct aws_byte_buf compute_encoded_checksum_output; |
| 17 |
struct aws_byte_buf stream_checksum_output; |
| 18 |
struct aws_byte_buf read_buf; |
| 19 |
size_t encoded_len = 0; |
| 20 |
aws_byte_buf_init(&read_buf, allocator, buffer_size); |
| 21 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_checksum_algo_priority_list); i++) { |
| 22 |
enum aws_s3_checksum_algorithm algorithm = s_checksum_algo_priority_list[i]; |
| 23 |
aws_base64_compute_encoded_len(aws_get_digest_size_from_checksum_algorithm(algorithm), &encoded_len); |
| 24 |
aws_byte_buf_init(&compute_checksum_output, allocator, aws_get_digest_size_from_checksum_algorithm(algorithm)); |
| 25 |
aws_byte_buf_init(&stream_checksum_output, allocator, encoded_len); |
| 26 |
aws_byte_buf_init(&compute_encoded_checksum_output, allocator, encoded_len); |
| 27 |
aws_checksum_compute(allocator, algorithm, input, &compute_checksum_output); |
| 28 |
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&compute_checksum_output); |
| 29 |
aws_base64_encode(&checksum_result_cursor, &compute_encoded_checksum_output); |
| 30 |
struct aws_input_stream *cursor_stream = aws_input_stream_new_from_cursor(allocator, input); |
| 31 |
struct aws_input_stream *stream = aws_checksum_stream_new(allocator, cursor_stream, algorithm); |
| 32 |
aws_input_stream_release(cursor_stream); |
| 33 |
struct aws_stream_status status; |
| 34 |
AWS_ZERO_STRUCT(status); |
| 35 |
while (!status.is_end_of_stream) { |
| 36 |
ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf)); |
| 37 |
read_buf.len = 0; |
| 38 |
ASSERT_TRUE(aws_input_stream_get_status(stream, &status) == 0); |
| 39 |
} |
| 40 |
aws_checksum_stream_finalize_checksum(stream, &stream_checksum_output); |
| 41 |
ASSERT_TRUE(aws_byte_buf_eq(&compute_encoded_checksum_output, &stream_checksum_output)); |
| 42 |
aws_byte_buf_clean_up(&compute_checksum_output); |
| 43 |
aws_byte_buf_clean_up(&stream_checksum_output); |
| 44 |
aws_byte_buf_clean_up(&compute_encoded_checksum_output); |
| 45 |
aws_input_stream_release(stream); |
| 46 |
} |
| 47 |
aws_byte_buf_clean_up(&read_buf); |
| 48 |
return AWS_OP_SUCCESS; |
| 49 |
} |
| 50 |
|
| 51 |
AWS_STATIC_STRING_FROM_LITERAL(s_0pre_chunk, "0\r\n"); |
| 52 |
AWS_STATIC_STRING_FROM_LITERAL(s_3pre_chunk, "3\r\n"); |
| 53 |
AWS_STATIC_STRING_FROM_LITERAL(s_56pre_chunk, "38\r\n"); |
| 54 |
AWS_STATIC_STRING_FROM_LITERAL(s_112pre_chunk, "70\r\n"); |
| 55 |
AWS_STATIC_STRING_FROM_LITERAL(s_11pre_chunk, "B\r\n"); |
| 56 |
AWS_STATIC_STRING_FROM_LITERAL(s_final_chunk, "\r\n0\r\n"); |
| 57 |
AWS_STATIC_STRING_FROM_LITERAL(s_colon, ":"); |
| 58 |
AWS_STATIC_STRING_FROM_LITERAL(s_post_trailer, "\r\n\r\n"); |
| 59 |
|
| 60 |
static int s_compute_chunk_stream( |
| 61 |
struct aws_allocator *allocator, |
| 62 |
const struct aws_string *pre_chunk, |
| 63 |
struct aws_byte_cursor *input, |
| 64 |
struct aws_byte_buf *output, |
| 65 |
enum aws_s3_checksum_algorithm algorithm, |
| 66 |
struct aws_byte_buf *encoded_checksum_output) { |
| 67 |
struct aws_byte_cursor pre_chunk_cursor = aws_byte_cursor_from_string(pre_chunk); |
| 68 |
struct aws_byte_cursor final_chunk = aws_byte_cursor_from_string(s_final_chunk); |
| 69 |
struct aws_byte_cursor checksum_header_name = aws_get_http_header_name_from_checksum_algorithm(algorithm); |
| 70 |
struct aws_byte_cursor colon = aws_byte_cursor_from_string(s_colon); |
| 71 |
struct aws_byte_cursor post_trailer = aws_byte_cursor_from_string(s_post_trailer); |
| 72 |
struct aws_byte_buf checksum_result; |
| 73 |
aws_byte_buf_init(&checksum_result, allocator, aws_get_digest_size_from_checksum_algorithm(algorithm)); |
| 74 |
if (aws_byte_buf_append(output, &pre_chunk_cursor)) { |
| 75 |
return AWS_OP_ERR; |
| 76 |
} |
| 77 |
if (aws_byte_buf_append(output, input)) { |
| 78 |
return AWS_OP_ERR; |
| 79 |
} |
| 80 |
if (input->len > 0) { |
| 81 |
if (aws_byte_buf_append(output, &final_chunk)) { |
| 82 |
return AWS_OP_ERR; |
| 83 |
} |
| 84 |
} |
| 85 |
if (aws_byte_buf_append(output, &checksum_header_name)) { |
| 86 |
return AWS_OP_ERR; |
| 87 |
} |
| 88 |
if (aws_byte_buf_append(output, &colon)) { |
| 89 |
return AWS_OP_ERR; |
| 90 |
} |
| 91 |
if (aws_checksum_compute(allocator, algorithm, input, &checksum_result)) { |
| 92 |
return AWS_OP_ERR; |
| 93 |
} |
| 94 |
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&checksum_result); |
| 95 |
if (aws_base64_encode(&checksum_result_cursor, encoded_checksum_output)) { |
| 96 |
return AWS_OP_ERR; |
| 97 |
} |
| 98 |
if (aws_base64_encode(&checksum_result_cursor, output)) { |
| 99 |
return AWS_OP_ERR; |
| 100 |
} |
| 101 |
if (aws_byte_buf_append(output, &post_trailer)) { |
| 102 |
return AWS_OP_ERR; |
| 103 |
} |
| 104 |
aws_byte_buf_clean_up(&checksum_result); |
| 105 |
return AWS_OP_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
static int s_stream_chunk( |
| 109 |
struct aws_allocator *allocator, |
| 110 |
struct aws_byte_cursor *input, |
| 111 |
struct aws_byte_buf *read_buf, |
| 112 |
struct aws_byte_buf *output, |
| 113 |
enum aws_s3_checksum_algorithm algorithm, |
| 114 |
struct aws_byte_buf *checksum_result) { |
| 115 |
|
| 116 |
|
| 117 |
struct aws_s3_meta_request_checksum_config_storage config = { |
| 118 |
.allocator = allocator, |
| 119 |
.checksum_algorithm = algorithm, |
| 120 |
.location = AWS_SCL_TRAILER, |
| 121 |
.has_full_object_checksum = false, |
| 122 |
}; |
| 123 |
AWS_ZERO_STRUCT(config.full_object_checksum); |
| 124 |
|
| 125 |
|
| 126 |
struct aws_s3_upload_request_checksum_context *context = |
| 127 |
aws_s3_upload_request_checksum_context_new(allocator, &config, false); |
| 128 |
if (!context) { |
| 129 |
return AWS_OP_ERR; |
| 130 |
} |
| 131 |
|
| 132 |
struct aws_input_stream *cursor_stream = aws_input_stream_new_from_cursor(allocator, input); |
| 133 |
struct aws_input_stream *stream = aws_chunk_stream_new(allocator, cursor_stream, context); |
| 134 |
aws_input_stream_release(cursor_stream); |
| 135 |
|
| 136 |
struct aws_stream_status status; |
| 137 |
AWS_ZERO_STRUCT(status); |
| 138 |
while (!status.is_end_of_stream) { |
| 139 |
ASSERT_SUCCESS(aws_input_stream_read(stream, read_buf)); |
| 140 |
struct aws_byte_cursor read_cursor = aws_byte_cursor_from_buf(read_buf); |
| 141 |
aws_byte_buf_append(output, &read_cursor); |
| 142 |
read_buf->len = 0; |
| 143 |
ASSERT_TRUE(aws_input_stream_get_status(stream, &status) == 0); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
if (checksum_result) { |
| 148 |
struct aws_byte_cursor checksum_cursor = aws_s3_upload_request_checksum_context_get_checksum_cursor(context); |
| 149 |
if (aws_byte_buf_init_copy_from_cursor(checksum_result, allocator, checksum_cursor) != AWS_OP_SUCCESS) { |
| 150 |
aws_input_stream_release(stream); |
| 151 |
aws_s3_upload_request_checksum_context_release(context); |
| 152 |
return AWS_OP_ERR; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
aws_input_stream_release(stream); |
| 157 |
aws_s3_upload_request_checksum_context_release(context); |
| 158 |
return AWS_OP_SUCCESS; |
| 159 |
} |
| 160 |
|
| 161 |
static int compare_chunk_stream( |
| 162 |
struct aws_allocator *allocator, |
| 163 |
const struct aws_string *pre_chunk, |
| 164 |
struct aws_byte_cursor *input, |
| 165 |
size_t buffer_size) { |
| 166 |
|
| 167 |
struct aws_byte_buf compute_chunk_output; |
| 168 |
struct aws_byte_buf stream_chunk_output; |
| 169 |
struct aws_byte_buf stream_chunk_output1; |
| 170 |
struct aws_byte_buf streamed_encoded_checksum; |
| 171 |
struct aws_byte_buf computed_encoded_checksum; |
| 172 |
size_t len_no_checksum = pre_chunk->len + input->len + s_final_chunk->len + s_post_trailer->len + s_colon->len; |
| 173 |
size_t encoded_len = 0; |
| 174 |
struct aws_byte_buf read_buf; |
| 175 |
aws_byte_buf_init(&read_buf, allocator, buffer_size); |
| 176 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_checksum_algo_priority_list); i++) { |
| 177 |
AWS_ZERO_STRUCT(streamed_encoded_checksum); |
| 178 |
enum aws_s3_checksum_algorithm algorithm = s_checksum_algo_priority_list[i]; |
| 179 |
aws_base64_compute_encoded_len(aws_get_digest_size_from_checksum_algorithm(algorithm), &encoded_len); |
| 180 |
size_t total_len = |
| 181 |
len_no_checksum + encoded_len + aws_get_http_header_name_from_checksum_algorithm(algorithm).len; |
| 182 |
aws_byte_buf_init(&computed_encoded_checksum, allocator, encoded_len); |
| 183 |
aws_byte_buf_init(&compute_chunk_output, allocator, total_len); |
| 184 |
aws_byte_buf_init(&stream_chunk_output, allocator, total_len); |
| 185 |
aws_byte_buf_init(&stream_chunk_output1, allocator, total_len); |
| 186 |
ASSERT_SUCCESS(s_compute_chunk_stream( |
| 187 |
allocator, pre_chunk, input, &compute_chunk_output, algorithm, &computed_encoded_checksum)); |
| 188 |
ASSERT_SUCCESS(s_stream_chunk(allocator, input, &read_buf, &stream_chunk_output, algorithm, NULL)); |
| 189 |
ASSERT_SUCCESS( |
| 190 |
s_stream_chunk(allocator, input, &read_buf, &stream_chunk_output1, algorithm, &streamed_encoded_checksum)); |
| 191 |
ASSERT_TRUE(aws_byte_buf_eq(&compute_chunk_output, &stream_chunk_output)); |
| 192 |
ASSERT_TRUE(aws_byte_buf_eq(&compute_chunk_output, &stream_chunk_output1)); |
| 193 |
ASSERT_TRUE(aws_byte_buf_eq(&computed_encoded_checksum, &streamed_encoded_checksum)); |
| 194 |
aws_byte_buf_clean_up(&compute_chunk_output); |
| 195 |
aws_byte_buf_clean_up(&stream_chunk_output); |
| 196 |
aws_byte_buf_clean_up(&stream_chunk_output1); |
| 197 |
aws_byte_buf_clean_up(&streamed_encoded_checksum); |
| 198 |
aws_byte_buf_clean_up(&computed_encoded_checksum); |
| 199 |
} |
| 200 |
aws_byte_buf_clean_up(&read_buf); |
| 201 |
return AWS_OP_SUCCESS; |
| 202 |
} |
| 203 |
|
| 204 |
static int s_verify_checksum_stream_fn(struct aws_allocator *allocator, void *ctx) { |
| 205 |
(void)ctx; |
| 206 |
|
| 207 |
aws_s3_library_init(allocator); |
| 208 |
struct aws_byte_cursor input0 = aws_byte_cursor_from_c_str(""); |
| 209 |
struct aws_byte_cursor input1 = aws_byte_cursor_from_c_str("abc"); |
| 210 |
struct aws_byte_cursor input2 = |
| 211 |
aws_byte_cursor_from_c_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
| 212 |
struct aws_byte_cursor input3 = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 213 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 214 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 215 |
struct aws_byte_cursor input4 = aws_byte_cursor_from_c_str("Hello world"); |
| 216 |
for (size_t buffer_size = 1; buffer_size < input0.len + 3; buffer_size++) { |
| 217 |
ASSERT_SUCCESS(compare_checksum_stream(allocator, &input0, buffer_size)); |
| 218 |
} |
| 219 |
for (size_t buffer_size = 1; buffer_size < input1.len + 3; buffer_size++) { |
| 220 |
ASSERT_SUCCESS(compare_checksum_stream(allocator, &input1, buffer_size)); |
| 221 |
} |
| 222 |
for (size_t buffer_size = 1; buffer_size < input2.len + 3; buffer_size++) { |
| 223 |
ASSERT_SUCCESS(compare_checksum_stream(allocator, &input2, buffer_size)); |
| 224 |
} |
| 225 |
for (size_t buffer_size = 1; buffer_size < input3.len + 3; buffer_size++) { |
| 226 |
ASSERT_SUCCESS(compare_checksum_stream(allocator, &input3, buffer_size)); |
| 227 |
} |
| 228 |
for (size_t buffer_size = 1; buffer_size < input4.len + 3; buffer_size++) { |
| 229 |
ASSERT_SUCCESS(compare_checksum_stream(allocator, &input4, buffer_size)); |
| 230 |
} |
| 231 |
aws_s3_library_clean_up(); |
| 232 |
return AWS_OP_SUCCESS; |
| 233 |
} |
| 234 |
AWS_TEST_CASE(verify_checksum_stream, s_verify_checksum_stream_fn) |
| 235 |
|
| 236 |
static int s_verify_chunk_stream_fn(struct aws_allocator *allocator, void *ctx) { |
| 237 |
(void)ctx; |
| 238 |
|
| 239 |
aws_s3_library_init(allocator); |
| 240 |
struct aws_byte_cursor input0 = aws_byte_cursor_from_c_str(""); |
| 241 |
struct aws_byte_cursor input1 = aws_byte_cursor_from_c_str("abc"); |
| 242 |
struct aws_byte_cursor input2 = |
| 243 |
aws_byte_cursor_from_c_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
| 244 |
struct aws_byte_cursor input3 = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 245 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 246 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 247 |
struct aws_byte_cursor input4 = aws_byte_cursor_from_c_str("Hello world"); |
| 248 |
for (size_t buffer_size = 1; buffer_size < input0.len + 70; buffer_size++) { |
| 249 |
ASSERT_SUCCESS(compare_chunk_stream(allocator, s_0pre_chunk, &input0, buffer_size)); |
| 250 |
} |
| 251 |
for (size_t buffer_size = 1; buffer_size < input1.len + 70; buffer_size++) { |
| 252 |
ASSERT_SUCCESS(compare_chunk_stream(allocator, s_3pre_chunk, &input1, buffer_size)); |
| 253 |
} |
| 254 |
for (size_t buffer_size = 1; buffer_size < input2.len + 70; buffer_size++) { |
| 255 |
ASSERT_SUCCESS(compare_chunk_stream(allocator, s_56pre_chunk, &input2, buffer_size)); |
| 256 |
} |
| 257 |
for (size_t buffer_size = 1; buffer_size < input3.len + 70; buffer_size++) { |
| 258 |
ASSERT_SUCCESS(compare_chunk_stream(allocator, s_112pre_chunk, &input3, buffer_size)); |
| 259 |
} |
| 260 |
for (size_t buffer_size = 1; buffer_size < input4.len + 70; buffer_size++) { |
| 261 |
ASSERT_SUCCESS(compare_chunk_stream(allocator, s_11pre_chunk, &input4, buffer_size)); |
| 262 |
} |
| 263 |
aws_s3_library_clean_up(); |
| 264 |
return AWS_OP_SUCCESS; |
| 265 |
} |
| 266 |
AWS_TEST_CASE(verify_chunk_stream, s_verify_chunk_stream_fn) |