Debian Codegraph @

crt/aws-c-s3/tests/s3_data_plane_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/s3/private/s3_checksums.h"
7 #include "aws/s3/private/s3_client_impl.h"
8 #include "aws/s3/private/s3_default_buffer_pool.h"
9 #include "aws/s3/private/s3_meta_request_impl.h"
10 #include "aws/s3/private/s3_util.h"
11 #include "aws/s3/s3_client.h"
12 #include "s3_tester.h"
13 #include <aws/checksums/crc.h>
14 #include <aws/common/byte_buf.h>
15 #include <aws/common/clock.h>
16 #include <aws/common/common.h>
17 #include <aws/common/encoding.h>
18 #include <aws/common/environment.h>
19 #include <aws/common/ref_count.h>
20 #include <aws/http/request_response.h>
21 #include <aws/http/status_code.h>
22 #include <aws/io/channel_bootstrap.h>
23 #include <aws/io/event_loop.h>
24 #include <aws/io/host_resolver.h>
25 #include <aws/io/stream.h>
26 #include <aws/io/tls_channel_handler.h>
27 #include <aws/io/uri.h>
28 #include <aws/testing/aws_test_harness.h>
29 #include <aws/testing/stream_tester.h>
30 #include <inttypes.h>
31
32 /* Forward declaration for checksum validation callback used in O_DIRECT tests */
33 void s_s3_test_validate_checksum(
34 struct aws_s3_meta_request *meta_request,
35 const struct aws_s3_meta_request_result *result,
36 void *user_data);
37
38 AWS_TEST_CASE(test_s3_client_create_destroy, s_test_s3_client_create_destroy)
39 static int s_test_s3_client_create_destroy(struct aws_allocator *allocator, void *ctx) {
40 (void)ctx;
41
42 struct aws_s3_tester tester;
43 AWS_ZERO_STRUCT(tester);
44 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
45
46 struct aws_s3_client_config client_config;
47 AWS_ZERO_STRUCT(client_config);
48
49 ASSERT_SUCCESS(aws_s3_tester_bind_client(
50 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
51
52 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
53
54 ASSERT_TRUE(client != NULL);
55
56 client = aws_s3_client_release(client);
57
58 aws_s3_tester_clean_up(&tester);
59
60 return 0;
61 }
62
63 AWS_TEST_CASE(test_s3_client_create_error, s_test_s3_client_create_error)
64 static int s_test_s3_client_create_error(struct aws_allocator *allocator, void *ctx) {
65 (void)ctx;
66
67 struct aws_s3_tester tester;
68 AWS_ZERO_STRUCT(tester);
69 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
70
71 struct aws_s3_client_config client_config;
72 AWS_ZERO_STRUCT(client_config);
73 struct aws_http_proxy_options proxy_options = {
74 .connection_type = AWS_HPCT_HTTP_LEGACY,
75 .host = aws_byte_cursor_from_c_str("localhost"),
76 .port = 8899,
77 };
78 client_config.proxy_options = &proxy_options;
79 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
80
81 ASSERT_TRUE(client == NULL);
82
83 aws_s3_tester_clean_up(&tester);
84
85 return 0;
86 }
87
88 AWS_TEST_CASE(
89 test_s3_client_create_error_with_invalid_memory_limit_config,
90 s_test_s3_client_create_error_with_invalid_memory_limit_config)
91 static int s_test_s3_client_create_error_with_invalid_memory_limit_config(struct aws_allocator *allocator, void *ctx) {
92 (void)ctx;
93
94 struct aws_s3_tester tester;
95 AWS_ZERO_STRUCT(tester);
96 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
97
98 struct aws_s3_client_config client_config;
99 AWS_ZERO_STRUCT(client_config);
100 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION));
101
102 client_config.memory_limit_in_bytes = 100;
103 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
104 ASSERT_TRUE(client == NULL);
105 client_config.memory_limit_in_bytes = GB_TO_BYTES(1);
106 client_config.max_part_size = GB_TO_BYTES(2);
107 client = aws_s3_client_new(allocator, &client_config);
108 ASSERT_TRUE(client == NULL);
109
110 tester.bound_to_client = false;
111 aws_s3_tester_clean_up(&tester);
112 return 0;
113 }
114
115 AWS_TEST_CASE(
116 test_s3_client_create_error_with_invalid_network_interface,
117 s_test_s3_client_create_error_with_invalid_network_interface)
118 static int s_test_s3_client_create_error_with_invalid_network_interface(struct aws_allocator *allocator, void *ctx) {
119 (void)ctx;
120
121 struct aws_s3_tester tester;
122 AWS_ZERO_STRUCT(tester);
123 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
124
125 struct aws_s3_client_config client_config;
126 AWS_ZERO_STRUCT(client_config);
127 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION));
128
129 struct aws_byte_cursor *interface_names_array = aws_mem_calloc(allocator, 1, sizeof(struct aws_byte_cursor));
130 interface_names_array[0] = aws_byte_cursor_from_c_str("invalid-nic");
131 client_config.network_interface_names_array = interface_names_array;
132 client_config.num_network_interface_names = 1;
133
134 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
135 ASSERT_TRUE(client == NULL);
136 tester.bound_to_client = false;
137
138 aws_s3_tester_clean_up(&tester);
139 aws_mem_release(allocator, interface_names_array);
140 return 0;
141 }
142
143 AWS_TEST_CASE(test_s3_client_monitoring_options_override, s_test_s3_client_monitoring_options_override)
144 static int s_test_s3_client_monitoring_options_override(struct aws_allocator *allocator, void *ctx) {
145 (void)ctx;
146
147 struct aws_s3_tester tester;
148 AWS_ZERO_STRUCT(tester);
149 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
150
151 struct aws_http_connection_monitoring_options monitoring_options = {.minimum_throughput_bytes_per_second = 3000};
152
153 struct aws_s3_client_config client_config = {.monitoring_options = &monitoring_options};
154
155 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION));
156
157 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
158
159 ASSERT_TRUE(
160 client->monitoring_options.minimum_throughput_bytes_per_second ==
161 client_config.monitoring_options->minimum_throughput_bytes_per_second);
162
163 aws_s3_client_release(client);
164 aws_s3_tester_clean_up(&tester);
165
166 return 0;
167 }
168
169 AWS_TEST_CASE(test_s3_client_proxy_ev_settings_override, s_test_s3_client_proxy_ev_settings_override)
170 static int s_test_s3_client_proxy_ev_settings_override(struct aws_allocator *allocator, void *ctx) {
171 (void)ctx;
172
173 struct aws_s3_tester tester;
174 AWS_ZERO_STRUCT(tester);
175 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
176 struct aws_tls_connection_options tls_conn_options;
177 AWS_ZERO_STRUCT(tls_conn_options);
178
179 struct proxy_env_var_settings proxy_ev_settings = {
180 .env_var_type = AWS_HPEV_ENABLE,
181 .tls_options = &tls_conn_options,
182 };
183
184 struct aws_s3_client_config client_config = {.proxy_ev_settings = &proxy_ev_settings};
185
186 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, 0));
187
188 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
189
190 ASSERT_TRUE(client->proxy_ev_settings->env_var_type == client_config.proxy_ev_settings->env_var_type);
191
192 aws_s3_client_release(client);
193 aws_s3_tester_clean_up(&tester);
194
195 return 0;
196 }
197
198 AWS_TEST_CASE(test_s3_client_tcp_keep_alive_options_override, s_test_s3_client_tcp_keep_alive_options_override)
199 static int s_test_s3_client_tcp_keep_alive_options_override(struct aws_allocator *allocator, void *ctx) {
200 (void)ctx;
201
202 struct aws_s3_tester tester;
203 AWS_ZERO_STRUCT(tester);
204 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
205
206 struct aws_s3_tcp_keep_alive_options keep_alive_options = {.keep_alive_interval_sec = 20};
207
208 struct aws_s3_client_config client_config = {.tcp_keep_alive_options = &keep_alive_options};
209
210 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, 0));
211
212 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
213
214 ASSERT_TRUE(
215 client->tcp_keep_alive_options->keep_alive_interval_sec ==
216 client_config.tcp_keep_alive_options->keep_alive_interval_sec);
217
218 aws_s3_client_release(client);
219 aws_s3_tester_clean_up(&tester);
220
221 return 0;
222 }
223
224 AWS_TEST_CASE(test_s3_client_max_active_connections_override, s_test_s3_client_max_active_connections_override)
225 static int s_test_s3_client_max_active_connections_override(struct aws_allocator *allocator, void *ctx) {
226 (void)ctx;
227
228 struct aws_s3_tester tester;
229 AWS_ZERO_STRUCT(tester);
230 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
231
232 struct aws_s3_client_config client_config = {
233 .max_active_connections_override = 10,
234 };
235
236 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, 0));
237
238 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
239
240 ASSERT_TRUE(client->max_active_connections_override == client_config.max_active_connections_override);
241
242 aws_s3_client_release(client);
243 aws_s3_tester_clean_up(&tester);
244
245 return 0;
246 }
247
248 AWS_TEST_CASE(test_s3_client_byo_crypto_no_options, s_test_s3_client_byo_crypto_no_options)
249 static int s_test_s3_client_byo_crypto_no_options(struct aws_allocator *allocator, void *ctx) {
250 (void)ctx;
251
252 struct aws_s3_tester tester;
253 AWS_ZERO_STRUCT(tester);
254 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
255
256 struct aws_s3_client_config client_config = {
257 .tls_mode = AWS_MR_TLS_ENABLED,
258 };
259
260 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
261
262 ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_ARGUMENT);
263 ASSERT_TRUE(client == NULL);
264
265 aws_s3_tester_clean_up(&tester);
266
267 return 0;
268 }
269
270 AWS_TEST_CASE(test_s3_client_byo_crypto_with_options, s_test_s3_client_byo_crypto_with_options)
271 static int s_test_s3_client_byo_crypto_with_options(struct aws_allocator *allocator, void *ctx) {
272 (void)ctx;
273
274 struct aws_s3_tester tester;
275 AWS_ZERO_STRUCT(tester);
276 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
277
278 struct aws_tls_connection_options tls_conn_options;
279 AWS_ZERO_STRUCT(tls_conn_options);
280
281 struct aws_s3_client_config client_config;
282 AWS_ZERO_STRUCT(client_config);
283 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, 0));
284
285 client_config.tls_mode = AWS_MR_TLS_ENABLED;
286 client_config.tls_connection_options = &tls_conn_options;
287
288 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
289
290 ASSERT_TRUE(client != NULL);
291
292 aws_s3_client_release(client);
293 aws_s3_tester_clean_up(&tester);
294
295 return 0;
296 }
297
298 size_t s_test_max_active_connections_host_count = 0;
299
300 size_t s_test_get_max_active_connections_host_address_count(
301 struct aws_host_resolver *host_resolver,
302 const struct aws_string *host_name,
303 uint32_t flags) {
304 (void)host_resolver;
305 (void)host_name;
306 (void)flags;
307
308 return s_test_max_active_connections_host_count;
309 }
310
311 AWS_TEST_CASE(test_s3_client_get_max_active_connections, s_test_s3_client_get_max_active_connections)
312 static int s_test_s3_client_get_max_active_connections(struct aws_allocator *allocator, void *ctx) {
313 (void)allocator;
314 (void)ctx;
315
316 struct aws_s3_tester tester;
317 AWS_ZERO_STRUCT(tester);
318 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
319
320 struct aws_client_bootstrap mock_client_bootstrap;
321 AWS_ZERO_STRUCT(mock_client_bootstrap);
322
323 struct aws_s3_client *mock_client = aws_s3_tester_mock_client_new(&tester);
324 *((uint32_t *)&mock_client->max_active_connections_override) = 0;
325 *((uint32_t *)&mock_client->ideal_connection_count) = 100;
326 mock_client->client_bootstrap = &mock_client_bootstrap;
327 mock_client->vtable->get_host_address_count = s_test_get_max_active_connections_host_address_count;
328
329 struct aws_s3_meta_request *mock_meta_requests[AWS_S3_META_REQUEST_TYPE_MAX];
330
331 for (size_t i = 0; i < AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
332 /* Setup test data. */
333 mock_meta_requests[i] = aws_s3_tester_mock_meta_request_new(&tester);
334 mock_meta_requests[i]->type = i;
335 mock_meta_requests[i]->endpoint = aws_s3_tester_mock_endpoint_new(&tester);
336 }
337
338 s_test_max_active_connections_host_count = 2;
339
340 /* Behavior should not be affected by max_active_connections_override since it is 0, and should just be in relation
341 * to ideal-connection-count. */
342 {
343 ASSERT_TRUE(aws_s3_client_get_max_active_connections(mock_client, NULL) == mock_client->ideal_connection_count);
344
345 for (size_t i = 0; i < AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
346 ASSERT_TRUE(
347 aws_s3_client_get_max_active_connections(mock_client, mock_meta_requests[i]) ==
348 mock_client->ideal_connection_count);
349 }
350 }
351
352 /* Max active connections override should now cap the calculated amount of active connections. */
353 {
354 *((uint32_t *)&mock_client->max_active_connections_override) = 3;
355
356 /* Assert that override is low enough to have effect */
357 ASSERT_TRUE(mock_client->max_active_connections_override < mock_client->ideal_connection_count);
358
359 ASSERT_TRUE(
360 aws_s3_client_get_max_active_connections(mock_client, NULL) ==
361 mock_client->max_active_connections_override);
362
363 for (size_t i = 0; i < AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
364 ASSERT_TRUE(mock_client->max_active_connections_override < mock_client->ideal_connection_count);
365
366 ASSERT_TRUE(
367 aws_s3_client_get_max_active_connections(mock_client, mock_meta_requests[i]) ==
368 mock_client->max_active_connections_override);
369 }
370 }
371
372 /* Max active connections override should be ignored since the calculated amount of max connections is less. */
373 {
374 *((uint32_t *)&mock_client->max_active_connections_override) = 100000;
375
376 /* Assert that override is NOT low enough to have effect */
377 ASSERT_TRUE(mock_client->max_active_connections_override > mock_client->ideal_connection_count);
378
379 ASSERT_TRUE(aws_s3_client_get_max_active_connections(mock_client, NULL) == mock_client->ideal_connection_count);
380
381 for (size_t i = 0; i < AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
382 ASSERT_TRUE(mock_client->max_active_connections_override > mock_client->ideal_connection_count);
383
384 ASSERT_TRUE(
385 aws_s3_client_get_max_active_connections(mock_client, mock_meta_requests[i]) ==
386 mock_client->ideal_connection_count);
387 }
388 }
389
390 for (size_t i = 0; i < AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
391 mock_meta_requests[i] = aws_s3_meta_request_release(mock_meta_requests[i]);
392 }
393
394 aws_s3_client_release(mock_client);
395 aws_s3_tester_clean_up(&tester);
396
397 return 0;
398 }
399
400 AWS_TEST_CASE(test_s3_request_create_destroy, s_test_s3_request_create_destroy)
401 static int s_test_s3_request_create_destroy(struct aws_allocator *allocator, void *ctx) {
402 (void)ctx;
403
404 const int request_tag = 1234;
405 const enum aws_s3_request_type request_type = AWS_S3_REQUEST_TYPE_LIST_PARTS;
406 const uint32_t part_number = 5678;
407
408 struct aws_s3_tester tester;
409 AWS_ZERO_STRUCT(tester);
410 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
411
412 struct aws_s3_meta_request *meta_request = aws_s3_tester_mock_meta_request_new(&tester);
413 ASSERT_TRUE(meta_request != NULL);
414
415 struct aws_s3_client *client = aws_s3_tester_mock_client_new(&tester);
416 ASSERT_TRUE(client != NULL);
417 meta_request->client = aws_s3_client_acquire(client);
418
419 struct aws_http_message *request_message = aws_s3_tester_dummy_http_request_new(&tester);
420 ASSERT_TRUE(request_message != NULL);
421
422 struct aws_s3_request *request = aws_s3_request_new(
423 meta_request, request_tag, request_type, part_number, AWS_S3_REQUEST_FLAG_RECORD_RESPONSE_HEADERS);
424
425 ASSERT_TRUE(request != NULL);
426
427 ASSERT_TRUE(request->meta_request == meta_request);
428 ASSERT_TRUE(request->part_number == part_number);
429 ASSERT_TRUE(request->request_tag == request_tag);
430 ASSERT_TRUE(request->request_type == request_type);
431 ASSERT_STR_EQUALS("ListParts", aws_string_c_str(request->operation_name));
432 ASSERT_TRUE(request->record_response_headers == true);
433
434 aws_s3_request_setup_send_data(request, request_message);
435
436 ASSERT_TRUE(request->send_data.message != NULL);
437 ASSERT_TRUE(request->send_data.response_headers == NULL);
438
439 request->send_data.response_headers = aws_http_headers_new(allocator);
440 ASSERT_TRUE(request->send_data.response_headers != NULL);
441 ASSERT_TRUE(request->send_data.metrics != NULL);
442 request->send_data.metrics = aws_s3_request_metrics_release(request->send_data.metrics);
443
444 aws_s3_request_clean_up_send_data(request);
445
446 ASSERT_TRUE(request->send_data.message == NULL);
447 ASSERT_TRUE(request->send_data.response_headers == NULL);
448 ASSERT_TRUE(request->send_data.response_status == 0);
449
450 aws_s3_request_release(request);
451 aws_http_message_release(request_message);
452 aws_s3_meta_request_release(meta_request);
453 aws_s3_client_release(client);
454
455 aws_s3_tester_clean_up(&tester);
456
457 return 0;
458 }
459
460 struct s3_test_body_streaming_user_data {
461 struct aws_s3_tester *tester;
462 struct aws_allocator *allocator;
463 uint64_t expected_range_start;
464 uint64_t received_body_size;
465 };
466
467 static int s_s3_meta_request_test_body_streaming_callback(
468 struct aws_s3_meta_request *meta_request,
469 const struct aws_byte_cursor *body,
470 uint64_t range_start,
471 void *user_data) {
472 (void)meta_request;
473 (void)body;
474 (void)range_start;
475
476 struct s3_test_body_streaming_user_data *body_streaming_user_data = user_data;
477
478 body_streaming_user_data->received_body_size += body->len;
479
480 ASSERT_TRUE(body_streaming_user_data->expected_range_start == range_start);
481 body_streaming_user_data->expected_range_start += body->len;
482
483 aws_s3_tester_inc_counter1(body_streaming_user_data->tester);
484
485 return AWS_OP_SUCCESS;
486 }
487
488 /* Test the meta request body streaming functionality. */
489 AWS_TEST_CASE(test_s3_meta_request_body_streaming, s_test_s3_meta_request_body_streaming)
490 static int s_test_s3_meta_request_body_streaming(struct aws_allocator *allocator, void *ctx) {
491 (void)ctx;
492
493 const uint32_t part_range0_start = 1;
494 const uint32_t part_range0_end = part_range0_start + 4;
495
496 const uint32_t part_range1_start = part_range0_end + 1;
497 const uint32_t part_range1_end = part_range1_start + 4;
498
499 const size_t request_response_body_size = 16;
500
501 const uint64_t total_object_size = (uint64_t)part_range1_end * request_response_body_size;
502
503 struct aws_byte_buf response_body_source_buffer;
504 aws_byte_buf_init(&response_body_source_buffer, allocator, request_response_body_size);
505
506 const struct aws_byte_cursor test_byte_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("0");
507
508 for (size_t i = 0; i < request_response_body_size; ++i) {
509 aws_byte_buf_append(&response_body_source_buffer, &test_byte_cursor);
510 }
511
512 struct aws_s3_tester tester;
513 AWS_ZERO_STRUCT(tester);
514 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
515
516 struct s3_test_body_streaming_user_data body_streaming_user_data = {
517 .tester = &tester,
518 };
519
520 struct aws_s3_client *mock_client = aws_s3_tester_mock_client_new(&tester);
521
522 struct aws_s3_meta_request *meta_request = aws_s3_tester_mock_meta_request_new(&tester);
523 ASSERT_TRUE(meta_request != NULL);
524
525 struct aws_event_loop_group *event_loop_group = aws_event_loop_group_new_default(allocator, 0, NULL);
526 meta_request->client = aws_s3_client_acquire(mock_client);
527 meta_request->user_data = &body_streaming_user_data;
528 *((size_t *)&meta_request->part_size) = request_response_body_size;
529 meta_request->body_callback = s_s3_meta_request_test_body_streaming_callback;
530 meta_request->io_event_loop = aws_event_loop_group_get_next_loop(event_loop_group);
531
532 /* Queue the first range of parts in order. Each part should be flushed one-by-one. */
533 {
534 for (uint32_t part_number = part_range0_start; part_number <= part_range0_end; ++part_number) {
535 struct aws_s3_request *request = aws_s3_request_new(
536 meta_request, 0 /*request_tag*/, AWS_S3_REQUEST_TYPE_GET_OBJECT, part_number, 0 /*flags*/);
537
538 aws_s3_calculate_auto_ranged_get_part_range(
539 0ULL,
540 total_object_size - 1,
541 request_response_body_size /*part_size*/,
542 (uint64_t)request_response_body_size /*first_part_size*/,
543 part_number,
544 &request->part_range_start,
545 &request->part_range_end);
546
547 aws_byte_buf_init_copy(&request->send_data.response_body, allocator, &response_body_source_buffer);
548
549 aws_s3_tester_set_counter1_desired(&tester, part_number);
550
551 aws_s3_meta_request_lock_synced_data(meta_request);
552
553 aws_s3_meta_request_stream_response_body_synced(meta_request, request);
554 ASSERT_TRUE(aws_priority_queue_size(&meta_request->synced_data.pending_body_streaming_requests) == 0);
555
556 aws_s3_meta_request_unlock_synced_data(meta_request);
557
558 aws_s3_tester_wait_for_counters(&tester);
559
560 aws_s3_request_release(request);
561 }
562 }
563
564 aws_s3_tester_set_counter1_desired(&tester, part_range1_end);
565
566 /* Queue parts for second range, but skip over the first part.*/
567 {
568 uint32_t num_parts_queued = 0;
569
570 ASSERT_TRUE(part_range1_start != part_range1_end);
571
572 for (uint32_t part_number = part_range1_start + 1; part_number <= part_range1_end; ++part_number) {
573 struct aws_s3_request *request = aws_s3_request_new(
574 meta_request, 0 /*request_tag*/, AWS_S3_REQUEST_TYPE_GET_OBJECT, part_number, 0 /*flags*/);
575
576 aws_s3_calculate_auto_ranged_get_part_range(
577 0ULL,
578 total_object_size - 1,
579 request_response_body_size /*part_size*/,
580 (uint64_t)request_response_body_size /*first_part_size*/,
581 part_number,
582 &request->part_range_start,
583 &request->part_range_end);
584
585 aws_byte_buf_init_copy(&request->send_data.response_body, allocator, &response_body_source_buffer);
586
587 aws_s3_meta_request_lock_synced_data(meta_request);
588 aws_s3_meta_request_stream_response_body_synced(meta_request, request);
589 aws_s3_meta_request_unlock_synced_data(meta_request);
590
591 aws_s3_request_release(request);
592 ++num_parts_queued;
593 }
594
595 aws_s3_meta_request_lock_synced_data(meta_request);
596 ASSERT_TRUE(
597 aws_priority_queue_size(&meta_request->synced_data.pending_body_streaming_requests) == num_parts_queued);
598 aws_s3_meta_request_unlock_synced_data(meta_request);
599 }
600
601 /* Stream the last part of the body, which should flush the priority queue. */
602 {
603 struct aws_s3_request *request = aws_s3_request_new(
604 meta_request, 0 /*request_tag*/, AWS_S3_REQUEST_TYPE_GET_OBJECT, part_range1_start, 0 /*flags*/);
605
606 aws_s3_calculate_auto_ranged_get_part_range(
607 0ULL,
608 total_object_size - 1,
609 request_response_body_size /*part_size*/,
610 (uint64_t)request_response_body_size /*first_part_size*/,
611 part_range1_start,
612 &request->part_range_start,
613 &request->part_range_end);
614
615 aws_byte_buf_init_copy(&request->send_data.response_body, allocator, &response_body_source_buffer);
616
617 aws_s3_meta_request_lock_synced_data(meta_request);
618 aws_s3_meta_request_stream_response_body_synced(meta_request, request);
619 aws_s3_meta_request_unlock_synced_data(meta_request);
620
621 aws_s3_meta_request_lock_synced_data(meta_request);
622 ASSERT_TRUE(aws_priority_queue_size(&meta_request->synced_data.pending_body_streaming_requests) == 0);
623 aws_s3_meta_request_unlock_synced_data(meta_request);
624
625 aws_s3_request_release(request);
626
627 aws_s3_tester_wait_for_counters(&tester);
628 }
629
630 ASSERT_TRUE(body_streaming_user_data.received_body_size == (request_response_body_size * part_range1_end));
631
632 aws_s3_meta_request_release(meta_request);
633 aws_s3_client_release(mock_client);
634 aws_event_loop_group_release(event_loop_group);
635 aws_byte_buf_clean_up(&response_body_source_buffer);
636 aws_s3_tester_clean_up(&tester);
637
638 return 0;
639 }
640
641 /* Test aws_s3_client_queue_requests_threaded and aws_s3_client_dequeue_request_threaded */
642 AWS_TEST_CASE(test_s3_client_queue_requests, s_test_s3_client_queue_requests)
643 static int s_test_s3_client_queue_requests(struct aws_allocator *allocator, void *ctx) {
644 (void)ctx;
645
646 struct aws_s3_tester tester;
647 aws_s3_tester_init(allocator, &tester);
648
649 struct aws_s3_client *mock_client = aws_s3_tester_mock_client_new(&tester);
650 aws_linked_list_init(&mock_client->threaded_data.request_queue);
651
652 struct aws_s3_meta_request *mock_meta_request = aws_s3_tester_mock_meta_request_new(&tester);
653 mock_meta_request->client = aws_s3_client_acquire(mock_client);
654
655 struct aws_s3_request *pivot_request = aws_s3_request_new(mock_meta_request, 0, 0, 0, 0);
656
657 struct aws_linked_list pivot_request_list;
658 aws_linked_list_init(&pivot_request_list);
659
660 struct aws_s3_request *requests[] = {
661 aws_s3_request_new(mock_meta_request, 0, 0, 0, 0),
662 aws_s3_request_new(mock_meta_request, 0, 0, 0, 0),
663 aws_s3_request_new(mock_meta_request, 0, 0, 0, 0),
664 };
665
666 const uint32_t num_requests = AWS_ARRAY_SIZE(requests);
667
668 struct aws_linked_list request_list;
669 aws_linked_list_init(&request_list);
670
671 {
672 aws_linked_list_push_back(&pivot_request_list, &pivot_request->node);
673 aws_s3_client_queue_requests_threaded(mock_client, &pivot_request_list, false);
674
675 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == 1);
676 ASSERT_TRUE(!aws_linked_list_empty(&mock_client->threaded_data.request_queue));
677
678 for (uint32_t i = 0; i < num_requests; ++i) {
679 aws_linked_list_push_back(&request_list, &requests[i]->node);
680 }
681
682 /* Move the requests to the back of the queue. */
683 aws_s3_client_queue_requests_threaded(mock_client, &request_list, false);
684 }
685
686 ASSERT_TRUE(aws_linked_list_empty(&request_list));
687 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == (num_requests + 1));
688 ASSERT_TRUE(!aws_linked_list_empty(&mock_client->threaded_data.request_queue));
689
690 {
691 /* The first request should be the pivot request since the other requests were pushed to the back. */
692 struct aws_s3_request *first_request = aws_s3_client_dequeue_request_threaded(mock_client);
693 ASSERT_TRUE(first_request == pivot_request);
694
695 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == num_requests);
696 ASSERT_TRUE(!aws_linked_list_empty(&mock_client->threaded_data.request_queue));
697 }
698
699 for (uint32_t i = 0; i < num_requests; ++i) {
700 struct aws_s3_request *request = aws_s3_client_dequeue_request_threaded(mock_client);
701
702 ASSERT_TRUE(request == requests[i]);
703
704 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == (num_requests - (i + 1)));
705
706 if (i < num_requests - 1) {
707 ASSERT_TRUE(!aws_linked_list_empty(&mock_client->threaded_data.request_queue));
708 }
709 }
710
711 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == 0);
712 ASSERT_TRUE(aws_linked_list_empty(&mock_client->threaded_data.request_queue));
713
714 {
715 aws_linked_list_push_back(&pivot_request_list, &pivot_request->node);
716 aws_s3_client_queue_requests_threaded(mock_client, &pivot_request_list, false);
717
718 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == 1);
719 ASSERT_TRUE(!aws_linked_list_empty(&mock_client->threaded_data.request_queue));
720
721 for (uint32_t i = 0; i < num_requests; ++i) {
722 aws_linked_list_push_back(&request_list, &requests[i]->node);
723 }
724
725 /* Move the requests to the front of the queue. */
726 aws_s3_client_queue_requests_threaded(mock_client, &request_list, true);
727 }
728
729 ASSERT_TRUE(aws_linked_list_empty(&request_list));
730 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == (num_requests + 1));
731 ASSERT_TRUE(!aws_linked_list_empty(&mock_client->threaded_data.request_queue));
732
733 for (uint32_t i = 0; i < num_requests; ++i) {
734 struct aws_s3_request *request = aws_s3_client_dequeue_request_threaded(mock_client);
735
736 ASSERT_TRUE(request == requests[i]);
737 }
738
739 {
740 /* The last request should be the pivot request since the other requests were pushed to the front. */
741 struct aws_s3_request *last_request = aws_s3_client_dequeue_request_threaded(mock_client);
742 ASSERT_TRUE(last_request == pivot_request);
743 }
744
745 ASSERT_TRUE(aws_linked_list_empty(&mock_client->threaded_data.request_queue));
746 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == 0);
747
748 for (uint32_t i = 0; i < num_requests; ++i) {
749 aws_s3_request_release(requests[i]);
750 }
751
752 aws_s3_request_release(pivot_request);
753 aws_s3_meta_request_release(mock_meta_request);
754 aws_s3_client_release(mock_client);
755 aws_s3_tester_clean_up(&tester);
756
757 return 0;
758 }
759
760 struct test_work_meta_request_update_user_data {
761 bool has_work_remaining;
762 uint32_t num_prepares;
763 };
764
765 static bool s_s3_test_work_meta_request_update(
766 struct aws_s3_meta_request *meta_request,
767 uint32_t flags,
768 struct aws_s3_request **out_request) {
769 AWS_ASSERT(meta_request);
770 (void)flags;
771
772 struct test_work_meta_request_update_user_data *user_data = meta_request->user_data;
773
774 if (out_request) {
775 if (user_data->has_work_remaining) {
776 *out_request = aws_s3_request_new(meta_request, 0, 0, 0, 0);
777 }
778 }
779
780 return user_data->has_work_remaining;
781 }
782
783 static void s_s3_test_work_meta_request_schedule_prepare_request(
784 struct aws_s3_meta_request *meta_request,
785 struct aws_s3_request *request,
786 aws_s3_meta_request_prepare_request_callback_fn *callback,
787 void *user_data) {
788 (void)request;
789 (void)callback;
790 (void)user_data;
791
792 AWS_ASSERT(meta_request);
793
794 struct test_work_meta_request_update_user_data *test_user_data = meta_request->user_data;
795 ++test_user_data->num_prepares;
796
797 aws_s3_request_release(request);
798 }
799
800 static size_t s_test_s3_update_meta_request_trigger_prepare_host_address_count = 0;
801
802 static size_t s_test_s3_update_meta_request_trigger_prepare_get_host_address_count(
803 struct aws_host_resolver *host_resolver,
804 const struct aws_string *host_name,
805 uint32_t flags) {
806 (void)host_resolver;
807 (void)host_name;
808 (void)flags;
809 return s_test_s3_update_meta_request_trigger_prepare_host_address_count;
810 }
811
812 static int s_validate_prepared_requests(
813 struct aws_s3_client *client,
814 size_t expected_num_being_prepared,
815 struct aws_s3_meta_request *meta_request_with_work,
816 struct aws_s3_meta_request *meta_request_without_work) {
817
818 ASSERT_TRUE(client->threaded_data.request_queue_size == 0);
819 ASSERT_TRUE(aws_linked_list_empty(&client->threaded_data.request_queue));
820 ASSERT_TRUE(client->threaded_data.num_requests_being_prepared == expected_num_being_prepared);
821 ASSERT_TRUE(aws_atomic_load_int(&client->stats.num_requests_in_flight) == expected_num_being_prepared);
822
823 uint32_t num_meta_requests_in_list = 0;
824 bool meta_request_with_work_found = false;
825
826 for (struct aws_linked_list_node *node = aws_linked_list_begin(&client->threaded_data.meta_requests);
827 node != aws_linked_list_end(&client->threaded_data.meta_requests);
828 node = aws_linked_list_next(node)) {
829
830 struct aws_s3_meta_request *meta_request =
831 AWS_CONTAINER_OF(node, struct aws_s3_meta_request, client_process_work_threaded_data);
832
833 if (meta_request == meta_request_with_work) {
834 meta_request_with_work_found = true;
835 }
836
837 ASSERT_TRUE(meta_request != meta_request_without_work);
838
839 ++num_meta_requests_in_list;
840 }
841
842 ASSERT_TRUE(meta_request_with_work_found);
843 ASSERT_TRUE(num_meta_requests_in_list == 1);
844
845 return AWS_OP_SUCCESS;
846 }
847
848 /* Test that the client will prepare requests correctly. */
849 AWS_TEST_CASE(test_s3_update_meta_requests_trigger_prepare, s_test_s3_update_meta_requests_trigger_prepare)
850 static int s_test_s3_update_meta_requests_trigger_prepare(struct aws_allocator *allocator, void *ctx) {
851 (void)ctx;
852
853 struct aws_s3_tester tester;
854 aws_s3_tester_init(allocator, &tester);
855
856 struct aws_client_bootstrap mock_bootstrap;
857 AWS_ZERO_STRUCT(mock_bootstrap);
858
859 const uint32_t ideal_connection_count = 100;
860
861 struct aws_s3_client *mock_client = aws_s3_tester_mock_client_new(&tester);
862 mock_client->client_bootstrap = &mock_bootstrap;
863 mock_client->vtable->get_host_address_count = s_test_s3_update_meta_request_trigger_prepare_get_host_address_count;
864 *((uint32_t *)&mock_client->ideal_connection_count) = ideal_connection_count;
865 aws_linked_list_init(&mock_client->threaded_data.request_queue);
866 aws_linked_list_init(&mock_client->threaded_data.meta_requests);
867
868 struct aws_s3_meta_request *mock_meta_request_without_work = aws_s3_tester_mock_meta_request_new(&tester);
869 mock_meta_request_without_work->client = aws_s3_client_acquire(mock_client);
870 mock_meta_request_without_work->endpoint = aws_s3_tester_mock_endpoint_new(&tester);
871
872 struct test_work_meta_request_update_user_data mock_meta_request_without_work_data = {
873 .has_work_remaining = false,
874 };
875
876 mock_meta_request_without_work->user_data = &mock_meta_request_without_work_data;
877
878 struct aws_s3_meta_request_vtable *meta_request_without_work_vtable =
879 aws_s3_tester_patch_meta_request_vtable(&tester, mock_meta_request_without_work, NULL);
880 meta_request_without_work_vtable->update = s_s3_test_work_meta_request_update;
881 meta_request_without_work_vtable->schedule_prepare_request = s_s3_test_work_meta_request_schedule_prepare_request;
882
883 /* Intentionally push this meta request first to test that it's properly removed from the list. */
884 aws_linked_list_push_back(
885 &mock_client->threaded_data.meta_requests,
886 &mock_meta_request_without_work->client_process_work_threaded_data.node);
887
888 aws_s3_meta_request_acquire(mock_meta_request_without_work);
889
890 struct aws_s3_meta_request *mock_meta_request_with_work = aws_s3_tester_mock_meta_request_new(&tester);
891 mock_meta_request_with_work->client = aws_s3_client_acquire(mock_client);
892 struct test_work_meta_request_update_user_data mock_meta_request_with_work_data = {
893 .has_work_remaining = true,
894 };
895
896 mock_meta_request_with_work->endpoint = aws_s3_tester_mock_endpoint_new(&tester);
897 mock_meta_request_with_work->user_data = &mock_meta_request_with_work_data;
898
899 struct aws_s3_meta_request_vtable *mock_meta_request_with_work_vtable =
900 aws_s3_tester_patch_meta_request_vtable(&tester, mock_meta_request_with_work, NULL);
901 mock_meta_request_with_work_vtable->update = s_s3_test_work_meta_request_update;
902 mock_meta_request_with_work_vtable->schedule_prepare_request = s_s3_test_work_meta_request_schedule_prepare_request;
903
904 aws_linked_list_push_back(
905 &mock_client->threaded_data.meta_requests,
906 &mock_meta_request_with_work->client_process_work_threaded_data.node);
907 aws_s3_meta_request_acquire(mock_meta_request_with_work);
908
909 /* With no known addresses, the amount of requests that can be prepared should be lower. */
910 {
911 s_test_s3_update_meta_request_trigger_prepare_host_address_count = 0;
912 aws_s3_client_update_meta_requests_threaded(mock_client);
913
914 ASSERT_SUCCESS(s_validate_prepared_requests(
915 mock_client, g_min_num_connections, mock_meta_request_with_work, mock_meta_request_without_work));
916 }
917
918 /* When the number of known addresses is 1+, the max number of requests should be reached. */
919 {
920 const uint32_t max_requests_prepare = aws_s3_client_get_max_requests_prepare(mock_client);
921
922 s_test_s3_update_meta_request_trigger_prepare_host_address_count = 1;
923 aws_s3_client_update_meta_requests_threaded(mock_client);
924
925 ASSERT_SUCCESS(s_validate_prepared_requests(
926 mock_client, max_requests_prepare, mock_meta_request_with_work, mock_meta_request_without_work));
927 }
928
929 while (!aws_linked_list_empty(&mock_client->threaded_data.meta_requests)) {
930 struct aws_linked_list_node *meta_request_node =
931 aws_linked_list_pop_front(&mock_client->threaded_data.meta_requests);
932
933 struct aws_s3_meta_request *meta_request =
934 AWS_CONTAINER_OF(meta_request_node, struct aws_s3_meta_request, client_process_work_threaded_data);
935
936 aws_s3_meta_request_release(meta_request);
937 }
938
939 aws_s3_meta_request_release(mock_meta_request_without_work);
940 aws_s3_meta_request_release(mock_meta_request_with_work);
941 aws_s3_client_release(mock_client);
942 aws_s3_tester_clean_up(&tester);
943 return 0;
944 }
945
946 struct s3_test_update_connections_finish_result_user_data {
947 struct aws_s3_request *finished_request;
948 struct aws_s3_request *create_connection_request;
949
950 uint32_t finished_request_call_counter;
951 uint32_t create_connection_request_call_counter;
952 };
953
954 static void s_s3_test_meta_request_has_finish_result_finished_request(
955 struct aws_s3_meta_request *meta_request,
956 struct aws_s3_request *request,
957 int error_code) {
958 AWS_ASSERT(meta_request);
959 AWS_ASSERT(request);
960 (void)error_code;
961
962 struct s3_test_update_connections_finish_result_user_data *user_data = meta_request->user_data;
963 user_data->finished_request = request;
964 ++user_data->finished_request_call_counter;
965 }
966
967 static void s_s3_test_meta_request_has_finish_result_client_create_connection_for_request(
968 struct aws_s3_client *client,
969 struct aws_s3_request *request) {
970 (void)client;
971 (void)request;
972 AWS_ASSERT(client);
973 AWS_ASSERT(request);
974
975 struct aws_s3_meta_request *meta_request = request->meta_request;
976
977 struct s3_test_update_connections_finish_result_user_data *user_data = meta_request->user_data;
978 user_data->create_connection_request = request;
979 ++user_data->create_connection_request_call_counter;
980 }
981
982 size_t s_test_update_conns_finish_result_host_address_count(
983 struct aws_host_resolver *host_resolver,
984 const struct aws_string *host_name,
985 uint32_t flags) {
986 (void)host_resolver;
987 (void)host_name;
988 (void)flags;
989
990 return 1;
991 }
992
993 /* Test that the client will correctly discard requests for meta requests that are trying to finish. */
994 AWS_TEST_CASE(test_s3_client_update_connections_finish_result, s_test_s3_client_update_connections_finish_result)
995 static int s_test_s3_client_update_connections_finish_result(struct aws_allocator *allocator, void *ctx) {
996 (void)ctx;
997
998 struct aws_s3_tester tester;
999 aws_s3_tester_init(allocator, &tester);
1000
1001 struct aws_client_bootstrap mock_client_bootstrap;
1002 AWS_ZERO_STRUCT(mock_client_bootstrap);
1003
1004 struct aws_s3_client *mock_client = aws_s3_tester_mock_client_new(&tester);
1005 mock_client->client_bootstrap = &mock_client_bootstrap;
1006 mock_client->vtable->get_host_address_count = s_test_update_conns_finish_result_host_address_count;
1007 mock_client->vtable->create_connection_for_request =
1008 s_s3_test_meta_request_has_finish_result_client_create_connection_for_request;
1009
1010 *((uint32_t *)&mock_client->ideal_connection_count) = 1;
1011
1012 aws_linked_list_init(&mock_client->threaded_data.request_queue);
1013
1014 struct s3_test_update_connections_finish_result_user_data test_update_connections_finish_result_user_data;
1015 AWS_ZERO_STRUCT(test_update_connections_finish_result_user_data);
1016
1017 /* Put together a mock meta request that is finished. */
1018 struct aws_s3_meta_request *mock_meta_request = aws_s3_tester_mock_meta_request_new(&tester);
1019 mock_meta_request->client = aws_s3_client_acquire(mock_client);
1020 mock_meta_request->synced_data.finish_result_set = true;
1021 mock_meta_request->user_data = &test_update_connections_finish_result_user_data;
1022 mock_meta_request->endpoint = aws_s3_tester_mock_endpoint_new(&tester);
1023
1024 struct aws_s3_meta_request_vtable *mock_meta_request_vtable =
1025 aws_s3_tester_patch_meta_request_vtable(&tester, mock_meta_request, NULL);
1026 mock_meta_request_vtable->finished_request = s_s3_test_meta_request_has_finish_result_finished_request;
1027
1028 /* Verify that the request does not get sent because the meta request has finish-result. */
1029 {
1030 struct aws_s3_request *request = aws_s3_request_new(mock_meta_request, 0, 0, 0, 0);
1031 aws_linked_list_push_back(&mock_client->threaded_data.request_queue, &request->node);
1032 ++mock_client->threaded_data.request_queue_size;
1033
1034 aws_s3_client_update_connections_threaded(mock_client);
1035
1036 /* Request should still have been dequeued, but immediately passed to the meta request finish function. */
1037 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == 0);
1038 ASSERT_TRUE(aws_linked_list_empty(&mock_client->threaded_data.request_queue));
1039
1040 ASSERT_TRUE(test_update_connections_finish_result_user_data.finished_request == request);
1041 ASSERT_TRUE(test_update_connections_finish_result_user_data.finished_request_call_counter == 1);
1042
1043 ASSERT_TRUE(test_update_connections_finish_result_user_data.create_connection_request == NULL);
1044 ASSERT_TRUE(test_update_connections_finish_result_user_data.create_connection_request_call_counter == 0);
1045 }
1046
1047 AWS_ZERO_STRUCT(test_update_connections_finish_result_user_data);
1048
1049 /* Verify that a request with the 'always send' flag still gets sent when the meta request has a finish-result. */
1050 {
1051 struct aws_s3_request *request =
1052 aws_s3_request_new(mock_meta_request, 0, 0, 0, AWS_S3_REQUEST_FLAG_ALWAYS_SEND);
1053 aws_linked_list_push_back(&mock_client->threaded_data.request_queue, &request->node);
1054 ++mock_client->threaded_data.request_queue_size;
1055
1056 aws_s3_client_update_connections_threaded(mock_client);
1057
1058 /* Request should have been dequeued, and then sent on a connection. */
1059 ASSERT_TRUE(mock_client->threaded_data.request_queue_size == 0);
1060 ASSERT_TRUE(aws_linked_list_empty(&mock_client->threaded_data.request_queue));
1061
1062 ASSERT_TRUE(test_update_connections_finish_result_user_data.finished_request == NULL);
1063 ASSERT_TRUE(test_update_connections_finish_result_user_data.finished_request_call_counter == 0);
1064
1065 ASSERT_TRUE(test_update_connections_finish_result_user_data.create_connection_request == request);
1066 ASSERT_TRUE(test_update_connections_finish_result_user_data.create_connection_request_call_counter == 1);
1067
1068 aws_s3_request_release(request);
1069 }
1070
1071 aws_s3_meta_request_release(mock_meta_request);
1072
1073 aws_s3_client_release(mock_client);
1074 aws_s3_tester_clean_up(&tester);
1075
1076 return 0;
1077 }
1078
1079 static int s_test_s3_get_object_helper(
1080 struct aws_allocator *allocator,
1081 enum aws_s3_client_tls_usage tls_usage,
1082 uint32_t extra_meta_request_flag,
1083 struct aws_byte_cursor s3_path) {
1084 struct aws_s3_tester tester;
1085 AWS_ZERO_STRUCT(tester);
1086 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1087
1088 struct aws_s3_client_config client_config = {
1089 .part_size = 64 * 1024,
1090 };
1091
1092 struct aws_tls_connection_options tls_connection_options;
1093 AWS_ZERO_STRUCT(tls_connection_options);
1094
1095 #ifndef BYO_CRYPTO
1096 struct aws_tls_ctx_options tls_context_options;
1097 aws_tls_ctx_options_init_default_client(&tls_context_options, allocator);
1098
1099 struct aws_tls_ctx *context = aws_tls_client_ctx_new(allocator, &tls_context_options);
1100 aws_tls_connection_options_init_from_ctx(&tls_connection_options, context);
1101 #endif
1102
1103 struct aws_string *endpoint =
1104 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
1105 struct aws_byte_cursor endpoint_cursor = aws_byte_cursor_from_string(endpoint);
1106
1107 tls_connection_options.server_name = aws_string_new_from_cursor(allocator, &endpoint_cursor);
1108
1109 switch (tls_usage) {
1110 case AWS_S3_TLS_ENABLED:
1111 client_config.tls_mode = AWS_MR_TLS_ENABLED;
1112 client_config.tls_connection_options = &tls_connection_options;
1113 break;
1114 case AWS_S3_TLS_DISABLED:
1115 client_config.tls_mode = AWS_MR_TLS_DISABLED;
1116 break;
1117 default:
1118 break;
1119 }
1120
1121 ASSERT_SUCCESS(aws_s3_tester_bind_client(
1122 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
1123
1124 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
1125
1126 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS | extra_meta_request_flag;
1127 ASSERT_SUCCESS(aws_s3_tester_send_get_object_meta_request(&tester, client, s3_path, flags, NULL));
1128
1129 aws_string_destroy(endpoint);
1130
1131 #ifndef BYO_CRYPTO
1132 aws_tls_ctx_release(context);
1133 aws_tls_connection_options_clean_up(&tls_connection_options);
1134 aws_tls_ctx_options_clean_up(&tls_context_options);
1135 #endif
1136
1137 client = aws_s3_client_release(client);
1138
1139 aws_s3_tester_clean_up(&tester);
1140
1141 return AWS_OP_SUCCESS;
1142 }
1143
1144 AWS_TEST_CASE(test_s3_get_object_tls_disabled, s_test_s3_get_object_tls_disabled)
1145 static int s_test_s3_get_object_tls_disabled(struct aws_allocator *allocator, void *ctx) {
1146 (void)ctx;
1147
1148 ASSERT_SUCCESS(s_test_s3_get_object_helper(allocator, AWS_S3_TLS_DISABLED, 0, g_pre_existing_object_1MB));
1149
1150 return 0;
1151 }
1152
1153 AWS_TEST_CASE(test_s3_get_object_tls_enabled, s_test_s3_get_object_tls_enabled)
1154 static int s_test_s3_get_object_tls_enabled(struct aws_allocator *allocator, void *ctx) {
1155 (void)ctx;
1156
1157 ASSERT_SUCCESS(s_test_s3_get_object_helper(allocator, AWS_S3_TLS_ENABLED, 0, g_pre_existing_object_1MB));
1158
1159 return 0;
1160 }
1161
1162 AWS_TEST_CASE(test_s3_get_object_tls_default, s_test_s3_get_object_tls_default)
1163 static int s_test_s3_get_object_tls_default(struct aws_allocator *allocator, void *ctx) {
1164 (void)ctx;
1165
1166 ASSERT_SUCCESS(s_test_s3_get_object_helper(allocator, AWS_S3_TLS_DEFAULT, 0, g_pre_existing_object_1MB));
1167
1168 return 0;
1169 }
1170
1171 AWS_TEST_CASE(test_s3_no_signing, s_test_s3_no_signing)
1172 static int s_test_s3_no_signing(struct aws_allocator *allocator, void *ctx) {
1173 (void)ctx;
1174
1175 struct aws_s3_tester tester;
1176 AWS_ZERO_STRUCT(tester);
1177 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1178
1179 struct aws_s3_client_config client_config;
1180 AWS_ZERO_STRUCT(client_config);
1181
1182 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION));
1183
1184 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
1185 ASSERT_NOT_NULL(client);
1186
1187 struct aws_string *host_name =
1188 aws_s3_tester_build_endpoint_string(allocator, &g_test_public_bucket_name, &g_test_s3_region);
1189
1190 /* Put together a simple S3 Get Object request. */
1191 struct aws_http_message *message = aws_s3_test_get_object_request_new(
1192 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
1193
1194 struct aws_s3_meta_request_options options;
1195 AWS_ZERO_STRUCT(options);
1196 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
1197 options.message = message;
1198
1199 /* Trigger accelerating of our Get Object request. */
1200 struct aws_s3_meta_request_test_results meta_request_test_results;
1201 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1202
1203 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(
1204 &tester, client, &options, &meta_request_test_results, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS));
1205 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0));
1206
1207 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1208
1209 aws_http_message_release(message);
1210 aws_string_destroy(host_name);
1211 aws_s3_client_release(client);
1212 aws_s3_tester_clean_up(&tester);
1213
1214 return 0;
1215 }
1216
1217 AWS_TEST_CASE(test_s3_signing_override, s_test_s3_signing_override)
1218 static int s_test_s3_signing_override(struct aws_allocator *allocator, void *ctx) {
1219 (void)ctx;
1220
1221 struct aws_s3_tester tester;
1222 AWS_ZERO_STRUCT(tester);
1223 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1224
1225 struct aws_s3_client_config client_config;
1226 AWS_ZERO_STRUCT(client_config);
1227
1228 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION));
1229
1230 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
1231
1232 struct aws_string *host_name =
1233 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
1234
1235 /* Put together a simple S3 Get Object request. */
1236 struct aws_http_message *message = aws_s3_test_get_object_request_new(
1237 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
1238
1239 /* Getting without signing should fail since the client has no signing set up. */
1240 {
1241 struct aws_s3_meta_request_options options;
1242 AWS_ZERO_STRUCT(options);
1243 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
1244 options.message = message;
1245
1246 /* Trigger accelerating of our Get Object request.*/
1247 struct aws_s3_meta_request_test_results meta_request_test_results;
1248 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1249
1250 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(&tester, client, &options, &meta_request_test_results, 0));
1251 ASSERT_TRUE(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0) != AWS_OP_SUCCESS);
1252
1253 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1254 }
1255
1256 /* Getting with signing should succeed if we set up signing on the meta request. */
1257 {
1258 struct aws_s3_meta_request_options options;
1259 AWS_ZERO_STRUCT(options);
1260 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
1261 options.message = message;
1262 options.signing_config = &tester.default_signing_config;
1263
1264 /* Trigger accelerating of our Get Object request. */
1265 struct aws_s3_meta_request_test_results meta_request_test_results;
1266 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1267
1268 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(
1269 &tester, client, &options, &meta_request_test_results, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS));
1270 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0));
1271
1272 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1273 }
1274
1275 aws_http_message_release(message);
1276 aws_string_destroy(host_name);
1277 aws_s3_client_release(client);
1278 aws_s3_tester_clean_up(&tester);
1279
1280 return 0;
1281 }
1282
1283 AWS_TEST_CASE(test_s3_get_object_less_than_part_size, s_test_s3_get_object_less_than_part_size)
1284 static int s_test_s3_get_object_less_than_part_size(struct aws_allocator *allocator, void *ctx) {
1285 (void)ctx;
1286
1287 struct aws_s3_tester tester;
1288 AWS_ZERO_STRUCT(tester);
1289 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1290
1291 struct aws_s3_client_config client_config = {
1292 .part_size = 20 * 1024 * 1024,
1293 };
1294
1295 ASSERT_SUCCESS(aws_s3_tester_bind_client(
1296 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
1297
1298 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
1299
1300 ASSERT_SUCCESS(aws_s3_tester_send_get_object_meta_request(
1301 &tester, client, g_pre_existing_object_1MB, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS, NULL));
1302
1303 client = aws_s3_client_release(client);
1304
1305 aws_s3_tester_clean_up(&tester);
1306
1307 return 0;
1308 }
1309
1310 AWS_TEST_CASE(test_s3_put_object_with_part_remainder, s_test_s3_put_object_with_part_remainder)
1311 static int s_test_s3_put_object_with_part_remainder(struct aws_allocator *allocator, void *ctx) {
1312 (void)ctx;
1313
1314 struct aws_s3_tester tester;
1315 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1316
1317 struct aws_s3_tester_client_options client_options = {
1318 .part_size = 5 * 1024 * 1024,
1319 };
1320
1321 struct aws_s3_client *client = NULL;
1322 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1323
1324 struct aws_s3_meta_request_test_results meta_request_test_results;
1325 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1326
1327 struct aws_s3_tester_meta_request_options options = {
1328 .allocator = allocator,
1329 .client = client,
1330 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
1331 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1332 .put_options =
1333 {
1334 /* Object size meant to be one megabyte larger than the part size of the client. */
1335 .object_size_mb = 6,
1336 },
1337 };
1338
1339 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &meta_request_test_results));
1340 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1341
1342 aws_s3_client_release(client);
1343 aws_s3_tester_clean_up(&tester);
1344
1345 return 0;
1346 }
1347
1348 AWS_TEST_CASE(test_s3_get_object_multiple, s_test_s3_get_object_multiple)
1349 static int s_test_s3_get_object_multiple(struct aws_allocator *allocator, void *ctx) {
1350 (void)ctx;
1351
1352 struct aws_s3_meta_request *meta_requests[4];
1353 struct aws_s3_meta_request_test_results meta_request_test_results[4];
1354 size_t num_meta_requests = AWS_ARRAY_SIZE(meta_requests);
1355
1356 ASSERT_TRUE(num_meta_requests == AWS_ARRAY_SIZE(meta_request_test_results));
1357
1358 struct aws_s3_tester tester;
1359 AWS_ZERO_STRUCT(tester);
1360 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1361
1362 struct aws_s3_client_config client_config = {
1363 .part_size = 64 * 1024,
1364 };
1365
1366 ASSERT_SUCCESS(aws_s3_tester_bind_client(
1367 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
1368
1369 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
1370
1371 struct aws_string *host_name =
1372 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
1373
1374 for (size_t i = 0; i < num_meta_requests; ++i) {
1375 /* Put together a simple S3 Get Object request. */
1376 struct aws_http_message *message = aws_s3_test_get_object_request_new(
1377 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
1378
1379 aws_s3_meta_request_test_results_init(&meta_request_test_results[i], allocator);
1380
1381 struct aws_s3_meta_request_options options;
1382 AWS_ZERO_STRUCT(options);
1383 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
1384 options.message = message;
1385
1386 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_results[i]));
1387
1388 /* Trigger accelerating of our Get Object request. */
1389 meta_requests[i] = aws_s3_client_make_meta_request(client, &options);
1390
1391 ASSERT_TRUE(meta_requests[i] != NULL);
1392
1393 aws_http_message_release(message);
1394 }
1395
1396 /* Wait for the request to finish. */
1397 aws_s3_tester_wait_for_meta_request_finish(&tester);
1398
1399 aws_s3_tester_lock_synced_data(&tester);
1400 ASSERT_TRUE(tester.synced_data.finish_error_code == AWS_ERROR_SUCCESS);
1401 aws_s3_tester_unlock_synced_data(&tester);
1402
1403 for (size_t i = 0; i < num_meta_requests; ++i) {
1404 meta_requests[i] = aws_s3_meta_request_release(meta_requests[i]);
1405 }
1406
1407 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
1408
1409 for (size_t i = 0; i < num_meta_requests; ++i) {
1410 aws_s3_tester_validate_get_object_results(&meta_request_test_results[i], 0);
1411 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results[i]);
1412 }
1413
1414 aws_string_destroy(host_name);
1415 host_name = NULL;
1416
1417 client = aws_s3_client_release(client);
1418
1419 aws_s3_tester_clean_up(&tester);
1420
1421 return 0;
1422 }
1423
1424 AWS_TEST_CASE(test_s3_get_object_multiple_serial, s_test_s3_get_object_multiple_serial)
1425 static int s_test_s3_get_object_multiple_serial(struct aws_allocator *allocator, void *ctx) {
1426 (void)ctx;
1427
1428 struct aws_s3_tester tester;
1429 AWS_ZERO_STRUCT(tester);
1430 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1431
1432 struct aws_s3_tester_client_options client_options = {
1433 .part_size = MB_TO_BYTES(5),
1434 };
1435
1436 struct aws_s3_client *client = NULL;
1437 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1438
1439 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-10MB");
1440 struct aws_s3_tester_meta_request_options get_options = {
1441 .allocator = allocator,
1442 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1443 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1444 .client = client,
1445 .get_options =
1446 {
1447 .object_path = object_path,
1448 },
1449 };
1450
1451 for (size_t i = 0; i < 4; ++i) {
1452 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
1453 }
1454
1455 /* Sleep for some time to wait for the cleanup task to run */
1456 aws_thread_current_sleep(aws_timestamp_convert(7, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
1457
1458 /* BEGIN CRITICAL SECTION */
1459 aws_s3_client_lock_synced_data(client);
1460
1461 AWS_ASSERT(client->synced_data.num_endpoints_allocated == 0);
1462
1463 aws_s3_client_unlock_synced_data(client);
1464 /* END CRITICAL SECTION */
1465
1466 client = aws_s3_client_release(client);
1467 aws_s3_tester_clean_up(&tester);
1468 return 0;
1469 }
1470
1471 AWS_TEST_CASE(test_s3_get_object_file_path, s_test_s3_get_object_file_path)
1472 static int s_test_s3_get_object_file_path(struct aws_allocator *allocator, void *ctx) {
1473 (void)ctx;
1474
1475 struct aws_s3_tester tester;
1476 AWS_ZERO_STRUCT(tester);
1477 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1478
1479 struct aws_s3_tester_client_options client_options = {
1480 .part_size = MB_TO_BYTES(5),
1481 };
1482
1483 struct aws_s3_client *client = NULL;
1484 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1485
1486 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1487 struct aws_s3_tester_meta_request_options get_options = {
1488 .allocator = allocator,
1489 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1490 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1491 .client = client,
1492 .get_options =
1493 {
1494 .object_path = object_path,
1495 .file_on_disk = true,
1496 },
1497 };
1498
1499 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
1500
1501 client = aws_s3_client_release(client);
1502 aws_s3_tester_clean_up(&tester);
1503 return 0;
1504 }
1505
1506 AWS_TEST_CASE(test_s3_get_object_file_path_create_new, s_test_s3_get_object_file_path_create_new)
1507 static int s_test_s3_get_object_file_path_create_new(struct aws_allocator *allocator, void *ctx) {
1508 (void)ctx;
1509
1510 struct aws_s3_tester tester;
1511 AWS_ZERO_STRUCT(tester);
1512 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1513
1514 struct aws_s3_tester_client_options client_options = {
1515 .part_size = MB_TO_BYTES(5),
1516 };
1517
1518 struct aws_s3_client *client = NULL;
1519 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1520
1521 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1522 struct aws_s3_tester_meta_request_options get_options = {
1523 .allocator = allocator,
1524 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1525 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
1526 .client = client,
1527 .get_options =
1528 {
1529 .object_path = object_path,
1530 .file_on_disk = true,
1531 .recv_file_option = AWS_S3_RECV_FILE_CREATE_NEW,
1532 .pre_exist_file_length = 10,
1533 .recv_file_delete_on_failure = true,
1534 },
1535 };
1536
1537 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
1538 ASSERT_UINT_EQUALS(AWS_ERROR_S3_RECV_FILE_ALREADY_EXISTS, aws_last_error());
1539
1540 /* The request failed without invoking the finish callback. Reset the finish and shutdown count */
1541 aws_s3_tester_lock_synced_data(&tester);
1542 ASSERT_UINT_EQUALS(0, tester.synced_data.meta_request_shutdown_count);
1543 ASSERT_UINT_EQUALS(0, tester.synced_data.meta_request_finish_count);
1544 tester.synced_data.desired_meta_request_shutdown_count = 0;
1545 tester.synced_data.desired_meta_request_finish_count = 0;
1546 aws_s3_tester_unlock_synced_data(&tester);
1547
1548 get_options.validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS;
1549 get_options.get_options.pre_exist_file_length = 0;
1550 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
1551
1552 client = aws_s3_client_release(client);
1553 aws_s3_tester_clean_up(&tester);
1554 return 0;
1555 }
1556
1557 AWS_TEST_CASE(test_s3_get_object_file_path_append, s_test_s3_get_object_file_path_append)
1558 static int s_test_s3_get_object_file_path_append(struct aws_allocator *allocator, void *ctx) {
1559 (void)ctx;
1560
1561 struct aws_s3_tester tester;
1562 AWS_ZERO_STRUCT(tester);
1563 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1564
1565 struct aws_s3_tester_client_options client_options = {
1566 .part_size = MB_TO_BYTES(5),
1567 };
1568
1569 struct aws_s3_meta_request_test_results meta_request_test_results;
1570 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1571 struct aws_s3_client *client = NULL;
1572 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1573
1574 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1575 uint64_t pre_exist_file_length = 10;
1576 struct aws_s3_tester_meta_request_options get_options = {
1577 .allocator = allocator,
1578 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1579 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1580 .client = client,
1581 .get_options =
1582 {
1583 .object_path = object_path,
1584 .file_on_disk = true,
1585 .recv_file_option = AWS_S3_RECV_FILE_CREATE_OR_APPEND,
1586 .pre_exist_file_length = pre_exist_file_length,
1587 },
1588 };
1589
1590 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
1591 ASSERT_UINT_EQUALS(pre_exist_file_length + MB_TO_BYTES(1), meta_request_test_results.received_file_size);
1592 ASSERT_UINT_EQUALS(MB_TO_BYTES(1), meta_request_test_results.progress.total_bytes_transferred);
1593 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1594 client = aws_s3_client_release(client);
1595 aws_s3_tester_clean_up(&tester);
1596 return 0;
1597 }
1598
1599 AWS_TEST_CASE(test_s3_get_object_file_path_to_position, s_test_s3_get_object_file_path_to_position)
1600 static int s_test_s3_get_object_file_path_to_position(struct aws_allocator *allocator, void *ctx) {
1601 (void)ctx;
1602
1603 struct aws_s3_tester tester;
1604 AWS_ZERO_STRUCT(tester);
1605 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1606
1607 struct aws_s3_tester_client_options client_options = {
1608 .part_size = MB_TO_BYTES(5),
1609 };
1610
1611 struct aws_s3_meta_request_test_results meta_request_test_results;
1612 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1613 struct aws_s3_client *client = NULL;
1614 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1615
1616 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1617 uint64_t pre_exist_file_length = 10;
1618 struct aws_s3_tester_meta_request_options get_options = {
1619 .allocator = allocator,
1620 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1621 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
1622 .client = client,
1623 .get_options =
1624 {
1625 .object_path = object_path,
1626 .file_on_disk = true,
1627 .recv_file_option = AWS_S3_RECV_FILE_WRITE_TO_POSITION,
1628 .pre_exist_file_length = 0,
1629 },
1630 };
1631
1632 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
1633 ASSERT_UINT_EQUALS(AWS_ERROR_S3_RECV_FILE_NOT_FOUND, aws_last_error());
1634
1635 /* The request failed without invoking the finish callback. Reset the finish and shutdown count */
1636 aws_s3_tester_lock_synced_data(&tester);
1637 ASSERT_UINT_EQUALS(0, tester.synced_data.meta_request_shutdown_count);
1638 ASSERT_UINT_EQUALS(0, tester.synced_data.meta_request_finish_count);
1639 tester.synced_data.desired_meta_request_shutdown_count = 0;
1640 tester.synced_data.desired_meta_request_finish_count = 0;
1641 aws_s3_tester_unlock_synced_data(&tester);
1642
1643 get_options.validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS;
1644 get_options.get_options.pre_exist_file_length = pre_exist_file_length;
1645 get_options.get_options.recv_file_position = 20;
1646 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
1647 ASSERT_UINT_EQUALS(
1648 get_options.get_options.recv_file_position + MB_TO_BYTES(1), meta_request_test_results.received_file_size);
1649 ASSERT_UINT_EQUALS(MB_TO_BYTES(1), meta_request_test_results.progress.total_bytes_transferred);
1650
1651 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1652 client = aws_s3_client_release(client);
1653 aws_s3_tester_clean_up(&tester);
1654 return 0;
1655 }
1656
1657 AWS_TEST_CASE(test_s3_get_object_file_path_direct_io, s_test_s3_get_object_file_path_direct_io)
1658 static int s_test_s3_get_object_file_path_direct_io(struct aws_allocator *allocator, void *ctx) {
1659 (void)ctx;
1660
1661 struct aws_s3_tester tester;
1662 AWS_ZERO_STRUCT(tester);
1663 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1664
1665 struct aws_s3_tester_client_options client_options = {
1666 .part_size = MB_TO_BYTES(5),
1667 };
1668
1669 struct aws_s3_client *client = NULL;
1670 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1671
1672 struct aws_s3_file_io_options fio_opts = {
1673 .direct_io = true,
1674 };
1675
1676 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1677 struct aws_s3_tester_meta_request_options get_options = {
1678 .allocator = allocator,
1679 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1680 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1681 .client = client,
1682 .fio_opts = &fio_opts,
1683 .get_options =
1684 {
1685 .object_path = object_path,
1686 .file_on_disk = true,
1687 },
1688 };
1689
1690 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
1691
1692 client = aws_s3_client_release(client);
1693 aws_s3_tester_clean_up(&tester);
1694 return 0;
1695 }
1696
1697 AWS_TEST_CASE(
1698 test_s3_get_object_file_path_direct_io_content_verify,
1699 s_test_s3_get_object_file_path_direct_io_content_verify)
1700 static int s_test_s3_get_object_file_path_direct_io_content_verify(struct aws_allocator *allocator, void *ctx) {
1701 (void)ctx;
1702
1703 struct aws_s3_tester tester;
1704 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1705
1706 struct aws_s3_tester_client_options client_options = {
1707 .part_size = MB_TO_BYTES(5),
1708 };
1709
1710 struct aws_s3_client *client = NULL;
1711 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1712
1713 /* Upload with CRC32 checksum */
1714 struct aws_byte_buf path_buf;
1715 AWS_ZERO_STRUCT(path_buf);
1716 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
1717 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/direct_io_verify.txt")));
1718 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
1719
1720 struct aws_s3_tester_meta_request_options put_options = {
1721 .allocator = allocator,
1722 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
1723 .client = client,
1724 .checksum_algorithm = AWS_SCA_CRC32,
1725 .put_options =
1726 {
1727 .object_size_mb = 20,
1728 .object_path_override = object_path,
1729 },
1730 };
1731 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
1732
1733 /* Download with O_DIRECT to a known file path */
1734 const char *local_file_path = "aws_s3_direct_io_crc32_verify_test_file";
1735 struct aws_string *host_name =
1736 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
1737 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_string(host_name);
1738 struct aws_http_message *message = aws_s3_test_get_object_request_new(allocator, host_cursor, object_path);
1739
1740 struct aws_s3_checksum_config checksum_config = {
1741 .validate_response_checksum = true,
1742 };
1743 struct aws_s3_file_io_options fio_opts = {
1744 .direct_io = true,
1745 };
1746 struct aws_s3_meta_request_options meta_request_options = {
1747 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1748 .message = message,
1749 .recv_filepath = aws_byte_cursor_from_c_str(local_file_path),
1750 .checksum_config = &checksum_config,
1751 .fio_opts = &fio_opts,
1752 };
1753
1754 struct aws_s3_meta_request_test_results meta_request_test_results;
1755 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1756 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &meta_request_options, &meta_request_test_results));
1757
1758 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &meta_request_options);
1759 ASSERT_NOT_NULL(meta_request);
1760
1761 aws_s3_tester_wait_for_meta_request_finish(&tester);
1762 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, meta_request_test_results.finished_error_code);
1763 /* S3 response checksum was validated */
1764 ASSERT_TRUE(meta_request_test_results.did_validate);
1765 ASSERT_INT_EQUALS(AWS_SCA_CRC32, meta_request_test_results.validation_algorithm);
1766 /* 20 MiB multipart download with 5 MiB parts: all parts page-aligned.
1767 * fallback_count is 0 if O_DIRECT is supported, otherwise 1 from the init-time platform fallback. */
1768 size_t expected_fallback_count = aws_file_direct_io_is_supported() ? 0 : 1;
1769 ASSERT_UINT_EQUALS(expected_fallback_count, meta_request->recv_file_direct_io_fallback_count);
1770
1771 aws_s3_meta_request_release(meta_request);
1772 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
1773
1774 /* Read file back from disk and compute CRC32.
1775 * The tester uploads content using AWS_AUTOGEN_LOREM_IPSUM pattern. */
1776 size_t expected_size = MB_TO_BYTES(20);
1777 struct aws_byte_buf expected_buf;
1778 s_byte_buf_init_autogenned(&expected_buf, allocator, expected_size, AWS_AUTOGEN_LOREM_IPSUM);
1779 uint32_t expected_crc = aws_checksums_crc32(expected_buf.buffer, (int)expected_buf.len, 0);
1780
1781 FILE *verify_file = aws_fopen(local_file_path, "rb");
1782 ASSERT_NOT_NULL(verify_file);
1783 struct aws_byte_buf file_buf;
1784 aws_byte_buf_init(&file_buf, allocator, expected_size);
1785 file_buf.len = fread(file_buf.buffer, 1, expected_size, verify_file);
1786 fclose(verify_file);
1787
1788 ASSERT_UINT_EQUALS(expected_size, file_buf.len);
1789 uint32_t actual_crc = aws_checksums_crc32(file_buf.buffer, (int)file_buf.len, 0);
1790 ASSERT_UINT_EQUALS(expected_crc, actual_crc);
1791
1792 /* Cleanup */
1793 remove(local_file_path);
1794 aws_byte_buf_clean_up(&file_buf);
1795 aws_byte_buf_clean_up(&expected_buf);
1796 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1797 aws_http_message_release(message);
1798 aws_string_destroy(host_name);
1799 aws_byte_buf_clean_up(&path_buf);
1800 aws_s3_client_release(client);
1801 aws_s3_tester_clean_up(&tester);
1802 return 0;
1803 }
1804
1805 AWS_TEST_CASE(test_s3_get_object_file_path_direct_io_dev_null, s_test_s3_get_object_file_path_direct_io_dev_null)
1806 static int s_test_s3_get_object_file_path_direct_io_dev_null(struct aws_allocator *allocator, void *ctx) {
1807 (void)ctx;
1808 #ifndef AWS_OS_LINUX
1809 (void)allocator;
1810 return AWS_OP_SKIP;
1811 #else
1812 struct aws_s3_tester tester;
1813 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1814
1815 struct aws_s3_tester_client_options client_options = {
1816 .part_size = MB_TO_BYTES(5),
1817 };
1818 struct aws_s3_client *client = NULL;
1819 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1820
1821 struct aws_byte_buf path_buf;
1822 AWS_ZERO_STRUCT(path_buf);
1823 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
1824 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/direct_io_dev_null.txt")));
1825 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
1826
1827 struct aws_s3_tester_meta_request_options put_options = {
1828 .allocator = allocator,
1829 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
1830 .client = client,
1831 .checksum_algorithm = AWS_SCA_CRC32,
1832 .put_options =
1833 {
1834 .object_size_mb = 6, // more than 1 part
1835 .object_path_override = object_path,
1836 },
1837 };
1838 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
1839
1840 /* Download to /dev/null with O_DIRECT — open with O_DIRECT fails on /dev/null,
1841 * so all writes fall back to buffered I/O. */
1842 struct aws_string *host_name =
1843 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
1844 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_string(host_name);
1845 struct aws_http_message *message = aws_s3_test_get_object_request_new(allocator, host_cursor, object_path);
1846
1847 struct aws_s3_checksum_config checksum_config = {
1848 .validate_response_checksum = true,
1849 };
1850 struct aws_s3_file_io_options fio_opts = {
1851 .direct_io = true,
1852 };
1853 struct aws_s3_meta_request_options meta_request_options = {
1854 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1855 .message = message,
1856 .recv_filepath = aws_byte_cursor_from_c_str("/dev/null"),
1857 .checksum_config = &checksum_config,
1858 .fio_opts = &fio_opts,
1859 };
1860
1861 struct aws_s3_meta_request_test_results meta_request_test_results;
1862 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1863 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &meta_request_options, &meta_request_test_results));
1864
1865 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &meta_request_options);
1866 ASSERT_NOT_NULL(meta_request);
1867
1868 aws_s3_tester_wait_for_meta_request_finish(&tester);
1869 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, meta_request_test_results.finished_error_code);
1870 ASSERT_TRUE(meta_request_test_results.did_validate);
1871 ASSERT_INT_EQUALS(AWS_SCA_CRC32, meta_request_test_results.validation_algorithm);
1872 /* /dev/null doesn't support O_DIRECT — verify fallback triggered (1 from init-time open failure) */
1873 ASSERT_UINT_EQUALS(1, meta_request->recv_file_direct_io_fallback_count);
1874
1875 aws_s3_meta_request_release(meta_request);
1876 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
1877
1878 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1879 aws_http_message_release(message);
1880 aws_string_destroy(host_name);
1881 aws_byte_buf_clean_up(&path_buf);
1882 aws_s3_client_release(client);
1883 aws_s3_tester_clean_up(&tester);
1884 return 0;
1885 #endif
1886 }
1887
1888 AWS_TEST_CASE(
1889 test_s3_get_object_file_path_direct_io_append_unaligned_fallback,
1890 s_test_s3_get_object_file_path_direct_io_append_unaligned_fallback)
1891 static int s_test_s3_get_object_file_path_direct_io_append_unaligned_fallback(
1892 struct aws_allocator *allocator,
1893 void *ctx) {
1894 (void)ctx;
1895
1896 struct aws_s3_tester tester;
1897 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1898
1899 struct aws_s3_tester_client_options client_options = {
1900 .part_size = MB_TO_BYTES(5),
1901 };
1902
1903 struct aws_s3_client *client = NULL;
1904 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1905
1906 struct aws_s3_file_io_options fio_opts = {
1907 .direct_io = true,
1908 };
1909
1910 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1911 /* Existing file size of 10 bytes is not page-aligned → init-time fallback to buffered I/O.
1912 * Download still succeeds via the FILE* fallback path. */
1913 struct aws_s3_tester_meta_request_options get_options = {
1914 .allocator = allocator,
1915 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1916 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1917 .client = client,
1918 .fio_opts = &fio_opts,
1919 .get_options =
1920 {
1921 .object_path = object_path,
1922 .file_on_disk = true,
1923 .recv_file_option = AWS_S3_RECV_FILE_CREATE_OR_APPEND,
1924 .pre_exist_file_length = 10,
1925 },
1926 };
1927
1928 struct aws_s3_meta_request_test_results meta_request_test_results;
1929 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1930 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
1931 /* one fallback occurred at init (unaligned existing file size). */
1932 ASSERT_TRUE(meta_request_test_results.recv_file_direct_io_fallback_count == 1);
1933
1934 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1935 aws_s3_client_release(client);
1936 aws_s3_tester_clean_up(&tester);
1937 return 0;
1938 }
1939
1940 AWS_TEST_CASE(
1941 test_s3_get_object_file_path_direct_io_write_to_position_unaligned_fallback,
1942 s_test_s3_get_object_file_path_direct_io_write_to_position_unaligned_fallback)
1943 static int s_test_s3_get_object_file_path_direct_io_write_to_position_unaligned_fallback(
1944 struct aws_allocator *allocator,
1945 void *ctx) {
1946 (void)ctx;
1947
1948 struct aws_s3_tester tester;
1949 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
1950
1951 struct aws_s3_tester_client_options client_options = {
1952 .part_size = MB_TO_BYTES(5),
1953 };
1954
1955 struct aws_s3_client *client = NULL;
1956 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
1957
1958 struct aws_s3_file_io_options fio_opts = {
1959 .direct_io = true,
1960 };
1961
1962 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/pre-existing-1MB");
1963 /* recv_file_position = 100 is not page-aligned → init-time fallback to buffered I/O.
1964 * Download still succeeds via the FILE* fallback path. */
1965 struct aws_s3_tester_meta_request_options get_options = {
1966 .allocator = allocator,
1967 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1968 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
1969 .client = client,
1970 .fio_opts = &fio_opts,
1971 .get_options =
1972 {
1973 .object_path = object_path,
1974 .file_on_disk = true,
1975 .recv_file_option = AWS_S3_RECV_FILE_WRITE_TO_POSITION,
1976 .recv_file_position = 100,
1977 .pre_exist_file_length = 200,
1978 },
1979 };
1980
1981 struct aws_s3_meta_request_test_results meta_request_test_results;
1982 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1983 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
1984 ASSERT_TRUE(meta_request_test_results.recv_file_direct_io_fallback_count == 1);
1985
1986 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1987 aws_s3_client_release(client);
1988 aws_s3_tester_clean_up(&tester);
1989 return 0;
1990 }
1991
1992 /* O_DIRECT with WRITE_TO_POSITION at a page-aligned position should successfully use O_DIRECT.
1993 * On Linux: count == 0 (all writes go through O_DIRECT, no fallback). The base_position is
1994 * applied to delivery write_offset. */
1995 AWS_TEST_CASE(
1996 test_s3_get_object_file_path_direct_io_write_to_position_aligned,
1997 s_test_s3_get_object_file_path_direct_io_write_to_position_aligned)
1998 static int s_test_s3_get_object_file_path_direct_io_write_to_position_aligned(
1999 struct aws_allocator *allocator,
2000 void *ctx) {
2001 (void)ctx;
2002
2003 struct aws_s3_tester tester;
2004 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2005
2006 struct aws_s3_tester_client_options client_options = {
2007 .part_size = MB_TO_BYTES(5),
2008 };
2009
2010 struct aws_s3_client *client = NULL;
2011 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
2012
2013 struct aws_s3_file_io_options fio_opts = {
2014 .direct_io = true,
2015 };
2016
2017 /* recv_file_position = 4096 (page-aligned). The 1MB object will be written starting at offset
2018 * 4096 in the local file. Pre-existing file size 4096 ensures WRITE_TO_POSITION precondition
2019 * is met. */
2020 struct aws_s3_tester_meta_request_options get_options = {
2021 .allocator = allocator,
2022 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
2023 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
2024 .client = client,
2025 .fio_opts = &fio_opts,
2026 .get_options =
2027 {
2028 .object_path = g_pre_existing_object_1MB,
2029 .file_on_disk = true,
2030 .recv_file_option = AWS_S3_RECV_FILE_WRITE_TO_POSITION,
2031 .recv_file_position = 4096,
2032 .pre_exist_file_length = 4096,
2033 },
2034 };
2035
2036 struct aws_s3_meta_request_test_results meta_request_test_results;
2037 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2038 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
2039 ASSERT_INT_EQUALS((int64_t)(4096 + MB_TO_BYTES(1)), meta_request_test_results.received_file_size);
2040 /* If O_DIRECT is supported: aligned position + part_size, no fallback needed.
2041 * Otherwise: init-time fallback to buffered I/O. */
2042 size_t expected_fallback_count = aws_file_direct_io_is_supported() ? 0 : 1;
2043 ASSERT_UINT_EQUALS(expected_fallback_count, meta_request_test_results.recv_file_direct_io_fallback_count);
2044
2045 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2046 aws_s3_client_release(client);
2047 aws_s3_tester_clean_up(&tester);
2048 return 0;
2049 }
2050
2051 /* Multi-part download with O_DIRECT: download a 10MB object with 4MB part_size,
2052 * exercises the per-part write loop. All parts are page-aligned in this case. */
2053 AWS_TEST_CASE(test_s3_get_object_file_path_direct_io_multi_part, s_test_s3_get_object_file_path_direct_io_multi_part)
2054 static int s_test_s3_get_object_file_path_direct_io_multi_part(struct aws_allocator *allocator, void *ctx) {
2055 (void)ctx;
2056
2057 struct aws_s3_tester tester;
2058 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2059
2060 struct aws_s3_tester_client_options client_options = {
2061 .part_size = MB_TO_BYTES(4),
2062 };
2063
2064 struct aws_s3_client *client = NULL;
2065 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
2066
2067 struct aws_s3_file_io_options fio_opts = {
2068 .direct_io = true,
2069 };
2070
2071 struct aws_s3_tester_meta_request_options get_options = {
2072 .allocator = allocator,
2073 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
2074 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
2075 .client = client,
2076 .fio_opts = &fio_opts,
2077 .validate_get_response_checksum = true,
2078 .expected_validate_checksum_alg = AWS_SCA_CRC32,
2079 .finish_callback = s_s3_test_validate_checksum,
2080 .get_options =
2081 {
2082 .object_path = g_pre_existing_object_10MB,
2083 .file_on_disk = true,
2084 },
2085 };
2086
2087 struct aws_s3_meta_request_test_results meta_request_test_results;
2088 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2089 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
2090 ASSERT_INT_EQUALS((int64_t)MB_TO_BYTES(10), meta_request_test_results.received_file_size);
2091 /* All parts (4+4+2 MiB) are page-aligned. fallback_count is 0 if O_DIRECT is supported,
2092 * otherwise 1 from the init-time platform fallback. */
2093 size_t expected_fallback_count = aws_file_direct_io_is_supported() ? 0 : 1;
2094 ASSERT_UINT_EQUALS(expected_fallback_count, meta_request_test_results.recv_file_direct_io_fallback_count);
2095
2096 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2097 aws_s3_client_release(client);
2098 aws_s3_tester_clean_up(&tester);
2099 return 0;
2100 }
2101
2102 /* O_DIRECT requires part_size to be page-aligned. With unaligned part_size, init silently
2103 * falls back to buffered I/O. Download still succeeds. */
2104 AWS_TEST_CASE(
2105 test_s3_get_object_file_path_direct_io_unaligned_part_size_fallback,
2106 s_test_s3_get_object_file_path_direct_io_unaligned_part_size_fallback)
2107 static int s_test_s3_get_object_file_path_direct_io_unaligned_part_size_fallback(
2108 struct aws_allocator *allocator,
2109 void *ctx) {
2110 (void)ctx;
2111
2112 struct aws_s3_tester tester;
2113 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2114
2115 /* part_size = 5 MB - 1 byte, not page-aligned */
2116 struct aws_s3_tester_client_options client_options = {
2117 .part_size = MB_TO_BYTES(5) - 1,
2118 };
2119
2120 struct aws_s3_client *client = NULL;
2121 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
2122
2123 struct aws_s3_file_io_options fio_opts = {
2124 .direct_io = true,
2125 };
2126
2127 struct aws_s3_tester_meta_request_options get_options = {
2128 .allocator = allocator,
2129 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
2130 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
2131 .client = client,
2132 .fio_opts = &fio_opts,
2133 .get_options =
2134 {
2135 .object_path = g_pre_existing_object_1MB,
2136 .file_on_disk = true,
2137 },
2138 };
2139
2140 struct aws_s3_meta_request_test_results meta_request_test_results;
2141 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2142 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &meta_request_test_results));
2143 /* Init-time fallback because part_size is not page-aligned */
2144 ASSERT_TRUE(meta_request_test_results.recv_file_direct_io_fallback_count == 1);
2145
2146 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2147 aws_s3_client_release(client);
2148 aws_s3_tester_clean_up(&tester);
2149 return 0;
2150 }
2151
2152 /* Round-trip with byte-precise object size (10MiB - 1 byte). Multi-part download with O_DIRECT.
2153 * Verifies the unaligned last-part fallback path: all but the last part go through O_DIRECT,
2154 * the last part has unaligned length and falls back to buffered fwrite. Read back from disk
2155 * and compare CRC32 to confirm all bytes are correct. */
2156 AWS_TEST_CASE(
2157 test_s3_get_object_file_path_direct_io_unaligned_last_part,
2158 s_test_s3_get_object_file_path_direct_io_unaligned_last_part)
2159 static int s_test_s3_get_object_file_path_direct_io_unaligned_last_part(struct aws_allocator *allocator, void *ctx) {
2160 (void)ctx;
2161
2162 struct aws_s3_tester tester;
2163 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2164
2165 /* 4 MB part_size: 10 MiB - 1 = 9 parts of 4 MiB + 1 last part of (10 MiB - 1 - 8 MiB) bytes.
2166 * Wait: 10 MiB = 10485760, 4 MiB = 4194304. 10485760 - 1 = 10485759.
2167 * 10485759 / 4194304 = 2 full parts (2 * 4194304 = 8388608), remainder = 2097151 bytes.
2168 * Last part = 2097151 = 2 MiB - 1 byte (NOT page-aligned). */
2169 struct aws_s3_tester_client_options client_options = {
2170 .part_size = MB_TO_BYTES(4),
2171 };
2172
2173 struct aws_s3_client *client = NULL;
2174 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
2175
2176 /* Upload 10 MiB - 1 byte */
2177 size_t object_size = MB_TO_BYTES(10) - 1;
2178 struct aws_byte_buf upload_buf;
2179 aws_s3_create_test_buffer(allocator, object_size, &upload_buf);
2180 struct aws_byte_cursor upload_cursor = aws_byte_cursor_from_buf(&upload_buf);
2181 struct aws_input_stream *upload_stream = aws_input_stream_new_from_cursor(allocator, &upload_cursor);
2182
2183 struct aws_byte_buf path_buf;
2184 AWS_ZERO_STRUCT(path_buf);
2185 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
2186 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/direct_io_unaligned_last.bin")));
2187 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
2188
2189 struct aws_string *host_name =
2190 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
2191 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_string(host_name);
2192
2193 /* Use the lower-level API to upload the byte-precise body */
2194 struct aws_http_message *put_message = aws_s3_test_put_object_request_new(
2195 allocator, &host_cursor, object_path, g_test_body_content_type, upload_stream, 0);
2196
2197 struct aws_s3_meta_request_options put_options = {
2198 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
2199 .message = put_message,
2200 };
2201
2202 struct aws_s3_meta_request_test_results put_results;
2203 aws_s3_meta_request_test_results_init(&put_results, allocator);
2204 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &put_options, &put_results));
2205
2206 struct aws_s3_meta_request *put_request = aws_s3_client_make_meta_request(client, &put_options);
2207 ASSERT_NOT_NULL(put_request);
2208 aws_s3_tester_wait_for_meta_request_finish(&tester);
2209 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, put_results.finished_error_code);
2210 aws_s3_meta_request_release(put_request);
2211 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
2212 aws_http_message_release(put_message);
2213 aws_s3_meta_request_test_results_clean_up(&put_results);
2214
2215 /* Download with O_DIRECT to a known path */
2216 const char *local_file_path = "aws_s3_direct_io_unaligned_last_test_file";
2217 struct aws_http_message *get_message = aws_s3_test_get_object_request_new(allocator, host_cursor, object_path);
2218
2219 struct aws_s3_file_io_options fio_opts = {
2220 .direct_io = true,
2221 };
2222 struct aws_s3_meta_request_options get_options = {
2223 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
2224 .message = get_message,
2225 .recv_filepath = aws_byte_cursor_from_c_str(local_file_path),
2226 .fio_opts = &fio_opts,
2227 };
2228
2229 struct aws_s3_meta_request_test_results get_results;
2230 aws_s3_meta_request_test_results_init(&get_results, allocator);
2231 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &get_options, &get_results));
2232
2233 struct aws_s3_meta_request *get_request = aws_s3_client_make_meta_request(client, &get_options);
2234 ASSERT_NOT_NULL(get_request);
2235 aws_s3_tester_wait_for_meta_request_finish(&tester);
2236 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, get_results.finished_error_code);
2237 /* On Linux: 2 aligned parts go through O_DIRECT, 1 unaligned last part falls back. count == 1.
2238 * On non-Linux: first write triggers UNSUPPORTED_OPERATION fallback, count == 1. */
2239 ASSERT_UINT_EQUALS(1, get_request->recv_file_direct_io_fallback_count);
2240 aws_s3_meta_request_release(get_request);
2241 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
2242
2243 /* Read back from disk and verify CRC32 matches the upload */
2244 uint32_t expected_crc = aws_checksums_crc32(upload_buf.buffer, (int)upload_buf.len, 0);
2245
2246 FILE *verify_file = aws_fopen(local_file_path, "rb");
2247 ASSERT_NOT_NULL(verify_file);
2248 struct aws_byte_buf file_buf;
2249 aws_byte_buf_init(&file_buf, allocator, object_size);
2250 file_buf.len = fread(file_buf.buffer, 1, object_size, verify_file);
2251 fclose(verify_file);
2252
2253 ASSERT_UINT_EQUALS(object_size, file_buf.len);
2254 uint32_t actual_crc = aws_checksums_crc32(file_buf.buffer, (int)file_buf.len, 0);
2255 ASSERT_UINT_EQUALS(expected_crc, actual_crc);
2256
2257 /* Cleanup */
2258 remove(local_file_path);
2259 aws_byte_buf_clean_up(&file_buf);
2260 aws_s3_meta_request_test_results_clean_up(&get_results);
2261 aws_http_message_release(get_message);
2262 aws_string_destroy(host_name);
2263 aws_input_stream_release(upload_stream);
2264 aws_byte_buf_clean_up(&upload_buf);
2265 aws_byte_buf_clean_up(&path_buf);
2266 aws_s3_client_release(client);
2267 aws_s3_tester_clean_up(&tester);
2268 return 0;
2269 }
2270
2271 AWS_TEST_CASE(test_s3_get_object_empty_object, s_test_s3_get_object_empty_default)
2272 static int s_test_s3_get_object_empty_default(struct aws_allocator *allocator, void *ctx) {
2273 (void)ctx;
2274
2275 return (s_test_s3_get_object_helper(allocator, AWS_S3_TLS_ENABLED, 0, g_pre_existing_empty_object));
2276 }
2277
2278 AWS_TEST_CASE(test_s3_get_object_sse_kms, s_test_s3_get_object_sse_kms)
2279 static int s_test_s3_get_object_sse_kms(struct aws_allocator *allocator, void *ctx) {
2280 (void)ctx;
2281
2282 /* Keep TLS enabled for SSE related download, or it will fail. */
2283 return s_test_s3_get_object_helper(
2284 allocator, AWS_S3_TLS_ENABLED, AWS_S3_TESTER_SEND_META_REQUEST_SSE_KMS, g_pre_existing_object_kms_10MB);
2285 }
2286
2287 AWS_TEST_CASE(test_s3_get_object_sse_aes256, s_test_s3_get_object_sse_aes256)
2288 static int s_test_s3_get_object_sse_aes256(struct aws_allocator *allocator, void *ctx) {
2289 (void)ctx;
2290
2291 /* Keep TLS enabled for SSE related download, or it will fail. */
2292 return s_test_s3_get_object_helper(
2293 allocator, AWS_S3_TLS_ENABLED, AWS_S3_TESTER_SEND_META_REQUEST_SSE_AES256, g_pre_existing_object_aes256_10MB);
2294 }
2295
2296 /* Assert that GetObject can download an object whose body is XML identical to an "async error" aka "200 error":
2297 * <?xml version="1.0" encoding="UTF-8"?>\n<Error><Code>InternalError</Code>... */
2298 AWS_TEST_CASE(test_s3_get_object_looks_like_async_error_xml, s_test_s3_get_object_looks_like_async_error_xml)
2299 static int s_test_s3_get_object_looks_like_async_error_xml(struct aws_allocator *allocator, void *ctx) {
2300 (void)ctx;
2301
2302 return s_test_s3_get_object_helper(
2303 allocator, AWS_S3_TLS_ENABLED, 0 /*extra_meta_request_flag*/, g_pre_existing_object_async_error_xml);
2304 }
2305
2306 /* Same as above, but send the "GetObject" via AWS_S3_META_REQUEST_TYPE_DEFAULT
2307 * (instead of the typical AWS_S3_META_REQUEST_TYPE_GET_OBJECT) */
2308 AWS_TEST_CASE(
2309 test_s3_default_get_object_looks_like_async_error_xml,
2310 s_test_s3_default_get_object_looks_like_async_error_xml)
2311 static int s_test_s3_default_get_object_looks_like_async_error_xml(struct aws_allocator *allocator, void *ctx) {
2312 (void)ctx;
2313
2314 struct aws_s3_meta_request_test_results meta_request_test_results;
2315 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2316
2317 struct aws_s3_tester_meta_request_options options = {
2318 .allocator = allocator,
2319 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
2320 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
2321 .default_type_options =
2322 {
2323 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
2324 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
2325 },
2326 .get_options =
2327 {
2328 .object_path = g_pre_existing_object_async_error_xml,
2329 },
2330 };
2331 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, NULL));
2332
2333 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2334 return 0;
2335 }
2336
2337 /**
2338 * Test read-backpressure functionality by repeatedly:
2339 * - letting the download stall
2340 * - incrementing the read window
2341 * - repeat...
2342 */
2343 static int s_apply_backpressure_until_meta_request_finish(
2344 struct aws_s3_tester *tester,
2345 struct aws_s3_meta_request *meta_request,
2346 struct aws_s3_meta_request_test_results *test_results,
2347 size_t part_size,
2348 size_t window_initial_size,
2349 uint64_t window_increment_size) {
2350 (void)part_size;
2351
2352 /* Remember the last time something happened (we received download data, or incremented read window) */
2353 uint64_t last_time_something_happened;
2354 ASSERT_SUCCESS(aws_sys_clock_get_ticks(&last_time_something_happened));
2355
2356 /* To ensure that backpressure is working, we wait a bit after download stalls
2357 * before incrementing the read window again.
2358 * This number also controls the max time we wait for bytes to start arriving
2359 * after incrementing the window.
2360 * If the magic number is too high the test will be slow,
2361 * if it's too low the test will fail on slow networks */
2362 const uint64_t wait_duration_with_nothing_happening =
2363 aws_timestamp_convert(3, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
2364
2365 uint64_t accumulated_window_increments = window_initial_size;
2366 uint64_t accumulated_data_size = 0;
2367
2368 while (true) {
2369 /* Check if meta-request is done (don't exit yet, we want to check some numbers first...) */
2370 aws_s3_tester_lock_synced_data(tester);
2371 bool done = tester->synced_data.meta_requests_finished != 0;
2372 aws_s3_tester_unlock_synced_data(tester);
2373
2374 /* Check how much data we've received */
2375 size_t received_body_size_delta = aws_atomic_exchange_int(&test_results->received_body_size_delta, 0);
2376 accumulated_data_size += (uint64_t)received_body_size_delta;
2377
2378 /* Check that we haven't received more data than the window allows */
2379 uint64_t max_data_allowed = accumulated_window_increments;
2380 ASSERT_TRUE(
2381 accumulated_data_size <= max_data_allowed,
2382 "Received more data than the read window allows accumulated_data_size: %zu, max_data_allowed: %zu",
2383 (size_t)accumulated_data_size,
2384 (size_t)max_data_allowed);
2385
2386 /* If we're done, we're done */
2387 if (done) {
2388 break;
2389 }
2390
2391 /* Figure out how long it's been since we last received data */
2392 uint64_t current_time;
2393 ASSERT_SUCCESS(aws_sys_clock_get_ticks(&current_time));
2394
2395 if (received_body_size_delta != 0) {
2396 last_time_something_happened = current_time;
2397 }
2398
2399 uint64_t duration_since_something_happened = current_time - last_time_something_happened;
2400
2401 /* If it seems like data has stopped flowing... */
2402 if (duration_since_something_happened >= wait_duration_with_nothing_happening) {
2403
2404 /* Assert that data stopped flowing because the window reached 0. */
2405 uint64_t current_window = aws_sub_u64_saturating(accumulated_window_increments, accumulated_data_size);
2406 ASSERT_INT_EQUALS(0, current_window, "Data stopped flowing but read window isn't 0 yet.");
2407
2408 /* Open the window a bit (this resets the "something happened" timer */
2409 accumulated_window_increments += window_increment_size;
2410 aws_s3_meta_request_increment_read_window(meta_request, window_increment_size);
2411
2412 last_time_something_happened = current_time;
2413 }
2414
2415 /* Sleep a moment, and loop again... */
2416 aws_thread_current_sleep(aws_timestamp_convert(100, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
2417 }
2418
2419 return AWS_OP_SUCCESS;
2420 }
2421
2422 static int s_test_s3_get_object_backpressure_helper(
2423 struct aws_allocator *allocator,
2424 size_t part_size,
2425 size_t window_initial_size,
2426 uint64_t window_increment_size,
2427 bool file_on_disk,
2428 bool s3express) {
2429
2430 struct aws_s3_tester tester;
2431 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2432
2433 struct aws_s3_client_config client_config = {
2434 .part_size = part_size,
2435 .enable_read_backpressure = true,
2436 .enable_s3express = s3express,
2437 .initial_read_window = window_initial_size,
2438 };
2439
2440 ASSERT_SUCCESS(aws_s3_tester_bind_client(
2441 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
2442
2443 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
2444 ASSERT_NOT_NULL(client);
2445
2446 struct aws_string *host_name =
2447 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
2448 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_string(host_name);
2449 if (s3express) {
2450 host_cursor = g_test_s3express_bucket_usw2_az1_endpoint;
2451 }
2452 /* Put together a simple S3 Get Object request. */
2453 struct aws_http_message *message =
2454 aws_s3_test_get_object_request_new(allocator, host_cursor, g_pre_existing_object_1MB);
2455
2456 struct aws_signing_config_aws s3express_signing_config = {
2457 .algorithm = AWS_SIGNING_ALGORITHM_V4_S3EXPRESS,
2458 .service = g_s3express_service_name,
2459 };
2460 struct aws_s3_meta_request_options options = {
2461 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
2462 .message = message,
2463 };
2464 if (s3express) {
2465 options.signing_config = &s3express_signing_config;
2466 }
2467 struct aws_string *filepath_str = NULL;
2468 if (file_on_disk) {
2469 filepath_str = aws_s3_tester_create_file(allocator, g_pre_existing_object_1MB, NULL);
2470 options.recv_filepath = aws_byte_cursor_from_string(filepath_str);
2471 }
2472
2473 struct aws_s3_meta_request_test_results meta_request_test_results;
2474 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2475
2476 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_results));
2477
2478 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &options);
2479
2480 ASSERT_TRUE(meta_request != NULL);
2481
2482 /* Increment read window bit by bit until all data is downloaded */
2483 ASSERT_SUCCESS(s_apply_backpressure_until_meta_request_finish(
2484 &tester, meta_request, &meta_request_test_results, part_size, window_initial_size, window_increment_size));
2485
2486 aws_s3_tester_lock_synced_data(&tester);
2487
2488 ASSERT_TRUE(tester.synced_data.finish_error_code == AWS_ERROR_SUCCESS);
2489
2490 aws_s3_tester_unlock_synced_data(&tester);
2491
2492 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0));
2493
2494 /* Regression test:
2495 * Ensure that it's safe to call increment-window even after the meta-request has finished */
2496 aws_s3_meta_request_increment_read_window(meta_request, 1024);
2497
2498 meta_request = aws_s3_meta_request_release(meta_request);
2499
2500 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
2501
2502 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2503
2504 aws_http_message_release(message);
2505 message = NULL;
2506
2507 aws_string_destroy(host_name);
2508 host_name = NULL;
2509
2510 client = aws_s3_client_release(client);
2511
2512 aws_s3_tester_clean_up(&tester);
2513 if (filepath_str) {
2514 aws_file_delete(filepath_str);
2515 aws_string_destroy(filepath_str);
2516 }
2517
2518 return 0;
2519 }
2520
2521 AWS_TEST_CASE(test_s3_get_object_backpressure_small_increments, s_test_s3_get_object_backpressure_small_increments)
2522 static int s_test_s3_get_object_backpressure_small_increments(struct aws_allocator *allocator, void *ctx) {
2523 /* Test increments smaller than part-size.
2524 * Only 1 part at a time should be in flight */
2525 (void)ctx;
2526 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2527 size_t part_size = file_size / 4;
2528 size_t window_initial_size = 1024;
2529 uint64_t window_increment_size = part_size / 4;
2530 return s_test_s3_get_object_backpressure_helper(
2531 allocator, part_size, window_initial_size, window_increment_size, false, false);
2532 }
2533
2534 AWS_TEST_CASE(test_s3_get_object_backpressure_big_increments, s_test_s3_get_object_backpressure_big_increments)
2535 static int s_test_s3_get_object_backpressure_big_increments(struct aws_allocator *allocator, void *ctx) {
2536 /* Test increments larger than part-size.
2537 * Multiple parts should be in flight at a time */
2538 (void)ctx;
2539 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2540 size_t part_size = file_size / 8;
2541 size_t window_initial_size = 1024;
2542 uint64_t window_increment_size = part_size * 3;
2543 return s_test_s3_get_object_backpressure_helper(
2544 allocator, part_size, window_initial_size, window_increment_size, false, false);
2545 }
2546
2547 AWS_TEST_CASE(test_s3_get_object_backpressure_initial_size_zero, s_test_s3_get_object_backpressure_initial_size_zero)
2548 static int s_test_s3_get_object_backpressure_initial_size_zero(struct aws_allocator *allocator, void *ctx) {
2549 /* Test with initial window size of zero */
2550 (void)ctx;
2551 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2552 size_t part_size = file_size / 4;
2553 size_t window_initial_size = 0;
2554 uint64_t window_increment_size = part_size / 2;
2555 return s_test_s3_get_object_backpressure_helper(
2556 allocator, part_size, window_initial_size, window_increment_size, false, false);
2557 }
2558
2559 AWS_TEST_CASE(
2560 test_s3_get_object_backpressure_small_increments_recv_filepath,
2561 s_test_s3_get_object_backpressure_small_increments_recv_filepath)
2562 static int s_test_s3_get_object_backpressure_small_increments_recv_filepath(
2563 struct aws_allocator *allocator,
2564 void *ctx) {
2565 /* Test increments smaller than part-size.
2566 * Only 1 part at a time should be in flight */
2567 (void)ctx;
2568 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2569 size_t part_size = file_size / 4;
2570 size_t window_initial_size = 1024;
2571 uint64_t window_increment_size = part_size / 2;
2572 return s_test_s3_get_object_backpressure_helper(
2573 allocator, part_size, window_initial_size, window_increment_size, true, false);
2574 }
2575
2576 AWS_TEST_CASE(
2577 test_s3_get_object_backpressure_big_increments_recv_filepath,
2578 s_test_s3_get_object_backpressure_big_increments_recv_filepath)
2579 static int s_test_s3_get_object_backpressure_big_increments_recv_filepath(struct aws_allocator *allocator, void *ctx) {
2580 /* Test increments larger than part-size.
2581 * Multiple parts should be in flight at a time */
2582 (void)ctx;
2583 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2584 size_t part_size = file_size / 8;
2585 size_t window_initial_size = 1024;
2586 uint64_t window_increment_size = part_size * 3;
2587 return s_test_s3_get_object_backpressure_helper(
2588 allocator, part_size, window_initial_size, window_increment_size, true, false);
2589 }
2590
2591 AWS_TEST_CASE(
2592 test_s3_get_object_backpressure_initial_size_zero_recv_filepath,
2593 s_test_s3_get_object_backpressure_initial_size_zero_recv_filepath)
2594 static int s_test_s3_get_object_backpressure_initial_size_zero_recv_filepath(
2595 struct aws_allocator *allocator,
2596 void *ctx) {
2597 /* Test with initial window size of zero */
2598 (void)ctx;
2599 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2600 size_t part_size = file_size / 4;
2601 size_t window_initial_size = 0;
2602 uint64_t window_increment_size = part_size / 2;
2603 return s_test_s3_get_object_backpressure_helper(
2604 allocator, part_size, window_initial_size, window_increment_size, true, false);
2605 }
2606
2607 AWS_TEST_CASE(
2608 test_s3_get_object_backpressure_small_increments_s3express,
2609 s_test_s3_get_object_backpressure_small_increments_s3express)
2610 static int s_test_s3_get_object_backpressure_small_increments_s3express(struct aws_allocator *allocator, void *ctx) {
2611 /* Test increments smaller than part-size with S3 Express.
2612 * Only 1 part at a time should be in flight */
2613 (void)ctx;
2614 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2615 size_t part_size = file_size / 4;
2616 size_t window_initial_size = 1024;
2617 uint64_t window_increment_size = part_size / 4;
2618 return s_test_s3_get_object_backpressure_helper(
2619 allocator, part_size, window_initial_size, window_increment_size, false, true);
2620 }
2621
2622 AWS_TEST_CASE(
2623 test_s3_get_object_backpressure_big_increments_s3express,
2624 s_test_s3_get_object_backpressure_big_increments_s3express)
2625 static int s_test_s3_get_object_backpressure_big_increments_s3express(struct aws_allocator *allocator, void *ctx) {
2626 /* Test increments larger than part-size with S3 Express.
2627 * Multiple parts should be in flight at a time */
2628 (void)ctx;
2629 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2630 size_t part_size = file_size / 8;
2631 size_t window_initial_size = 1024;
2632 uint64_t window_increment_size = part_size * 3;
2633 return s_test_s3_get_object_backpressure_helper(
2634 allocator, part_size, window_initial_size, window_increment_size, false, true);
2635 }
2636
2637 AWS_TEST_CASE(
2638 test_s3_get_object_backpressure_initial_size_s3express,
2639 s_test_s3_get_object_backpressure_initial_size_s3express)
2640 static int s_test_s3_get_object_backpressure_initial_size_s3express(struct aws_allocator *allocator, void *ctx) {
2641 /* Test with initial window size of zero with S3 Express */
2642 (void)ctx;
2643 size_t file_size = 1 * 1024 * 1024; /* Test downloads 1MB file */
2644 size_t part_size = file_size / 4;
2645 size_t window_initial_size = 0;
2646 uint64_t window_increment_size = part_size / 2;
2647 return s_test_s3_get_object_backpressure_helper(
2648 allocator, part_size, window_initial_size, window_increment_size, false, true);
2649 }
2650
2651 AWS_TEST_CASE(test_s3_get_object_part, s_test_s3_get_object_part)
2652 static int s_test_s3_get_object_part(struct aws_allocator *allocator, void *ctx) {
2653 (void)ctx;
2654
2655 struct aws_s3_tester tester;
2656 AWS_ZERO_STRUCT(tester);
2657 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2658
2659 struct aws_s3_client_config client_config = {
2660 .part_size = MB_TO_BYTES(8),
2661 };
2662
2663 ASSERT_SUCCESS(aws_s3_tester_bind_client(
2664 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
2665
2666 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
2667 struct aws_s3_meta_request_test_results meta_request_test_results;
2668 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2669
2670 /*** PUT FILE ***/
2671
2672 struct aws_byte_buf path_buf;
2673 AWS_ZERO_STRUCT(path_buf);
2674
2675 ASSERT_SUCCESS(
2676 aws_s3_tester_upload_file_path_init(allocator, &path_buf, aws_byte_cursor_from_c_str("/get_object_part_test")));
2677
2678 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
2679
2680 struct aws_s3_tester_meta_request_options put_options = {
2681 .allocator = allocator,
2682 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
2683 .client = client,
2684 .put_options =
2685 {
2686 .object_size_mb = 10,
2687 .object_path_override = object_path,
2688 },
2689 };
2690
2691 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
2692
2693 /* GET FILE */
2694
2695 struct aws_s3_tester_meta_request_options options = {
2696 .allocator = allocator,
2697 .client = client,
2698 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
2699 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_NO_VALIDATE,
2700 .get_options =
2701 {
2702 .object_path = object_path,
2703 .part_number = 2,
2704 },
2705 };
2706
2707 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &meta_request_test_results));
2708
2709 ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, meta_request_test_results.finished_error_code);
2710 /* Only one request was made to get the second part of the object */
2711 ASSERT_UINT_EQUALS(1, aws_array_list_length(&meta_request_test_results.synced_data.succeed_metrics));
2712
2713 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2714
2715 client = aws_s3_client_release(client);
2716
2717 aws_byte_buf_clean_up(&path_buf);
2718 aws_s3_tester_clean_up(&tester);
2719
2720 return 0;
2721 }
2722
2723 static int s_test_s3_put_object_helper(
2724 struct aws_allocator *allocator,
2725 enum aws_s3_client_tls_usage tls_usage,
2726 uint32_t extra_meta_request_flag) {
2727 struct aws_s3_tester tester;
2728 AWS_ZERO_STRUCT(tester);
2729 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2730
2731 struct aws_tls_connection_options tls_connection_options;
2732 AWS_ZERO_STRUCT(tls_connection_options);
2733
2734 #ifndef BYO_CRYPTO
2735 struct aws_tls_ctx_options tls_context_options;
2736 aws_tls_ctx_options_init_default_client(&tls_context_options, allocator);
2737
2738 struct aws_tls_ctx *context = aws_tls_client_ctx_new(allocator, &tls_context_options);
2739 aws_tls_connection_options_init_from_ctx(&tls_connection_options, context);
2740 #endif
2741
2742 struct aws_string *endpoint =
2743 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
2744 struct aws_byte_cursor endpoint_cursor = aws_byte_cursor_from_string(endpoint);
2745
2746 tls_connection_options.server_name = aws_string_new_from_cursor(allocator, &endpoint_cursor);
2747
2748 struct aws_s3_client_config client_config = {
2749 .part_size = 5 * 1024 * 1024,
2750 };
2751
2752 switch (tls_usage) {
2753 case AWS_S3_TLS_ENABLED:
2754 client_config.tls_mode = AWS_MR_TLS_ENABLED;
2755 client_config.tls_connection_options = &tls_connection_options;
2756 break;
2757 case AWS_S3_TLS_DISABLED:
2758 client_config.tls_mode = AWS_MR_TLS_DISABLED;
2759 break;
2760 default:
2761 break;
2762 }
2763
2764 ASSERT_SUCCESS(aws_s3_tester_bind_client(
2765 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
2766
2767 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
2768
2769 ASSERT_SUCCESS(aws_s3_tester_send_put_object_meta_request(
2770 &tester, client, 10, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS | extra_meta_request_flag, NULL));
2771
2772 aws_string_destroy(endpoint);
2773
2774 #ifndef BYO_CRYPTO
2775 aws_tls_ctx_release(context);
2776 aws_tls_connection_options_clean_up(&tls_connection_options);
2777 aws_tls_ctx_options_clean_up(&tls_context_options);
2778 #endif
2779
2780 client = aws_s3_client_release(client);
2781
2782 aws_s3_tester_clean_up(&tester);
2783
2784 return 0;
2785 }
2786
2787 AWS_TEST_CASE(test_s3_put_object_tls_disabled, s_test_s3_put_object_tls_disabled)
2788 static int s_test_s3_put_object_tls_disabled(struct aws_allocator *allocator, void *ctx) {
2789 (void)ctx;
2790
2791 ASSERT_SUCCESS(s_test_s3_put_object_helper(allocator, AWS_S3_TLS_DISABLED, 0));
2792
2793 return 0;
2794 }
2795
2796 AWS_TEST_CASE(test_s3_put_object_tls_enabled, s_test_s3_put_object_tls_enabled)
2797 static int s_test_s3_put_object_tls_enabled(struct aws_allocator *allocator, void *ctx) {
2798 (void)ctx;
2799
2800 ASSERT_SUCCESS(s_test_s3_put_object_helper(allocator, AWS_S3_TLS_ENABLED, 0));
2801
2802 return 0;
2803 }
2804
2805 AWS_TEST_CASE(test_s3_put_object_tls_default, s_test_s3_put_object_tls_default)
2806 static int s_test_s3_put_object_tls_default(struct aws_allocator *allocator, void *ctx) {
2807 (void)ctx;
2808
2809 ASSERT_SUCCESS(s_test_s3_put_object_helper(allocator, AWS_S3_TLS_DEFAULT, 0));
2810
2811 return 0;
2812 }
2813
2814 AWS_TEST_CASE(test_s3_multipart_put_object_with_acl, s_test_s3_multipart_put_object_with_acl)
2815 static int s_test_s3_multipart_put_object_with_acl(struct aws_allocator *allocator, void *ctx) {
2816 (void)ctx;
2817
2818 ASSERT_SUCCESS(s_test_s3_put_object_helper(allocator, AWS_S3_TLS_DEFAULT, AWS_S3_TESTER_SEND_META_REQUEST_PUT_ACL));
2819
2820 return 0;
2821 }
2822
2823 static int s_test_s3_put_object_multiple_helper(
2824 struct aws_allocator *allocator,
2825 bool file_on_disk,
2826 bool enable_streaming) {
2827
2828 enum s_numbers { NUM_REQUESTS = 5 };
2829
2830 struct aws_s3_meta_request *meta_requests[NUM_REQUESTS];
2831 struct aws_s3_meta_request_test_results meta_request_test_results[NUM_REQUESTS];
2832 struct aws_http_message *messages[NUM_REQUESTS];
2833 struct aws_input_stream *input_streams[NUM_REQUESTS];
2834 struct aws_byte_buf input_stream_buffers[NUM_REQUESTS];
2835 struct aws_string *filepath_str[NUM_REQUESTS];
2836
2837 struct aws_s3_tester tester;
2838 AWS_ZERO_STRUCT(tester);
2839 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2840
2841 struct aws_s3_client_config client_config;
2842 AWS_ZERO_STRUCT(client_config);
2843
2844 ASSERT_SUCCESS(aws_s3_tester_bind_client(
2845 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
2846
2847 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
2848
2849 struct aws_string *host_name =
2850 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
2851
2852 /* Configure file I/O options for streaming upload */
2853 struct aws_s3_file_io_options fio_opts = {
2854 .should_stream = enable_streaming,
2855 .direct_io = true,
2856 };
2857 size_t content_length = MB_TO_BYTES(10);
2858
2859 for (size_t i = 0; i < NUM_REQUESTS; ++i) {
2860 aws_s3_meta_request_test_results_init(&meta_request_test_results[i], allocator);
2861 char object_path_buffer[128] = "";
2862 snprintf(
2863 object_path_buffer,
2864 sizeof(object_path_buffer),
2865 "" PRInSTR "-10MB-%zu.txt",
2866 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
2867 i);
2868 AWS_ZERO_STRUCT(input_stream_buffers[i]);
2869 aws_s3_create_test_buffer(allocator, content_length, &input_stream_buffers[i]);
2870 struct aws_byte_cursor test_body_cursor = aws_byte_cursor_from_buf(&input_stream_buffers[i]);
2871 input_streams[i] = aws_input_stream_new_from_cursor(allocator, &test_body_cursor);
2872 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str(object_path_buffer);
2873 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(host_name);
2874
2875 struct aws_s3_meta_request_options options;
2876 AWS_ZERO_STRUCT(options);
2877 options.type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT;
2878 if (file_on_disk) {
2879 filepath_str[i] = aws_s3_tester_create_file(allocator, test_object_path, input_streams[i]);
2880 messages[i] = aws_s3_test_put_object_request_new_without_body(
2881 allocator, &host_cur, g_test_body_content_type, test_object_path, content_length, 0 /*flags*/);
2882 options.send_filepath = aws_byte_cursor_from_string(filepath_str[i]);
2883 } else {
2884 filepath_str[i] = NULL;
2885 messages[i] = aws_s3_test_put_object_request_new(
2886 allocator, &host_cur, test_object_path, g_test_body_content_type, input_streams[i], 0);
2887 }
2888 options.message = messages[i];
2889 options.fio_opts = &fio_opts;
2890 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_results[i]));
2891
2892 /* Trigger accelerating of our Put Object request. */
2893 meta_requests[i] = aws_s3_client_make_meta_request(client, &options);
2894
2895 ASSERT_TRUE(meta_requests[i] != NULL);
2896 }
2897
2898 /* Wait for the request to finish. */
2899 aws_s3_tester_wait_for_meta_request_finish(&tester);
2900
2901 aws_s3_tester_lock_synced_data(&tester);
2902 ASSERT_TRUE(tester.synced_data.finish_error_code == AWS_ERROR_SUCCESS);
2903 aws_s3_tester_unlock_synced_data(&tester);
2904
2905 for (size_t i = 0; i < NUM_REQUESTS; ++i) {
2906 meta_requests[i] = aws_s3_meta_request_release(meta_requests[i]);
2907 }
2908
2909 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
2910
2911 for (size_t i = 0; i < NUM_REQUESTS; ++i) {
2912 aws_s3_tester_validate_get_object_results(&meta_request_test_results[i], 0);
2913 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results[i]);
2914 }
2915
2916 for (size_t i = 0; i < NUM_REQUESTS; ++i) {
2917 aws_http_message_release(messages[i]);
2918 aws_input_stream_release(input_streams[i]);
2919 aws_byte_buf_clean_up(&input_stream_buffers[i]);
2920 if (filepath_str[i]) {
2921 ASSERT_SUCCESS(aws_file_delete(filepath_str[i]));
2922 aws_string_destroy(filepath_str[i]);
2923 }
2924 }
2925
2926 aws_string_destroy(host_name);
2927 host_name = NULL;
2928
2929 client = aws_s3_client_release(client);
2930
2931 aws_s3_tester_clean_up(&tester);
2932
2933 return 0;
2934 }
2935
2936 AWS_TEST_CASE(test_s3_put_object_multiple, s_test_s3_put_object_multiple)
2937 static int s_test_s3_put_object_multiple(struct aws_allocator *allocator, void *ctx) {
2938 (void)ctx;
2939 return s_test_s3_put_object_multiple_helper(allocator, false, false);
2940 }
2941
2942 AWS_TEST_CASE(test_s3_put_object_multiple_with_filepath, s_test_s3_put_object_multiple_with_filepath)
2943 static int s_test_s3_put_object_multiple_with_filepath(struct aws_allocator *allocator, void *ctx) {
2944 (void)ctx;
2945 return s_test_s3_put_object_multiple_helper(allocator, true, false);
2946 }
2947
2948 AWS_TEST_CASE(
2949 test_s3_put_object_multiple_with_filepath_streaming,
2950 s_test_s3_put_object_multiple_with_filepath_streaming)
2951 static int s_test_s3_put_object_multiple_with_filepath_streaming(struct aws_allocator *allocator, void *ctx) {
2952 (void)ctx;
2953 return s_test_s3_put_object_multiple_helper(allocator, true, true);
2954 }
2955
2956 /*
2957 * Helper that simulates the situation where the checksum value is provided in the message on put,
2958 * but also checksum config is specified.
2959 */
2960 static int s_test_s3_put_object_with_checksum_header_helper(struct aws_allocator *allocator) {
2961
2962 struct aws_s3_meta_request *meta_request;
2963 struct aws_s3_meta_request_test_results meta_request_test_result;
2964 struct aws_http_message *message;
2965 struct aws_input_stream *input_stream;
2966 struct aws_byte_buf input_stream_buffer;
2967
2968 struct aws_s3_tester tester;
2969 AWS_ZERO_STRUCT(tester);
2970 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
2971
2972 struct aws_s3_client_config client_config;
2973 AWS_ZERO_STRUCT(client_config);
2974
2975 ASSERT_SUCCESS(aws_s3_tester_bind_client(
2976 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
2977
2978 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
2979
2980 struct aws_string *host_name =
2981 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
2982
2983 size_t content_length = MB_TO_BYTES(20);
2984
2985 aws_s3_meta_request_test_results_init(&meta_request_test_result, allocator);
2986 char object_path_buffer[128] = "";
2987 snprintf(
2988 object_path_buffer,
2989 sizeof(object_path_buffer),
2990 "" PRInSTR "-20MB-custom.txt",
2991 AWS_BYTE_CURSOR_PRI(g_put_object_prefix));
2992 AWS_ZERO_STRUCT(input_stream_buffer);
2993 aws_s3_create_test_buffer(allocator, content_length, &input_stream_buffer);
2994 struct aws_byte_cursor test_body_cursor = aws_byte_cursor_from_buf(&input_stream_buffer);
2995 input_stream = aws_input_stream_new_from_cursor(allocator, &test_body_cursor);
2996 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str(object_path_buffer);
2997 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(host_name);
2998
2999 struct aws_s3_meta_request_options options;
3000 AWS_ZERO_STRUCT(options);
3001 options.type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT;
3002 message = aws_s3_test_put_object_request_new(
3003 allocator, &host_cur, test_object_path, g_test_body_content_type, input_stream, 0);
3004
3005 struct aws_http_header checksum_header = {
3006 .name = aws_byte_cursor_from_c_str("x-amz-checksum-md5"),
3007 .value = aws_byte_cursor_from_c_str("9Z7oAj6JEFbH2PcvG+6V+w=="),
3008 };
3009
3010 aws_http_message_add_header(message, checksum_header);
3011
3012 options.message = message;
3013 struct aws_s3_checksum_config checksum_config = {
3014 .checksum_algorithm = AWS_SCA_CRC32,
3015 .location = AWS_SCL_TRAILER,
3016 };
3017
3018 options.checksum_config = &checksum_config;
3019
3020 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_result));
3021
3022 meta_request = aws_s3_client_make_meta_request(client, &options);
3023
3024 ASSERT_TRUE(meta_request != NULL);
3025
3026 /* Wait for the request to finish. */
3027 aws_s3_tester_wait_for_meta_request_finish(&tester);
3028
3029 aws_s3_tester_lock_synced_data(&tester);
3030 ASSERT_TRUE(tester.synced_data.finish_error_code != AWS_OP_SUCCESS);
3031
3032 aws_s3_tester_unlock_synced_data(&tester);
3033
3034 meta_request = aws_s3_meta_request_release(meta_request);
3035
3036 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
3037
3038 ASSERT_INT_EQUALS(meta_request_test_result.finished_response_status, 400);
3039
3040 struct aws_byte_cursor body = aws_byte_cursor_from_buf(&meta_request_test_result.error_response_body);
3041 struct aws_byte_cursor to_find =
3042 aws_byte_cursor_from_c_str("The FULL_OBJECT checksum type cannot be used with the md5 checksum algorithm");
3043 struct aws_byte_cursor result_cur = {0};
3044 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&body, &to_find, &result_cur));
3045
3046 aws_s3_meta_request_test_results_clean_up(&meta_request_test_result);
3047
3048 aws_http_message_release(message);
3049 aws_input_stream_release(input_stream);
3050 aws_byte_buf_clean_up(&input_stream_buffer);
3051
3052 aws_string_destroy(host_name);
3053 host_name = NULL;
3054
3055 client = aws_s3_client_release(client);
3056
3057 aws_s3_tester_clean_up(&tester);
3058
3059 return 0;
3060 }
3061
3062 AWS_TEST_CASE(test_s3_put_object_custom_md5, s_test_s3_put_object_custom_md5)
3063 static int s_test_s3_put_object_custom_md5(struct aws_allocator *allocator, void *ctx) {
3064 (void)ctx;
3065 return s_test_s3_put_object_with_checksum_header_helper(allocator);
3066 }
3067
3068 AWS_TEST_CASE(test_s3_put_object_less_than_part_size, s_test_s3_put_object_less_than_part_size)
3069 static int s_test_s3_put_object_less_than_part_size(struct aws_allocator *allocator, void *ctx) {
3070 (void)ctx;
3071
3072 struct aws_s3_tester tester;
3073 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3074
3075 struct aws_s3_client_config client_config = {
3076 .part_size = 20 * 1024 * 1024,
3077 };
3078
3079 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3080 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3081
3082 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3083
3084 ASSERT_TRUE(client != NULL);
3085
3086 struct aws_s3_tester_meta_request_options put_options = {
3087 .allocator = allocator,
3088 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3089 .client = client,
3090 .put_options =
3091 {
3092 .object_size_mb = 1,
3093 },
3094 };
3095
3096 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3097
3098 client = aws_s3_client_release(client);
3099
3100 aws_s3_tester_clean_up(&tester);
3101
3102 return 0;
3103 }
3104
3105 AWS_TEST_CASE(test_s3_put_object_buffer_pool_trim, s_test_s3_put_object_buffer_pool_trim)
3106 static int s_test_s3_put_object_buffer_pool_trim(struct aws_allocator *allocator, void *ctx) {
3107 (void)ctx;
3108
3109 struct aws_s3_tester tester;
3110 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3111
3112 struct aws_s3_client_config client_config = {
3113 .part_size = 8 * 1024 * 1024,
3114 };
3115
3116 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3117 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3118
3119 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3120
3121 ASSERT_TRUE(client != NULL);
3122
3123 struct aws_s3_tester_meta_request_options put_options = {
3124 .allocator = allocator,
3125 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3126 .client = client,
3127 .put_options =
3128 {
3129 .object_size_mb = 32,
3130 },
3131 };
3132
3133 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3134
3135 struct aws_s3_default_buffer_pool_usage_stats usage_before =
3136 aws_s3_default_buffer_pool_get_usage(client->buffer_pool);
3137
3138 ASSERT_TRUE(0 != usage_before.primary_num_blocks);
3139
3140 aws_thread_current_sleep(aws_timestamp_convert(6, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
3141
3142 struct aws_s3_default_buffer_pool_usage_stats usage_after =
3143 aws_s3_default_buffer_pool_get_usage(client->buffer_pool);
3144
3145 ASSERT_INT_EQUALS(0, usage_after.primary_num_blocks);
3146
3147 client = aws_s3_client_release(client);
3148
3149 aws_s3_tester_clean_up(&tester);
3150
3151 return 0;
3152 }
3153
3154 AWS_TEST_CASE(
3155 test_s3_put_object_less_than_part_size_with_content_encoding,
3156 s_test_s3_put_object_less_than_part_size_with_content_encoding)
3157 static int s_test_s3_put_object_less_than_part_size_with_content_encoding(struct aws_allocator *allocator, void *ctx) {
3158 (void)ctx;
3159
3160 struct aws_s3_tester tester;
3161 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3162
3163 struct aws_s3_client_config client_config = {
3164 .part_size = 20 * 1024 * 1024,
3165 };
3166
3167 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3168 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3169
3170 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3171
3172 ASSERT_TRUE(client != NULL);
3173
3174 struct aws_byte_cursor content_encoding_cursor = aws_byte_cursor_from_c_str("gzip");
3175 uint32_t object_size_mb = 1;
3176 char object_path_sprintf_buffer[128] = "";
3177 snprintf(
3178 object_path_sprintf_buffer,
3179 sizeof(object_path_sprintf_buffer),
3180 "" PRInSTR "-content-encoding-%uMB.txt",
3181 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
3182 object_size_mb);
3183 struct aws_byte_cursor object_path_cursor = aws_byte_cursor_from_c_str(object_path_sprintf_buffer);
3184
3185 /*** put file with encoding ***/
3186 struct aws_s3_tester_meta_request_options put_options = {
3187 .allocator = allocator,
3188 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3189 .client = client,
3190 .checksum_algorithm = AWS_SCA_SHA256,
3191 .put_options =
3192 {
3193 .object_size_mb = object_size_mb,
3194 .object_path_override = object_path_cursor,
3195 .content_encoding = content_encoding_cursor,
3196 },
3197 };
3198
3199 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3200
3201 /*** get file and validate encoding ***/
3202 struct aws_s3_tester_meta_request_options get_options = {
3203 .allocator = allocator,
3204 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
3205 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
3206 .client = client,
3207 .get_options =
3208 {
3209 .object_path = object_path_cursor,
3210 },
3211 };
3212
3213 struct aws_s3_meta_request_test_results get_object_result;
3214 aws_s3_meta_request_test_results_init(&get_object_result, allocator);
3215
3216 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &get_object_result));
3217 struct aws_byte_cursor content_encoding_header_cursor;
3218 ASSERT_SUCCESS(aws_http_headers_get(
3219 get_object_result.response_headers, g_content_encoding_header_name, &content_encoding_header_cursor));
3220 ASSERT_TRUE(aws_byte_cursor_eq(&content_encoding_cursor, &content_encoding_header_cursor));
3221 aws_s3_meta_request_test_results_clean_up(&get_object_result);
3222
3223 client = aws_s3_client_release(client);
3224
3225 aws_s3_tester_clean_up(&tester);
3226
3227 return 0;
3228 }
3229
3230 AWS_TEST_CASE(test_s3_put_object_mpu_with_content_encoding, s_test_s3_put_object_mpu_with_content_encoding)
3231 static int s_test_s3_put_object_mpu_with_content_encoding(struct aws_allocator *allocator, void *ctx) {
3232 (void)ctx;
3233
3234 struct aws_s3_tester tester;
3235 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3236
3237 struct aws_s3_client_config client_config = {
3238 .part_size = 5 * 1024 * 1024,
3239 };
3240
3241 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3242 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3243
3244 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3245
3246 ASSERT_TRUE(client != NULL);
3247
3248 struct aws_byte_cursor content_encoding_cursor = aws_byte_cursor_from_c_str("gzip");
3249 uint32_t object_size_mb = 10;
3250 char object_path_sprintf_buffer[128] = "";
3251 snprintf(
3252 object_path_sprintf_buffer,
3253 sizeof(object_path_sprintf_buffer),
3254 "" PRInSTR "-content-encoding-%uMB.txt",
3255 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
3256 object_size_mb);
3257 struct aws_byte_cursor object_path_cursor = aws_byte_cursor_from_c_str(object_path_sprintf_buffer);
3258
3259 /*** put file with encoding ***/
3260 struct aws_s3_tester_meta_request_options put_options = {
3261 .allocator = allocator,
3262 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3263 .client = client,
3264 .checksum_algorithm = AWS_SCA_SHA256,
3265 .put_options =
3266 {
3267 .object_size_mb = object_size_mb,
3268 .object_path_override = object_path_cursor,
3269 .content_encoding = content_encoding_cursor,
3270 .ensure_multipart = true,
3271 },
3272 };
3273
3274 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3275
3276 /*** get file and validate encoding ***/
3277 struct aws_s3_tester_meta_request_options get_options = {
3278 .allocator = allocator,
3279 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
3280 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
3281 .client = client,
3282 .get_options =
3283 {
3284 .object_path = object_path_cursor,
3285 },
3286 };
3287
3288 struct aws_s3_meta_request_test_results get_object_result;
3289 aws_s3_meta_request_test_results_init(&get_object_result, allocator);
3290
3291 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &get_object_result));
3292 struct aws_byte_cursor content_encoding_header_cursor;
3293 ASSERT_SUCCESS(aws_http_headers_get(
3294 get_object_result.response_headers, g_content_encoding_header_name, &content_encoding_header_cursor));
3295 ASSERT_TRUE(aws_byte_cursor_eq(&content_encoding_cursor, &content_encoding_header_cursor));
3296 aws_s3_meta_request_test_results_clean_up(&get_object_result);
3297
3298 client = aws_s3_client_release(client);
3299
3300 aws_s3_tester_clean_up(&tester);
3301
3302 return 0;
3303 }
3304
3305 AWS_TEST_CASE(test_s3_put_object_multipart_threshold, s_test_s3_put_object_multipart_threshold)
3306 static int s_test_s3_put_object_multipart_threshold(struct aws_allocator *allocator, void *ctx) {
3307 (void)ctx;
3308
3309 struct aws_s3_tester tester;
3310 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3311
3312 struct aws_s3_client_config client_config = {
3313 .part_size = MB_TO_BYTES(8),
3314 .multipart_upload_threshold = MB_TO_BYTES(15),
3315 };
3316
3317 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3318 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3319
3320 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3321
3322 ASSERT_TRUE(client != NULL);
3323
3324 /* First smaller than the part size */
3325 struct aws_s3_tester_meta_request_options put_options = {
3326 .allocator = allocator,
3327 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3328 .client = client,
3329 .put_options =
3330 {
3331 .object_size_mb = 5,
3332 },
3333 };
3334 struct aws_s3_meta_request_test_results meta_request_test_results;
3335 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
3336
3337 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &meta_request_test_results));
3338 /* Result in a single part upload, and have 0 as part size */
3339 ASSERT_UINT_EQUALS(0, meta_request_test_results.part_size);
3340 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
3341
3342 /* Second smaller than threshold and larger than part size */
3343 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
3344 put_options.put_options.object_size_mb = 10;
3345 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &meta_request_test_results));
3346 /* Result in a single part upload, and have 0 as part size */
3347 ASSERT_UINT_EQUALS(0, meta_request_test_results.part_size);
3348 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
3349
3350 /* Third larger than threshold*/
3351 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
3352 put_options.put_options.object_size_mb = 20;
3353 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &meta_request_test_results));
3354 /* Result in multi-part upload, and have the real part size */
3355 ASSERT_UINT_EQUALS(client_config.part_size, meta_request_test_results.part_size);
3356 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
3357 client = aws_s3_client_release(client);
3358
3359 aws_s3_tester_clean_up(&tester);
3360
3361 return 0;
3362 }
3363
3364 AWS_TEST_CASE(
3365 test_s3_put_object_multipart_threshold_less_than_part_size,
3366 s_test_s3_put_object_multipart_threshold_less_than_part_size)
3367 static int s_test_s3_put_object_multipart_threshold_less_than_part_size(struct aws_allocator *allocator, void *ctx) {
3368 (void)ctx;
3369
3370 struct aws_s3_tester tester;
3371 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3372
3373 struct aws_s3_client_config client_config = {
3374 .part_size = MB_TO_BYTES(8),
3375 .multipart_upload_threshold = MB_TO_BYTES(5),
3376 };
3377
3378 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3379 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3380
3381 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3382
3383 ASSERT_TRUE(client != NULL);
3384
3385 /* First smaller than the part size */
3386 struct aws_s3_tester_meta_request_options put_options = {
3387 .allocator = allocator,
3388 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3389 .client = client,
3390 .put_options =
3391 {
3392 .object_size_mb = 6,
3393 },
3394 };
3395 struct aws_s3_meta_request_test_results meta_request_test_results;
3396 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
3397
3398 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &meta_request_test_results));
3399 /* Result in a one part of multipart upload, and have the content length as part size */
3400 ASSERT_UINT_EQUALS(MB_TO_BYTES(put_options.put_options.object_size_mb), meta_request_test_results.part_size);
3401 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
3402
3403 client = aws_s3_client_release(client);
3404
3405 aws_s3_tester_clean_up(&tester);
3406
3407 return 0;
3408 }
3409
3410 AWS_TEST_CASE(test_s3_put_object_empty_object, s_test_s3_put_object_empty_object)
3411 static int s_test_s3_put_object_empty_object(struct aws_allocator *allocator, void *ctx) {
3412 (void)ctx;
3413
3414 struct aws_s3_tester tester;
3415 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3416
3417 struct aws_s3_client_config client_config;
3418 AWS_ZERO_STRUCT(client_config);
3419
3420 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3421 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3422
3423 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3424
3425 ASSERT_TRUE(client != NULL);
3426
3427 struct aws_s3_tester_meta_request_options put_options = {
3428 .allocator = allocator,
3429 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3430 .client = client,
3431 .put_options =
3432 {
3433 .object_size_mb = 0,
3434 },
3435 };
3436
3437 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3438
3439 client = aws_s3_client_release(client);
3440
3441 aws_s3_tester_clean_up(&tester);
3442
3443 return 0;
3444 }
3445
3446 static int s3_no_content_length_test_helper(
3447 struct aws_allocator *allocator,
3448 void *ctx,
3449 uint32_t object_size_in_mb,
3450 bool use_checksum) {
3451 (void)ctx;
3452
3453 struct aws_s3_tester tester;
3454 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3455
3456 struct aws_s3_client_config client_config = {
3457 .part_size = MB_TO_BYTES(8),
3458 };
3459
3460 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3461 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3462
3463 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3464
3465 ASSERT_TRUE(client != NULL);
3466
3467 struct aws_s3_tester_meta_request_options put_options = {
3468 .allocator = allocator,
3469 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3470 .client = client,
3471 .checksum_algorithm = use_checksum ? AWS_SCA_CRC32 : AWS_SCA_NONE,
3472 .put_options =
3473 {
3474 .object_size_mb = object_size_in_mb,
3475 .skip_content_length = true,
3476 },
3477 };
3478 struct aws_s3_meta_request_test_results meta_request_test_results;
3479 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
3480
3481 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &meta_request_test_results));
3482 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
3483
3484 aws_s3_client_release(client);
3485
3486 aws_s3_tester_clean_up(&tester);
3487
3488 return 0;
3489 }
3490
3491 AWS_TEST_CASE(test_s3_put_object_no_content_length, s_test_s3_put_object_no_content_length)
3492 static int s_test_s3_put_object_no_content_length(struct aws_allocator *allocator, void *ctx) {
3493 ASSERT_SUCCESS(s3_no_content_length_test_helper(allocator, ctx, 19, false));
3494
3495 return 0;
3496 }
3497
3498 AWS_TEST_CASE(test_s3_put_object_single_part_no_content_length, s_test_s3_put_object_single_part_no_content_length)
3499 static int s_test_s3_put_object_single_part_no_content_length(struct aws_allocator *allocator, void *ctx) {
3500 ASSERT_SUCCESS(s3_no_content_length_test_helper(allocator, ctx, 5, false));
3501
3502 return 0;
3503 }
3504
3505 AWS_TEST_CASE(test_s3_put_object_zero_size_no_content_length, s_test_s3_put_object_zero_size_no_content_length)
3506 static int s_test_s3_put_object_zero_size_no_content_length(struct aws_allocator *allocator, void *ctx) {
3507 ASSERT_SUCCESS(s3_no_content_length_test_helper(allocator, ctx, 0, false));
3508
3509 return 0;
3510 }
3511
3512 AWS_TEST_CASE(
3513 test_s3_put_large_object_no_content_length_with_checksum,
3514 s_test_s3_put_large_object_no_content_length_with_checksum)
3515 static int s_test_s3_put_large_object_no_content_length_with_checksum(struct aws_allocator *allocator, void *ctx) {
3516 ASSERT_SUCCESS(s3_no_content_length_test_helper(allocator, ctx, 128, true));
3517
3518 return 0;
3519 }
3520
3521 /**
3522 * Once upon a time, we have a bug that without content-length, we will schedule more requests to prepare than needed.
3523 * And those extra request will be cleaned up, however, the client level count of `num_requests_being_prepared` will
3524 * still keep record for those.
3525 *
3526 * To reproduce, we create bunch of requests with less than a part body. And then sleep for a while to let dns resolve
3527 * purge all records. (Otherwise, we will always have one valid request to be available to send.) to trigger not going
3528 * full speed code. And we will hang.
3529 *
3530 */
3531 AWS_TEST_CASE(test_s3_put_object_no_content_length_multiple, s_test_s3_put_object_no_content_length_multiple)
3532 static int s_test_s3_put_object_no_content_length_multiple(struct aws_allocator *allocator, void *ctx) {
3533 (void)ctx;
3534
3535 struct aws_s3_tester tester;
3536 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3537
3538 struct aws_s3_client_config client_config = {
3539 .part_size = MB_TO_BYTES(8),
3540 };
3541
3542 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3543 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3544
3545 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3546
3547 aws_s3_set_dns_ttl(55);
3548
3549 ASSERT_TRUE(client != NULL);
3550 struct aws_s3_tester_meta_request_options put_options = {
3551 .allocator = allocator,
3552 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3553 .client = client,
3554 .checksum_algorithm = AWS_SCA_CRC32,
3555 .put_options =
3556 {
3557 .object_size_mb = 1,
3558 .skip_content_length = true,
3559 },
3560 };
3561 for (int i = 0; i < 6; i++) {
3562 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3563 }
3564 /* Sleep more than the DNS ttl to purge all records. */
3565 aws_thread_current_sleep(aws_timestamp_convert(60, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
3566
3567 /* After sleep for a while, make another meta request */
3568 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3569
3570 aws_s3_client_release(client);
3571
3572 aws_s3_tester_clean_up(&tester);
3573
3574 return 0;
3575 }
3576
3577 /* Test async-input-stream when we're not doing multipart upload */
3578 AWS_TEST_CASE(test_s3_put_object_async_singlepart, s_test_s3_put_object_async_singlepart)
3579 static int s_test_s3_put_object_async_singlepart(struct aws_allocator *allocator, void *ctx) {
3580 (void)ctx;
3581
3582 struct aws_s3_meta_request_test_results test_results;
3583 aws_s3_meta_request_test_results_init(&test_results, allocator);
3584
3585 struct aws_s3_tester_meta_request_options put_options = {
3586 .allocator = allocator,
3587 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3588 .put_options =
3589 {
3590 .object_size_mb = 4,
3591 .async_input_stream = true,
3592 },
3593 };
3594 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3595
3596 aws_s3_meta_request_test_results_clean_up(&test_results);
3597 return 0;
3598 }
3599
3600 /* Test async-input-stream in multipart upload */
3601 AWS_TEST_CASE(test_s3_put_object_async_multipart, s_test_s3_put_object_async_multipart)
3602 static int s_test_s3_put_object_async_multipart(struct aws_allocator *allocator, void *ctx) {
3603 (void)ctx;
3604
3605 struct aws_s3_meta_request_test_results test_results;
3606 aws_s3_meta_request_test_results_init(&test_results, allocator);
3607
3608 struct aws_s3_tester_meta_request_options put_options = {
3609 .allocator = allocator,
3610 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3611 .put_options =
3612 {
3613 .object_size_mb = 16,
3614 .async_input_stream = true,
3615 },
3616 };
3617 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3618
3619 aws_s3_meta_request_test_results_clean_up(&test_results);
3620 return 0;
3621 }
3622
3623 /* Test async-input-stream, but the aws_async_input_stream_read() calls all complete synchronously */
3624 AWS_TEST_CASE(
3625 test_s3_put_object_async_read_completes_synchronously,
3626 s_test_s3_put_object_async_read_completes_synchronously)
3627 static int s_test_s3_put_object_async_read_completes_synchronously(struct aws_allocator *allocator, void *ctx) {
3628 (void)ctx;
3629
3630 struct aws_s3_meta_request_test_results test_results;
3631 aws_s3_meta_request_test_results_init(&test_results, allocator);
3632
3633 struct aws_s3_tester_meta_request_options put_options = {
3634 .allocator = allocator,
3635 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3636 .put_options =
3637 {
3638 .object_size_mb = 10,
3639 .async_input_stream = true,
3640 .async_read_strategy = AWS_ASYNC_READ_COMPLETES_IMMEDIATELY,
3641 },
3642 };
3643 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3644
3645 aws_s3_meta_request_test_results_clean_up(&test_results);
3646 return 0;
3647 }
3648
3649 /* Test async-input-stream, where it takes multiple read() calls to fill each part */
3650 AWS_TEST_CASE(test_s3_put_object_async_small_reads, s_test_s3_put_object_async_small_reads)
3651 static int s_test_s3_put_object_async_small_reads(struct aws_allocator *allocator, void *ctx) {
3652 (void)ctx;
3653
3654 struct aws_s3_meta_request_test_results test_results;
3655 aws_s3_meta_request_test_results_init(&test_results, allocator);
3656
3657 struct aws_s3_tester_meta_request_options put_options = {
3658 .allocator = allocator,
3659 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3660 .put_options =
3661 {
3662 .object_size_mb = 10,
3663 .async_input_stream = true,
3664 .max_bytes_per_read = KB_TO_BYTES(1001), /* something that doesn't evenly divide into 8MB parts */
3665 },
3666 };
3667 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3668
3669 aws_s3_meta_request_test_results_clean_up(&test_results);
3670 return 0;
3671 }
3672
3673 /* Test synchronous input-stream, where it takes multiple read() calls to fill each part */
3674 AWS_TEST_CASE(test_s3_put_object_small_reads, s_test_s3_put_object_small_reads)
3675 static int s_test_s3_put_object_small_reads(struct aws_allocator *allocator, void *ctx) {
3676 (void)ctx;
3677
3678 struct aws_s3_meta_request_test_results test_results;
3679 aws_s3_meta_request_test_results_init(&test_results, allocator);
3680
3681 struct aws_s3_tester_meta_request_options put_options = {
3682 .allocator = allocator,
3683 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3684 .put_options =
3685 {
3686 .object_size_mb = 10,
3687 .max_bytes_per_read = KB_TO_BYTES(1001), /* something that doesn't evenly divide into 8MB parts */
3688 },
3689 };
3690 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3691
3692 aws_s3_meta_request_test_results_clean_up(&test_results);
3693 return 0;
3694 }
3695
3696 /* Test async-input-stream, with undeclared Content-Length, that doesn't end exactly on a part boundary */
3697 AWS_TEST_CASE(
3698 test_s3_put_object_async_no_content_length_partial_part,
3699 s_test_s3_put_object_async_no_content_length_partial_part)
3700 static int s_test_s3_put_object_async_no_content_length_partial_part(struct aws_allocator *allocator, void *ctx) {
3701 (void)ctx;
3702
3703 struct aws_s3_meta_request_test_results test_results;
3704 aws_s3_meta_request_test_results_init(&test_results, allocator);
3705
3706 struct aws_s3_tester_meta_request_options put_options = {
3707 .allocator = allocator,
3708 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3709 .put_options =
3710 {
3711 .object_size_mb = 3,
3712 .async_input_stream = true,
3713 .skip_content_length = true,
3714 },
3715 };
3716 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3717
3718 aws_s3_meta_request_test_results_clean_up(&test_results);
3719 return 0;
3720 }
3721
3722 /* Test async-input-stream, with undeclared Content-Length, that fills exactly 1 part */
3723 AWS_TEST_CASE(test_s3_put_object_async_no_content_length_1part, s_test_s3_put_object_async_no_content_length_1part)
3724 static int s_test_s3_put_object_async_no_content_length_1part(struct aws_allocator *allocator, void *ctx) {
3725 (void)ctx;
3726
3727 struct aws_s3_meta_request_test_results test_results;
3728 aws_s3_meta_request_test_results_init(&test_results, allocator);
3729
3730 struct aws_s3_tester_meta_request_options put_options = {
3731 .allocator = allocator,
3732 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3733 .put_options =
3734 {
3735 .object_size_mb = 8,
3736 .async_input_stream = true,
3737 .skip_content_length = true,
3738 },
3739 };
3740 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3741
3742 aws_s3_meta_request_test_results_clean_up(&test_results);
3743 return 0;
3744 }
3745
3746 /* Test async-input-stream, with undeclared Content-Length, that doesn't realize
3747 * it's at EOF until it tries to read the 2nd part and gets 0 bytes */
3748 AWS_TEST_CASE(
3749 test_s3_put_object_async_no_content_length_empty_part2,
3750 s_test_s3_put_object_async_no_content_length_empty_part2)
3751 static int s_test_s3_put_object_async_no_content_length_empty_part2(struct aws_allocator *allocator, void *ctx) {
3752 (void)ctx;
3753
3754 struct aws_s3_meta_request_test_results test_results;
3755 aws_s3_meta_request_test_results_init(&test_results, allocator);
3756
3757 struct aws_s3_tester_meta_request_options put_options = {
3758 .allocator = allocator,
3759 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3760 .put_options =
3761 {
3762 .object_size_mb = 8, /* read 1 part's worth of data */
3763 .eof_requires_extra_read = true, /* don't report EOF until it tries to read 2nd part */
3764 .async_input_stream = true,
3765 .skip_content_length = true,
3766 },
3767 };
3768 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3769
3770 aws_s3_meta_request_test_results_clean_up(&test_results);
3771 return 0;
3772 }
3773
3774 /* Test async-input-stream, with undeclared Content-Length, that fills multiple parts */
3775 AWS_TEST_CASE(test_s3_put_object_async_no_content_length_2parts, s_test_s3_put_object_async_no_content_length_2parts)
3776 static int s_test_s3_put_object_async_no_content_length_2parts(struct aws_allocator *allocator, void *ctx) {
3777 (void)ctx;
3778
3779 struct aws_s3_meta_request_test_results test_results;
3780 aws_s3_meta_request_test_results_init(&test_results, allocator);
3781
3782 struct aws_s3_tester_meta_request_options put_options = {
3783 .allocator = allocator,
3784 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3785 .put_options =
3786 {
3787 .object_size_mb = 16,
3788 .async_input_stream = true,
3789 .skip_content_length = true,
3790 },
3791 };
3792 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3793
3794 aws_s3_meta_request_test_results_clean_up(&test_results);
3795 return 0;
3796 }
3797
3798 AWS_TEST_CASE(test_s3_put_object_async_fail_reading, s_test_s3_put_object_async_fail_reading)
3799 static int s_test_s3_put_object_async_fail_reading(struct aws_allocator *allocator, void *ctx) {
3800 (void)ctx;
3801
3802 struct aws_s3_meta_request_test_results test_results;
3803 aws_s3_meta_request_test_results_init(&test_results, allocator);
3804
3805 struct aws_s3_tester_meta_request_options put_options = {
3806 .allocator = allocator,
3807 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3808 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
3809 .put_options =
3810 {
3811 .object_size_mb = 10,
3812 .async_input_stream = true,
3813 .invalid_input_stream = true,
3814 },
3815 };
3816 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3817
3818 ASSERT_INT_EQUALS(AWS_IO_STREAM_READ_FAILED, test_results.finished_error_code);
3819
3820 aws_s3_meta_request_test_results_clean_up(&test_results);
3821 return 0;
3822 }
3823
3824 static int s_test_validate_if_none_match_failure_response(struct aws_s3_meta_request_test_results *test_results) {
3825
3826 /**
3827 * response body should be like:
3828 * <Error>
3829 * <Code>PreconditionFailed</Code>
3830 * <Message>At least one of the pre-conditions you specified did not hold</Message>
3831 * <Condition>If-None-Match</Condition>
3832 * <RequestId></RequestId>
3833 * <HostId></HostId>
3834 * </Error>
3835 */
3836
3837 struct aws_byte_cursor xml_doc = aws_byte_cursor_from_buf(&test_results->error_response_body);
3838 struct aws_byte_cursor error_code_string = {0};
3839 struct aws_byte_cursor condition_string = {0};
3840
3841 const char *error_code_path[] = {"Error", "Code", NULL};
3842 ASSERT_SUCCESS(aws_xml_get_body_at_path(test_results->allocator, xml_doc, error_code_path, &error_code_string));
3843 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&error_code_string, "PreconditionFailed"));
3844
3845 const char *condition_path[] = {"Error", "Condition", NULL};
3846 ASSERT_SUCCESS(aws_xml_get_body_at_path(test_results->allocator, xml_doc, condition_path, &condition_string));
3847 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&condition_string, "If-None-Match"));
3848
3849 return AWS_OP_SUCCESS;
3850 }
3851
3852 AWS_TEST_CASE(test_s3_put_object_if_none_match, s_test_s3_put_object_if_none_match)
3853 static int s_test_s3_put_object_if_none_match(struct aws_allocator *allocator, void *ctx) {
3854 (void)ctx;
3855
3856 struct aws_s3_meta_request_test_results test_results;
3857 aws_s3_meta_request_test_results_init(&test_results, allocator);
3858 struct aws_byte_cursor if_none_match_all = aws_byte_cursor_from_c_str("*");
3859 struct aws_s3_tester_meta_request_options put_options = {
3860 .allocator = allocator,
3861 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3862 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
3863 .put_options =
3864 {
3865 /* Use pre_exist object so that the request should fail with the expected failure message. */
3866 .object_path_override = g_pre_existing_object_1MB,
3867 .object_size_mb = 1,
3868 .if_none_match_header = if_none_match_all,
3869 },
3870 };
3871 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3872
3873 ASSERT_UINT_EQUALS(AWS_HTTP_STATUS_CODE_412_PRECONDITION_FAILED, test_results.finished_response_status);
3874 ASSERT_SUCCESS(s_test_validate_if_none_match_failure_response(&test_results));
3875
3876 aws_s3_meta_request_test_results_clean_up(&test_results);
3877 return 0;
3878 }
3879
3880 AWS_TEST_CASE(test_s3_put_object_mpu_if_none_match, s_test_s3_put_object_mpu_if_none_match)
3881 static int s_test_s3_put_object_mpu_if_none_match(struct aws_allocator *allocator, void *ctx) {
3882 (void)ctx;
3883
3884 struct aws_s3_meta_request_test_results test_results;
3885 aws_s3_meta_request_test_results_init(&test_results, allocator);
3886 struct aws_byte_cursor if_none_match_all = aws_byte_cursor_from_c_str("*");
3887 struct aws_s3_tester_meta_request_options put_options = {
3888 .allocator = allocator,
3889 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3890 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
3891 .put_options =
3892 {
3893 /* Use pre_exist object so that the request should fail with the expected failure message. */
3894 .object_path_override = g_pre_existing_object_10MB,
3895 .object_size_mb = 10,
3896 .if_none_match_header = if_none_match_all,
3897 },
3898 };
3899 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
3900
3901 /** Complete MPU can fail with 200 error */
3902 ASSERT_TRUE(
3903 AWS_HTTP_STATUS_CODE_412_PRECONDITION_FAILED == test_results.finished_response_status ||
3904 AWS_HTTP_STATUS_CODE_200_OK == test_results.finished_response_status);
3905 ASSERT_SUCCESS(s_test_validate_if_none_match_failure_response(&test_results));
3906
3907 aws_s3_meta_request_test_results_clean_up(&test_results);
3908 return 0;
3909 }
3910
3911 AWS_TEST_CASE(test_s3_put_object_sse_kms, s_test_s3_put_object_sse_kms)
3912 static int s_test_s3_put_object_sse_kms(struct aws_allocator *allocator, void *ctx) {
3913 (void)ctx;
3914
3915 struct aws_s3_tester tester;
3916 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3917
3918 struct aws_s3_client_config client_config = {
3919 .part_size = 20 * 1024 * 1024,
3920 };
3921
3922 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3923 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3924
3925 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3926
3927 ASSERT_TRUE(client != NULL);
3928
3929 struct aws_s3_tester_meta_request_options put_options = {
3930 .allocator = allocator,
3931 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3932 .client = client,
3933 .sse_type = AWS_S3_TESTER_SSE_KMS,
3934 .put_options =
3935 {
3936 .object_size_mb = 10,
3937 },
3938 };
3939
3940 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3941
3942 client = aws_s3_client_release(client);
3943
3944 aws_s3_tester_clean_up(&tester);
3945
3946 return 0;
3947 }
3948
3949 AWS_TEST_CASE(test_s3_put_object_sse_kms_multipart, s_test_s3_put_object_sse_kms_multipart)
3950 static int s_test_s3_put_object_sse_kms_multipart(struct aws_allocator *allocator, void *ctx) {
3951 (void)ctx;
3952
3953 struct aws_s3_tester tester;
3954 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3955
3956 struct aws_s3_client_config client_config = {
3957 .part_size = 5 * 1024 * 1024,
3958 };
3959
3960 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3961 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
3962
3963 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
3964
3965 ASSERT_TRUE(client != NULL);
3966
3967 struct aws_s3_tester_meta_request_options put_options = {
3968 .allocator = allocator,
3969 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
3970 .client = client,
3971 .sse_type = AWS_S3_TESTER_SSE_KMS,
3972 .put_options =
3973 {
3974 .object_size_mb = 10,
3975 },
3976 };
3977
3978 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
3979
3980 client = aws_s3_client_release(client);
3981
3982 aws_s3_tester_clean_up(&tester);
3983
3984 return 0;
3985 }
3986
3987 AWS_TEST_CASE(test_s3_put_object_sse_aes256, s_test_s3_put_object_sse_aes256)
3988 static int s_test_s3_put_object_sse_aes256(struct aws_allocator *allocator, void *ctx) {
3989 (void)ctx;
3990
3991 struct aws_s3_tester tester;
3992 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
3993
3994 struct aws_s3_client_config client_config = {
3995 .part_size = 20 * 1024 * 1024,
3996 };
3997
3998 ASSERT_SUCCESS(aws_s3_tester_bind_client(
3999 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
4000
4001 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
4002
4003 ASSERT_TRUE(client != NULL);
4004
4005 struct aws_s3_tester_meta_request_options put_options = {
4006 .allocator = allocator,
4007 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4008 .client = client,
4009 .sse_type = AWS_S3_TESTER_SSE_AES256,
4010 .put_options =
4011 {
4012 .object_size_mb = 10,
4013 },
4014 };
4015
4016 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4017
4018 client = aws_s3_client_release(client);
4019
4020 aws_s3_tester_clean_up(&tester);
4021
4022 return 0;
4023 }
4024
4025 AWS_TEST_CASE(test_s3_put_object_sse_aes256_multipart, s_test_s3_put_object_sse_aes256_multipart)
4026 static int s_test_s3_put_object_sse_aes256_multipart(struct aws_allocator *allocator, void *ctx) {
4027 (void)ctx;
4028
4029 struct aws_s3_tester tester;
4030 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4031
4032 struct aws_s3_client_config client_config = {
4033 .part_size = 5 * 1024 * 1024,
4034 };
4035
4036 ASSERT_SUCCESS(aws_s3_tester_bind_client(
4037 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
4038
4039 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
4040
4041 ASSERT_TRUE(client != NULL);
4042
4043 struct aws_s3_tester_meta_request_options put_options = {
4044 .allocator = allocator,
4045 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4046 .client = client,
4047 .sse_type = AWS_S3_TESTER_SSE_AES256,
4048 .put_options =
4049 {
4050 .object_size_mb = 10,
4051 },
4052 };
4053
4054 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4055
4056 client = aws_s3_client_release(client);
4057
4058 aws_s3_tester_clean_up(&tester);
4059
4060 return 0;
4061 }
4062
4063 AWS_TEST_CASE(test_s3_put_object_sse_c_aes256_multipart, s_test_s3_put_object_sse_c_aes256_multipart)
4064 static int s_test_s3_put_object_sse_c_aes256_multipart(struct aws_allocator *allocator, void *ctx) {
4065 (void)ctx;
4066 (void)allocator;
4067
4068 struct aws_s3_tester tester;
4069 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4070
4071 struct aws_s3_client_config client_config = {
4072 .part_size = 5 * 1024 * 1024,
4073 };
4074
4075 ASSERT_SUCCESS(aws_s3_tester_bind_client(
4076 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
4077
4078 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
4079
4080 ASSERT_TRUE(client != NULL);
4081
4082 struct aws_byte_buf path_buf;
4083 AWS_ZERO_STRUCT(path_buf);
4084
4085 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4086 tester.allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_sse_c.txt")));
4087
4088 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4089
4090 struct aws_s3_tester_meta_request_options put_options = {
4091 .allocator = allocator,
4092 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4093 .client = client,
4094 .sse_type = AWS_S3_TESTER_SSE_C_AES256,
4095 .put_options =
4096 {
4097 .object_size_mb = 10,
4098 .object_path_override = object_path,
4099 },
4100 };
4101
4102 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4103 client = aws_s3_client_release(client);
4104
4105 aws_byte_buf_clean_up(&path_buf);
4106 aws_s3_tester_clean_up(&tester);
4107
4108 return 0;
4109 }
4110
4111 AWS_TEST_CASE(
4112 test_s3_put_object_sse_c_aes256_multipart_with_checksum,
4113 s_test_s3_put_object_sse_c_aes256_multipart_with_checksum)
4114 static int s_test_s3_put_object_sse_c_aes256_multipart_with_checksum(struct aws_allocator *allocator, void *ctx) {
4115 (void)ctx;
4116
4117 struct aws_s3_tester tester;
4118 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4119
4120 struct aws_s3_client_config client_config = {
4121 .part_size = 5 * 1024 * 1024,
4122 };
4123
4124 ASSERT_SUCCESS(aws_s3_tester_bind_client(
4125 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
4126
4127 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
4128
4129 ASSERT_TRUE(client != NULL);
4130
4131 struct aws_byte_buf path_buf;
4132 AWS_ZERO_STRUCT(path_buf);
4133
4134 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4135 tester.allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_sse_c_fc.txt")));
4136
4137 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4138
4139 struct aws_s3_tester_meta_request_options put_options = {
4140 .allocator = allocator,
4141 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4142 .client = client,
4143 .sse_type = AWS_S3_TESTER_SSE_C_AES256,
4144 .checksum_algorithm = AWS_SCA_CRC32,
4145 .put_options =
4146 {
4147 .object_size_mb = 10,
4148 .object_path_override = object_path,
4149 },
4150 };
4151
4152 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4153 client = aws_s3_client_release(client);
4154
4155 aws_byte_buf_clean_up(&path_buf);
4156 aws_s3_tester_clean_up(&tester);
4157
4158 return 0;
4159 }
4160
4161 struct aws_future_s3_buffer_ticket *s_failing_pool_reserve(
4162 struct aws_s3_buffer_pool *pool,
4163 struct aws_s3_buffer_pool_reserve_meta meta) {
4164 (void)meta;
4165
4166 struct aws_future_s3_buffer_ticket *future = aws_future_s3_buffer_ticket_new((struct aws_allocator *)pool->impl);
4167 aws_future_s3_buffer_ticket_set_error(future, AWS_ERROR_S3_BUFFER_ALLOCATION_FAILED);
4168 return future;
4169 }
4170
4171 void s_failing_pool_trim(struct aws_s3_buffer_pool *pool) {
4172 (void)pool;
4173 }
4174
4175 static struct aws_s3_buffer_pool_vtable s_failing_pool_vtable = {
4176 .reserve = s_failing_pool_reserve,
4177 .trim = s_failing_pool_trim,
4178 };
4179
4180 void s_failing_pool_destroy(struct aws_s3_buffer_pool *buffer_pool_wrapper) {
4181 aws_mem_release((struct aws_allocator *)buffer_pool_wrapper->impl, buffer_pool_wrapper);
4182 }
4183
4184 struct aws_s3_buffer_pool *s_always_error_buffer_pool_fn(
4185 struct aws_allocator *allocator,
4186 struct aws_s3_buffer_pool_config config,
4187 void *user_data) {
4188 (void)config;
4189 (void)user_data;
4190 struct aws_s3_buffer_pool *pool = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_buffer_pool));
4191 pool->impl = allocator;
4192 pool->vtable = &s_failing_pool_vtable;
4193 aws_ref_count_init(&pool->ref_count, pool, (aws_simple_completion_callback *)s_failing_pool_destroy);
4194
4195 return pool;
4196 }
4197
4198 AWS_TEST_CASE(test_s3_put_object_buffer_acquire_error, s_test_s3_put_object_buffer_acquire_error)
4199 static int s_test_s3_put_object_buffer_acquire_error(struct aws_allocator *allocator, void *ctx) {
4200 (void)ctx;
4201
4202 struct aws_s3_tester tester;
4203 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4204
4205 struct aws_s3_client_config client_config = {
4206 .part_size = 8 * 1024 * 1024, .buffer_pool_factory_fn = s_always_error_buffer_pool_fn};
4207
4208 ASSERT_SUCCESS(aws_s3_tester_bind_client(
4209 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
4210
4211 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
4212
4213 ASSERT_TRUE(client != NULL);
4214
4215 struct aws_byte_buf path_buf;
4216 AWS_ZERO_STRUCT(path_buf);
4217
4218 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4219 tester.allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_acquire_buffer_error.txt")));
4220
4221 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4222
4223 struct aws_s3_tester_meta_request_options put_options = {
4224 .allocator = allocator,
4225 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4226 .client = client,
4227 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
4228 .put_options =
4229 {
4230 .object_size_mb = 10,
4231 .object_path_override = object_path,
4232 },
4233 };
4234
4235 struct aws_s3_meta_request_test_results meta_request_test_results;
4236 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
4237 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &meta_request_test_results));
4238 ASSERT_INT_EQUALS(AWS_ERROR_S3_BUFFER_ALLOCATION_FAILED, meta_request_test_results.finished_error_code);
4239 client = aws_s3_client_release(client);
4240
4241 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
4242 aws_byte_buf_clean_up(&path_buf);
4243 aws_s3_tester_clean_up(&tester);
4244
4245 return 0;
4246 }
4247
4248 static int s_test_s3_put_object_content_md5_helper(
4249 struct aws_allocator *allocator,
4250 bool multipart_upload,
4251 uint32_t flags,
4252 enum aws_s3_meta_request_compute_content_md5 compute_content_md5) {
4253 struct aws_s3_tester tester;
4254 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4255
4256 size_t part_size = 5 * 1024 * 1024;
4257 if (!multipart_upload) {
4258 /* content_length < part_size */
4259 part_size = 15 * 1024 * 1024;
4260 }
4261
4262 struct aws_s3_client_config client_config = {
4263 .part_size = part_size,
4264 };
4265
4266 client_config.compute_content_md5 = compute_content_md5;
4267
4268 ASSERT_SUCCESS(aws_s3_tester_bind_client(
4269 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
4270
4271 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
4272
4273 ASSERT_TRUE(client != NULL);
4274
4275 ASSERT_SUCCESS(aws_s3_tester_send_put_object_meta_request(&tester, client, 10, flags, NULL));
4276
4277 client = aws_s3_client_release(client);
4278
4279 aws_s3_tester_clean_up(&tester);
4280
4281 return 0;
4282 }
4283
4284 AWS_TEST_CASE(
4285 test_s3_put_object_singlepart_no_content_md5_enabled,
4286 s_test_s3_put_object_singlepart_no_content_md5_enabled)
4287 static int s_test_s3_put_object_singlepart_no_content_md5_enabled(struct aws_allocator *allocator, void *ctx) {
4288 (void)ctx;
4289
4290 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4291 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, false, flags, AWS_MR_CONTENT_MD5_ENABLED));
4292
4293 return 0;
4294 }
4295
4296 AWS_TEST_CASE(
4297 test_s3_put_object_singlepart_no_content_md5_disabled,
4298 s_test_s3_put_object_singlepart_no_content_md5_disabled)
4299 static int s_test_s3_put_object_singlepart_no_content_md5_disabled(struct aws_allocator *allocator, void *ctx) {
4300 (void)ctx;
4301
4302 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4303 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, false, flags, AWS_MR_CONTENT_MD5_DISABLED));
4304
4305 return 0;
4306 }
4307
4308 AWS_TEST_CASE(
4309 test_s3_put_object_singlepart_correct_content_md5_enabled,
4310 s_test_s3_put_object_singlepart_correct_content_md5_enabled)
4311 static int s_test_s3_put_object_singlepart_correct_content_md5_enabled(struct aws_allocator *allocator, void *ctx) {
4312 (void)ctx;
4313
4314 uint32_t flags =
4315 AWS_S3_TESTER_SEND_META_REQUEST_WITH_CORRECT_CONTENT_MD5 | AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4316 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, false, flags, AWS_MR_CONTENT_MD5_ENABLED));
4317
4318 return 0;
4319 }
4320
4321 AWS_TEST_CASE(
4322 test_s3_put_object_singlepart_correct_content_md5_disabled,
4323 s_test_s3_put_object_singlepart_correct_content_md5_disabled)
4324 static int s_test_s3_put_object_singlepart_correct_content_md5_disabled(struct aws_allocator *allocator, void *ctx) {
4325 (void)ctx;
4326
4327 uint32_t flags =
4328 AWS_S3_TESTER_SEND_META_REQUEST_WITH_CORRECT_CONTENT_MD5 | AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4329 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, false, flags, AWS_MR_CONTENT_MD5_DISABLED));
4330
4331 return 0;
4332 }
4333
4334 AWS_TEST_CASE(
4335 test_s3_put_object_singlepart_incorrect_content_md5_enabled,
4336 s_test_s3_put_object_singlepart_incorrect_content_md5_enabled)
4337 static int s_test_s3_put_object_singlepart_incorrect_content_md5_enabled(struct aws_allocator *allocator, void *ctx) {
4338 (void)ctx;
4339
4340 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_WITH_INCORRECT_CONTENT_MD5;
4341 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, false, flags, AWS_MR_CONTENT_MD5_ENABLED));
4342
4343 return 0;
4344 }
4345
4346 AWS_TEST_CASE(
4347 test_s3_put_object_singlepart_incorrect_content_md5_disabled,
4348 s_test_s3_put_object_singlepart_incorrect_content_md5_disabled)
4349 static int s_test_s3_put_object_singlepart_incorrect_content_md5_disabled(struct aws_allocator *allocator, void *ctx) {
4350 (void)ctx;
4351
4352 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_WITH_INCORRECT_CONTENT_MD5;
4353 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, false, flags, AWS_MR_CONTENT_MD5_DISABLED));
4354
4355 return 0;
4356 }
4357
4358 AWS_TEST_CASE(
4359 test_s3_put_object_multipart_no_content_md5_enabled,
4360 s_test_s3_put_object_multipart_no_content_md5_enabled)
4361 static int s_test_s3_put_object_multipart_no_content_md5_enabled(struct aws_allocator *allocator, void *ctx) {
4362 (void)ctx;
4363
4364 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4365 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, true, flags, AWS_MR_CONTENT_MD5_ENABLED));
4366
4367 return 0;
4368 }
4369
4370 AWS_TEST_CASE(
4371 test_s3_put_object_multipart_no_content_md5_disabled,
4372 s_test_s3_put_object_multipart_no_content_md5_disabled)
4373 static int s_test_s3_put_object_multipart_no_content_md5_disabled(struct aws_allocator *allocator, void *ctx) {
4374 (void)ctx;
4375
4376 uint32_t flags = AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4377 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, true, flags, AWS_MR_CONTENT_MD5_DISABLED));
4378
4379 return 0;
4380 }
4381
4382 AWS_TEST_CASE(
4383 test_s3_put_object_multipart_correct_content_md5_enabled,
4384 s_test_s3_put_object_multipart_correct_content_md5_enabled)
4385 static int s_test_s3_put_object_multipart_correct_content_md5_enabled(struct aws_allocator *allocator, void *ctx) {
4386 (void)ctx;
4387
4388 uint32_t flags =
4389 AWS_S3_TESTER_SEND_META_REQUEST_WITH_CORRECT_CONTENT_MD5 | AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4390 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, true, flags, AWS_MR_CONTENT_MD5_ENABLED));
4391
4392 return 0;
4393 }
4394
4395 AWS_TEST_CASE(
4396 test_s3_put_object_multipart_correct_content_md5_disabled,
4397 s_test_s3_put_object_multipart_correct_content_md5_disabled)
4398 static int s_test_s3_put_object_multipart_correct_content_md5_disabled(struct aws_allocator *allocator, void *ctx) {
4399 (void)ctx;
4400
4401 uint32_t flags =
4402 AWS_S3_TESTER_SEND_META_REQUEST_WITH_CORRECT_CONTENT_MD5 | AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4403 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, true, flags, AWS_MR_CONTENT_MD5_DISABLED));
4404
4405 return 0;
4406 }
4407
4408 AWS_TEST_CASE(
4409 test_s3_put_object_multipart_incorrect_content_md5_enabled,
4410 s_test_s3_put_object_multipart_incorrect_content_md5_enabled)
4411 static int s_test_s3_put_object_multipart_incorrect_content_md5_enabled(struct aws_allocator *allocator, void *ctx) {
4412 (void)ctx;
4413
4414 uint32_t flags =
4415 AWS_S3_TESTER_SEND_META_REQUEST_WITH_INCORRECT_CONTENT_MD5 | AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4416 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, true, flags, AWS_MR_CONTENT_MD5_ENABLED));
4417
4418 return 0;
4419 }
4420
4421 AWS_TEST_CASE(
4422 test_s3_put_object_multipart_incorrect_content_md5_disabled,
4423 s_test_s3_put_object_multipart_incorrect_content_md5_disabled)
4424 static int s_test_s3_put_object_multipart_incorrect_content_md5_disabled(struct aws_allocator *allocator, void *ctx) {
4425 (void)ctx;
4426
4427 uint32_t flags =
4428 AWS_S3_TESTER_SEND_META_REQUEST_WITH_INCORRECT_CONTENT_MD5 | AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS;
4429 ASSERT_SUCCESS(s_test_s3_put_object_content_md5_helper(allocator, true, flags, AWS_MR_CONTENT_MD5_DISABLED));
4430
4431 return 0;
4432 }
4433
4434 static int s_test_s3_upload_part_message_helper(struct aws_allocator *allocator, bool should_compute_content_md5) {
4435
4436 aws_s3_library_init(allocator);
4437
4438 struct aws_byte_buf test_buffer;
4439 aws_s3_create_test_buffer(allocator, 19 /* size of "This is an S3 test." */, &test_buffer);
4440 /* base64 encoded md5 of "This is an S3 test." */
4441 struct aws_byte_cursor expected_content_md5 = aws_byte_cursor_from_c_str("+y3U+EY5uFXhVVmRoiJWyA==");
4442
4443 struct aws_byte_cursor test_body_cursor = aws_byte_cursor_from_buf(&test_buffer);
4444 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &test_body_cursor);
4445
4446 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
4447
4448 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str("dummy_key");
4449
4450 /* Put together a simple S3 Put Object request. */
4451 struct aws_http_message *base_message = aws_s3_test_put_object_request_new(
4452 allocator, &host_name, test_object_path, g_test_body_content_type, input_stream, AWS_S3_TESTER_SSE_NONE);
4453
4454 uint32_t part_number = 1;
4455 struct aws_string *upload_id = aws_string_new_from_c_str(allocator, "dummy_upload_id");
4456
4457 struct aws_http_message *new_message = aws_s3_upload_part_message_new(
4458 allocator, base_message, &test_buffer, part_number, upload_id, should_compute_content_md5, NULL);
4459
4460 struct aws_http_headers *new_headers = aws_http_message_get_headers(new_message);
4461 if (should_compute_content_md5) {
4462 ASSERT_TRUE(aws_http_headers_has(new_headers, g_content_md5_header_name));
4463 struct aws_byte_cursor content_md5;
4464 aws_http_headers_get(new_headers, g_content_md5_header_name, &content_md5);
4465 ASSERT_BIN_ARRAYS_EQUALS(expected_content_md5.ptr, expected_content_md5.len, content_md5.ptr, content_md5.len);
4466 } else {
4467 ASSERT_FALSE(aws_http_headers_has(new_headers, g_content_md5_header_name));
4468 }
4469
4470 aws_http_message_release(new_message);
4471 new_message = NULL;
4472
4473 aws_http_message_release(base_message);
4474 base_message = NULL;
4475
4476 aws_string_destroy(upload_id);
4477 upload_id = NULL;
4478
4479 aws_input_stream_release(input_stream);
4480 input_stream = NULL;
4481
4482 aws_byte_buf_clean_up(&test_buffer);
4483
4484 aws_s3_library_clean_up();
4485
4486 return 0;
4487 }
4488
4489 AWS_TEST_CASE(test_s3_upload_part_message_with_content_md5, s_test_s3_upload_part_message_with_content_md5)
4490 static int s_test_s3_upload_part_message_with_content_md5(struct aws_allocator *allocator, void *ctx) {
4491 (void)ctx;
4492
4493 ASSERT_SUCCESS(s_test_s3_upload_part_message_helper(allocator, true));
4494
4495 return 0;
4496 }
4497
4498 AWS_TEST_CASE(test_s3_upload_part_message_without_content_md5, s_test_s3_upload_part_message_without_content_md5)
4499 static int s_test_s3_upload_part_message_without_content_md5(struct aws_allocator *allocator, void *ctx) {
4500 (void)ctx;
4501
4502 ASSERT_SUCCESS(s_test_s3_upload_part_message_helper(allocator, false));
4503
4504 return 0;
4505 }
4506
4507 AWS_TEST_CASE(
4508 test_s3_create_multipart_upload_message_with_content_md5,
4509 s_test_s3_create_multipart_upload_message_with_content_md5)
4510 static int s_test_s3_create_multipart_upload_message_with_content_md5(struct aws_allocator *allocator, void *ctx) {
4511 (void)ctx;
4512
4513 struct aws_byte_buf test_buffer;
4514 aws_s3_create_test_buffer(allocator, 19 /* size of "This is an S3 test." */, &test_buffer);
4515
4516 struct aws_byte_cursor test_body_cursor = aws_byte_cursor_from_buf(&test_buffer);
4517 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &test_body_cursor);
4518
4519 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
4520
4521 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str("dummy_key");
4522
4523 /* Put together a simple S3 Put Object request. */
4524 struct aws_http_message *base_message = aws_s3_test_put_object_request_new(
4525 allocator, &host_name, test_object_path, g_test_body_content_type, input_stream, AWS_S3_TESTER_SSE_NONE);
4526
4527 struct aws_http_header content_md5_header = {
4528 .name = g_content_md5_header_name,
4529 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("dummy_content_md5"),
4530 };
4531 ASSERT_SUCCESS(aws_http_message_add_header(base_message, content_md5_header));
4532
4533 struct aws_http_headers *base_headers = aws_http_message_get_headers(base_message);
4534 ASSERT_TRUE(aws_http_headers_has(base_headers, g_content_md5_header_name));
4535
4536 struct aws_http_message *new_message = aws_s3_create_multipart_upload_message_new(allocator, base_message, NULL);
4537
4538 struct aws_http_headers *new_headers = aws_http_message_get_headers(new_message);
4539 ASSERT_FALSE(aws_http_headers_has(new_headers, g_content_md5_header_name));
4540
4541 aws_http_message_release(new_message);
4542 new_message = NULL;
4543
4544 aws_http_message_release(base_message);
4545 base_message = NULL;
4546
4547 aws_input_stream_release(input_stream);
4548 input_stream = NULL;
4549
4550 aws_byte_buf_clean_up(&test_buffer);
4551
4552 return 0;
4553 }
4554
4555 AWS_TEST_CASE(
4556 test_s3_complete_multipart_message_with_content_md5,
4557 s_test_s3_complete_multipart_message_with_content_md5)
4558 static int s_test_s3_complete_multipart_message_with_content_md5(struct aws_allocator *allocator, void *ctx) {
4559 (void)ctx;
4560
4561 struct aws_byte_buf test_buffer;
4562 aws_s3_create_test_buffer(allocator, 19 /* size of "This is an S3 test." */, &test_buffer);
4563
4564 struct aws_byte_cursor test_body_cursor = aws_byte_cursor_from_buf(&test_buffer);
4565 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &test_body_cursor);
4566
4567 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
4568
4569 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str("dummy_key");
4570
4571 /* Put together a simple S3 Put Object request. */
4572 struct aws_http_message *base_message = aws_s3_test_put_object_request_new(
4573 allocator, &host_name, test_object_path, g_test_body_content_type, input_stream, AWS_S3_TESTER_SSE_NONE);
4574
4575 struct aws_http_header content_md5_header = {
4576 .name = g_content_md5_header_name,
4577 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("dummy_content_md5"),
4578 };
4579 ASSERT_SUCCESS(aws_http_message_add_header(base_message, content_md5_header));
4580
4581 struct aws_http_headers *base_headers = aws_http_message_get_headers(base_message);
4582 ASSERT_TRUE(aws_http_headers_has(base_headers, g_content_md5_header_name));
4583
4584 struct aws_byte_buf body_buffer;
4585 aws_byte_buf_init(&body_buffer, allocator, 512);
4586
4587 struct aws_string *upload_id = aws_string_new_from_c_str(allocator, "dummy_upload_id");
4588
4589 struct aws_array_list parts;
4590 ASSERT_SUCCESS(aws_array_list_init_dynamic(&parts, allocator, 0, sizeof(struct aws_s3_mpu_part_info *)));
4591
4592 struct aws_http_message *new_message =
4593 aws_s3_complete_multipart_message_new(allocator, base_message, &body_buffer, upload_id, &parts, NULL);
4594
4595 struct aws_http_headers *new_headers = aws_http_message_get_headers(new_message);
4596 ASSERT_FALSE(aws_http_headers_has(new_headers, g_content_md5_header_name));
4597
4598 aws_http_message_release(new_message);
4599 new_message = NULL;
4600
4601 aws_http_message_release(base_message);
4602 base_message = NULL;
4603
4604 aws_array_list_clean_up(&parts);
4605
4606 aws_string_destroy(upload_id);
4607 upload_id = NULL;
4608
4609 aws_byte_buf_clean_up(&body_buffer);
4610
4611 aws_input_stream_release(input_stream);
4612 input_stream = NULL;
4613
4614 aws_byte_buf_clean_up(&test_buffer);
4615
4616 return 0;
4617 }
4618
4619 AWS_TEST_CASE(test_s3_put_object_double_slashes, s_test_s3_put_object_double_slashes)
4620 static int s_test_s3_put_object_double_slashes(struct aws_allocator *allocator, void *ctx) {
4621 (void)ctx;
4622
4623 struct aws_s3_meta_request_test_results meta_request_test_results;
4624 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
4625
4626 struct aws_byte_buf path_buf;
4627 AWS_ZERO_STRUCT(path_buf);
4628
4629 ASSERT_SUCCESS(
4630 aws_s3_tester_upload_file_path_init(allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix//test.txt")));
4631
4632 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4633 struct aws_s3_tester_meta_request_options options = {
4634 .allocator = allocator,
4635 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4636 .put_options =
4637 {
4638 .object_size_mb = 1,
4639 .object_path_override = object_path,
4640 },
4641 };
4642
4643 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
4644
4645 aws_byte_buf_clean_up(&path_buf);
4646 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
4647
4648 return 0;
4649 }
4650
4651 AWS_TEST_CASE(test_s3_round_trip, s_test_s3_round_trip)
4652 static int s_test_s3_round_trip(struct aws_allocator *allocator, void *ctx) {
4653 (void)ctx;
4654
4655 struct aws_s3_tester tester;
4656 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4657
4658 struct aws_s3_tester_client_options client_options = {
4659 .part_size = 16 * 1024,
4660 };
4661
4662 struct aws_s3_client *client = NULL;
4663 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
4664
4665 struct aws_byte_buf path_buf;
4666 AWS_ZERO_STRUCT(path_buf);
4667
4668 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4669 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test.txt")));
4670
4671 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4672
4673 struct aws_s3_tester_meta_request_options put_options = {
4674 .allocator = allocator,
4675 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4676 .client = client,
4677 .put_options =
4678 {
4679 .object_size_mb = 1,
4680 .object_path_override = object_path,
4681 },
4682 };
4683
4684 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4685
4686 /*** GET FILE ***/
4687
4688 struct aws_s3_tester_meta_request_options get_options = {
4689 .allocator = allocator,
4690 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
4691 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
4692 .client = client,
4693 .get_options =
4694 {
4695 .object_path = object_path,
4696 },
4697 };
4698
4699 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
4700
4701 aws_byte_buf_clean_up(&path_buf);
4702 aws_s3_client_release(client);
4703 aws_s3_tester_clean_up(&tester);
4704
4705 return 0;
4706 }
4707
4708 AWS_TEST_CASE(test_s3_round_trip_default_get, s_test_s3_round_trip_default_get)
4709 static int s_test_s3_round_trip_default_get(struct aws_allocator *allocator, void *ctx) {
4710 (void)ctx;
4711
4712 struct aws_s3_tester tester;
4713 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4714
4715 struct aws_s3_tester_client_options client_options = {
4716 .part_size = 16 * 1024,
4717 };
4718
4719 struct aws_s3_client *client = NULL;
4720 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
4721
4722 struct aws_byte_buf path_buf;
4723 AWS_ZERO_STRUCT(path_buf);
4724
4725 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4726 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_default.txt")));
4727
4728 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4729
4730 struct aws_s3_tester_meta_request_options put_options = {
4731 .allocator = allocator,
4732 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4733 .client = client,
4734 .put_options =
4735 {
4736 .object_size_mb = 1,
4737 .object_path_override = object_path,
4738 },
4739 };
4740
4741 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4742
4743 /*** GET FILE ***/
4744
4745 struct aws_s3_tester_meta_request_options get_options = {
4746 .allocator = allocator,
4747 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
4748 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
4749 .client = client,
4750 .get_options =
4751 {
4752 .object_path = object_path,
4753 },
4754 .default_type_options =
4755 {
4756 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
4757 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
4758 },
4759 };
4760
4761 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
4762
4763 aws_byte_buf_clean_up(&path_buf);
4764 aws_s3_client_release(client);
4765 aws_s3_tester_clean_up(&tester);
4766
4767 return 0;
4768 }
4769
4770 int s_s3_validate_headers_checksum_set(
4771 struct aws_s3_meta_request *meta_request,
4772 const struct aws_http_headers *headers,
4773 int response_status,
4774 void *user_data) {
4775 (void)response_status;
4776 (void)headers;
4777 struct aws_s3_meta_request_test_results *meta_request_test_results =
4778 (struct aws_s3_meta_request_test_results *)user_data;
4779 ASSERT_NOT_NULL(meta_request->meta_request_level_running_response_sum);
4780 ASSERT_INT_EQUALS(
4781 meta_request->meta_request_level_running_response_sum->algorithm, meta_request_test_results->algorithm);
4782 return AWS_OP_SUCCESS;
4783 }
4784
4785 int s_s3_validate_headers_checksum_unset(
4786 struct aws_s3_meta_request *meta_request,
4787 const struct aws_http_headers *headers,
4788 int response_status,
4789 void *user_data) {
4790 (void)response_status;
4791 (void)headers;
4792 (void)user_data;
4793 ASSERT_NULL(meta_request->meta_request_level_running_response_sum);
4794 return AWS_OP_SUCCESS;
4795 }
4796
4797 void s_s3_test_validate_checksum(
4798 struct aws_s3_meta_request *meta_request,
4799 const struct aws_s3_meta_request_result *result,
4800 void *user_data) {
4801 (void)meta_request;
4802 struct aws_s3_meta_request_test_results *meta_request_test_results =
4803 (struct aws_s3_meta_request_test_results *)user_data;
4804 AWS_FATAL_ASSERT(result->did_validate);
4805 AWS_FATAL_ASSERT(result->validation_algorithm == meta_request_test_results->algorithm);
4806 AWS_FATAL_ASSERT(result->error_code == AWS_OP_SUCCESS);
4807 }
4808 void s_s3_test_no_validate_checksum(
4809 struct aws_s3_meta_request *meta_request,
4810 const struct aws_s3_meta_request_result *result,
4811 void *user_data) {
4812 (void)meta_request;
4813 (void)user_data;
4814 AWS_FATAL_ASSERT(!result->did_validate);
4815 AWS_FATAL_ASSERT(result->error_code == AWS_OP_SUCCESS);
4816 }
4817
4818 /* TODO: maybe refactor the fc -> flexible checksum tests to be less copy/paste */
4819 static int s_test_s3_round_trip_default_get_fc_helper(
4820 struct aws_allocator *allocator,
4821 void *ctx,
4822 bool via_header,
4823 uint32_t object_size_mb,
4824 enum aws_s3_tester_full_object_checksum full_object_checksum) {
4825 (void)ctx;
4826
4827 struct aws_s3_tester tester;
4828 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4829 struct aws_s3_tester_client_options client_options = {
4830 .part_size = MB_TO_BYTES(5),
4831 };
4832
4833 struct aws_s3_client *client = NULL;
4834 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
4835
4836 struct aws_byte_buf path_buf;
4837 AWS_ZERO_STRUCT(path_buf);
4838
4839 for (size_t i = 0; i < AWS_ARRAY_SIZE(s_checksum_algo_priority_list); i++) {
4840 enum aws_s3_checksum_algorithm algorithm = s_checksum_algo_priority_list[i];
4841
4842 char object_path_sprintf_buffer[128] = "";
4843 snprintf(
4844 object_path_sprintf_buffer,
4845 sizeof(object_path_sprintf_buffer),
4846 "/prefix/round_trip/test_default_fc_%d_%d.txt",
4847 algorithm,
4848 object_size_mb);
4849
4850 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4851 allocator, &path_buf, aws_byte_cursor_from_c_str(object_path_sprintf_buffer)));
4852 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4853 /*** PUT FILE ***/
4854
4855 struct aws_s3_tester_meta_request_options put_options = {
4856 .allocator = allocator,
4857 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4858 .client = client,
4859 .checksum_algorithm = algorithm,
4860 .validate_get_response_checksum = false,
4861 .checksum_via_header = via_header,
4862 .put_options =
4863 {
4864 .object_size_mb = object_size_mb,
4865 .object_path_override = object_path,
4866 },
4867 };
4868 if (algorithm != AWS_SCA_SHA1 && algorithm != AWS_SCA_SHA256) {
4869 /* Full object checksums doesn't support SHA. */
4870 put_options.put_options.full_object_checksum = full_object_checksum;
4871 }
4872
4873 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4874
4875 /*** GET FILE ***/
4876
4877 struct aws_s3_tester_meta_request_options get_options = {
4878 .allocator = allocator,
4879 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
4880 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
4881 .client = client,
4882 .expected_validate_checksum_alg = algorithm,
4883 .validate_get_response_checksum = true,
4884 .get_options =
4885 {
4886 .object_path = object_path,
4887 },
4888 .finish_callback = s_s3_test_validate_checksum,
4889 .headers_callback = s_s3_validate_headers_checksum_set,
4890 };
4891
4892 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
4893
4894 aws_byte_buf_clean_up(&path_buf);
4895 }
4896
4897 aws_s3_client_release(client);
4898 aws_s3_tester_clean_up(&tester);
4899
4900 return 0;
4901 }
4902
4903 AWS_TEST_CASE(test_s3_round_trip_default_get_fc, s_test_s3_round_trip_default_get_fc)
4904 static int s_test_s3_round_trip_default_get_fc(struct aws_allocator *allocator, void *ctx) {
4905 return s_test_s3_round_trip_default_get_fc_helper(allocator, ctx, false, 1 /*object_size_mb*/, AWS_TEST_FOC_NONE);
4906 }
4907 AWS_TEST_CASE(test_s3_round_trip_empty_fc, s_test_s3_round_trip_empty_fc)
4908 static int s_test_s3_round_trip_empty_fc(struct aws_allocator *allocator, void *ctx) {
4909 return s_test_s3_round_trip_default_get_fc_helper(allocator, ctx, false, 0, AWS_TEST_FOC_NONE);
4910 }
4911
4912 AWS_TEST_CASE(test_s3_round_trip_default_get_fc_header, s_test_s3_round_trip_default_get_fc_header)
4913 static int s_test_s3_round_trip_default_get_fc_header(struct aws_allocator *allocator, void *ctx) {
4914 return s_test_s3_round_trip_default_get_fc_helper(allocator, ctx, true, 1, AWS_TEST_FOC_NONE);
4915 }
4916 AWS_TEST_CASE(
4917 test_s3_round_trip_default_get_full_object_checksum_fc,
4918 s_test_s3_round_trip_default_get_full_object_checksum_fc)
4919 static int s_test_s3_round_trip_default_get_full_object_checksum_fc(struct aws_allocator *allocator, void *ctx) {
4920 return s_test_s3_round_trip_default_get_fc_helper(allocator, ctx, false, 1, AWS_TEST_FOC_HEADER);
4921 }
4922
4923 static int s_test_s3_round_trip_multipart_get_fc_helper(struct aws_allocator *allocator, void *ctx, bool via_header) {
4924 (void)ctx;
4925
4926 struct aws_s3_tester tester;
4927 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
4928 struct aws_s3_tester_client_options client_options = {
4929 .part_size = 16 * 1024,
4930 };
4931
4932 struct aws_s3_client *client = NULL;
4933 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
4934
4935 struct aws_byte_buf path_buf;
4936 AWS_ZERO_STRUCT(path_buf);
4937
4938 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
4939 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_fc.txt")));
4940
4941 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
4942
4943 struct aws_s3_tester_meta_request_options put_options = {
4944 .allocator = allocator,
4945 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
4946 .client = client,
4947 .checksum_algorithm = AWS_SCA_CRC64NVME,
4948 .validate_get_response_checksum = false,
4949 .checksum_via_header = via_header,
4950 .put_options =
4951 {
4952 .object_size_mb = 1,
4953 .object_path_override = object_path,
4954 },
4955 };
4956
4957 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
4958
4959 /*** GET FILE ***/
4960
4961 struct aws_s3_tester_meta_request_options get_options = {
4962 .allocator = allocator,
4963 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
4964 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
4965 .client = client,
4966 .validate_get_response_checksum = true,
4967 .expected_validate_checksum_alg = AWS_SCA_CRC64NVME,
4968 .get_options =
4969 {
4970 .object_path = object_path,
4971 },
4972 .finish_callback = s_s3_test_validate_checksum,
4973 .headers_callback = s_s3_validate_headers_checksum_set,
4974 };
4975
4976 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
4977
4978 aws_byte_buf_clean_up(&path_buf);
4979 aws_s3_client_release(client);
4980 aws_s3_tester_clean_up(&tester);
4981
4982 return 0;
4983 }
4984
4985 AWS_TEST_CASE(test_s3_round_trip_multipart_get_fc, s_test_s3_round_trip_multipart_get_fc)
4986 static int s_test_s3_round_trip_multipart_get_fc(struct aws_allocator *allocator, void *ctx) {
4987 return s_test_s3_round_trip_multipart_get_fc_helper(allocator, ctx, false);
4988 }
4989
4990 AWS_TEST_CASE(test_s3_round_trip_multipart_get_fc_header, s_test_s3_round_trip_multipart_get_fc_header)
4991 static int s_test_s3_round_trip_multipart_get_fc_header(struct aws_allocator *allocator, void *ctx) {
4992 return s_test_s3_round_trip_multipart_get_fc_helper(allocator, ctx, true);
4993 }
4994
4995 /* Test the multipart uploaded object was downloaded with same part size, which will download the object matches all the
4996 * parts and validate the parts checksum. */
4997 static int s_test_s3_round_trip_mpu_multipart_get_fc_helper(
4998 struct aws_allocator *allocator,
4999 void *ctx,
5000 bool via_header,
5001 enum aws_s3_tester_full_object_checksum full_object_checksum) {
5002 (void)ctx;
5003
5004 struct aws_s3_tester tester;
5005 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5006 struct aws_s3_tester_client_options client_options = {
5007 .part_size = MB_TO_BYTES(5),
5008 };
5009
5010 struct aws_s3_client *client = NULL;
5011 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5012
5013 struct aws_byte_buf path_buf;
5014 AWS_ZERO_STRUCT(path_buf);
5015 struct aws_byte_cursor object_name;
5016 /* Use different name to avoid collision when tests run concurrently. */
5017 switch (full_object_checksum) {
5018 case AWS_TEST_FOC_CALLBACK:
5019 object_name = aws_byte_cursor_from_c_str("/prefix/round_trip/test_mpu_fc_full_object_via_callback.txt");
5020 break;
5021 case AWS_TEST_FOC_HEADER:
5022 object_name = aws_byte_cursor_from_c_str("/prefix/round_trip/test_mpu_fc_full_object_via_header.txt");
5023 break;
5024 default:
5025 object_name = aws_byte_cursor_from_c_str("/prefix/round_trip/test_mpu_fc.txt");
5026 break;
5027 }
5028 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(allocator, &path_buf, object_name));
5029
5030 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5031
5032 struct aws_s3_tester_meta_request_options put_options = {
5033 .allocator = allocator,
5034 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5035 .client = client,
5036 .checksum_algorithm = AWS_SCA_CRC32,
5037 .validate_get_response_checksum = false,
5038 .checksum_via_header = via_header,
5039 .put_options =
5040 {
5041 .object_size_mb = 10,
5042 .object_path_override = object_path,
5043 .full_object_checksum = full_object_checksum,
5044 },
5045 };
5046
5047 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
5048
5049 /*** GET FILE ***/
5050
5051 struct aws_s3_tester_meta_request_options get_options = {
5052 .allocator = allocator,
5053 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5054 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5055 .client = client,
5056 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5057 .validate_get_response_checksum = true,
5058 .get_options =
5059 {
5060 .object_path = object_path,
5061 },
5062 .finish_callback = s_s3_test_validate_checksum,
5063 };
5064
5065 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5066
5067 aws_byte_buf_clean_up(&path_buf);
5068 aws_s3_client_release(client);
5069 aws_s3_tester_clean_up(&tester);
5070
5071 return 0;
5072 }
5073
5074 AWS_TEST_CASE(test_s3_round_trip_mpu_multipart_get_fc, s_test_s3_round_trip_mpu_multipart_get_fc)
5075 static int s_test_s3_round_trip_mpu_multipart_get_fc(struct aws_allocator *allocator, void *ctx) {
5076 return s_test_s3_round_trip_mpu_multipart_get_fc_helper(allocator, ctx, false, AWS_TEST_FOC_NONE);
5077 }
5078
5079 AWS_TEST_CASE(test_s3_round_trip_mpu_multipart_get_fc_header, s_test_s3_round_trip_mpu_multipart_get_fc_header)
5080 static int s_test_s3_round_trip_mpu_multipart_get_fc_header(struct aws_allocator *allocator, void *ctx) {
5081 return s_test_s3_round_trip_mpu_multipart_get_fc_helper(allocator, ctx, true, AWS_TEST_FOC_NONE);
5082 }
5083
5084 AWS_TEST_CASE(
5085 test_s3_round_trip_mpu_multipart_get_full_object_checksum_fc,
5086 s_test_s3_round_trip_mpu_multipart_get_full_object_checksum_fc)
5087 static int s_test_s3_round_trip_mpu_multipart_get_full_object_checksum_fc(struct aws_allocator *allocator, void *ctx) {
5088 return s_test_s3_round_trip_mpu_multipart_get_fc_helper(allocator, ctx, false, AWS_TEST_FOC_HEADER);
5089 }
5090
5091 AWS_TEST_CASE(
5092 test_s3_round_trip_mpu_multipart_get_full_object_checksum_fc_header,
5093 s_test_s3_round_trip_mpu_multipart_get_full_object_checksum_fc_header)
5094 static int s_test_s3_round_trip_mpu_multipart_get_full_object_checksum_fc_header(
5095 struct aws_allocator *allocator,
5096 void *ctx) {
5097 return s_test_s3_round_trip_mpu_multipart_get_fc_helper(allocator, ctx, true, AWS_TEST_FOC_HEADER);
5098 }
5099
5100 AWS_TEST_CASE(
5101 test_s3_round_trip_mpu_multipart_get_full_object_checksum_via_callback,
5102 s_test_s3_round_trip_mpu_multipart_get_full_object_checksum_via_callback)
5103 static int s_test_s3_round_trip_mpu_multipart_get_full_object_checksum_via_callback(
5104 struct aws_allocator *allocator,
5105 void *ctx) {
5106 return s_test_s3_round_trip_mpu_multipart_get_fc_helper(allocator, ctx, false, AWS_TEST_FOC_CALLBACK);
5107 }
5108
5109 static int s_test_s3_download_empty_file_with_checksum_helper(
5110 struct aws_allocator *allocator,
5111 void *ctx,
5112 bool via_header) {
5113 (void)ctx;
5114
5115 /* Upload the file */
5116 struct aws_s3_tester tester;
5117 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5118 struct aws_s3_tester_client_options client_options = {
5119 .part_size = MB_TO_BYTES(5),
5120 };
5121
5122 struct aws_s3_client *client = NULL;
5123 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5124
5125 struct aws_byte_buf path_buf;
5126 AWS_ZERO_STRUCT(path_buf);
5127
5128 ASSERT_SUCCESS(
5129 aws_s3_tester_upload_file_path_init(allocator, &path_buf, aws_byte_cursor_from_c_str("/empty-file-CRC32.txt")));
5130
5131 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5132
5133 struct aws_s3_tester_meta_request_options put_options = {
5134 .allocator = allocator,
5135 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5136 .client = client,
5137 .checksum_algorithm = AWS_SCA_CRC32,
5138 .checksum_via_header = via_header,
5139 .put_options =
5140 {
5141 .object_size_mb = 0,
5142 .object_path_override = object_path,
5143 },
5144 };
5145
5146 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
5147
5148 /*** GET FILE WITH GET_FIRST_PART ***/
5149 uint64_t small_object_size_hint = 1;
5150 struct aws_s3_tester_meta_request_options get_options = {
5151 .allocator = allocator,
5152 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5153 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5154 .client = client,
5155 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5156 .validate_get_response_checksum = true,
5157 .get_options =
5158 {
5159 .object_path = object_path,
5160 },
5161 .finish_callback = s_s3_test_validate_checksum,
5162 .object_size_hint =
5163 &small_object_size_hint /* pass a object_size_hint > 0 so that the request goes through the getPart flow */,
5164 };
5165 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5166
5167 /*** GET FILE WITH HEAD_OBJECT ***/
5168 get_options.object_size_hint = NULL;
5169 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5170
5171 aws_s3_client_release(client);
5172 aws_byte_buf_clean_up(&path_buf);
5173 aws_s3_tester_clean_up(&tester);
5174
5175 return 0;
5176 }
5177
5178 AWS_TEST_CASE(test_s3_download_empty_file_with_checksum, s_test_s3_download_empty_file_with_checksum)
5179 static int s_test_s3_download_empty_file_with_checksum(struct aws_allocator *allocator, void *ctx) {
5180 return s_test_s3_download_empty_file_with_checksum_helper(allocator, ctx, false);
5181 }
5182 AWS_TEST_CASE(test_s3_download_empty_file_with_checksum_header, s_test_s3_download_empty_file_with_checksum_header)
5183 static int s_test_s3_download_empty_file_with_checksum_header(struct aws_allocator *allocator, void *ctx) {
5184 return s_test_s3_download_empty_file_with_checksum_helper(allocator, ctx, true);
5185 }
5186
5187 AWS_TEST_CASE(test_s3_download_single_part_file_with_checksum, s_test_s3_download_single_part_file_with_checksum)
5188 static int s_test_s3_download_single_part_file_with_checksum(struct aws_allocator *allocator, void *ctx) {
5189 (void)ctx;
5190
5191 /* Upload the file */
5192 struct aws_s3_tester tester;
5193 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5194 struct aws_s3_tester_client_options client_options = {
5195 .part_size = MB_TO_BYTES(10),
5196 };
5197
5198 struct aws_s3_client *client = NULL;
5199 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5200
5201 struct aws_byte_buf path_buf;
5202 AWS_ZERO_STRUCT(path_buf);
5203
5204 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
5205 allocator, &path_buf, aws_byte_cursor_from_c_str("/single-part-10Mb-CRC32.txt")));
5206
5207 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5208 uint32_t object_size_mb = 10;
5209
5210 struct aws_s3_tester_meta_request_options put_options = {
5211 .allocator = allocator,
5212 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5213 .client = client,
5214 .checksum_algorithm = AWS_SCA_CRC32,
5215 .put_options =
5216 {
5217 .object_size_mb = object_size_mb,
5218 .object_path_override = object_path,
5219 },
5220 };
5221
5222 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
5223 client = aws_s3_client_release(client);
5224 tester.bound_to_client = false;
5225
5226 /*** GET FILE with part_size < file_size ***/
5227 client_options.part_size = MB_TO_BYTES(3);
5228
5229 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5230 uint64_t object_size_hint = MB_TO_BYTES(object_size_mb);
5231
5232 struct aws_s3_tester_meta_request_options get_options = {
5233 .allocator = allocator,
5234 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5235 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5236 .client = client,
5237 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5238 .validate_get_response_checksum = true,
5239 .get_options =
5240 {
5241 .object_path = object_path,
5242 },
5243 .finish_callback = s_s3_test_validate_checksum,
5244 .object_size_hint = &object_size_hint,
5245 };
5246 uint64_t small_object_size_hint = MB_TO_BYTES(1);
5247
5248 /* will do headRequest */
5249 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5250 client = aws_s3_client_release(client);
5251 tester.bound_to_client = false;
5252
5253 /*** GET FILE with part_size > file_size ***/
5254 client_options.part_size = MB_TO_BYTES(20);
5255
5256 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5257 get_options.client = client;
5258
5259 /* will do getPart */
5260 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5261 client = aws_s3_client_release(client);
5262 tester.bound_to_client = false;
5263
5264 /* will do getPart */
5265 /*** GET FILE with part_size = file_size ***/
5266 client_options.part_size = MB_TO_BYTES(10);
5267
5268 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5269 get_options.client = client;
5270 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5271 client = aws_s3_client_release(client);
5272 tester.bound_to_client = false;
5273
5274 /*** GET FILE with part_size < file_size and wrong object_size_hint ***/
5275 client_options.part_size = MB_TO_BYTES(3);
5276
5277 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5278 get_options.client = client;
5279 get_options.object_size_hint = &small_object_size_hint;
5280 /* will do getPart first, cancel it and then rangedGet */
5281 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5282 client = aws_s3_client_release(client);
5283 tester.bound_to_client = false;
5284
5285 aws_byte_buf_clean_up(&path_buf);
5286 aws_s3_tester_clean_up(&tester);
5287
5288 return 0;
5289 }
5290
5291 AWS_TEST_CASE(test_s3_download_multipart_file_with_checksum, s_test_s3_download_multipart_file_with_checksum)
5292 static int s_test_s3_download_multipart_file_with_checksum(struct aws_allocator *allocator, void *ctx) {
5293 (void)ctx;
5294
5295 /* Upload the file */
5296 struct aws_s3_tester tester;
5297 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5298 struct aws_s3_tester_client_options client_options = {
5299 .part_size = MB_TO_BYTES(5),
5300 };
5301
5302 struct aws_s3_client *client = NULL;
5303 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5304
5305 struct aws_byte_buf path_buf;
5306 AWS_ZERO_STRUCT(path_buf);
5307
5308 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
5309 allocator, &path_buf, aws_byte_cursor_from_c_str("/multipart-10Mb-CRC32.txt")));
5310
5311 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5312 uint32_t object_size_mb = 10;
5313
5314 struct aws_s3_tester_meta_request_options put_options = {
5315 .allocator = allocator,
5316 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5317 .client = client,
5318 .checksum_algorithm = AWS_SCA_CRC32,
5319 .put_options =
5320 {
5321 .object_size_mb = object_size_mb,
5322 .object_path_override = object_path,
5323 },
5324 };
5325
5326 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
5327 client = aws_s3_client_release(client);
5328 tester.bound_to_client = false;
5329
5330 /*** GET FILE with part_size < first_part_size ***/
5331 client_options.part_size = MB_TO_BYTES(3);
5332
5333 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5334 uint64_t object_size_hint = MB_TO_BYTES(object_size_mb);
5335
5336 struct aws_s3_tester_meta_request_options get_options = {
5337 .allocator = allocator,
5338 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5339 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5340 .client = client,
5341 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5342 .validate_get_response_checksum = true,
5343 .get_options =
5344 {
5345 .object_path = object_path,
5346 },
5347 .object_size_hint = &object_size_hint,
5348 };
5349
5350 /* will do HeadRequest first */
5351 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5352 client = aws_s3_client_release(client);
5353 tester.bound_to_client = false;
5354
5355 /*** GET FILE with part_size > first_part_size ***/
5356 client_options.part_size = MB_TO_BYTES(7);
5357
5358 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5359 get_options.client = client;
5360 /* will do HeadObject first */
5361 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5362 client = aws_s3_client_release(client);
5363 tester.bound_to_client = false;
5364
5365 /*** GET FILE with part_size = first_part_size ***/
5366 client_options.part_size = MB_TO_BYTES(5);
5367
5368 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5369 get_options.client = client;
5370 get_options.finish_callback = s_s3_test_validate_checksum;
5371 /* will do HeadObject First */
5372 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5373 client = aws_s3_client_release(client);
5374 tester.bound_to_client = false;
5375
5376 uint64_t small_object_size_hint = 1;
5377
5378 /*** GET FILE with with wrong object_size_hint ***/
5379 get_options.object_size_hint = &small_object_size_hint;
5380 get_options.finish_callback = NULL;
5381
5382 /*** GET FILE with part_size < first_part_size***/
5383 client_options.part_size = MB_TO_BYTES(3);
5384
5385 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5386 get_options.client = client;
5387
5388 /* will do GetPart, cancel the request and then do ranged Gets. */
5389 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5390 client = aws_s3_client_release(client);
5391 tester.bound_to_client = false;
5392
5393 /*** GET FILE with part_size > first_part_size ***/
5394 client_options.part_size = MB_TO_BYTES(7);
5395
5396 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5397 get_options.client = client;
5398 /* Wrong hint causes buffer-sized ranged gets that don't align to S3 part boundaries, so no checksum. */
5399 get_options.finish_callback = s_s3_test_no_validate_checksum;
5400
5401 /* will do GetPart, cancel and do ranged Gets that don't align to part boundaries */
5402 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5403 client = aws_s3_client_release(client);
5404 tester.bound_to_client = false;
5405
5406 /*** GET FILE with part_size = first_part_size ***/
5407 client_options.part_size = MB_TO_BYTES(5);
5408
5409 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5410 get_options.client = client;
5411 get_options.finish_callback = s_s3_test_validate_checksum;
5412 /* will do GetPart first */
5413 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5414 client = aws_s3_client_release(client);
5415 tester.bound_to_client = false;
5416
5417 /*** GET FILE with part_size > fileSize ***/
5418 client_options.part_size = MB_TO_BYTES(20);
5419
5420 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5421 get_options.client = client;
5422 /* Wrong hint causes mismatch; ranged get spans full object, misaligned from stored parts, so no checksum. */
5423 get_options.finish_callback = s_s3_test_no_validate_checksum;
5424 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5425 client = aws_s3_client_release(client);
5426 tester.bound_to_client = false;
5427
5428 aws_byte_buf_clean_up(&path_buf);
5429 aws_s3_tester_clean_up(&tester);
5430
5431 return 0;
5432 }
5433
5434 AWS_TEST_CASE(
5435 test_s3_round_trip_mpu_multipart_get_with_list_algorithm_fc,
5436 s_test_s3_round_trip_mpu_multipart_get_with_list_algorithm_fc)
5437 static int s_test_s3_round_trip_mpu_multipart_get_with_list_algorithm_fc(struct aws_allocator *allocator, void *ctx) {
5438 (void)ctx;
5439
5440 struct aws_s3_tester tester;
5441 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5442 struct aws_s3_tester_client_options client_options = {
5443 .part_size = MB_TO_BYTES(5),
5444 };
5445
5446 struct aws_s3_client *client = NULL;
5447 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5448
5449 struct aws_byte_buf path_buf;
5450 AWS_ZERO_STRUCT(path_buf);
5451
5452 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
5453 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_mpu_fc.txt")));
5454
5455 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5456
5457 struct aws_s3_tester_meta_request_options put_options = {
5458 .allocator = allocator,
5459 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5460 .client = client,
5461 .checksum_algorithm = AWS_SCA_CRC32,
5462 .validate_get_response_checksum = false,
5463 .put_options =
5464 {
5465 .object_size_mb = 10,
5466 .object_path_override = object_path,
5467 },
5468 };
5469
5470 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
5471
5472 /*** GET FILE ***/
5473
5474 struct aws_array_list response_checksum_list;
5475 /* Check for all algorithm but the CRC32 */
5476 ASSERT_SUCCESS(
5477 aws_array_list_init_dynamic(&response_checksum_list, allocator, 4, sizeof(enum aws_s3_checksum_algorithm)));
5478 enum aws_s3_checksum_algorithm alg = AWS_SCA_CRC32C;
5479 ASSERT_SUCCESS(aws_array_list_push_back(&response_checksum_list, &alg));
5480 alg = AWS_SCA_SHA1;
5481 ASSERT_SUCCESS(aws_array_list_push_back(&response_checksum_list, &alg));
5482 alg = AWS_SCA_SHA256;
5483 ASSERT_SUCCESS(aws_array_list_push_back(&response_checksum_list, &alg));
5484
5485 struct aws_s3_tester_meta_request_options get_options = {
5486 .allocator = allocator,
5487 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5488 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5489 .client = client,
5490 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5491 .validate_get_response_checksum = true,
5492 .validate_checksum_algorithms = &response_checksum_list,
5493 .get_options =
5494 {
5495 .object_path = object_path,
5496 },
5497 .finish_callback = s_s3_test_no_validate_checksum,
5498 };
5499
5500 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5501
5502 /* Push all the algorithms to the list for validation, now we should have the checksum validated. */
5503 alg = AWS_SCA_CRC32;
5504 ASSERT_SUCCESS(aws_array_list_push_back(&response_checksum_list, &alg));
5505 get_options.finish_callback = s_s3_test_validate_checksum;
5506 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5507
5508 aws_byte_buf_clean_up(&path_buf);
5509 aws_s3_client_release(client);
5510 aws_s3_tester_clean_up(&tester);
5511 aws_array_list_clean_up(&response_checksum_list);
5512
5513 return 0;
5514 }
5515
5516 AWS_TEST_CASE(test_s3_round_trip_mpu_default_get_fc, s_test_s3_round_trip_mpu_default_get_fc)
5517 static int s_test_s3_round_trip_mpu_default_get_fc(struct aws_allocator *allocator, void *ctx) {
5518 (void)ctx;
5519
5520 struct aws_s3_tester tester;
5521 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5522 struct aws_s3_tester_client_options client_options = {
5523 .part_size = MB_TO_BYTES(5),
5524 };
5525
5526 struct aws_s3_client *client = NULL;
5527 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5528
5529 struct aws_byte_buf path_buf;
5530 AWS_ZERO_STRUCT(path_buf);
5531
5532 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
5533 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/test_mpu_default_get_fc.txt")));
5534
5535 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5536
5537 struct aws_s3_tester_meta_request_options put_options = {
5538 .allocator = allocator,
5539 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5540 .client = client,
5541 .checksum_algorithm = AWS_SCA_CRC32,
5542 .validate_get_response_checksum = false,
5543 .put_options =
5544 {
5545 .object_size_mb = 10,
5546 .object_path_override = object_path,
5547 },
5548 };
5549
5550 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, NULL));
5551
5552 /*** GET FILE ***/
5553
5554 struct aws_s3_tester_meta_request_options get_options = {
5555 .allocator = allocator,
5556 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
5557 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5558 .client = client,
5559 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5560 .validate_get_response_checksum = true,
5561 .get_options =
5562 {
5563 .object_path = object_path,
5564 },
5565 .default_type_options =
5566 {
5567 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
5568 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
5569 },
5570 .finish_callback = s_s3_test_no_validate_checksum,
5571 .headers_callback = s_s3_validate_headers_checksum_unset,
5572 };
5573
5574 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
5575
5576 aws_byte_buf_clean_up(&path_buf);
5577 aws_s3_client_release(client);
5578 aws_s3_tester_clean_up(&tester);
5579
5580 return 0;
5581 }
5582
5583 static int s_test_s3_round_trip_with_filepath_helper(
5584 struct aws_allocator *allocator,
5585 struct aws_byte_cursor key,
5586 int object_size_mb,
5587 bool unknown_content_length,
5588 bool streaming,
5589 bool via_header,
5590 enum aws_s3_tester_full_object_checksum full_object_checksum) {
5591
5592 struct aws_s3_tester tester;
5593 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5594
5595 struct aws_s3_tester_client_options client_options = {
5596 .part_size = MB_TO_BYTES(8),
5597 };
5598
5599 struct aws_s3_client *client = NULL;
5600 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5601
5602 struct aws_s3_meta_request_test_results test_results;
5603 aws_s3_meta_request_test_results_init(&test_results, allocator);
5604
5605 /*** PUT FILE ***/
5606
5607 struct aws_byte_buf path_buf;
5608 AWS_ZERO_STRUCT(path_buf);
5609
5610 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(allocator, &path_buf, key));
5611
5612 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5613 /* Configure file I/O options for streaming upload */
5614 struct aws_s3_file_io_options fio_opts = {
5615 .should_stream = streaming,
5616 .direct_io = true,
5617 };
5618
5619 struct aws_s3_tester_meta_request_options put_options = {
5620 .allocator = allocator,
5621 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5622 .client = client,
5623 .checksum_algorithm = AWS_SCA_CRC32,
5624 .validate_get_response_checksum = false,
5625 .fio_opts = &fio_opts,
5626 .checksum_via_header = via_header,
5627 .put_options =
5628 {
5629 .object_size_mb = object_size_mb,
5630 .object_path_override = object_path,
5631 .file_on_disk = true,
5632 .skip_content_length = unknown_content_length,
5633 .full_object_checksum = full_object_checksum,
5634 },
5635 };
5636
5637 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
5638 aws_s3_meta_request_test_results_clean_up(&test_results);
5639
5640 /*** GET FILE ***/
5641 aws_s3_meta_request_test_results_init(&test_results, allocator);
5642
5643 struct aws_s3_tester_meta_request_options get_options = {
5644 .allocator = allocator,
5645 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5646 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5647 .client = client,
5648 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5649 .validate_get_response_checksum = true,
5650 .get_options =
5651 {
5652 .object_path = object_path,
5653 .file_on_disk = true,
5654 },
5655 };
5656 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &test_results));
5657
5658 ASSERT_UINT_EQUALS(
5659 MB_TO_BYTES(put_options.put_options.object_size_mb), test_results.progress.total_bytes_transferred);
5660
5661 aws_s3_meta_request_test_results_clean_up(&test_results);
5662 aws_byte_buf_clean_up(&path_buf);
5663 aws_s3_client_release(client);
5664 aws_s3_tester_clean_up(&tester);
5665
5666 return 0;
5667 }
5668
5669 AWS_TEST_CASE(test_s3_round_trip_with_filepath, s_test_s3_round_trip_with_filepath)
5670 static int s_test_s3_round_trip_with_filepath(struct aws_allocator *allocator, void *ctx) {
5671 (void)ctx;
5672 return s_test_s3_round_trip_with_filepath_helper(
5673 allocator,
5674 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath"),
5675 1,
5676 false /*unknown_content_length*/,
5677 false /*streaming*/,
5678 false /*checksum via_header*/,
5679 AWS_TEST_FOC_NONE);
5680 }
5681
5682 AWS_TEST_CASE(test_s3_round_trip_mpu_with_filepath, s_test_s3_round_trip_mpu_with_filepath)
5683 static int s_test_s3_round_trip_mpu_with_filepath(struct aws_allocator *allocator, void *ctx) {
5684 (void)ctx;
5685 return s_test_s3_round_trip_with_filepath_helper(
5686 allocator,
5687 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu"),
5688 50,
5689 false /*unknown_content_length*/,
5690 false /*streaming*/,
5691 false /*checksum via_header*/,
5692 AWS_TEST_FOC_NONE);
5693 }
5694
5695 AWS_TEST_CASE(test_s3_round_trip_with_filepath_no_content_length, s_test_s3_round_trip_with_filepath_no_content_length)
5696 static int s_test_s3_round_trip_with_filepath_no_content_length(struct aws_allocator *allocator, void *ctx) {
5697 (void)ctx;
5698 return s_test_s3_round_trip_with_filepath_helper(
5699 allocator,
5700 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_no_content_length"),
5701 1,
5702 true /*unknown_content_length*/,
5703 false /*streaming*/,
5704 false /*checksum via_header*/,
5705 AWS_TEST_FOC_NONE);
5706 }
5707
5708 AWS_TEST_CASE(
5709 test_s3_round_trip_mpu_with_filepath_no_content_length,
5710 s_test_s3_round_trip_mpu_with_filepath_no_content_length)
5711 static int s_test_s3_round_trip_mpu_with_filepath_no_content_length(struct aws_allocator *allocator, void *ctx) {
5712 (void)ctx;
5713 return s_test_s3_round_trip_with_filepath_helper(
5714 allocator,
5715 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu_no_content_length"),
5716 50,
5717 true /*unknown_content_length*/,
5718 false /*streaming*/,
5719 false /*checksum via_header*/,
5720 AWS_TEST_FOC_NONE);
5721 }
5722
5723 AWS_TEST_CASE(test_s3_round_trip_mpu_with_filepath_streaming, s_test_s3_round_trip_mpu_with_filepath_streaming)
5724 static int s_test_s3_round_trip_mpu_with_filepath_streaming(struct aws_allocator *allocator, void *ctx) {
5725 (void)ctx;
5726 return s_test_s3_round_trip_with_filepath_helper(
5727 allocator,
5728 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu_streaming"),
5729 50,
5730 false /*unknown_content_length*/,
5731 true /*streaming*/,
5732 false /*checksum via_header*/,
5733 AWS_TEST_FOC_NONE);
5734 }
5735
5736 AWS_TEST_CASE(
5737 test_s3_round_trip_mpu_with_filepath_no_content_length_streaming,
5738 s_test_s3_round_trip_mpu_with_filepath_no_content_length_streaming)
5739 static int s_test_s3_round_trip_mpu_with_filepath_no_content_length_streaming(
5740 struct aws_allocator *allocator,
5741 void *ctx) {
5742 (void)ctx;
5743 return s_test_s3_round_trip_with_filepath_helper(
5744 allocator,
5745 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu_no_content_length_streaming"),
5746 50,
5747 true /*unknown_content_length*/,
5748 true /*streaming*/,
5749 false /*checksum via_header*/,
5750 AWS_TEST_FOC_NONE);
5751 }
5752
5753 AWS_TEST_CASE(
5754 test_s3_round_trip_mpu_with_filepath_streaming_full_object_checksum_header,
5755 s_test_s3_round_trip_mpu_with_filepath_streaming_full_object_checksum_header)
5756 static int s_test_s3_round_trip_mpu_with_filepath_streaming_full_object_checksum_header(
5757 struct aws_allocator *allocator,
5758 void *ctx) {
5759 (void)ctx;
5760 return s_test_s3_round_trip_with_filepath_helper(
5761 allocator,
5762 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu_streaming"),
5763 50,
5764 false /*unknown_content_length*/,
5765 true /*streaming*/,
5766 false /*checksum via_header*/,
5767 AWS_TEST_FOC_HEADER);
5768 }
5769
5770 AWS_TEST_CASE(
5771 test_s3_round_trip_mpu_with_filepath_streaming_full_object_checksum_via_callback,
5772 s_test_s3_round_trip_mpu_with_filepath_streaming_full_object_checksum_via_callback)
5773 static int s_test_s3_round_trip_mpu_with_filepath_streaming_full_object_checksum_via_callback(
5774 struct aws_allocator *allocator,
5775 void *ctx) {
5776 (void)ctx;
5777 return s_test_s3_round_trip_with_filepath_helper(
5778 allocator,
5779 aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu_streaming"),
5780 50,
5781 false /*unknown_content_length*/,
5782 true /*streaming*/,
5783 false /*checksum via_header*/,
5784 AWS_TEST_FOC_CALLBACK);
5785 }
5786
5787 static int s_check_metrics_upload_part_helper(
5788 struct aws_s3_meta_request_test_results *test_results,
5789 size_t expected_part_size,
5790 size_t index) {
5791 struct aws_s3_request_metrics *metrics = NULL;
5792 /* The upload part can be completed out of order. Just make sure the range matches the part number */
5793 ASSERT_SUCCESS(aws_array_list_get_at(&test_results->synced_data.succeed_metrics, (void **)&metrics, index));
5794 enum aws_s3_request_type out_request_type = AWS_S3_REQUEST_TYPE_UNKNOWN;
5795 aws_s3_request_metrics_get_request_type(metrics, &out_request_type);
5796 uint32_t part_number = 0;
5797 aws_s3_request_metrics_get_part_number(metrics, &part_number);
5798 ASSERT_TRUE(part_number >= 1); /* The part number starts at 1 */
5799 /* calculate the expected range start and end based on the part number and part size. */
5800 size_t expected_range_start = (part_number - 1) * expected_part_size;
5801 size_t expected_range_end = part_number * expected_part_size - 1;
5802
5803 uint64_t range_start = 0;
5804 uint64_t range_end = 0;
5805 aws_s3_request_metrics_get_part_range_start(metrics, &range_start);
5806 aws_s3_request_metrics_get_part_range_end(metrics, &range_end);
5807 ASSERT_UINT_EQUALS(AWS_S3_REQUEST_TYPE_UPLOAD_PART, out_request_type);
5808 ASSERT_UINT_EQUALS(expected_range_start, range_start);
5809 ASSERT_UINT_EQUALS(expected_range_end, range_end);
5810
5811 return AWS_OP_SUCCESS;
5812 }
5813
5814 static int s_check_metrics_helper(
5815 struct aws_s3_meta_request_test_results *test_results,
5816 size_t index,
5817 enum aws_s3_request_type expected_type,
5818 size_t expected_part_number,
5819 size_t expected_range_start,
5820 size_t expected_range_end) {
5821 struct aws_s3_request_metrics *metrics = NULL;
5822
5823 ASSERT_SUCCESS(aws_array_list_get_at(&test_results->synced_data.succeed_metrics, (void **)&metrics, index));
5824 enum aws_s3_request_type out_request_type = AWS_S3_REQUEST_TYPE_UNKNOWN;
5825 aws_s3_request_metrics_get_request_type(metrics, &out_request_type);
5826 uint32_t part_number = 0;
5827 uint64_t range_start = 0;
5828 uint64_t range_end = 0;
5829 aws_s3_request_metrics_get_part_number(metrics, &part_number);
5830 aws_s3_request_metrics_get_part_range_start(metrics, &range_start);
5831 aws_s3_request_metrics_get_part_range_end(metrics, &range_end);
5832 ASSERT_UINT_EQUALS(expected_type, out_request_type);
5833 ASSERT_UINT_EQUALS(expected_part_number, part_number);
5834 ASSERT_UINT_EQUALS(expected_range_start, range_start);
5835 ASSERT_UINT_EQUALS(expected_range_end, range_end);
5836
5837 return AWS_OP_SUCCESS;
5838 }
5839
5840 AWS_TEST_CASE(
5841 test_s3_round_trip_dynamic_range_size_download_multipart,
5842 s_test_s3_round_trip_dynamic_range_size_download_multipart)
5843 static int s_test_s3_round_trip_dynamic_range_size_download_multipart(struct aws_allocator *allocator, void *ctx) {
5844 (void)ctx;
5845 struct aws_s3_tester tester;
5846 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
5847
5848 size_t stored_part_size = MB_TO_BYTES(15);
5849 /* Gives it a higher memory limit to allow parts around 15MiB */
5850 struct aws_s3_tester_client_options client_options = {
5851 .memory_limit_in_bytes = GB_TO_BYTES(2),
5852 };
5853
5854 struct aws_s3_client *client = NULL;
5855 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
5856
5857 struct aws_s3_meta_request_test_results test_results;
5858 aws_s3_meta_request_test_results_init(&test_results, allocator);
5859 struct aws_byte_buf path_buf;
5860 AWS_ZERO_STRUCT(path_buf);
5861
5862 {
5863
5864 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
5865 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/dynamic_size_multipart")));
5866 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
5867 /* Configure file I/O options for streaming upload */
5868 struct aws_s3_file_io_options fio_opts = {
5869 .should_stream = true,
5870 .direct_io = true,
5871 };
5872
5873 size_t object_size_mb = 30;
5874 size_t object_size = MB_TO_BYTES(object_size_mb);
5875 struct aws_s3_tester_meta_request_options put_options = {
5876 .allocator = allocator,
5877 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
5878 .client = client,
5879 .checksum_algorithm = AWS_SCA_CRC32,
5880 .validate_get_response_checksum = false,
5881 .fio_opts = &fio_opts,
5882 .part_size = stored_part_size,
5883 .put_options =
5884 {
5885 .object_size_mb = (uint32_t)object_size_mb,
5886 .object_path_override = object_path,
5887 .file_on_disk = true,
5888 .full_object_checksum = true,
5889 },
5890 };
5891
5892 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
5893 ASSERT_SUCCESS(s_check_metrics_helper(&test_results, 0, AWS_S3_REQUEST_TYPE_CREATE_MULTIPART_UPLOAD, 0, 0, 0));
5894 ASSERT_SUCCESS(s_check_metrics_upload_part_helper(&test_results, stored_part_size, 1));
5895 ASSERT_SUCCESS(s_check_metrics_upload_part_helper(&test_results, stored_part_size, 2));
5896 aws_s3_meta_request_test_results_clean_up(&test_results);
5897
5898 /*** GET FILE WITH CHECKSUM -- Head object first ***/
5899 /* Get object will use the aligned part size based on the estimated part size stored in S3 */
5900 uint64_t aligned_part_size_u64 =
5901 aws_s3_buffer_pool_derive_aligned_buffer_size(client->buffer_pool, stored_part_size);
5902 size_t aligned_part_size = (size_t)aligned_part_size_u64;
5903 aws_s3_meta_request_test_results_init(&test_results, allocator);
5904
5905 struct aws_s3_tester_meta_request_options get_options_with_checksum = {
5906 .allocator = allocator,
5907 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5908 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5909 .client = client,
5910 .expected_validate_checksum_alg = AWS_SCA_CRC32,
5911 .validate_get_response_checksum = true,
5912 .get_options =
5913 {
5914 .object_path = object_path,
5915 .file_on_disk = true,
5916 .force_dynamic_part_size = true,
5917 },
5918 };
5919 ASSERT_SUCCESS(
5920 aws_s3_tester_send_meta_request_with_options(&tester, &get_options_with_checksum, &test_results));
5921 ASSERT_TRUE(test_results.did_validate);
5922 ASSERT_UINT_EQUALS(AWS_SCA_CRC32, test_results.validation_algorithm);
5923 /* The tests has been done, we are safe to touch the synced data from test results. */
5924 ASSERT_UINT_EQUALS(3, aws_array_list_length(&test_results.synced_data.succeed_metrics));
5925 /* First request made was head object and the range should be 0 */
5926 ASSERT_SUCCESS(s_check_metrics_helper(&test_results, 0, AWS_S3_REQUEST_TYPE_HEAD_OBJECT, 0, 0, 0));
5927 /* Second request made should be get with range and range from 0 to stored part size -1. */
5928 ASSERT_SUCCESS(
5929 s_check_metrics_helper(&test_results, 1, AWS_S3_REQUEST_TYPE_GET_OBJECT, 1, 0, aligned_part_size - 1));
5930 /* The last part will be ending with the total size of the object */
5931 ASSERT_SUCCESS(s_check_metrics_helper(
5932 &test_results, 2, AWS_S3_REQUEST_TYPE_GET_OBJECT, 2, aligned_part_size, object_size - 1));
5933
5934 aws_s3_meta_request_test_results_clean_up(&test_results);
5935
5936 /*** GET FILE WITHOUT CHECKSUM -- RANGE get first ***/
5937 aws_s3_meta_request_test_results_init(&test_results, allocator);
5938
5939 struct aws_s3_tester_meta_request_options get_options_without_checksum = {
5940 .allocator = allocator,
5941 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5942 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5943 .client = client,
5944 .validate_get_response_checksum = false,
5945 .get_options =
5946 {
5947 .object_path = object_path,
5948 .file_on_disk = true,
5949 .force_dynamic_part_size = true,
5950 },
5951 };
5952 ASSERT_SUCCESS(
5953 aws_s3_tester_send_meta_request_with_options(&tester, &get_options_without_checksum, &test_results));
5954 ASSERT_FALSE(test_results.did_validate);
5955 /* The tests has been done, we are safe to touch the synced data from test results. */
5956 ASSERT_UINT_EQUALS(3, aws_array_list_length(&test_results.synced_data.succeed_metrics));
5957 /* First request made was Get object and the range should be 0 to default range - 1 */
5958 ASSERT_SUCCESS(s_check_metrics_helper(
5959 &test_results, 0, AWS_S3_REQUEST_TYPE_GET_OBJECT, 1, 0, (size_t)g_default_part_size_fallback - 1));
5960 /* Second request made should be get with range and range from 0 to optimal part size. */
5961 ASSERT_SUCCESS(s_check_metrics_helper(
5962 &test_results,
5963 1,
5964 AWS_S3_REQUEST_TYPE_GET_OBJECT,
5965 2,
5966 (size_t)g_default_part_size_fallback,
5967 (size_t)g_default_part_size_fallback + aligned_part_size - 1));
5968 aws_s3_meta_request_test_results_clean_up(&test_results);
5969
5970 /*** GET FILE WITHOUT FORCING -- old behavior should NOT be changed ***/
5971 aws_s3_meta_request_test_results_init(&test_results, allocator);
5972
5973 struct aws_s3_tester_meta_request_options get_options = {
5974 .allocator = allocator,
5975 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
5976 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
5977 .client = client,
5978 .validate_get_response_checksum = false,
5979 .part_size = (size_t)g_default_part_size_fallback,
5980 .get_options =
5981 {
5982 .object_path = object_path,
5983 .file_on_disk = true,
5984 .force_dynamic_part_size = false,
5985 },
5986 };
5987 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &test_results));
5988 ASSERT_FALSE(test_results.did_validate);
5989 /* The tests has been done, we are safe to touch the synced data from test results. */
5990 /* 4 parts in total for 30MiB with 8MiB part */
5991 ASSERT_UINT_EQUALS(4, aws_array_list_length(&test_results.synced_data.succeed_metrics));
5992 aws_s3_meta_request_test_results_clean_up(&test_results);
5993 }
5994 /* TODO: The MPU file still works with dynamic part size */
5995 aws_byte_buf_clean_up(&path_buf);
5996 aws_s3_client_release(client);
5997 aws_s3_tester_clean_up(&tester);
5998
5999 return 0;
6000 }
6001
6002 void s_s3_test_buffer_pool_optimize_before_finish(
6003 struct aws_s3_meta_request *meta_request,
6004 const struct aws_s3_meta_request_result *result,
6005 void *user_data) {
6006 (void)meta_request;
6007 struct aws_s3_meta_request_test_results *meta_request_test_results =
6008 (struct aws_s3_meta_request_test_results *)user_data;
6009
6010 struct aws_s3_default_buffer_pool_usage_stats pool_usage =
6011 aws_s3_default_buffer_pool_get_usage(meta_request->client->buffer_pool);
6012 /* Make sure the special block is used here. */
6013 AWS_FATAL_ASSERT(pool_usage.special_blocks_num == 1);
6014 AWS_FATAL_ASSERT(pool_usage.special_blocks_allocated > 0);
6015 AWS_FATAL_ASSERT(result->did_validate);
6016 AWS_FATAL_ASSERT(result->validation_algorithm == meta_request_test_results->algorithm);
6017 AWS_FATAL_ASSERT(result->error_code == AWS_OP_SUCCESS);
6018 }
6019
6020 AWS_TEST_CASE(
6021 test_s3_round_trip_dynamic_range_size_download_multipart_with_buffer_pool_optimize,
6022 s_test_s3_round_trip_dynamic_range_size_download_multipart_with_buffer_pool_optimize)
6023 static int s_test_s3_round_trip_dynamic_range_size_download_multipart_with_buffer_pool_optimize(
6024 struct aws_allocator *allocator,
6025 void *ctx) {
6026 (void)ctx;
6027 struct aws_s3_tester tester;
6028 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6029
6030 size_t stored_part_size = MB_TO_BYTES(10);
6031 size_t object_size_mb = 120;
6032 /**
6033 * Given the implementation in Oct. 2025:
6034 * - The dynamic part size will be at least g_default_part_size_fallback, which is 8MiB.
6035 * - The special size will be used only if there will be more than 2 times of max connections get object requests to
6036 * be made. (parts_threshold)
6037 * - The buffer pool will create the special size blocks when it's larger than the primary cutoff.
6038 * - The buffer pool will align the range size with the chunk size, which is the part size in the client config.
6039 *
6040 * Thus
6041 * - We use 10MiB as the part size to upload, which will result in the dynamic adjustment to set the range to 10MiB
6042 * for download.
6043 * - Set the max_active_connections_override to 5, so that the parts_threshold will be 2*5, which is 10.
6044 * - Object size will need to be larger than 10 * 10, which is 100. We pick 120 here.
6045 * - Set the part size to be 1MiB, so that the range size will be larger than the primary cutoff from the pool.
6046 **/
6047 struct aws_s3_tester_client_options client_options = {
6048 .memory_limit_in_bytes = GB_TO_BYTES(2),
6049 .max_active_connections_override = 5,
6050 .part_size = MB_TO_BYTES(1),
6051 };
6052
6053 struct aws_s3_client *client = NULL;
6054 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
6055
6056 struct aws_s3_meta_request_test_results test_results;
6057 aws_s3_meta_request_test_results_init(&test_results, allocator);
6058 struct aws_byte_buf path_buf;
6059 AWS_ZERO_STRUCT(path_buf);
6060
6061 {
6062
6063 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
6064 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/dynamic_size_multipart_with_pool")));
6065 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
6066 /* Configure file I/O options for streaming upload */
6067 struct aws_s3_file_io_options fio_opts = {
6068 .should_stream = true,
6069 .direct_io = true,
6070 };
6071
6072 struct aws_s3_tester_meta_request_options put_options = {
6073 .allocator = allocator,
6074 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
6075 .client = client,
6076 .checksum_algorithm = AWS_SCA_CRC32,
6077 .validate_get_response_checksum = false,
6078 .fio_opts = &fio_opts,
6079 .part_size = stored_part_size,
6080 .put_options =
6081 {
6082 .object_size_mb = (uint32_t)object_size_mb,
6083 .object_path_override = object_path,
6084 .file_on_disk = true,
6085 .full_object_checksum = true,
6086 },
6087 };
6088
6089 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
6090 aws_s3_meta_request_test_results_clean_up(&test_results);
6091
6092 /*** GET FILE WITH CHECKSUM -- Head object first ***/
6093 aws_s3_meta_request_test_results_init(&test_results, allocator);
6094
6095 struct aws_s3_tester_meta_request_options get_options_with_checksum = {
6096 .allocator = allocator,
6097 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6098 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
6099 .client = client,
6100 .expected_validate_checksum_alg = AWS_SCA_CRC32,
6101 .validate_get_response_checksum = true,
6102 .finish_callback = s_s3_test_buffer_pool_optimize_before_finish,
6103 .get_options =
6104 {
6105 .object_path = object_path,
6106 .file_on_disk = true,
6107 .force_dynamic_part_size = true,
6108 },
6109 };
6110 ASSERT_SUCCESS(
6111 aws_s3_tester_send_meta_request_with_options(&tester, &get_options_with_checksum, &test_results));
6112 ASSERT_TRUE(test_results.did_validate);
6113 /* After the meta request finishes, the special block is trimmed. */
6114 struct aws_s3_default_buffer_pool_usage_stats pool_usage =
6115 aws_s3_default_buffer_pool_get_usage(client->buffer_pool);
6116 ASSERT_UINT_EQUALS(pool_usage.special_blocks_num, 0);
6117 ASSERT_UINT_EQUALS(pool_usage.special_blocks_allocated, 0);
6118 aws_s3_meta_request_test_results_clean_up(&test_results);
6119 }
6120 aws_byte_buf_clean_up(&path_buf);
6121 aws_s3_client_release(client);
6122 aws_s3_tester_clean_up(&tester);
6123
6124 return 0;
6125 }
6126
6127 AWS_TEST_CASE(
6128 test_s3_round_trip_dynamic_range_size_download_single_part,
6129 s_test_s3_round_trip_dynamic_range_size_download_single_part)
6130 static int s_test_s3_round_trip_dynamic_range_size_download_single_part(struct aws_allocator *allocator, void *ctx) {
6131 (void)ctx;
6132 struct aws_s3_tester tester;
6133 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6134
6135 /* Gives it a higher memory limit to allow parts around 30MiB */
6136 struct aws_s3_tester_client_options client_options = {
6137 .part_size = MB_TO_BYTES(8),
6138 .memory_limit_in_bytes = GB_TO_BYTES(5),
6139 };
6140
6141 struct aws_s3_client *client = NULL;
6142 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
6143
6144 struct aws_s3_meta_request_test_results test_results;
6145 aws_s3_meta_request_test_results_init(&test_results, allocator);
6146 struct aws_byte_buf path_buf;
6147 AWS_ZERO_STRUCT(path_buf);
6148
6149 /*** 1. SINGLE PART ***/
6150 {
6151
6152 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
6153 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/dynamic_size_singlepart")));
6154 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
6155 /* Configure file I/O options for streaming upload */
6156 struct aws_s3_file_io_options fio_opts = {
6157 .should_stream = true,
6158 .direct_io = true,
6159 };
6160
6161 size_t stored_part_size_mb = 30;
6162 struct aws_s3_tester_meta_request_options put_options = {
6163 .allocator = allocator,
6164 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
6165 .client = client,
6166 .checksum_algorithm = AWS_SCA_CRC32,
6167 .validate_get_response_checksum = false,
6168 .fio_opts = &fio_opts,
6169 .default_type_options =
6170 {
6171 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_PUT,
6172 .operation_name = aws_byte_cursor_from_c_str("PutObject"),
6173 },
6174 .put_options =
6175 {
6176 .object_size_mb = (uint32_t)stored_part_size_mb,
6177 .object_path_override = object_path,
6178 .file_on_disk = true,
6179 .full_object_checksum = true,
6180 },
6181 };
6182
6183 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
6184 aws_s3_meta_request_test_results_clean_up(&test_results);
6185
6186 /*** GET FILE WITH CHECKSUM -- Head object first ***/
6187 aws_s3_meta_request_test_results_init(&test_results, allocator);
6188
6189 struct aws_s3_tester_meta_request_options get_options_with_checksum = {
6190 .allocator = allocator,
6191 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6192 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
6193 .client = client,
6194 .expected_validate_checksum_alg = AWS_SCA_CRC32,
6195 .validate_get_response_checksum = true,
6196 .get_options =
6197 {
6198 .object_path = object_path,
6199 .file_on_disk = true,
6200 .force_dynamic_part_size = true,
6201 },
6202 };
6203 ASSERT_SUCCESS(
6204 aws_s3_tester_send_meta_request_with_options(&tester, &get_options_with_checksum, &test_results));
6205 ASSERT_TRUE(test_results.did_validate);
6206 ASSERT_UINT_EQUALS(AWS_SCA_CRC32, test_results.validation_algorithm);
6207 /* The tests has been done, we are safe to touch the synced data from test results. */
6208 ASSERT_UINT_EQUALS(2, aws_array_list_length(&test_results.synced_data.succeed_metrics));
6209 /* First request made was head object and the range should be 0 */
6210 ASSERT_SUCCESS(s_check_metrics_helper(&test_results, 0, AWS_S3_REQUEST_TYPE_HEAD_OBJECT, 0, 0, 0));
6211 /* Second request made should be get with range and range from 0 to optimal part size. */
6212 ASSERT_SUCCESS(s_check_metrics_helper(
6213 &test_results, 1, AWS_S3_REQUEST_TYPE_GET_OBJECT, 1, 0, MB_TO_BYTES(stored_part_size_mb) - 1));
6214
6215 aws_s3_meta_request_test_results_clean_up(&test_results);
6216
6217 /*** GET FILE WITHOUT CHECKSUM -- RANGE get first ***/
6218 aws_s3_meta_request_test_results_init(&test_results, allocator);
6219
6220 struct aws_s3_tester_meta_request_options get_options_without_checksum = {
6221 .allocator = allocator,
6222 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6223 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
6224 .client = client,
6225 .validate_get_response_checksum = false,
6226 .get_options =
6227 {
6228 .object_path = object_path,
6229 .file_on_disk = true,
6230 .force_dynamic_part_size = true,
6231 },
6232 };
6233 ASSERT_SUCCESS(
6234 aws_s3_tester_send_meta_request_with_options(&tester, &get_options_without_checksum, &test_results));
6235 ASSERT_FALSE(test_results.did_validate);
6236 /* The tests has been done, we are safe to touch the synced data from test results. */
6237 ASSERT_UINT_EQUALS(2, aws_array_list_length(&test_results.synced_data.succeed_metrics));
6238 /* First request made was Get object and the range should be 0 to default range - 1 */
6239 ASSERT_SUCCESS(s_check_metrics_helper(
6240 &test_results, 0, AWS_S3_REQUEST_TYPE_GET_OBJECT, 1, 0, (size_t)g_default_part_size_fallback - 1));
6241 /* Second request made should be get with range and range from 0 to optimal part size. */
6242 ASSERT_SUCCESS(s_check_metrics_helper(
6243 &test_results,
6244 1,
6245 AWS_S3_REQUEST_TYPE_GET_OBJECT,
6246 2,
6247 (size_t)g_default_part_size_fallback,
6248 MB_TO_BYTES(stored_part_size_mb) - 1));
6249 aws_s3_meta_request_test_results_clean_up(&test_results);
6250
6251 /*** GET FILE WITHOUT FORCING -- old behavior should be changed ***/
6252 aws_s3_meta_request_test_results_init(&test_results, allocator);
6253
6254 struct aws_s3_tester_meta_request_options get_options = {
6255 .allocator = allocator,
6256 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6257 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
6258 .client = client,
6259 .validate_get_response_checksum = false,
6260 .get_options =
6261 {
6262 .object_path = object_path,
6263 .file_on_disk = true,
6264 .force_dynamic_part_size = false,
6265 },
6266 };
6267 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &test_results));
6268 ASSERT_FALSE(test_results.did_validate);
6269 /* The tests has been done, we are safe to touch the synced data from test results. */
6270 /* 4 parts in total for 30MiB with 8MiB part */
6271 ASSERT_UINT_EQUALS(4, aws_array_list_length(&test_results.synced_data.succeed_metrics));
6272 aws_s3_meta_request_test_results_clean_up(&test_results);
6273 }
6274 /* TODO: The MPU file still works with dynamic part size */
6275 aws_byte_buf_clean_up(&path_buf);
6276 aws_s3_client_release(client);
6277 aws_s3_tester_clean_up(&tester);
6278
6279 return 0;
6280 }
6281
6282 /* When streaming, the data will be read after the headers are sent, so, we don't support set the parts level checksum
6283 * via headers when streaming. */
6284 AWS_TEST_CASE(
6285 test_s3_mpu_with_filepath_streaming_dont_support_parts_checksum_via_header,
6286 s_test_s3_mpu_with_filepath_streaming_dont_support_parts_checksum_via_header)
6287 static int s_test_s3_mpu_with_filepath_streaming_dont_support_parts_checksum_via_header(
6288 struct aws_allocator *allocator,
6289 void *ctx) {
6290 (void)ctx;
6291
6292 struct aws_s3_tester tester;
6293 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6294
6295 struct aws_s3_tester_client_options client_options = {
6296 .part_size = MB_TO_BYTES(8),
6297 };
6298
6299 struct aws_s3_client *client = NULL;
6300 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
6301
6302 struct aws_s3_meta_request_test_results test_results;
6303 aws_s3_meta_request_test_results_init(&test_results, allocator);
6304 struct aws_byte_buf path_buf;
6305 AWS_ZERO_STRUCT(path_buf); /* Configure file I/O options for streaming upload */
6306 struct aws_s3_file_io_options fio_opts = {
6307 .should_stream = true,
6308 .direct_io = true,
6309 };
6310
6311 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
6312 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/round_trip/with_filepath_mpu_streaming")));
6313
6314 struct aws_byte_cursor object_path = aws_byte_cursor_from_buf(&path_buf);
6315
6316 struct aws_s3_tester_meta_request_options put_options = {
6317 .allocator = allocator,
6318 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
6319 .client = client,
6320 .checksum_algorithm = AWS_SCA_CRC32,
6321 .validate_get_response_checksum = false,
6322 .fio_opts = &fio_opts,
6323 .checksum_via_header = true,
6324 /* Expect to fail. */
6325 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6326 .put_options =
6327 {
6328 .object_size_mb = 50,
6329 .object_path_override = object_path,
6330 .file_on_disk = true,
6331 },
6332 };
6333
6334 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
6335 ASSERT_UINT_EQUALS(test_results.finished_error_code, AWS_ERROR_INVALID_ARGUMENT);
6336 aws_s3_meta_request_test_results_clean_up(&test_results);
6337 aws_byte_buf_clean_up(&path_buf);
6338 aws_s3_client_release(client);
6339 aws_s3_tester_clean_up(&tester);
6340 return AWS_OP_SUCCESS;
6341 }
6342
6343 AWS_TEST_CASE(test_s3_chunked_then_unchunked, s_test_s3_chunked_then_unchunked)
6344 static int s_test_s3_chunked_then_unchunked(struct aws_allocator *allocator, void *ctx) {
6345 (void)ctx;
6346
6347 /* Test to see if signed_body_value modified when signing chunked request */
6348 struct aws_s3_tester tester;
6349 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6350 struct aws_s3_tester_client_options client_options = {
6351 .part_size = MB_TO_BYTES(5),
6352 };
6353
6354 struct aws_s3_client *client = NULL;
6355 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
6356
6357 struct aws_byte_buf path_buf;
6358 AWS_ZERO_STRUCT(path_buf);
6359
6360 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
6361 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/chunked_unchunked/test_chunked.txt")));
6362
6363 struct aws_byte_cursor chunked_object_path = aws_byte_cursor_from_buf(&path_buf);
6364
6365 struct aws_s3_tester_meta_request_options chunked_put_options = {
6366 .allocator = allocator,
6367 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
6368 .client = client,
6369 .checksum_algorithm = AWS_SCA_CRC32,
6370 .validate_get_response_checksum = false,
6371 .put_options =
6372 {
6373 .object_size_mb = 10,
6374 .object_path_override = chunked_object_path,
6375 },
6376 };
6377
6378 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &chunked_put_options, NULL));
6379
6380 aws_byte_buf_clean_up(&path_buf);
6381 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
6382 allocator, &path_buf, aws_byte_cursor_from_c_str("/prefix/chunked_unchunked/test_unchunked.txt")));
6383
6384 struct aws_byte_cursor unchunked_object_path = aws_byte_cursor_from_buf(&path_buf);
6385
6386 struct aws_s3_tester_meta_request_options unchunked_put_options = {
6387 .allocator = allocator,
6388 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
6389 .client = client,
6390 .checksum_algorithm = AWS_SCA_NONE,
6391 .validate_get_response_checksum = false,
6392 .put_options =
6393 {
6394 .object_size_mb = 10,
6395 .object_path_override = unchunked_object_path,
6396 },
6397 };
6398
6399 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &unchunked_put_options, NULL));
6400
6401 aws_byte_buf_clean_up(&path_buf);
6402 aws_s3_client_release(client);
6403 aws_s3_tester_clean_up(&tester);
6404
6405 return 0;
6406 }
6407
6408 AWS_TEST_CASE(test_s3_meta_request_default, s_test_s3_meta_request_default)
6409 static int s_test_s3_meta_request_default(struct aws_allocator *allocator, void *ctx) {
6410 (void)ctx;
6411
6412 struct aws_s3_tester tester;
6413 AWS_ZERO_STRUCT(tester);
6414 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6415
6416 struct aws_s3_client_config client_config;
6417 AWS_ZERO_STRUCT(client_config);
6418
6419 ASSERT_SUCCESS(aws_s3_tester_bind_client(
6420 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
6421
6422 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
6423
6424 struct aws_string *host_name =
6425 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
6426
6427 /* Put together a simple S3 Get Object request. */
6428 struct aws_http_message *message = aws_s3_test_get_object_request_new(
6429 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
6430
6431 struct aws_s3_meta_request_options options;
6432 AWS_ZERO_STRUCT(options);
6433
6434 /* Pass the request through as a default request so that it goes through as-is. */
6435 options.type = AWS_S3_META_REQUEST_TYPE_DEFAULT;
6436 options.operation_name = aws_byte_cursor_from_c_str("GetObject");
6437 options.message = message;
6438
6439 struct aws_s3_meta_request_test_results meta_request_test_results;
6440 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6441
6442 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_results));
6443
6444 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &options);
6445
6446 ASSERT_TRUE(meta_request != NULL);
6447
6448 /* Wait for the request to finish. */
6449 aws_s3_tester_wait_for_meta_request_finish(&tester);
6450
6451 aws_s3_tester_lock_synced_data(&tester);
6452
6453 ASSERT_TRUE(tester.synced_data.finish_error_code == AWS_ERROR_SUCCESS);
6454
6455 aws_s3_tester_unlock_synced_data(&tester);
6456
6457 /* Check the size of the metrics should be the same as the number of
6458 requests, which should be 1 */
6459 ASSERT_UINT_EQUALS(1, aws_array_list_length(&meta_request_test_results.synced_data.succeed_metrics));
6460 struct aws_s3_request_metrics *metrics = NULL;
6461 aws_array_list_back(&meta_request_test_results.synced_data.succeed_metrics, (void **)&metrics);
6462
6463 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0));
6464
6465 meta_request = aws_s3_meta_request_release(meta_request);
6466
6467 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
6468 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6469
6470 aws_http_message_release(message);
6471 message = NULL;
6472
6473 aws_string_destroy(host_name);
6474 host_name = NULL;
6475
6476 client = aws_s3_client_release(client);
6477
6478 aws_s3_tester_clean_up(&tester);
6479
6480 return 0;
6481 }
6482
6483 AWS_TEST_CASE(test_s3_error_missing_file, s_test_s3_error_missing_file)
6484 static int s_test_s3_error_missing_file(struct aws_allocator *allocator, void *ctx) {
6485 (void)ctx;
6486
6487 const struct aws_byte_cursor test_object_path =
6488 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/non-existing-file12345.txt");
6489
6490 struct aws_s3_tester tester;
6491 AWS_ZERO_STRUCT(tester);
6492 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6493
6494 struct aws_s3_client_config client_config = {
6495 .part_size = 64 * 1024,
6496 };
6497
6498 ASSERT_SUCCESS(aws_s3_tester_bind_client(
6499 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
6500
6501 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
6502
6503 struct aws_string *host_name =
6504 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
6505
6506 /* Put together a simple S3 Get Object request. */
6507 struct aws_http_message *message =
6508 aws_s3_test_get_object_request_new(allocator, aws_byte_cursor_from_string(host_name), test_object_path);
6509
6510 struct aws_s3_meta_request_options options;
6511 AWS_ZERO_STRUCT(options);
6512 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
6513 options.message = message;
6514
6515 /* Trigger accelerating of our Get Object request. */
6516 struct aws_s3_meta_request_test_results meta_request_test_results;
6517 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6518
6519 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_results));
6520
6521 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &options);
6522
6523 ASSERT_TRUE(meta_request != NULL);
6524
6525 /* Wait for the request to finish. */
6526 aws_s3_tester_wait_for_meta_request_finish(&tester);
6527
6528 aws_s3_tester_lock_synced_data(&tester);
6529 ASSERT_TRUE(tester.synced_data.finish_error_code != AWS_ERROR_SUCCESS);
6530 aws_s3_tester_unlock_synced_data(&tester);
6531
6532 ASSERT_TRUE(meta_request_test_results.finished_response_status == 404);
6533 ASSERT_TRUE(meta_request_test_results.finished_error_code != AWS_ERROR_SUCCESS);
6534
6535 ASSERT_TRUE(meta_request_test_results.error_response_headers != NULL);
6536
6537 ASSERT_NOT_NULL(meta_request_test_results.error_response_operation_name);
6538 ASSERT_TRUE(
6539 aws_string_eq_c_str(meta_request_test_results.error_response_operation_name, "GetObject") ||
6540 aws_string_eq_c_str(meta_request_test_results.error_response_operation_name, "HeadObject"));
6541
6542 meta_request = aws_s3_meta_request_release(meta_request);
6543
6544 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
6545 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6546
6547 aws_http_message_release(message);
6548 message = NULL;
6549
6550 aws_string_destroy(host_name);
6551 host_name = NULL;
6552
6553 client = aws_s3_client_release(client);
6554
6555 aws_s3_tester_clean_up(&tester);
6556
6557 return 0;
6558 }
6559
6560 static void s_test_s3_existing_host_entry_address_resolved_callback(
6561 struct aws_host_resolver *resolver,
6562 const struct aws_string *host_name,
6563 int err_code,
6564 const struct aws_array_list *host_addresses,
6565 void *user_data) {
6566 (void)resolver;
6567 (void)host_name;
6568 (void)err_code;
6569 (void)host_addresses;
6570
6571 struct aws_s3_tester *tester = user_data;
6572 AWS_ASSERT(tester);
6573 aws_s3_tester_notify_signal(tester);
6574 }
6575
6576 AWS_TEST_CASE(test_s3_existing_host_entry, s_test_s3_existing_host_entry)
6577 static int s_test_s3_existing_host_entry(struct aws_allocator *allocator, void *ctx) {
6578 (void)ctx;
6579
6580 struct aws_s3_tester tester;
6581 AWS_ZERO_STRUCT(tester);
6582 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6583
6584 struct aws_s3_client_config client_config;
6585 AWS_ZERO_STRUCT(client_config);
6586
6587 ASSERT_SUCCESS(aws_s3_tester_bind_client(
6588 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
6589
6590 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
6591
6592 struct aws_string *host_name =
6593 aws_s3_tester_build_endpoint_string(allocator, &g_test_public_bucket_name, &g_test_s3_region);
6594
6595 {
6596 struct aws_host_resolution_config host_resolver_config;
6597 AWS_ZERO_STRUCT(host_resolver_config);
6598 host_resolver_config.impl = aws_default_dns_resolve;
6599 host_resolver_config.max_ttl = 30;
6600 host_resolver_config.impl_data = NULL;
6601
6602 ASSERT_SUCCESS(aws_host_resolver_resolve_host(
6603 client_config.client_bootstrap->host_resolver,
6604 host_name,
6605 s_test_s3_existing_host_entry_address_resolved_callback,
6606 &host_resolver_config,
6607 &tester));
6608
6609 aws_s3_tester_wait_for_signal(&tester);
6610 }
6611
6612 /* Put together a simple S3 Get Object request. */
6613 struct aws_http_message *message = aws_s3_test_get_object_request_new(
6614 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
6615
6616 struct aws_s3_meta_request_options options;
6617 AWS_ZERO_STRUCT(options);
6618 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
6619 options.message = message;
6620
6621 /* Trigger accelerating of our Get Object request. */
6622 struct aws_s3_meta_request_test_results meta_request_test_results;
6623 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6624
6625 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(
6626 &tester, client, &options, &meta_request_test_results, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS));
6627 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0));
6628
6629 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6630
6631 aws_http_message_release(message);
6632 aws_string_destroy(host_name);
6633 aws_s3_client_release(client);
6634 aws_s3_tester_clean_up(&tester);
6635
6636 return 0;
6637 }
6638
6639 AWS_TEST_CASE(test_s3_bad_endpoint, s_test_s3_bad_endpoint)
6640 static int s_test_s3_bad_endpoint(struct aws_allocator *allocator, void *ctx) {
6641 (void)ctx;
6642
6643 struct aws_s3_tester tester;
6644 AWS_ZERO_STRUCT(tester);
6645 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
6646
6647 struct aws_s3_client_config client_config;
6648 AWS_ZERO_STRUCT(client_config);
6649
6650 ASSERT_SUCCESS(aws_s3_tester_bind_client(
6651 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
6652
6653 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
6654
6655 struct aws_byte_cursor test_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test_key");
6656
6657 AWS_STATIC_STRING_FROM_LITERAL(invalid_host_name, "invalid_host_name_totally_absolutely");
6658
6659 /* Construct a message that points to an invalid host name. Key can be anything. */
6660 struct aws_http_message *message =
6661 aws_s3_test_get_object_request_new(allocator, aws_byte_cursor_from_string(invalid_host_name), test_key);
6662
6663 struct aws_s3_meta_request_options options;
6664 AWS_ZERO_STRUCT(options);
6665 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
6666 options.message = message;
6667
6668 struct aws_s3_meta_request_test_results meta_request_test_results;
6669 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6670
6671 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(&tester, client, &options, &meta_request_test_results, 0));
6672
6673 ASSERT_TRUE(
6674 meta_request_test_results.finished_error_code == AWS_IO_DNS_INVALID_NAME ||
6675 meta_request_test_results.finished_error_code == AWS_IO_DNS_QUERY_AGAIN ||
6676 meta_request_test_results.finished_error_code == AWS_IO_DNS_QUERY_FAILED);
6677
6678 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6679
6680 aws_http_message_release(message);
6681
6682 client = aws_s3_client_release(client);
6683
6684 aws_s3_tester_clean_up(&tester);
6685
6686 return 0;
6687 }
6688
6689 static int s_s3_test_headers_callback_raise_error(
6690 struct aws_s3_meta_request *meta_request,
6691 const struct aws_http_headers *headers,
6692 int response_status,
6693 void *user_data) {
6694 (void)meta_request;
6695 (void)headers;
6696 (void)response_status;
6697 (void)user_data;
6698 aws_raise_error(AWS_ERROR_UNKNOWN);
6699 return AWS_OP_ERR;
6700 }
6701
6702 static int s_s3_test_body_callback_raise_error(
6703 struct aws_s3_meta_request *meta_request,
6704 const struct aws_byte_cursor *body,
6705 uint64_t range_start,
6706 void *user_data) {
6707 (void)meta_request;
6708 (void)body;
6709 (void)range_start;
6710 (void)user_data;
6711 aws_raise_error(AWS_ERROR_UNKNOWN);
6712 return AWS_OP_ERR;
6713 }
6714
6715 AWS_TEST_CASE(test_s3_put_object_fail_headers_callback, s_test_s3_put_object_fail_headers_callback)
6716 static int s_test_s3_put_object_fail_headers_callback(struct aws_allocator *allocator, void *ctx) {
6717 (void)ctx;
6718
6719 struct aws_s3_meta_request_test_results meta_request_test_results;
6720 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6721
6722 struct aws_s3_tester_meta_request_options options = {
6723 .allocator = allocator,
6724 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
6725 .headers_callback = s_s3_test_headers_callback_raise_error,
6726 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6727 .put_options =
6728 {
6729 .ensure_multipart = true,
6730 },
6731 };
6732
6733 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6734 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_UNKNOWN);
6735
6736 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6737
6738 return 0;
6739 }
6740
6741 AWS_TEST_CASE(test_s3_put_object_fail_body_callback, s_test_s3_put_object_fail_body_callback)
6742 static int s_test_s3_put_object_fail_body_callback(struct aws_allocator *allocator, void *ctx) {
6743 (void)ctx;
6744
6745 struct aws_s3_tester_meta_request_options options = {
6746 .allocator = allocator,
6747 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
6748 .body_callback = s_s3_test_body_callback_raise_error,
6749
6750 /* Put object currently never invokes the body callback, which means it should not fail. */
6751 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
6752
6753 .put_options =
6754 {
6755 .ensure_multipart = true,
6756 },
6757 };
6758
6759 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, NULL));
6760
6761 return 0;
6762 }
6763
6764 AWS_TEST_CASE(test_s3_get_object_fail_headers_callback, s_test_s3_get_object_fail_headers_callback)
6765 static int s_test_s3_get_object_fail_headers_callback(struct aws_allocator *allocator, void *ctx) {
6766 (void)ctx;
6767
6768 struct aws_s3_meta_request_test_results meta_request_test_results;
6769 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6770
6771 struct aws_s3_tester_meta_request_options options = {
6772 .allocator = allocator,
6773 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6774 .headers_callback = s_s3_test_headers_callback_raise_error,
6775 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6776 .get_options =
6777 {
6778 .object_path = g_pre_existing_object_1MB,
6779 },
6780 };
6781
6782 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6783 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_UNKNOWN);
6784
6785 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6786
6787 return 0;
6788 }
6789
6790 AWS_TEST_CASE(test_s3_get_object_fail_body_callback, s_test_s3_get_object_fail_body_callback)
6791 static int s_test_s3_get_object_fail_body_callback(struct aws_allocator *allocator, void *ctx) {
6792 (void)ctx;
6793
6794 struct aws_s3_meta_request_test_results meta_request_test_results;
6795 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6796
6797 struct aws_s3_tester_meta_request_options options = {
6798 .allocator = allocator,
6799 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6800 .body_callback = s_s3_test_body_callback_raise_error,
6801 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6802 .get_options =
6803 {
6804 .object_path = g_pre_existing_object_1MB,
6805 },
6806 };
6807
6808 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6809 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_UNKNOWN);
6810
6811 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6812
6813 return 0;
6814 }
6815
6816 AWS_TEST_CASE(test_s3_default_fail_headers_callback, s_test_s3_default_fail_headers_callback)
6817 static int s_test_s3_default_fail_headers_callback(struct aws_allocator *allocator, void *ctx) {
6818 (void)ctx;
6819
6820 struct aws_s3_meta_request_test_results meta_request_test_results;
6821 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6822
6823 struct aws_s3_tester_meta_request_options options = {
6824 .allocator = allocator,
6825 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
6826 .headers_callback = s_s3_test_headers_callback_raise_error,
6827 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6828 .default_type_options =
6829 {
6830 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
6831 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
6832 },
6833 .get_options =
6834 {
6835 .object_path = g_pre_existing_object_1MB,
6836 },
6837 };
6838
6839 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6840 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_UNKNOWN);
6841
6842 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6843
6844 return 0;
6845 }
6846
6847 static struct aws_atomic_var s_test_headers_callback_invoked;
6848
6849 static int s_s3_test_headers_callback_check_returns_success(
6850 struct aws_s3_meta_request *meta_request,
6851 const struct aws_http_headers *headers,
6852 int response_status,
6853 void *user_data) {
6854 (void)meta_request;
6855 (void)headers;
6856 (void)response_status;
6857 (void)user_data;
6858
6859 /* increments counter to check if callback was invoked exactly once */
6860 aws_atomic_fetch_add(&s_test_headers_callback_invoked, 1);
6861
6862 return AWS_OP_SUCCESS;
6863 }
6864
6865 static int s_s3_test_headers_callback_check_returns_error(
6866 struct aws_s3_meta_request *meta_request,
6867 const struct aws_http_headers *headers,
6868 int response_status,
6869 void *user_data) {
6870 (void)meta_request;
6871 (void)headers;
6872 (void)response_status;
6873 (void)user_data;
6874
6875 /* increments counter to check if callback was invoked exactly once */
6876 aws_atomic_fetch_add(&s_test_headers_callback_invoked, 1);
6877
6878 aws_raise_error(AWS_ERROR_UNKNOWN);
6879 return AWS_OP_ERR;
6880 }
6881
6882 AWS_TEST_CASE(test_s3_default_invoke_headers_callback_on_error, s_test_s3_default_invoke_headers_callback_on_error)
6883 static int s_test_s3_default_invoke_headers_callback_on_error(struct aws_allocator *allocator, void *ctx) {
6884 (void)ctx;
6885
6886 struct aws_s3_meta_request_test_results meta_request_test_results;
6887 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6888 aws_atomic_init_int(&s_test_headers_callback_invoked, 0);
6889
6890 struct aws_byte_cursor invalid_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("___INVALID_PATH___");
6891
6892 struct aws_s3_tester_meta_request_options options = {
6893 .allocator = allocator,
6894 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
6895 .headers_callback = s_s3_test_headers_callback_check_returns_success,
6896
6897 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6898 .default_type_options =
6899 {
6900 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
6901 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
6902 },
6903 .get_options =
6904 {
6905 .object_path = invalid_path,
6906 },
6907 };
6908
6909 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6910 ASSERT_INT_EQUALS(1, aws_atomic_load_int(&s_test_headers_callback_invoked));
6911 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_S3_INVALID_RESPONSE_STATUS);
6912
6913 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6914
6915 return 0;
6916 }
6917
6918 AWS_TEST_CASE(
6919 test_s3_default_invoke_headers_callback_cancels_on_error,
6920 s_test_s3_default_invoke_headers_callback_cancels_on_error)
6921 static int s_test_s3_default_invoke_headers_callback_cancels_on_error(struct aws_allocator *allocator, void *ctx) {
6922 (void)ctx;
6923
6924 struct aws_s3_meta_request_test_results meta_request_test_results;
6925 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6926 aws_atomic_init_int(&s_test_headers_callback_invoked, 0);
6927
6928 struct aws_byte_cursor invalid_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("___INVALID_PATH___");
6929
6930 struct aws_s3_tester_meta_request_options options = {
6931 .allocator = allocator,
6932 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
6933 .headers_callback = s_s3_test_headers_callback_check_returns_error,
6934 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6935 .default_type_options =
6936 {
6937 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
6938 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
6939 },
6940 .get_options =
6941 {
6942 .object_path = invalid_path,
6943 },
6944 };
6945
6946 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6947 ASSERT_INT_EQUALS(1, aws_atomic_load_int(&s_test_headers_callback_invoked));
6948 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_UNKNOWN);
6949
6950 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6951
6952 return 0;
6953 }
6954
6955 AWS_TEST_CASE(
6956 test_s3_get_object_invoke_headers_callback_on_error,
6957 s_test_s3_get_object_invoke_headers_callback_on_error)
6958 static int s_test_s3_get_object_invoke_headers_callback_on_error(struct aws_allocator *allocator, void *ctx) {
6959 (void)ctx;
6960
6961 struct aws_s3_meta_request_test_results meta_request_test_results;
6962 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6963 aws_atomic_init_int(&s_test_headers_callback_invoked, 0);
6964
6965 struct aws_byte_cursor invalid_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("___INVALID_PATH___");
6966
6967 struct aws_s3_tester_meta_request_options options = {
6968 .allocator = allocator,
6969 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
6970 .headers_callback = s_s3_test_headers_callback_check_returns_success,
6971 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
6972 .get_options =
6973 {
6974 .object_path = invalid_path,
6975 },
6976 };
6977
6978 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
6979 ASSERT_INT_EQUALS(1, aws_atomic_load_int(&s_test_headers_callback_invoked));
6980 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_S3_INVALID_RESPONSE_STATUS);
6981
6982 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
6983
6984 return 0;
6985 }
6986
6987 AWS_TEST_CASE(
6988 test_s3_put_object_invoke_headers_callback_on_error,
6989 s_test_s3_put_object_invoke_headers_callback_on_error)
6990 static int s_test_s3_put_object_invoke_headers_callback_on_error(struct aws_allocator *allocator, void *ctx) {
6991 (void)ctx;
6992
6993 struct aws_s3_meta_request_test_results meta_request_test_results;
6994 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
6995 aws_atomic_init_int(&s_test_headers_callback_invoked, 0);
6996
6997 struct aws_s3_tester_meta_request_options options = {
6998 .allocator = allocator,
6999 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7000 .headers_callback = s_s3_test_headers_callback_check_returns_success,
7001 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7002 .put_options =
7003 {
7004 .object_size_mb = 10,
7005 .invalid_request = true,
7006 },
7007 };
7008
7009 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7010 ASSERT_INT_EQUALS(1, aws_atomic_load_int(&s_test_headers_callback_invoked));
7011 ASSERT_UINT_EQUALS(AWS_ERROR_S3_INVALID_RESPONSE_STATUS, meta_request_test_results.finished_error_code);
7012
7013 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7014
7015 return 0;
7016 }
7017
7018 AWS_TEST_CASE(
7019 test_s3_put_object_invoke_headers_callback_on_error_with_user_cancellation,
7020 s_test_s3_put_object_invoke_headers_callback_on_error_with_user_cancellation)
7021 static int s_test_s3_put_object_invoke_headers_callback_on_error_with_user_cancellation(
7022 struct aws_allocator *allocator,
7023 void *ctx) {
7024 (void)ctx;
7025
7026 struct aws_s3_meta_request_test_results meta_request_test_results;
7027 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7028 aws_atomic_init_int(&s_test_headers_callback_invoked, 0);
7029
7030 struct aws_s3_tester_meta_request_options options = {
7031 .allocator = allocator,
7032 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7033 .headers_callback = s_s3_test_headers_callback_check_returns_error,
7034 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7035 .put_options =
7036 {
7037 .ensure_multipart = true,
7038 },
7039 };
7040
7041 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7042 ASSERT_INT_EQUALS(1, aws_atomic_load_int(&s_test_headers_callback_invoked));
7043 ASSERT_UINT_EQUALS(AWS_ERROR_UNKNOWN, meta_request_test_results.finished_error_code);
7044
7045 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7046
7047 return 0;
7048 }
7049
7050 AWS_TEST_CASE(test_s3_default_fail_body_callback, s_test_s3_default_fail_body_callback)
7051 static int s_test_s3_default_fail_body_callback(struct aws_allocator *allocator, void *ctx) {
7052 (void)ctx;
7053
7054 struct aws_s3_meta_request_test_results meta_request_test_results;
7055 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7056
7057 struct aws_s3_tester_meta_request_options options = {
7058 .allocator = allocator,
7059 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7060 .body_callback = s_s3_test_body_callback_raise_error,
7061 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7062 .default_type_options =
7063 {
7064 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
7065 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
7066 },
7067 .get_options =
7068 {
7069 .object_path = g_pre_existing_object_1MB,
7070 },
7071 };
7072
7073 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7074 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_UNKNOWN);
7075
7076 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7077
7078 return 0;
7079 }
7080
7081 /* Test that if a DEFAULt meta-request sets the operation_name, and gets an error response,
7082 * then aws_s3_meta_request_result.error_response_operation_name is set. */
7083 AWS_TEST_CASE(test_s3_default_fail_operation_name, s_test_s3_default_fail_operation_name)
7084 static int s_test_s3_default_fail_operation_name(struct aws_allocator *allocator, void *ctx) {
7085 (void)ctx;
7086
7087 struct aws_s3_meta_request_test_results meta_request_test_results;
7088 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7089
7090 struct aws_byte_cursor invalid_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("___INVALID_PATH___");
7091
7092 struct aws_s3_tester_meta_request_options options = {
7093 .allocator = allocator,
7094 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7095
7096 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7097 .default_type_options =
7098 {
7099 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
7100 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
7101 },
7102 .get_options =
7103 {
7104 .object_path = invalid_path,
7105 },
7106 };
7107
7108 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7109 ASSERT_TRUE(meta_request_test_results.finished_error_code == AWS_ERROR_S3_INVALID_RESPONSE_STATUS);
7110 ASSERT_STR_EQUALS("GetObject", aws_string_c_str(meta_request_test_results.error_response_operation_name));
7111
7112 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7113
7114 return 0;
7115 }
7116
7117 AWS_TEST_CASE(test_s3_put_fail_object_invalid_request, s_test_s3_put_fail_object_invalid_request)
7118 static int s_test_s3_put_fail_object_invalid_request(struct aws_allocator *allocator, void *ctx) {
7119 (void)ctx;
7120
7121 struct aws_s3_meta_request_test_results meta_request_test_results;
7122 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7123
7124 struct aws_s3_tester_meta_request_options options = {
7125 .allocator = allocator,
7126 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7127 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7128 .put_options =
7129 {
7130 .object_size_mb = 1,
7131 .invalid_request = true,
7132 },
7133 };
7134
7135 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7136 ASSERT_UINT_EQUALS(AWS_ERROR_S3_INVALID_RESPONSE_STATUS, meta_request_test_results.finished_error_code);
7137
7138 /* Since 1MB is under part_size, there will be a single PutObject request */
7139 ASSERT_STR_EQUALS("PutObject", aws_string_c_str(meta_request_test_results.error_response_operation_name));
7140
7141 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7142
7143 return 0;
7144 }
7145
7146 /* Test that we fail to create a metarequest when an invalid `send_filepath` is passed in */
7147 AWS_TEST_CASE(test_s3_put_fail_object_invalid_send_filepath, s_test_s3_put_fail_object_invalid_send_filepath)
7148 static int s_test_s3_put_fail_object_invalid_send_filepath(struct aws_allocator *allocator, void *ctx) {
7149 (void)ctx;
7150
7151 struct aws_s3_tester tester;
7152 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7153
7154 struct aws_s3_tester_client_options client_options;
7155 AWS_ZERO_STRUCT(client_options);
7156 struct aws_s3_client *client = NULL;
7157 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
7158
7159 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7160 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7161
7162 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
7163 allocator, &host_name, g_test_body_content_type, object_key, 1024 /*content_length*/, 0 /*flags*/);
7164 ASSERT_NOT_NULL(message);
7165
7166 struct aws_s3_meta_request_options meta_request_options = {
7167 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7168 .message = message,
7169 .send_filepath = aws_byte_cursor_from_c_str("obviously_invalid_file_path"),
7170 };
7171 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &meta_request_options);
7172 ASSERT_NULL(meta_request);
7173 ASSERT_INT_EQUALS(AWS_ERROR_FILE_INVALID_PATH, aws_last_error());
7174
7175 aws_http_message_release(message);
7176 aws_s3_client_release(client);
7177 aws_s3_tester_clean_up(&tester);
7178 return 0;
7179 }
7180
7181 static int s_assert_make_meta_request_fails(
7182 struct aws_allocator *allocator,
7183 struct aws_s3_meta_request_options *options,
7184 int expected_error) {
7185
7186 struct aws_s3_tester tester;
7187 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7188
7189 struct aws_s3_tester_client_options client_options;
7190 AWS_ZERO_STRUCT(client_options);
7191 struct aws_s3_client *client = NULL;
7192 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
7193
7194 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, options);
7195 ASSERT_NULL(meta_request);
7196 ASSERT_INT_EQUALS(expected_error, aws_last_error());
7197
7198 aws_s3_client_release(client);
7199 aws_s3_tester_clean_up(&tester);
7200 return 0;
7201 }
7202
7203 AWS_TEST_CASE(test_s3_request_body_fail_null_ptr, s_test_s3_request_body_fail_null_ptr)
7204 static int s_test_s3_request_body_fail_null_ptr(struct aws_allocator *allocator, void *ctx) {
7205 (void)ctx;
7206
7207 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7208 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7209
7210 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
7211 allocator, &host_name, g_test_body_content_type, object_key, 1024 /*content_length*/, 0 /*flags*/);
7212 ASSERT_NOT_NULL(message);
7213
7214 struct aws_s3_meta_request_options meta_request_options = {
7215 .type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7216 .operation_name = aws_byte_cursor_from_c_str("PutObject"),
7217 .message = message,
7218 .request_body = {.ptr = NULL, .len = 1024},
7219 };
7220 ASSERT_SUCCESS(s_assert_make_meta_request_fails(allocator, &meta_request_options, AWS_ERROR_INVALID_ARGUMENT));
7221
7222 aws_http_message_release(message);
7223 return 0;
7224 }
7225
7226 AWS_TEST_CASE(test_s3_request_body_fail_non_default_type, s_test_s3_request_body_fail_non_default_type)
7227 static int s_test_s3_request_body_fail_non_default_type(struct aws_allocator *allocator, void *ctx) {
7228 (void)ctx;
7229
7230 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7231 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7232 struct aws_byte_cursor body = aws_byte_cursor_from_c_str("hello world");
7233
7234 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
7235 allocator, &host_name, g_test_body_content_type, object_key, body.len, 0 /*flags*/);
7236 ASSERT_NOT_NULL(message);
7237
7238 struct aws_s3_meta_request_options meta_request_options = {
7239 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7240 .message = message,
7241 .request_body = body,
7242 };
7243 ASSERT_SUCCESS(s_assert_make_meta_request_fails(allocator, &meta_request_options, AWS_ERROR_INVALID_ARGUMENT));
7244
7245 aws_http_message_release(message);
7246 return 0;
7247 }
7248
7249 AWS_TEST_CASE(test_s3_request_body_fail_content_length_mismatch, s_test_s3_request_body_fail_content_length_mismatch)
7250 static int s_test_s3_request_body_fail_content_length_mismatch(struct aws_allocator *allocator, void *ctx) {
7251 (void)ctx;
7252
7253 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7254 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7255 struct aws_byte_cursor body = aws_byte_cursor_from_c_str("hello world");
7256
7257 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
7258 allocator, &host_name, g_test_body_content_type, object_key, body.len + 1 /*content_length*/, 0 /*flags*/);
7259 ASSERT_NOT_NULL(message);
7260
7261 struct aws_s3_meta_request_options meta_request_options = {
7262 .type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7263 .operation_name = aws_byte_cursor_from_c_str("PutObject"),
7264 .message = message,
7265 .request_body = body,
7266 };
7267 ASSERT_SUCCESS(
7268 s_assert_make_meta_request_fails(allocator, &meta_request_options, AWS_ERROR_S3_INCORRECT_CONTENT_LENGTH));
7269
7270 aws_http_message_release(message);
7271 return 0;
7272 }
7273
7274 AWS_TEST_CASE(test_s3_request_body_fail_with_send_filepath, s_test_s3_request_body_fail_with_send_filepath)
7275 static int s_test_s3_request_body_fail_with_send_filepath(struct aws_allocator *allocator, void *ctx) {
7276 (void)ctx;
7277
7278 /* Write a small file to disk so send_filepath alone would be valid. */
7279 struct aws_byte_cursor file_contents = aws_byte_cursor_from_c_str("hello world");
7280 struct aws_input_stream *file_stream = aws_input_stream_new_from_cursor(allocator, &file_contents);
7281 ASSERT_NOT_NULL(file_stream);
7282 struct aws_string *filepath_str =
7283 aws_s3_tester_create_file(allocator, aws_byte_cursor_from_c_str("request_body_conflict"), file_stream);
7284 ASSERT_NOT_NULL(filepath_str);
7285
7286 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7287 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7288
7289 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
7290 allocator, &host_name, g_test_body_content_type, object_key, file_contents.len, 0 /*flags*/);
7291 ASSERT_NOT_NULL(message);
7292
7293 struct aws_s3_meta_request_options meta_request_options = {
7294 .type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7295 .operation_name = aws_byte_cursor_from_c_str("PutObject"),
7296 .message = message,
7297 .send_filepath = aws_byte_cursor_from_string(filepath_str),
7298 .request_body = file_contents,
7299 };
7300 ASSERT_SUCCESS(s_assert_make_meta_request_fails(allocator, &meta_request_options, AWS_ERROR_INVALID_ARGUMENT));
7301
7302 aws_http_message_release(message);
7303 aws_input_stream_release(file_stream);
7304 aws_file_delete(filepath_str);
7305 aws_string_destroy(filepath_str);
7306 return 0;
7307 }
7308
7309 /* Test that a zero-length `request_body` with a non-NULL pointer (a caller opting in to an empty body) is still
7310 * validated: it must be rejected on a non-DEFAULT meta request type, just like a non-empty request_body. */
7311 AWS_TEST_CASE(
7312 test_s3_request_body_zero_length_fail_non_default_type,
7313 s_test_s3_request_body_zero_length_fail_non_default_type)
7314 static int s_test_s3_request_body_zero_length_fail_non_default_type(struct aws_allocator *allocator, void *ctx) {
7315 (void)ctx;
7316
7317 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7318 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7319
7320 /* Use a GET meta request: it needs no body source, so the only thing that can reject this is the
7321 * request_body DEFAULT-only validation. */
7322 struct aws_http_message *message = aws_s3_test_get_object_request_new(allocator, host_name, object_key);
7323 ASSERT_NOT_NULL(message);
7324
7325 uint8_t body_storage[1];
7326 struct aws_byte_cursor empty_body = {.ptr = body_storage, .len = 0};
7327
7328 struct aws_s3_meta_request_options meta_request_options = {
7329 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
7330 .message = message,
7331 .request_body = empty_body,
7332 };
7333 ASSERT_SUCCESS(s_assert_make_meta_request_fails(allocator, &meta_request_options, AWS_ERROR_INVALID_ARGUMENT));
7334
7335 aws_http_message_release(message);
7336 return 0;
7337 }
7338
7339 /* Test that a zero-length `request_body` with a non-NULL pointer counts as a body source, so combining it with
7340 * another body source (send_filepath) is rejected. */
7341 AWS_TEST_CASE(
7342 test_s3_request_body_zero_length_fail_with_send_filepath,
7343 s_test_s3_request_body_zero_length_fail_with_send_filepath)
7344 static int s_test_s3_request_body_zero_length_fail_with_send_filepath(struct aws_allocator *allocator, void *ctx) {
7345 (void)ctx;
7346
7347 /* Write a small file to disk so send_filepath alone would be valid. */
7348 struct aws_byte_cursor file_contents = aws_byte_cursor_from_c_str("hello world");
7349 struct aws_input_stream *file_stream = aws_input_stream_new_from_cursor(allocator, &file_contents);
7350 ASSERT_NOT_NULL(file_stream);
7351 struct aws_string *filepath_str =
7352 aws_s3_tester_create_file(allocator, aws_byte_cursor_from_c_str("request_body_zero_len_conflict"), file_stream);
7353 ASSERT_NOT_NULL(filepath_str);
7354
7355 struct aws_byte_cursor host_name = aws_byte_cursor_from_c_str("dummy_host");
7356 struct aws_byte_cursor object_key = aws_byte_cursor_from_c_str("dummy_key");
7357
7358 /* Content-Length 0 matches the zero-length request_body, so the only error is the dual body source (not a
7359 * content-length mismatch). */
7360 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
7361 allocator, &host_name, g_test_body_content_type, object_key, 0 /*content_length*/, 0 /*flags*/);
7362 ASSERT_NOT_NULL(message);
7363
7364 /* Non-NULL pointer, zero length: still a body source, so this conflicts with send_filepath. */
7365 uint8_t body_storage[1];
7366 struct aws_byte_cursor empty_body = {.ptr = body_storage, .len = 0};
7367
7368 struct aws_s3_meta_request_options meta_request_options = {
7369 .type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7370 .operation_name = aws_byte_cursor_from_c_str("PutObject"),
7371 .message = message,
7372 .send_filepath = aws_byte_cursor_from_string(filepath_str),
7373 .request_body = empty_body,
7374 };
7375 ASSERT_SUCCESS(s_assert_make_meta_request_fails(allocator, &meta_request_options, AWS_ERROR_INVALID_ARGUMENT));
7376
7377 aws_http_message_release(message);
7378 aws_input_stream_release(file_stream);
7379 aws_file_delete(filepath_str);
7380 aws_string_destroy(filepath_str);
7381 return 0;
7382 }
7383
7384 /* Test that the parallel read stream failed to send read the second part. */
7385 AWS_TEST_CASE(test_s3_put_fail_object_bad_parallel_read_stream, s_test_s3_put_fail_object_bad_parallel_read_stream)
7386 static int s_test_s3_put_fail_object_bad_parallel_read_stream(struct aws_allocator *allocator, void *ctx) {
7387 (void)ctx;
7388
7389 struct aws_s3_tester tester;
7390 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7391
7392 struct aws_s3_tester_client_options client_options;
7393 AWS_ZERO_STRUCT(client_options);
7394 struct aws_s3_client *client = NULL;
7395 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
7396 /* Override the parallel input stream new function to create a bad parallel input stream */
7397 client->vtable->parallel_input_stream_new_from_file = aws_parallel_input_stream_new_from_file_failure_tester;
7398
7399 struct aws_s3_meta_request_test_results meta_request_test_results;
7400 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7401
7402 struct aws_s3_tester_meta_request_options options = {
7403 .allocator = allocator,
7404 .client = client,
7405 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7406 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7407 .put_options =
7408 {
7409 .object_size_mb = 100,
7410 .file_on_disk = true,
7411 },
7412 };
7413
7414 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &meta_request_test_results));
7415
7416 ASSERT_UINT_EQUALS(AWS_ERROR_UNIMPLEMENTED, meta_request_test_results.finished_error_code);
7417
7418 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7419 client = aws_s3_client_release(client);
7420
7421 aws_s3_tester_clean_up(&tester);
7422
7423 return AWS_OP_SUCCESS;
7424 }
7425
7426 AWS_TEST_CASE(
7427 test_s3_put_single_part_fail_object_inputstream_fail_reading,
7428 s_test_s3_put_single_part_fail_object_inputstream_fail_reading)
7429 static int s_test_s3_put_single_part_fail_object_inputstream_fail_reading(struct aws_allocator *allocator, void *ctx) {
7430 (void)ctx;
7431
7432 struct aws_s3_meta_request_test_results meta_request_test_results;
7433 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7434
7435 struct aws_s3_tester_meta_request_options options = {
7436 .allocator = allocator,
7437 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7438 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7439 .put_options =
7440 {
7441 .invalid_input_stream = true,
7442 .content_length = 10,
7443 },
7444 };
7445
7446 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7447
7448 ASSERT_TRUE(meta_request_test_results.finished_error_code != AWS_ERROR_SUCCESS);
7449
7450 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7451
7452 return 0;
7453 }
7454
7455 AWS_TEST_CASE(
7456 test_s3_put_single_part_fail_object_inputstream_mismatch_content_length,
7457 s_test_s3_put_single_part_fail_object_inputstream_mismatch_content_length)
7458 static int s_test_s3_put_single_part_fail_object_inputstream_mismatch_content_length(
7459 struct aws_allocator *allocator,
7460 void *ctx) {
7461 (void)ctx;
7462
7463 struct aws_s3_meta_request_test_results meta_request_test_results;
7464 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7465
7466 struct aws_s3_tester_meta_request_options options = {
7467 .allocator = allocator,
7468 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7469 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7470 .put_options =
7471 {
7472 .object_size_mb = 1,
7473 .content_length = MB_TO_BYTES(2),
7474 },
7475 };
7476
7477 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7478
7479 ASSERT_TRUE(meta_request_test_results.finished_error_code != AWS_ERROR_SUCCESS);
7480
7481 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7482
7483 return 0;
7484 }
7485
7486 AWS_TEST_CASE(test_s3_put_fail_object_inputstream_fail_reading, s_test_s3_put_fail_object_inputstream_fail_reading)
7487 static int s_test_s3_put_fail_object_inputstream_fail_reading(struct aws_allocator *allocator, void *ctx) {
7488 (void)ctx;
7489
7490 struct aws_s3_meta_request_test_results meta_request_test_results;
7491 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7492
7493 struct aws_s3_tester_meta_request_options options = {
7494 .allocator = allocator,
7495 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7496 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7497 .put_options =
7498 {
7499 .ensure_multipart = true,
7500 .invalid_input_stream = true,
7501 .content_length = 10 * 1024 * 1024,
7502 },
7503 };
7504
7505 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7506
7507 ASSERT_UINT_EQUALS(AWS_IO_STREAM_READ_FAILED, meta_request_test_results.finished_error_code);
7508
7509 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7510
7511 return 0;
7512 }
7513
7514 AWS_TEST_CASE(
7515 test_s3_put_fail_object_inputstream_mismatch_content_length,
7516 s_test_s3_put_fail_object_inputstream_mismatch_content_length)
7517 static int s_test_s3_put_fail_object_inputstream_mismatch_content_length(struct aws_allocator *allocator, void *ctx) {
7518 (void)ctx;
7519
7520 struct aws_s3_meta_request_test_results meta_request_test_results;
7521 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
7522
7523 struct aws_s3_tester_meta_request_options options = {
7524 .allocator = allocator,
7525 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7526 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
7527 .put_options =
7528 {
7529 .ensure_multipart = false,
7530 .object_size_mb = 1,
7531 .content_length = 10 * 1024 * 1024,
7532 },
7533 };
7534
7535 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &options, &meta_request_test_results));
7536
7537 ASSERT_UINT_EQUALS(AWS_ERROR_S3_INCORRECT_CONTENT_LENGTH, meta_request_test_results.finished_error_code);
7538
7539 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
7540
7541 return 0;
7542 }
7543
7544 AWS_TEST_CASE(test_s3_put_object_clamp_part_size, s_test_s3_put_object_clamp_part_size)
7545 static int s_test_s3_put_object_clamp_part_size(struct aws_allocator *allocator, void *ctx) {
7546 (void)ctx;
7547
7548 struct aws_s3_tester tester;
7549 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7550
7551 struct aws_s3_client_config client_config = {
7552 .part_size = 64 * 1024,
7553 .max_part_size = 64 * 1024,
7554 };
7555
7556 ASSERT_TRUE(client_config.part_size < g_s3_min_upload_part_size);
7557 ASSERT_TRUE(client_config.max_part_size < g_s3_min_upload_part_size);
7558
7559 ASSERT_SUCCESS(aws_s3_tester_bind_client(
7560 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
7561
7562 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
7563
7564 ASSERT_TRUE(client != NULL);
7565
7566 struct aws_s3_meta_request_test_results test_results;
7567 aws_s3_meta_request_test_results_init(&test_results, allocator);
7568
7569 /* Upload should now succeed even when specifying a smaller than allowed part size. */
7570
7571 struct aws_s3_tester_meta_request_options put_options = {
7572 .allocator = allocator,
7573 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7574 .client = client,
7575 .put_options =
7576 {
7577 .object_size_mb = 10,
7578 },
7579 };
7580
7581 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
7582
7583 ASSERT_TRUE(test_results.part_size == g_s3_min_upload_part_size);
7584
7585 aws_s3_meta_request_test_results_clean_up(&test_results);
7586
7587 client = aws_s3_client_release(client);
7588
7589 aws_s3_tester_clean_up(&tester);
7590
7591 return 0;
7592 }
7593
7594 static int s_get_expected_user_agent(struct aws_allocator *allocator, struct aws_byte_buf *dest) {
7595 AWS_ASSERT(allocator);
7596 AWS_ASSERT(dest);
7597
7598 const struct aws_byte_cursor forward_slash = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/");
7599 const struct aws_byte_cursor single_space = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" ");
7600
7601 ASSERT_SUCCESS(aws_byte_buf_init(dest, allocator, 32));
7602 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &g_user_agent_header_product_name));
7603 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &forward_slash));
7604 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &g_s3_client_version));
7605 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &single_space));
7606 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &g_user_agent_header_platform));
7607 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &forward_slash));
7608 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(dest, &g_user_agent_header_unknown));
7609 return AWS_OP_SUCCESS;
7610 }
7611
7612 AWS_TEST_CASE(test_add_user_agent_header, s_test_add_user_agent_header)
7613 static int s_test_add_user_agent_header(struct aws_allocator *allocator, void *ctx) {
7614 (void)ctx;
7615
7616 struct aws_s3_tester tester;
7617 AWS_ZERO_STRUCT(tester);
7618 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7619
7620 const struct aws_byte_cursor single_space = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" ");
7621
7622 struct aws_byte_buf expected_user_agent_value_buf;
7623 s_get_expected_user_agent(allocator, &expected_user_agent_value_buf);
7624
7625 struct aws_byte_cursor expected_user_agent_value = aws_byte_cursor_from_buf(&expected_user_agent_value_buf);
7626
7627 {
7628 struct aws_byte_cursor user_agent_value;
7629 AWS_ZERO_STRUCT(user_agent_value);
7630
7631 struct aws_http_message *message = aws_http_message_new_request(allocator);
7632
7633 aws_s3_add_user_agent_header(allocator, message);
7634
7635 struct aws_http_headers *headers = aws_http_message_get_headers(message);
7636
7637 ASSERT_TRUE(headers != NULL);
7638 ASSERT_SUCCESS(aws_http_headers_get(headers, g_user_agent_header_name, &user_agent_value));
7639 ASSERT_BIN_ARRAYS_EQUALS(
7640 user_agent_value.ptr, user_agent_value.len, expected_user_agent_value.ptr, expected_user_agent_value.len);
7641 aws_http_message_release(message);
7642 }
7643
7644 {
7645 const struct aws_byte_cursor dummy_agent_header_value =
7646 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("dummy_user_agent_product/dummy_user_agent_value");
7647
7648 struct aws_byte_buf total_expected_user_agent_value_buf;
7649 aws_byte_buf_init(&total_expected_user_agent_value_buf, allocator, 64);
7650 aws_byte_buf_append_dynamic(&total_expected_user_agent_value_buf, &dummy_agent_header_value);
7651 aws_byte_buf_append_dynamic(&total_expected_user_agent_value_buf, &single_space);
7652 aws_byte_buf_append_dynamic(&total_expected_user_agent_value_buf, &expected_user_agent_value);
7653
7654 struct aws_byte_cursor total_expected_user_agent_value =
7655 aws_byte_cursor_from_buf(&total_expected_user_agent_value_buf);
7656
7657 struct aws_http_message *message = aws_http_message_new_request(allocator);
7658 struct aws_http_headers *headers = aws_http_message_get_headers(message);
7659 ASSERT_TRUE(headers != NULL);
7660
7661 ASSERT_SUCCESS(aws_http_headers_add(headers, g_user_agent_header_name, dummy_agent_header_value));
7662
7663 aws_s3_add_user_agent_header(allocator, message);
7664
7665 {
7666 struct aws_byte_cursor user_agent_value;
7667 AWS_ZERO_STRUCT(user_agent_value);
7668 ASSERT_SUCCESS(aws_http_headers_get(headers, g_user_agent_header_name, &user_agent_value));
7669 ASSERT_BIN_ARRAYS_EQUALS(
7670 user_agent_value.ptr,
7671 user_agent_value.len,
7672 total_expected_user_agent_value.ptr,
7673 total_expected_user_agent_value.len);
7674 }
7675
7676 aws_byte_buf_clean_up(&total_expected_user_agent_value_buf);
7677 aws_http_message_release(message);
7678 }
7679
7680 aws_byte_buf_clean_up(&expected_user_agent_value_buf);
7681 aws_s3_tester_clean_up(&tester);
7682
7683 return 0;
7684 }
7685
7686 static void s_s3_test_user_agent_meta_request_finished_request(
7687 struct aws_s3_meta_request *meta_request,
7688 struct aws_s3_request *request,
7689 int error_code) {
7690 AWS_ASSERT(meta_request != NULL);
7691
7692 struct aws_s3_meta_request_test_results *results = meta_request->user_data;
7693 AWS_ASSERT(results != NULL);
7694
7695 struct aws_s3_tester *tester = results->tester;
7696 AWS_ASSERT(tester != NULL);
7697
7698 struct aws_byte_buf expected_user_agent_value_buf;
7699 s_get_expected_user_agent(meta_request->allocator, &expected_user_agent_value_buf);
7700
7701 struct aws_byte_cursor expected_user_agent_value = aws_byte_cursor_from_buf(&expected_user_agent_value_buf);
7702
7703 struct aws_http_message *message = request->send_data.message;
7704 struct aws_http_headers *headers = aws_http_message_get_headers(message);
7705
7706 struct aws_byte_cursor user_agent_value;
7707 AWS_ZERO_STRUCT(user_agent_value);
7708
7709 AWS_FATAL_ASSERT(aws_http_headers_get(headers, g_user_agent_header_name, &user_agent_value) == AWS_OP_SUCCESS);
7710 AWS_FATAL_ASSERT(aws_byte_cursor_eq(&user_agent_value, &expected_user_agent_value));
7711 aws_byte_buf_clean_up(&expected_user_agent_value_buf);
7712
7713 struct aws_s3_meta_request_vtable *original_meta_request_vtable =
7714 aws_s3_tester_get_meta_request_vtable_patch(tester, 0)->original_vtable;
7715
7716 original_meta_request_vtable->finished_request(meta_request, request, error_code);
7717 }
7718
7719 static struct aws_s3_meta_request *s_s3_meta_request_factory_override_finished_request(
7720 struct aws_s3_client *client,
7721 const struct aws_s3_meta_request_options *options) {
7722 AWS_ASSERT(client != NULL);
7723
7724 struct aws_s3_tester *tester = client->shutdown_callback_user_data;
7725 AWS_ASSERT(tester != NULL);
7726
7727 struct aws_s3_client_vtable *original_client_vtable =
7728 aws_s3_tester_get_client_vtable_patch(tester, 0)->original_vtable;
7729
7730 struct aws_s3_meta_request *meta_request = original_client_vtable->meta_request_factory(client, options);
7731
7732 struct aws_s3_meta_request_vtable *patched_meta_request_vtable =
7733 aws_s3_tester_patch_meta_request_vtable(tester, meta_request, NULL);
7734 patched_meta_request_vtable->finished_request = s_s3_test_user_agent_meta_request_finished_request;
7735
7736 return meta_request;
7737 }
7738
7739 int s_s3_test_sending_user_agent_create_client(struct aws_s3_tester *tester, struct aws_s3_client **client) {
7740 AWS_ASSERT(tester);
7741
7742 struct aws_s3_tester_client_options client_options;
7743 AWS_ZERO_STRUCT(client_options);
7744
7745 ASSERT_SUCCESS(aws_s3_tester_client_new(tester, &client_options, client));
7746
7747 struct aws_s3_client_vtable *patched_client_vtable = aws_s3_tester_patch_client_vtable(tester, *client, NULL);
7748 patched_client_vtable->meta_request_factory = s_s3_meta_request_factory_override_finished_request;
7749
7750 return AWS_OP_SUCCESS;
7751 }
7752
7753 AWS_TEST_CASE(test_s3_auto_ranged_get_sending_user_agent, s_test_s3_auto_ranged_get_sending_user_agent)
7754 static int s_test_s3_auto_ranged_get_sending_user_agent(struct aws_allocator *allocator, void *ctx) {
7755 (void)ctx;
7756
7757 struct aws_s3_tester tester;
7758 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7759
7760 struct aws_s3_client *client = NULL;
7761 ASSERT_SUCCESS(s_s3_test_sending_user_agent_create_client(&tester, &client));
7762
7763 {
7764 struct aws_s3_tester_meta_request_options options = {
7765 .allocator = allocator,
7766 .client = client,
7767 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
7768 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
7769 .get_options =
7770 {
7771 .object_path = g_pre_existing_object_1MB,
7772 },
7773 };
7774
7775 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, NULL));
7776 }
7777
7778 aws_s3_client_release(client);
7779 aws_s3_tester_clean_up(&tester);
7780
7781 return 0;
7782 }
7783
7784 AWS_TEST_CASE(test_s3_auto_ranged_put_sending_user_agent, s_test_s3_auto_ranged_put_sending_user_agent)
7785 static int s_test_s3_auto_ranged_put_sending_user_agent(struct aws_allocator *allocator, void *ctx) {
7786 (void)ctx;
7787
7788 struct aws_s3_tester tester;
7789 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7790
7791 struct aws_s3_client *client = NULL;
7792 ASSERT_SUCCESS(s_s3_test_sending_user_agent_create_client(&tester, &client));
7793
7794 {
7795 struct aws_s3_tester_meta_request_options options = {
7796 .allocator = allocator,
7797 .client = client,
7798 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
7799 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
7800 .put_options =
7801 {
7802 .ensure_multipart = true,
7803 },
7804 };
7805
7806 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, NULL));
7807 }
7808
7809 aws_s3_client_release(client);
7810 aws_s3_tester_clean_up(&tester);
7811
7812 return 0;
7813 }
7814
7815 AWS_TEST_CASE(test_s3_default_sending_meta_request_user_agent, s_test_s3_default_sending_meta_request_user_agent)
7816 static int s_test_s3_default_sending_meta_request_user_agent(struct aws_allocator *allocator, void *ctx) {
7817 (void)ctx;
7818
7819 struct aws_s3_tester tester;
7820 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7821
7822 struct aws_s3_client *client = NULL;
7823 ASSERT_SUCCESS(s_s3_test_sending_user_agent_create_client(&tester, &client));
7824
7825 {
7826 struct aws_s3_tester_meta_request_options options = {
7827 .allocator = allocator,
7828 .client = client,
7829 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
7830 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
7831 .default_type_options =
7832 {
7833 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
7834 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
7835 },
7836 .get_options =
7837 {
7838 .object_path = g_pre_existing_object_1MB,
7839 },
7840 };
7841
7842 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, NULL));
7843 }
7844
7845 aws_s3_client_release(client);
7846 aws_s3_tester_clean_up(&tester);
7847
7848 return 0;
7849 }
7850
7851 struct range_requests_test_user_data {
7852 struct aws_http_headers *headers;
7853 struct aws_byte_buf *body_buffer;
7854 };
7855
7856 static int s_range_requests_headers_callback(
7857 struct aws_s3_meta_request *meta_request,
7858 const struct aws_http_headers *headers,
7859 int response_status,
7860 void *user_data) {
7861 (void)meta_request;
7862 (void)response_status;
7863
7864 struct aws_s3_meta_request_test_results *test_results = user_data;
7865 struct range_requests_test_user_data *test_user_data = test_results->tester->user_data;
7866
7867 if (test_user_data != NULL) {
7868 copy_http_headers(headers, test_user_data->headers);
7869 }
7870
7871 return AWS_OP_SUCCESS;
7872 }
7873
7874 static int s_range_requests_receive_body_callback(
7875 struct aws_s3_meta_request *meta_request,
7876 const struct aws_byte_cursor *body,
7877 uint64_t range_start,
7878 void *user_data) {
7879 (void)meta_request;
7880 (void)range_start;
7881
7882 struct aws_s3_meta_request_test_results *test_results = user_data;
7883 struct range_requests_test_user_data *test_user_data = test_results->tester->user_data;
7884
7885 aws_byte_buf_append_dynamic(test_user_data->body_buffer, body);
7886
7887 return AWS_OP_SUCCESS;
7888 }
7889
7890 AWS_TEST_CASE(test_s3_range_requests, s_test_s3_range_requests)
7891 static int s_test_s3_range_requests(struct aws_allocator *allocator, void *ctx) {
7892 (void)ctx;
7893
7894 struct aws_s3_tester tester;
7895 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
7896
7897 const struct aws_byte_cursor object_names[] = {
7898 g_pre_existing_object_1MB,
7899 g_pre_existing_object_kms_10MB,
7900 g_pre_existing_object_aes256_10MB,
7901 };
7902
7903 enum aws_s3_tester_sse_type object_sse_types[] = {
7904 AWS_S3_TESTER_SSE_NONE,
7905 AWS_S3_TESTER_SSE_KMS,
7906 AWS_S3_TESTER_SSE_AES256,
7907 };
7908
7909 const struct aws_byte_cursor ranges[] = {
7910 // No range at all.
7911 {0, NULL},
7912
7913 // Single byte range.
7914 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=8-8"),
7915
7916 // Single byte range (first byte).
7917 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-0"),
7918
7919 // First 8K. 8K < client's 16K part size.
7920 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-8191"),
7921
7922 // First 0.5 MB. 0.5 MB < 1 MB test file.
7923 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-524287"),
7924
7925 // 0.5 MB - 2 MB range. This overlaps and goes beyond the 1 MB test file size.
7926 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=524288-2097151"),
7927
7928 // Get everything after the first 0.5 MB
7929 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=524288-"),
7930
7931 // Last 0.5 MB
7932 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=-524288"),
7933
7934 // Everything after first 8K
7935 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=8192-"),
7936
7937 // Last 8K
7938 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=-8192"),
7939
7940 // 1MiB - 8K to the end.
7941 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=1040384-"),
7942 };
7943
7944 /* List of headers that should have matching values between the auto_ranged_get and default (which sends the HTTP
7945 * request as-is to S3) meta request.*/
7946 const struct aws_byte_cursor headers_that_should_match[] = {
7947 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ETag"),
7948 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Accept-Ranges"),
7949 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Range"),
7950 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
7951 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
7952 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Server"),
7953 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption"),
7954 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-aws-kms-key"),
7955 };
7956
7957 /* List of headers that are okay to be in the auto_ranged_get response and not in the default response, or vice
7958 * versa.*/
7959 const struct aws_byte_cursor headers_to_ignore[] = {
7960 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"),
7961 };
7962
7963 struct aws_s3_tester_client_options client_options = {
7964 .part_size = 16 * 1024,
7965 };
7966
7967 struct aws_s3_client *client = NULL;
7968 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
7969
7970 const size_t num_object_names = AWS_ARRAY_SIZE(object_names);
7971 const size_t num_ranges = AWS_ARRAY_SIZE(ranges);
7972
7973 for (size_t object_name_index = 0; object_name_index < num_object_names; ++object_name_index) {
7974 for (size_t range_index = 0; range_index < num_ranges; ++range_index) {
7975
7976 AWS_LOGF_INFO(
7977 AWS_LS_S3_GENERAL, "Testing object name %d and range %d", (int)object_name_index, (int)range_index);
7978
7979 struct aws_byte_buf range_get_buffer;
7980 aws_byte_buf_init(&range_get_buffer, allocator, 256);
7981 struct aws_http_headers *range_get_headers = aws_http_headers_new(allocator);
7982
7983 struct aws_byte_buf verify_range_get_buffer;
7984 aws_byte_buf_init(&verify_range_get_buffer, allocator, 256);
7985 struct aws_http_headers *verify_range_get_headers = aws_http_headers_new(allocator);
7986
7987 struct aws_s3_tester_meta_request_options options = {
7988 .allocator = allocator,
7989 .client = client,
7990 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
7991 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
7992 .headers_callback = s_range_requests_headers_callback,
7993 .body_callback = s_range_requests_receive_body_callback,
7994 .get_options =
7995 {
7996 .object_path = object_names[object_name_index],
7997 .object_range = ranges[range_index],
7998 },
7999 .sse_type = object_sse_types[object_name_index],
8000 };
8001
8002 {
8003 struct range_requests_test_user_data test_user_data = {
8004 .headers = range_get_headers,
8005 .body_buffer = &range_get_buffer,
8006 };
8007
8008 tester.user_data = &test_user_data;
8009
8010 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, NULL));
8011 }
8012
8013 /* Send a default meta request (which just pushes the request directly to S3) with the same options to
8014 * verify the format of each request. */
8015 struct aws_s3_tester_meta_request_options verify_options = {
8016 .allocator = allocator,
8017 .client = client,
8018 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
8019 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8020 .headers_callback = s_range_requests_headers_callback,
8021 .body_callback = s_range_requests_receive_body_callback,
8022 .default_type_options =
8023 {
8024 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
8025 .operation_name = aws_byte_cursor_from_c_str("GetObject"),
8026 },
8027 .get_options =
8028 {
8029 .object_path = object_names[object_name_index],
8030 .object_range = ranges[range_index],
8031 },
8032 .sse_type = object_sse_types[object_name_index],
8033 };
8034
8035 {
8036 struct range_requests_test_user_data test_user_data = {
8037 .headers = verify_range_get_headers,
8038 .body_buffer = &verify_range_get_buffer,
8039 };
8040
8041 tester.user_data = &test_user_data;
8042
8043 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &verify_options, NULL));
8044 }
8045
8046 /* Compare headers. */
8047 for (size_t i = 0; i < aws_http_headers_count(verify_range_get_headers); ++i) {
8048 struct aws_http_header verify_header;
8049 ASSERT_SUCCESS(aws_http_headers_get_index(verify_range_get_headers, i, &verify_header));
8050
8051 bool ignore_header = false;
8052
8053 for (size_t j = 0; j < AWS_ARRAY_SIZE(headers_to_ignore); ++j) {
8054 if (aws_byte_cursor_eq_ignore_case(&headers_to_ignore[j], &verify_header.name)) {
8055 ignore_header = true;
8056 break;
8057 }
8058 }
8059
8060 if (ignore_header) {
8061 aws_http_headers_erase(range_get_headers, verify_header.name);
8062 continue;
8063 }
8064
8065 AWS_LOGF_INFO(
8066 AWS_LS_S3_GENERAL,
8067 "%d,%d Checking for header " PRInSTR,
8068 (int)object_name_index,
8069 (int)range_index,
8070 AWS_BYTE_CURSOR_PRI(verify_header.name));
8071
8072 struct aws_byte_cursor header_value;
8073 ASSERT_SUCCESS(aws_http_headers_get(range_get_headers, verify_header.name, &header_value));
8074
8075 for (size_t j = 0; j < AWS_ARRAY_SIZE(headers_that_should_match); ++j) {
8076 if (!aws_byte_cursor_eq_ignore_case(&headers_that_should_match[j], &verify_header.name)) {
8077 continue;
8078 }
8079
8080 AWS_LOGF_INFO(
8081 AWS_LS_S3_GENERAL,
8082 "%d,%d Header Contents " PRInSTR " vs " PRInSTR,
8083 (int)object_name_index,
8084 (int)range_index,
8085 AWS_BYTE_CURSOR_PRI(verify_header.value),
8086 AWS_BYTE_CURSOR_PRI(header_value));
8087
8088 ASSERT_TRUE(aws_byte_cursor_eq(&verify_header.value, &header_value));
8089 }
8090
8091 ASSERT_SUCCESS(aws_http_headers_erase(range_get_headers, verify_header.name));
8092 }
8093
8094 for (size_t i = 0; i < aws_http_headers_count(range_get_headers); ++i) {
8095 struct aws_http_header header;
8096
8097 ASSERT_SUCCESS(aws_http_headers_get_index(range_get_headers, i, &header));
8098 bool ignore_header = false;
8099
8100 /* If the ignore header doesn't exist in the verify_range_get_headers, ignore it here. */
8101 for (size_t j = 0; j < AWS_ARRAY_SIZE(headers_to_ignore); ++j) {
8102 if (aws_byte_cursor_eq_ignore_case(&headers_to_ignore[j], &header.name)) {
8103 ignore_header = true;
8104 break;
8105 }
8106 }
8107
8108 if (ignore_header) {
8109 ASSERT_SUCCESS(aws_http_headers_erase(range_get_headers, header.name));
8110 continue;
8111 }
8112
8113 AWS_LOGF_INFO(AWS_LS_S3_GENERAL, "Left over header: " PRInSTR, AWS_BYTE_CURSOR_PRI(header.name));
8114 }
8115
8116 ASSERT_TRUE(aws_http_headers_count(range_get_headers) == 0);
8117
8118 /* Compare Body Contents */
8119 ASSERT_TRUE(aws_byte_buf_eq(&range_get_buffer, &verify_range_get_buffer));
8120
8121 aws_http_headers_release(range_get_headers);
8122 aws_byte_buf_clean_up(&range_get_buffer);
8123
8124 aws_http_headers_release(verify_range_get_headers);
8125 aws_byte_buf_clean_up(&verify_range_get_buffer);
8126 }
8127 }
8128
8129 aws_s3_client_release(client);
8130 aws_s3_tester_clean_up(&tester);
8131
8132 return 0;
8133 }
8134
8135 /* Using the range to fetch the object that has less than 1 part. */
8136 AWS_TEST_CASE(test_s3_range_requests_less_than_a_part, s_test_s3_range_requests_less_than_a_part)
8137 static int s_test_s3_range_requests_less_than_a_part(struct aws_allocator *allocator, void *ctx) {
8138 (void)ctx;
8139
8140 struct aws_s3_tester tester;
8141 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8142
8143 struct aws_s3_tester_client_options client_options = {
8144 .part_size = MB_TO_BYTES(1), // Use 1 MiB as the part.
8145 };
8146
8147 struct aws_s3_client *client = NULL;
8148 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
8149 const struct aws_byte_cursor ranges[] = {
8150 // 0.5 MB - 2 MB range. This overlaps and goes beyond the 1 MB test file size.
8151 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=524288-2097151"),
8152
8153 // Get everything after the first 0.5 MB
8154 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=524288-"),
8155 };
8156 const size_t num_ranges = AWS_ARRAY_SIZE(ranges);
8157
8158 for (size_t range_index = 0; range_index < num_ranges; ++range_index) {
8159 struct aws_s3_tester_meta_request_options options = {
8160 .allocator = allocator,
8161 .client = client,
8162 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
8163 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8164 .get_options =
8165 {
8166 .object_path = g_pre_existing_object_1MB,
8167 .object_range = ranges[range_index],
8168 },
8169 };
8170 struct aws_s3_meta_request_test_results results;
8171 aws_s3_meta_request_test_results_init(&results, allocator);
8172
8173 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &results));
8174 aws_s3_meta_request_test_results_clean_up(&results);
8175 }
8176
8177 aws_s3_client_release(client);
8178 aws_s3_tester_clean_up(&tester);
8179
8180 return 0;
8181 }
8182
8183 AWS_TEST_CASE(test_s3_not_satisfiable_range, s_test_s3_not_satisfiable_range)
8184 static int s_test_s3_not_satisfiable_range(struct aws_allocator *allocator, void *ctx) {
8185 (void)ctx;
8186
8187 struct aws_s3_tester tester;
8188 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8189
8190 struct aws_s3_tester_client_options client_options = {
8191 .part_size = 16 * 1024,
8192 };
8193
8194 struct aws_s3_client *client = NULL;
8195 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
8196
8197 struct aws_s3_tester_meta_request_options options = {
8198 .allocator = allocator,
8199 .client = client,
8200 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
8201 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
8202 .headers_callback = s_range_requests_headers_callback,
8203 .body_callback = s_range_requests_receive_body_callback,
8204 .get_options =
8205 {
8206 .object_path = g_pre_existing_object_1MB,
8207 .object_range = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=2097151-"),
8208 },
8209 };
8210
8211 struct aws_s3_meta_request_test_results results;
8212 aws_s3_meta_request_test_results_init(&results, allocator);
8213
8214 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &results));
8215
8216 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE, results.finished_response_status);
8217 ASSERT_NOT_NULL(results.error_response_operation_name);
8218 ASSERT_TRUE(
8219 aws_string_eq_c_str(results.error_response_operation_name, "GetObject") ||
8220 aws_string_eq_c_str(results.error_response_operation_name, "HeadObject"));
8221
8222 aws_s3_meta_request_test_results_clean_up(&results);
8223
8224 aws_s3_client_release(client);
8225 aws_s3_tester_clean_up(&tester);
8226
8227 return 0;
8228 }
8229
8230 AWS_TEST_CASE(test_s3_invalid_start_range_greator_than_end_range, s_test_s3_invalid_start_range_greator_than_end_range)
8231 static int s_test_s3_invalid_start_range_greator_than_end_range(struct aws_allocator *allocator, void *ctx) {
8232 (void)ctx;
8233
8234 struct aws_s3_tester tester;
8235 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8236
8237 struct aws_s3_tester_client_options client_options = {
8238 .part_size = 16 * 1024,
8239 };
8240
8241 struct aws_s3_client *client = NULL;
8242 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
8243
8244 struct aws_s3_tester_meta_request_options options = {
8245 .allocator = allocator,
8246 .client = client,
8247 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
8248 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
8249 .get_options =
8250 {
8251 .object_path = g_pre_existing_object_1MB,
8252 .object_range = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=20-10"),
8253 },
8254 };
8255
8256 struct aws_s3_meta_request_test_results results;
8257 aws_s3_meta_request_test_results_init(&results, allocator);
8258
8259 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &results));
8260 ASSERT_INT_EQUALS(results.finished_error_code, AWS_ERROR_S3_INVALID_RANGE_HEADER);
8261 ASSERT_INT_EQUALS(0, tester.synced_data.meta_request_finish_count);
8262
8263 aws_s3_meta_request_test_results_clean_up(&results);
8264
8265 aws_s3_client_release(client);
8266 aws_s3_tester_clean_up(&tester);
8267
8268 return 0;
8269 }
8270
8271 AWS_TEST_CASE(test_s3_empty_file_edge_case, s_test_s3_empty_file_edge_case)
8272 static int s_test_s3_empty_file_edge_case(struct aws_allocator *allocator, void *ctx) {
8273 (void)ctx;
8274
8275 struct aws_s3_tester tester;
8276 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8277
8278 struct aws_s3_tester_client_options client_options = {
8279 .part_size = 16 * 1024,
8280 };
8281
8282 struct aws_s3_client *client = NULL;
8283 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
8284 struct aws_s3_meta_request_test_results results;
8285 {
8286 /***** Empty download failed with range *****/
8287 aws_s3_meta_request_test_results_init(&results, allocator);
8288 struct aws_s3_tester_meta_request_options options = {
8289 .allocator = allocator,
8290 .client = client,
8291 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
8292 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
8293 .get_options =
8294 {
8295 .object_path = g_pre_existing_empty_object,
8296 .object_range = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-0"),
8297 },
8298 };
8299
8300 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &results));
8301 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE, results.finished_response_status);
8302 ASSERT_NOT_NULL(results.error_response_operation_name);
8303 ASSERT_TRUE(aws_string_eq_c_str(results.error_response_operation_name, "GetObject"));
8304 aws_s3_meta_request_test_results_clean_up(&results);
8305 }
8306 {
8307 /***** Empty download succeed with force_dynamic_part_size *****/
8308 aws_s3_meta_request_test_results_init(&results, allocator);
8309 struct aws_s3_tester_meta_request_options force_dynamic_options = {
8310 .allocator = allocator,
8311 .client = client,
8312 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
8313 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8314 .get_options =
8315 {
8316 .force_dynamic_part_size = true,
8317 .object_path = g_pre_existing_empty_object,
8318 },
8319 };
8320
8321 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &force_dynamic_options, &results));
8322
8323 aws_s3_meta_request_test_results_clean_up(&results);
8324 }
8325 aws_s3_client_release(client);
8326 aws_s3_tester_clean_up(&tester);
8327
8328 return 0;
8329 }
8330
8331 static int s_test_s3_copy_object_helper(
8332 struct aws_allocator *allocator,
8333 struct aws_byte_cursor source_key,
8334 struct aws_byte_cursor destination_key,
8335 int expected_error_code,
8336 int expected_response_status,
8337 uint64_t expected_size) {
8338 struct aws_s3_tester tester;
8339 AWS_ZERO_STRUCT(tester);
8340 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8341
8342 struct aws_byte_cursor source_bucket = g_test_bucket_name;
8343 struct aws_byte_cursor destination_bucket = g_test_bucket_name;
8344
8345 char endpoint[1024];
8346 snprintf(
8347 endpoint,
8348 sizeof(endpoint),
8349 "%.*s.s3.%s.amazonaws.com",
8350 (int)destination_bucket.len,
8351 destination_bucket.ptr,
8352 g_test_s3_region.ptr);
8353
8354 struct aws_byte_cursor copy_source_uri;
8355 struct aws_byte_buf encoded_path;
8356 AWS_ZERO_STRUCT(copy_source_uri);
8357 AWS_ZERO_STRUCT(encoded_path);
8358
8359 aws_byte_buf_init(&encoded_path, allocator, source_key.len);
8360 aws_byte_buf_append_encoding_uri_path(&encoded_path, &source_key);
8361
8362 /* without copy_source_uri */
8363 ASSERT_SUCCESS(aws_test_s3_copy_object_helper(
8364 allocator,
8365 &tester,
8366 source_bucket,
8367 source_key,
8368 aws_byte_cursor_from_c_str(endpoint),
8369 destination_key,
8370 expected_error_code,
8371 expected_response_status,
8372 expected_size,
8373 false,
8374 copy_source_uri));
8375
8376 /* with path style copy_source_uri */
8377 char source_url[1024];
8378 snprintf(
8379 source_url,
8380 sizeof(source_url),
8381 "https://s3.%s.amazonaws.com/" PRInSTR "/" PRInSTR "",
8382 g_test_s3_region.ptr,
8383 AWS_BYTE_CURSOR_PRI(source_bucket),
8384 AWS_BYTE_BUF_PRI(encoded_path));
8385 copy_source_uri = aws_byte_cursor_from_c_str(source_url);
8386 ASSERT_SUCCESS(aws_test_s3_copy_object_helper(
8387 allocator,
8388 &tester,
8389 source_bucket,
8390 source_key,
8391 aws_byte_cursor_from_c_str(endpoint),
8392 destination_key,
8393 expected_error_code,
8394 expected_response_status,
8395 expected_size,
8396 false,
8397 copy_source_uri));
8398
8399 /* with virtual style copy_source_uri */
8400 snprintf(
8401 source_url,
8402 sizeof(source_url),
8403 "https://" PRInSTR ".s3.%s.amazonaws.com/" PRInSTR "",
8404 AWS_BYTE_CURSOR_PRI(source_bucket),
8405 g_test_s3_region.ptr,
8406 AWS_BYTE_BUF_PRI(encoded_path));
8407 copy_source_uri = aws_byte_cursor_from_c_str(source_url);
8408 ASSERT_SUCCESS(aws_test_s3_copy_object_helper(
8409 allocator,
8410 &tester,
8411 source_bucket,
8412 source_key,
8413 aws_byte_cursor_from_c_str(endpoint),
8414 destination_key,
8415 expected_error_code,
8416 expected_response_status,
8417 expected_size,
8418 false,
8419 copy_source_uri));
8420
8421 aws_s3_tester_clean_up(&tester);
8422 aws_byte_buf_clean_up(&encoded_path);
8423 return AWS_OP_SUCCESS;
8424 }
8425
8426 AWS_TEST_CASE(test_s3_copy_small_object, s_test_s3_copy_small_object)
8427 static int s_test_s3_copy_small_object(struct aws_allocator *allocator, void *ctx) {
8428 (void)ctx;
8429
8430 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-1MB");
8431 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_1MB");
8432 return s_test_s3_copy_object_helper(
8433 allocator, source_key, destination_key, AWS_ERROR_SUCCESS, AWS_HTTP_STATUS_CODE_200_OK, MB_TO_BYTES(1));
8434 }
8435
8436 AWS_TEST_CASE(test_s3_copy_small_object_special_char, s_test_s3_copy_small_object_special_char)
8437 static int s_test_s3_copy_small_object_special_char(struct aws_allocator *allocator, void *ctx) {
8438 (void)ctx;
8439
8440 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-1MB-@");
8441 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_1MB_@");
8442
8443 return s_test_s3_copy_object_helper(
8444 allocator, source_key, destination_key, AWS_ERROR_SUCCESS, AWS_HTTP_STATUS_CODE_200_OK, MB_TO_BYTES(1));
8445 }
8446
8447 AWS_TEST_CASE(test_s3_multipart_copy_large_object_special_char, s_test_s3_multipart_copy_large_object_special_char)
8448 static int s_test_s3_multipart_copy_large_object_special_char(struct aws_allocator *allocator, void *ctx) {
8449 (void)ctx;
8450
8451 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-2GB-@");
8452 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_2GB-@");
8453
8454 return s_test_s3_copy_object_helper(
8455 allocator, source_key, destination_key, AWS_ERROR_SUCCESS, AWS_HTTP_STATUS_CODE_200_OK, GB_TO_BYTES(2));
8456 }
8457
8458 AWS_TEST_CASE(test_s3_multipart_copy_large_object, s_test_s3_multipart_copy_large_object)
8459 static int s_test_s3_multipart_copy_large_object(struct aws_allocator *allocator, void *ctx) {
8460 (void)ctx;
8461
8462 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-2GB");
8463 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_2GB");
8464 return s_test_s3_copy_object_helper(
8465 allocator, source_key, destination_key, AWS_ERROR_SUCCESS, AWS_HTTP_STATUS_CODE_200_OK, GB_TO_BYTES(2));
8466 }
8467
8468 AWS_TEST_CASE(test_s3_copy_object_invalid_source_key, s_test_s3_copy_object_invalid_source_key)
8469 static int s_test_s3_copy_object_invalid_source_key(struct aws_allocator *allocator, void *ctx) {
8470 (void)ctx;
8471
8472 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("__INVALID__");
8473 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/__INVALID__");
8474 return s_test_s3_copy_object_helper(
8475 allocator,
8476 source_key,
8477 destination_key,
8478 AWS_ERROR_S3_INVALID_RESPONSE_STATUS,
8479 AWS_HTTP_STATUS_CODE_404_NOT_FOUND,
8480 0 /* expected_size is ignored */);
8481 }
8482
8483 /**
8484 * Test a bypass Copy Object meta request using a slash prefix in the x_amz_copy_source header.
8485 * S3 supports both bucket/key and /bucket/key
8486 * This test validates the fix for the bug described in https://sim.amazon.com/issues/AWSCRT-730
8487 */
8488 AWS_TEST_CASE(test_s3_copy_source_prefixed_by_slash, s_test_s3_copy_source_prefixed_by_slash)
8489 static int s_test_s3_copy_source_prefixed_by_slash(struct aws_allocator *allocator, void *ctx) {
8490 (void)ctx;
8491 struct aws_s3_tester tester;
8492 AWS_ZERO_STRUCT(tester);
8493 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8494
8495 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-1MB");
8496 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_1MB");
8497
8498 struct aws_byte_cursor source_bucket = g_test_bucket_name;
8499
8500 char copy_source_value[1024];
8501 snprintf(
8502 copy_source_value,
8503 sizeof(copy_source_value),
8504 "/%.*s/%.*s",
8505 (int)source_bucket.len,
8506 source_bucket.ptr,
8507 (int)source_key.len,
8508 source_key.ptr);
8509
8510 struct aws_byte_cursor x_amz_copy_source = aws_byte_cursor_from_c_str(copy_source_value);
8511 struct aws_byte_cursor destination_bucket = g_test_bucket_name;
8512
8513 char endpoint[1024];
8514 snprintf(
8515 endpoint,
8516 sizeof(endpoint),
8517 "%.*s.s3.%s.amazonaws.com",
8518 (int)destination_bucket.len,
8519 destination_bucket.ptr,
8520 g_test_s3_region.ptr);
8521
8522 struct aws_byte_cursor copy_source_uri;
8523 AWS_ZERO_STRUCT(copy_source_uri);
8524
8525 ASSERT_SUCCESS(aws_test_s3_copy_object_from_x_amz_copy_source(
8526 allocator,
8527 &tester,
8528 x_amz_copy_source,
8529 aws_byte_cursor_from_c_str(endpoint),
8530 destination_key,
8531 AWS_ERROR_SUCCESS,
8532 AWS_HTTP_STATUS_CODE_200_OK,
8533 MB_TO_BYTES(1),
8534 false,
8535 copy_source_uri));
8536
8537 aws_s3_tester_clean_up(&tester);
8538 return AWS_OP_SUCCESS;
8539 }
8540
8541 AWS_TEST_CASE(test_s3_copy_invalid_source_uri, s_test_s3_copy_invalid_source_uri)
8542 static int s_test_s3_copy_invalid_source_uri(struct aws_allocator *allocator, void *ctx) {
8543 (void)ctx;
8544 struct aws_s3_tester tester;
8545 AWS_ZERO_STRUCT(tester);
8546 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8547
8548 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-1MB");
8549 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_1MB");
8550
8551 struct aws_byte_cursor source_bucket = g_test_bucket_name;
8552
8553 char copy_source_value[1024];
8554 snprintf(
8555 copy_source_value,
8556 sizeof(copy_source_value),
8557 "/%.*s/%.*s",
8558 (int)source_bucket.len,
8559 source_bucket.ptr,
8560 (int)source_key.len,
8561 source_key.ptr);
8562
8563 struct aws_byte_cursor x_amz_copy_source = aws_byte_cursor_from_c_str(copy_source_value);
8564 struct aws_byte_cursor destination_bucket = g_test_bucket_name;
8565
8566 char endpoint[1024];
8567 snprintf(
8568 endpoint,
8569 sizeof(endpoint),
8570 "%.*s.s3.%s.amazonaws.com",
8571 (int)destination_bucket.len,
8572 destination_bucket.ptr,
8573 g_test_s3_region.ptr);
8574
8575 struct aws_byte_cursor copy_source_uri = aws_byte_cursor_from_c_str("http://invalid-uri.com:80:80/path");
8576
8577 ASSERT_SUCCESS(aws_test_s3_copy_object_from_x_amz_copy_source(
8578 allocator,
8579 &tester,
8580 x_amz_copy_source,
8581 aws_byte_cursor_from_c_str(endpoint),
8582 destination_key,
8583 AWS_ERROR_MALFORMED_INPUT_STRING,
8584 0,
8585 MB_TO_BYTES(1),
8586 false,
8587 copy_source_uri));
8588
8589 aws_s3_tester_clean_up(&tester);
8590 return AWS_OP_SUCCESS;
8591 }
8592
8593 /**
8594 * Test multipart Copy Object meta request using a slash prefix in the x_amz_copy_source header.
8595 * S3 supports both bucket/key and /bucket/key
8596 * This test validates the fix for the bug described in https://sim.amazon.com/issues/AWSCRT-730
8597 */
8598 AWS_TEST_CASE(test_s3_copy_source_prefixed_by_slash_multipart, s_test_s3_copy_source_prefixed_by_slash_multipart)
8599 static int s_test_s3_copy_source_prefixed_by_slash_multipart(struct aws_allocator *allocator, void *ctx) {
8600 (void)ctx;
8601 struct aws_s3_tester tester;
8602 AWS_ZERO_STRUCT(tester);
8603 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8604
8605 struct aws_byte_cursor source_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pre-existing-256MB");
8606 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("copies/destination_256MB");
8607
8608 struct aws_byte_cursor source_bucket = g_test_bucket_name;
8609
8610 char copy_source_value[1024];
8611 snprintf(
8612 copy_source_value,
8613 sizeof(copy_source_value),
8614 "/%.*s/%.*s",
8615 (int)source_bucket.len,
8616 source_bucket.ptr,
8617 (int)source_key.len,
8618 source_key.ptr);
8619
8620 struct aws_byte_cursor x_amz_copy_source = aws_byte_cursor_from_c_str(copy_source_value);
8621 struct aws_byte_cursor destination_bucket = g_test_bucket_name;
8622
8623 char endpoint[1024];
8624 snprintf(
8625 endpoint,
8626 sizeof(endpoint),
8627 "%.*s.s3.%s.amazonaws.com",
8628 (int)destination_bucket.len,
8629 destination_bucket.ptr,
8630 g_test_s3_region.ptr);
8631
8632 struct aws_byte_cursor copy_source_uri;
8633 AWS_ZERO_STRUCT(copy_source_uri);
8634
8635 ASSERT_SUCCESS(aws_test_s3_copy_object_from_x_amz_copy_source(
8636 allocator,
8637 &tester,
8638 x_amz_copy_source,
8639 aws_byte_cursor_from_c_str(endpoint),
8640 destination_key,
8641 AWS_ERROR_SUCCESS,
8642 AWS_HTTP_STATUS_CODE_200_OK,
8643 MB_TO_BYTES(256),
8644 false,
8645 copy_source_uri));
8646
8647 aws_s3_tester_clean_up(&tester);
8648 return AWS_OP_SUCCESS;
8649 }
8650
8651 /**
8652 * Helper: copy object then verify metadata and tags on the destination.
8653 * If use_replace: sets REPLACE directives with explicit metadata/tags on the request.
8654 * If !use_replace: sets COPY directives (no metadata/tags on request).
8655 * expect_metadata: whether HeadObject should have the metadata after copy.
8656 */
8657 static int s_test_s3_copy_object_properties_helper(
8658 struct aws_allocator *allocator,
8659 struct aws_byte_cursor source_key,
8660 struct aws_byte_cursor destination_key,
8661 bool use_replace,
8662 bool expect_metadata) {
8663
8664 struct aws_s3_tester tester;
8665 AWS_ZERO_STRUCT(tester);
8666 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8667
8668 struct aws_byte_cursor source_bucket = g_test_bucket_name;
8669
8670 char endpoint[1024];
8671 snprintf(
8672 endpoint,
8673 sizeof(endpoint),
8674 "%.*s.s3.%s.amazonaws.com",
8675 (int)source_bucket.len,
8676 source_bucket.ptr,
8677 g_test_s3_region.ptr);
8678
8679 char copy_source_value[1024];
8680 snprintf(
8681 copy_source_value,
8682 sizeof(copy_source_value),
8683 "%.*s/%.*s",
8684 (int)source_bucket.len,
8685 source_bucket.ptr,
8686 (int)source_key.len,
8687 source_key.ptr);
8688
8689 char destination_path[512];
8690 snprintf(destination_path, sizeof(destination_path), "/%.*s", (int)destination_key.len, destination_key.ptr);
8691
8692 /* Construct CopyObject request */
8693 struct aws_http_message *message = aws_http_message_new_request(allocator);
8694 ASSERT_NOT_NULL(message);
8695 ASSERT_SUCCESS(aws_http_message_set_request_path(message, aws_byte_cursor_from_c_str(destination_path)));
8696 ASSERT_SUCCESS(aws_http_message_set_request_method(message, aws_http_method_put));
8697
8698 struct aws_http_headers *headers = aws_http_message_get_headers(message);
8699 ASSERT_SUCCESS(aws_http_headers_add(headers, g_host_header_name, aws_byte_cursor_from_c_str(endpoint)));
8700 ASSERT_SUCCESS(aws_http_headers_add(
8701 headers, aws_byte_cursor_from_c_str("x-amz-copy-source"), aws_byte_cursor_from_c_str(copy_source_value)));
8702
8703 if (use_replace) {
8704 ASSERT_SUCCESS(aws_http_headers_add(
8705 headers, aws_byte_cursor_from_c_str("x-amz-metadata-directive"), aws_byte_cursor_from_c_str("REPLACE")));
8706 ASSERT_SUCCESS(aws_http_headers_add(
8707 headers, aws_byte_cursor_from_c_str("x-amz-meta-test-key"), aws_byte_cursor_from_c_str("test-value")));
8708 ASSERT_SUCCESS(aws_http_headers_add(
8709 headers, aws_byte_cursor_from_c_str("Content-Type"), aws_byte_cursor_from_c_str("application/x-replaced")));
8710 ASSERT_SUCCESS(aws_http_headers_add(
8711 headers, aws_byte_cursor_from_c_str("Content-Disposition"), aws_byte_cursor_from_c_str("attachment")));
8712 ASSERT_SUCCESS(aws_http_headers_add(
8713 headers, aws_byte_cursor_from_c_str("Content-Language"), aws_byte_cursor_from_c_str("en-US")));
8714 ASSERT_SUCCESS(aws_http_headers_add(
8715 headers, aws_byte_cursor_from_c_str("Cache-Control"), aws_byte_cursor_from_c_str("max-age=3600")));
8716 ASSERT_SUCCESS(aws_http_headers_add(
8717 headers, aws_byte_cursor_from_c_str("x-amz-tagging-directive"), aws_byte_cursor_from_c_str("REPLACE")));
8718 ASSERT_SUCCESS(aws_http_headers_add(
8719 headers, aws_byte_cursor_from_c_str("x-amz-tagging"), aws_byte_cursor_from_c_str("env=test&feature=copy")));
8720 } else {
8721 ASSERT_SUCCESS(aws_http_headers_add(
8722 headers, aws_byte_cursor_from_c_str("x-amz-metadata-directive"), aws_byte_cursor_from_c_str("COPY")));
8723 ASSERT_SUCCESS(aws_http_headers_add(
8724 headers, aws_byte_cursor_from_c_str("x-amz-tagging-directive"), aws_byte_cursor_from_c_str("COPY")));
8725 }
8726
8727 /* Execute the copy */
8728 struct aws_s3_meta_request_test_results copy_results;
8729 aws_s3_meta_request_test_results_init(&copy_results, allocator);
8730
8731 struct aws_s3_tester_meta_request_options options = {
8732 .allocator = allocator,
8733 .meta_request_type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT,
8734 .message = message,
8735 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8736 };
8737
8738 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &copy_results));
8739 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_200_OK, copy_results.finished_response_status);
8740
8741 aws_s3_meta_request_test_results_clean_up(&copy_results);
8742 aws_http_message_release(message);
8743 aws_s3_tester_clean_up(&tester);
8744
8745 /* Verify destination metadata via HeadObject */
8746 struct aws_s3_tester head_tester;
8747 AWS_ZERO_STRUCT(head_tester);
8748 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &head_tester));
8749
8750 struct aws_http_message *head_message = aws_http_message_new_request(allocator);
8751 ASSERT_NOT_NULL(head_message);
8752 ASSERT_SUCCESS(aws_http_message_set_request_method(head_message, aws_byte_cursor_from_c_str("HEAD")));
8753 ASSERT_SUCCESS(aws_http_message_set_request_path(head_message, aws_byte_cursor_from_c_str(destination_path)));
8754 struct aws_http_headers *head_headers = aws_http_message_get_headers(head_message);
8755 ASSERT_SUCCESS(aws_http_headers_add(head_headers, g_host_header_name, aws_byte_cursor_from_c_str(endpoint)));
8756
8757 struct aws_s3_meta_request_test_results head_results;
8758 aws_s3_meta_request_test_results_init(&head_results, allocator);
8759
8760 struct aws_s3_tester_meta_request_options head_options = {
8761 .allocator = allocator,
8762 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
8763 .message = head_message,
8764 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8765 .default_type_options =
8766 {
8767 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
8768 .operation_name = aws_byte_cursor_from_c_str("HeadObject"),
8769 },
8770 };
8771
8772 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&head_tester, &head_options, &head_results));
8773 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_200_OK, head_results.finished_response_status);
8774
8775 if (expect_metadata) {
8776 struct aws_byte_cursor meta_value;
8777 ASSERT_SUCCESS(aws_http_headers_get(
8778 head_results.response_headers, aws_byte_cursor_from_c_str("x-amz-meta-test-key"), &meta_value));
8779 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&meta_value, "test-value"));
8780
8781 struct aws_byte_cursor cache_value;
8782 ASSERT_SUCCESS(aws_http_headers_get(
8783 head_results.response_headers, aws_byte_cursor_from_c_str("Cache-Control"), &cache_value));
8784 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&cache_value, "max-age=3600"));
8785
8786 struct aws_byte_cursor content_type_value;
8787 ASSERT_SUCCESS(aws_http_headers_get(
8788 head_results.response_headers, aws_byte_cursor_from_c_str("Content-Type"), &content_type_value));
8789 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&content_type_value, "application/x-replaced"));
8790 } else {
8791 /* Metadata NOT expected (documents MPU path limitation with COPY directive) */
8792 struct aws_byte_cursor meta_value;
8793 ASSERT_TRUE(
8794 aws_http_headers_get(
8795 head_results.response_headers, aws_byte_cursor_from_c_str("x-amz-meta-test-key"), &meta_value) !=
8796 AWS_OP_SUCCESS);
8797 }
8798
8799 aws_s3_meta_request_test_results_clean_up(&head_results);
8800 aws_http_message_release(head_message);
8801 aws_s3_tester_clean_up(&head_tester);
8802
8803 /* Verify tags */
8804 if (expect_metadata) {
8805 struct aws_s3_tester tag_tester;
8806 AWS_ZERO_STRUCT(tag_tester);
8807 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tag_tester));
8808
8809 struct aws_http_message *tag_message = aws_http_message_new_request(allocator);
8810 ASSERT_NOT_NULL(tag_message);
8811 ASSERT_SUCCESS(aws_http_message_set_request_method(tag_message, aws_byte_cursor_from_c_str("GET")));
8812
8813 char tag_path[1536];
8814 snprintf(tag_path, sizeof(tag_path), "%s?tagging", destination_path);
8815 ASSERT_SUCCESS(aws_http_message_set_request_path(tag_message, aws_byte_cursor_from_c_str(tag_path)));
8816
8817 struct aws_http_headers *tag_req_headers = aws_http_message_get_headers(tag_message);
8818 ASSERT_SUCCESS(aws_http_headers_add(tag_req_headers, g_host_header_name, aws_byte_cursor_from_c_str(endpoint)));
8819
8820 struct aws_s3_meta_request_test_results tag_results;
8821 aws_s3_meta_request_test_results_init(&tag_results, allocator);
8822
8823 struct aws_s3_tester_meta_request_options tag_options = {
8824 .allocator = allocator,
8825 .meta_request_type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
8826 .message = tag_message,
8827 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8828 .default_type_options =
8829 {
8830 .mode = AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET,
8831 .operation_name = aws_byte_cursor_from_c_str("GetObjectTagging"),
8832 },
8833 };
8834
8835 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tag_tester, &tag_options, &tag_results));
8836 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_200_OK, tag_results.finished_response_status);
8837 /* Body > 100 bytes = contains actual tags (empty TagSet is ~39 bytes) */
8838 ASSERT_TRUE(tag_results.received_body_size > 100);
8839
8840 aws_s3_meta_request_test_results_clean_up(&tag_results);
8841 aws_http_message_release(tag_message);
8842 aws_s3_tester_clean_up(&tag_tester);
8843 }
8844
8845 return AWS_OP_SUCCESS;
8846 }
8847
8848 AWS_TEST_CASE(
8849 test_s3_copy_object_with_replace_metadata_and_tagging,
8850 s_test_s3_copy_object_with_replace_metadata_and_tagging)
8851 static int s_test_s3_copy_object_with_replace_metadata_and_tagging(struct aws_allocator *allocator, void *ctx) {
8852 (void)ctx;
8853 /* REPLACE: copy with explicit metadata+tags, verify they arrive on destination */
8854 ASSERT_SUCCESS(s_test_s3_copy_object_properties_helper(
8855 allocator,
8856 aws_byte_cursor_from_c_str("pre-existing-1MB"),
8857 aws_byte_cursor_from_c_str("copies/destination_1MB_replace_props"),
8858 true /* use_replace */,
8859 true /* expect_metadata */));
8860
8861 /* COPY directive on the result: single-part CopyObject natively handles it */
8862 ASSERT_SUCCESS(s_test_s3_copy_object_properties_helper(
8863 allocator,
8864 aws_byte_cursor_from_c_str("copies/destination_1MB_replace_props"),
8865 aws_byte_cursor_from_c_str("copies/destination_1MB_copy_directive"),
8866 false /* use_replace */,
8867 true /* expect_metadata: single-part handles COPY natively */));
8868
8869 return AWS_OP_SUCCESS;
8870 }
8871
8872 AWS_TEST_CASE(
8873 test_s3_multipart_copy_object_with_replace_metadata_and_tagging,
8874 s_test_s3_multipart_copy_object_with_replace_metadata_and_tagging)
8875 static int s_test_s3_multipart_copy_object_with_replace_metadata_and_tagging(
8876 struct aws_allocator *allocator,
8877 void *ctx) {
8878 (void)ctx;
8879 /* REPLACE: copy with explicit metadata+tags, verify they arrive on destination */
8880 ASSERT_SUCCESS(s_test_s3_copy_object_properties_helper(
8881 allocator,
8882 aws_byte_cursor_from_c_str("pre-existing-2GB"),
8883 aws_byte_cursor_from_c_str("copies/destination_2GB_replace_props"),
8884 true /* use_replace */,
8885 true /* expect_metadata */));
8886
8887 /* COPY directive on the result: MPU path does NOT copy metadata (known limitation) */
8888 ASSERT_SUCCESS(s_test_s3_copy_object_properties_helper(
8889 allocator,
8890 aws_byte_cursor_from_c_str("copies/destination_2GB_replace_props"),
8891 aws_byte_cursor_from_c_str("copies/destination_2GB_copy_directive"),
8892 false /* use_replace */,
8893 false /* expect_metadata: MPU path does NOT handle COPY directive */));
8894
8895 return AWS_OP_SUCCESS;
8896 }
8897
8898 static int s_s3_get_object_mrap_helper(struct aws_allocator *allocator, bool multipart) {
8899 struct aws_s3_tester tester;
8900 AWS_ZERO_STRUCT(tester);
8901 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8902
8903 struct aws_signing_config_aws signing_config = tester.default_signing_config;
8904 /* Use Sigv4A for signing */
8905 signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
8906 /* Use * for region to sign */
8907 signing_config.region = aws_byte_cursor_from_c_str("*");
8908
8909 struct aws_s3_client_config client_config = {
8910 .part_size = multipart ? 64 * 1024 : 20 * 1024 * 1024,
8911 .region = aws_byte_cursor_from_c_str("*"),
8912 .signing_config = &signing_config,
8913 };
8914
8915 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, 0 /*flag*/));
8916
8917 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
8918 struct aws_s3_meta_request_test_results meta_request_test_results;
8919 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
8920 struct aws_s3_tester_meta_request_options options = {
8921 .allocator = allocator,
8922 .client = client,
8923 .mrap_test = true,
8924 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
8925 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8926 .get_options =
8927 {
8928 .object_path = g_pre_existing_object_1MB,
8929 },
8930 };
8931
8932 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &meta_request_test_results));
8933 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
8934
8935 client = aws_s3_client_release(client);
8936
8937 aws_s3_tester_clean_up(&tester);
8938
8939 return 0;
8940 }
8941
8942 /* Test single-part get object through MRAP (multi-region access point) */
8943 AWS_TEST_CASE(test_s3_get_object_less_than_part_size_mrap, s_test_s3_get_object_less_than_part_size_mrap)
8944 static int s_test_s3_get_object_less_than_part_size_mrap(struct aws_allocator *allocator, void *ctx) {
8945 (void)ctx;
8946 return s_s3_get_object_mrap_helper(allocator, false /*multipart*/);
8947 }
8948
8949 /* Test multi-part get object through MRAP (multi-region access point) */
8950 AWS_TEST_CASE(test_s3_get_object_multipart_mrap, s_test_s3_get_object_multipart_mrap)
8951 static int s_test_s3_get_object_multipart_mrap(struct aws_allocator *allocator, void *ctx) {
8952 (void)ctx;
8953 return s_s3_get_object_mrap_helper(allocator, true /*multipart*/);
8954 }
8955
8956 /* NOTE: MRAP doesn't support MPU yet. https://github.com/aws/aws-sdk-java-v2/discussions/3969 */
8957 static int s_s3_put_object_mrap_helper(struct aws_allocator *allocator, bool multipart) {
8958 struct aws_s3_tester tester;
8959 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
8960
8961 struct aws_signing_config_aws signing_config = tester.default_signing_config;
8962 /* Use Sigv4A for signing */
8963 signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
8964 /* Use * for region to sign */
8965 signing_config.region = aws_byte_cursor_from_c_str("*");
8966
8967 struct aws_s3_client_config client_config = {
8968 .part_size = 5 * 1024 * 1024,
8969 .region = aws_byte_cursor_from_c_str("*"),
8970 .signing_config = &signing_config,
8971 };
8972
8973 ASSERT_SUCCESS(aws_s3_tester_bind_client(&tester, &client_config, 0 /*flag*/));
8974
8975 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
8976
8977 ASSERT_TRUE(client != NULL);
8978
8979 struct aws_s3_meta_request_test_results meta_request_test_results;
8980 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
8981
8982 struct aws_s3_tester_meta_request_options options = {
8983 .allocator = allocator,
8984 .client = client,
8985 .mrap_test = true,
8986 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
8987 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
8988 .put_options =
8989 {
8990 .object_size_mb = multipart ? 10 : 1,
8991 },
8992 };
8993
8994 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &options, &meta_request_test_results));
8995 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
8996
8997 client = aws_s3_client_release(client);
8998
8999 aws_s3_tester_clean_up(&tester);
9000
9001 return 0;
9002 }
9003
9004 /* Test single-part put object through MRAP (multi-region access point) */
9005 AWS_TEST_CASE(test_s3_put_object_less_than_part_size_mrap, s_test_s3_put_object_less_than_part_size_mrap)
9006 static int s_test_s3_put_object_less_than_part_size_mrap(struct aws_allocator *allocator, void *ctx) {
9007 (void)ctx;
9008 return s_s3_put_object_mrap_helper(allocator, false /*multipart*/);
9009 }
9010
9011 static struct aws_http_message *s_put_object_request_new(
9012 struct aws_allocator *allocator,
9013 struct aws_byte_cursor key,
9014 struct aws_byte_cursor endpoint,
9015 struct aws_input_stream *body_stream,
9016 uint64_t content_length) {
9017 AWS_PRECONDITION(allocator);
9018
9019 struct aws_http_message *message = aws_http_message_new_request(allocator);
9020
9021 if (message == NULL) {
9022 return NULL;
9023 }
9024
9025 if (aws_http_message_set_request_path(message, key)) {
9026 goto error_clean_up_message;
9027 }
9028
9029 struct aws_http_header host_header = {
9030 .name = g_host_header_name,
9031 .value = endpoint,
9032 };
9033 if (aws_http_message_add_header(message, host_header)) {
9034 goto error_clean_up_message;
9035 }
9036
9037 char content_length_c_str[1024];
9038 snprintf(content_length_c_str, sizeof(content_length_c_str), "%" PRIu64, content_length);
9039
9040 struct aws_http_header content_length_header = {
9041 .name = g_content_length_header_name,
9042 .value = aws_byte_cursor_from_c_str(content_length_c_str),
9043 };
9044
9045 if (aws_http_message_add_header(message, content_length_header)) {
9046 goto error_clean_up_message;
9047 }
9048
9049 if (aws_http_message_set_request_method(message, aws_http_method_put)) {
9050 goto error_clean_up_message;
9051 }
9052
9053 aws_http_message_set_body_stream(message, body_stream);
9054
9055 return message;
9056
9057 error_clean_up_message:
9058
9059 if (message != NULL) {
9060 aws_http_message_release(message);
9061 message = NULL;
9062 }
9063
9064 return NULL;
9065 }
9066
9067 struct put_object_pause_resume_test_data {
9068 struct aws_mutex mutex;
9069 struct aws_condition_variable c_var;
9070
9071 /* execution of the test meta request completed */
9072 bool execution_completed;
9073
9074 /* accumulator of amount of bytes uploaded */
9075 struct aws_atomic_var total_bytes_uploaded;
9076
9077 /* the offset where upload should be paused */
9078 struct aws_atomic_var request_pause_offset;
9079
9080 struct aws_atomic_var pause_requested;
9081
9082 struct aws_atomic_var pause_result;
9083
9084 /* the persistable state of the paused request */
9085 struct aws_atomic_var persistable_state_ptr;
9086
9087 int meta_request_error_code;
9088 int response_status_code;
9089
9090 /* (Optional) content_length to send. If not set, use the length of the input stream. */
9091 uint64_t content_length;
9092 };
9093
9094 static void s_put_pause_resume_meta_request_finish(
9095 struct aws_s3_meta_request *meta_request,
9096 const struct aws_s3_meta_request_result *meta_request_result,
9097 void *user_data) {
9098 (void)meta_request;
9099 struct aws_s3_tester *tester = user_data;
9100 struct put_object_pause_resume_test_data *test_data = tester->user_data;
9101
9102 /* if error response body is available, dump it to test result to help investigation of failed tests */
9103 if (meta_request_result->error_response_body != NULL && meta_request_result->error_response_body->len > 0) {
9104 AWS_LOGF_ERROR(
9105 AWS_LS_S3_GENERAL,
9106 "Response error body: %.*s",
9107 (int)meta_request_result->error_response_body->len,
9108 meta_request_result->error_response_body->buffer);
9109 }
9110
9111 aws_mutex_lock(&test_data->mutex);
9112 test_data->meta_request_error_code = meta_request_result->error_code;
9113 test_data->response_status_code = meta_request_result->response_status;
9114 test_data->execution_completed = true;
9115 aws_mutex_unlock(&test_data->mutex);
9116 aws_condition_variable_notify_one(&test_data->c_var);
9117 }
9118
9119 static bool s_put_pause_resume_test_completion_predicate(void *arg) {
9120 struct put_object_pause_resume_test_data *test_data = arg;
9121 return test_data->execution_completed;
9122 }
9123
9124 /* Patched version of aws_s3_meta_request_vtable->finished_request() for pause/resume tests.
9125 * It can pause the meta-request immediately after a part completes.
9126 * We use a patched vtable, instead of the progress_callback, because
9127 * the progress_callback fires on another thread, which might be too late to
9128 * prevent more parts from being sent. */
9129 static void s_meta_request_finished_request_patched_for_pause_resume_tests(
9130 struct aws_s3_meta_request *meta_request,
9131 struct aws_s3_request *request,
9132 int error_code) {
9133 AWS_ASSERT(meta_request);
9134 struct aws_s3_tester *tester = meta_request->user_data;
9135 struct put_object_pause_resume_test_data *test_data = tester->user_data;
9136 AWS_ASSERT(test_data);
9137
9138 if ((error_code == AWS_ERROR_SUCCESS) && (meta_request->type == AWS_S3_META_REQUEST_TYPE_PUT_OBJECT) &&
9139 (request->request_tag == AWS_S3_AUTO_RANGED_PUT_REQUEST_TAG_PART)) {
9140
9141 if (!request->is_noop) {
9142 /* If the request is noop, we are not really uploading the part */
9143 aws_atomic_fetch_add(&test_data->total_bytes_uploaded, request->request_body.len);
9144 }
9145
9146 size_t total_bytes_uploaded = aws_atomic_load_int(&test_data->total_bytes_uploaded);
9147 uint64_t offset_to_pause = aws_atomic_load_int(&test_data->request_pause_offset);
9148
9149 if (total_bytes_uploaded >= offset_to_pause) {
9150 /* offset of the upload at which we should pause was reached. let's pause the upload */
9151 /* if the meta request has already been paused previously, do nothing. */
9152 size_t expected = false;
9153 bool request_pause = aws_atomic_compare_exchange_int(&test_data->pause_requested, &expected, true);
9154 if (request_pause) {
9155 struct aws_s3_meta_request_resume_token *resume_token = NULL;
9156 int pause_result = aws_s3_meta_request_pause(meta_request, &resume_token);
9157 struct aws_byte_cursor upload_id = aws_s3_meta_request_resume_token_upload_id(resume_token);
9158 /* Make Sure we have upload ID */
9159 AWS_FATAL_ASSERT(aws_byte_cursor_eq_c_str(&upload_id, "") == false);
9160 aws_atomic_store_int(&test_data->pause_result, pause_result);
9161 aws_atomic_store_ptr(&test_data->persistable_state_ptr, resume_token);
9162 }
9163 }
9164 }
9165
9166 /* Continue with original vtable function... */
9167 struct aws_s3_meta_request_vtable *original_meta_request_vtable =
9168 aws_s3_tester_get_meta_request_vtable_patch(tester, 0)->original_vtable;
9169
9170 original_meta_request_vtable->finished_request(meta_request, request, error_code);
9171 }
9172
9173 static struct aws_s3_meta_request *s_meta_request_factory_patch_for_pause_resume_tests(
9174 struct aws_s3_client *client,
9175 const struct aws_s3_meta_request_options *options) {
9176 AWS_ASSERT(client != NULL);
9177 struct aws_s3_tester *tester = client->shutdown_callback_user_data;
9178 AWS_ASSERT(tester != NULL);
9179
9180 struct aws_s3_client_vtable *original_client_vtable =
9181 aws_s3_tester_get_client_vtable_patch(tester, 0)->original_vtable;
9182
9183 struct aws_s3_meta_request *meta_request = original_client_vtable->meta_request_factory(client, options);
9184
9185 struct aws_s3_meta_request_vtable *patched_meta_request_vtable =
9186 aws_s3_tester_patch_meta_request_vtable(tester, meta_request, NULL);
9187 patched_meta_request_vtable->finished_request = s_meta_request_finished_request_patched_for_pause_resume_tests;
9188
9189 return meta_request;
9190 }
9191
9192 /* total length of the object to simulate for upload */
9193 static const size_t s_pause_resume_object_length_128MB = 128 * 1024 * 1024;
9194
9195 /* this runs when a RESUMED upload is about to successfully complete */
9196 static int s_pause_resume_upload_review_callback(
9197 struct aws_s3_meta_request *meta_request,
9198 const struct aws_s3_upload_review *review,
9199 void *user_data) {
9200 (void)meta_request;
9201 (void)user_data;
9202 struct aws_allocator *allocator = meta_request->allocator;
9203
9204 /* A bit hacky, but stream the same data that the test always uploads, and ensure the checksums match */
9205
9206 struct aws_input_stream *reread_stream =
9207 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9208
9209 for (size_t part_index = 0; part_index < review->part_count; ++part_index) {
9210 const struct aws_s3_upload_part_review *part_review = &review->part_array[part_index];
9211 struct aws_byte_buf reread_part_buf;
9212 ASSERT_TRUE(part_review->size <= SIZE_MAX);
9213 aws_byte_buf_init(&reread_part_buf, allocator, (size_t)part_review->size);
9214 ASSERT_SUCCESS(aws_input_stream_read(reread_stream, &reread_part_buf));
9215
9216 /* part sizes should match */
9217 ASSERT_UINT_EQUALS(part_review->size, reread_part_buf.len);
9218
9219 if (review->checksum_algorithm != AWS_SCA_NONE) {
9220 struct aws_byte_cursor reread_part_cursor = aws_byte_cursor_from_buf(&reread_part_buf);
9221
9222 struct aws_byte_buf checksum_buf;
9223 aws_byte_buf_init(&checksum_buf, allocator, 128);
9224 ASSERT_SUCCESS(
9225 aws_checksum_compute(allocator, review->checksum_algorithm, &reread_part_cursor, &checksum_buf));
9226 struct aws_byte_cursor checksum_cursor = aws_byte_cursor_from_buf(&checksum_buf);
9227
9228 struct aws_byte_buf encoded_checksum_buf;
9229 aws_byte_buf_init(&encoded_checksum_buf, allocator, 128);
9230
9231 ASSERT_SUCCESS(aws_base64_encode(&checksum_cursor, &encoded_checksum_buf));
9232
9233 /* part checksums should match */
9234 ASSERT_BIN_ARRAYS_EQUALS(
9235 encoded_checksum_buf.buffer,
9236 encoded_checksum_buf.len,
9237 part_review->checksum.ptr,
9238 part_review->checksum.len);
9239
9240 aws_byte_buf_clean_up(&checksum_buf);
9241 aws_byte_buf_clean_up(&encoded_checksum_buf);
9242 }
9243
9244 aws_byte_buf_clean_up(&reread_part_buf);
9245 }
9246
9247 aws_input_stream_release(reread_stream);
9248
9249 return AWS_OP_SUCCESS;
9250 }
9251
9252 static int s_pause_resume_receive_body_callback(
9253 struct aws_s3_meta_request *meta_request,
9254 const struct aws_byte_cursor *body,
9255 uint64_t range_start,
9256 void *user_data) {
9257 (void)meta_request;
9258 (void)range_start;
9259 (void)user_data;
9260
9261 // TODO: this is a bit hacky, as it will try to compare every partial get result we receive to the input stream.
9262 // Something better?
9263
9264 struct aws_input_stream *input_stream =
9265 aws_s3_test_input_stream_new(meta_request->allocator, s_pause_resume_object_length_128MB);
9266
9267 struct aws_byte_buf buf;
9268 aws_byte_buf_init(&buf, meta_request->allocator, (size_t)range_start);
9269 aws_input_stream_read(input_stream, &buf);
9270
9271 aws_byte_buf_clean_up(&buf);
9272 aws_byte_buf_init(&buf, meta_request->allocator, body->len);
9273 aws_input_stream_read(input_stream, &buf);
9274
9275 struct aws_byte_cursor input_cur = aws_byte_cursor_from_buf(&buf);
9276
9277 bool body_matches_expected = aws_byte_cursor_eq(&input_cur, body);
9278
9279 aws_input_stream_destroy(input_stream);
9280 aws_byte_buf_clean_up(&buf);
9281
9282 ASSERT_TRUE(body_matches_expected);
9283 return AWS_OP_SUCCESS;
9284 }
9285
9286 static int s_test_s3_put_pause_resume_helper(
9287 struct aws_s3_tester *tester,
9288 struct aws_allocator *allocator,
9289 void *ctx,
9290 struct put_object_pause_resume_test_data *test_data,
9291 struct aws_byte_cursor destination_key,
9292 struct aws_input_stream *upload_body_stream,
9293 struct aws_s3_meta_request_resume_token *resume_state,
9294 enum aws_s3_checksum_algorithm checksum_algorithm,
9295 int expected_error_code,
9296 int expected_response_status) {
9297 (void)ctx;
9298
9299 struct aws_s3_client_config client_config;
9300 AWS_ZERO_STRUCT(client_config);
9301
9302 if (resume_state == NULL) {
9303 /* If we're going to cancel this operation, limit the client to 1 HTTP connection.
9304 * That way, we don't end up "cancelling" but all the parts actually
9305 * succeed anyway on other connections */
9306 client_config.max_active_connections_override = 1;
9307 }
9308
9309 ASSERT_SUCCESS(aws_s3_tester_bind_client(
9310 tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
9311
9312 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
9313
9314 struct aws_s3_client_vtable *patched_client_vtable = aws_s3_tester_patch_client_vtable(tester, client, NULL);
9315 patched_client_vtable->meta_request_factory = s_meta_request_factory_patch_for_pause_resume_tests;
9316
9317 struct aws_byte_cursor destination_bucket = g_test_bucket_name;
9318
9319 char endpoint[1024];
9320 snprintf(
9321 endpoint,
9322 sizeof(endpoint),
9323 "%.*s.s3.%s.amazonaws.com",
9324 (int)destination_bucket.len,
9325 destination_bucket.ptr,
9326 g_test_s3_region.ptr);
9327
9328 /* creates a PutObject request */
9329 int64_t content_length = test_data->content_length;
9330 if (content_length == 0) {
9331 /* If not set, use the length of the input stream */
9332 aws_input_stream_get_length(upload_body_stream, &content_length);
9333 }
9334 struct aws_http_message *message = s_put_object_request_new(
9335 allocator, destination_key, aws_byte_cursor_from_c_str(endpoint), upload_body_stream, content_length);
9336
9337 test_data->c_var = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT;
9338 aws_mutex_init(&test_data->mutex);
9339 test_data->execution_completed = false;
9340
9341 tester->user_data = test_data;
9342
9343 struct aws_s3_checksum_config checksum_config = {
9344 .checksum_algorithm = checksum_algorithm,
9345 .location = checksum_algorithm == AWS_SCA_NONE ? AWS_SCL_NONE : AWS_SCL_TRAILER,
9346 };
9347
9348 struct aws_s3_meta_request_options meta_request_options = {
9349 .user_data = tester,
9350 .body_callback = NULL,
9351 .signing_config = client_config.signing_config,
9352 .finish_callback = s_put_pause_resume_meta_request_finish,
9353 .headers_callback = NULL,
9354 .upload_review_callback = s_pause_resume_upload_review_callback,
9355 .message = message,
9356 .shutdown_callback = NULL,
9357 .resume_token = NULL,
9358 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
9359 .checksum_config = &checksum_config,
9360 };
9361
9362 if (resume_state) {
9363 meta_request_options.resume_token = resume_state;
9364 }
9365
9366 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &meta_request_options);
9367 ASSERT_NOT_NULL(meta_request);
9368
9369 /* wait completion of the meta request */
9370 aws_mutex_lock(&test_data->mutex);
9371 aws_condition_variable_wait_pred(
9372 &test_data->c_var, &test_data->mutex, s_put_pause_resume_test_completion_predicate, test_data);
9373 aws_mutex_unlock(&test_data->mutex);
9374
9375 /* assert error_code and response_status_code */
9376 ASSERT_INT_EQUALS(expected_error_code, test_data->meta_request_error_code);
9377 ASSERT_INT_EQUALS(expected_response_status, test_data->response_status_code);
9378
9379 aws_s3_meta_request_release(meta_request);
9380 aws_mutex_clean_up(&test_data->mutex);
9381 aws_http_message_destroy(message);
9382
9383 /* release this client with its crazy patched vtables */
9384 client = aws_s3_client_release(client);
9385 aws_s3_tester_wait_for_client_shutdown(tester);
9386 tester->bound_to_client = false;
9387
9388 if (expected_error_code == AWS_ERROR_SUCCESS) {
9389 /* get the file and verify it matches what we uploaded */
9390 struct aws_s3_tester_meta_request_options options = {
9391 .allocator = allocator,
9392 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
9393 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
9394 .body_callback = s_pause_resume_receive_body_callback,
9395 .get_options =
9396 {
9397 .object_path = destination_key,
9398 },
9399 };
9400
9401 struct aws_s3_meta_request_test_results results;
9402 aws_s3_meta_request_test_results_init(&results, allocator);
9403
9404 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(tester, &options, &results));
9405 aws_s3_meta_request_test_results_clean_up(&results);
9406 }
9407
9408 return 0;
9409 }
9410
9411 AWS_TEST_CASE(test_s3_put_pause_resume_happy_path, s_test_s3_put_pause_resume_happy_path)
9412 static int s_test_s3_put_pause_resume_happy_path(struct aws_allocator *allocator, void *ctx) {
9413 struct aws_s3_tester tester;
9414 AWS_ZERO_STRUCT(tester);
9415 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
9416
9417 struct aws_byte_cursor destination_key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload/test_pause_resume.txt");
9418
9419 struct put_object_pause_resume_test_data test_data;
9420 AWS_ZERO_STRUCT(test_data);
9421
9422 /* initialize the atomic members */
9423 aws_atomic_init_int(&test_data.total_bytes_uploaded, 0);
9424 aws_atomic_init_int(&test_data.request_pause_offset, 0);
9425 aws_atomic_init_int(&test_data.pause_requested, false);
9426 aws_atomic_init_int(&test_data.pause_result, 0);
9427 aws_atomic_init_ptr(&test_data.persistable_state_ptr, NULL);
9428
9429 /* offset of the upload where pause should be requested by test client */
9430 aws_atomic_store_int(&test_data.request_pause_offset, 8 * 1024 * 1024);
9431
9432 /* stream used to initiate upload */
9433 struct aws_input_stream *initial_upload_stream =
9434 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9435
9436 /* starts the upload request that will be paused */
9437 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9438 &tester,
9439 allocator,
9440 ctx,
9441 &test_data,
9442 destination_key,
9443 initial_upload_stream,
9444 NULL,
9445 AWS_SCA_CRC32,
9446 AWS_ERROR_S3_PAUSED,
9447 0));
9448
9449 aws_input_stream_destroy(initial_upload_stream);
9450
9451 /* new stream used to resume upload. it begins at the offset specified in the persistable state */
9452 struct aws_input_stream *resume_upload_stream =
9453 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9454 struct aws_s3_meta_request_resume_token *persistable_state = aws_atomic_load_ptr(&test_data.persistable_state_ptr);
9455
9456 size_t bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9457
9458 /* offset where pause should be requested is set to a value greater than content length,
9459 * to avoid any more pause when resuming the upload */
9460 aws_atomic_store_int(&test_data.request_pause_offset, s_pause_resume_object_length_128MB * 2);
9461 aws_atomic_store_int(&test_data.total_bytes_uploaded, 0);
9462
9463 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9464 &tester,
9465 allocator,
9466 ctx,
9467 &test_data,
9468 destination_key,
9469 resume_upload_stream,
9470 persistable_state,
9471 AWS_SCA_CRC32,
9472 AWS_ERROR_SUCCESS,
9473 AWS_HTTP_STATUS_CODE_200_OK));
9474
9475 bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9476
9477 /* bytes uploaded is smaller since we are skipping uploaded parts */
9478 ASSERT_TRUE(bytes_uploaded < s_pause_resume_object_length_128MB);
9479
9480 aws_s3_meta_request_resume_token_release(persistable_state);
9481 aws_input_stream_destroy(resume_upload_stream);
9482 aws_s3_tester_clean_up(&tester);
9483
9484 return AWS_OP_SUCCESS;
9485 }
9486
9487 AWS_TEST_CASE(test_s3_put_pause_resume_all_parts_done, s_test_s3_put_pause_resume_all_parts_done)
9488 static int s_test_s3_put_pause_resume_all_parts_done(struct aws_allocator *allocator, void *ctx) {
9489 struct aws_s3_tester tester;
9490 AWS_ZERO_STRUCT(tester);
9491 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
9492
9493 struct aws_byte_cursor destination_key =
9494 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload/test_pause_resume_all_parts_done.txt");
9495
9496 struct put_object_pause_resume_test_data test_data;
9497 AWS_ZERO_STRUCT(test_data);
9498
9499 /* initialize the atomic members */
9500 aws_atomic_init_int(&test_data.total_bytes_uploaded, 0);
9501 aws_atomic_init_int(&test_data.request_pause_offset, 0);
9502 aws_atomic_init_int(&test_data.pause_requested, false);
9503 aws_atomic_init_int(&test_data.pause_result, 0);
9504 aws_atomic_init_ptr(&test_data.persistable_state_ptr, NULL);
9505
9506 /* offset of the upload where pause should be requested by test client */
9507 aws_atomic_store_int(&test_data.request_pause_offset, 128 * 1024 * 1024);
9508
9509 /* stream used to initiate upload */
9510 struct aws_input_stream *initial_upload_stream =
9511 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9512
9513 /* starts the upload request that will be paused */
9514 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9515 &tester,
9516 allocator,
9517 ctx,
9518 &test_data,
9519 destination_key,
9520 initial_upload_stream,
9521 NULL,
9522 AWS_SCA_NONE,
9523 AWS_ERROR_S3_PAUSED,
9524 0));
9525
9526 aws_input_stream_destroy(initial_upload_stream);
9527
9528 /* new stream used to resume upload. it begins at the offset specified in the persistable state */
9529 struct aws_input_stream *resume_upload_stream =
9530 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9531 struct aws_s3_meta_request_resume_token *persistable_state = aws_atomic_load_ptr(&test_data.persistable_state_ptr);
9532
9533 AWS_LOGF_INFO(AWS_LS_S3_GENERAL, "Persistable state %p", persistable_state);
9534
9535 size_t bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9536
9537 /* offset where pause should be requested is set to a value greater than content length,
9538 * to avoid any more pause when resuming the upload */
9539 aws_atomic_store_int(&test_data.request_pause_offset, s_pause_resume_object_length_128MB * 2);
9540 aws_atomic_store_int(&test_data.total_bytes_uploaded, 0);
9541
9542 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9543 &tester,
9544 allocator,
9545 ctx,
9546 &test_data,
9547 destination_key,
9548 resume_upload_stream,
9549 persistable_state,
9550 AWS_SCA_NONE,
9551 AWS_ERROR_SUCCESS,
9552 AWS_HTTP_STATUS_CODE_200_OK));
9553
9554 bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9555
9556 /* bytes uploaded is smaller since we are skipping uploaded parts */
9557 ASSERT_INT_EQUALS(0, bytes_uploaded);
9558
9559 aws_s3_meta_request_resume_token_release(persistable_state);
9560 aws_input_stream_destroy(resume_upload_stream);
9561 aws_s3_tester_clean_up(&tester);
9562
9563 return AWS_OP_SUCCESS;
9564 }
9565
9566 AWS_TEST_CASE(test_s3_put_pause_resume_invalid_resume_data, s_test_s3_put_pause_resume_invalid_resume_data)
9567 static int s_test_s3_put_pause_resume_invalid_resume_data(struct aws_allocator *allocator, void *ctx) {
9568 struct aws_s3_tester tester;
9569 AWS_ZERO_STRUCT(tester);
9570 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
9571
9572 struct aws_byte_cursor destination_key =
9573 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload/test_pause_resume_resume_data.txt");
9574
9575 struct put_object_pause_resume_test_data test_data;
9576 AWS_ZERO_STRUCT(test_data);
9577
9578 /* initialize the atomic members */
9579 aws_atomic_init_int(&test_data.total_bytes_uploaded, 0);
9580 aws_atomic_init_int(&test_data.request_pause_offset, 0);
9581 aws_atomic_init_int(&test_data.pause_requested, false);
9582 aws_atomic_init_int(&test_data.pause_result, 0);
9583 aws_atomic_init_ptr(&test_data.persistable_state_ptr, NULL);
9584
9585 /* offset of the upload where pause should be requested by test client */
9586 aws_atomic_store_int(&test_data.request_pause_offset, 8 * 1024 * 1024);
9587
9588 /* stream used to initiate upload */
9589 struct aws_input_stream *initial_upload_stream =
9590 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9591
9592 /* starts the upload request that will be paused */
9593 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9594 &tester,
9595 allocator,
9596 ctx,
9597 &test_data,
9598 destination_key,
9599 initial_upload_stream,
9600 NULL,
9601 AWS_SCA_CRC32,
9602 AWS_ERROR_S3_PAUSED,
9603 0));
9604
9605 aws_input_stream_destroy(initial_upload_stream);
9606
9607 /* new stream used to resume upload. it begins at the offset specified in the persistable state */
9608 struct aws_input_stream *resume_upload_stream = aws_s3_test_input_stream_new_with_value_type(
9609 allocator, s_pause_resume_object_length_128MB, TEST_STREAM_VALUE_2);
9610
9611 struct aws_s3_meta_request_resume_token *persistable_state = aws_atomic_load_ptr(&test_data.persistable_state_ptr);
9612
9613 size_t bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9614
9615 /* offset where pause should be requested is set to a value greater than content length,
9616 * to avoid any more pause when resuming the upload */
9617 aws_atomic_store_int(&test_data.request_pause_offset, s_pause_resume_object_length_128MB * 2);
9618 aws_atomic_store_int(&test_data.total_bytes_uploaded, 0);
9619
9620 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9621 &tester,
9622 allocator,
9623 ctx,
9624 &test_data,
9625 destination_key,
9626 resume_upload_stream,
9627 persistable_state,
9628 AWS_SCA_CRC32,
9629 AWS_ERROR_S3_RESUMED_PART_CHECKSUM_MISMATCH,
9630 0));
9631
9632 bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9633
9634 /* bytes uploaded is smaller since we are skipping uploaded parts */
9635 ASSERT_TRUE(bytes_uploaded < s_pause_resume_object_length_128MB);
9636
9637 aws_s3_meta_request_resume_token_release(persistable_state);
9638 aws_input_stream_destroy(resume_upload_stream);
9639 aws_s3_tester_clean_up(&tester);
9640
9641 return AWS_OP_SUCCESS;
9642 }
9643
9644 AWS_TEST_CASE(test_s3_put_pause_resume_invalid_resume_stream, s_test_s3_put_pause_resume_invalid_resume_stream)
9645 static int s_test_s3_put_pause_resume_invalid_resume_stream(struct aws_allocator *allocator, void *ctx) {
9646 struct aws_s3_tester tester;
9647 AWS_ZERO_STRUCT(tester);
9648 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
9649
9650 struct aws_byte_cursor destination_key =
9651 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload/test_pause_resume_bad_resume_stream.txt");
9652
9653 struct put_object_pause_resume_test_data test_data;
9654 AWS_ZERO_STRUCT(test_data);
9655
9656 /* initialize the atomic members */
9657 aws_atomic_init_int(&test_data.total_bytes_uploaded, 0);
9658 aws_atomic_init_int(&test_data.request_pause_offset, 0);
9659 aws_atomic_init_int(&test_data.pause_requested, false);
9660 aws_atomic_init_int(&test_data.pause_result, 0);
9661 aws_atomic_init_ptr(&test_data.persistable_state_ptr, NULL);
9662
9663 /* offset of the upload where pause should be requested by test client */
9664 aws_atomic_store_int(&test_data.request_pause_offset, 8 * 1024 * 1024);
9665
9666 /* stream used to initiate upload */
9667 struct aws_input_stream *initial_upload_stream =
9668 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9669
9670 /* starts the upload request that will be paused */
9671 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9672 &tester,
9673 allocator,
9674 ctx,
9675 &test_data,
9676 destination_key,
9677 initial_upload_stream,
9678 NULL,
9679 AWS_SCA_CRC32,
9680 AWS_ERROR_S3_PAUSED,
9681 0));
9682
9683 aws_input_stream_release(initial_upload_stream);
9684
9685 /* a bad input stream to resume from */
9686 struct aws_input_stream_tester_options stream_options = {
9687 .autogen_length = s_pause_resume_object_length_128MB,
9688 .fail_on_nth_read = 1,
9689 .fail_with_error_code = AWS_IO_STREAM_READ_FAILED,
9690 };
9691 struct aws_input_stream *resume_upload_stream = aws_input_stream_new_tester(allocator, &stream_options);
9692
9693 struct aws_s3_meta_request_resume_token *persistable_state = aws_atomic_load_ptr(&test_data.persistable_state_ptr);
9694
9695 size_t bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9696
9697 /* offset where pause should be requested is set to a value greater than content length,
9698 * to avoid any more pause when resuming the upload */
9699 aws_atomic_store_int(&test_data.request_pause_offset, s_pause_resume_object_length_128MB * 2);
9700 aws_atomic_store_int(&test_data.total_bytes_uploaded, 0);
9701
9702 /* Each failed resume will delete the MPU */
9703 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9704 &tester,
9705 allocator,
9706 ctx,
9707 &test_data,
9708 destination_key,
9709 resume_upload_stream,
9710 persistable_state,
9711 AWS_SCA_CRC32,
9712 AWS_IO_STREAM_READ_FAILED,
9713 0));
9714
9715 bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9716
9717 /* resume didn't read any bytes because the bad input stream failed to read. */
9718 ASSERT_TRUE(bytes_uploaded == 0);
9719
9720 aws_s3_meta_request_resume_token_release(persistable_state);
9721 aws_input_stream_release(resume_upload_stream);
9722 aws_s3_tester_clean_up(&tester);
9723
9724 return AWS_OP_SUCCESS;
9725 }
9726
9727 AWS_TEST_CASE(test_s3_put_pause_resume_invalid_content_length, s_test_s3_put_pause_resume_invalid_content_length)
9728 static int s_test_s3_put_pause_resume_invalid_content_length(struct aws_allocator *allocator, void *ctx) {
9729 struct aws_s3_tester tester;
9730 AWS_ZERO_STRUCT(tester);
9731 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
9732
9733 struct aws_byte_cursor destination_key =
9734 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload/test_pause_resume_bad_resume_stream.txt");
9735
9736 struct put_object_pause_resume_test_data test_data;
9737 AWS_ZERO_STRUCT(test_data);
9738
9739 /* initialize the atomic members */
9740 aws_atomic_init_int(&test_data.total_bytes_uploaded, 0);
9741 aws_atomic_init_int(&test_data.request_pause_offset, 0);
9742 aws_atomic_init_int(&test_data.pause_requested, false);
9743 aws_atomic_init_int(&test_data.pause_result, 0);
9744 aws_atomic_init_ptr(&test_data.persistable_state_ptr, NULL);
9745
9746 /* offset of the upload where pause should be requested by test client */
9747 aws_atomic_store_int(&test_data.request_pause_offset, 8 * 1024 * 1024);
9748 test_data.content_length = s_pause_resume_object_length_128MB;
9749
9750 /* stream used to initiate upload */
9751 struct aws_input_stream *initial_upload_stream =
9752 aws_s3_test_input_stream_new(allocator, s_pause_resume_object_length_128MB);
9753
9754 /* starts the upload request that will be paused */
9755 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9756 &tester,
9757 allocator,
9758 ctx,
9759 &test_data,
9760 destination_key,
9761 initial_upload_stream,
9762 NULL,
9763 AWS_SCA_CRC32,
9764 AWS_ERROR_S3_PAUSED,
9765 0));
9766
9767 aws_input_stream_release(initial_upload_stream);
9768
9769 /* a small input stream to resume with */
9770 struct aws_input_stream *resume_upload_stream = aws_s3_test_input_stream_new(allocator, 8 * 1024 * 1024);
9771
9772 struct aws_s3_meta_request_resume_token *persistable_state = aws_atomic_load_ptr(&test_data.persistable_state_ptr);
9773
9774 size_t bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9775
9776 /* offset where pause should be requested is set to a value greater than content length,
9777 * to avoid any more pause when resuming the upload */
9778 aws_atomic_store_int(&test_data.request_pause_offset, s_pause_resume_object_length_128MB * 2);
9779 aws_atomic_store_int(&test_data.total_bytes_uploaded, 0);
9780
9781 /* Each failed resume will delete the MPU */
9782 ASSERT_SUCCESS(s_test_s3_put_pause_resume_helper(
9783 &tester,
9784 allocator,
9785 ctx,
9786 &test_data,
9787 destination_key,
9788 resume_upload_stream,
9789 persistable_state,
9790 AWS_SCA_CRC32,
9791 AWS_ERROR_S3_INCORRECT_CONTENT_LENGTH,
9792 0));
9793
9794 bytes_uploaded = aws_atomic_load_int(&test_data.total_bytes_uploaded);
9795
9796 /* resume didn't read any bytes because the bad input stream failed to read. */
9797 ASSERT_TRUE(bytes_uploaded == 0);
9798
9799 aws_s3_meta_request_resume_token_release(persistable_state);
9800 aws_input_stream_release(resume_upload_stream);
9801 aws_s3_tester_clean_up(&tester);
9802
9803 return AWS_OP_SUCCESS;
9804 }
9805
9806 /* Most basic test of the upload_review_callback */
9807 AWS_TEST_CASE(test_s3_upload_review, s_test_s3_upload_review)
9808 static int s_test_s3_upload_review(struct aws_allocator *allocator, void *ctx) {
9809 (void)ctx;
9810 struct aws_s3_meta_request_test_results test_results;
9811 aws_s3_meta_request_test_results_init(&test_results, allocator);
9812
9813 struct aws_s3_tester_meta_request_options put_options = {
9814 .allocator = allocator,
9815 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
9816 .checksum_algorithm = AWS_SCA_CRC32,
9817 .put_options =
9818 {
9819 .object_path_override = aws_byte_cursor_from_c_str("/upload/review_10MB_CRC32.txt"),
9820 .object_size_mb = 10,
9821 },
9822 };
9823
9824 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
9825
9826 /* The tester always registers an upload_review_callback.
9827 * Check that it got what we expect */
9828 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
9829 ASSERT_UINT_EQUALS(2, test_results.upload_review.part_count);
9830 ASSERT_UINT_EQUALS(MB_TO_BYTES(8), test_results.upload_review.part_sizes_array[0]);
9831 ASSERT_UINT_EQUALS(MB_TO_BYTES(10) - MB_TO_BYTES(8), test_results.upload_review.part_sizes_array[1]);
9832 ASSERT_INT_EQUALS(AWS_SCA_CRC32, test_results.upload_review.checksum_algorithm);
9833 ASSERT_STR_EQUALS("9J8ZNA==", aws_string_c_str(test_results.upload_review.part_checksums_array[0]));
9834 ASSERT_STR_EQUALS("BNjxzQ==", aws_string_c_str(test_results.upload_review.part_checksums_array[1]));
9835
9836 aws_s3_meta_request_test_results_clean_up(&test_results);
9837 return 0;
9838 }
9839
9840 /*
9841 * Make sure that upload reads data in sequential order.
9842 * Specifically this tests a case of mem pool hitting allocation limit for regular sized part,
9843 * but still having enough mem for last part. for this case mem pool should not allocate last part
9844 * until all regular parts are done. i.e. ordering should be fifo.
9845 */
9846 AWS_TEST_CASE(test_s3_upload_in_order_review, s_test_s3_upload_in_order_review)
9847 static int s_test_s3_upload_in_order_review(struct aws_allocator *allocator, void *ctx) {
9848 (void)ctx;
9849 struct aws_s3_meta_request_test_results test_results;
9850 aws_s3_meta_request_test_results_init(&test_results, allocator);
9851
9852 struct aws_s3_tester_client_options client_options = {
9853 .part_size = MB_TO_BYTES(250), .memory_limit_in_bytes = GB_TO_BYTES(1)};
9854
9855 struct aws_s3_tester_meta_request_options put_options = {
9856 .allocator = allocator,
9857 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
9858 .checksum_algorithm = AWS_SCA_CRC32,
9859 .client_options = &client_options,
9860 .put_options =
9861 {
9862 .object_path_override = aws_byte_cursor_from_c_str("/upload/review_1040MB_CRC32.txt"),
9863 .object_size_mb = 1040,
9864 },
9865 };
9866
9867 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
9868
9869 /* The tester always registers an upload_review_callback.
9870 * Check that it got what we expect */
9871 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
9872 ASSERT_UINT_EQUALS(5, test_results.upload_review.part_count);
9873 ASSERT_UINT_EQUALS(MB_TO_BYTES(250), test_results.upload_review.part_sizes_array[0]);
9874 ASSERT_UINT_EQUALS(MB_TO_BYTES(250), test_results.upload_review.part_sizes_array[1]);
9875 ASSERT_UINT_EQUALS(MB_TO_BYTES(250), test_results.upload_review.part_sizes_array[2]);
9876 ASSERT_UINT_EQUALS(MB_TO_BYTES(250), test_results.upload_review.part_sizes_array[3]);
9877 ASSERT_UINT_EQUALS(MB_TO_BYTES(40), test_results.upload_review.part_sizes_array[4]);
9878 ASSERT_INT_EQUALS(AWS_SCA_CRC32, test_results.upload_review.checksum_algorithm);
9879 ASSERT_STR_EQUALS("RYd3Aw==", aws_string_c_str(test_results.upload_review.part_checksums_array[0]));
9880 ASSERT_STR_EQUALS("v5Or2w==", aws_string_c_str(test_results.upload_review.part_checksums_array[1]));
9881 ASSERT_STR_EQUALS("9kntaA==", aws_string_c_str(test_results.upload_review.part_checksums_array[2]));
9882 ASSERT_STR_EQUALS("w6wh3A==", aws_string_c_str(test_results.upload_review.part_checksums_array[3]));
9883 ASSERT_STR_EQUALS("jwpwGA==", aws_string_c_str(test_results.upload_review.part_checksums_array[4]));
9884
9885 aws_s3_meta_request_test_results_clean_up(&test_results);
9886 return 0;
9887 }
9888
9889 struct aws_byte_buf s_manual_ticket_claim(struct aws_s3_buffer_ticket *ticket) {
9890 return aws_byte_buf_from_empty_array((uint8_t *)ticket->impl, MB_TO_BYTES(8));
9891 }
9892
9893 struct aws_s3_buffer_ticket_vtable s_manual_ticket_vtable = {
9894 .claim = s_manual_ticket_claim,
9895 };
9896
9897 struct s_manual_pool_impl {
9898 struct aws_allocator *allocator;
9899
9900 struct aws_future_s3_buffer_ticket *futures[10];
9901 struct aws_s3_buffer_ticket tickets[10];
9902 size_t futures_count;
9903
9904 uint8_t buffers[10][8 * 1024 * 1024];
9905 };
9906
9907 static void s_aws_ticket_wrapper_destroy(void *data) {
9908 (void)data;
9909 }
9910
9911 static struct aws_future_s3_buffer_ticket *s_manual_pool_reserve(
9912 struct aws_s3_buffer_pool *pool,
9913 struct aws_s3_buffer_pool_reserve_meta meta) {
9914 (void)meta;
9915
9916 struct s_manual_pool_impl *pool_impl = (struct s_manual_pool_impl *)pool->impl;
9917
9918 struct aws_future_s3_buffer_ticket *future = aws_future_s3_buffer_ticket_new(pool_impl->allocator);
9919 aws_future_s3_buffer_ticket_acquire(future);
9920 pool_impl->futures[pool_impl->futures_count] = future;
9921
9922 pool_impl->futures_count++;
9923
9924 if (pool_impl->futures_count == 5) {
9925 for (size_t i = pool_impl->futures_count; i > 0; i--) {
9926 struct aws_s3_buffer_ticket *ticket = &pool_impl->tickets[i - 1];
9927 pool_impl->tickets[i - 1].impl = pool_impl->buffers[i - 1];
9928 pool_impl->tickets[i - 1].vtable = &s_manual_ticket_vtable;
9929 aws_ref_count_init(
9930 &pool_impl->tickets[i - 1].ref_count,
9931 &pool_impl->tickets[i - 1],
9932 (aws_simple_completion_callback *)s_aws_ticket_wrapper_destroy);
9933
9934 aws_future_s3_buffer_ticket_set_result_by_move(pool_impl->futures[i - 1], &ticket);
9935 }
9936 }
9937
9938 return future;
9939 }
9940
9941 static void s_manual_pool_trim(struct aws_s3_buffer_pool *pool) {
9942 (void)pool;
9943 }
9944
9945 static struct aws_s3_buffer_pool_vtable s_manual_pool_vtable = {
9946 .reserve = s_manual_pool_reserve,
9947 .trim = s_manual_pool_trim,
9948 };
9949
9950 static void s_manual_pool_destroy(struct aws_s3_buffer_pool *buffer_pool) {
9951 struct s_manual_pool_impl *pool_impl = (struct s_manual_pool_impl *)buffer_pool->impl;
9952
9953 for (size_t i = 0; i < 10; ++i) {
9954 if (pool_impl->futures[i]) {
9955 aws_future_s3_buffer_ticket_release(pool_impl->futures[i]);
9956 }
9957 }
9958
9959 aws_mem_release(pool_impl->allocator, buffer_pool);
9960 aws_mem_release(pool_impl->allocator, pool_impl);
9961 }
9962
9963 struct aws_s3_buffer_pool *s_manual_pool_fn(
9964 struct aws_allocator *allocator,
9965 struct aws_s3_buffer_pool_config config,
9966 void *user_data) {
9967 (void)config;
9968 (void)user_data;
9969 struct s_manual_pool_impl *pool_impl = aws_mem_calloc(allocator, 1, sizeof(struct s_manual_pool_impl));
9970 struct aws_s3_buffer_pool *pool = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_buffer_pool));
9971
9972 pool_impl->allocator = allocator;
9973
9974 pool->impl = pool_impl;
9975 pool->vtable = &s_manual_pool_vtable;
9976
9977 aws_ref_count_init(&pool->ref_count, pool, (aws_simple_completion_callback *)s_manual_pool_destroy);
9978
9979 return pool;
9980 }
9981
9982 /* Custom buffer tool test that will put first 5 buffer pool reservations into pending and then
9983 complete all of them in reverse order. */
9984 AWS_TEST_CASE(test_s3_upload_out_of_order_review, s_test_s3_upload_out_of_order_review)
9985 static int s_test_s3_upload_out_of_order_review(struct aws_allocator *allocator, void *ctx) {
9986 (void)ctx;
9987 struct aws_s3_meta_request_test_results test_results;
9988 aws_s3_meta_request_test_results_init(&test_results, allocator);
9989
9990 struct aws_s3_tester_client_options client_options = {.buffer_pool_factory_fn = s_manual_pool_fn};
9991
9992 struct aws_s3_tester_meta_request_options put_options = {
9993 .allocator = allocator,
9994 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
9995 .checksum_algorithm = AWS_SCA_CRC32,
9996 .client_options = &client_options,
9997 .put_options =
9998 {
9999 .object_path_override = aws_byte_cursor_from_c_str("/upload/review_16MB_CRC32.txt"),
10000 .object_size_mb = 39,
10001 },
10002 };
10003
10004 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
10005
10006 /* The tester always registers an upload_review_callback.
10007 * Check that it got what we expect */
10008 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
10009 ASSERT_UINT_EQUALS(5, test_results.upload_review.part_count);
10010 ASSERT_UINT_EQUALS(MB_TO_BYTES(8), test_results.upload_review.part_sizes_array[0]);
10011 ASSERT_UINT_EQUALS(MB_TO_BYTES(8), test_results.upload_review.part_sizes_array[1]);
10012 ASSERT_UINT_EQUALS(MB_TO_BYTES(8), test_results.upload_review.part_sizes_array[2]);
10013 ASSERT_UINT_EQUALS(MB_TO_BYTES(8), test_results.upload_review.part_sizes_array[3]);
10014 ASSERT_UINT_EQUALS(MB_TO_BYTES(7), test_results.upload_review.part_sizes_array[4]);
10015 ASSERT_INT_EQUALS(AWS_SCA_CRC32, test_results.upload_review.checksum_algorithm);
10016 ASSERT_STR_EQUALS("9J8ZNA==", aws_string_c_str(test_results.upload_review.part_checksums_array[0]));
10017 ASSERT_STR_EQUALS("KtQF9Q==", aws_string_c_str(test_results.upload_review.part_checksums_array[1]));
10018 ASSERT_STR_EQUALS("yagJog==", aws_string_c_str(test_results.upload_review.part_checksums_array[2]));
10019 ASSERT_STR_EQUALS("a5Y5pw==", aws_string_c_str(test_results.upload_review.part_checksums_array[3]));
10020 ASSERT_STR_EQUALS("XkcCkw==", aws_string_c_str(test_results.upload_review.part_checksums_array[4]));
10021
10022 aws_s3_meta_request_test_results_clean_up(&test_results);
10023 return 0;
10024 }
10025
10026 /* Test upload_review_callback when Content-Length is not declared */
10027 AWS_TEST_CASE(test_s3_upload_review_no_content_length, s_test_s3_upload_review_no_content_length)
10028 static int s_test_s3_upload_review_no_content_length(struct aws_allocator *allocator, void *ctx) {
10029 (void)ctx;
10030 struct aws_s3_meta_request_test_results test_results;
10031 aws_s3_meta_request_test_results_init(&test_results, allocator);
10032
10033 struct aws_s3_tester_meta_request_options put_options = {
10034 .allocator = allocator,
10035 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
10036 .checksum_algorithm = AWS_SCA_CRC32,
10037 .put_options =
10038 {
10039 .object_path_override = aws_byte_cursor_from_c_str("/upload/review_1MB_CRC32.txt"),
10040 .object_size_mb = 1,
10041 .skip_content_length = true,
10042 },
10043 };
10044
10045 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(NULL, &put_options, &test_results));
10046
10047 /* The tester always registers an upload_review_callback.
10048 * Check that it got what we expect */
10049 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
10050 ASSERT_UINT_EQUALS(1, test_results.upload_review.part_count);
10051 ASSERT_UINT_EQUALS(MB_TO_BYTES(1), test_results.upload_review.part_sizes_array[0]);
10052 ASSERT_STR_EQUALS("4hP4ig==", aws_string_c_str(test_results.upload_review.part_checksums_array[0]));
10053
10054 aws_s3_meta_request_test_results_clean_up(&test_results);
10055 return 0;
10056 }
10057
10058 static int s_upload_review_raise_canceled_error(
10059 struct aws_s3_meta_request *meta_request,
10060 const struct aws_s3_upload_review *review,
10061 void *user_data) {
10062 (void)meta_request;
10063 (void)review;
10064 (void)user_data;
10065
10066 return aws_raise_error(AWS_ERROR_S3_CANCELED);
10067 }
10068
10069 /* Test that if upload_review_callback raises an error, then the upload is canceled. */
10070 AWS_TEST_CASE(test_s3_upload_review_rejection, s_test_s3_upload_review_rejection)
10071 static int s_test_s3_upload_review_rejection(struct aws_allocator *allocator, void *ctx) {
10072 (void)ctx;
10073 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/upload/review_rejection.txt");
10074
10075 struct aws_s3_tester tester;
10076 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10077
10078 struct aws_s3_client_config client_config;
10079 AWS_ZERO_STRUCT(client_config);
10080 ASSERT_SUCCESS(aws_s3_tester_bind_client(
10081 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
10082
10083 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
10084 ASSERT_NOT_NULL(client);
10085
10086 /* Send meta-request that will raise an error from the review_upload_callback */
10087 struct aws_s3_meta_request_test_results test_results;
10088 aws_s3_meta_request_test_results_init(&test_results, allocator);
10089
10090 struct aws_s3_tester_meta_request_options put_options = {
10091 .allocator = allocator,
10092 .client = client,
10093 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
10094 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
10095 .checksum_algorithm = AWS_SCA_CRC32,
10096 .upload_review_callback = s_upload_review_raise_canceled_error,
10097 .put_options =
10098 {
10099 .object_path_override = object_path,
10100 .object_size_mb = 10,
10101 },
10102 };
10103
10104 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
10105
10106 /* Check that meta-request failed with the error raised by the upload_review_callback */
10107 ASSERT_INT_EQUALS(AWS_ERROR_S3_CANCELED, test_results.finished_error_code);
10108
10109 aws_s3_meta_request_test_results_clean_up(&test_results);
10110
10111 /*
10112 * Now check that the upload did not complete on the server either
10113 * (server should have received AbortMultipartUpload).
10114 * Check by attempting to GET the object, which should fail with 404 NOT FOUND.
10115 */
10116
10117 aws_s3_meta_request_test_results_init(&test_results, allocator);
10118
10119 struct aws_s3_tester_meta_request_options get_options = {
10120 .allocator = allocator,
10121 .client = client,
10122 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
10123 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE,
10124 .get_options =
10125 {
10126 .object_path = object_path,
10127 },
10128 };
10129 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &test_results));
10130 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_404_NOT_FOUND, test_results.finished_response_status);
10131 ASSERT_NOT_NULL(test_results.error_response_operation_name);
10132 ASSERT_TRUE(
10133 aws_string_eq_c_str(test_results.error_response_operation_name, "GetObject") ||
10134 aws_string_eq_c_str(test_results.error_response_operation_name, "HeadObject"));
10135
10136 aws_s3_meta_request_test_results_clean_up(&test_results);
10137 aws_s3_client_release(client);
10138 aws_s3_tester_clean_up(&tester);
10139 return 0;
10140 }
10141
10142 /* Test that an MPU can be done with checksum location = NONE as long as an upload review callback
10143 * is used, and the resulting object doesn't have checksums uploaded. */
10144 AWS_TEST_CASE(test_s3_upload_review_checksum_location_none, s_test_s3_upload_review_checksum_location_none)
10145 static int s_test_s3_upload_review_checksum_location_none(struct aws_allocator *allocator, void *ctx) {
10146 (void)ctx;
10147
10148 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/upload/review_10MB_no_CRC32.txt");
10149
10150 struct aws_s3_tester tester;
10151 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10152 struct aws_s3_tester_client_options client_options = {
10153 .part_size = MB_TO_BYTES(5),
10154 };
10155
10156 struct aws_s3_client *client = NULL;
10157 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
10158
10159 struct aws_s3_tester_meta_request_options put_options = {
10160 .allocator = allocator,
10161 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
10162 .client = client,
10163 .checksum_algorithm = AWS_SCA_CRC32,
10164 .disable_put_trailing_checksum = true,
10165 .put_options =
10166 {
10167 .object_size_mb = 10,
10168 .object_path_override = object_path,
10169 },
10170 };
10171
10172 struct aws_s3_meta_request_test_results test_results;
10173 aws_s3_meta_request_test_results_init(&test_results, allocator);
10174
10175 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
10176
10177 /* The tester always registers an upload_review_callback.
10178 * Check that it got what we expect */
10179 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
10180 ASSERT_UINT_EQUALS(2, test_results.upload_review.part_count);
10181 ASSERT_UINT_EQUALS(MB_TO_BYTES(5), test_results.upload_review.part_sizes_array[0]);
10182 ASSERT_UINT_EQUALS(MB_TO_BYTES(5), test_results.upload_review.part_sizes_array[1]);
10183 ASSERT_INT_EQUALS(AWS_SCA_CRC32, test_results.upload_review.checksum_algorithm);
10184 ASSERT_STR_EQUALS("7/xUXw==", aws_string_c_str(test_results.upload_review.part_checksums_array[0]));
10185 ASSERT_STR_EQUALS("PCOjcw==", aws_string_c_str(test_results.upload_review.part_checksums_array[1]));
10186
10187 /* S3 will store the crc64 checksum for the whole object, and we can still have validate the checksum, but the algo
10188 * be validated will be crc64, instead of the crc32 we get from the client. */
10189 struct aws_s3_tester_meta_request_options get_options = {
10190 .allocator = allocator,
10191 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
10192 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
10193 .client = client,
10194 .expected_validate_checksum_alg = AWS_SCA_CRC64NVME,
10195 .validate_get_response_checksum = true,
10196 .get_options =
10197 {
10198 .object_path = object_path,
10199 },
10200 .finish_callback = s_s3_test_validate_checksum,
10201 };
10202
10203 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, NULL));
10204
10205 aws_s3_meta_request_test_results_clean_up(&test_results);
10206 aws_s3_client_release(client);
10207 aws_s3_tester_clean_up(&tester);
10208 return 0;
10209 }
10210
10211 AWS_TEST_CASE(test_s3_upload_review_checksum_location_none_async, s_test_s3_upload_review_checksum_location_none_async)
10212 static int s_test_s3_upload_review_checksum_location_none_async(struct aws_allocator *allocator, void *ctx) {
10213 (void)ctx;
10214
10215 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/upload/review_10MB_no_CRC32.txt");
10216
10217 struct aws_s3_tester tester;
10218 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10219 struct aws_s3_tester_client_options client_options = {
10220 .part_size = MB_TO_BYTES(5),
10221 };
10222
10223 struct aws_s3_client *client = NULL;
10224 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
10225
10226 struct aws_s3_tester_meta_request_options put_options = {
10227 .allocator = allocator,
10228 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
10229 .client = client,
10230 .checksum_algorithm = AWS_SCA_CRC32,
10231 .disable_put_trailing_checksum = true,
10232 .put_options =
10233 {
10234 .object_size_mb = 10,
10235 .object_path_override = object_path,
10236 .async_input_stream = true,
10237 },
10238 };
10239
10240 struct aws_s3_meta_request_test_results test_results;
10241 aws_s3_meta_request_test_results_init(&test_results, allocator);
10242
10243 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
10244 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
10245
10246 aws_s3_meta_request_test_results_clean_up(&test_results);
10247 aws_s3_client_release(client);
10248 aws_s3_tester_clean_up(&tester);
10249 return 0;
10250 }
10251
10252 /* Trying to reach the noop case of async read */
10253 AWS_TEST_CASE(
10254 test_s3_upload_review_checksum_location_none_async_noop_part,
10255 s_test_s3_upload_review_checksum_location_none_async_noop_part)
10256 static int s_test_s3_upload_review_checksum_location_none_async_noop_part(struct aws_allocator *allocator, void *ctx) {
10257 (void)ctx;
10258
10259 struct aws_byte_cursor object_path = aws_byte_cursor_from_c_str("/upload/review_10MB_no_CRC32.txt");
10260
10261 struct aws_s3_tester tester;
10262 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10263 struct aws_s3_tester_client_options client_options = {
10264 .part_size = MB_TO_BYTES(5),
10265 };
10266
10267 struct aws_s3_client *client = NULL;
10268 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
10269
10270 struct aws_s3_tester_meta_request_options put_options = {
10271 .allocator = allocator,
10272 .meta_request_type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
10273 .client = client,
10274 .checksum_algorithm = AWS_SCA_CRC32,
10275 .disable_put_trailing_checksum = true,
10276 .put_options =
10277 {
10278 .object_size_mb = 10,
10279 .object_path_override = object_path,
10280 .async_input_stream = true,
10281 .skip_content_length = true,
10282 .eof_requires_extra_read = true, /* don't report EOF until it tries to read 2nd part */
10283 },
10284 };
10285
10286 struct aws_s3_meta_request_test_results test_results;
10287 aws_s3_meta_request_test_results_init(&test_results, allocator);
10288
10289 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &put_options, &test_results));
10290 ASSERT_UINT_EQUALS(1, test_results.upload_review.invoked_count);
10291
10292 aws_s3_meta_request_test_results_clean_up(&test_results);
10293 aws_s3_client_release(client);
10294 aws_s3_tester_clean_up(&tester);
10295 return 0;
10296 }
10297
10298 static struct aws_http_stream *s_http_connection_make_request_patch(
10299 struct aws_http_connection *client_connection,
10300 const struct aws_http_make_request_options *options) {
10301 struct aws_http_message *message = options->request;
10302 struct aws_http_headers *headers = aws_http_message_get_headers(message);
10303 struct aws_byte_cursor out_value;
10304 int e = aws_http_headers_get(headers, aws_byte_cursor_from_c_str("Content-Length"), &out_value);
10305 AWS_FATAL_ASSERT(e == AWS_OP_ERR); // Assert that the header is not present
10306 AWS_FATAL_ASSERT(aws_last_error() == AWS_ERROR_HTTP_HEADER_NOT_FOUND);
10307
10308 return aws_http_connection_make_request(client_connection, options);
10309 }
10310
10311 AWS_TEST_CASE(test_s3_default_get_without_content_length, s_test_s3_default_get_without_content_length)
10312 static int s_test_s3_default_get_without_content_length(struct aws_allocator *allocator, void *ctx) {
10313 (void)ctx;
10314
10315 struct aws_s3_tester tester;
10316 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10317
10318 struct aws_s3_client *client = NULL;
10319 struct aws_s3_tester_client_options client_options;
10320 AWS_ZERO_STRUCT(client_options);
10321
10322 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
10323 struct aws_s3_client_vtable *patched_client_vtable = aws_s3_tester_patch_client_vtable(&tester, client, NULL);
10324 patched_client_vtable->http_connection_make_request = s_http_connection_make_request_patch;
10325
10326 struct aws_s3_meta_request_test_results meta_request_test_results;
10327 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
10328
10329 struct aws_string *host_name =
10330 aws_s3_tester_build_endpoint_string(allocator, &g_test_public_bucket_name, &g_test_s3_region);
10331
10332 /* Put together a simple S3 Get Object request. */
10333 struct aws_http_message *message = aws_s3_test_get_object_request_new(
10334 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
10335
10336 struct aws_s3_meta_request_options options;
10337 AWS_ZERO_STRUCT(options);
10338 /* Send default type */
10339 options.type = AWS_S3_META_REQUEST_TYPE_DEFAULT;
10340 options.message = message;
10341 options.operation_name = aws_byte_cursor_from_c_str("GetObject");
10342
10343 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(
10344 &tester, client, &options, &meta_request_test_results, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS));
10345 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(&meta_request_test_results, 0));
10346
10347 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
10348 aws_string_destroy(host_name);
10349 aws_http_message_release(message);
10350 aws_s3_client_release(client);
10351 aws_s3_tester_clean_up(&tester);
10352
10353 return AWS_OP_SUCCESS;
10354 }
10355
10356 AWS_TEST_CASE(
10357 test_s3_get_object_size_hint_sizes_first_request_buffer,
10358 s_test_s3_get_object_size_hint_sizes_first_request_buffer)
10359 static int s_test_s3_get_object_size_hint_sizes_first_request_buffer(struct aws_allocator *allocator, void *ctx) {
10360 (void)ctx;
10361
10362 /* Verify that when object_size_hint is provided, the first GET request's range end reflects the hint
10363 * rather than the full part_size. This ensures we don't over-reserve buffer memory. */
10364
10365 struct aws_s3_tester tester;
10366 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10367
10368 /* Use a part_size larger than the object so the hint fits in one part */
10369 size_t part_size = MB_TO_BYTES(20);
10370 struct aws_s3_tester_client_options client_options = {
10371 .part_size = part_size,
10372 };
10373
10374 struct aws_s3_client *client = NULL;
10375 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
10376
10377 struct aws_s3_meta_request_test_results test_results;
10378 aws_s3_meta_request_test_results_init(&test_results, allocator);
10379
10380 /* Hint exactly matches the object size. The first request buffer reservation should be sized to
10381 * the hint (10MB - 1), not the full part_size (20MB - 1). */
10382 uint64_t object_size_hint = MB_TO_BYTES(10);
10383
10384 struct aws_s3_tester_meta_request_options get_options = {
10385 .allocator = allocator,
10386 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
10387 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
10388 .client = client,
10389 .get_options =
10390 {
10391 .object_path = g_pre_existing_object_10MB,
10392 },
10393 .object_size_hint = &object_size_hint,
10394 };
10395
10396 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &test_results));
10397
10398 /* The first (and only) request should have part_range_end == object_size_hint - 1, not part_size - 1.
10399 * Without the fix, this would be part_size - 1 (20MB - 1 = 20971519). */
10400 uint64_t first_range_end = 0;
10401 struct aws_s3_request_metrics *first_metrics = NULL;
10402 ASSERT_TRUE(aws_array_list_length(&test_results.synced_data.succeed_metrics) > 0);
10403 ASSERT_SUCCESS(aws_array_list_get_at(&test_results.synced_data.succeed_metrics, (void **)&first_metrics, 0));
10404 aws_s3_request_metrics_get_part_range_end(first_metrics, &first_range_end);
10405 ASSERT_UINT_EQUALS(object_size_hint - 1, first_range_end);
10406
10407 aws_s3_meta_request_test_results_clean_up(&test_results);
10408 aws_s3_client_release(client);
10409 aws_s3_tester_clean_up(&tester);
10410
10411 return AWS_OP_SUCCESS;
10412 }
10413
10414 AWS_TEST_CASE(
10415 test_s3_get_object_size_hint_too_small_falls_back_to_ranged_get,
10416 s_test_s3_get_object_size_hint_too_small_falls_back_to_ranged_get)
10417 static int s_test_s3_get_object_size_hint_too_small_falls_back_to_ranged_get(
10418 struct aws_allocator *allocator,
10419 void *ctx) {
10420 (void)ctx;
10421
10422 /* When object_size_hint is smaller than the actual first part, the hint-sized PART_NUMBER_1 request
10423 * is cancelled at the header stage. We should keep the discovered object size and fetch the data as
10424 * ranged gets, NOT re-issue another PART_NUMBER_1 request. This guards against the regression where
10425 * the meta request re-entered discovery and sent a second partNumber=1 request. */
10426
10427 struct aws_s3_tester tester;
10428 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
10429
10430 /* part_size smaller than the object forces a multi-part ranged download once the size is known. */
10431 size_t part_size = MB_TO_BYTES(12);
10432 struct aws_s3_tester_client_options client_options = {
10433 .part_size = part_size,
10434 };
10435
10436 struct aws_s3_client *client = NULL;
10437 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
10438
10439 struct aws_s3_meta_request_test_results test_results;
10440 aws_s3_meta_request_test_results_init(&test_results, allocator);
10441
10442 /* Hint of 1 byte smaller than the 10MB object, so the PART_NUMBER_1 buffer (sized to the hint)
10443 * cannot hold the first part and the request is cancelled. */
10444 uint64_t object_size_hint = 1024 * 1024 * 10 - 1;
10445
10446 struct aws_s3_tester_meta_request_options get_options = {
10447 .allocator = allocator,
10448 .meta_request_type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
10449 .validate_type = AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS,
10450 .client = client,
10451 .get_options =
10452 {
10453 .object_path = g_pre_existing_object_10MB,
10454 },
10455 .object_size_hint = &object_size_hint,
10456 };
10457
10458 ASSERT_SUCCESS(aws_s3_tester_send_meta_request_with_options(&tester, &get_options, &test_results));
10459
10460 /* Exactly 1 failed request: the cancelled PART_NUMBER_1 whose buffer was too small. */
10461 ASSERT_UINT_EQUALS(1, aws_array_list_length(&test_results.synced_data.fail_metrics));
10462 {
10463 struct aws_s3_request_metrics *metrics = NULL;
10464 ASSERT_SUCCESS(aws_array_list_get_at(&test_results.synced_data.fail_metrics, (void **)&metrics, 0));
10465 enum aws_s3_request_type request_type = AWS_S3_REQUEST_TYPE_UNKNOWN;
10466 aws_s3_request_metrics_get_request_type(metrics, &request_type);
10467 ASSERT_UINT_EQUALS(AWS_S3_REQUEST_TYPE_GET_OBJECT, request_type);
10468 const struct aws_string *path_query = NULL;
10469 aws_s3_request_metrics_get_request_path_query(metrics, &path_query);
10470 ASSERT_NOT_NULL(path_query);
10471 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_string(path_query);
10472 struct aws_byte_cursor part_number_query = aws_byte_cursor_from_c_str("partNumber");
10473 struct aws_byte_cursor found = {0};
10474 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&path_cursor, &part_number_query, &found));
10475 }
10476
10477 /* Exactly 1 succeeding ranged GET covering the whole 10MB object (part_size=12MB > object). */
10478 ASSERT_UINT_EQUALS(1, aws_array_list_length(&test_results.synced_data.succeed_metrics));
10479 {
10480 struct aws_s3_request_metrics *metrics = NULL;
10481 ASSERT_SUCCESS(aws_array_list_get_at(&test_results.synced_data.succeed_metrics, (void **)&metrics, 0));
10482 enum aws_s3_request_type request_type = AWS_S3_REQUEST_TYPE_UNKNOWN;
10483 aws_s3_request_metrics_get_request_type(metrics, &request_type);
10484 ASSERT_UINT_EQUALS(AWS_S3_REQUEST_TYPE_GET_OBJECT, request_type);
10485 uint64_t range_start = UINT64_MAX;
10486 aws_s3_request_metrics_get_part_range_start(metrics, &range_start);
10487 ASSERT_UINT_EQUALS(0, range_start);
10488 uint64_t range_end = 0;
10489 aws_s3_request_metrics_get_part_range_end(metrics, &range_end);
10490 ASSERT_UINT_EQUALS((1024 * 1024 * 10) - 1, range_end);
10491 }
10492
10493 aws_s3_meta_request_test_results_clean_up(&test_results);
10494 aws_s3_client_release(client);
10495 aws_s3_tester_clean_up(&tester);
10496
10497 return AWS_OP_SUCCESS;
10498 }