| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/byte_buf.h> |
| 7 |
#include <aws/common/file.h> |
| 8 |
#include <aws/common/hash_table.h> |
| 9 |
#include <aws/common/json.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
#include <aws/sdkutils/endpoints_rule_engine.h> |
| 12 |
#include <aws/sdkutils/partitions.h> |
| 13 |
#include <aws/sdkutils/private/endpoints_types_impl.h> |
| 14 |
#include <aws/testing/aws_test_harness.h> |
| 15 |
#include <time.h> |
| 16 |
|
| 17 |
static int read_file_contents( |
| 18 |
struct aws_byte_buf *out_buf, |
| 19 |
struct aws_allocator *alloc, |
| 20 |
const struct aws_byte_cursor filename_cur) { |
| 21 |
AWS_ZERO_STRUCT(*out_buf); |
| 22 |
struct aws_string *mode = aws_string_new_from_c_str(alloc, "r"); |
| 23 |
struct aws_string *filename = aws_string_new_from_cursor(alloc, &filename_cur); |
| 24 |
FILE *fp = aws_fopen_safe(filename, mode); |
| 25 |
aws_string_destroy(filename); |
| 26 |
aws_string_destroy(mode); |
| 27 |
ASSERT_NOT_NULL(fp); |
| 28 |
|
| 29 |
int64_t file_size = 0; |
| 30 |
ASSERT_SUCCESS(aws_file_get_length(fp, &file_size)); |
| 31 |
|
| 32 |
ASSERT_SUCCESS(aws_byte_buf_init(out_buf, alloc, (size_t)file_size)); |
| 33 |
size_t read = fread(out_buf->buffer, 1, (size_t)file_size, fp); |
| 34 |
fclose(fp); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
out_buf->len = read; |
| 41 |
|
| 42 |
return AWS_OP_SUCCESS; |
| 43 |
} |
| 44 |
|
| 45 |
static int s_add_context(struct aws_endpoints_request_context *context, struct aws_allocator *alloc) { |
| 46 |
struct aws_string *region = aws_string_new_from_c_str(alloc, "Region"); |
| 47 |
|
| 48 |
ASSERT_SUCCESS(aws_endpoints_request_context_add_string( |
| 49 |
alloc, context, aws_byte_cursor_from_string(region), aws_byte_cursor_from_c_str("us-west-2"))); |
| 50 |
aws_string_destroy(region); |
| 51 |
return AWS_OP_SUCCESS; |
| 52 |
} |
| 53 |
|
| 54 |
AWS_TEST_CASE(parse_ruleset_from_string, s_test_parse_ruleset_from_string) |
| 55 |
static int s_test_parse_ruleset_from_string(struct aws_allocator *allocator, void *ctx) { |
| 56 |
(void)ctx; |
| 57 |
|
| 58 |
aws_sdkutils_library_init(allocator); |
| 59 |
|
| 60 |
struct aws_byte_buf buf; |
| 61 |
ASSERT_SUCCESS(read_file_contents(&buf, allocator, aws_byte_cursor_from_c_str("sample_ruleset.json"))); |
| 62 |
struct aws_byte_cursor ruleset_json = aws_byte_cursor_from_buf(&buf); |
| 63 |
|
| 64 |
clock_t begin = clock(); |
| 65 |
struct aws_endpoints_ruleset *ruleset = aws_endpoints_ruleset_new_from_string(allocator, ruleset_json); |
| 66 |
clock_t end = clock(); |
| 67 |
double time_taken = (((double)(end - begin)) / CLOCKS_PER_SEC); |
| 68 |
AWS_LOGF_INFO(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Parsed in(s): %f", time_taken); |
| 69 |
|
| 70 |
ASSERT_NOT_NULL(ruleset); |
| 71 |
|
| 72 |
struct aws_byte_buf partitions_buf; |
| 73 |
ASSERT_SUCCESS( |
| 74 |
read_file_contents(&partitions_buf, allocator, aws_byte_cursor_from_c_str("sample_partitions.json"))); |
| 75 |
struct aws_byte_cursor partitions_json = aws_byte_cursor_from_buf(&partitions_buf); |
| 76 |
struct aws_partitions_config *partitions = aws_partitions_config_new_from_string(allocator, partitions_json); |
| 77 |
|
| 78 |
ASSERT_NOT_NULL(partitions); |
| 79 |
|
| 80 |
const struct aws_hash_table *parameters = aws_endpoints_ruleset_get_parameters(ruleset); |
| 81 |
struct aws_byte_cursor param_name_cur = aws_byte_cursor_from_c_str("Region"); |
| 82 |
struct aws_hash_element *element = NULL; |
| 83 |
aws_hash_table_find(parameters, ¶m_name_cur, &element); |
| 84 |
ASSERT_NOT_NULL(element); |
| 85 |
|
| 86 |
struct aws_byte_cursor built_in = |
| 87 |
aws_endpoints_parameter_get_built_in((struct aws_endpoints_parameter *)element->value); |
| 88 |
ASSERT_CURSOR_VALUE_CSTRING_EQUALS(built_in, "AWS::Region"); |
| 89 |
|
| 90 |
struct aws_endpoints_rule_engine *engine = aws_endpoints_rule_engine_new(allocator, ruleset, partitions); |
| 91 |
|
| 92 |
struct aws_endpoints_request_context *context = aws_endpoints_request_context_new(allocator); |
| 93 |
ASSERT_SUCCESS(s_add_context(context, allocator)); |
| 94 |
|
| 95 |
struct aws_endpoints_resolved_endpoint *resolved_endpoint = NULL; |
| 96 |
clock_t begin_resolve = clock(); |
| 97 |
ASSERT_SUCCESS(aws_endpoints_rule_engine_resolve(engine, context, &resolved_endpoint)); |
| 98 |
clock_t end_resolve = clock(); |
| 99 |
double time_taken_resolve = (((double)(end_resolve - begin_resolve)) / CLOCKS_PER_SEC); |
| 100 |
AWS_LOGF_INFO(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Resolved in(s): %f", time_taken_resolve); |
| 101 |
|
| 102 |
ASSERT_INT_EQUALS(AWS_ENDPOINTS_RESOLVED_ENDPOINT, aws_endpoints_resolved_endpoint_get_type(resolved_endpoint)); |
| 103 |
|
| 104 |
struct aws_byte_cursor url_cur; |
| 105 |
ASSERT_SUCCESS(aws_endpoints_resolved_endpoint_get_url(resolved_endpoint, &url_cur)); |
| 106 |
|
| 107 |
ASSERT_CURSOR_VALUE_CSTRING_EQUALS(url_cur, "https://example.us-west-2.amazonaws.com"); |
| 108 |
|
| 109 |
aws_endpoints_ruleset_release(ruleset); |
| 110 |
aws_partitions_config_release(partitions); |
| 111 |
aws_endpoints_rule_engine_release(engine); |
| 112 |
aws_endpoints_resolved_endpoint_release(resolved_endpoint); |
| 113 |
aws_endpoints_request_context_release(context); |
| 114 |
aws_byte_buf_clean_up(&buf); |
| 115 |
aws_byte_buf_clean_up(&partitions_buf); |
| 116 |
aws_sdkutils_library_clean_up(); |
| 117 |
return AWS_OP_SUCCESS; |
| 118 |
} |
| 119 |
|
| 120 |
struct iteration_wrapper { |
| 121 |
struct aws_allocator *allocator; |
| 122 |
struct aws_endpoints_request_context *context; |
| 123 |
}; |
| 124 |
|
| 125 |
enum { |
| 126 |
|
| 127 |
MAX_STRING_ARRAY_ELEMENTS = 4 |
| 128 |
}; |
| 129 |
|
| 130 |
static int s_on_parameter_key( |
| 131 |
const struct aws_byte_cursor *key, |
| 132 |
const struct aws_json_value *value, |
| 133 |
bool *out_should_continue, |
| 134 |
void *user_data) { |
| 135 |
(void)out_should_continue; |
| 136 |
|
| 137 |
struct iteration_wrapper *wrapper = user_data; |
| 138 |
|
| 139 |
if (aws_json_value_is_string(value)) { |
| 140 |
struct aws_byte_cursor cur; |
| 141 |
ASSERT_SUCCESS(aws_json_value_get_string(value, &cur)); |
| 142 |
ASSERT_SUCCESS(aws_endpoints_request_context_add_string(wrapper->allocator, wrapper->context, *key, cur)); |
| 143 |
return AWS_OP_SUCCESS; |
| 144 |
} else if (aws_json_value_is_boolean(value)) { |
| 145 |
bool b; |
| 146 |
ASSERT_SUCCESS(aws_json_value_get_boolean(value, &b)); |
| 147 |
ASSERT_SUCCESS(aws_endpoints_request_context_add_boolean(wrapper->allocator, wrapper->context, *key, b)); |
| 148 |
return AWS_OP_SUCCESS; |
| 149 |
} else if (aws_json_value_is_array(value)) { |
| 150 |
struct aws_byte_cursor strings[MAX_STRING_ARRAY_ELEMENTS]; |
| 151 |
size_t len = aws_json_get_array_size(value); |
| 152 |
ASSERT_TRUE(len <= MAX_STRING_ARRAY_ELEMENTS); |
| 153 |
for (size_t i = 0; i < len; ++i) { |
| 154 |
struct aws_json_value *str = aws_json_get_array_element(value, i); |
| 155 |
ASSERT_SUCCESS(aws_json_value_get_string(str, &strings[i])); |
| 156 |
} |
| 157 |
ASSERT_SUCCESS( |
| 158 |
aws_endpoints_request_context_add_string_array(wrapper->allocator, wrapper->context, *key, strings, len)); |
| 159 |
return AWS_OP_SUCCESS; |
| 160 |
} |
| 161 |
|
| 162 |
return AWS_OP_ERR; |
| 163 |
} |
| 164 |
|
| 165 |
struct headers_wrapper { |
| 166 |
struct aws_allocator *allocator; |
| 167 |
const struct aws_hash_table *headers; |
| 168 |
}; |
| 169 |
|
| 170 |
static int s_on_header_key( |
| 171 |
const struct aws_byte_cursor *key, |
| 172 |
const struct aws_json_value *value, |
| 173 |
bool *out_should_continue, |
| 174 |
void *user_data) { |
| 175 |
(void)out_should_continue; |
| 176 |
|
| 177 |
struct headers_wrapper *wrapper = user_data; |
| 178 |
|
| 179 |
struct aws_string *key_string = aws_string_new_from_cursor(wrapper->allocator, key); |
| 180 |
struct aws_hash_element *element = NULL; |
| 181 |
|
| 182 |
ASSERT_SUCCESS(aws_hash_table_find(wrapper->headers, key_string, &element)); |
| 183 |
|
| 184 |
ASSERT_NOT_NULL(element); |
| 185 |
|
| 186 |
struct aws_array_list *header_values = element->value; |
| 187 |
ASSERT_NOT_NULL(header_values); |
| 188 |
|
| 189 |
ASSERT_INT_EQUALS(aws_json_get_array_size(value), aws_array_list_length(header_values)); |
| 190 |
|
| 191 |
for (size_t i = 0; i < aws_json_get_array_size(value); ++i) { |
| 192 |
struct aws_json_value *val = aws_json_get_array_element(value, i); |
| 193 |
struct aws_byte_cursor cur; |
| 194 |
ASSERT_SUCCESS(aws_json_value_get_string(val, &cur)); |
| 195 |
|
| 196 |
bool found_match = false; |
| 197 |
for (size_t j = 0; j < aws_array_list_length(header_values); ++j) { |
| 198 |
struct aws_string *header_val = NULL; |
| 199 |
ASSERT_SUCCESS(aws_array_list_get_at(header_values, &header_val, j)); |
| 200 |
if (aws_string_eq_byte_cursor(header_val, &cur)) { |
| 201 |
found_match = true; |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
ASSERT_TRUE(found_match); |
| 207 |
} |
| 208 |
|
| 209 |
aws_string_destroy(key_string); |
| 210 |
|
| 211 |
return AWS_OP_SUCCESS; |
| 212 |
} |
| 213 |
|
| 214 |
static int eval_expected(struct aws_allocator *allocator, struct aws_byte_cursor file_name) { |
| 215 |
aws_sdkutils_library_init(allocator); |
| 216 |
|
| 217 |
struct aws_byte_buf ruleset_file_path; |
| 218 |
ASSERT_SUCCESS( |
| 219 |
aws_byte_buf_init_copy_from_cursor(&ruleset_file_path, allocator, aws_byte_cursor_from_c_str("valid-rules/"))); |
| 220 |
ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&ruleset_file_path, &file_name)); |
| 221 |
|
| 222 |
struct aws_byte_buf test_cases_file_path; |
| 223 |
ASSERT_SUCCESS(aws_byte_buf_init_copy_from_cursor( |
| 224 |
&test_cases_file_path, allocator, aws_byte_cursor_from_c_str("test-cases/"))); |
| 225 |
ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&test_cases_file_path, &file_name)); |
| 226 |
|
| 227 |
struct aws_byte_buf ruleset_buf; |
| 228 |
ASSERT_SUCCESS(read_file_contents(&ruleset_buf, allocator, aws_byte_cursor_from_buf(&ruleset_file_path))); |
| 229 |
struct aws_byte_cursor ruleset_json = aws_byte_cursor_from_buf(&ruleset_buf); |
| 230 |
|
| 231 |
clock_t begin = clock(); |
| 232 |
struct aws_endpoints_ruleset *ruleset = aws_endpoints_ruleset_new_from_string(allocator, ruleset_json); |
| 233 |
clock_t end = clock(); |
| 234 |
double time_taken = (((double)(end - begin)) / CLOCKS_PER_SEC); |
| 235 |
AWS_LOGF_INFO(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Parsed in(s): %f", time_taken); |
| 236 |
|
| 237 |
ASSERT_NOT_NULL(ruleset); |
| 238 |
|
| 239 |
struct aws_byte_buf partitions_buf; |
| 240 |
ASSERT_SUCCESS(read_file_contents(&partitions_buf, allocator, aws_byte_cursor_from_c_str("partitions.json"))); |
| 241 |
struct aws_byte_cursor partitions_json = aws_byte_cursor_from_buf(&partitions_buf); |
| 242 |
struct aws_partitions_config *partitions = aws_partitions_config_new_from_string(allocator, partitions_json); |
| 243 |
|
| 244 |
struct aws_endpoints_rule_engine *engine = aws_endpoints_rule_engine_new(allocator, ruleset, partitions); |
| 245 |
|
| 246 |
struct aws_byte_buf test_cases_buf; |
| 247 |
if (read_file_contents(&test_cases_buf, allocator, aws_byte_cursor_from_buf(&test_cases_file_path))) { |
| 248 |
AWS_LOGF_INFO( |
| 249 |
AWS_LS_SDKUTILS_ENDPOINTS_PARSING, |
| 250 |
"Ruleset has no associated test cases: " PRInSTR, |
| 251 |
AWS_BYTE_CURSOR_PRI(file_name)); |
| 252 |
goto skip_test_cases; |
| 253 |
} |
| 254 |
struct aws_byte_cursor test_cases_json = aws_byte_cursor_from_buf(&test_cases_buf); |
| 255 |
|
| 256 |
struct aws_json_value *test_cases = aws_json_value_new_from_string(allocator, test_cases_json); |
| 257 |
struct aws_json_value *tests = aws_json_value_get_from_object(test_cases, aws_byte_cursor_from_c_str("testCases")); |
| 258 |
|
| 259 |
for (size_t i = 0; i < aws_json_get_array_size(tests); ++i) { |
| 260 |
struct aws_endpoints_request_context *context = aws_endpoints_request_context_new(allocator); |
| 261 |
struct aws_json_value *test = aws_json_get_array_element(tests, i); |
| 262 |
|
| 263 |
struct aws_byte_cursor documentation; |
| 264 |
struct aws_json_value *doc_json = |
| 265 |
aws_json_value_get_from_object(test, aws_byte_cursor_from_c_str("documentation")); |
| 266 |
ASSERT_SUCCESS(aws_json_value_get_string(doc_json, &documentation)); |
| 267 |
|
| 268 |
AWS_LOGF_INFO(0, "Running test case #%zu: " PRInSTR, i, AWS_BYTE_CURSOR_PRI(documentation)); |
| 269 |
|
| 270 |
struct aws_json_value *params = aws_json_value_get_from_object(test, aws_byte_cursor_from_c_str("params")); |
| 271 |
struct iteration_wrapper wrapper = {.allocator = allocator, .context = context}; |
| 272 |
ASSERT_SUCCESS(aws_json_const_iterate_object(params, s_on_parameter_key, &wrapper)); |
| 273 |
|
| 274 |
struct aws_endpoints_resolved_endpoint *resolved_endpoint = NULL; |
| 275 |
clock_t begin_resolve = clock(); |
| 276 |
ASSERT_SUCCESS(aws_endpoints_rule_engine_resolve(engine, context, &resolved_endpoint)); |
| 277 |
clock_t end_resolve = clock(); |
| 278 |
double time_taken_resolve = (((double)(end_resolve - begin_resolve)) / CLOCKS_PER_SEC); |
| 279 |
AWS_LOGF_INFO(0, "Resolved in(s): %f", time_taken_resolve); |
| 280 |
|
| 281 |
struct aws_json_value *expect = aws_json_value_get_from_object(test, aws_byte_cursor_from_c_str("expect")); |
| 282 |
struct aws_json_value *endpoint = |
| 283 |
aws_json_value_get_from_object(expect, aws_byte_cursor_from_c_str("endpoint")); |
| 284 |
|
| 285 |
if (endpoint != NULL) { |
| 286 |
ASSERT_INT_EQUALS( |
| 287 |
AWS_ENDPOINTS_RESOLVED_ENDPOINT, aws_endpoints_resolved_endpoint_get_type(resolved_endpoint)); |
| 288 |
struct aws_byte_cursor url; |
| 289 |
ASSERT_SUCCESS(aws_endpoints_resolved_endpoint_get_url(resolved_endpoint, &url)); |
| 290 |
struct aws_json_value *expected_url_node = |
| 291 |
aws_json_value_get_from_object(endpoint, aws_byte_cursor_from_c_str("url")); |
| 292 |
struct aws_byte_cursor expected_url; |
| 293 |
aws_json_value_get_string(expected_url_node, &expected_url); |
| 294 |
ASSERT_TRUE(aws_byte_cursor_eq(&url, &expected_url)); |
| 295 |
|
| 296 |
struct aws_byte_cursor properties; |
| 297 |
ASSERT_SUCCESS(aws_endpoints_resolved_endpoint_get_properties(resolved_endpoint, &properties)); |
| 298 |
|
| 299 |
struct aws_json_value *properties_json = aws_json_value_new_from_string(allocator, properties); |
| 300 |
struct aws_json_value *expected_properties = |
| 301 |
aws_json_value_get_from_object(endpoint, aws_byte_cursor_from_c_str("properties")); |
| 302 |
|
| 303 |
ASSERT_TRUE(expected_properties == NULL ? properties.len == 0 : properties.len > 0); |
| 304 |
|
| 305 |
if (expected_properties != NULL) { |
| 306 |
ASSERT_TRUE(aws_json_value_compare(properties_json, expected_properties, false)); |
| 307 |
} |
| 308 |
|
| 309 |
aws_json_value_destroy(properties_json); |
| 310 |
|
| 311 |
const struct aws_hash_table *headers; |
| 312 |
ASSERT_SUCCESS(aws_endpoints_resolved_endpoint_get_headers(resolved_endpoint, &headers)); |
| 313 |
struct aws_json_value *expected_headers_node = |
| 314 |
aws_json_value_get_from_object(endpoint, aws_byte_cursor_from_c_str("headers")); |
| 315 |
if (expected_headers_node) { |
| 316 |
struct headers_wrapper headers_wrapper = {.allocator = allocator, .headers = headers}; |
| 317 |
ASSERT_SUCCESS(aws_json_const_iterate_object(expected_headers_node, s_on_header_key, &headers_wrapper)); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
struct aws_json_value *error_node = aws_json_value_get_from_object(expect, aws_byte_cursor_from_c_str("error")); |
| 322 |
if (error_node != NULL) { |
| 323 |
ASSERT_INT_EQUALS( |
| 324 |
AWS_ENDPOINTS_RESOLVED_ERROR, aws_endpoints_resolved_endpoint_get_type(resolved_endpoint)); |
| 325 |
struct aws_byte_cursor error; |
| 326 |
ASSERT_SUCCESS(aws_endpoints_resolved_endpoint_get_error(resolved_endpoint, &error)); |
| 327 |
struct aws_byte_cursor expected_error; |
| 328 |
ASSERT_SUCCESS(aws_json_value_get_string(error_node, &expected_error)); |
| 329 |
|
| 330 |
ASSERT_TRUE(aws_byte_cursor_eq(&error, &expected_error)); |
| 331 |
} |
| 332 |
|
| 333 |
aws_endpoints_resolved_endpoint_release(resolved_endpoint); |
| 334 |
aws_endpoints_request_context_release(context); |
| 335 |
} |
| 336 |
|
| 337 |
aws_json_value_destroy(test_cases); |
| 338 |
aws_byte_buf_clean_up(&test_cases_buf); |
| 339 |
|
| 340 |
skip_test_cases: |
| 341 |
aws_endpoints_ruleset_release(ruleset); |
| 342 |
aws_partitions_config_release(partitions); |
| 343 |
aws_endpoints_rule_engine_release(engine); |
| 344 |
aws_byte_buf_clean_up(&ruleset_file_path); |
| 345 |
aws_byte_buf_clean_up(&ruleset_buf); |
| 346 |
aws_byte_buf_clean_up(&partitions_buf); |
| 347 |
aws_byte_buf_clean_up(&test_cases_file_path); |
| 348 |
|
| 349 |
return AWS_OP_SUCCESS; |
| 350 |
} |
| 351 |
|
| 352 |
AWS_TEST_CASE(test_endpoints_aws_region, s_test_endpoints_aws_region) |
| 353 |
static int s_test_endpoints_aws_region(struct aws_allocator *allocator, void *ctx) { |
| 354 |
(void)ctx; |
| 355 |
|
| 356 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("aws-region.json"))); |
| 357 |
|
| 358 |
return AWS_OP_SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
AWS_TEST_CASE(test_endpoints_default_values, s_test_endpoints_default_values) |
| 362 |
static int s_test_endpoints_default_values(struct aws_allocator *allocator, void *ctx) { |
| 363 |
(void)ctx; |
| 364 |
|
| 365 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("default-values.json"))); |
| 366 |
|
| 367 |
return AWS_OP_SUCCESS; |
| 368 |
} |
| 369 |
|
| 370 |
AWS_TEST_CASE(test_endpoints_eventbridge, s_test_endpoints_eventbridge) |
| 371 |
static int s_test_endpoints_eventbridge(struct aws_allocator *allocator, void *ctx) { |
| 372 |
(void)ctx; |
| 373 |
|
| 374 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("eventbridge.json"))); |
| 375 |
|
| 376 |
return AWS_OP_SUCCESS; |
| 377 |
} |
| 378 |
|
| 379 |
AWS_TEST_CASE(test_endpoints_fns, s_test_endpoints_fns) |
| 380 |
static int s_test_endpoints_fns(struct aws_allocator *allocator, void *ctx) { |
| 381 |
(void)ctx; |
| 382 |
|
| 383 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("fns.json"))); |
| 384 |
|
| 385 |
return AWS_OP_SUCCESS; |
| 386 |
} |
| 387 |
|
| 388 |
AWS_TEST_CASE(test_endpoints_get_attr_type_inference, s_test_endpoints_get_attr_type_inference) |
| 389 |
static int s_test_endpoints_get_attr_type_inference(struct aws_allocator *allocator, void *ctx) { |
| 390 |
(void)ctx; |
| 391 |
|
| 392 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("get-attr-type-inference.json"))); |
| 393 |
|
| 394 |
return AWS_OP_SUCCESS; |
| 395 |
} |
| 396 |
|
| 397 |
AWS_TEST_CASE(test_endpoints_headers, s_test_endpoints_headers) |
| 398 |
static int s_test_endpoints_headers(struct aws_allocator *allocator, void *ctx) { |
| 399 |
(void)ctx; |
| 400 |
|
| 401 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("headers.json"))); |
| 402 |
|
| 403 |
return AWS_OP_SUCCESS; |
| 404 |
} |
| 405 |
|
| 406 |
AWS_TEST_CASE(test_endpoints_is_virtual_hostable_s3_bucket, s_test_endpoints_is_virtual_hostable_s3_bucket) |
| 407 |
static int s_test_endpoints_is_virtual_hostable_s3_bucket(struct aws_allocator *allocator, void *ctx) { |
| 408 |
(void)ctx; |
| 409 |
|
| 410 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("is-virtual-hostable-s3-bucket.json"))); |
| 411 |
|
| 412 |
return AWS_OP_SUCCESS; |
| 413 |
} |
| 414 |
|
| 415 |
AWS_TEST_CASE(test_endpoints_region_override, s_test_endpoints_region_override) |
| 416 |
static int s_test_endpoints_region_override(struct aws_allocator *allocator, void *ctx) { |
| 417 |
(void)ctx; |
| 418 |
|
| 419 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("region-override.json"))); |
| 420 |
|
| 421 |
return AWS_OP_SUCCESS; |
| 422 |
} |
| 423 |
|
| 424 |
AWS_TEST_CASE(test_endpoints_minimal_ruleset, s_test_endpoints_minimal_ruleset) |
| 425 |
static int s_test_endpoints_minimal_ruleset(struct aws_allocator *allocator, void *ctx) { |
| 426 |
(void)ctx; |
| 427 |
|
| 428 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("minimal-ruleset.json"))); |
| 429 |
|
| 430 |
return AWS_OP_SUCCESS; |
| 431 |
} |
| 432 |
|
| 433 |
AWS_TEST_CASE(test_endpoints_parse_arn, s_test_endpoints_parse_arn) |
| 434 |
static int s_test_endpoints_parse_arn(struct aws_allocator *allocator, void *ctx) { |
| 435 |
(void)ctx; |
| 436 |
|
| 437 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("parse-arn.json"))); |
| 438 |
|
| 439 |
return AWS_OP_SUCCESS; |
| 440 |
} |
| 441 |
|
| 442 |
AWS_TEST_CASE(test_endpoints_parse_url, s_test_endpoints_parse_url) |
| 443 |
static int s_test_endpoints_parse_url(struct aws_allocator *allocator, void *ctx) { |
| 444 |
(void)ctx; |
| 445 |
|
| 446 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("parse-url.json"))); |
| 447 |
|
| 448 |
return AWS_OP_SUCCESS; |
| 449 |
} |
| 450 |
|
| 451 |
AWS_TEST_CASE(test_endpoints_partition_fn, s_test_endpoints_partition_fn) |
| 452 |
static int s_test_endpoints_partition_fn(struct aws_allocator *allocator, void *ctx) { |
| 453 |
(void)ctx; |
| 454 |
|
| 455 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("partition-fn.json"))); |
| 456 |
|
| 457 |
return AWS_OP_SUCCESS; |
| 458 |
} |
| 459 |
|
| 460 |
AWS_TEST_CASE(test_endpoints_substring, s_test_endpoints_substring) |
| 461 |
static int s_test_endpoints_substring(struct aws_allocator *allocator, void *ctx) { |
| 462 |
(void)ctx; |
| 463 |
|
| 464 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("substring.json"))); |
| 465 |
|
| 466 |
return AWS_OP_SUCCESS; |
| 467 |
} |
| 468 |
|
| 469 |
AWS_TEST_CASE(test_endpoints_uri_encode, s_test_endpoints_uri_encode) |
| 470 |
static int s_test_endpoints_uri_encode(struct aws_allocator *allocator, void *ctx) { |
| 471 |
(void)ctx; |
| 472 |
|
| 473 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("uri-encode.json"))); |
| 474 |
|
| 475 |
return AWS_OP_SUCCESS; |
| 476 |
} |
| 477 |
|
| 478 |
AWS_TEST_CASE(test_endpoints_valid_hostlabel, s_test_endpoints_valid_hostlabel) |
| 479 |
static int s_test_endpoints_valid_hostlabel(struct aws_allocator *allocator, void *ctx) { |
| 480 |
(void)ctx; |
| 481 |
|
| 482 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("valid-hostlabel.json"))); |
| 483 |
|
| 484 |
return AWS_OP_SUCCESS; |
| 485 |
} |
| 486 |
|
| 487 |
AWS_TEST_CASE(test_endpoints_condition_mem_clean_up, s_test_condition_mem_clean_up) |
| 488 |
static int s_test_condition_mem_clean_up(struct aws_allocator *allocator, void *ctx) { |
| 489 |
(void)ctx; |
| 490 |
|
| 491 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("custom_object_condition.json"))); |
| 492 |
|
| 493 |
return AWS_OP_SUCCESS; |
| 494 |
} |
| 495 |
|
| 496 |
AWS_TEST_CASE(test_endpoints_custom, s_test_endpoints_custom) |
| 497 |
static int s_test_endpoints_custom(struct aws_allocator *allocator, void *ctx) { |
| 498 |
(void)ctx; |
| 499 |
|
| 500 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("custom_partition.json"))); |
| 501 |
|
| 502 |
return AWS_OP_SUCCESS; |
| 503 |
} |
| 504 |
|
| 505 |
AWS_TEST_CASE(test_endpoints_string_array, s_test_endpoints_string_array) |
| 506 |
static int s_test_endpoints_string_array(struct aws_allocator *allocator, void *ctx) { |
| 507 |
(void)ctx; |
| 508 |
|
| 509 |
ASSERT_SUCCESS(eval_expected(allocator, aws_byte_cursor_from_c_str("string_array.json"))); |
| 510 |
|
| 511 |
return AWS_OP_SUCCESS; |
| 512 |
} |
| 513 |
|
| 514 |
AWS_TEST_CASE(test_endpoints_malformed_no_required_default, s_test_endpoints_malformed_no_required_default) |
| 515 |
static int s_test_endpoints_malformed_no_required_default(struct aws_allocator *allocator, void *ctx) { |
| 516 |
(void)ctx; |
| 517 |
|
| 518 |
aws_sdkutils_library_init(allocator); |
| 519 |
|
| 520 |
struct aws_byte_buf buf; |
| 521 |
ASSERT_SUCCESS( |
| 522 |
read_file_contents(&buf, allocator, aws_byte_cursor_from_c_str("malformed-rules/no_default_on_required.json"))); |
| 523 |
struct aws_byte_cursor ruleset_json = aws_byte_cursor_from_buf(&buf); |
| 524 |
|
| 525 |
struct aws_endpoints_ruleset *ruleset = aws_endpoints_ruleset_new_from_string(allocator, ruleset_json); |
| 526 |
ASSERT_NOT_NULL(ruleset); |
| 527 |
|
| 528 |
struct aws_byte_buf partitions_buf; |
| 529 |
ASSERT_SUCCESS( |
| 530 |
read_file_contents(&partitions_buf, allocator, aws_byte_cursor_from_c_str("sample_partitions.json"))); |
| 531 |
struct aws_byte_cursor partitions_json = aws_byte_cursor_from_buf(&partitions_buf); |
| 532 |
struct aws_partitions_config *partitions = aws_partitions_config_new_from_string(allocator, partitions_json); |
| 533 |
ASSERT_NOT_NULL(partitions); |
| 534 |
|
| 535 |
struct aws_endpoints_rule_engine *engine = aws_endpoints_rule_engine_new(allocator, ruleset, partitions); |
| 536 |
ASSERT_NOT_NULL(engine); |
| 537 |
struct aws_endpoints_request_context *context = aws_endpoints_request_context_new(allocator); |
| 538 |
ASSERT_NOT_NULL(context); |
| 539 |
struct aws_endpoints_resolved_endpoint *resolved = NULL; |
| 540 |
ASSERT_ERROR( |
| 541 |
AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED, |
| 542 |
aws_endpoints_rule_engine_resolve(engine, context, &resolved)); |
| 543 |
|
| 544 |
aws_endpoints_ruleset_release(ruleset); |
| 545 |
aws_partitions_config_release(partitions); |
| 546 |
aws_endpoints_rule_engine_release(engine); |
| 547 |
aws_endpoints_request_context_release(context); |
| 548 |
aws_byte_buf_clean_up(&buf); |
| 549 |
aws_byte_buf_clean_up(&partitions_buf); |
| 550 |
aws_sdkutils_library_clean_up(); |
| 551 |
|
| 552 |
return AWS_OP_SUCCESS; |
| 553 |
} |
| 554 |
|
| 555 |
AWS_TEST_CASE(test_endpoints_malformed_regex, s_test_endpoints_malformed_regex) |
| 556 |
static int s_test_endpoints_malformed_regex(struct aws_allocator *allocator, void *ctx) { |
| 557 |
(void)ctx; |
| 558 |
|
| 559 |
aws_sdkutils_library_init(allocator); |
| 560 |
|
| 561 |
struct aws_byte_buf partitions_buf; |
| 562 |
ASSERT_SUCCESS( |
| 563 |
read_file_contents(&partitions_buf, allocator, aws_byte_cursor_from_c_str("sample_partitions_bad_regex.json"))); |
| 564 |
struct aws_byte_cursor partitions_json = aws_byte_cursor_from_buf(&partitions_buf); |
| 565 |
struct aws_partitions_config *partitions = aws_partitions_config_new_from_string(allocator, partitions_json); |
| 566 |
ASSERT_NULL(partitions); |
| 567 |
ASSERT_INT_EQUALS(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED, aws_last_error()); |
| 568 |
|
| 569 |
aws_byte_buf_clean_up(&partitions_buf); |
| 570 |
aws_sdkutils_library_clean_up(); |
| 571 |
|
| 572 |
return AWS_OP_SUCCESS; |
| 573 |
} |