Debian Codegraph @

crt/aws-c-auth/tests/credentials_provider_utils.h

raw symbols SCIP
1 #ifndef AWS_AUTH_CREDENTIALS_PROVIDER_MOCK_H
2 #define AWS_AUTH_CREDENTIALS_PROVIDER_MOCK_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/auth/private/aws_profile.h>
10 #include <aws/auth/private/credentials_utils.h>
11
12 #include <aws/common/condition_variable.h>
13 #include <aws/common/mutex.h>
14 #include <aws/http/connection_manager.h>
15 #include <aws/http/request_response.h>
16
17 struct aws_credentials;
18 struct aws_credentials_provider;
19 struct aws_credentials_provider_shutdown_options;
20 struct aws_event_loop_group;
21 struct aws_string;
22
23 /*
24 * This file contains a number of helper functions and data structures
25 * that let us verify async behavior within the credentials provider.
26 *
27 * It includes multiple provider mocks (one synchronous, one background-thread
28 * based and externally controllable), a synchronizing controller that uses
29 * concurrency primitives to ensure we can perform operations at troublesome
30 * time points (freeze the cached background query so that we can queue up
31 * multiple pending queries, for example), and misc supporting functions like
32 * time function mocks.
33 */
34
35 /*
36 * test helper struct to correctly wait on async credentials callbacks
37 */
38 struct aws_get_credentials_test_callback_result {
39 struct aws_mutex sync;
40 struct aws_condition_variable signal;
41 struct aws_credentials *credentials;
42 int count;
43 int required_count;
44 int last_error;
45 };
46
47 void aws_get_credentials_test_callback_result_init(
48 struct aws_get_credentials_test_callback_result *result,
49 int required_count);
50 void aws_get_credentials_test_callback_result_clean_up(struct aws_get_credentials_test_callback_result *result);
51
52 void aws_wait_on_credentials_callback(struct aws_get_credentials_test_callback_result *result);
53
54 void aws_test_get_credentials_async_callback(struct aws_credentials *credentials, int error_code, void *user_data);
55
56 struct get_credentials_mock_result {
57 int error_code;
58 struct aws_credentials *credentials;
59 };
60
61 /*
62 * Mock credentials provider, synchronous
63 */
64 struct aws_credentials_provider *aws_credentials_provider_new_mock(
65 struct aws_allocator *allocator,
66 struct get_credentials_mock_result *results,
67 size_t result_count,
68 struct aws_credentials_provider_shutdown_options *shutdown_options);
69
70 struct aws_credentials_provider *aws_credentials_provider_new_mock_async(
71 struct aws_allocator *allocator,
72 struct get_credentials_mock_result *results,
73 size_t result_count,
74 struct aws_event_loop_group *elg,
75 struct aws_credentials_provider_shutdown_options *shutdown_options);
76
77 /* If any pending queries, deliver the next mock-result to all of them from another thread.
78 * If no pending queries, nothing happens. */
79 void aws_credentials_provider_mock_async_fire_callbacks(struct aws_credentials_provider *provider);
80
81 /*
82 * Simple global clock mocks
83 */
84 int mock_aws_get_system_time(uint64_t *current_time);
85 void mock_aws_set_system_time(uint64_t current_time);
86
87 int mock_aws_get_high_res_time(uint64_t *current_time);
88 void mock_aws_set_high_res_time(uint64_t current_time);
89
90 /*
91 * Credentials provider that always returns NULL. Useful for chain tests.
92 */
93 struct aws_credentials_provider *aws_credentials_provider_new_null(
94 struct aws_allocator *allocator,
95 struct aws_credentials_provider_shutdown_options *shutdown_options);
96
97 /**
98 * Create the directory components of @path:
99 * - if @path ends in a path separator, create every directory component;
100 * - else, stop at the last path separator (parent directory of @path).
101 */
102 int aws_create_directory_components(struct aws_allocator *allocator, const struct aws_string *path);
103
104 /**
105 * Create a new directory (under current working dir) and set $HOME env variable.
106 */
107 int aws_create_random_home_directory(struct aws_allocator *allocator, struct aws_string **out_path);
108
109 /**
110 * Mocked HTTP connection manager for tests
111 */
112 struct aws_credentials_provider_http_mock_tester {
113 struct aws_tls_ctx *tls_ctx;
114 struct aws_event_loop_group *el_group;
115 struct aws_host_resolver *resolver;
116 struct aws_client_bootstrap *bootstrap;
117
118 struct aws_byte_buf request_path;
119 struct aws_byte_buf request_body;
120 struct aws_http_make_request_options request_options;
121
122 struct aws_array_list response_data_callbacks;
123 bool is_connection_acquire_successful;
124 bool is_request_successful;
125
126 struct aws_mutex lock;
127 struct aws_condition_variable signal;
128
129 struct aws_credentials *credentials;
130 bool has_received_credentials_callback;
131 bool has_received_shutdown_callback;
132
133 int attempts;
134 int response_code;
135 int error_code;
136 int failure_response_code;
137 int failure_count;
138 };
139
140 extern struct aws_credentials_provider_http_mock_tester credentials_provider_http_mock_tester;
141 int aws_credentials_provider_http_mock_tester_init(struct aws_allocator *allocator);
142 void aws_credentials_provider_http_mock_tester_cleanup(void);
143 void aws_credentials_provider_http_mock_on_shutdown_complete(void *user_data);
144 bool aws_credentials_provider_http_mock_has_received_shutdown_callback(void *user_data);
145 void aws_credentials_provider_http_mock_wait_for_shutdown_callback(void);
146 struct aws_http_connection_manager *aws_credentials_provider_http_mock_connection_manager_new(
147 struct aws_allocator *allocator,
148 const struct aws_http_connection_manager_options *options);
149 void aws_credentials_provider_http_mock_connection_manager_release(struct aws_http_connection_manager *manager);
150 void aws_credentials_provider_http_mock_connection_manager_acquire_connection(
151 struct aws_http_connection_manager *manager,
152 aws_http_connection_manager_on_connection_setup_fn *callback,
153 void *user_data);
154 int aws_credentials_provider_http_mock_connection_manager_release_connection(
155 struct aws_http_connection_manager *manager,
156 struct aws_http_connection *connection);
157 void aws_credentials_provider_http_mock_invoke_request_callbacks(
158 const struct aws_http_make_request_options *options,
159 struct aws_array_list *data_callbacks,
160 bool is_request_successful);
161 struct aws_http_stream *aws_credentials_provider_http_mock_make_request(
162 struct aws_http_connection *client_connection,
163 const struct aws_http_make_request_options *options);
164 int aws_credentials_provider_http_mock_stream_activate(struct aws_http_stream *stream);
165 int aws_credentials_provider_http_mock_stream_get_incoming_response_status(
166 const struct aws_http_stream *stream,
167 int *out_status_code);
168 void aws_credentials_provider_http_mock_stream_release(struct aws_http_stream *stream);
169 void aws_credentials_provider_http_mock_connection_close(struct aws_http_connection *connection);
170 struct aws_http_connection *aws_credentials_provider_http_mock_stream_get_connection(
171 const struct aws_http_stream *stream);
172 bool aws_credentials_provider_http_mock_has_received_credentials_callback(void *user_data);
173 void aws_credentials_provider_http_mock_wait_for_credentials_result(void);
174 void aws_credentials_provider_http_mock_get_credentials_callback(
175 struct aws_credentials *credentials,
176 int error_code,
177 void *user_data);
178 extern struct aws_auth_http_system_vtable aws_credentials_provider_http_mock_function_table;
179
180 #endif /* AWS_AUTH_CREDENTIALS_PROVIDER_MOCK_H */