| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "credentials_provider_utils.h" |
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
#include <aws/auth/credentials.h> |
| 10 |
#include <aws/auth/private/credentials_utils.h> |
| 11 |
#include <aws/common/environment.h> |
| 12 |
#include <aws/common/string.h> |
| 13 |
#include <aws/common/task_scheduler.h> |
| 14 |
#include <aws/common/uuid.h> |
| 15 |
#include <aws/http/status_code.h> |
| 16 |
#include <aws/io/channel_bootstrap.h> |
| 17 |
#include <aws/io/event_loop.h> |
| 18 |
#include <aws/io/file_utils.h> |
| 19 |
#include <aws/io/stream.h> |
| 20 |
#include <aws/io/tls_channel_handler.h> |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
|
| 24 |
AWS_STATIC_STRING_FROM_LITERAL(s_home_env_var, "HOME"); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
void aws_get_credentials_test_callback_result_init( |
| 30 |
struct aws_get_credentials_test_callback_result *result, |
| 31 |
int required_count) { |
| 32 |
AWS_ZERO_STRUCT(*result); |
| 33 |
aws_mutex_init(&result->sync); |
| 34 |
aws_condition_variable_init(&result->signal); |
| 35 |
result->required_count = required_count; |
| 36 |
} |
| 37 |
|
| 38 |
void aws_get_credentials_test_callback_result_clean_up(struct aws_get_credentials_test_callback_result *result) { |
| 39 |
|
| 40 |
if (result->credentials) { |
| 41 |
aws_credentials_release(result->credentials); |
| 42 |
} |
| 43 |
|
| 44 |
aws_condition_variable_clean_up(&result->signal); |
| 45 |
aws_mutex_clean_up(&result->sync); |
| 46 |
} |
| 47 |
|
| 48 |
void aws_test_get_credentials_async_callback(struct aws_credentials *credentials, int error_code, void *user_data) { |
| 49 |
struct aws_get_credentials_test_callback_result *result = |
| 50 |
(struct aws_get_credentials_test_callback_result *)user_data; |
| 51 |
|
| 52 |
aws_mutex_lock(&result->sync); |
| 53 |
|
| 54 |
result->count++; |
| 55 |
result->last_error = error_code; |
| 56 |
|
| 57 |
if (result->credentials != NULL) { |
| 58 |
aws_credentials_release(result->credentials); |
| 59 |
} |
| 60 |
|
| 61 |
result->credentials = credentials; |
| 62 |
if (credentials != NULL) { |
| 63 |
aws_credentials_acquire(credentials); |
| 64 |
} |
| 65 |
|
| 66 |
aws_condition_variable_notify_one(&result->signal); |
| 67 |
|
| 68 |
aws_mutex_unlock(&result->sync); |
| 69 |
} |
| 70 |
|
| 71 |
static bool s_sync_credentials_predicate(void *context) { |
| 72 |
struct aws_get_credentials_test_callback_result *result = |
| 73 |
(struct aws_get_credentials_test_callback_result *)context; |
| 74 |
|
| 75 |
return result->count == result->required_count; |
| 76 |
} |
| 77 |
|
| 78 |
void aws_wait_on_credentials_callback(struct aws_get_credentials_test_callback_result *result) { |
| 79 |
bool done = false; |
| 80 |
while (!done) { |
| 81 |
aws_mutex_lock(&result->sync); |
| 82 |
|
| 83 |
aws_condition_variable_wait_pred(&result->signal, &result->sync, s_sync_credentials_predicate, result); |
| 84 |
|
| 85 |
done = result->count == result->required_count; |
| 86 |
aws_mutex_unlock(&result->sync); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
struct aws_credentials_provider_mock_impl { |
| 94 |
struct aws_array_list results; |
| 95 |
size_t next_result; |
| 96 |
}; |
| 97 |
|
| 98 |
static int s_mock_credentials_provider_get_credentials_async( |
| 99 |
struct aws_credentials_provider *provider, |
| 100 |
aws_on_get_credentials_callback_fn callback, |
| 101 |
void *user_data) { |
| 102 |
struct aws_credentials_provider_mock_impl *impl = (struct aws_credentials_provider_mock_impl *)provider->impl; |
| 103 |
|
| 104 |
if (impl->next_result < aws_array_list_length(&impl->results)) { |
| 105 |
struct get_credentials_mock_result result; |
| 106 |
if (aws_array_list_get_at(&impl->results, &result, impl->next_result)) { |
| 107 |
AWS_FATAL_ASSERT(false); |
| 108 |
} else { |
| 109 |
callback(result.credentials, result.error_code, user_data); |
| 110 |
} |
| 111 |
impl->next_result++; |
| 112 |
} else { |
| 113 |
AWS_FATAL_ASSERT(false); |
| 114 |
} |
| 115 |
|
| 116 |
return AWS_OP_SUCCESS; |
| 117 |
} |
| 118 |
|
| 119 |
static void s_mock_credentials_provider_destroy(struct aws_credentials_provider *provider) { |
| 120 |
struct aws_credentials_provider_mock_impl *impl = (struct aws_credentials_provider_mock_impl *)provider->impl; |
| 121 |
|
| 122 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 123 |
|
| 124 |
aws_array_list_clean_up(&impl->results); |
| 125 |
|
| 126 |
aws_mem_release(provider->allocator, provider); |
| 127 |
} |
| 128 |
|
| 129 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_mock_vtable = { |
| 130 |
.get_credentials = s_mock_credentials_provider_get_credentials_async, |
| 131 |
.destroy = s_mock_credentials_provider_destroy, |
| 132 |
}; |
| 133 |
|
| 134 |
struct aws_credentials_provider *aws_credentials_provider_new_mock( |
| 135 |
struct aws_allocator *allocator, |
| 136 |
struct get_credentials_mock_result *results, |
| 137 |
size_t result_count, |
| 138 |
struct aws_credentials_provider_shutdown_options *shutdown_options) { |
| 139 |
|
| 140 |
struct aws_credentials_provider *provider = NULL; |
| 141 |
struct aws_credentials_provider_mock_impl *impl = NULL; |
| 142 |
|
| 143 |
aws_mem_acquire_many( |
| 144 |
allocator, |
| 145 |
2, |
| 146 |
&provider, |
| 147 |
sizeof(struct aws_credentials_provider), |
| 148 |
&impl, |
| 149 |
sizeof(struct aws_credentials_provider_mock_impl)); |
| 150 |
|
| 151 |
if (!provider) { |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
AWS_ZERO_STRUCT(*provider); |
| 156 |
AWS_ZERO_STRUCT(*impl); |
| 157 |
|
| 158 |
if (aws_array_list_init_dynamic( |
| 159 |
&impl->results, allocator, result_count, sizeof(struct get_credentials_mock_result))) { |
| 160 |
goto on_init_result_list_failure; |
| 161 |
} |
| 162 |
|
| 163 |
for (size_t i = 0; i < result_count; ++i) { |
| 164 |
aws_array_list_push_back(&impl->results, results + i); |
| 165 |
} |
| 166 |
|
| 167 |
provider->allocator = allocator; |
| 168 |
provider->vtable = &s_aws_credentials_provider_mock_vtable; |
| 169 |
provider->impl = impl; |
| 170 |
if (shutdown_options) { |
| 171 |
provider->shutdown_options = *shutdown_options; |
| 172 |
} |
| 173 |
aws_atomic_store_int(&provider->ref_count, 1); |
| 174 |
|
| 175 |
return provider; |
| 176 |
|
| 177 |
on_init_result_list_failure: |
| 178 |
aws_mem_release(allocator, provider); |
| 179 |
|
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
struct aws_credentials_provider_mock_async_impl { |
| 188 |
struct aws_event_loop_group *event_loop_group; |
| 189 |
struct aws_mutex sync; |
| 190 |
struct aws_array_list queries; |
| 191 |
struct aws_array_list mock_results; |
| 192 |
size_t next_result; |
| 193 |
}; |
| 194 |
|
| 195 |
static int s_async_mock_credentials_provider_get_credentials_async( |
| 196 |
struct aws_credentials_provider *provider, |
| 197 |
aws_on_get_credentials_callback_fn callback, |
| 198 |
void *user_data) { |
| 199 |
struct aws_credentials_provider_mock_async_impl *impl = |
| 200 |
(struct aws_credentials_provider_mock_async_impl *)provider->impl; |
| 201 |
|
| 202 |
aws_mutex_lock(&impl->sync); |
| 203 |
|
| 204 |
struct aws_credentials_query query; |
| 205 |
AWS_ZERO_STRUCT(query); |
| 206 |
|
| 207 |
aws_credentials_query_init(&query, provider, callback, user_data); |
| 208 |
|
| 209 |
aws_array_list_push_back(&impl->queries, &query); |
| 210 |
|
| 211 |
aws_mutex_unlock(&impl->sync); |
| 212 |
|
| 213 |
return AWS_OP_SUCCESS; |
| 214 |
} |
| 215 |
|
| 216 |
static void s_async_mock_credentials_provider_destroy(struct aws_credentials_provider *provider) { |
| 217 |
struct aws_credentials_provider_mock_async_impl *impl = |
| 218 |
(struct aws_credentials_provider_mock_async_impl *)provider->impl; |
| 219 |
|
| 220 |
aws_array_list_clean_up(&impl->queries); |
| 221 |
aws_array_list_clean_up(&impl->mock_results); |
| 222 |
|
| 223 |
aws_event_loop_group_release(impl->event_loop_group); |
| 224 |
|
| 225 |
aws_mutex_clean_up(&impl->sync); |
| 226 |
|
| 227 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 228 |
|
| 229 |
aws_mem_release(provider->allocator, provider); |
| 230 |
} |
| 231 |
|
| 232 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_mock_async_vtable = { |
| 233 |
.get_credentials = s_async_mock_credentials_provider_get_credentials_async, |
| 234 |
.destroy = s_async_mock_credentials_provider_destroy, |
| 235 |
}; |
| 236 |
|
| 237 |
static void s_async_mock_credentials_provider_fire_callbacks_task( |
| 238 |
struct aws_task *task, |
| 239 |
void *arg, |
| 240 |
enum aws_task_status status) { |
| 241 |
|
| 242 |
(void)status; |
| 243 |
struct aws_credentials_provider *provider = arg; |
| 244 |
struct aws_credentials_provider_mock_async_impl *impl = provider->impl; |
| 245 |
|
| 246 |
aws_mem_release(provider->allocator, task); |
| 247 |
|
| 248 |
aws_mutex_lock(&impl->sync); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
struct aws_array_list temp_queries; |
| 255 |
AWS_FATAL_ASSERT( |
| 256 |
aws_array_list_init_dynamic(&temp_queries, impl->queries.alloc, 10, sizeof(struct aws_credentials_query)) == |
| 257 |
AWS_OP_SUCCESS); |
| 258 |
|
| 259 |
struct get_credentials_mock_result result; |
| 260 |
AWS_ZERO_STRUCT(result); |
| 261 |
|
| 262 |
size_t callback_count = aws_array_list_length(&impl->queries); |
| 263 |
if (callback_count != 0) { |
| 264 |
size_t result_count = aws_array_list_length(&impl->mock_results); |
| 265 |
if (impl->next_result >= result_count || |
| 266 |
aws_array_list_get_at(&impl->mock_results, &result, impl->next_result)) { |
| 267 |
AWS_FATAL_ASSERT(false); |
| 268 |
} |
| 269 |
impl->next_result++; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
aws_array_list_swap_contents(&impl->queries, &temp_queries); |
| 276 |
} |
| 277 |
aws_mutex_unlock(&impl->sync); |
| 278 |
|
| 279 |
|
| 280 |
for (size_t i = 0; i < callback_count; ++i) { |
| 281 |
struct aws_credentials_query query; |
| 282 |
AWS_ZERO_STRUCT(query); |
| 283 |
if (aws_array_list_get_at(&temp_queries, &query, i)) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
AWS_FATAL_ASSERT(query.callback != NULL); |
| 288 |
query.callback(result.credentials, result.error_code, query.user_data); |
| 289 |
|
| 290 |
aws_credentials_query_clean_up(&query); |
| 291 |
} |
| 292 |
|
| 293 |
aws_array_list_clean_up(&temp_queries); |
| 294 |
aws_credentials_provider_release(provider); |
| 295 |
} |
| 296 |
|
| 297 |
void aws_credentials_provider_mock_async_fire_callbacks(struct aws_credentials_provider *provider) { |
| 298 |
struct aws_credentials_provider_mock_async_impl *impl = provider->impl; |
| 299 |
|
| 300 |
struct aws_task *task = aws_mem_calloc(provider->allocator, 1, sizeof(struct aws_task)); |
| 301 |
AWS_FATAL_ASSERT(task); |
| 302 |
aws_task_init( |
| 303 |
task, |
| 304 |
s_async_mock_credentials_provider_fire_callbacks_task, |
| 305 |
provider, |
| 306 |
"async_mock_credentials_provider_fire_callbacks_task"); |
| 307 |
|
| 308 |
|
| 309 |
aws_credentials_provider_acquire(provider); |
| 310 |
|
| 311 |
struct aws_event_loop *loop = aws_event_loop_group_get_next_loop(impl->event_loop_group); |
| 312 |
aws_event_loop_schedule_task_now(loop, task); |
| 313 |
} |
| 314 |
|
| 315 |
struct aws_credentials_provider *aws_credentials_provider_new_mock_async( |
| 316 |
struct aws_allocator *allocator, |
| 317 |
struct get_credentials_mock_result *results, |
| 318 |
size_t result_count, |
| 319 |
struct aws_event_loop_group *elg, |
| 320 |
struct aws_credentials_provider_shutdown_options *shutdown_options) { |
| 321 |
|
| 322 |
struct aws_credentials_provider *provider = NULL; |
| 323 |
struct aws_credentials_provider_mock_async_impl *impl = NULL; |
| 324 |
|
| 325 |
aws_mem_acquire_many( |
| 326 |
allocator, |
| 327 |
2, |
| 328 |
&provider, |
| 329 |
sizeof(struct aws_credentials_provider), |
| 330 |
&impl, |
| 331 |
sizeof(struct aws_credentials_provider_mock_async_impl)); |
| 332 |
|
| 333 |
if (!provider) { |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
|
| 337 |
AWS_ZERO_STRUCT(*provider); |
| 338 |
AWS_ZERO_STRUCT(*impl); |
| 339 |
|
| 340 |
if (aws_mutex_init(&impl->sync)) { |
| 341 |
goto on_lock_init_failure; |
| 342 |
} |
| 343 |
|
| 344 |
if (aws_array_list_init_dynamic(&impl->queries, allocator, 10, sizeof(struct aws_credentials_query))) { |
| 345 |
goto on_query_list_init_failure; |
| 346 |
} |
| 347 |
|
| 348 |
if (aws_array_list_init_dynamic( |
| 349 |
&impl->mock_results, allocator, result_count, sizeof(struct get_credentials_mock_result))) { |
| 350 |
goto on_mock_result_list_init_failure; |
| 351 |
} |
| 352 |
|
| 353 |
for (size_t i = 0; i < result_count; ++i) { |
| 354 |
aws_array_list_push_back(&impl->mock_results, results + i); |
| 355 |
} |
| 356 |
|
| 357 |
impl->event_loop_group = aws_event_loop_group_acquire(elg); |
| 358 |
|
| 359 |
provider->allocator = allocator; |
| 360 |
provider->vtable = &s_aws_credentials_provider_mock_async_vtable; |
| 361 |
provider->impl = impl; |
| 362 |
if (shutdown_options) { |
| 363 |
provider->shutdown_options = *shutdown_options; |
| 364 |
} |
| 365 |
aws_atomic_store_int(&provider->ref_count, 1); |
| 366 |
|
| 367 |
return provider; |
| 368 |
|
| 369 |
on_mock_result_list_init_failure: |
| 370 |
aws_array_list_clean_up(&impl->queries); |
| 371 |
|
| 372 |
on_query_list_init_failure: |
| 373 |
aws_mutex_clean_up(&impl->sync); |
| 374 |
|
| 375 |
on_lock_init_failure: |
| 376 |
aws_mem_release(allocator, provider); |
| 377 |
|
| 378 |
return NULL; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static struct aws_mutex system_clock_sync = AWS_MUTEX_INIT; |
| 386 |
static uint64_t system_clock_time = 0; |
| 387 |
|
| 388 |
int mock_aws_get_system_time(uint64_t *current_time) { |
| 389 |
aws_mutex_lock(&system_clock_sync); |
| 390 |
|
| 391 |
*current_time = system_clock_time; |
| 392 |
|
| 393 |
aws_mutex_unlock(&system_clock_sync); |
| 394 |
|
| 395 |
return AWS_OP_SUCCESS; |
| 396 |
} |
| 397 |
|
| 398 |
void mock_aws_set_system_time(uint64_t current_time) { |
| 399 |
aws_mutex_lock(&system_clock_sync); |
| 400 |
|
| 401 |
system_clock_time = current_time; |
| 402 |
|
| 403 |
aws_mutex_unlock(&system_clock_sync); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
static struct aws_mutex high_res_clock_sync = AWS_MUTEX_INIT; |
| 411 |
static uint64_t high_res_clock_time = 0; |
| 412 |
|
| 413 |
int mock_aws_get_high_res_time(uint64_t *current_time) { |
| 414 |
aws_mutex_lock(&high_res_clock_sync); |
| 415 |
|
| 416 |
*current_time = high_res_clock_time; |
| 417 |
|
| 418 |
aws_mutex_unlock(&high_res_clock_sync); |
| 419 |
|
| 420 |
return AWS_OP_SUCCESS; |
| 421 |
} |
| 422 |
|
| 423 |
void mock_aws_set_high_res_time(uint64_t current_time) { |
| 424 |
aws_mutex_lock(&high_res_clock_sync); |
| 425 |
|
| 426 |
high_res_clock_time = current_time; |
| 427 |
|
| 428 |
aws_mutex_unlock(&high_res_clock_sync); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static int s_credentials_provider_null_get_credentials_async( |
| 436 |
struct aws_credentials_provider *provider, |
| 437 |
aws_on_get_credentials_callback_fn callback, |
| 438 |
void *user_data) { |
| 439 |
(void)provider; |
| 440 |
callback(NULL, AWS_ERROR_UNKNOWN, user_data); |
| 441 |
|
| 442 |
return AWS_OP_SUCCESS; |
| 443 |
} |
| 444 |
|
| 445 |
static void s_credentials_provider_null_destroy(struct aws_credentials_provider *provider) { |
| 446 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 447 |
|
| 448 |
aws_mem_release(provider->allocator, provider); |
| 449 |
} |
| 450 |
|
| 451 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_null_vtable = { |
| 452 |
.get_credentials = s_credentials_provider_null_get_credentials_async, |
| 453 |
.destroy = s_credentials_provider_null_destroy, |
| 454 |
}; |
| 455 |
|
| 456 |
struct aws_credentials_provider *aws_credentials_provider_new_null( |
| 457 |
struct aws_allocator *allocator, |
| 458 |
struct aws_credentials_provider_shutdown_options *shutdown_options) { |
| 459 |
struct aws_credentials_provider *provider = |
| 460 |
(struct aws_credentials_provider *)aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider)); |
| 461 |
if (provider == NULL) { |
| 462 |
return NULL; |
| 463 |
} |
| 464 |
|
| 465 |
AWS_ZERO_STRUCT(*provider); |
| 466 |
|
| 467 |
provider->allocator = allocator; |
| 468 |
provider->vtable = &s_aws_credentials_provider_null_vtable; |
| 469 |
provider->impl = NULL; |
| 470 |
if (shutdown_options) { |
| 471 |
provider->shutdown_options = *shutdown_options; |
| 472 |
} |
| 473 |
aws_atomic_store_int(&provider->ref_count, 1); |
| 474 |
|
| 475 |
return provider; |
| 476 |
} |
| 477 |
|
| 478 |
int aws_create_directory_components(struct aws_allocator *allocator, const struct aws_string *path) { |
| 479 |
const char local_platform_separator = aws_get_platform_directory_separator(); |
| 480 |
|
| 481 |
|
| 482 |
for (size_t i = 0; i < path->len; ++i) { |
| 483 |
if (aws_is_any_directory_separator((char)path->bytes[i])) { |
| 484 |
((char *)path->bytes)[i] = local_platform_separator; |
| 485 |
|
| 486 |
struct aws_string *segment = aws_string_new_from_array(allocator, path->bytes, i); |
| 487 |
int rc = aws_directory_create(segment); |
| 488 |
aws_string_destroy(segment); |
| 489 |
|
| 490 |
if (rc != AWS_OP_SUCCESS) { |
| 491 |
return rc; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
return AWS_OP_SUCCESS; |
| 496 |
} |
| 497 |
|
| 498 |
int aws_create_random_home_directory(struct aws_allocator *allocator, struct aws_string **out_path) { |
| 499 |
struct aws_byte_buf path_buf; |
| 500 |
ASSERT_SUCCESS(aws_byte_buf_init(&path_buf, allocator, 256)); |
| 501 |
|
| 502 |
struct aws_byte_cursor prefix = aws_byte_cursor_from_c_str("./home-"); |
| 503 |
ASSERT_SUCCESS(aws_byte_buf_append(&path_buf, &prefix)); |
| 504 |
|
| 505 |
struct aws_uuid uuid; |
| 506 |
ASSERT_SUCCESS(aws_uuid_init(&uuid)); |
| 507 |
ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &path_buf)); |
| 508 |
|
| 509 |
ASSERT_SUCCESS(aws_byte_buf_append_byte_dynamic(&path_buf, '/')); |
| 510 |
|
| 511 |
struct aws_string *path_str = aws_string_new_from_buf(allocator, &path_buf); |
| 512 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, path_str)); |
| 513 |
|
| 514 |
ASSERT_SUCCESS(aws_set_environment_value(s_home_env_var, path_str)); |
| 515 |
|
| 516 |
aws_byte_buf_clean_up(&path_buf); |
| 517 |
*out_path = path_str; |
| 518 |
return AWS_OP_SUCCESS; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
struct aws_auth_http_system_vtable aws_credentials_provider_http_mock_function_table = { |
| 526 |
.aws_http_connection_manager_new = aws_credentials_provider_http_mock_connection_manager_new, |
| 527 |
.aws_http_connection_manager_release = aws_credentials_provider_http_mock_connection_manager_release, |
| 528 |
.aws_http_connection_manager_acquire_connection = |
| 529 |
aws_credentials_provider_http_mock_connection_manager_acquire_connection, |
| 530 |
.aws_http_connection_manager_release_connection = |
| 531 |
aws_credentials_provider_http_mock_connection_manager_release_connection, |
| 532 |
.aws_http_connection_make_request = aws_credentials_provider_http_mock_make_request, |
| 533 |
.aws_http_stream_activate = aws_credentials_provider_http_mock_stream_activate, |
| 534 |
.aws_http_stream_get_connection = aws_credentials_provider_http_mock_stream_get_connection, |
| 535 |
.aws_http_stream_get_incoming_response_status = |
| 536 |
aws_credentials_provider_http_mock_stream_get_incoming_response_status, |
| 537 |
.aws_http_stream_release = aws_credentials_provider_http_mock_stream_release, |
| 538 |
.aws_http_connection_close = aws_credentials_provider_http_mock_connection_close}; |
| 539 |
|
| 540 |
struct aws_credentials_provider_http_mock_tester credentials_provider_http_mock_tester; |
| 541 |
|
| 542 |
int aws_credentials_provider_http_mock_tester_init(struct aws_allocator *allocator) { |
| 543 |
aws_auth_library_init(allocator); |
| 544 |
|
| 545 |
AWS_ZERO_STRUCT(credentials_provider_http_mock_tester); |
| 546 |
|
| 547 |
struct aws_tls_ctx_options tls_ctx_options; |
| 548 |
aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator); |
| 549 |
credentials_provider_http_mock_tester.tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options); |
| 550 |
ASSERT_NOT_NULL(credentials_provider_http_mock_tester.tls_ctx); |
| 551 |
|
| 552 |
credentials_provider_http_mock_tester.el_group = aws_event_loop_group_new_default(allocator, 0, NULL); |
| 553 |
|
| 554 |
struct aws_host_resolver_default_options resolver_options = { |
| 555 |
.el_group = credentials_provider_http_mock_tester.el_group, |
| 556 |
.max_entries = 8, |
| 557 |
}; |
| 558 |
credentials_provider_http_mock_tester.resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 559 |
|
| 560 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 561 |
.event_loop_group = credentials_provider_http_mock_tester.el_group, |
| 562 |
.host_resolver = credentials_provider_http_mock_tester.resolver, |
| 563 |
}; |
| 564 |
credentials_provider_http_mock_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 565 |
|
| 566 |
if (aws_array_list_init_dynamic( |
| 567 |
&credentials_provider_http_mock_tester.response_data_callbacks, |
| 568 |
allocator, |
| 569 |
10, |
| 570 |
sizeof(struct aws_byte_cursor))) { |
| 571 |
return AWS_OP_ERR; |
| 572 |
} |
| 573 |
|
| 574 |
if (aws_byte_buf_init(&credentials_provider_http_mock_tester.request_path, allocator, 256)) { |
| 575 |
return AWS_OP_ERR; |
| 576 |
} |
| 577 |
if (aws_byte_buf_init(&credentials_provider_http_mock_tester.request_body, allocator, 256)) { |
| 578 |
return AWS_OP_ERR; |
| 579 |
} |
| 580 |
|
| 581 |
if (aws_mutex_init(&credentials_provider_http_mock_tester.lock)) { |
| 582 |
return AWS_OP_ERR; |
| 583 |
} |
| 584 |
|
| 585 |
if (aws_condition_variable_init(&credentials_provider_http_mock_tester.signal)) { |
| 586 |
return AWS_OP_ERR; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
credentials_provider_http_mock_tester.is_connection_acquire_successful = true; |
| 591 |
credentials_provider_http_mock_tester.is_request_successful = true; |
| 592 |
|
| 593 |
|
| 594 |
aws_tls_ctx_options_clean_up(&tls_ctx_options); |
| 595 |
|
| 596 |
return AWS_OP_SUCCESS; |
| 597 |
} |
| 598 |
|
| 599 |
void aws_credentials_provider_http_mock_tester_cleanup(void) { |
| 600 |
aws_tls_ctx_release(credentials_provider_http_mock_tester.tls_ctx); |
| 601 |
aws_client_bootstrap_release(credentials_provider_http_mock_tester.bootstrap); |
| 602 |
aws_host_resolver_release(credentials_provider_http_mock_tester.resolver); |
| 603 |
aws_event_loop_group_release(credentials_provider_http_mock_tester.el_group); |
| 604 |
aws_array_list_clean_up(&credentials_provider_http_mock_tester.response_data_callbacks); |
| 605 |
aws_byte_buf_clean_up(&credentials_provider_http_mock_tester.request_path); |
| 606 |
aws_byte_buf_clean_up(&credentials_provider_http_mock_tester.request_body); |
| 607 |
aws_condition_variable_clean_up(&credentials_provider_http_mock_tester.signal); |
| 608 |
aws_mutex_clean_up(&credentials_provider_http_mock_tester.lock); |
| 609 |
aws_credentials_release(credentials_provider_http_mock_tester.credentials); |
| 610 |
aws_auth_library_clean_up(); |
| 611 |
} |
| 612 |
|
| 613 |
void aws_credentials_provider_http_mock_on_shutdown_complete(void *user_data) { |
| 614 |
(void)user_data; |
| 615 |
aws_mutex_lock(&credentials_provider_http_mock_tester.lock); |
| 616 |
credentials_provider_http_mock_tester.has_received_shutdown_callback = true; |
| 617 |
aws_mutex_unlock(&credentials_provider_http_mock_tester.lock); |
| 618 |
|
| 619 |
aws_condition_variable_notify_one(&credentials_provider_http_mock_tester.signal); |
| 620 |
} |
| 621 |
|
| 622 |
bool aws_credentials_provider_http_mock_has_received_shutdown_callback(void *user_data) { |
| 623 |
(void)user_data; |
| 624 |
|
| 625 |
return credentials_provider_http_mock_tester.has_received_shutdown_callback; |
| 626 |
} |
| 627 |
|
| 628 |
void aws_credentials_provider_http_mock_wait_for_shutdown_callback(void) { |
| 629 |
aws_mutex_lock(&credentials_provider_http_mock_tester.lock); |
| 630 |
aws_condition_variable_wait_pred( |
| 631 |
&credentials_provider_http_mock_tester.signal, |
| 632 |
&credentials_provider_http_mock_tester.lock, |
| 633 |
aws_credentials_provider_http_mock_has_received_shutdown_callback, |
| 634 |
NULL); |
| 635 |
aws_mutex_unlock(&credentials_provider_http_mock_tester.lock); |
| 636 |
} |
| 637 |
|
| 638 |
struct mock_connection_manager { |
| 639 |
struct aws_allocator *allocator; |
| 640 |
aws_http_connection_manager_shutdown_complete_fn *shutdown_complete_callback; |
| 641 |
void *shutdown_complete_user_data; |
| 642 |
}; |
| 643 |
|
| 644 |
struct aws_http_connection_manager *aws_credentials_provider_http_mock_connection_manager_new( |
| 645 |
struct aws_allocator *allocator, |
| 646 |
const struct aws_http_connection_manager_options *options) { |
| 647 |
|
| 648 |
struct mock_connection_manager *mock_manager = aws_mem_calloc(allocator, 1, sizeof(struct mock_connection_manager)); |
| 649 |
mock_manager->allocator = allocator; |
| 650 |
mock_manager->shutdown_complete_callback = options->shutdown_complete_callback; |
| 651 |
mock_manager->shutdown_complete_user_data = options->shutdown_complete_user_data; |
| 652 |
return (struct aws_http_connection_manager *)mock_manager; |
| 653 |
} |
| 654 |
|
| 655 |
void aws_credentials_provider_http_mock_connection_manager_release(struct aws_http_connection_manager *manager) { |
| 656 |
struct mock_connection_manager *mock_manager = (struct mock_connection_manager *)manager; |
| 657 |
mock_manager->shutdown_complete_callback(mock_manager->shutdown_complete_user_data); |
| 658 |
aws_mem_release(mock_manager->allocator, mock_manager); |
| 659 |
} |
| 660 |
|
| 661 |
void aws_credentials_provider_http_mock_connection_manager_acquire_connection( |
| 662 |
struct aws_http_connection_manager *manager, |
| 663 |
aws_http_connection_manager_on_connection_setup_fn *callback, |
| 664 |
void *user_data) { |
| 665 |
|
| 666 |
(void)manager; |
| 667 |
(void)callback; |
| 668 |
(void)user_data; |
| 669 |
|
| 670 |
if (credentials_provider_http_mock_tester.is_connection_acquire_successful) { |
| 671 |
callback((struct aws_http_connection *)1, AWS_OP_SUCCESS, user_data); |
| 672 |
} else { |
| 673 |
aws_raise_error(AWS_ERROR_HTTP_UNKNOWN); |
| 674 |
callback(NULL, AWS_OP_ERR, user_data); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
int aws_credentials_provider_http_mock_connection_manager_release_connection( |
| 679 |
struct aws_http_connection_manager *manager, |
| 680 |
struct aws_http_connection *connection) { |
| 681 |
|
| 682 |
(void)manager; |
| 683 |
(void)connection; |
| 684 |
|
| 685 |
return AWS_OP_SUCCESS; |
| 686 |
} |
| 687 |
|
| 688 |
void aws_credentials_provider_http_mock_invoke_request_callbacks( |
| 689 |
const struct aws_http_make_request_options *options, |
| 690 |
struct aws_array_list *data_callbacks, |
| 691 |
bool is_request_successful) { |
| 692 |
|
| 693 |
size_t data_callback_count = aws_array_list_length(data_callbacks); |
| 694 |
|
| 695 |
struct aws_http_header headers[1]; |
| 696 |
AWS_ZERO_ARRAY(headers); |
| 697 |
|
| 698 |
headers[0].name = aws_byte_cursor_from_c_str("some-header"); |
| 699 |
headers[0].value = aws_byte_cursor_from_c_str("value"); |
| 700 |
if (options->on_response_headers) { |
| 701 |
options->on_response_headers( |
| 702 |
(struct aws_http_stream *)1, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data); |
| 703 |
} |
| 704 |
if (options->on_response_header_block_done) { |
| 705 |
options->on_response_header_block_done( |
| 706 |
(struct aws_http_stream *)1, data_callback_count > 0, options->user_data); |
| 707 |
} |
| 708 |
|
| 709 |
for (size_t i = 0; i < data_callback_count; ++i) { |
| 710 |
struct aws_byte_cursor data_callback_cursor; |
| 711 |
if (aws_array_list_get_at(data_callbacks, &data_callback_cursor, i)) { |
| 712 |
continue; |
| 713 |
} |
| 714 |
|
| 715 |
options->on_response_body((struct aws_http_stream *)1, &data_callback_cursor, options->user_data); |
| 716 |
} |
| 717 |
|
| 718 |
options->on_complete( |
| 719 |
(struct aws_http_stream *)1, |
| 720 |
is_request_successful ? AWS_ERROR_SUCCESS : AWS_ERROR_HTTP_UNKNOWN, |
| 721 |
options->user_data); |
| 722 |
} |
| 723 |
|
| 724 |
struct aws_http_stream *aws_credentials_provider_http_mock_make_request( |
| 725 |
struct aws_http_connection *client_connection, |
| 726 |
const struct aws_http_make_request_options *options) { |
| 727 |
|
| 728 |
(void)client_connection; |
| 729 |
(void)options; |
| 730 |
|
| 731 |
struct aws_byte_cursor path; |
| 732 |
AWS_ZERO_STRUCT(path); |
| 733 |
struct aws_input_stream *body_stream = aws_http_message_get_body_stream(options->request); |
| 734 |
struct aws_allocator *allocator = credentials_provider_http_mock_tester.request_body.allocator; |
| 735 |
aws_byte_buf_clean_up(&credentials_provider_http_mock_tester.request_body); |
| 736 |
aws_byte_buf_init(&credentials_provider_http_mock_tester.request_body, allocator, 256); |
| 737 |
if (body_stream) { |
| 738 |
aws_input_stream_read(body_stream, &credentials_provider_http_mock_tester.request_body); |
| 739 |
} |
| 740 |
aws_byte_buf_clean_up(&credentials_provider_http_mock_tester.request_path); |
| 741 |
|
| 742 |
struct aws_byte_cursor request_path_cursor; |
| 743 |
aws_http_message_get_request_path(options->request, &request_path_cursor); |
| 744 |
aws_byte_buf_init_copy_from_cursor( |
| 745 |
&credentials_provider_http_mock_tester.request_path, allocator, request_path_cursor); |
| 746 |
credentials_provider_http_mock_tester.attempts++; |
| 747 |
credentials_provider_http_mock_tester.request_options = *options; |
| 748 |
|
| 749 |
return (struct aws_http_stream *)1; |
| 750 |
} |
| 751 |
|
| 752 |
int aws_credentials_provider_http_mock_stream_activate(struct aws_http_stream *stream) { |
| 753 |
(void)stream; |
| 754 |
aws_credentials_provider_http_mock_invoke_request_callbacks( |
| 755 |
&credentials_provider_http_mock_tester.request_options, |
| 756 |
&credentials_provider_http_mock_tester.response_data_callbacks, |
| 757 |
credentials_provider_http_mock_tester.is_request_successful); |
| 758 |
return AWS_OP_SUCCESS; |
| 759 |
} |
| 760 |
|
| 761 |
int aws_credentials_provider_http_mock_stream_get_incoming_response_status( |
| 762 |
const struct aws_http_stream *stream, |
| 763 |
int *out_status_code) { |
| 764 |
(void)stream; |
| 765 |
|
| 766 |
if (credentials_provider_http_mock_tester.failure_count) { |
| 767 |
credentials_provider_http_mock_tester.failure_count--; |
| 768 |
*out_status_code = credentials_provider_http_mock_tester.failure_response_code; |
| 769 |
} else if (credentials_provider_http_mock_tester.response_code) { |
| 770 |
*out_status_code = credentials_provider_http_mock_tester.response_code; |
| 771 |
} else { |
| 772 |
*out_status_code = AWS_HTTP_STATUS_CODE_200_OK; |
| 773 |
} |
| 774 |
|
| 775 |
return AWS_OP_SUCCESS; |
| 776 |
} |
| 777 |
|
| 778 |
void aws_credentials_provider_http_mock_stream_release(struct aws_http_stream *stream) { |
| 779 |
(void)stream; |
| 780 |
} |
| 781 |
|
| 782 |
void aws_credentials_provider_http_mock_connection_close(struct aws_http_connection *connection) { |
| 783 |
(void)connection; |
| 784 |
} |
| 785 |
|
| 786 |
struct aws_http_connection *aws_credentials_provider_http_mock_stream_get_connection( |
| 787 |
const struct aws_http_stream *stream) { |
| 788 |
(void)stream; |
| 789 |
return (struct aws_http_connection *)1; |
| 790 |
} |
| 791 |
|
| 792 |
bool aws_credentials_provider_http_mock_has_received_credentials_callback(void *user_data) { |
| 793 |
(void)user_data; |
| 794 |
|
| 795 |
return credentials_provider_http_mock_tester.has_received_credentials_callback; |
| 796 |
} |
| 797 |
|
| 798 |
void aws_credentials_provider_http_mock_wait_for_credentials_result(void) { |
| 799 |
aws_mutex_lock(&credentials_provider_http_mock_tester.lock); |
| 800 |
aws_condition_variable_wait_pred( |
| 801 |
&credentials_provider_http_mock_tester.signal, |
| 802 |
&credentials_provider_http_mock_tester.lock, |
| 803 |
aws_credentials_provider_http_mock_has_received_credentials_callback, |
| 804 |
NULL); |
| 805 |
aws_mutex_unlock(&credentials_provider_http_mock_tester.lock); |
| 806 |
} |
| 807 |
|
| 808 |
void aws_credentials_provider_http_mock_get_credentials_callback( |
| 809 |
struct aws_credentials *credentials, |
| 810 |
int error_code, |
| 811 |
void *user_data) { |
| 812 |
(void)user_data; |
| 813 |
|
| 814 |
aws_mutex_lock(&credentials_provider_http_mock_tester.lock); |
| 815 |
credentials_provider_http_mock_tester.has_received_credentials_callback = true; |
| 816 |
credentials_provider_http_mock_tester.credentials = credentials; |
| 817 |
credentials_provider_http_mock_tester.error_code = error_code; |
| 818 |
if (credentials != NULL) { |
| 819 |
aws_credentials_acquire(credentials); |
| 820 |
} |
| 821 |
aws_condition_variable_notify_one(&credentials_provider_http_mock_tester.signal); |
| 822 |
aws_mutex_unlock(&credentials_provider_http_mock_tester.lock); |
| 823 |
} |