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
|
#ifndef AWS_AUTH_CREDENTIALS_PROVIDER_MOCK_H
#define AWS_AUTH_CREDENTIALS_PROVIDER_MOCK_H
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/auth/private/aws_profile.h>
#include <aws/auth/private/credentials_utils.h>
#include <aws/common/condition_variable.h>
#include <aws/common/mutex.h>
#include <aws/http/connection_manager.h>
#include <aws/http/request_response.h>
struct aws_credentials;
struct aws_credentials_provider;
struct aws_credentials_provider_shutdown_options;
struct aws_event_loop_group;
struct aws_string;
/*
* This file contains a number of helper functions and data structures
* that let us verify async behavior within the credentials provider.
*
* It includes multiple provider mocks (one synchronous, one background-thread
* based and externally controllable), a synchronizing controller that uses
* concurrency primitives to ensure we can perform operations at troublesome
* time points (freeze the cached background query so that we can queue up
* multiple pending queries, for example), and misc supporting functions like
* time function mocks.
*/
/*
* test helper struct to correctly wait on async credentials callbacks
*/
struct aws_get_credentials_test_callback_result {
struct aws_mutex sync;
struct aws_condition_variable signal;
struct aws_credentials *credentials;
int count;
int required_count;
int last_error;
};
void aws_get_credentials_test_callback_result_init(
struct aws_get_credentials_test_callback_result *result,
int required_count);
void aws_get_credentials_test_callback_result_clean_up(struct aws_get_credentials_test_callback_result *result);
void aws_wait_on_credentials_callback(struct aws_get_credentials_test_callback_result *result);
void aws_test_get_credentials_async_callback(struct aws_credentials *credentials, int error_code, void *user_data);
struct get_credentials_mock_result {
int error_code;
struct aws_credentials *credentials;
};
/*
* Mock credentials provider, synchronous
*/
struct aws_credentials_provider *aws_credentials_provider_new_mock(
struct aws_allocator *allocator,
struct get_credentials_mock_result *results,
size_t result_count,
struct aws_credentials_provider_shutdown_options *shutdown_options);
struct aws_credentials_provider *aws_credentials_provider_new_mock_async(
struct aws_allocator *allocator,
struct get_credentials_mock_result *results,
size_t result_count,
struct aws_event_loop_group *elg,
struct aws_credentials_provider_shutdown_options *shutdown_options);
/* If any pending queries, deliver the next mock-result to all of them from another thread.
* If no pending queries, nothing happens. */
void aws_credentials_provider_mock_async_fire_callbacks(struct aws_credentials_provider *provider);
/*
* Simple global clock mocks
*/
int mock_aws_get_system_time(uint64_t *current_time);
void mock_aws_set_system_time(uint64_t current_time);
int mock_aws_get_high_res_time(uint64_t *current_time);
void mock_aws_set_high_res_time(uint64_t current_time);
/*
* Credentials provider that always returns NULL. Useful for chain tests.
*/
struct aws_credentials_provider *aws_credentials_provider_new_null(
struct aws_allocator *allocator,
struct aws_credentials_provider_shutdown_options *shutdown_options);
/**
* Create the directory components of @path:
* - if @path ends in a path separator, create every directory component;
* - else, stop at the last path separator (parent directory of @path).
*/
int aws_create_directory_components(struct aws_allocator *allocator, const struct aws_string *path);
/**
* Create a new directory (under current working dir) and set $HOME env variable.
*/
int aws_create_random_home_directory(struct aws_allocator *allocator, struct aws_string **out_path);
/**
* Mocked HTTP connection manager for tests
*/
struct aws_credentials_provider_http_mock_tester {
struct aws_tls_ctx *tls_ctx;
struct aws_event_loop_group *el_group;
struct aws_host_resolver *resolver;
struct aws_client_bootstrap *bootstrap;
struct aws_byte_buf request_path;
struct aws_byte_buf request_body;
struct aws_http_make_request_options request_options;
struct aws_array_list response_data_callbacks;
bool is_connection_acquire_successful;
bool is_request_successful;
struct aws_mutex lock;
struct aws_condition_variable signal;
struct aws_credentials *credentials;
bool has_received_credentials_callback;
bool has_received_shutdown_callback;
int attempts;
int response_code;
int error_code;
int failure_response_code;
int failure_count;
};
extern struct aws_credentials_provider_http_mock_tester credentials_provider_http_mock_tester;
int aws_credentials_provider_http_mock_tester_init(struct aws_allocator *allocator);
void aws_credentials_provider_http_mock_tester_cleanup(void);
void aws_credentials_provider_http_mock_on_shutdown_complete(void *user_data);
bool aws_credentials_provider_http_mock_has_received_shutdown_callback(void *user_data);
void aws_credentials_provider_http_mock_wait_for_shutdown_callback(void);
struct aws_http_connection_manager *aws_credentials_provider_http_mock_connection_manager_new(
struct aws_allocator *allocator,
const struct aws_http_connection_manager_options *options);
void aws_credentials_provider_http_mock_connection_manager_release(struct aws_http_connection_manager *manager);
void aws_credentials_provider_http_mock_connection_manager_acquire_connection(
struct aws_http_connection_manager *manager,
aws_http_connection_manager_on_connection_setup_fn *callback,
void *user_data);
int aws_credentials_provider_http_mock_connection_manager_release_connection(
struct aws_http_connection_manager *manager,
struct aws_http_connection *connection);
void aws_credentials_provider_http_mock_invoke_request_callbacks(
const struct aws_http_make_request_options *options,
struct aws_array_list *data_callbacks,
bool is_request_successful);
struct aws_http_stream *aws_credentials_provider_http_mock_make_request(
struct aws_http_connection *client_connection,
const struct aws_http_make_request_options *options);
int aws_credentials_provider_http_mock_stream_activate(struct aws_http_stream *stream);
int aws_credentials_provider_http_mock_stream_get_incoming_response_status(
const struct aws_http_stream *stream,
int *out_status_code);
void aws_credentials_provider_http_mock_stream_release(struct aws_http_stream *stream);
void aws_credentials_provider_http_mock_connection_close(struct aws_http_connection *connection);
struct aws_http_connection *aws_credentials_provider_http_mock_stream_get_connection(
const struct aws_http_stream *stream);
bool aws_credentials_provider_http_mock_has_received_credentials_callback(void *user_data);
void aws_credentials_provider_http_mock_wait_for_credentials_result(void);
void aws_credentials_provider_http_mock_get_credentials_callback(
struct aws_credentials *credentials,
int error_code,
void *user_data);
extern struct aws_auth_http_system_vtable aws_credentials_provider_http_mock_function_table;
#endif /* AWS_AUTH_CREDENTIALS_PROVIDER_MOCK_H */
|