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