Debian Codegraph @

crt/aws-c-s3/tests/s3_tester.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 "s3_tester.h"
7 #include "aws/s3/private/s3_auto_ranged_get.h"
8 #include "aws/s3/private/s3_checksums.h"
9 #include "aws/s3/private/s3_client_impl.h"
10 #include "aws/s3/private/s3_default_buffer_pool.h"
11 #include "aws/s3/private/s3_meta_request_impl.h"
12 #include "aws/s3/private/s3_util.h"
13 #include <aws/auth/credentials.h>
14 #include <aws/common/encoding.h>
15 #include <aws/common/environment.h>
16 #include <aws/common/system_info.h>
17 #include <aws/common/uri.h>
18 #include <aws/common/uuid.h>
19 #include <aws/http/request_response.h>
20 #include <aws/io/channel_bootstrap.h>
21 #include <aws/io/event_loop.h>
22 #include <aws/io/host_resolver.h>
23 #include <aws/io/stream.h>
24 #include <aws/io/tls_channel_handler.h>
25 #include <aws/testing/aws_test_harness.h>
26 #include <aws/testing/stream_tester.h>
27 #include <ctype.h>
28 #include <inttypes.h>
29 #include <stdlib.h>
30 #include <time.h>
31
32 #ifdef _MSC_VER
33 # pragma warning(disable : 4232) /* function pointer to dll symbol */
34 #endif
35
36 const struct aws_byte_cursor g_mock_server_uri = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("http://localhost:8080/");
37
38 const struct aws_byte_cursor g_test_mrap_endpoint =
39 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("moujmk3izc19y.mrap.accesspoint.s3-global.amazonaws.com");
40
41 const struct aws_byte_cursor g_test_body_content_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("text/plain");
42 const struct aws_byte_cursor g_test_s3_region = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-west-2");
43
44 const struct aws_byte_cursor g_s3_sse_header = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption");
45 const struct aws_byte_cursor g_s3_sse_c_alg_header =
46 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-algorithm");
47 const struct aws_byte_cursor g_s3_sse_c_key_header =
48 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key");
49 const struct aws_byte_cursor g_s3_sse_c_key_md5_header =
50 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-server-side-encryption-customer-key-md5");
51
52 /* TODO populate these at the beginning of running tests with names that are unique to the test run. */
53
54 const struct aws_byte_cursor g_pre_existing_object_1MB = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/pre-existing-1MB");
55 const struct aws_byte_cursor g_pre_existing_object_10MB = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/pre-existing-10MB");
56 const struct aws_byte_cursor g_pre_existing_object_kms_10MB =
57 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/pre-existing-10MB-kms");
58 const struct aws_byte_cursor g_pre_existing_object_aes256_10MB =
59 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/pre-existing-10MB-aes256");
60 const struct aws_byte_cursor g_pre_existing_empty_object = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/pre-existing-empty");
61 const struct aws_byte_cursor g_pre_existing_object_async_error_xml =
62 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/pre-existing-async-error-xml");
63
64 const struct aws_byte_cursor g_put_object_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload/put-object-test");
65 const struct aws_byte_cursor g_upload_folder = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/upload");
66
67 /* If `$CRT_S3_TEST_BUCKET_NAME` environment variable is set, use that; otherwise, use aws-c-s3-test-bucket */
68 struct aws_byte_cursor g_test_bucket_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("aws-c-s3-test-bucket");
69 /* If `$CRT_S3_TEST_BUCKET_NAME` envrionment variable is set, use `$CRT_S3_TEST_BUCKET_NAME-public`; otherwise, use
70 * aws-c-s3-test-bucket-public */
71 struct aws_byte_cursor g_test_public_bucket_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("aws-c-s3-test-bucket-public");
72 /* If `$CRT_S3_TEST_BUCKET_NAME` environment variable is set, use
73 * `$CRT_S3_TEST_BUCKET_NAME--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com`; otherwise, use
74 * aws-c-s3-test-bucket--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com */
75 struct aws_byte_cursor g_test_s3express_bucket_usw2_az1_endpoint = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
76 "aws-c-s3-test-bucket--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com");
77 /* If `$CRT_S3_TEST_BUCKET_NAME` environment variable is set, use
78 * `$CRT_S3_TEST_BUCKET_NAME--us1-az1--x-s3.s3express-use1-az4.us-east-1.amazonaws.com`; otherwise, use
79 * aws-c-s3-test-bucket--use1-az4--x-s3.s3express-use1-az4.us-east-1.amazonaws.com */
80 struct aws_byte_cursor g_test_s3express_bucket_use1_az4_endpoint = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
81 "aws-c-s3-test-bucket--use1-az4--x-s3.s3express-use1-az4.us-east-1.amazonaws.com");
82
83 #ifdef BYO_CRYPTO
84 /* Under BYO_CRYPTO, this function currently needs to be defined by the user. Defining a null implementation here so
85 * that tests build, but it is not currently meant to be used by any tests. */
86 struct aws_byte_buf aws_tls_handler_protocol(struct aws_channel_handler *handler) {
87 (void)handler;
88 AWS_FATAL_ASSERT(false);
89 struct aws_byte_buf byte_buf;
90 AWS_ZERO_STRUCT(byte_buf);
91 return byte_buf;
92 }
93 #endif
94
95 static int s_s3_test_meta_request_header_callback(
96 struct aws_s3_meta_request *meta_request,
97 const struct aws_http_headers *headers,
98 int response_status,
99 void *user_data) {
100 (void)meta_request;
101
102 struct aws_s3_meta_request_test_results *meta_request_test_results =
103 (struct aws_s3_meta_request_test_results *)user_data;
104
105 aws_http_headers_release(meta_request_test_results->response_headers);
106
107 meta_request_test_results->response_headers = (struct aws_http_headers *)headers;
108 aws_http_headers_acquire(meta_request_test_results->response_headers);
109
110 meta_request_test_results->headers_response_status = response_status;
111
112 if (meta_request_test_results->headers_callback != NULL) {
113 return meta_request_test_results->headers_callback(meta_request, headers, response_status, user_data);
114 }
115
116 return AWS_OP_SUCCESS;
117 }
118
119 static int s_s3_test_meta_request_body_callback(
120 struct aws_s3_meta_request *meta_request,
121 const struct aws_byte_cursor *body,
122 uint64_t range_start,
123 void *user_data) {
124 (void)meta_request;
125 (void)body;
126 AWS_PRECONDITION(meta_request);
127 AWS_PRECONDITION(body);
128
129 struct aws_s3_meta_request_test_results *meta_request_test_results = user_data;
130 meta_request_test_results->received_body_size += body->len;
131 aws_atomic_fetch_add(&meta_request_test_results->received_body_size_delta, body->len);
132 AWS_LOGF_DEBUG(
133 AWS_LS_S3_GENERAL,
134 "Received range %" PRIu64 "-%" PRIu64 ". Expected range start: %" PRIu64,
135 range_start,
136 range_start + body->len - 1,
137 meta_request_test_results->expected_range_start);
138
139 uint64_t object_range_start = 0;
140
141 /* If this is an auto-ranged-get meta request, then grab the object range start so that the expected_range_start can
142 * be properly offset.*/
143 if (meta_request->type == AWS_S3_META_REQUEST_TYPE_GET_OBJECT && meta_request->part_size != 0) {
144
145 aws_s3_meta_request_lock_synced_data(meta_request);
146
147 struct aws_s3_auto_ranged_get *auto_ranged_get = meta_request->impl;
148 AWS_PRECONDITION(auto_ranged_get);
149
150 bool object_range_known = auto_ranged_get->synced_data.object_range_known != 0;
151 object_range_start = auto_ranged_get->synced_data.object_range_start;
152
153 aws_s3_meta_request_unlock_synced_data(meta_request);
154
155 ASSERT_TRUE(object_range_known);
156 }
157
158 ASSERT_TRUE((object_range_start + meta_request_test_results->expected_range_start) == range_start);
159 meta_request_test_results->expected_range_start += body->len;
160
161 if (meta_request_test_results->body_callback != NULL) {
162 return meta_request_test_results->body_callback(meta_request, body, range_start, user_data);
163 }
164
165 return AWS_OP_SUCCESS;
166 }
167
168 static void s_s3_test_meta_request_finish(
169 struct aws_s3_meta_request *meta_request,
170 const struct aws_s3_meta_request_result *result,
171 void *user_data) {
172 (void)meta_request;
173
174 struct aws_s3_meta_request_test_results *meta_request_test_results = user_data;
175 struct aws_s3_tester *tester = meta_request_test_results->tester;
176
177 meta_request_test_results->error_response_headers = result->error_response_headers;
178
179 if (result->error_response_headers != NULL) {
180 aws_http_headers_acquire(result->error_response_headers);
181 }
182
183 if (result->error_response_body != NULL) {
184 aws_byte_buf_init_copy(
185 &meta_request_test_results->error_response_body, tester->allocator, result->error_response_body);
186 }
187
188 if (result->error_response_operation_name != NULL) {
189 meta_request_test_results->error_response_operation_name =
190 aws_string_new_from_string(tester->allocator, result->error_response_operation_name);
191 }
192
193 meta_request_test_results->finished_response_status = result->response_status;
194 meta_request_test_results->finished_error_code = result->error_code;
195 meta_request_test_results->did_validate = result->did_validate;
196 meta_request_test_results->validation_algorithm = result->validation_algorithm;
197 meta_request_test_results->recv_file_direct_io_fallback_count = meta_request->recv_file_direct_io_fallback_count;
198
199 if (meta_request_test_results->finish_callback != NULL) {
200 meta_request_test_results->finish_callback(meta_request, result, user_data);
201 }
202
203 aws_s3_tester_notify_meta_request_finished(tester, result);
204 }
205
206 static void s_s3_test_meta_request_shutdown(void *user_data) {
207 struct aws_s3_meta_request_test_results *meta_request_test_results = user_data;
208 struct aws_s3_tester *tester = meta_request_test_results->tester;
209
210 aws_s3_tester_notify_meta_request_shutdown(tester);
211 }
212 static void s_s3_test_meta_request_telemetry(
213 struct aws_s3_meta_request *meta_request,
214 struct aws_s3_request_metrics *metrics,
215 void *user_data) {
216 (void)meta_request;
217 struct aws_s3_meta_request_test_results *meta_request_test_results = user_data;
218 struct aws_s3_tester *tester = meta_request_test_results->tester;
219 uint64_t time_stamp = 0;
220 aws_s3_request_metrics_get_start_timestamp_ns(metrics, &time_stamp);
221 AWS_FATAL_ASSERT(time_stamp > 0);
222 aws_s3_request_metrics_get_end_timestamp_ns(metrics, &time_stamp);
223 AWS_FATAL_ASSERT(time_stamp > 0);
224 aws_s3_request_metrics_get_total_duration_ns(metrics, &time_stamp);
225 AWS_FATAL_ASSERT(time_stamp > 0);
226 if (!aws_s3_request_metrics_get_send_end_timestamp_ns(metrics, &time_stamp)) {
227 AWS_FATAL_ASSERT(time_stamp > 0);
228 uint64_t start_time = 0;
229 uint64_t end_time = 0;
230 uint64_t during_time = 0;
231 int error = 0;
232 error |= aws_s3_request_metrics_get_send_start_timestamp_ns(metrics, &start_time);
233 error |= aws_s3_request_metrics_get_send_end_timestamp_ns(metrics, &end_time);
234 error |= aws_s3_request_metrics_get_sending_duration_ns(metrics, &during_time);
235 AWS_FATAL_ASSERT(error == AWS_OP_SUCCESS);
236 AWS_FATAL_ASSERT(during_time == (end_time - start_time));
237 }
238 if (!aws_s3_request_metrics_get_receive_end_timestamp_ns(metrics, &time_stamp)) {
239 AWS_FATAL_ASSERT(time_stamp > 0);
240 uint64_t start_time = 0;
241 uint64_t end_time = 0;
242 uint64_t during_time = 0;
243 int error = 0;
244 error |= aws_s3_request_metrics_get_receive_start_timestamp_ns(metrics, &start_time);
245 error |= aws_s3_request_metrics_get_receive_end_timestamp_ns(metrics, &end_time);
246 error |= aws_s3_request_metrics_get_receiving_duration_ns(metrics, &during_time);
247 AWS_FATAL_ASSERT(error == AWS_OP_SUCCESS);
248 AWS_FATAL_ASSERT(during_time == (end_time - start_time));
249 }
250
251 if (aws_s3_request_metrics_get_memory_allocated_from_pool(metrics)) {
252 uint64_t start_time = 0;
253 uint64_t end_time = 0;
254 uint64_t duration_time = 0;
255 int error = 0;
256 error |= aws_s3_request_metrics_get_mem_acquire_start_timestamp_ns(metrics, &start_time);
257 error |= aws_s3_request_metrics_get_mem_acquire_end_timestamp_ns(metrics, &end_time);
258 error |= aws_s3_request_metrics_get_mem_acquire_duration_ns(metrics, &duration_time);
259 AWS_FATAL_ASSERT(error == AWS_OP_SUCCESS);
260 AWS_FATAL_ASSERT(duration_time == (end_time - start_time));
261 }
262
263 aws_s3_tester_lock_synced_data(tester);
264 aws_array_list_push_back(&meta_request_test_results->synced_data.metrics, &metrics);
265 if (aws_s3_request_metrics_get_error_code(metrics) == AWS_ERROR_SUCCESS) {
266 aws_array_list_push_back(&meta_request_test_results->synced_data.succeed_metrics, &metrics);
267 } else {
268 aws_array_list_push_back(&meta_request_test_results->synced_data.fail_metrics, &metrics);
269 }
270 aws_s3_request_metrics_acquire(metrics);
271 aws_s3_tester_unlock_synced_data(tester);
272 }
273
274 static void s_s3_test_meta_request_progress(
275 struct aws_s3_meta_request *meta_request,
276 const struct aws_s3_meta_request_progress *progress,
277 void *user_data) {
278
279 (void)meta_request;
280 AWS_ASSERT(meta_request);
281 AWS_ASSERT(progress);
282 AWS_ASSERT(user_data);
283
284 struct aws_s3_meta_request_test_results *meta_request_test_results = user_data;
285
286 meta_request_test_results->progress.total_bytes_transferred += progress->bytes_transferred;
287
288 /* Once content_length is reported, it shouldn't change */
289 if (meta_request_test_results->progress.content_length == 0) {
290 meta_request_test_results->progress.content_length = progress->content_length;
291 } else {
292 AWS_FATAL_ASSERT(meta_request_test_results->progress.content_length == progress->content_length);
293 }
294
295 /* If content_length is known, we shouldn't go over it */
296 if (progress->content_length != 0) {
297 AWS_FATAL_ASSERT(meta_request_test_results->progress.total_bytes_transferred <= progress->content_length);
298 }
299
300 if (meta_request_test_results->progress_callback != NULL) {
301 meta_request_test_results->progress_callback(meta_request, progress, user_data);
302 }
303 }
304
305 static int s_s3_test_meta_request_upload_review(
306 struct aws_s3_meta_request *meta_request,
307 const struct aws_s3_upload_review *review,
308 void *user_data) {
309
310 struct aws_s3_meta_request_test_results *test_results = user_data;
311
312 AWS_FATAL_ASSERT(test_results->upload_review.invoked_count == 0);
313 test_results->upload_review.invoked_count++;
314
315 test_results->upload_review.checksum_algorithm = review->checksum_algorithm;
316
317 test_results->upload_review.part_count = review->part_count;
318 if (test_results->upload_review.part_count > 0) {
319 test_results->upload_review.part_sizes_array =
320 aws_mem_calloc(test_results->allocator, review->part_count, sizeof(uint64_t));
321
322 test_results->upload_review.part_checksums_array =
323 aws_mem_calloc(test_results->allocator, review->part_count, sizeof(struct aws_string *));
324
325 for (size_t i = 0; i < review->part_count; ++i) {
326 test_results->upload_review.part_sizes_array[i] = review->part_array[i].size;
327 test_results->upload_review.part_checksums_array[i] =
328 aws_string_new_from_cursor(test_results->allocator, &review->part_array[i].checksum);
329 }
330 }
331
332 if (test_results->upload_review_callback != NULL) {
333 return test_results->upload_review_callback(meta_request, review, user_data);
334 } else {
335 return AWS_OP_SUCCESS;
336 }
337 }
338
339 /* Notify the tester that a particular clean up step has finished. */
340 static void s_s3_test_client_shutdown(void *user_data);
341
342 static bool s_s3_tester_have_meta_requests_finished(void *user_data);
343
344 static bool s_s3_tester_has_client_shutdown(void *user_data);
345
346 struct aws_string *aws_s3_tester_build_endpoint_string(
347 struct aws_allocator *allocator,
348 const struct aws_byte_cursor *bucket_name,
349 const struct aws_byte_cursor *region) {
350
351 struct aws_byte_cursor endpoint_url_part0 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(".s3.");
352 struct aws_byte_cursor endpoint_url_part1 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(".amazonaws.com");
353
354 struct aws_byte_buf endpoint_buffer;
355 aws_byte_buf_init(&endpoint_buffer, allocator, 128);
356
357 aws_byte_buf_append_dynamic(&endpoint_buffer, bucket_name);
358 aws_byte_buf_append_dynamic(&endpoint_buffer, &endpoint_url_part0);
359 aws_byte_buf_append_dynamic(&endpoint_buffer, region);
360 aws_byte_buf_append_dynamic(&endpoint_buffer, &endpoint_url_part1);
361
362 struct aws_string *endpoint_string = aws_string_new_from_buf(allocator, &endpoint_buffer);
363
364 aws_byte_buf_clean_up(&endpoint_buffer);
365
366 return endpoint_string;
367 }
368
369 AWS_STATIC_STRING_FROM_LITERAL(s_bucket_name_env_var, "CRT_S3_TEST_BUCKET_NAME");
370
371 int aws_s3_tester_init(struct aws_allocator *allocator, struct aws_s3_tester *tester) {
372
373 AWS_PRECONDITION(allocator);
374 AWS_PRECONDITION(tester);
375
376 (void)allocator;
377
378 AWS_ZERO_STRUCT(*tester);
379
380 tester->allocator = allocator;
381 if (aws_get_environment_value(allocator, s_bucket_name_env_var, &tester->bucket_name) == AWS_OP_SUCCESS &&
382 tester->bucket_name != NULL) {
383 g_test_bucket_name = aws_byte_cursor_from_string(tester->bucket_name);
384 char public_bucket_name_buffer[128] = "";
385 snprintf(
386 public_bucket_name_buffer,
387 sizeof(public_bucket_name_buffer),
388 "" PRInSTR "-public",
389 AWS_BYTE_CURSOR_PRI(g_test_bucket_name));
390 tester->public_bucket_name = aws_string_new_from_c_str(allocator, public_bucket_name_buffer);
391 g_test_public_bucket_name = aws_byte_cursor_from_string(tester->public_bucket_name);
392
393 char s3express_bucket_usw2_az1_endpoint_buffer[512] = "";
394 snprintf(
395 s3express_bucket_usw2_az1_endpoint_buffer,
396 sizeof(s3express_bucket_usw2_az1_endpoint_buffer),
397 "" PRInSTR "--usw2-az1--x-s3.s3express-usw2-az1.us-west-2.amazonaws.com",
398 AWS_BYTE_CURSOR_PRI(g_test_bucket_name));
399 tester->s3express_bucket_usw2_az1_endpoint =
400 aws_string_new_from_c_str(allocator, s3express_bucket_usw2_az1_endpoint_buffer);
401 g_test_s3express_bucket_usw2_az1_endpoint =
402 aws_byte_cursor_from_string(tester->s3express_bucket_usw2_az1_endpoint);
403
404 char s3express_bucket_use1_az4_name_buffer[128] = "";
405 snprintf(
406 s3express_bucket_use1_az4_name_buffer,
407 sizeof(s3express_bucket_use1_az4_name_buffer),
408 "" PRInSTR "--use1-az4--x-s3.s3express-use1-az4.us-east-1.amazonaws.com",
409 AWS_BYTE_CURSOR_PRI(g_test_bucket_name));
410 tester->s3express_bucket_use1_az4_endpoint =
411 aws_string_new_from_c_str(allocator, s3express_bucket_use1_az4_name_buffer);
412 g_test_s3express_bucket_use1_az4_endpoint =
413 aws_byte_cursor_from_string(tester->s3express_bucket_use1_az4_endpoint);
414 }
415
416 aws_s3_library_init(allocator);
417
418 if (aws_mutex_init(&tester->synced_data.lock)) {
419 return AWS_OP_ERR;
420 }
421
422 if (aws_condition_variable_init(&tester->signal)) {
423 goto condition_variable_failed;
424 }
425
426 ASSERT_SUCCESS(aws_array_list_init_dynamic(
427 &tester->client_vtable_patches, tester->allocator, 4, sizeof(struct aws_s3_client_vtable_patch)));
428
429 ASSERT_SUCCESS(aws_array_list_init_dynamic(
430 &tester->meta_request_vtable_patches, tester->allocator, 4, sizeof(struct aws_s3_meta_request_vtable_patch)));
431
432 /* Setup an event loop group and host resolver. */
433 tester->el_group = aws_event_loop_group_new_default(allocator, 0, NULL);
434 ASSERT_TRUE(tester->el_group != NULL);
435
436 struct aws_host_resolver_default_options resolver_options = {
437 .max_entries = 10,
438 .el_group = tester->el_group,
439 };
440 tester->host_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
441 ASSERT_TRUE(tester->host_resolver != NULL);
442
443 /* Setup the client boot strap. */
444 {
445 struct aws_client_bootstrap_options bootstrap_options;
446 AWS_ZERO_STRUCT(bootstrap_options);
447 bootstrap_options.event_loop_group = tester->el_group;
448 bootstrap_options.host_resolver = tester->host_resolver;
449 bootstrap_options.user_data = tester;
450
451 tester->client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
452 }
453
454 tester->anonymous_creds = aws_credentials_new_anonymous(allocator);
455 tester->anonymous_signing_config.credentials = tester->anonymous_creds;
456 #ifndef BYO_CRYPTO
457 /* Setup the credentials provider */
458 {
459 struct aws_credentials_provider_chain_default_options credentials_config;
460 AWS_ZERO_STRUCT(credentials_config);
461 credentials_config.bootstrap = tester->client_bootstrap;
462 tester->credentials_provider = aws_credentials_provider_new_chain_default(allocator, &credentials_config);
463 aws_s3_init_default_signing_config(
464 &tester->default_signing_config, g_test_s3_region, tester->credentials_provider);
465 }
466 #else
467 {
468 tester->default_signing_config = tester->anonymous_signing_config;
469 }
470 #endif
471
472 return AWS_OP_SUCCESS;
473
474 condition_variable_failed:
475
476 aws_mutex_clean_up(&tester->synced_data.lock);
477
478 return AWS_OP_ERR;
479 }
480
481 int aws_s3_tester_bind_client(struct aws_s3_tester *tester, struct aws_s3_client_config *config, uint32_t flags) {
482 AWS_PRECONDITION(tester);
483 AWS_PRECONDITION(config);
484
485 ASSERT_TRUE(!tester->bound_to_client);
486 tester->bound_to_client = true;
487
488 ASSERT_TRUE(config->client_bootstrap == NULL);
489 config->client_bootstrap = tester->client_bootstrap;
490
491 if (flags & AWS_S3_TESTER_BIND_CLIENT_SIGNING) {
492 ASSERT_TRUE(config->signing_config == NULL);
493 config->signing_config = &tester->default_signing_config;
494 }
495 if (flags & AWS_S3_TESTER_BIND_CLIENT_REGION) {
496 ASSERT_TRUE(config->region.len == 0);
497 config->region = g_test_s3_region;
498 } else {
499 if (config->signing_config) {
500 ((struct aws_signing_config_aws *)config->signing_config)->region = config->region;
501 }
502 }
503 if (!config->signing_config) {
504 config->signing_config = &tester->anonymous_signing_config;
505 }
506
507 ASSERT_TRUE(config->shutdown_callback == NULL);
508 config->shutdown_callback = s_s3_test_client_shutdown;
509
510 ASSERT_TRUE(config->shutdown_callback_user_data == NULL);
511 config->shutdown_callback_user_data = tester;
512
513 return AWS_OP_SUCCESS;
514 }
515
516 int aws_s3_tester_bind_meta_request(
517 struct aws_s3_tester *tester,
518 struct aws_s3_meta_request_options *options,
519 struct aws_s3_meta_request_test_results *meta_request_test_results) {
520
521 meta_request_test_results->tester = tester;
522
523 aws_s3_tester_lock_synced_data(tester);
524 ++tester->synced_data.desired_meta_request_finish_count;
525 ++tester->synced_data.desired_meta_request_shutdown_count;
526 aws_s3_tester_unlock_synced_data(tester);
527
528 ASSERT_TRUE(options->headers_callback == NULL);
529 options->headers_callback = s_s3_test_meta_request_header_callback;
530
531 ASSERT_TRUE(options->body_callback == NULL);
532 options->body_callback = s_s3_test_meta_request_body_callback;
533
534 ASSERT_TRUE(options->finish_callback == NULL);
535 options->finish_callback = s_s3_test_meta_request_finish;
536
537 ASSERT_TRUE(options->shutdown_callback == NULL);
538 options->shutdown_callback = s_s3_test_meta_request_shutdown;
539
540 ASSERT_TRUE(options->telemetry_callback == NULL);
541 options->telemetry_callback = s_s3_test_meta_request_telemetry;
542
543 ASSERT_TRUE(options->progress_callback == NULL);
544 options->progress_callback = s_s3_test_meta_request_progress;
545
546 ASSERT_TRUE(options->upload_review_callback == NULL);
547 options->upload_review_callback = s_s3_test_meta_request_upload_review;
548
549 ASSERT_TRUE(options->user_data == NULL);
550 options->user_data = meta_request_test_results;
551
552 return AWS_OP_SUCCESS;
553 }
554
555 void aws_s3_meta_request_test_results_init(
556 struct aws_s3_meta_request_test_results *test_meta_request,
557 struct aws_allocator *allocator) {
558
559 AWS_ZERO_STRUCT(*test_meta_request);
560 test_meta_request->allocator = allocator;
561 aws_atomic_init_int(&test_meta_request->received_body_size_delta, 0);
562 aws_array_list_init_dynamic(
563 &test_meta_request->synced_data.metrics, allocator, 4, sizeof(struct aws_s3_request_metrics *));
564 aws_array_list_init_dynamic(
565 &test_meta_request->synced_data.succeed_metrics, allocator, 4, sizeof(struct aws_s3_request_metrics *));
566 aws_array_list_init_dynamic(
567 &test_meta_request->synced_data.fail_metrics, allocator, 4, sizeof(struct aws_s3_request_metrics *));
568 }
569
570 void aws_s3_meta_request_test_results_clean_up(struct aws_s3_meta_request_test_results *test_meta_request) {
571 if (test_meta_request == NULL) {
572 return;
573 }
574 aws_http_headers_release(test_meta_request->error_response_headers);
575 aws_byte_buf_clean_up(&test_meta_request->error_response_body);
576 aws_string_destroy(test_meta_request->error_response_operation_name);
577 aws_http_headers_release(test_meta_request->response_headers);
578 while (aws_array_list_length(&test_meta_request->synced_data.metrics) > 0) {
579 struct aws_s3_request_metrics *metrics = NULL;
580 aws_array_list_back(&test_meta_request->synced_data.metrics, (void **)&metrics);
581 aws_array_list_pop_back(&test_meta_request->synced_data.metrics);
582 aws_s3_request_metrics_release(metrics);
583 }
584 aws_array_list_clean_up(&test_meta_request->synced_data.metrics);
585 /* We don't need to release the metrics from succeed/fail lists, since they're already released from the main list.
586 */
587 aws_array_list_clean_up(&test_meta_request->synced_data.succeed_metrics);
588 aws_array_list_clean_up(&test_meta_request->synced_data.fail_metrics);
589
590 for (size_t i = 0; i < test_meta_request->upload_review.part_count; ++i) {
591 aws_string_destroy(test_meta_request->upload_review.part_checksums_array[i]);
592 }
593 aws_mem_release(test_meta_request->allocator, test_meta_request->upload_review.part_sizes_array);
594 aws_mem_release(test_meta_request->allocator, test_meta_request->upload_review.part_checksums_array);
595
596 AWS_ZERO_STRUCT(*test_meta_request);
597 }
598
599 void aws_s3_tester_notify_meta_request_finished(
600 struct aws_s3_tester *tester,
601 const struct aws_s3_meta_request_result *result) {
602 AWS_PRECONDITION(tester);
603
604 bool notify = false;
605
606 aws_s3_tester_lock_synced_data(tester);
607 ++tester->synced_data.meta_request_finish_count;
608
609 int error_code = AWS_ERROR_SUCCESS;
610
611 if (result != NULL) {
612 error_code = result->error_code;
613 }
614
615 if (tester->synced_data.desired_meta_request_finish_count == 0 ||
616 tester->synced_data.meta_request_finish_count == tester->synced_data.desired_meta_request_finish_count ||
617 (error_code != AWS_ERROR_SUCCESS)) {
618
619 tester->synced_data.meta_requests_finished = true;
620 tester->synced_data.finish_error_code = error_code;
621
622 notify = true;
623 }
624
625 aws_s3_tester_unlock_synced_data(tester);
626
627 if (notify) {
628 aws_condition_variable_notify_all(&tester->signal);
629 }
630 }
631
632 static bool s_s3_tester_have_meta_requests_finished(void *user_data) {
633 AWS_PRECONDITION(user_data);
634 struct aws_s3_tester *tester = (struct aws_s3_tester *)user_data;
635
636 return tester->synced_data.meta_requests_finished > 0;
637 }
638
639 void aws_s3_tester_wait_for_meta_request_finish(struct aws_s3_tester *tester) {
640 AWS_PRECONDITION(tester);
641
642 aws_s3_tester_lock_synced_data(tester);
643 aws_condition_variable_wait_pred(
644 &tester->signal, &tester->synced_data.lock, s_s3_tester_have_meta_requests_finished, tester);
645
646 tester->synced_data.meta_requests_finished = false;
647 aws_s3_tester_unlock_synced_data(tester);
648 }
649
650 void aws_s3_tester_notify_meta_request_shutdown(struct aws_s3_tester *tester) {
651 bool notify = false;
652
653 aws_s3_tester_lock_synced_data(tester);
654 ++tester->synced_data.meta_request_shutdown_count;
655
656 if (tester->synced_data.desired_meta_request_shutdown_count == 0 ||
657 tester->synced_data.meta_request_shutdown_count == tester->synced_data.desired_meta_request_shutdown_count) {
658
659 tester->synced_data.meta_requests_shutdown = true;
660 notify = true;
661 }
662
663 aws_s3_tester_unlock_synced_data(tester);
664
665 if (notify) {
666 aws_condition_variable_notify_all(&tester->signal);
667 }
668 }
669
670 static bool s_s3_tester_have_meta_requests_shutdown(void *user_data) {
671 AWS_PRECONDITION(user_data);
672 struct aws_s3_tester *tester = (struct aws_s3_tester *)user_data;
673
674 return tester->synced_data.meta_requests_shutdown > 0;
675 }
676
677 void aws_s3_tester_wait_for_meta_request_shutdown(struct aws_s3_tester *tester) {
678 AWS_PRECONDITION(tester);
679
680 aws_s3_tester_lock_synced_data(tester);
681 aws_condition_variable_wait_pred(
682 &tester->signal, &tester->synced_data.lock, s_s3_tester_have_meta_requests_shutdown, tester);
683
684 tester->synced_data.meta_requests_shutdown = false;
685 aws_s3_tester_unlock_synced_data(tester);
686 }
687
688 static bool s_s3_tester_counters_equal_desired(void *user_data) {
689 AWS_PRECONDITION(user_data);
690 struct aws_s3_tester *tester = (struct aws_s3_tester *)user_data;
691
692 return tester->synced_data.counter1 == tester->synced_data.desired_counter1 &&
693 tester->synced_data.counter2 == tester->synced_data.desired_counter2;
694 }
695
696 void aws_s3_tester_wait_for_signal(struct aws_s3_tester *tester) {
697 aws_s3_tester_lock_synced_data(tester);
698 aws_condition_variable_wait(&tester->signal, &tester->synced_data.lock);
699 aws_s3_tester_unlock_synced_data(tester);
700 }
701
702 void aws_s3_tester_notify_signal(struct aws_s3_tester *tester) {
703 aws_condition_variable_notify_all(&tester->signal);
704 }
705
706 void aws_s3_tester_wait_for_counters(struct aws_s3_tester *tester) {
707 aws_s3_tester_lock_synced_data(tester);
708 aws_condition_variable_wait_pred(
709 &tester->signal, &tester->synced_data.lock, s_s3_tester_counters_equal_desired, tester);
710 aws_s3_tester_unlock_synced_data(tester);
711 }
712
713 size_t aws_s3_tester_inc_counter1(struct aws_s3_tester *tester) {
714 aws_s3_tester_lock_synced_data(tester);
715 size_t result = ++tester->synced_data.counter1;
716 aws_s3_tester_unlock_synced_data(tester);
717
718 aws_condition_variable_notify_all(&tester->signal);
719
720 return result;
721 }
722
723 size_t aws_s3_tester_inc_counter2(struct aws_s3_tester *tester) {
724 aws_s3_tester_lock_synced_data(tester);
725 size_t result = ++tester->synced_data.counter2;
726 aws_s3_tester_unlock_synced_data(tester);
727
728 aws_condition_variable_notify_all(&tester->signal);
729
730 return result;
731 }
732
733 void aws_s3_tester_reset_counter1(struct aws_s3_tester *tester) {
734 aws_s3_tester_lock_synced_data(tester);
735 tester->synced_data.counter1 = 0;
736 aws_s3_tester_unlock_synced_data(tester);
737 }
738
739 void aws_s3_tester_reset_counter2(struct aws_s3_tester *tester) {
740 aws_s3_tester_lock_synced_data(tester);
741 tester->synced_data.counter2 = 0;
742 aws_s3_tester_unlock_synced_data(tester);
743 }
744
745 void aws_s3_tester_set_counter1_desired(struct aws_s3_tester *tester, size_t value) {
746 aws_s3_tester_lock_synced_data(tester);
747 tester->synced_data.desired_counter1 = value;
748 aws_s3_tester_unlock_synced_data(tester);
749 }
750
751 void aws_s3_tester_set_counter2_desired(struct aws_s3_tester *tester, size_t value) {
752 aws_s3_tester_lock_synced_data(tester);
753 tester->synced_data.desired_counter2 = value;
754 aws_s3_tester_unlock_synced_data(tester);
755 }
756
757 void aws_s3_tester_clean_up(struct aws_s3_tester *tester) {
758 AWS_PRECONDITION(tester);
759
760 if (tester->bound_to_client) {
761 aws_s3_tester_wait_for_client_shutdown(tester);
762 tester->bound_to_client = false;
763 }
764 aws_string_destroy(tester->bucket_name);
765 aws_string_destroy(tester->public_bucket_name);
766 aws_string_destroy(tester->s3express_bucket_usw2_az1_endpoint);
767 aws_string_destroy(tester->s3express_bucket_use1_az4_endpoint);
768
769 aws_credentials_release(tester->anonymous_creds);
770
771 aws_array_list_clean_up(&tester->client_vtable_patches);
772 aws_array_list_clean_up(&tester->meta_request_vtable_patches);
773
774 aws_client_bootstrap_release(tester->client_bootstrap);
775 tester->client_bootstrap = NULL;
776
777 aws_credentials_provider_release(tester->credentials_provider);
778 tester->credentials_provider = NULL;
779
780 aws_host_resolver_release(tester->host_resolver);
781 tester->host_resolver = NULL;
782
783 aws_event_loop_group_release(tester->el_group);
784 tester->el_group = NULL;
785
786 aws_s3_library_clean_up();
787
788 aws_condition_variable_clean_up(&tester->signal);
789 aws_mutex_clean_up(&tester->synced_data.lock);
790 }
791
792 void aws_s3_tester_lock_synced_data(struct aws_s3_tester *tester) {
793 AWS_PRECONDITION(tester);
794 aws_mutex_lock(&tester->synced_data.lock);
795 }
796
797 void aws_s3_tester_unlock_synced_data(struct aws_s3_tester *tester) {
798 AWS_PRECONDITION(tester);
799
800 aws_mutex_unlock(&tester->synced_data.lock);
801 }
802
803 struct aws_s3_meta_request *s_s3_client_meta_request_factory_empty(
804 struct aws_s3_client *client,
805 const struct aws_s3_meta_request_options *options) {
806 AWS_PRECONDITION(client);
807 AWS_PRECONDITION(options);
808
809 (void)client;
810 (void)options;
811
812 return NULL;
813 }
814
815 void s_s3_client_create_connection_for_request_empty(struct aws_s3_client *client, struct aws_s3_request *request) {
816 AWS_PRECONDITION(client);
817 AWS_PRECONDITION(request);
818
819 (void)client;
820 (void)request;
821 }
822
823 static void s_s3_client_acquire_http_connection_empty(
824 struct aws_http_connection_manager *conn_manager,
825 aws_http_connection_manager_on_connection_setup_fn *on_connection_acquired_callback,
826 void *user_data) {
827 (void)conn_manager;
828 (void)on_connection_acquired_callback;
829 (void)user_data;
830 }
831
832 size_t s_s3_client_get_host_address_count_empty(
833 struct aws_host_resolver *host_resolver,
834 const struct aws_string *host_name,
835 uint32_t flags) {
836 (void)host_resolver;
837 (void)host_name;
838 (void)flags;
839 return 0;
840 }
841
842 static void s_s3_client_schedule_process_work_synced_empty(struct aws_s3_client *client) {
843 (void)client;
844 }
845
846 static void s_s3_client_process_work_empty(struct aws_s3_client *client) {
847 AWS_PRECONDITION(client);
848 (void)client;
849 }
850
851 static void s_s3_client_endpoint_shutdown_callback_empty(struct aws_s3_client *client) {
852 AWS_PRECONDITION(client);
853 (void)client;
854 }
855
856 static void s_s3_client_finish_destroy_empty(struct aws_s3_client *client) {
857 AWS_PRECONDITION(client);
858 (void)client;
859 }
860
861 struct aws_s3_client_vtable g_aws_s3_client_mock_vtable = {
862 .meta_request_factory = s_s3_client_meta_request_factory_empty,
863 .create_connection_for_request = s_s3_client_create_connection_for_request_empty,
864 .acquire_http_connection = s_s3_client_acquire_http_connection_empty,
865 .get_host_address_count = s_s3_client_get_host_address_count_empty,
866 .schedule_process_work_synced = s_s3_client_schedule_process_work_synced_empty,
867 .process_work = s_s3_client_process_work_empty,
868 .endpoint_shutdown_callback = s_s3_client_endpoint_shutdown_callback_empty,
869 .finish_destroy = s_s3_client_finish_destroy_empty,
870 };
871
872 static void s_s3_mock_client_start_destroy(void *user_data) {
873 struct aws_s3_client *client = user_data;
874 AWS_ASSERT(client);
875
876 aws_s3_buffer_pool_release(client->buffer_pool);
877 client->buffer_pool = NULL;
878 aws_mem_release(client->allocator, client);
879 }
880
881 struct aws_s3_client *aws_s3_tester_mock_client_new(struct aws_s3_tester *tester) {
882 struct aws_allocator *allocator = tester->allocator;
883 struct aws_s3_client *mock_client = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_client));
884
885 mock_client->allocator = allocator;
886
887 mock_client->buffer_pool = aws_s3_default_buffer_pool_new(
888 allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)});
889 mock_client->vtable = &g_aws_s3_client_mock_vtable;
890
891 aws_ref_count_init(
892 &mock_client->ref_count, mock_client, (aws_simple_completion_callback *)s_s3_mock_client_start_destroy);
893
894 aws_mutex_init(&mock_client->synced_data.lock);
895
896 aws_atomic_init_int(&mock_client->stats.num_requests_in_flight, 0);
897
898 for (uint32_t i = 0; i < (uint32_t)AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
899 aws_atomic_init_int(&mock_client->stats.num_requests_network_io[i], 0);
900 }
901
902 aws_atomic_init_int(&mock_client->stats.num_requests_stream_queued_waiting, 0);
903 aws_atomic_init_int(&mock_client->stats.num_requests_streaming_response, 0);
904
905 return mock_client;
906 }
907
908 struct aws_http_message *aws_s3_tester_dummy_http_request_new(struct aws_s3_tester *tester) {
909 AWS_PRECONDITION(tester);
910
911 struct aws_http_message *message = aws_http_message_new_request(tester->allocator);
912 aws_http_message_set_request_method(message, aws_http_method_get);
913 aws_http_message_set_request_path(message, aws_byte_cursor_from_c_str("/dummy_test"));
914
915 struct aws_http_header host_header = {
916 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
917 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("dummy_host"),
918 };
919
920 aws_http_message_add_header(message, host_header);
921
922 return message;
923 }
924
925 static bool s_s3_meta_request_update_empty(
926 struct aws_s3_meta_request *meta_request,
927 uint32_t flags,
928 struct aws_s3_request **out_request) {
929 (void)meta_request;
930 (void)flags;
931 (void)out_request;
932 return false;
933 }
934
935 void s_s3_meta_request_send_request_finish_empty(
936 struct aws_s3_connection *connection,
937 struct aws_http_stream *stream,
938 int error_code) {
939 (void)connection;
940 (void)stream;
941 (void)error_code;
942 }
943
944 static void s_s3_meta_request_finished_request_empty(
945 struct aws_s3_meta_request *meta_request,
946 struct aws_s3_request *request,
947 int error_code) {
948 (void)meta_request;
949 (void)request;
950 (void)error_code;
951 }
952
953 static void s_s3_meta_request_schedule_prepare_request_empty(
954 struct aws_s3_meta_request *meta_request,
955 struct aws_s3_request *request,
956 aws_s3_meta_request_prepare_request_callback_fn *callback,
957 void *user_data) {
958 (void)meta_request;
959 (void)request;
960 (void)callback;
961 (void)user_data;
962 }
963
964 static struct aws_future_void *s_s3_meta_request_prepare_request_async_empty(struct aws_s3_request *request) {
965 struct aws_future_void *future = aws_future_void_new(request->allocator);
966 aws_future_void_set_error(future, AWS_ERROR_UNKNOWN);
967 return future;
968 }
969
970 static void s_s3_meta_request_init_signing_date_time_empty(
971 struct aws_s3_meta_request *meta_request,
972 struct aws_date_time *date_time) {
973 (void)meta_request;
974 (void)date_time;
975 }
976
977 static void s_s3_meta_request_sign_request_empty(
978 struct aws_s3_meta_request *meta_request,
979 struct aws_s3_request *request,
980 aws_signing_complete_fn *on_signing_complete,
981 void *user_data) {
982 (void)meta_request;
983 (void)request;
984 (void)on_signing_complete;
985 (void)user_data;
986 }
987
988 static void s_s3_mock_meta_request_destroy(struct aws_s3_meta_request *meta_request) {
989 AWS_PRECONDITION(meta_request);
990
991 aws_mem_release(meta_request->allocator, meta_request->impl);
992 }
993
994 static struct aws_s3_meta_request_vtable s_s3_mock_meta_request_vtable = {
995 .update = s_s3_meta_request_update_empty,
996 .send_request_finish = s_s3_meta_request_send_request_finish_empty,
997 .schedule_prepare_request = s_s3_meta_request_schedule_prepare_request_empty,
998 .prepare_request = s_s3_meta_request_prepare_request_async_empty,
999 .finished_request = s_s3_meta_request_finished_request_empty,
1000 .init_signing_date_time = s_s3_meta_request_init_signing_date_time_empty,
1001 .sign_request = s_s3_meta_request_sign_request_empty,
1002 .destroy = s_s3_mock_meta_request_destroy,
1003 };
1004
1005 static struct aws_string *s_full_object_checksum_callback(struct aws_s3_meta_request *meta_request, void *user_data) {
1006 (void)meta_request;
1007 struct aws_byte_buf *src = (struct aws_byte_buf *)user_data;
1008 struct aws_string *checksum_str = aws_string_new_from_buf(src->allocator, src);
1009 /* Clean up the user data from the callback */
1010 struct aws_allocator *allocator = src->allocator;
1011 aws_byte_buf_clean_up_secure(src);
1012 aws_mem_release(allocator, src);
1013 return checksum_str;
1014 }
1015
1016 struct aws_s3_empty_meta_request {
1017 struct aws_s3_meta_request base;
1018 };
1019
1020 static void s_s3_mock_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock) {
1021 (void)already_holding_lock;
1022 ++endpoint->client_synced_data.ref_count;
1023 }
1024
1025 static void s_s3_mock_endpoint_release(struct aws_s3_endpoint *endpoint) {
1026 if (--endpoint->client_synced_data.ref_count == 0) {
1027 aws_string_destroy(endpoint->host_name);
1028 aws_mem_release(endpoint->allocator, endpoint);
1029 }
1030 }
1031
1032 static struct aws_s3_endpoint_system_vtable s_s3_mock_endpoint_vtable = {
1033 .acquire = s_s3_mock_endpoint_acquire,
1034 .release = s_s3_mock_endpoint_release,
1035 };
1036
1037 struct aws_s3_endpoint *aws_s3_tester_mock_endpoint_new(struct aws_s3_tester *tester) {
1038 aws_s3_endpoint_set_system_vtable(&s_s3_mock_endpoint_vtable);
1039
1040 struct aws_s3_endpoint *endpoint = aws_mem_calloc(tester->allocator, 1, sizeof(struct aws_s3_endpoint));
1041 endpoint->allocator = tester->allocator;
1042 endpoint->client_synced_data.ref_count = 1;
1043
1044 struct aws_byte_cursor empty_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
1045 endpoint->host_name = aws_string_new_from_cursor(tester->allocator, &empty_cursor);
1046
1047 return endpoint;
1048 }
1049
1050 /* Mock request defaults to GET request */
1051 struct aws_s3_meta_request *aws_s3_tester_mock_meta_request_new(struct aws_s3_tester *tester) {
1052 AWS_PRECONDITION(tester);
1053
1054 struct aws_s3_empty_meta_request *empty_meta_request =
1055 aws_mem_calloc(tester->allocator, 1, sizeof(struct aws_s3_empty_meta_request));
1056
1057 struct aws_http_message *dummy_http_message = aws_s3_tester_dummy_http_request_new(tester);
1058
1059 struct aws_s3_meta_request_options options = {
1060 .message = dummy_http_message,
1061 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
1062 };
1063
1064 aws_s3_meta_request_init_base(
1065 tester->allocator,
1066 NULL,
1067 0,
1068 false,
1069 false,
1070 &options,
1071 empty_meta_request,
1072 &s_s3_mock_meta_request_vtable,
1073 &empty_meta_request->base);
1074
1075 aws_http_message_release(dummy_http_message);
1076
1077 return &empty_meta_request->base;
1078 }
1079
1080 void aws_s3_create_test_buffer(struct aws_allocator *allocator, size_t buffer_size, struct aws_byte_buf *out_buf) {
1081 AWS_PRECONDITION(allocator);
1082 AWS_PRECONDITION(out_buf);
1083
1084 struct aws_byte_cursor test_string = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("This is an S3 test.");
1085
1086 aws_byte_buf_init(out_buf, allocator, buffer_size);
1087
1088 for (size_t buffer_pos = 0; buffer_pos < buffer_size; buffer_pos += test_string.len) {
1089 size_t buffer_size_remaining = buffer_size - buffer_pos;
1090 size_t string_copy_size = test_string.len;
1091
1092 if (buffer_size_remaining < string_copy_size) {
1093 string_copy_size = buffer_size_remaining;
1094 }
1095
1096 struct aws_byte_cursor from_byte_cursor = {.len = string_copy_size, .ptr = test_string.ptr};
1097
1098 aws_byte_buf_append(out_buf, &from_byte_cursor);
1099 }
1100 }
1101
1102 static void s_s3_test_client_shutdown(void *user_data) {
1103 AWS_PRECONDITION(user_data);
1104
1105 struct aws_s3_tester *tester = (struct aws_s3_tester *)user_data;
1106
1107 aws_s3_tester_lock_synced_data(tester);
1108 tester->synced_data.client_shutdown = true;
1109 aws_s3_tester_unlock_synced_data(tester);
1110
1111 aws_condition_variable_notify_all(&tester->signal);
1112 }
1113
1114 static bool s_s3_tester_has_client_shutdown(void *user_data) {
1115 AWS_PRECONDITION(user_data);
1116 struct aws_s3_tester *tester = (struct aws_s3_tester *)user_data;
1117
1118 return tester->synced_data.client_shutdown > 0;
1119 }
1120
1121 void aws_s3_tester_wait_for_client_shutdown(struct aws_s3_tester *tester) {
1122 AWS_PRECONDITION(tester);
1123
1124 aws_s3_tester_lock_synced_data(tester);
1125 aws_condition_variable_wait_pred(
1126 &tester->signal, &tester->synced_data.lock, s_s3_tester_has_client_shutdown, tester);
1127
1128 tester->synced_data.client_shutdown = false;
1129 aws_s3_tester_unlock_synced_data(tester);
1130 }
1131 struct aws_http_message *aws_s3_test_get_object_request_new(
1132 struct aws_allocator *allocator,
1133 struct aws_byte_cursor host,
1134 struct aws_byte_cursor key) {
1135
1136 struct aws_http_message *message = aws_http_message_new_request(allocator);
1137
1138 if (message == NULL) {
1139 return NULL;
1140 }
1141
1142 struct aws_http_header host_header = {.name = g_host_header_name, .value = host};
1143
1144 if (aws_http_message_add_header(message, host_header)) {
1145 goto error_clean_up_message;
1146 }
1147
1148 if (aws_http_message_set_request_method(message, aws_http_method_get)) {
1149 goto error_clean_up_message;
1150 }
1151
1152 if (aws_http_message_set_request_path(message, key)) {
1153 goto error_clean_up_message;
1154 }
1155
1156 return message;
1157
1158 error_clean_up_message:
1159
1160 if (message != NULL) {
1161 aws_http_message_release(message);
1162 message = NULL;
1163 }
1164
1165 return NULL;
1166 }
1167
1168 struct aws_s3_client_vtable *aws_s3_tester_patch_client_vtable(
1169 struct aws_s3_tester *tester,
1170 struct aws_s3_client *client,
1171 size_t *out_index) {
1172
1173 struct aws_s3_client_vtable_patch patch;
1174 AWS_ZERO_STRUCT(patch);
1175
1176 /* Push a new vtable patch into the array. */
1177 aws_array_list_push_back(&tester->client_vtable_patches, (void *)&patch);
1178
1179 /* Get a pointer to the new vtable patch. */
1180 size_t index = aws_array_list_length(&tester->client_vtable_patches) - 1;
1181 struct aws_s3_client_vtable_patch *patch_array_ptr = aws_s3_tester_get_client_vtable_patch(tester, index);
1182
1183 /* Cache a pointer to the original vtable. */
1184 patch_array_ptr->original_vtable = client->vtable;
1185
1186 /* Copy the original vtable contents into the patched vtable. */
1187 memcpy(&patch_array_ptr->patched_vtable, patch_array_ptr->original_vtable, sizeof(struct aws_s3_client_vtable));
1188
1189 /* Point the client at the new vtable. */
1190 client->vtable = &patch_array_ptr->patched_vtable;
1191
1192 if (out_index) {
1193 *out_index = index;
1194 }
1195
1196 return &patch_array_ptr->patched_vtable;
1197 }
1198
1199 struct aws_s3_client_vtable_patch *aws_s3_tester_get_client_vtable_patch(struct aws_s3_tester *tester, size_t index) {
1200 struct aws_s3_client_vtable_patch *patch = NULL;
1201 aws_array_list_get_at_ptr(&tester->client_vtable_patches, (void **)&patch, index);
1202 return patch;
1203 }
1204
1205 struct aws_s3_meta_request_vtable *aws_s3_tester_patch_meta_request_vtable(
1206 struct aws_s3_tester *tester,
1207 struct aws_s3_meta_request *meta_request,
1208 size_t *out_index) {
1209
1210 struct aws_s3_meta_request_vtable_patch patch;
1211 AWS_ZERO_STRUCT(patch);
1212
1213 /* Push a new vtable patch into the array. */
1214 aws_array_list_push_back(&tester->meta_request_vtable_patches, (void *)&patch);
1215
1216 /* Get a pointer to the new vtable patch. */
1217 size_t index = aws_array_list_length(&tester->meta_request_vtable_patches) - 1;
1218 struct aws_s3_meta_request_vtable_patch *patch_array_ptr =
1219 aws_s3_tester_get_meta_request_vtable_patch(tester, index);
1220
1221 /* Cache a pointer to the original vtable. */
1222 patch_array_ptr->original_vtable = meta_request->vtable;
1223
1224 /* Copy the original vtable contents into the patched vtable. */
1225 memcpy(
1226 &patch_array_ptr->patched_vtable, patch_array_ptr->original_vtable, sizeof(struct aws_s3_meta_request_vtable));
1227
1228 /* Point the meta request at the new vtable. */
1229 meta_request->vtable = &patch_array_ptr->patched_vtable;
1230
1231 if (out_index) {
1232 *out_index = index;
1233 }
1234
1235 return &patch_array_ptr->patched_vtable;
1236 }
1237
1238 struct aws_s3_meta_request_vtable_patch *aws_s3_tester_get_meta_request_vtable_patch(
1239 struct aws_s3_tester *tester,
1240 size_t index) {
1241 struct aws_s3_meta_request_vtable_patch *patch = NULL;
1242 aws_array_list_get_at_ptr(&tester->meta_request_vtable_patches, (void **)&patch, index);
1243 return patch;
1244 }
1245 struct aws_http_message *aws_s3_test_put_object_request_new_without_body(
1246 struct aws_allocator *allocator,
1247 struct aws_byte_cursor *host,
1248 struct aws_byte_cursor content_type,
1249 struct aws_byte_cursor key,
1250 uint64_t content_length,
1251 uint32_t flags) {
1252
1253 AWS_PRECONDITION(allocator);
1254
1255 struct aws_http_message *message = aws_http_message_new_request(allocator);
1256
1257 if (message == NULL) {
1258 return NULL;
1259 }
1260 if (host) {
1261 struct aws_http_header host_header = {.name = g_host_header_name, .value = *host};
1262 if (aws_http_message_add_header(message, host_header)) {
1263 goto error_clean_up_message;
1264 }
1265 }
1266 struct aws_http_header content_type_header = {.name = g_content_type_header_name, .value = content_type};
1267
1268 char content_length_buffer[64] = "";
1269 snprintf(content_length_buffer, sizeof(content_length_buffer), "%" PRIu64 "", content_length);
1270
1271 struct aws_http_header content_length_header = {
1272 .name = g_content_length_header_name,
1273 .value = aws_byte_cursor_from_c_str(content_length_buffer),
1274 };
1275
1276 struct aws_http_header sse_kms_header = {.name = g_s3_sse_header, .value = aws_byte_cursor_from_c_str("aws:kms")};
1277 struct aws_http_header sse_aes256_header = {.name = g_s3_sse_header, .value = aws_byte_cursor_from_c_str("AES256")};
1278 struct aws_http_header acl_public_read_header = {
1279 .name = g_acl_header_name,
1280 .value = aws_byte_cursor_from_c_str("bucket-owner-read"),
1281 };
1282
1283 if (aws_http_message_add_header(message, content_type_header)) {
1284 goto error_clean_up_message;
1285 }
1286
1287 if (aws_http_message_add_header(message, content_length_header)) {
1288 goto error_clean_up_message;
1289 }
1290
1291 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_KMS) {
1292 if (aws_http_message_add_header(message, sse_kms_header)) {
1293 goto error_clean_up_message;
1294 }
1295 }
1296
1297 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_AES256) {
1298 if (aws_http_message_add_header(message, sse_aes256_header)) {
1299 goto error_clean_up_message;
1300 }
1301 }
1302
1303 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_C_AES256) {
1304 struct aws_http_header sse_c_alg_header = {
1305 .name = g_s3_sse_c_alg_header,
1306 .value = aws_byte_cursor_from_c_str("AES256"),
1307 };
1308 struct aws_http_header sse_c_key_header = {
1309 .name = g_s3_sse_c_key_header,
1310 .value = aws_byte_cursor_from_c_str("MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDE="),
1311 };
1312 struct aws_http_header sse_c_key_md5_header = {
1313 .name = g_s3_sse_c_key_md5_header,
1314 .value = aws_byte_cursor_from_c_str("5inxltxhJQnqmmHfVSQjoA=="),
1315 };
1316 if (aws_http_message_add_header(message, sse_c_alg_header) ||
1317 aws_http_message_add_header(message, sse_c_key_header) ||
1318 aws_http_message_add_header(message, sse_c_key_md5_header)) {
1319 goto error_clean_up_message;
1320 }
1321 }
1322
1323 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_PUT_ACL) {
1324 if (aws_http_message_add_header(message, acl_public_read_header)) {
1325 goto error_clean_up_message;
1326 }
1327 }
1328
1329 if (aws_http_message_set_request_method(message, aws_http_method_put)) {
1330 goto error_clean_up_message;
1331 }
1332
1333 if (aws_http_message_set_request_path(message, key)) {
1334 goto error_clean_up_message;
1335 }
1336
1337 return message;
1338
1339 error_clean_up_message:
1340
1341 if (message != NULL) {
1342 aws_http_message_release(message);
1343 message = NULL;
1344 }
1345
1346 return NULL;
1347 }
1348
1349 /**
1350 * Calculate the in memory checksum based on the checksum config. Initialize and set the out_checksum to the encoded
1351 * checksum result
1352 */
1353 static int s_calculate_in_memory_checksum_helper(
1354 struct aws_allocator *allocator,
1355 struct aws_byte_cursor data,
1356 enum aws_s3_checksum_algorithm algo,
1357 struct aws_byte_buf *out_encoded_checksum) {
1358 AWS_ASSERT(out_encoded_checksum != NULL);
1359
1360 int ret_code = AWS_OP_ERR;
1361 size_t digest_size = aws_get_digest_size_from_checksum_algorithm(algo);
1362 size_t encoded_checksum_len = 0;
1363 if (aws_base64_compute_encoded_len(digest_size, &encoded_checksum_len)) {
1364 return ret_code;
1365 }
1366
1367 aws_byte_buf_init(out_encoded_checksum, allocator, encoded_checksum_len);
1368
1369 struct aws_byte_buf raw_checksum;
1370 aws_byte_buf_init(&raw_checksum, allocator, digest_size);
1371
1372 if (aws_checksum_compute(allocator, algo, &data, &raw_checksum)) {
1373 goto done;
1374 }
1375 struct aws_byte_cursor raw_checksum_cursor = aws_byte_cursor_from_buf(&raw_checksum);
1376 if (aws_base64_encode(&raw_checksum_cursor, out_encoded_checksum)) {
1377 goto done;
1378 }
1379
1380 ret_code = AWS_OP_SUCCESS;
1381 done:
1382 aws_byte_buf_clean_up(&raw_checksum);
1383 return ret_code;
1384 }
1385
1386 struct aws_http_message *aws_s3_test_put_object_request_new(
1387 struct aws_allocator *allocator,
1388 struct aws_byte_cursor *host,
1389 struct aws_byte_cursor key,
1390 struct aws_byte_cursor content_type,
1391 struct aws_input_stream *body_stream,
1392 uint32_t flags) {
1393
1394 AWS_PRECONDITION(allocator);
1395 AWS_PRECONDITION(body_stream);
1396
1397 int64_t body_stream_length = 0;
1398 if (aws_input_stream_get_length(body_stream, &body_stream_length)) {
1399 return NULL;
1400 }
1401
1402 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
1403 allocator, host, content_type, key, (uint64_t)body_stream_length, flags);
1404 if (!message) {
1405 return NULL;
1406 }
1407
1408 aws_http_message_set_body_stream(message, body_stream);
1409 return message;
1410 }
1411
1412 int aws_s3_tester_client_new(
1413 struct aws_s3_tester *tester,
1414 struct aws_s3_tester_client_options *options,
1415 struct aws_s3_client **out_client) {
1416 ASSERT_TRUE(tester != NULL);
1417 ASSERT_TRUE(options != NULL);
1418 ASSERT_TRUE(out_client != NULL);
1419
1420 struct aws_s3_client_config client_config = {
1421 .part_size = options->part_size,
1422 .max_part_size = options->max_part_size,
1423 .s3express_provider_override_factory = options->s3express_provider_override_factory,
1424 .factory_user_data = options->factory_user_data,
1425 .max_active_connections_override = options->max_active_connections_override,
1426 .enable_s3express = options->s3express_provider_override_factory != NULL,
1427 .memory_limit_in_bytes = options->memory_limit_in_bytes,
1428 .buffer_pool_factory_fn = options->buffer_pool_factory_fn,
1429 };
1430 struct aws_http_proxy_options proxy_options = {
1431 .connection_type = AWS_HPCT_HTTP_FORWARD,
1432 .host = aws_byte_cursor_from_c_str("localhost"),
1433 .port = 8899,
1434 };
1435 if (options->use_proxy) {
1436 client_config.proxy_options = &proxy_options;
1437 }
1438 if (options->num_network_interface_names) {
1439 client_config.network_interface_names_array = options->network_interface_names_array;
1440 client_config.num_network_interface_names = options->num_network_interface_names;
1441 }
1442
1443 struct aws_tls_connection_options tls_connection_options;
1444 AWS_ZERO_STRUCT(tls_connection_options);
1445
1446 #ifndef BYO_CRYPTO
1447 struct aws_tls_ctx_options tls_context_options;
1448 aws_tls_ctx_options_init_default_client(&tls_context_options, tester->allocator);
1449
1450 struct aws_tls_ctx *context = aws_tls_client_ctx_new(tester->allocator, &tls_context_options);
1451 aws_tls_connection_options_init_from_ctx(&tls_connection_options, context);
1452 #endif
1453
1454 struct aws_string *endpoint =
1455 aws_s3_tester_build_endpoint_string(tester->allocator, &g_test_bucket_name, &g_test_s3_region);
1456 struct aws_byte_cursor endpoint_cursor = aws_byte_cursor_from_string(endpoint);
1457
1458 tls_connection_options.server_name = aws_string_new_from_cursor(tester->allocator, &endpoint_cursor);
1459
1460 switch (options->tls_usage) {
1461 case AWS_S3_TLS_ENABLED:
1462 client_config.tls_mode = AWS_MR_TLS_ENABLED;
1463 client_config.tls_connection_options = &tls_connection_options;
1464 break;
1465 case AWS_S3_TLS_DISABLED:
1466 client_config.tls_mode = AWS_MR_TLS_DISABLED;
1467 break;
1468 default:
1469 break;
1470 }
1471
1472 ASSERT_SUCCESS(aws_s3_tester_bind_client(
1473 tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
1474
1475 *out_client = aws_s3_client_new(tester->allocator, &client_config);
1476
1477 aws_string_destroy(endpoint);
1478
1479 #ifndef BYO_CRYPTO
1480 aws_tls_ctx_release(context);
1481 aws_tls_ctx_options_clean_up(&tls_context_options);
1482 #endif
1483
1484 aws_tls_connection_options_clean_up(&tls_connection_options);
1485
1486 return AWS_OP_SUCCESS;
1487 }
1488
1489 /* Disable tsan as we hack into the client threaded data */
1490 AWS_SUPPRESS_TSAN
1491 static int s_tester_check_client_thread_data(struct aws_s3_client *client) {
1492 ASSERT_UINT_EQUALS(0, client->threaded_data.num_requests_being_prepared);
1493 ASSERT_UINT_EQUALS(0, client->threaded_data.request_queue_size);
1494 return AWS_OP_SUCCESS;
1495 }
1496
1497 int aws_s3_tester_send_meta_request_with_options(
1498 struct aws_s3_tester *tester,
1499 struct aws_s3_tester_meta_request_options *options,
1500 struct aws_s3_meta_request_test_results *out_results) {
1501 ASSERT_TRUE(options != NULL);
1502
1503 struct aws_allocator *allocator = options->allocator;
1504
1505 struct aws_string *filepath_str = NULL;
1506
1507 struct aws_s3_tester local_tester;
1508 AWS_ZERO_STRUCT(local_tester);
1509 bool clean_up_local_tester = false;
1510
1511 if (tester == NULL) {
1512 ASSERT_TRUE(options->allocator);
1513 ASSERT_SUCCESS(aws_s3_tester_init(options->allocator, &local_tester));
1514 tester = &local_tester;
1515 clean_up_local_tester = true;
1516 } else if (allocator == NULL) {
1517 allocator = tester->allocator;
1518 }
1519
1520 struct aws_s3_client *client = options->client;
1521
1522 struct aws_uri mock_server;
1523 ASSERT_SUCCESS(aws_uri_init_parse(&mock_server, allocator, &g_mock_server_uri));
1524 if (client == NULL) {
1525
1526 if (options->client_options != NULL) {
1527 ASSERT_SUCCESS(aws_s3_tester_client_new(tester, options->client_options, &client));
1528 } else {
1529 struct aws_s3_tester_client_options client_options;
1530 AWS_ZERO_STRUCT(client_options);
1531 ASSERT_SUCCESS(aws_s3_tester_client_new(tester, &client_options, &client));
1532 }
1533
1534 } else {
1535 aws_s3_client_acquire(client);
1536 }
1537
1538 bool disable_trailing_checksum =
1539 options->checksum_algorithm == AWS_SCA_NONE || options->disable_put_trailing_checksum;
1540 struct aws_s3_checksum_config checksum_config = {
1541 .checksum_algorithm = options->checksum_algorithm,
1542 .validate_response_checksum = options->validate_get_response_checksum,
1543 .validate_checksum_algorithms = options->validate_checksum_algorithms,
1544 };
1545 if (!disable_trailing_checksum) {
1546 checksum_config.location = options->checksum_via_header ? AWS_SCL_HEADER : AWS_SCL_TRAILER;
1547 }
1548
1549 struct aws_s3_meta_request_options meta_request_options = {
1550 .type = options->meta_request_type,
1551 .operation_name = options->default_type_options.operation_name,
1552 .message = options->message,
1553 .checksum_config = &checksum_config,
1554 .resume_token = options->put_options.resume_token,
1555 .object_size_hint = options->object_size_hint,
1556 .fio_opts = options->fio_opts,
1557 .part_size = options->part_size,
1558 };
1559
1560 if (options->mock_server) {
1561 meta_request_options.endpoint = &mock_server;
1562 }
1563
1564 if (options->signing_config) {
1565 meta_request_options.signing_config = options->signing_config;
1566 }
1567 struct aws_signing_config_aws signing_config = {
1568 .algorithm = AWS_SIGNING_ALGORITHM_V4_S3EXPRESS,
1569 .service = g_s3express_service_name,
1570 };
1571 meta_request_options.signing_config =
1572 options->use_s3express_signing ? &signing_config : meta_request_options.signing_config;
1573
1574 struct aws_byte_buf input_stream_buffer;
1575 AWS_ZERO_STRUCT(input_stream_buffer);
1576
1577 struct aws_input_stream *input_stream = NULL;
1578 struct aws_async_input_stream *async_stream = NULL;
1579 size_t upload_size_bytes = 0;
1580
1581 if (meta_request_options.message == NULL) {
1582 const struct aws_byte_cursor *bucket_name = options->bucket_name;
1583
1584 if (bucket_name == NULL) {
1585 bucket_name = &g_test_bucket_name;
1586 }
1587
1588 struct aws_string *host_name = NULL;
1589 if (options->mock_server) {
1590 const struct aws_byte_cursor *host_cursor = aws_uri_authority(&mock_server);
1591 host_name = aws_string_new_from_cursor(allocator, host_cursor);
1592 } else if (options->mrap_test) {
1593 host_name = aws_string_new_from_cursor(allocator, &g_test_mrap_endpoint);
1594 } else {
1595 host_name = aws_s3_tester_build_endpoint_string(allocator, bucket_name, &g_test_s3_region);
1596 }
1597 if (meta_request_options.type == AWS_S3_META_REQUEST_TYPE_GET_OBJECT ||
1598 (meta_request_options.type == AWS_S3_META_REQUEST_TYPE_DEFAULT &&
1599 options->default_type_options.mode == AWS_S3_TESTER_DEFAULT_TYPE_MODE_GET)) {
1600
1601 struct aws_http_message *message = aws_s3_test_get_object_request_new(
1602 allocator, aws_byte_cursor_from_string(host_name), options->get_options.object_path);
1603 ASSERT_SUCCESS(aws_s3_message_util_set_multipart_request_path(
1604 allocator,
1605 NULL /*upload_id*/,
1606 options->get_options.part_number,
1607 false /*append_uploads_suffix*/,
1608 message));
1609
1610 if (options->get_options.object_range.ptr != NULL) {
1611 struct aws_http_header range_header = {
1612 .name = g_range_header_name,
1613 .value = options->get_options.object_range,
1614 };
1615
1616 aws_http_message_add_header(message, range_header);
1617 }
1618
1619 if (options->get_options.file_on_disk) {
1620 if (options->get_options.pre_exist_file_length > 0) {
1621 char *buffer = aws_mem_calloc(allocator, (size_t)options->get_options.pre_exist_file_length + 1, 1);
1622 memset(buffer, 'a', (size_t)options->get_options.pre_exist_file_length);
1623 buffer[(size_t)options->get_options.pre_exist_file_length] = '\0';
1624 struct aws_byte_cursor cursor = aws_byte_cursor_from_c_str(buffer);
1625 struct aws_input_stream *buffer_input_stream = aws_input_stream_new_from_cursor(allocator, &cursor);
1626 filepath_str =
1627 aws_s3_tester_create_file(allocator, options->get_options.object_path, buffer_input_stream);
1628 aws_input_stream_release(buffer_input_stream);
1629 aws_mem_release(allocator, buffer);
1630 } else {
1631 filepath_str = aws_s3_tester_create_file(allocator, options->get_options.object_path, NULL);
1632 }
1633 meta_request_options.recv_filepath = aws_byte_cursor_from_string(filepath_str);
1634 meta_request_options.recv_file_option = options->get_options.recv_file_option;
1635 meta_request_options.recv_file_position = options->get_options.recv_file_position;
1636 }
1637 meta_request_options.message = message;
1638 meta_request_options.force_dynamic_part_size = options->get_options.force_dynamic_part_size;
1639 } else if (
1640 meta_request_options.type == AWS_S3_META_REQUEST_TYPE_PUT_OBJECT ||
1641 (meta_request_options.type == AWS_S3_META_REQUEST_TYPE_DEFAULT &&
1642 options->default_type_options.mode == AWS_S3_TESTER_DEFAULT_TYPE_MODE_PUT)) {
1643
1644 uint32_t object_size_mb = options->put_options.object_size_mb;
1645 upload_size_bytes = (size_t)object_size_mb * 1024ULL * 1024ULL;
1646
1647 /* This doesn't do what we think it should because
1648 * g_min_upload_part_size overrides client->part_size */
1649 if (options->put_options.ensure_multipart) {
1650 if (upload_size_bytes == 0) {
1651 upload_size_bytes = client->part_size * 2;
1652 object_size_mb = (uint32_t)(upload_size_bytes / 1024 / 1024);
1653 }
1654
1655 ASSERT_TRUE(upload_size_bytes > client->part_size);
1656 }
1657
1658 struct aws_byte_buf object_path_buffer;
1659 aws_byte_buf_init(&object_path_buffer, allocator, 128);
1660
1661 if (options->put_options.object_path_override.ptr != NULL) {
1662 aws_byte_buf_append_dynamic(&object_path_buffer, &options->put_options.object_path_override);
1663 } else {
1664 char object_path_sprintf_buffer[128] = "";
1665
1666 switch (options->sse_type) {
1667 case AWS_S3_TESTER_SSE_NONE:
1668 snprintf(
1669 object_path_sprintf_buffer,
1670 sizeof(object_path_sprintf_buffer),
1671 "" PRInSTR "-%uMB.txt",
1672 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
1673 object_size_mb);
1674 break;
1675 case AWS_S3_TESTER_SSE_KMS:
1676 snprintf(
1677 object_path_sprintf_buffer,
1678 sizeof(object_path_sprintf_buffer),
1679 "" PRInSTR "-kms-%uMB.txt",
1680 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
1681 object_size_mb);
1682 break;
1683 case AWS_S3_TESTER_SSE_AES256:
1684 snprintf(
1685 object_path_sprintf_buffer,
1686 sizeof(object_path_sprintf_buffer),
1687 "" PRInSTR "-aes256-%uMB.txt",
1688 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
1689 object_size_mb);
1690 break;
1691 case AWS_S3_TESTER_SSE_C_AES256:
1692 snprintf(
1693 object_path_sprintf_buffer,
1694 sizeof(object_path_sprintf_buffer),
1695 "" PRInSTR "-aes256-c-%uMB.txt",
1696 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
1697 object_size_mb);
1698 break;
1699
1700 default:
1701 break;
1702 }
1703
1704 struct aws_byte_cursor sprintf_buffer_cursor = aws_byte_cursor_from_c_str(object_path_sprintf_buffer);
1705 aws_byte_buf_append_dynamic(&object_path_buffer, &sprintf_buffer_cursor);
1706 }
1707
1708 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_buf(&object_path_buffer);
1709 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(host_name);
1710
1711 /* Create "tester" stream with appropriate options */
1712 struct aws_async_input_stream_tester_options stream_options = {
1713 .base =
1714 {
1715 .autogen_length = upload_size_bytes,
1716 .eof_requires_extra_read = options->put_options.eof_requires_extra_read,
1717 .max_bytes_per_read = options->put_options.max_bytes_per_read,
1718 },
1719 };
1720 if (options->put_options.invalid_input_stream) {
1721 stream_options.base.fail_on_nth_read = 1;
1722 stream_options.base.fail_with_error_code = AWS_IO_STREAM_READ_FAILED;
1723 }
1724
1725 if (options->put_options.async_input_stream) {
1726 stream_options.completion_strategy = options->put_options.async_read_strategy;
1727 stream_options.read_duration_ns = MS_TO_NS(100); /* have async reads take a bit of time. */
1728
1729 async_stream = aws_async_input_stream_new_tester(allocator, &stream_options);
1730 ASSERT_NOT_NULL(async_stream);
1731 meta_request_options.send_async_stream = async_stream;
1732 } else {
1733 input_stream = aws_input_stream_new_tester(allocator, &stream_options.base);
1734 ASSERT_NOT_NULL(input_stream);
1735 }
1736 struct aws_byte_buf *out_encoded_checksum = NULL;
1737 if (options->put_options.full_object_checksum != AWS_TEST_FOC_NONE) {
1738 ASSERT_NOT_NULL(input_stream);
1739 struct aws_byte_buf data;
1740 int64_t out_length = 0;
1741 aws_input_stream_get_length(input_stream, &out_length);
1742 aws_byte_buf_init(&data, allocator, (size_t)out_length);
1743 /* Read everything into the buf */
1744 aws_input_stream_read(input_stream, &data);
1745 /* Seek back to beginning for upload. */
1746 aws_input_stream_seek(input_stream, 0, AWS_SSB_BEGIN);
1747 /* Get the checksum from the buf */
1748 out_encoded_checksum = aws_mem_calloc(allocator, 1, sizeof(struct aws_byte_buf));
1749 ASSERT_SUCCESS(s_calculate_in_memory_checksum_helper(
1750 allocator, aws_byte_cursor_from_buf(&data), options->checksum_algorithm, out_encoded_checksum));
1751 aws_byte_buf_clean_up(&data);
1752 }
1753
1754 /* if uploading via filepath, write input_stream out as tmp file on disk, and then upload that */
1755 if (options->put_options.file_on_disk) {
1756 ASSERT_NOT_NULL(input_stream);
1757 filepath_str = aws_s3_tester_create_file(allocator, test_object_path, input_stream);
1758 meta_request_options.send_filepath = aws_byte_cursor_from_string(filepath_str);
1759 input_stream = aws_input_stream_release(input_stream);
1760 }
1761
1762 /* Put together a simple S3 Put Object request. */
1763 struct aws_http_message *message;
1764 if (input_stream != NULL) {
1765 message = aws_s3_test_put_object_request_new(
1766 allocator, &host_cur, test_object_path, g_test_body_content_type, input_stream, options->sse_type);
1767 } else {
1768 message = aws_s3_test_put_object_request_new_without_body(
1769 allocator,
1770 &host_cur,
1771 g_test_body_content_type,
1772 test_object_path,
1773 upload_size_bytes,
1774 options->sse_type);
1775 }
1776
1777 if (options->put_options.full_object_checksum == AWS_TEST_FOC_HEADER) {
1778 struct aws_http_headers *headers = aws_http_message_get_headers(message);
1779 const struct aws_byte_cursor header_name =
1780 aws_get_http_header_name_from_checksum_algorithm(options->checksum_algorithm);
1781 ASSERT_SUCCESS(
1782 aws_http_headers_set(headers, header_name, aws_byte_cursor_from_buf(out_encoded_checksum)));
1783 aws_byte_buf_clean_up(out_encoded_checksum);
1784 aws_mem_release(allocator, out_encoded_checksum);
1785 } else if (options->put_options.full_object_checksum == AWS_TEST_FOC_CALLBACK) {
1786 /* Set the full object checksum via the callback. */
1787 checksum_config.full_object_checksum_callback = s_full_object_checksum_callback;
1788 out_encoded_checksum->allocator = allocator;
1789 checksum_config.user_data = out_encoded_checksum;
1790 }
1791
1792 if (options->put_options.content_length) {
1793 /* make a invalid request */
1794 char content_length_buffer[64] = "";
1795 snprintf(
1796 content_length_buffer, sizeof(content_length_buffer), "%zu", options->put_options.content_length);
1797
1798 struct aws_http_headers *headers = aws_http_message_get_headers(message);
1799 aws_http_headers_set(
1800 headers, g_content_length_header_name, aws_byte_cursor_from_c_str(content_length_buffer));
1801 }
1802
1803 if (options->put_options.skip_content_length) {
1804 struct aws_http_headers *headers = aws_http_message_get_headers(message);
1805 aws_http_headers_erase(headers, g_content_length_header_name);
1806 }
1807
1808 if (options->put_options.invalid_request) {
1809 /* make a invalid request */
1810 aws_http_message_set_request_path(message, aws_byte_cursor_from_c_str("invalid_path"));
1811 }
1812
1813 if (options->put_options.content_encoding.ptr != NULL) {
1814 struct aws_http_header content_encoding_header = {
1815 .name = g_content_encoding_header_name,
1816 .value = options->put_options.content_encoding,
1817 };
1818 aws_http_message_add_header(message, content_encoding_header);
1819 }
1820 if (options->put_options.if_none_match_header.ptr != NULL) {
1821 struct aws_http_header if_none_match_header = {
1822 .name = aws_byte_cursor_from_c_str("if-none-match"),
1823 .value = options->put_options.if_none_match_header,
1824 };
1825 aws_http_message_add_header(message, if_none_match_header);
1826 }
1827 meta_request_options.message = message;
1828 aws_byte_buf_clean_up(&object_path_buffer);
1829 }
1830
1831 ASSERT_TRUE(meta_request_options.message != NULL);
1832
1833 aws_string_destroy(host_name);
1834 } else {
1835 aws_http_message_acquire(meta_request_options.message);
1836
1837 if (options->meta_request_type == AWS_S3_META_REQUEST_TYPE_PUT_OBJECT) {
1838 /* Figure out how much is being uploaded from pre-existing message */
1839 struct aws_input_stream *mystery_stream = aws_http_message_get_body_stream(meta_request_options.message);
1840 int64_t mystery_stream_length = 0;
1841 if (mystery_stream != NULL) {
1842 ASSERT_SUCCESS(aws_input_stream_get_length(mystery_stream, (int64_t *)&mystery_stream_length));
1843 }
1844 upload_size_bytes = (size_t)mystery_stream_length;
1845 }
1846 }
1847
1848 struct aws_s3_meta_request_test_results meta_request_test_results;
1849 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
1850
1851 if (out_results == NULL) {
1852 out_results = &meta_request_test_results;
1853 }
1854
1855 out_results->headers_callback = options->headers_callback;
1856 out_results->body_callback = options->body_callback;
1857 out_results->finish_callback = options->finish_callback;
1858 out_results->progress_callback = options->progress_callback;
1859 out_results->upload_review_callback = options->upload_review_callback;
1860
1861 out_results->algorithm = options->expected_validate_checksum_alg;
1862
1863 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(tester, &meta_request_options, out_results));
1864
1865 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &meta_request_options);
1866
1867 if (meta_request == NULL) {
1868 out_results->finished_error_code = aws_last_error();
1869 }
1870
1871 aws_http_message_release(meta_request_options.message);
1872 meta_request_options.message = NULL;
1873
1874 if (meta_request != NULL) {
1875 /* Wait for the request to finish. */
1876 aws_s3_tester_wait_for_meta_request_finish(tester);
1877 ASSERT_TRUE(aws_s3_meta_request_is_finished(meta_request));
1878 }
1879
1880 switch (options->validate_type) {
1881 case AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_SUCCESS:
1882 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, out_results->finished_error_code);
1883
1884 if (meta_request_options.type == AWS_S3_META_REQUEST_TYPE_GET_OBJECT) {
1885 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(out_results, options->sse_type));
1886 } else if (meta_request_options.type == AWS_S3_META_REQUEST_TYPE_PUT_OBJECT) {
1887 ASSERT_SUCCESS(aws_s3_tester_validate_put_object_results(out_results, options->sse_type));
1888
1889 /* Expected number of bytes should have been read from stream, and reported via progress callbacks */
1890 if (input_stream != NULL && options->put_options.full_object_checksum == AWS_TEST_FOC_NONE) {
1891 ASSERT_UINT_EQUALS(upload_size_bytes, aws_input_stream_tester_total_bytes_read(input_stream));
1892 } else if (async_stream != NULL) {
1893 ASSERT_UINT_EQUALS(upload_size_bytes, aws_async_input_stream_tester_total_bytes_read(async_stream));
1894 }
1895
1896 ASSERT_UINT_EQUALS(upload_size_bytes, out_results->progress.total_bytes_transferred);
1897 if (!options->put_options.skip_content_length) {
1898 ASSERT_UINT_EQUALS(upload_size_bytes, out_results->progress.content_length);
1899 }
1900 }
1901 ASSERT_UINT_EQUALS(0, aws_atomic_load_int(&client->stats.num_requests_in_flight));
1902 ASSERT_UINT_EQUALS(0, aws_atomic_load_int(&client->stats.num_requests_stream_queued_waiting));
1903 ASSERT_UINT_EQUALS(0, aws_atomic_load_int(&client->stats.num_requests_streaming_response));
1904 ASSERT_SUCCESS(s_tester_check_client_thread_data(client));
1905 if (options->get_options.file_on_disk) {
1906 /* Validate the size match. */
1907 ASSERT_NOT_NULL(filepath_str);
1908 FILE *file = aws_fopen(aws_string_c_str(filepath_str), "rb");
1909 ASSERT_NOT_NULL(file);
1910 ASSERT_SUCCESS(aws_file_get_length(file, &out_results->received_file_size));
1911 if (options->get_options.recv_file_option == AWS_S3_RECV_FILE_CREATE_OR_REPLACE) {
1912 ASSERT_UINT_EQUALS(out_results->progress.total_bytes_transferred, out_results->received_file_size);
1913 }
1914 fclose(file);
1915 }
1916 break;
1917 case AWS_S3_TESTER_VALIDATE_TYPE_EXPECT_FAILURE:
1918 ASSERT_FALSE(out_results->finished_error_code == AWS_ERROR_SUCCESS);
1919 break;
1920 case AWS_S3_TESTER_VALIDATE_TYPE_NO_VALIDATE:
1921 break;
1922 default:
1923 ASSERT_TRUE(false);
1924 break;
1925 }
1926
1927 if (meta_request != NULL) {
1928 out_results->part_size = meta_request->part_size;
1929 meta_request = aws_s3_meta_request_release(meta_request);
1930
1931 if (!options->dont_wait_for_shutdown) {
1932 aws_s3_tester_wait_for_meta_request_shutdown(tester);
1933 }
1934 if (filepath_str && out_results->finished_error_code != AWS_ERROR_SUCCESS) {
1935 if (options->get_options.recv_file_delete_on_failure) {
1936 /* Check the file already gone on failure */
1937 ASSERT_FALSE(aws_path_exists(filepath_str));
1938 } else {
1939 ASSERT_TRUE(aws_path_exists(filepath_str));
1940 }
1941 }
1942 }
1943
1944 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
1945
1946 aws_s3_client_release(client);
1947
1948 aws_input_stream_release(input_stream);
1949 input_stream = NULL;
1950
1951 async_stream = aws_async_input_stream_release(async_stream);
1952
1953 aws_byte_buf_clean_up(&input_stream_buffer);
1954
1955 if (clean_up_local_tester) {
1956 aws_s3_tester_clean_up(&local_tester);
1957 }
1958 aws_uri_clean_up(&mock_server);
1959
1960 if (filepath_str) {
1961 aws_file_delete(filepath_str);
1962 aws_string_destroy(filepath_str);
1963 }
1964
1965 return AWS_OP_SUCCESS;
1966 }
1967
1968 int aws_s3_tester_send_meta_request(
1969 struct aws_s3_tester *tester,
1970 struct aws_s3_client *client,
1971 struct aws_s3_meta_request_options *options,
1972 struct aws_s3_meta_request_test_results *test_results,
1973 uint32_t flags) {
1974
1975 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(tester, options, test_results));
1976
1977 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, options);
1978
1979 ASSERT_TRUE(meta_request != NULL);
1980
1981 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_CANCEL) {
1982 /* take a random sleep from 0-1 ms. */
1983 srand((uint32_t)time(NULL));
1984 aws_thread_current_sleep(rand() % MS_TO_NS(1));
1985 aws_s3_meta_request_cancel(meta_request);
1986 }
1987
1988 /* Wait for the request to finish. */
1989 aws_s3_tester_wait_for_meta_request_finish(tester);
1990
1991 ASSERT_TRUE(aws_s3_meta_request_is_finished(meta_request));
1992
1993 aws_s3_tester_lock_synced_data(tester);
1994
1995 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS) {
1996 ASSERT_TRUE(tester->synced_data.finish_error_code == AWS_ERROR_SUCCESS);
1997 } else if (flags & AWS_S3_TESTER_SEND_META_REQUEST_CANCEL) {
1998 ASSERT_TRUE(tester->synced_data.finish_error_code == AWS_ERROR_S3_CANCELED);
1999 } else {
2000 ASSERT_FALSE(tester->synced_data.finish_error_code == AWS_ERROR_SUCCESS);
2001 }
2002
2003 aws_s3_tester_unlock_synced_data(tester);
2004
2005 test_results->part_size = meta_request->part_size;
2006
2007 aws_s3_meta_request_release(meta_request);
2008
2009 if ((flags & AWS_S3_TESTER_SEND_META_REQUEST_DONT_WAIT_FOR_SHUTDOWN) == 0) {
2010 aws_s3_tester_wait_for_meta_request_shutdown(tester);
2011 }
2012
2013 return AWS_OP_SUCCESS;
2014 }
2015
2016 int aws_s3_tester_send_get_object_meta_request(
2017 struct aws_s3_tester *tester,
2018 struct aws_s3_client *client,
2019 struct aws_byte_cursor s3_path,
2020 uint32_t flags,
2021 struct aws_s3_meta_request_test_results *out_results) {
2022
2023 struct aws_string *host_name =
2024 aws_s3_tester_build_endpoint_string(tester->allocator, &g_test_bucket_name, &g_test_s3_region);
2025
2026 /* Put together a simple S3 Get Object request. */
2027 struct aws_http_message *message =
2028 aws_s3_test_get_object_request_new(tester->allocator, aws_byte_cursor_from_string(host_name), s3_path);
2029
2030 struct aws_s3_meta_request_options options;
2031 AWS_ZERO_STRUCT(options);
2032 options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
2033 options.message = message;
2034
2035 /* Trigger accelerating of our Get Object request. */
2036 struct aws_s3_meta_request_test_results meta_request_test_results;
2037 aws_s3_meta_request_test_results_init(&meta_request_test_results, tester->allocator);
2038
2039 if (out_results == NULL) {
2040 out_results = &meta_request_test_results;
2041 }
2042
2043 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(tester, client, &options, out_results, flags));
2044
2045 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS) {
2046 ASSERT_SUCCESS(aws_s3_tester_validate_get_object_results(out_results, flags));
2047 }
2048
2049 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2050
2051 aws_http_message_release(message);
2052 aws_string_destroy(host_name);
2053
2054 return AWS_OP_SUCCESS;
2055 }
2056
2057 int aws_s3_tester_validate_get_object_results(
2058 struct aws_s3_meta_request_test_results *meta_request_test_results,
2059 uint32_t flags) {
2060 AWS_PRECONDITION(meta_request_test_results);
2061 AWS_PRECONDITION(meta_request_test_results->tester);
2062
2063 ASSERT_TRUE(meta_request_test_results->response_headers != NULL);
2064
2065 if (aws_http_headers_has(
2066 meta_request_test_results->response_headers, aws_byte_cursor_from_c_str("Content-Range"))) {
2067 ASSERT_TRUE(meta_request_test_results->finished_response_status == 206);
2068 } else {
2069 ASSERT_TRUE(meta_request_test_results->finished_response_status == 200);
2070 }
2071
2072 ASSERT_TRUE(
2073 meta_request_test_results->finished_response_status == meta_request_test_results->headers_response_status);
2074 ASSERT_TRUE(meta_request_test_results->finished_error_code == AWS_ERROR_SUCCESS);
2075
2076 ASSERT_TRUE(meta_request_test_results->error_response_headers == NULL);
2077 ASSERT_TRUE(meta_request_test_results->error_response_body.len == 0);
2078 ASSERT_NULL(meta_request_test_results->error_response_operation_name);
2079
2080 struct aws_byte_cursor sse_byte_cursor;
2081
2082 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_KMS) {
2083 ASSERT_SUCCESS(
2084 aws_http_headers_get(meta_request_test_results->response_headers, g_s3_sse_header, &sse_byte_cursor));
2085 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&sse_byte_cursor, "aws:kms"));
2086 }
2087
2088 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_AES256) {
2089 ASSERT_SUCCESS(
2090 aws_http_headers_get(meta_request_test_results->response_headers, g_s3_sse_header, &sse_byte_cursor));
2091 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&sse_byte_cursor, "AES256"));
2092 }
2093
2094 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_C_AES256) {
2095 ASSERT_SUCCESS(
2096 aws_http_headers_get(meta_request_test_results->response_headers, g_s3_sse_c_alg_header, &sse_byte_cursor));
2097 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&sse_byte_cursor, "AES256"));
2098 }
2099
2100 uint64_t content_length = 0;
2101 ASSERT_SUCCESS(aws_s3_tester_get_content_length(meta_request_test_results->response_headers, &content_length));
2102
2103 AWS_LOGF_DEBUG(
2104 AWS_LS_S3_GENERAL,
2105 "Content length in header is %" PRIu64 " and received body size is %" PRIu64,
2106 content_length,
2107 meta_request_test_results->progress.total_bytes_transferred);
2108
2109 ASSERT_TRUE(content_length == meta_request_test_results->progress.total_bytes_transferred);
2110 ASSERT_UINT_EQUALS(content_length, meta_request_test_results->progress.total_bytes_transferred);
2111 ASSERT_UINT_EQUALS(content_length, meta_request_test_results->progress.content_length);
2112
2113 return AWS_OP_SUCCESS;
2114 }
2115
2116 /* Avoid using this function as it will soon go away. Use aws_s3_tester_send_meta_request_with_options instead.*/
2117 int aws_s3_tester_send_put_object_meta_request(
2118 struct aws_s3_tester *tester,
2119 struct aws_s3_client *client,
2120 uint32_t file_size_mb,
2121 uint32_t flags,
2122 struct aws_s3_meta_request_test_results *out_results) {
2123 ASSERT_TRUE(tester != NULL);
2124 ASSERT_TRUE(client != NULL);
2125
2126 struct aws_allocator *allocator = tester->allocator;
2127
2128 struct aws_byte_buf test_buffer;
2129 aws_s3_create_test_buffer(allocator, (size_t)file_size_mb * 1024ULL * 1024ULL, &test_buffer);
2130
2131 struct aws_byte_cursor test_body_cursor = aws_byte_cursor_from_buf(&test_buffer);
2132 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &test_body_cursor);
2133
2134 struct aws_string *host_name =
2135 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
2136
2137 char object_path_buffer[128] = "";
2138
2139 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_PUT_ACL) {
2140 snprintf(
2141 object_path_buffer,
2142 sizeof(object_path_buffer),
2143 "" PRInSTR "-acl-public-read-%uMB.txt",
2144 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
2145 file_size_mb);
2146 } else {
2147 snprintf(
2148 object_path_buffer,
2149 sizeof(object_path_buffer),
2150 "" PRInSTR "-%uMB.txt",
2151 AWS_BYTE_CURSOR_PRI(g_put_object_prefix),
2152 file_size_mb);
2153 }
2154 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str(object_path_buffer);
2155
2156 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(host_name);
2157 /* Put together a simple S3 Put Object request. */
2158 struct aws_http_message *message = aws_s3_test_put_object_request_new(
2159 allocator, &host_cur, test_object_path, g_test_body_content_type, input_stream, flags);
2160
2161 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_WITH_CORRECT_CONTENT_MD5) {
2162 ASSERT_SUCCESS(aws_s3_message_util_add_content_md5_header(allocator, &test_buffer, message));
2163 } else if (flags & AWS_S3_TESTER_SEND_META_REQUEST_WITH_INCORRECT_CONTENT_MD5) {
2164 struct aws_http_header content_md5_header = {
2165 .name = g_content_md5_header_name,
2166 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("dummy_content_md5"),
2167 };
2168 ASSERT_SUCCESS(aws_http_message_add_header(message, content_md5_header));
2169 }
2170
2171 struct aws_s3_meta_request_options options;
2172 AWS_ZERO_STRUCT(options);
2173 options.type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT;
2174 options.message = message;
2175
2176 struct aws_s3_meta_request_test_results meta_request_test_results;
2177 aws_s3_meta_request_test_results_init(&meta_request_test_results, allocator);
2178
2179 if (out_results == NULL) {
2180 out_results = &meta_request_test_results;
2181 }
2182
2183 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(tester, client, &options, out_results, flags));
2184
2185 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS) {
2186 ASSERT_SUCCESS(aws_s3_tester_validate_put_object_results(out_results, flags));
2187 }
2188
2189 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results);
2190
2191 aws_http_message_release(message);
2192 message = NULL;
2193
2194 aws_string_destroy(host_name);
2195 host_name = NULL;
2196
2197 aws_input_stream_release(input_stream);
2198 input_stream = NULL;
2199
2200 aws_byte_buf_clean_up(&test_buffer);
2201
2202 return AWS_OP_SUCCESS;
2203 }
2204
2205 /* Avoid using this function as it will soon go away. Use aws_s3_tester_send_meta_request_with_options instead.*/
2206 int aws_s3_tester_validate_put_object_results(
2207 struct aws_s3_meta_request_test_results *meta_request_test_results,
2208 uint32_t flags) {
2209 ASSERT_TRUE(meta_request_test_results->finished_response_status == 200);
2210 ASSERT_TRUE(
2211 meta_request_test_results->finished_response_status == meta_request_test_results->headers_response_status);
2212 ASSERT_TRUE(meta_request_test_results->finished_error_code == AWS_ERROR_SUCCESS);
2213
2214 ASSERT_TRUE(meta_request_test_results->error_response_headers == NULL);
2215 ASSERT_TRUE(meta_request_test_results->error_response_body.len == 0);
2216 ASSERT_NULL(meta_request_test_results->error_response_operation_name);
2217
2218 struct aws_byte_cursor etag_byte_cursor;
2219 AWS_ZERO_STRUCT(etag_byte_cursor);
2220 ASSERT_SUCCESS(
2221 aws_http_headers_get(meta_request_test_results->response_headers, g_etag_header_name, &etag_byte_cursor));
2222 struct aws_byte_cursor sse_byte_cursor;
2223 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_KMS) {
2224 ASSERT_SUCCESS(
2225 aws_http_headers_get(meta_request_test_results->response_headers, g_s3_sse_header, &sse_byte_cursor));
2226 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&sse_byte_cursor, "aws:kms"));
2227 }
2228 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_AES256) {
2229 ASSERT_SUCCESS(
2230 aws_http_headers_get(meta_request_test_results->response_headers, g_s3_sse_header, &sse_byte_cursor));
2231 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&sse_byte_cursor, "AES256"));
2232 }
2233 ASSERT_TRUE(etag_byte_cursor.len > 0);
2234
2235 if (flags & AWS_S3_TESTER_SEND_META_REQUEST_SSE_C_AES256) {
2236 ASSERT_SUCCESS(
2237 aws_http_headers_get(meta_request_test_results->response_headers, g_s3_sse_c_alg_header, &sse_byte_cursor));
2238 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&sse_byte_cursor, "AES256"));
2239 }
2240
2241 struct aws_byte_cursor quote_entity = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("&quot;");
2242
2243 if (etag_byte_cursor.len >= quote_entity.len) {
2244 for (size_t i = 0; i < (etag_byte_cursor.len - quote_entity.len + 1); ++i) {
2245 ASSERT_TRUE(
2246 strncmp((const char *)&etag_byte_cursor.ptr[i], (const char *)quote_entity.ptr, quote_entity.len) != 0);
2247 }
2248 }
2249
2250 return AWS_OP_SUCCESS;
2251 }
2252
2253 int aws_s3_tester_upload_file_path_init(
2254 struct aws_allocator *allocator,
2255 struct aws_byte_buf *out_path_buffer,
2256 struct aws_byte_cursor file_path) {
2257
2258 ASSERT_SUCCESS(aws_byte_buf_init_copy_from_cursor(out_path_buffer, allocator, g_upload_folder));
2259 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(out_path_buffer, &file_path));
2260
2261 return AWS_OP_SUCCESS;
2262 }
2263
2264 int aws_s3_tester_get_content_length(const struct aws_http_headers *headers, uint64_t *out_content_length) {
2265 struct aws_byte_cursor value_cursor;
2266 AWS_ZERO_STRUCT(value_cursor);
2267 ASSERT_SUCCESS(aws_http_headers_get(headers, aws_byte_cursor_from_c_str("Content-Length"), &value_cursor));
2268
2269 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64(value_cursor, out_content_length));
2270 return AWS_OP_SUCCESS;
2271 }
2272
2273 /* The default mock response is:
2274 <?xml version="1.0" encoding="UTF-8"?>
2275 <CreateSessionResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
2276 <Credentials>
2277 <SessionToken>sessionToken</SessionToken>
2278 <SecretAccessKey>secretKey</SecretAccessKey>
2279 <AccessKeyId>accessKeyId</AccessKeyId>
2280 <Expiration>2023-06-26T17:33:30Z</Expiration>
2281 </Credentials>
2282 </CreateSessionResult>
2283 */
2284 int aws_s3_tester_check_s3express_creds_for_default_mock_response(struct aws_credentials *credentials) {
2285
2286 struct aws_byte_cursor result;
2287 bool match = true;
2288 result = aws_credentials_get_access_key_id(credentials);
2289 match &= aws_byte_cursor_eq_c_str(&result, "accessKeyId");
2290 result = aws_credentials_get_secret_access_key(credentials);
2291 match &= aws_byte_cursor_eq_c_str(&result, "secretKey");
2292 result = aws_credentials_get_session_token(credentials);
2293 match &= aws_byte_cursor_eq_c_str(&result, "sessionToken");
2294
2295 uint64_t expiration_secs = aws_credentials_get_expiration_timepoint_seconds(credentials);
2296 ASSERT_UINT_EQUALS(1687800810, expiration_secs);
2297
2298 ASSERT_TRUE(match);
2299 return AWS_OP_SUCCESS;
2300 }
2301
2302 struct aws_string *aws_s3_tester_create_file(
2303 struct aws_allocator *allocator,
2304 struct aws_byte_cursor test_object_path,
2305 struct aws_input_stream *input_stream) {
2306
2307 struct aws_byte_buf filepath_buf;
2308 aws_byte_buf_init(&filepath_buf, allocator, 128);
2309
2310 struct aws_byte_cursor filepath_prefix = aws_byte_cursor_from_c_str("tmp");
2311 aws_byte_buf_append_dynamic(&filepath_buf, &filepath_prefix);
2312 aws_byte_buf_append_dynamic(&filepath_buf, &test_object_path);
2313 struct aws_uuid uuid;
2314 aws_uuid_init(&uuid);
2315 uint8_t uuid_array[AWS_UUID_STR_LEN] = {0};
2316 struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
2317 uuid_buf.len = 0;
2318 aws_uuid_to_str(&uuid, &uuid_buf);
2319 struct aws_byte_cursor uuid_cursor = aws_byte_cursor_from_buf(&uuid_buf);
2320 aws_byte_buf_append_dynamic(&filepath_buf, &uuid_cursor);
2321
2322 for (size_t i = 0; i < filepath_buf.len; ++i) {
2323 if (!isalnum(filepath_buf.buffer[i])) {
2324 filepath_buf.buffer[i] = '_'; /* sanitize filename */
2325 }
2326 }
2327
2328 struct aws_string *filepath_str = aws_string_new_from_buf(allocator, &filepath_buf);
2329 aws_byte_buf_clean_up(&filepath_buf);
2330
2331 if (input_stream) {
2332 FILE *file = aws_fopen(aws_string_c_str(filepath_str), "wb");
2333 AWS_FATAL_ASSERT(file != NULL);
2334 int64_t stream_length = 0;
2335 AWS_FATAL_ASSERT(aws_input_stream_get_length(input_stream, &stream_length) == AWS_OP_SUCCESS);
2336
2337 struct aws_byte_buf data_buf;
2338 AWS_FATAL_ASSERT(aws_byte_buf_init(&data_buf, allocator, (size_t)stream_length) == AWS_OP_SUCCESS);
2339 AWS_FATAL_ASSERT(aws_input_stream_read(input_stream, &data_buf) == AWS_OP_SUCCESS);
2340 AWS_FATAL_ASSERT((size_t)stream_length == data_buf.len);
2341 AWS_FATAL_ASSERT(data_buf.len == fwrite(data_buf.buffer, 1, data_buf.len, file));
2342 fclose(file);
2343 aws_byte_buf_clean_up(&data_buf);
2344 }
2345
2346 return filepath_str;
2347 }
2348
2349 static const struct aws_byte_cursor g_x_amz_copy_source_name =
2350 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source");
2351
2352 static struct aws_http_message *s_copy_object_request_new(
2353 struct aws_allocator *allocator,
2354 struct aws_byte_cursor x_amz_source,
2355 struct aws_byte_cursor endpoint,
2356 struct aws_byte_cursor destination_key) {
2357
2358 AWS_PRECONDITION(allocator);
2359
2360 struct aws_http_message *message = aws_http_message_new_request(allocator);
2361
2362 if (message == NULL) {
2363 return NULL;
2364 }
2365
2366 /* the URI path is / followed by the key */
2367 char destination_path[1024];
2368 snprintf(destination_path, sizeof(destination_path), "/%.*s", (int)destination_key.len, destination_key.ptr);
2369 struct aws_byte_cursor unencoded_destination_path = aws_byte_cursor_from_c_str(destination_path);
2370 struct aws_byte_buf copy_destination_path_encoded;
2371 aws_byte_buf_init(&copy_destination_path_encoded, allocator, 1024);
2372 aws_byte_buf_append_encoding_uri_path(&copy_destination_path_encoded, &unencoded_destination_path);
2373 if (aws_http_message_set_request_path(message, aws_byte_cursor_from_buf(&copy_destination_path_encoded))) {
2374 goto error_clean_up_message;
2375 }
2376
2377 struct aws_http_header host_header = {.name = g_host_header_name, .value = endpoint};
2378 if (aws_http_message_add_header(message, host_header)) {
2379 goto error_clean_up_message;
2380 }
2381
2382 /* Copy Object request has a content length of 0 */
2383 struct aws_http_header content_length_header = {
2384 .name = g_content_length_header_name,
2385 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("0"),
2386 };
2387 if (aws_http_message_add_header(message, content_length_header)) {
2388 goto error_clean_up_message;
2389 }
2390
2391 struct aws_byte_buf copy_source_value_encoded;
2392 aws_byte_buf_init(&copy_source_value_encoded, allocator, 1024);
2393 aws_byte_buf_append_encoding_uri_path(&copy_source_value_encoded, &x_amz_source);
2394
2395 struct aws_http_header copy_source_header = {
2396 .name = g_x_amz_copy_source_name,
2397 .value = aws_byte_cursor_from_buf(&copy_source_value_encoded),
2398 };
2399
2400 if (aws_http_message_add_header(message, copy_source_header)) {
2401 goto error_clean_up_message;
2402 }
2403
2404 if (aws_http_message_set_request_method(message, aws_http_method_put)) {
2405 goto error_clean_up_message;
2406 }
2407
2408 aws_byte_buf_clean_up(&copy_source_value_encoded);
2409 aws_byte_buf_clean_up(&copy_destination_path_encoded);
2410 return message;
2411
2412 error_clean_up_message:
2413
2414 aws_byte_buf_clean_up(&copy_source_value_encoded);
2415 aws_byte_buf_clean_up(&copy_destination_path_encoded);
2416 if (message != NULL) {
2417 aws_http_message_release(message);
2418 message = NULL;
2419 }
2420
2421 return NULL;
2422 }
2423
2424 struct copy_object_test_data {
2425 struct aws_mutex mutex;
2426 struct aws_condition_variable c_var;
2427 bool execution_completed;
2428 bool headers_callback_was_invoked;
2429 int meta_request_error_code;
2430 int response_status_code;
2431 uint64_t progress_callback_content_length;
2432 uint64_t progress_callback_total_bytes_transferred;
2433 };
2434
2435 static void s_copy_object_meta_request_finish(
2436 struct aws_s3_meta_request *meta_request,
2437 const struct aws_s3_meta_request_result *meta_request_result,
2438 void *user_data) {
2439
2440 (void)meta_request;
2441
2442 struct copy_object_test_data *test_data = user_data;
2443
2444 /* if error response body is available, dump it to test result to help investigation of failed tests */
2445 if (meta_request_result->error_response_body != NULL && meta_request_result->error_response_body->len > 0) {
2446 AWS_LOGF_ERROR(
2447 AWS_LS_S3_GENERAL,
2448 "Response error body: %.*s",
2449 (int)meta_request_result->error_response_body->len,
2450 meta_request_result->error_response_body->buffer);
2451 }
2452
2453 aws_mutex_lock(&test_data->mutex);
2454 test_data->meta_request_error_code = meta_request_result->error_code;
2455 test_data->response_status_code = meta_request_result->response_status;
2456 test_data->execution_completed = true;
2457 aws_mutex_unlock(&test_data->mutex);
2458 aws_condition_variable_notify_one(&test_data->c_var);
2459 }
2460
2461 static int s_copy_object_meta_request_headers_callback(
2462 struct aws_s3_meta_request *meta_request,
2463 const struct aws_http_headers *headers,
2464 int response_status,
2465 void *user_data) {
2466
2467 (void)meta_request;
2468 (void)headers;
2469 (void)response_status;
2470
2471 struct copy_object_test_data *test_data = user_data;
2472
2473 aws_mutex_lock(&test_data->mutex);
2474 test_data->headers_callback_was_invoked = true;
2475 aws_mutex_unlock(&test_data->mutex);
2476
2477 return AWS_OP_SUCCESS;
2478 }
2479
2480 static void s_copy_object_meta_request_progress_callback(
2481 struct aws_s3_meta_request *meta_request,
2482 const struct aws_s3_meta_request_progress *progress,
2483 void *user_data) {
2484
2485 (void)meta_request;
2486 struct copy_object_test_data *test_data = user_data;
2487
2488 aws_mutex_lock(&test_data->mutex);
2489 test_data->progress_callback_content_length = progress->content_length;
2490 test_data->progress_callback_total_bytes_transferred += progress->bytes_transferred;
2491 aws_mutex_unlock(&test_data->mutex);
2492 }
2493
2494 static bool s_copy_test_completion_predicate(void *arg) {
2495 struct copy_object_test_data *test_data = arg;
2496 return test_data->execution_completed;
2497 }
2498
2499 int aws_test_s3_copy_object_from_x_amz_copy_source(
2500 struct aws_allocator *allocator,
2501 struct aws_s3_tester *tester,
2502 struct aws_byte_cursor x_amz_copy_source,
2503 struct aws_byte_cursor destination_endpoint,
2504 struct aws_byte_cursor destination_key,
2505 int expected_error_code,
2506 int expected_response_status,
2507 uint64_t expected_size,
2508 bool s3express,
2509 struct aws_byte_cursor copy_source_uri) {
2510
2511 struct aws_s3_client_config client_config;
2512 AWS_ZERO_STRUCT(client_config);
2513 client_config.enable_s3express = s3express;
2514
2515 struct aws_byte_cursor region_cursor = g_test_s3_region;
2516 client_config.region = region_cursor;
2517 ASSERT_SUCCESS(aws_s3_tester_bind_client(tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_SIGNING));
2518
2519 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
2520
2521 /* creates a CopyObject request */
2522 struct aws_http_message *message =
2523 s_copy_object_request_new(allocator, x_amz_copy_source, destination_endpoint, destination_key);
2524
2525 struct copy_object_test_data test_data;
2526 AWS_ZERO_STRUCT(test_data);
2527
2528 struct aws_signing_config_aws s3express_signing_config = {
2529 .algorithm = AWS_SIGNING_ALGORITHM_V4_S3EXPRESS,
2530 .service = g_s3express_service_name,
2531 };
2532 test_data.c_var = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT;
2533 aws_mutex_init(&test_data.mutex);
2534 struct aws_s3_meta_request_options meta_request_options = {
2535 .user_data = &test_data,
2536 .body_callback = NULL,
2537 .finish_callback = s_copy_object_meta_request_finish,
2538 .headers_callback = s_copy_object_meta_request_headers_callback,
2539 .progress_callback = s_copy_object_meta_request_progress_callback,
2540 .message = message,
2541 .shutdown_callback = NULL,
2542 .signing_config = client_config.signing_config,
2543 .type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT,
2544 .copy_source_uri = copy_source_uri};
2545
2546 if (s3express) {
2547 meta_request_options.signing_config = &s3express_signing_config;
2548 }
2549
2550 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &meta_request_options);
2551 if (meta_request == NULL) {
2552 if (expected_error_code == AWS_OP_SUCCESS) {
2553 AWS_FATAL_ASSERT(false && "meta_request is NULL");
2554 } else {
2555 ASSERT_INT_EQUALS(expected_error_code, aws_last_error());
2556 goto cleanup;
2557 }
2558 }
2559
2560 /* wait completion of the meta request */
2561 aws_mutex_lock(&test_data.mutex);
2562 aws_condition_variable_wait_pred(&test_data.c_var, &test_data.mutex, s_copy_test_completion_predicate, &test_data);
2563 aws_mutex_unlock(&test_data.mutex);
2564
2565 /* assert error_code and response_status_code */
2566 ASSERT_INT_EQUALS(expected_error_code, test_data.meta_request_error_code);
2567 ASSERT_INT_EQUALS(expected_response_status, test_data.response_status_code);
2568
2569 /* assert that progress_callback matches the expected size*/
2570 if (test_data.meta_request_error_code == AWS_ERROR_SUCCESS) {
2571 ASSERT_UINT_EQUALS(expected_size, test_data.progress_callback_total_bytes_transferred);
2572 ASSERT_UINT_EQUALS(expected_size, test_data.progress_callback_content_length);
2573 /* assert headers callback was invoked */
2574 ASSERT_TRUE(test_data.headers_callback_was_invoked);
2575 }
2576
2577 cleanup:
2578 aws_s3_meta_request_release(meta_request);
2579 aws_mutex_clean_up(&test_data.mutex);
2580 aws_http_message_destroy(message);
2581 client = aws_s3_client_release(client);
2582
2583 aws_s3_tester_wait_for_client_shutdown(tester);
2584 tester->bound_to_client = false;
2585 aws_s3_tester_lock_synced_data(tester);
2586 tester->synced_data.client_shutdown = false;
2587 aws_s3_tester_unlock_synced_data(tester);
2588
2589 return 0;
2590 }
2591
2592 int aws_test_s3_copy_object_helper(
2593 struct aws_allocator *allocator,
2594 struct aws_s3_tester *tester,
2595 struct aws_byte_cursor source_bucket,
2596 struct aws_byte_cursor source_key,
2597 struct aws_byte_cursor destination_endpoint,
2598 struct aws_byte_cursor destination_key,
2599 int expected_error_code,
2600 int expected_response_status,
2601 uint64_t expected_size,
2602 bool s3_express,
2603 struct aws_byte_cursor copy_source_uri) {
2604
2605 char copy_source_value[1024];
2606 snprintf(
2607 copy_source_value,
2608 sizeof(copy_source_value),
2609 "%.*s/%.*s",
2610 (int)source_bucket.len,
2611 source_bucket.ptr,
2612 (int)source_key.len,
2613 source_key.ptr);
2614
2615 struct aws_byte_cursor x_amz_copy_source = aws_byte_cursor_from_c_str(copy_source_value);
2616
2617 return aws_test_s3_copy_object_from_x_amz_copy_source(
2618 allocator,
2619 tester,
2620 x_amz_copy_source,
2621 destination_endpoint,
2622 destination_key,
2623 expected_error_code,
2624 expected_response_status,
2625 expected_size,
2626 s3_express,
2627 copy_source_uri);
2628 }