| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "aws/s3/private/s3_client_impl.h" |
| 7 |
#include "aws/s3/private/s3_list_objects.h" |
| 8 |
#include "s3_tester.h" |
| 9 |
|
| 10 |
#include <aws/auth/credentials.h> |
| 11 |
#include <aws/io/channel_bootstrap.h> |
| 12 |
#include <aws/io/tls_channel_handler.h> |
| 13 |
#include <aws/testing/aws_test_harness.h> |
| 14 |
|
| 15 |
static int s_test_s3_list_bucket_init_mem_safety(struct aws_allocator *allocator, void *ctx) { |
| 16 |
(void)ctx; |
| 17 |
struct aws_s3_tester tester; |
| 18 |
ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester)); |
| 19 |
|
| 20 |
struct aws_s3_tester_client_options client_options; |
| 21 |
AWS_ZERO_STRUCT(client_options); |
| 22 |
client_options.tls_usage = AWS_S3_TLS_ENABLED; |
| 23 |
|
| 24 |
struct aws_s3_client *client = NULL; |
| 25 |
ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client)); |
| 26 |
|
| 27 |
struct aws_s3_list_objects_params params = { |
| 28 |
.client = client, |
| 29 |
.endpoint = aws_byte_cursor_from_c_str("test-endpoint.com"), |
| 30 |
.bucket_name = aws_byte_cursor_from_c_str("test-bucket"), |
| 31 |
}; |
| 32 |
|
| 33 |
struct aws_s3_paginator *paginator = aws_s3_initiate_list_objects(allocator, ¶ms); |
| 34 |
ASSERT_NOT_NULL(paginator); |
| 35 |
|
| 36 |
aws_s3_paginator_release(paginator); |
| 37 |
aws_s3_client_release(client); |
| 38 |
aws_s3_tester_clean_up(&tester); |
| 39 |
|
| 40 |
return AWS_OP_SUCCESS; |
| 41 |
} |
| 42 |
|
| 43 |
AWS_TEST_CASE(test_s3_list_bucket_init_mem_safety, s_test_s3_list_bucket_init_mem_safety) |
| 44 |
|
| 45 |
static int s_test_s3_list_bucket_init_mem_safety_optional_copies(struct aws_allocator *allocator, void *ctx) { |
| 46 |
(void)ctx; |
| 47 |
struct aws_s3_tester tester; |
| 48 |
ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester)); |
| 49 |
|
| 50 |
struct aws_s3_tester_client_options client_options; |
| 51 |
AWS_ZERO_STRUCT(client_options); |
| 52 |
client_options.tls_usage = AWS_S3_TLS_ENABLED; |
| 53 |
|
| 54 |
struct aws_s3_client *client = NULL; |
| 55 |
ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client)); |
| 56 |
|
| 57 |
struct aws_s3_list_objects_params params = { |
| 58 |
.client = client, |
| 59 |
.endpoint = aws_byte_cursor_from_c_str("test-endpoint.com"), |
| 60 |
.bucket_name = aws_byte_cursor_from_c_str("test-bucket"), |
| 61 |
.prefix = aws_byte_cursor_from_c_str("foo/bar"), |
| 62 |
.delimiter = aws_byte_cursor_from_c_str("/"), |
| 63 |
.continuation_token = aws_byte_cursor_from_c_str("base64_encrypted_thing"), |
| 64 |
}; |
| 65 |
|
| 66 |
struct aws_s3_paginator *paginator = aws_s3_initiate_list_objects(allocator, ¶ms); |
| 67 |
ASSERT_NOT_NULL(paginator); |
| 68 |
|
| 69 |
aws_s3_paginator_release(paginator); |
| 70 |
aws_s3_client_release(client); |
| 71 |
aws_s3_tester_clean_up(&tester); |
| 72 |
|
| 73 |
return AWS_OP_SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
AWS_TEST_CASE( |
| 77 |
test_s3_list_bucket_init_mem_safety_optional_copies, |
| 78 |
s_test_s3_list_bucket_init_mem_safety_optional_copies) |
| 79 |
|
| 80 |
struct list_bucket_test_data { |
| 81 |
struct aws_allocator *allocator; |
| 82 |
struct aws_signing_config_aws signing_config; |
| 83 |
struct aws_mutex mutex; |
| 84 |
struct aws_condition_variable c_var; |
| 85 |
bool done; |
| 86 |
int error_code; |
| 87 |
struct aws_array_list entries_found; |
| 88 |
}; |
| 89 |
|
| 90 |
static bool s_on_paginator_finished_predicate(void *arg) { |
| 91 |
struct list_bucket_test_data *test_data = arg; |
| 92 |
return test_data->done; |
| 93 |
} |
| 94 |
|
| 95 |
static int s_on_list_bucket_valid_object_fn(const struct aws_s3_object_info *info, void *user_data) { |
| 96 |
(void)info; |
| 97 |
struct list_bucket_test_data *test_data = user_data; |
| 98 |
struct aws_string *path = NULL; |
| 99 |
|
| 100 |
if (info->key.len) { |
| 101 |
path = aws_string_new_from_cursor(test_data->allocator, &info->key); |
| 102 |
} else if (info->prefix.len) { |
| 103 |
path = aws_string_new_from_cursor(test_data->allocator, &info->prefix); |
| 104 |
} |
| 105 |
|
| 106 |
aws_array_list_push_back(&test_data->entries_found, &path); |
| 107 |
|
| 108 |
return AWS_OP_SUCCESS; |
| 109 |
} |
| 110 |
|
| 111 |
static void s_on_list_bucket_page_finished_fn(struct aws_s3_paginator *paginator, int error_code, void *user_data) { |
| 112 |
|
| 113 |
struct list_bucket_test_data *test_data = user_data; |
| 114 |
|
| 115 |
test_data->error_code = error_code; |
| 116 |
|
| 117 |
if (!error_code && aws_s3_paginator_has_more_results(paginator)) { |
| 118 |
aws_s3_paginator_continue(paginator, &test_data->signing_config); |
| 119 |
} else { |
| 120 |
aws_mutex_lock(&test_data->mutex); |
| 121 |
test_data->done = true; |
| 122 |
aws_mutex_unlock(&test_data->mutex); |
| 123 |
aws_condition_variable_notify_one(&test_data->c_var); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
static int s_test_s3_list_bucket_valid(struct aws_allocator *allocator, void *ctx) { |
| 128 |
(void)ctx; |
| 129 |
|
| 130 |
struct aws_s3_tester tester; |
| 131 |
ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester)); |
| 132 |
|
| 133 |
struct aws_s3_tester_client_options client_options; |
| 134 |
AWS_ZERO_STRUCT(client_options); |
| 135 |
client_options.tls_usage = AWS_S3_TLS_ENABLED; |
| 136 |
|
| 137 |
struct aws_s3_client *client = NULL; |
| 138 |
ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client)); |
| 139 |
|
| 140 |
struct aws_signing_config_aws signing_config; |
| 141 |
AWS_ZERO_STRUCT(signing_config); |
| 142 |
aws_s3_init_default_signing_config(&signing_config, g_test_s3_region, tester.credentials_provider); |
| 143 |
|
| 144 |
struct list_bucket_test_data test_data = { |
| 145 |
.allocator = allocator, |
| 146 |
.signing_config = signing_config, |
| 147 |
.mutex = AWS_MUTEX_INIT, |
| 148 |
.c_var = AWS_CONDITION_VARIABLE_INIT, |
| 149 |
.done = false, |
| 150 |
}; |
| 151 |
|
| 152 |
ASSERT_SUCCESS(aws_array_list_init_dynamic(&test_data.entries_found, allocator, 16, sizeof(struct aws_string *))); |
| 153 |
|
| 154 |
char endpoint_buff[128] = ""; |
| 155 |
snprintf( |
| 156 |
endpoint_buff, sizeof(endpoint_buff), "s3." PRInSTR ".amazonaws.com", AWS_BYTE_CURSOR_PRI(g_test_s3_region)); |
| 157 |
struct aws_byte_cursor endpoint = aws_byte_cursor_from_c_str(endpoint_buff); |
| 158 |
|
| 159 |
struct aws_s3_list_objects_params params = { |
| 160 |
.client = client, |
| 161 |
.endpoint = endpoint, |
| 162 |
.bucket_name = g_test_bucket_name, |
| 163 |
.on_object = s_on_list_bucket_valid_object_fn, |
| 164 |
.on_list_finished = s_on_list_bucket_page_finished_fn, |
| 165 |
.user_data = &test_data, |
| 166 |
.delimiter = aws_byte_cursor_from_c_str("/"), |
| 167 |
}; |
| 168 |
|
| 169 |
struct aws_s3_paginator *paginator = aws_s3_initiate_list_objects(allocator, ¶ms); |
| 170 |
ASSERT_NOT_NULL(paginator); |
| 171 |
|
| 172 |
aws_mutex_lock(&test_data.mutex); |
| 173 |
aws_s3_paginator_continue(paginator, &signing_config); |
| 174 |
aws_condition_variable_wait_pred(&test_data.c_var, &test_data.mutex, s_on_paginator_finished_predicate, &test_data); |
| 175 |
aws_mutex_unlock(&test_data.mutex); |
| 176 |
|
| 177 |
if (test_data.error_code == AWS_OP_SUCCESS) { |
| 178 |
struct aws_string *path = NULL; |
| 179 |
|
| 180 |
|
| 181 |
size_t length = aws_array_list_length(&test_data.entries_found); |
| 182 |
for (size_t i = 0; i < length; ++i) { |
| 183 |
aws_array_list_get_at(&test_data.entries_found, &path, i); |
| 184 |
ASSERT_TRUE(path->len > 0); |
| 185 |
aws_string_destroy(path); |
| 186 |
} |
| 187 |
ASSERT_TRUE(length > 0); |
| 188 |
} else { |
| 189 |
ASSERT_TRUE( |
| 190 |
false, |
| 191 |
"Failing test because the operation failed with error %s\n", |
| 192 |
aws_error_debug_str(test_data.error_code)); |
| 193 |
} |
| 194 |
|
| 195 |
aws_array_list_clean_up(&test_data.entries_found); |
| 196 |
aws_s3_paginator_release(paginator); |
| 197 |
aws_s3_client_release(client); |
| 198 |
aws_s3_tester_clean_up(&tester); |
| 199 |
|
| 200 |
return AWS_OP_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
AWS_TEST_CASE(test_s3_list_bucket_valid, s_test_s3_list_bucket_valid) |