| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/connection.h> |
| 7 |
|
| 8 |
#include <aws/http/private/connection_impl.h> |
| 9 |
#include <aws/http/private/h1_stream.h> |
| 10 |
#include <aws/http/private/proxy_impl.h> |
| 11 |
#include <aws/http/proxy.h> |
| 12 |
|
| 13 |
#include <aws/io/uri.h> |
| 14 |
|
| 15 |
#include <aws/common/string.h> |
| 16 |
|
| 17 |
#include <aws/testing/aws_test_harness.h> |
| 18 |
#include <aws/testing/io_testing_channel.h> |
| 19 |
|
| 20 |
#include "proxy_test_helper.h" |
| 21 |
|
| 22 |
static struct proxy_tester tester; |
| 23 |
|
| 24 |
static char *s_host_name = "aws.amazon.com"; |
| 25 |
static uint32_t s_port = 80; |
| 26 |
static char *s_proxy_host_name = "www.myproxy.hmm"; |
| 27 |
static uint32_t s_proxy_port = 777; |
| 28 |
|
| 29 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_request_method, "GET"); |
| 30 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_request_path, "/"); |
| 31 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_request_host, "aws.amazon.com"); |
| 32 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_basic_auth_header_name, "Proxy-Authorization"); |
| 33 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_basic_auth_header_value, "Basic U29tZVVzZXI6U3VwZXJTZWNyZXQ="); |
| 34 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_request_username, "SomeUser"); |
| 35 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_request_password, "SuperSecret"); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
struct aws_http_message *s_build_dummy_http_request( |
| 41 |
struct aws_allocator *allocator, |
| 42 |
struct aws_byte_cursor method, |
| 43 |
struct aws_byte_cursor path, |
| 44 |
struct aws_byte_cursor host) { |
| 45 |
|
| 46 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 47 |
aws_http_message_set_request_method(request, method); |
| 48 |
aws_http_message_set_request_path(request, path); |
| 49 |
|
| 50 |
struct aws_http_header host_header = { |
| 51 |
.name = aws_byte_cursor_from_c_str("Host"), |
| 52 |
.value = host, |
| 53 |
}; |
| 54 |
aws_http_message_add_header(request, host_header); |
| 55 |
|
| 56 |
struct aws_http_header accept_header = { |
| 57 |
.name = aws_byte_cursor_from_c_str("Accept"), |
| 58 |
.value = aws_byte_cursor_from_c_str("*/*"), |
| 59 |
}; |
| 60 |
aws_http_message_add_header(request, accept_header); |
| 61 |
|
| 62 |
return request; |
| 63 |
} |
| 64 |
|
| 65 |
static struct aws_http_message *s_build_http_request(struct aws_allocator *allocator) { |
| 66 |
return s_build_dummy_http_request( |
| 67 |
allocator, |
| 68 |
aws_byte_cursor_from_string(s_mock_request_method), |
| 69 |
aws_byte_cursor_from_string(s_mock_request_path), |
| 70 |
aws_byte_cursor_from_string(s_mock_request_host)); |
| 71 |
} |
| 72 |
|
| 73 |
static bool s_is_header_in_request(struct aws_http_message *request, struct aws_byte_cursor header_name) { |
| 74 |
size_t header_count = aws_http_message_get_header_count(request); |
| 75 |
for (size_t i = 0; i < header_count; ++i) { |
| 76 |
struct aws_http_header current_header; |
| 77 |
ASSERT_SUCCESS(aws_http_message_get_header(request, ¤t_header, i)); |
| 78 |
|
| 79 |
if (aws_byte_cursor_eq_ignore_case(¤t_header.name, &header_name)) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
static bool s_is_header_and_value_in_request(struct aws_http_message *request, struct aws_http_header *header) { |
| 88 |
size_t header_count = aws_http_message_get_header_count(request); |
| 89 |
for (size_t i = 0; i < header_count; ++i) { |
| 90 |
struct aws_http_header current_header; |
| 91 |
ASSERT_SUCCESS(aws_http_message_get_header(request, ¤t_header, i)); |
| 92 |
|
| 93 |
if (aws_byte_cursor_eq_ignore_case(¤t_header.name, &header->name) && |
| 94 |
aws_byte_cursor_eq(¤t_header.value, &header->value)) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
static int s_test_proxy_setup_client_tls( |
| 106 |
struct aws_channel_slot *right_of_slot, |
| 107 |
struct aws_tls_connection_options *tls_options) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
AWS_FATAL_ASSERT(right_of_slot != NULL); |
| 115 |
struct aws_channel *channel = right_of_slot->channel; |
| 116 |
struct aws_allocator *allocator = right_of_slot->alloc; |
| 117 |
|
| 118 |
struct aws_channel_slot *tls_slot = aws_channel_slot_new(channel); |
| 119 |
if (!tls_slot) { |
| 120 |
return AWS_OP_ERR; |
| 121 |
} |
| 122 |
|
| 123 |
struct aws_channel_handler *tls_handler = aws_tls_client_handler_new(allocator, tls_options, tls_slot); |
| 124 |
if (!tls_handler) { |
| 125 |
aws_mem_release(allocator, tls_slot); |
| 126 |
return AWS_OP_ERR; |
| 127 |
} |
| 128 |
|
| 129 |
aws_channel_slot_insert_right(right_of_slot, tls_slot); |
| 130 |
if (aws_channel_slot_set_handler(tls_slot, tls_handler) != AWS_OP_SUCCESS) { |
| 131 |
return AWS_OP_ERR; |
| 132 |
} |
| 133 |
|
| 134 |
if (tester.failure_type == PTFT_TLS_NEGOTIATION) { |
| 135 |
tls_options->on_negotiation_result(NULL, NULL, AWS_ERROR_UNKNOWN, tls_options->user_data); |
| 136 |
} else { |
| 137 |
tls_options->on_negotiation_result(NULL, NULL, AWS_ERROR_SUCCESS, tls_options->user_data); |
| 138 |
} |
| 139 |
|
| 140 |
return AWS_OP_SUCCESS; |
| 141 |
} |
| 142 |
|
| 143 |
struct aws_http_proxy_system_vtable s_proxy_table_for_tls = { |
| 144 |
.aws_channel_setup_client_tls = s_test_proxy_setup_client_tls, |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static int s_test_aws_proxy_new_socket_channel(struct aws_socket_channel_bootstrap_options *channel_options) { |
| 151 |
|
| 152 |
aws_mutex_lock(&tester.wait_lock); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
struct aws_byte_cursor host_cursor = aws_byte_cursor_from_c_str(channel_options->host_name); |
| 158 |
aws_byte_buf_append_dynamic(&tester.connection_host_name, &host_cursor); |
| 159 |
|
| 160 |
tester.connection_port = channel_options->port; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if (tester.failure_type == PTFT_CHANNEL) { |
| 166 |
tester.wait_result = AWS_ERROR_UNKNOWN; |
| 167 |
} else if (tester.failure_type != PTFT_CONNECTION) { |
| 168 |
ASSERT_SUCCESS(proxy_tester_create_testing_channel_connection(&tester, channel_options->user_data)); |
| 169 |
} |
| 170 |
|
| 171 |
aws_mutex_unlock(&tester.wait_lock); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if (tester.failure_type == PTFT_CHANNEL) { |
| 177 |
return AWS_OP_ERR; |
| 178 |
} |
| 179 |
|
| 180 |
if (tester.failure_type == PTFT_CONNECTION) { |
| 181 |
channel_options->setup_callback(tester.client_bootstrap, AWS_ERROR_UNKNOWN, NULL, channel_options->user_data); |
| 182 |
return AWS_OP_SUCCESS; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
struct aws_http_client_bootstrap *http_bootstrap = channel_options->user_data; |
| 189 |
http_bootstrap->on_setup(tester.client_connection, AWS_ERROR_SUCCESS, http_bootstrap->user_data); |
| 190 |
|
| 191 |
struct testing_channel *channel = proxy_tester_get_current_channel(&tester); |
| 192 |
if (tester.failure_type == PTFT_PROXY_STRATEGY) { |
| 193 |
testing_channel_drain_queued_tasks(channel); |
| 194 |
} else { |
| 195 |
testing_channel_run_currently_queued_tasks(channel); |
| 196 |
} |
| 197 |
|
| 198 |
if (tester.test_mode == PTTM_NO_PROXY) { |
| 199 |
return AWS_OP_SUCCESS; |
| 200 |
} |
| 201 |
|
| 202 |
if (tester.failure_type == PTFT_NONE || tester.failure_type == PTFT_CONNECT_REQUEST || |
| 203 |
tester.failure_type == PTFT_TLS_NEGOTIATION) { |
| 204 |
if (tester.proxy_options.connection_type == AWS_HPCT_HTTP_TUNNEL) { |
| 205 |
|
| 206 |
ASSERT_SUCCESS(proxy_tester_verify_connect_request(&tester)); |
| 207 |
ASSERT_SUCCESS(proxy_tester_send_connect_response(&tester)); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return AWS_OP_SUCCESS; |
| 212 |
} |
| 213 |
|
| 214 |
struct aws_http_connection_system_vtable s_proxy_connection_system_vtable = { |
| 215 |
.aws_client_bootstrap_new_socket_channel = s_test_aws_proxy_new_socket_channel, |
| 216 |
}; |
| 217 |
|
| 218 |
struct mocked_proxy_test_options { |
| 219 |
enum proxy_tester_test_mode test_mode; |
| 220 |
enum proxy_tester_failure_type failure_type; |
| 221 |
struct aws_http_proxy_strategy *proxy_strategy; |
| 222 |
|
| 223 |
enum aws_http_proxy_authentication_type auth_type; |
| 224 |
struct aws_byte_cursor legacy_basic_username; |
| 225 |
struct aws_byte_cursor legacy_basic_password; |
| 226 |
|
| 227 |
uint32_t mocked_response_count; |
| 228 |
struct aws_byte_cursor *mocked_responses; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
static int s_setup_proxy_test(struct aws_allocator *allocator, struct mocked_proxy_test_options *config) { |
| 235 |
|
| 236 |
aws_http_connection_set_system_vtable(&s_proxy_connection_system_vtable); |
| 237 |
aws_http_proxy_system_set_vtable(&s_proxy_table_for_tls); |
| 238 |
|
| 239 |
struct aws_http_proxy_options proxy_options = { |
| 240 |
.connection_type = (config->test_mode == PTTM_HTTP_FORWARD) ? AWS_HPCT_HTTP_FORWARD : AWS_HPCT_HTTP_TUNNEL, |
| 241 |
.host = aws_byte_cursor_from_c_str(s_proxy_host_name), |
| 242 |
.port = s_proxy_port, |
| 243 |
.proxy_strategy = config->proxy_strategy, |
| 244 |
.auth_type = config->auth_type, |
| 245 |
.auth_username = config->legacy_basic_username, |
| 246 |
.auth_password = config->legacy_basic_password, |
| 247 |
}; |
| 248 |
|
| 249 |
struct proxy_tester_options options = { |
| 250 |
.alloc = allocator, |
| 251 |
.proxy_options = &proxy_options, |
| 252 |
.host = aws_byte_cursor_from_c_str(s_host_name), |
| 253 |
.port = s_port, |
| 254 |
.test_mode = config->test_mode, |
| 255 |
.failure_type = config->failure_type, |
| 256 |
.desired_connect_response_count = config->mocked_response_count, |
| 257 |
.desired_connect_responses = config->mocked_responses, |
| 258 |
}; |
| 259 |
|
| 260 |
ASSERT_SUCCESS(proxy_tester_init(&tester, &options)); |
| 261 |
|
| 262 |
proxy_tester_wait(&tester, proxy_tester_connection_setup_pred); |
| 263 |
|
| 264 |
return AWS_OP_SUCCESS; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static int s_test_http_forwarding_proxy_connection_proxy_target(struct aws_allocator *allocator, void *ctx) { |
| 272 |
(void)ctx; |
| 273 |
|
| 274 |
struct mocked_proxy_test_options options = { |
| 275 |
.test_mode = PTTM_HTTP_FORWARD, |
| 276 |
.failure_type = PTFT_NONE, |
| 277 |
}; |
| 278 |
|
| 279 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 280 |
|
| 281 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 282 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 283 |
|
| 284 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 285 |
|
| 286 |
return AWS_OP_SUCCESS; |
| 287 |
} |
| 288 |
AWS_TEST_CASE(http_forwarding_proxy_connection_proxy_target, s_test_http_forwarding_proxy_connection_proxy_target); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static int s_test_http_forwarding_proxy_connection_channel_failure(struct aws_allocator *allocator, void *ctx) { |
| 295 |
(void)ctx; |
| 296 |
|
| 297 |
struct mocked_proxy_test_options options = { |
| 298 |
.test_mode = PTTM_HTTP_FORWARD, |
| 299 |
.failure_type = PTFT_CHANNEL, |
| 300 |
}; |
| 301 |
|
| 302 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 303 |
|
| 304 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 305 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 306 |
ASSERT_TRUE(tester.wait_result != AWS_ERROR_SUCCESS); |
| 307 |
ASSERT_TRUE(tester.client_connection == NULL); |
| 308 |
|
| 309 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 310 |
|
| 311 |
return AWS_OP_SUCCESS; |
| 312 |
} |
| 313 |
AWS_TEST_CASE( |
| 314 |
http_forwarding_proxy_connection_channel_failure, |
| 315 |
s_test_http_forwarding_proxy_connection_channel_failure); |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
static int s_test_http_forwarding_proxy_connection_connect_failure(struct aws_allocator *allocator, void *ctx) { |
| 322 |
(void)ctx; |
| 323 |
|
| 324 |
struct mocked_proxy_test_options options = { |
| 325 |
.test_mode = PTTM_HTTP_FORWARD, |
| 326 |
.failure_type = PTFT_CONNECTION, |
| 327 |
}; |
| 328 |
|
| 329 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 330 |
|
| 331 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 332 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 333 |
ASSERT_TRUE(tester.wait_result != AWS_ERROR_SUCCESS); |
| 334 |
ASSERT_TRUE(tester.client_connection == NULL); |
| 335 |
|
| 336 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 337 |
|
| 338 |
return AWS_OP_SUCCESS; |
| 339 |
} |
| 340 |
AWS_TEST_CASE( |
| 341 |
http_forwarding_proxy_connection_connect_failure, |
| 342 |
s_test_http_forwarding_proxy_connection_connect_failure); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static int s_test_https_tunnel_proxy_connection_success(struct aws_allocator *allocator, void *ctx) { |
| 349 |
(void)ctx; |
| 350 |
|
| 351 |
struct mocked_proxy_test_options options = { |
| 352 |
.test_mode = PTTM_HTTPS_TUNNEL, |
| 353 |
.failure_type = PTFT_NONE, |
| 354 |
}; |
| 355 |
|
| 356 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 357 |
|
| 358 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 359 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 360 |
ASSERT_TRUE(tester.client_connection != NULL); |
| 361 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_SUCCESS); |
| 362 |
|
| 363 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 364 |
|
| 365 |
return AWS_OP_SUCCESS; |
| 366 |
} |
| 367 |
AWS_TEST_CASE(https_tunnel_proxy_connection_success, s_test_https_tunnel_proxy_connection_success); |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
static int s_test_http_tunnel_proxy_connection_success(struct aws_allocator *allocator, void *ctx) { |
| 374 |
(void)ctx; |
| 375 |
|
| 376 |
struct mocked_proxy_test_options options = { |
| 377 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 378 |
.failure_type = PTFT_NONE, |
| 379 |
}; |
| 380 |
|
| 381 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 382 |
|
| 383 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 384 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 385 |
ASSERT_TRUE(tester.client_connection != NULL); |
| 386 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_SUCCESS); |
| 387 |
|
| 388 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 389 |
|
| 390 |
return AWS_OP_SUCCESS; |
| 391 |
} |
| 392 |
AWS_TEST_CASE(http_tunnel_proxy_connection_success, s_test_http_tunnel_proxy_connection_success); |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static int s_test_https_tunnel_proxy_connection_failure_connect(struct aws_allocator *allocator, void *ctx) { |
| 399 |
(void)ctx; |
| 400 |
|
| 401 |
struct mocked_proxy_test_options options = { |
| 402 |
.test_mode = PTTM_HTTPS_TUNNEL, |
| 403 |
.failure_type = PTFT_CONNECT_REQUEST, |
| 404 |
}; |
| 405 |
|
| 406 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 407 |
|
| 408 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 409 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 410 |
ASSERT_TRUE(tester.client_connection == NULL); |
| 411 |
ASSERT_TRUE(tester.wait_result != AWS_ERROR_SUCCESS); |
| 412 |
|
| 413 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 414 |
|
| 415 |
return AWS_OP_SUCCESS; |
| 416 |
} |
| 417 |
AWS_TEST_CASE(https_tunnel_proxy_connection_failure_connect, s_test_https_tunnel_proxy_connection_failure_connect); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
static int s_test_http_tunnel_proxy_connection_failure_connect(struct aws_allocator *allocator, void *ctx) { |
| 424 |
(void)ctx; |
| 425 |
|
| 426 |
struct mocked_proxy_test_options options = { |
| 427 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 428 |
.failure_type = PTFT_CONNECT_REQUEST, |
| 429 |
}; |
| 430 |
|
| 431 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 432 |
|
| 433 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 434 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 435 |
ASSERT_TRUE(tester.client_connection == NULL); |
| 436 |
ASSERT_TRUE(tester.wait_result != AWS_ERROR_SUCCESS); |
| 437 |
|
| 438 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 439 |
|
| 440 |
return AWS_OP_SUCCESS; |
| 441 |
} |
| 442 |
AWS_TEST_CASE(http_tunnel_proxy_connection_failure_connect, s_test_http_tunnel_proxy_connection_failure_connect); |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
static int s_test_https_tunnel_proxy_connection_failure_tls(struct aws_allocator *allocator, void *ctx) { |
| 449 |
(void)ctx; |
| 450 |
|
| 451 |
struct mocked_proxy_test_options options = { |
| 452 |
.test_mode = PTTM_HTTPS_TUNNEL, |
| 453 |
.failure_type = PTFT_TLS_NEGOTIATION, |
| 454 |
}; |
| 455 |
|
| 456 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 457 |
|
| 458 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 459 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 460 |
ASSERT_NULL(tester.client_connection); |
| 461 |
ASSERT_TRUE(AWS_ERROR_SUCCESS != tester.wait_result); |
| 462 |
|
| 463 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 464 |
|
| 465 |
return AWS_OP_SUCCESS; |
| 466 |
} |
| 467 |
AWS_TEST_CASE(https_tunnel_proxy_connection_failure_tls, s_test_https_tunnel_proxy_connection_failure_tls); |
| 468 |
|
| 469 |
static int s_verify_transformed_request( |
| 470 |
struct aws_http_message *untransformed_request, |
| 471 |
struct aws_http_message *transformed_request, |
| 472 |
struct aws_allocator *allocator) { |
| 473 |
|
| 474 |
|
| 475 |
struct aws_byte_cursor method_cursor; |
| 476 |
ASSERT_SUCCESS(aws_http_message_get_request_method(transformed_request, &method_cursor)); |
| 477 |
|
| 478 |
struct aws_byte_cursor starting_method_cursor; |
| 479 |
ASSERT_SUCCESS(aws_http_message_get_request_method(untransformed_request, &starting_method_cursor)); |
| 480 |
|
| 481 |
ASSERT_TRUE(aws_byte_cursor_eq(&method_cursor, &starting_method_cursor)); |
| 482 |
|
| 483 |
|
| 484 |
struct aws_byte_cursor path; |
| 485 |
ASSERT_SUCCESS(aws_http_message_get_request_path(transformed_request, &path)); |
| 486 |
|
| 487 |
struct aws_uri uri; |
| 488 |
ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &path)); |
| 489 |
|
| 490 |
struct aws_byte_cursor expected_scheme = aws_byte_cursor_from_c_str("http"); |
| 491 |
ASSERT_TRUE(aws_byte_cursor_eq(aws_uri_scheme(&uri), &expected_scheme)); |
| 492 |
|
| 493 |
struct aws_byte_cursor expected_host = aws_byte_cursor_from_string(s_mock_request_host); |
| 494 |
ASSERT_TRUE(aws_byte_cursor_eq(aws_uri_host_name(&uri), &expected_host)); |
| 495 |
|
| 496 |
struct aws_byte_cursor expected_query = aws_byte_cursor_from_c_str(""); |
| 497 |
ASSERT_TRUE(aws_byte_cursor_eq(aws_uri_query_string(&uri), &expected_query)); |
| 498 |
|
| 499 |
struct aws_byte_cursor expected_path = aws_byte_cursor_from_c_str("/"); |
| 500 |
ASSERT_TRUE(aws_byte_cursor_eq(aws_uri_path(&uri), &expected_path)); |
| 501 |
|
| 502 |
|
| 503 |
size_t untransformed_header_count = aws_http_message_get_header_count(untransformed_request); |
| 504 |
for (size_t i = 0; i < untransformed_header_count; ++i) { |
| 505 |
struct aws_http_header header; |
| 506 |
ASSERT_SUCCESS(aws_http_message_get_header(untransformed_request, &header, i)); |
| 507 |
ASSERT_TRUE(s_is_header_and_value_in_request(transformed_request, &header)); |
| 508 |
} |
| 509 |
|
| 510 |
aws_uri_clean_up(&uri); |
| 511 |
|
| 512 |
return AWS_OP_SUCCESS; |
| 513 |
} |
| 514 |
|
| 515 |
static int s_do_http_forwarding_proxy_request_transform_test( |
| 516 |
struct aws_allocator *allocator, |
| 517 |
struct mocked_proxy_test_options *test_options, |
| 518 |
int (*transformed_request_verifier_fn)(struct aws_http_message *)) { |
| 519 |
|
| 520 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, test_options)); |
| 521 |
|
| 522 |
struct aws_http_message *untransformed_request = s_build_http_request(allocator); |
| 523 |
struct aws_http_message *request = s_build_http_request(allocator); |
| 524 |
|
| 525 |
struct aws_http_make_request_options request_options = { |
| 526 |
.self_size = sizeof(request_options), |
| 527 |
.request = request, |
| 528 |
.user_data = &tester, |
| 529 |
}; |
| 530 |
|
| 531 |
struct aws_http_stream *stream = aws_http_connection_make_request(tester.client_connection, &request_options); |
| 532 |
ASSERT_NOT_NULL(stream); |
| 533 |
aws_http_stream_activate(stream); |
| 534 |
|
| 535 |
struct testing_channel *channel = proxy_tester_get_current_channel(&tester); |
| 536 |
testing_channel_run_currently_queued_tasks(channel); |
| 537 |
|
| 538 |
s_verify_transformed_request(untransformed_request, request, allocator); |
| 539 |
|
| 540 |
if (transformed_request_verifier_fn != NULL) { |
| 541 |
ASSERT_SUCCESS(transformed_request_verifier_fn(request)); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
aws_http_stream_release(stream); |
| 546 |
|
| 547 |
aws_http_message_destroy(request); |
| 548 |
aws_http_message_destroy(untransformed_request); |
| 549 |
|
| 550 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 551 |
|
| 552 |
return AWS_OP_SUCCESS; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
static int s_test_http_forwarding_proxy_request_transform(struct aws_allocator *allocator, void *ctx) { |
| 559 |
(void)ctx; |
| 560 |
|
| 561 |
struct mocked_proxy_test_options options = { |
| 562 |
.test_mode = PTTM_HTTP_FORWARD, |
| 563 |
.failure_type = PTFT_NONE, |
| 564 |
.proxy_strategy = NULL, |
| 565 |
}; |
| 566 |
|
| 567 |
ASSERT_SUCCESS(s_do_http_forwarding_proxy_request_transform_test(allocator, &options, NULL)); |
| 568 |
|
| 569 |
return AWS_OP_SUCCESS; |
| 570 |
} |
| 571 |
AWS_TEST_CASE(http_forwarding_proxy_request_transform, s_test_http_forwarding_proxy_request_transform); |
| 572 |
|
| 573 |
static int s_check_for_basic_auth_header(struct aws_http_message *transformed_request) { |
| 574 |
|
| 575 |
struct aws_http_header auth_header; |
| 576 |
auth_header.name = aws_byte_cursor_from_string(s_expected_basic_auth_header_name); |
| 577 |
auth_header.value = aws_byte_cursor_from_string(s_expected_basic_auth_header_value); |
| 578 |
ASSERT_TRUE(s_is_header_and_value_in_request(transformed_request, &auth_header)); |
| 579 |
|
| 580 |
return AWS_OP_SUCCESS; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
static int s_test_http_forwarding_proxy_request_transform_basic_auth(struct aws_allocator *allocator, void *ctx) { |
| 587 |
(void)ctx; |
| 588 |
|
| 589 |
struct aws_http_proxy_strategy_basic_auth_options config = { |
| 590 |
.proxy_connection_type = AWS_HPCT_HTTP_FORWARD, |
| 591 |
.user_name = aws_byte_cursor_from_string(s_mock_request_username), |
| 592 |
.password = aws_byte_cursor_from_string(s_mock_request_password), |
| 593 |
}; |
| 594 |
|
| 595 |
struct aws_http_proxy_strategy *proxy_strategy = aws_http_proxy_strategy_new_basic_auth(allocator, &config); |
| 596 |
|
| 597 |
struct mocked_proxy_test_options options = { |
| 598 |
.test_mode = PTTM_HTTP_FORWARD, |
| 599 |
.failure_type = PTFT_NONE, |
| 600 |
.proxy_strategy = proxy_strategy, |
| 601 |
}; |
| 602 |
|
| 603 |
ASSERT_SUCCESS( |
| 604 |
s_do_http_forwarding_proxy_request_transform_test(allocator, &options, s_check_for_basic_auth_header)); |
| 605 |
|
| 606 |
aws_http_proxy_strategy_release(proxy_strategy); |
| 607 |
|
| 608 |
return AWS_OP_SUCCESS; |
| 609 |
} |
| 610 |
AWS_TEST_CASE( |
| 611 |
http_forwarding_proxy_request_transform_basic_auth, |
| 612 |
s_test_http_forwarding_proxy_request_transform_basic_auth); |
| 613 |
|
| 614 |
static int s_test_http_forwarding_proxy_request_transform_legacy_basic_auth( |
| 615 |
struct aws_allocator *allocator, |
| 616 |
void *ctx) { |
| 617 |
(void)ctx; |
| 618 |
|
| 619 |
struct mocked_proxy_test_options options = { |
| 620 |
.test_mode = PTTM_HTTP_FORWARD, |
| 621 |
.failure_type = PTFT_NONE, |
| 622 |
.auth_type = AWS_HPAT_BASIC, |
| 623 |
.legacy_basic_username = aws_byte_cursor_from_string(s_mock_request_username), |
| 624 |
.legacy_basic_password = aws_byte_cursor_from_string(s_mock_request_password), |
| 625 |
}; |
| 626 |
|
| 627 |
ASSERT_SUCCESS( |
| 628 |
s_do_http_forwarding_proxy_request_transform_test(allocator, &options, s_check_for_basic_auth_header)); |
| 629 |
|
| 630 |
return AWS_OP_SUCCESS; |
| 631 |
} |
| 632 |
AWS_TEST_CASE( |
| 633 |
http_forwarding_proxy_request_transform_legacy_basic_auth, |
| 634 |
s_test_http_forwarding_proxy_request_transform_legacy_basic_auth); |
| 635 |
|
| 636 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_kerberos_token_value, "abcdefABCDEF123"); |
| 637 |
|
| 638 |
static struct aws_string *s_mock_aws_http_proxy_negotiation_kerberos_get_token_sync_fn( |
| 639 |
void *user_data, |
| 640 |
int *out_error_code) { |
| 641 |
|
| 642 |
struct aws_allocator *allocator = user_data; |
| 643 |
|
| 644 |
*out_error_code = AWS_ERROR_SUCCESS; |
| 645 |
return aws_string_new_from_string(allocator, s_mock_kerberos_token_value); |
| 646 |
} |
| 647 |
|
| 648 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_auth_header_name, "Proxy-Authorization"); |
| 649 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_kerberos_auth_header_value, "Negotiate abcdefABCDEF123"); |
| 650 |
|
| 651 |
static int s_verify_kerberos_connect_request(struct aws_http_message *request) { |
| 652 |
|
| 653 |
struct aws_http_header auth_header; |
| 654 |
auth_header.name = aws_byte_cursor_from_string(s_expected_auth_header_name); |
| 655 |
auth_header.value = aws_byte_cursor_from_string(s_expected_kerberos_auth_header_value); |
| 656 |
ASSERT_TRUE(s_is_header_and_value_in_request(request, &auth_header)); |
| 657 |
|
| 658 |
return AWS_OP_SUCCESS; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
static int s_test_http_proxy_request_transform_kerberos(struct aws_allocator *allocator, void *ctx) { |
| 665 |
(void)ctx; |
| 666 |
|
| 667 |
struct aws_http_proxy_strategy_tunneling_kerberos_options config = { |
| 668 |
.get_token = s_mock_aws_http_proxy_negotiation_kerberos_get_token_sync_fn, |
| 669 |
.get_token_user_data = allocator, |
| 670 |
}; |
| 671 |
|
| 672 |
struct aws_http_proxy_strategy *kerberos_strategy = |
| 673 |
aws_http_proxy_strategy_new_tunneling_kerberos(allocator, &config); |
| 674 |
|
| 675 |
struct mocked_proxy_test_options options = { |
| 676 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 677 |
.failure_type = PTFT_NONE, |
| 678 |
.proxy_strategy = kerberos_strategy, |
| 679 |
}; |
| 680 |
|
| 681 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 682 |
|
| 683 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 684 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 685 |
ASSERT_TRUE(tester.client_connection != NULL); |
| 686 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_SUCCESS); |
| 687 |
|
| 688 |
ASSERT_INT_EQUALS(1, aws_array_list_length(&tester.connect_requests)); |
| 689 |
|
| 690 |
struct aws_http_message *connect_request = NULL; |
| 691 |
aws_array_list_get_at(&tester.connect_requests, &connect_request, 0); |
| 692 |
|
| 693 |
ASSERT_SUCCESS(s_verify_kerberos_connect_request(connect_request)); |
| 694 |
|
| 695 |
aws_http_proxy_strategy_release(kerberos_strategy); |
| 696 |
|
| 697 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 698 |
|
| 699 |
return AWS_OP_SUCCESS; |
| 700 |
} |
| 701 |
|
| 702 |
AWS_TEST_CASE(http_proxy_request_transform_kerberos, s_test_http_proxy_request_transform_kerberos); |
| 703 |
|
| 704 |
static struct aws_string *s_mock_aws_http_proxy_negotiation_kerberos_get_token_sync_failure_fn( |
| 705 |
void *user_data, |
| 706 |
int *out_error_code) { |
| 707 |
|
| 708 |
(void)user_data; |
| 709 |
|
| 710 |
*out_error_code = AWS_ERROR_UNKNOWN; |
| 711 |
|
| 712 |
return NULL; |
| 713 |
} |
| 714 |
|
| 715 |
static int s_test_http_proxy_kerberos_token_failure(struct aws_allocator *allocator, void *ctx) { |
| 716 |
(void)ctx; |
| 717 |
|
| 718 |
struct aws_http_proxy_strategy_tunneling_kerberos_options config = { |
| 719 |
.get_token = s_mock_aws_http_proxy_negotiation_kerberos_get_token_sync_failure_fn, |
| 720 |
.get_token_user_data = NULL, |
| 721 |
}; |
| 722 |
|
| 723 |
struct aws_http_proxy_strategy *kerberos_strategy = |
| 724 |
aws_http_proxy_strategy_new_tunneling_kerberos(allocator, &config); |
| 725 |
|
| 726 |
struct mocked_proxy_test_options options = { |
| 727 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 728 |
.failure_type = PTFT_PROXY_STRATEGY, |
| 729 |
.proxy_strategy = kerberos_strategy, |
| 730 |
}; |
| 731 |
|
| 732 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 733 |
|
| 734 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 735 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 736 |
ASSERT_TRUE(tester.client_connection == NULL); |
| 737 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_UNKNOWN); |
| 738 |
|
| 739 |
aws_http_proxy_strategy_release(kerberos_strategy); |
| 740 |
|
| 741 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 742 |
|
| 743 |
return AWS_OP_SUCCESS; |
| 744 |
} |
| 745 |
|
| 746 |
AWS_TEST_CASE(http_proxy_kerberos_token_failure, s_test_http_proxy_kerberos_token_failure); |
| 747 |
|
| 748 |
static int s_test_http_proxy_kerberos_connect_failure(struct aws_allocator *allocator, void *ctx) { |
| 749 |
(void)ctx; |
| 750 |
|
| 751 |
struct aws_http_proxy_strategy_tunneling_kerberos_options config = { |
| 752 |
.get_token = s_mock_aws_http_proxy_negotiation_kerberos_get_token_sync_fn, |
| 753 |
.get_token_user_data = allocator, |
| 754 |
}; |
| 755 |
|
| 756 |
struct aws_http_proxy_strategy *kerberos_strategy = |
| 757 |
aws_http_proxy_strategy_new_tunneling_kerberos(allocator, &config); |
| 758 |
|
| 759 |
struct mocked_proxy_test_options options = { |
| 760 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 761 |
.failure_type = PTFT_CONNECT_REQUEST, |
| 762 |
.proxy_strategy = kerberos_strategy, |
| 763 |
}; |
| 764 |
|
| 765 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 766 |
|
| 767 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 768 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 769 |
ASSERT_TRUE(tester.client_connection == NULL); |
| 770 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_HTTP_PROXY_CONNECT_FAILED); |
| 771 |
|
| 772 |
aws_http_proxy_strategy_release(kerberos_strategy); |
| 773 |
|
| 774 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 775 |
|
| 776 |
return AWS_OP_SUCCESS; |
| 777 |
} |
| 778 |
|
| 779 |
AWS_TEST_CASE(http_proxy_kerberos_connect_failure, s_test_http_proxy_kerberos_connect_failure); |
| 780 |
|
| 781 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_ntlm_token_value, "NTLM_TOKEN"); |
| 782 |
AWS_STATIC_STRING_FROM_LITERAL(s_mock_ntlm_challenge_token_value, "NTLM_CHALLENGE_TOKEN"); |
| 783 |
|
| 784 |
static struct aws_string *s_mock_aws_http_proxy_negotiation_ntlm_get_challenge_token_sync_fn( |
| 785 |
void *user_data, |
| 786 |
const struct aws_byte_cursor *challenge_value, |
| 787 |
int *out_error_code) { |
| 788 |
|
| 789 |
(void)challenge_value; |
| 790 |
|
| 791 |
struct aws_allocator *allocator = user_data; |
| 792 |
|
| 793 |
*out_error_code = AWS_ERROR_SUCCESS; |
| 794 |
return aws_string_new_from_string(allocator, s_mock_ntlm_challenge_token_value); |
| 795 |
} |
| 796 |
|
| 797 |
static struct aws_string *s_mock_aws_http_proxy_negotiation_ntlm_get_token_sync_fn( |
| 798 |
void *user_data, |
| 799 |
int *out_error_code) { |
| 800 |
struct aws_allocator *allocator = user_data; |
| 801 |
|
| 802 |
*out_error_code = AWS_ERROR_SUCCESS; |
| 803 |
return aws_string_new_from_string(allocator, s_mock_ntlm_token_value); |
| 804 |
} |
| 805 |
|
| 806 |
static int s_verify_identity_connect_request(struct aws_http_message *request) { |
| 807 |
ASSERT_FALSE(s_is_header_in_request(request, aws_byte_cursor_from_string(s_expected_auth_header_name))); |
| 808 |
|
| 809 |
return AWS_OP_SUCCESS; |
| 810 |
} |
| 811 |
|
| 812 |
static struct aws_http_proxy_strategy *s_create_adaptive_strategy(struct aws_allocator *allocator) { |
| 813 |
struct aws_http_proxy_strategy_tunneling_kerberos_options kerberos_config = { |
| 814 |
.get_token = s_mock_aws_http_proxy_negotiation_kerberos_get_token_sync_fn, |
| 815 |
.get_token_user_data = allocator, |
| 816 |
}; |
| 817 |
|
| 818 |
struct aws_http_proxy_strategy_tunneling_ntlm_options ntlm_config = { |
| 819 |
.get_token = s_mock_aws_http_proxy_negotiation_ntlm_get_token_sync_fn, |
| 820 |
.get_challenge_token = s_mock_aws_http_proxy_negotiation_ntlm_get_challenge_token_sync_fn, |
| 821 |
.get_challenge_token_user_data = allocator, |
| 822 |
}; |
| 823 |
|
| 824 |
struct aws_http_proxy_strategy_tunneling_adaptive_options config = { |
| 825 |
.ntlm_options = &ntlm_config, |
| 826 |
.kerberos_options = &kerberos_config, |
| 827 |
}; |
| 828 |
|
| 829 |
return aws_http_proxy_strategy_new_tunneling_adaptive(allocator, &config); |
| 830 |
} |
| 831 |
|
| 832 |
static int s_test_http_proxy_adaptive_identity_success(struct aws_allocator *allocator, void *ctx) { |
| 833 |
(void)ctx; |
| 834 |
|
| 835 |
struct aws_http_proxy_strategy *adaptive_strategy = s_create_adaptive_strategy(allocator); |
| 836 |
|
| 837 |
struct mocked_proxy_test_options options = { |
| 838 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 839 |
.failure_type = PTFT_NONE, |
| 840 |
.proxy_strategy = adaptive_strategy, |
| 841 |
}; |
| 842 |
|
| 843 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 844 |
|
| 845 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 846 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 847 |
ASSERT_TRUE(tester.client_connection != NULL); |
| 848 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_SUCCESS); |
| 849 |
|
| 850 |
ASSERT_INT_EQUALS(1, aws_array_list_length(&tester.connect_requests)); |
| 851 |
|
| 852 |
struct aws_http_message *connect_request = NULL; |
| 853 |
aws_array_list_get_at(&tester.connect_requests, &connect_request, 0); |
| 854 |
|
| 855 |
ASSERT_SUCCESS(s_verify_identity_connect_request(connect_request)); |
| 856 |
|
| 857 |
aws_http_proxy_strategy_release(adaptive_strategy); |
| 858 |
|
| 859 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 860 |
|
| 861 |
return AWS_OP_SUCCESS; |
| 862 |
} |
| 863 |
|
| 864 |
AWS_TEST_CASE(http_proxy_adaptive_identity_success, s_test_http_proxy_adaptive_identity_success); |
| 865 |
|
| 866 |
AWS_STATIC_STRING_FROM_LITERAL(s_unauthorized_response, "HTTP/1.0 407 Unauthorized\r\n\r\n"); |
| 867 |
AWS_STATIC_STRING_FROM_LITERAL(s_good_response, "HTTP/1.0 200 Connection established\r\nconnection: close\r\n\r\n"); |
| 868 |
|
| 869 |
typedef int (*aws_proxy_test_verify_connect_fn)(struct aws_http_message *); |
| 870 |
|
| 871 |
static int s_verify_connect_requests(aws_proxy_test_verify_connect_fn verify_functions[], size_t function_count) { |
| 872 |
size_t connect_requests = aws_array_list_length(&tester.connect_requests); |
| 873 |
ASSERT_INT_EQUALS(function_count, connect_requests); |
| 874 |
|
| 875 |
for (size_t i = 0; i < connect_requests; ++i) { |
| 876 |
struct aws_http_message *request = NULL; |
| 877 |
aws_array_list_get_at(&tester.connect_requests, &request, i); |
| 878 |
|
| 879 |
ASSERT_SUCCESS(verify_functions[i](request)); |
| 880 |
} |
| 881 |
|
| 882 |
return AWS_OP_SUCCESS; |
| 883 |
} |
| 884 |
|
| 885 |
static int s_test_http_proxy_adaptive_kerberos_success(struct aws_allocator *allocator, void *ctx) { |
| 886 |
(void)ctx; |
| 887 |
|
| 888 |
struct aws_http_proxy_strategy *adaptive_strategy = s_create_adaptive_strategy(allocator); |
| 889 |
|
| 890 |
struct aws_byte_cursor first_response = aws_byte_cursor_from_string(s_unauthorized_response); |
| 891 |
struct aws_byte_cursor second_response = aws_byte_cursor_from_string(s_good_response); |
| 892 |
|
| 893 |
struct aws_byte_cursor connect_responses[] = { |
| 894 |
first_response, |
| 895 |
second_response, |
| 896 |
}; |
| 897 |
|
| 898 |
aws_proxy_test_verify_connect_fn verifiers[] = { |
| 899 |
s_verify_identity_connect_request, |
| 900 |
s_verify_kerberos_connect_request, |
| 901 |
}; |
| 902 |
|
| 903 |
struct mocked_proxy_test_options options = { |
| 904 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 905 |
.failure_type = PTFT_NONE, |
| 906 |
.proxy_strategy = adaptive_strategy, |
| 907 |
.mocked_response_count = 2, |
| 908 |
.mocked_responses = connect_responses, |
| 909 |
}; |
| 910 |
|
| 911 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 912 |
|
| 913 |
ASSERT_SUCCESS(proxy_tester_wait(&tester, proxy_tester_connection_setup_pred)); |
| 914 |
|
| 915 |
ASSERT_TRUE(tester.client_connection != NULL); |
| 916 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_SUCCESS); |
| 917 |
|
| 918 |
s_verify_connect_requests(verifiers, 2); |
| 919 |
|
| 920 |
aws_http_proxy_strategy_release(adaptive_strategy); |
| 921 |
|
| 922 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 923 |
|
| 924 |
return AWS_OP_SUCCESS; |
| 925 |
} |
| 926 |
|
| 927 |
AWS_TEST_CASE(http_proxy_adaptive_kerberos_success, s_test_http_proxy_adaptive_kerberos_success); |
| 928 |
|
| 929 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_ntlm_token_auth_header_value, "NTLM NTLM_TOKEN"); |
| 930 |
|
| 931 |
static int s_verify_ntlm_connect_token_request(struct aws_http_message *request) { |
| 932 |
|
| 933 |
struct aws_http_header auth_header; |
| 934 |
auth_header.name = aws_byte_cursor_from_string(s_expected_auth_header_name); |
| 935 |
auth_header.value = aws_byte_cursor_from_string(s_expected_ntlm_token_auth_header_value); |
| 936 |
ASSERT_TRUE(s_is_header_and_value_in_request(request, &auth_header)); |
| 937 |
|
| 938 |
return AWS_OP_SUCCESS; |
| 939 |
} |
| 940 |
|
| 941 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_ntlm_challenge_token_auth_header_value, "NTLM NTLM_CHALLENGE_TOKEN"); |
| 942 |
|
| 943 |
static int s_verify_ntlm_connect_challenge_token_request(struct aws_http_message *request) { |
| 944 |
|
| 945 |
struct aws_http_header auth_header; |
| 946 |
auth_header.name = aws_byte_cursor_from_string(s_expected_auth_header_name); |
| 947 |
auth_header.value = aws_byte_cursor_from_string(s_expected_ntlm_challenge_token_auth_header_value); |
| 948 |
ASSERT_TRUE(s_is_header_and_value_in_request(request, &auth_header)); |
| 949 |
|
| 950 |
return AWS_OP_SUCCESS; |
| 951 |
} |
| 952 |
|
| 953 |
AWS_STATIC_STRING_FROM_LITERAL(s_ntlm_response, "HTTP/1.0 407 Bad\r\nProxy-Authenticate: TestChallenge\r\n\r\n"); |
| 954 |
|
| 955 |
static int s_test_http_proxy_adaptive_ntlm_success(struct aws_allocator *allocator, void *ctx) { |
| 956 |
(void)ctx; |
| 957 |
|
| 958 |
struct aws_http_proxy_strategy *adaptive_strategy = s_create_adaptive_strategy(allocator); |
| 959 |
|
| 960 |
struct aws_byte_cursor bad_response = aws_byte_cursor_from_string(s_ntlm_response); |
| 961 |
struct aws_byte_cursor good_response = aws_byte_cursor_from_string(s_good_response); |
| 962 |
|
| 963 |
struct aws_byte_cursor connect_responses[] = { |
| 964 |
bad_response, |
| 965 |
bad_response, |
| 966 |
bad_response, |
| 967 |
good_response, |
| 968 |
}; |
| 969 |
|
| 970 |
aws_proxy_test_verify_connect_fn verifiers[] = { |
| 971 |
s_verify_identity_connect_request, |
| 972 |
s_verify_kerberos_connect_request, |
| 973 |
s_verify_ntlm_connect_token_request, |
| 974 |
s_verify_ntlm_connect_challenge_token_request, |
| 975 |
}; |
| 976 |
|
| 977 |
struct mocked_proxy_test_options options = { |
| 978 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 979 |
.failure_type = PTFT_NONE, |
| 980 |
.proxy_strategy = adaptive_strategy, |
| 981 |
.mocked_response_count = 4, |
| 982 |
.mocked_responses = connect_responses, |
| 983 |
}; |
| 984 |
|
| 985 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 986 |
|
| 987 |
ASSERT_SUCCESS(proxy_tester_verify_connect_request(&tester)); |
| 988 |
ASSERT_SUCCESS(proxy_tester_send_connect_response(&tester)); |
| 989 |
|
| 990 |
ASSERT_SUCCESS(proxy_tester_wait(&tester, proxy_tester_connection_setup_pred)); |
| 991 |
|
| 992 |
ASSERT_TRUE(tester.client_connection != NULL); |
| 993 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_SUCCESS); |
| 994 |
|
| 995 |
ASSERT_SUCCESS(s_verify_connect_requests(verifiers, 4)); |
| 996 |
|
| 997 |
aws_http_proxy_strategy_release(adaptive_strategy); |
| 998 |
|
| 999 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 1000 |
|
| 1001 |
return AWS_OP_SUCCESS; |
| 1002 |
} |
| 1003 |
|
| 1004 |
AWS_TEST_CASE(http_proxy_adaptive_ntlm_success, s_test_http_proxy_adaptive_ntlm_success); |
| 1005 |
|
| 1006 |
static int s_test_http_proxy_adaptive_failure(struct aws_allocator *allocator, void *ctx) { |
| 1007 |
(void)ctx; |
| 1008 |
|
| 1009 |
struct aws_http_proxy_strategy *adaptive_strategy = s_create_adaptive_strategy(allocator); |
| 1010 |
|
| 1011 |
struct aws_byte_cursor bad_response = aws_byte_cursor_from_string(s_ntlm_response); |
| 1012 |
|
| 1013 |
struct aws_byte_cursor connect_responses[] = { |
| 1014 |
bad_response, |
| 1015 |
bad_response, |
| 1016 |
bad_response, |
| 1017 |
bad_response, |
| 1018 |
}; |
| 1019 |
|
| 1020 |
aws_proxy_test_verify_connect_fn verifiers[] = { |
| 1021 |
s_verify_identity_connect_request, |
| 1022 |
s_verify_kerberos_connect_request, |
| 1023 |
s_verify_ntlm_connect_token_request, |
| 1024 |
s_verify_ntlm_connect_challenge_token_request, |
| 1025 |
}; |
| 1026 |
|
| 1027 |
struct mocked_proxy_test_options options = { |
| 1028 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 1029 |
.failure_type = PTFT_NONE, |
| 1030 |
.proxy_strategy = adaptive_strategy, |
| 1031 |
.mocked_response_count = 4, |
| 1032 |
.mocked_responses = connect_responses, |
| 1033 |
}; |
| 1034 |
|
| 1035 |
ASSERT_SUCCESS(s_setup_proxy_test(allocator, &options)); |
| 1036 |
|
| 1037 |
ASSERT_SUCCESS(proxy_tester_verify_connect_request(&tester)); |
| 1038 |
ASSERT_SUCCESS(proxy_tester_send_connect_response(&tester)); |
| 1039 |
|
| 1040 |
ASSERT_SUCCESS(proxy_tester_wait(&tester, proxy_tester_connection_setup_pred)); |
| 1041 |
|
| 1042 |
ASSERT_TRUE(tester.wait_result == AWS_ERROR_HTTP_PROXY_CONNECT_FAILED); |
| 1043 |
|
| 1044 |
ASSERT_SUCCESS(s_verify_connect_requests(verifiers, 4)); |
| 1045 |
|
| 1046 |
aws_http_proxy_strategy_release(adaptive_strategy); |
| 1047 |
|
| 1048 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 1049 |
|
| 1050 |
return AWS_OP_SUCCESS; |
| 1051 |
} |
| 1052 |
|
| 1053 |
AWS_TEST_CASE(http_proxy_adaptive_failure, s_test_http_proxy_adaptive_failure); |
| 1054 |
|
| 1055 |
AWS_STATIC_STRING_FROM_LITERAL(s_rewrite_host, "www.uri.com"); |
| 1056 |
AWS_STATIC_STRING_FROM_LITERAL(s_rewrite_path, "/main/index.html?foo=bar"); |
| 1057 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_rewritten_path, "http://www.uri.com:80/main/index.html?foo=bar"); |
| 1058 |
|
| 1059 |
static void s_proxy_forwarding_request_rewrite_setup_fn( |
| 1060 |
struct aws_http_connection *connection, |
| 1061 |
int error_code, |
| 1062 |
void *user_data) { |
| 1063 |
(void)connection; |
| 1064 |
(void)error_code; |
| 1065 |
(void)user_data; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static void s_proxy_forwarding_request_rewrite_shutdown_fn( |
| 1069 |
struct aws_http_connection *connection, |
| 1070 |
int error_code, |
| 1071 |
void *user_data) { |
| 1072 |
(void)connection; |
| 1073 |
(void)error_code; |
| 1074 |
(void)user_data; |
| 1075 |
} |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
static int s_do_request_rewrite_test( |
| 1084 |
struct aws_allocator *allocator, |
| 1085 |
const struct aws_string *method, |
| 1086 |
const struct aws_string *path, |
| 1087 |
const struct aws_string *host, |
| 1088 |
const struct aws_string *expected_path) { |
| 1089 |
|
| 1090 |
struct aws_http_proxy_options proxy_options = { |
| 1091 |
.host = aws_byte_cursor_from_c_str(s_proxy_host_name), |
| 1092 |
.port = s_proxy_port, |
| 1093 |
}; |
| 1094 |
|
| 1095 |
struct aws_http_client_connection_options connection_options = { |
| 1096 |
.allocator = allocator, |
| 1097 |
.host_name = aws_byte_cursor_from_string(s_rewrite_host), |
| 1098 |
.port = 80, |
| 1099 |
.proxy_options = &proxy_options, |
| 1100 |
.on_setup = s_proxy_forwarding_request_rewrite_setup_fn, |
| 1101 |
.on_shutdown = s_proxy_forwarding_request_rewrite_shutdown_fn, |
| 1102 |
}; |
| 1103 |
|
| 1104 |
struct aws_http_proxy_user_data *user_data = |
| 1105 |
aws_http_proxy_user_data_new(allocator, &connection_options, NULL, NULL); |
| 1106 |
struct aws_http_message *request = s_build_dummy_http_request( |
| 1107 |
allocator, |
| 1108 |
aws_byte_cursor_from_string(method), |
| 1109 |
aws_byte_cursor_from_string(path), |
| 1110 |
aws_byte_cursor_from_string(host)); |
| 1111 |
|
| 1112 |
ASSERT_SUCCESS(aws_http_rewrite_uri_for_proxy_request(request, user_data)); |
| 1113 |
|
| 1114 |
struct aws_byte_cursor expected_rewritten_path = aws_byte_cursor_from_string(expected_path); |
| 1115 |
struct aws_byte_cursor rewritten_path; |
| 1116 |
ASSERT_SUCCESS(aws_http_message_get_request_path(request, &rewritten_path)); |
| 1117 |
|
| 1118 |
ASSERT_TRUE(aws_byte_cursor_eq(&rewritten_path, &expected_rewritten_path)); |
| 1119 |
|
| 1120 |
aws_http_message_destroy(request); |
| 1121 |
aws_http_proxy_user_data_destroy(user_data); |
| 1122 |
|
| 1123 |
return AWS_OP_SUCCESS; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static int s_test_http_forwarding_proxy_uri_rewrite(struct aws_allocator *allocator, void *ctx) { |
| 1127 |
(void)ctx; |
| 1128 |
|
| 1129 |
ASSERT_SUCCESS(s_do_request_rewrite_test( |
| 1130 |
allocator, s_mock_request_method, s_rewrite_path, s_rewrite_host, s_expected_rewritten_path)); |
| 1131 |
|
| 1132 |
return AWS_OP_SUCCESS; |
| 1133 |
} |
| 1134 |
AWS_TEST_CASE(http_forwarding_proxy_uri_rewrite, s_test_http_forwarding_proxy_uri_rewrite); |
| 1135 |
|
| 1136 |
AWS_STATIC_STRING_FROM_LITERAL(s_options_request_method, "OPTIONS"); |
| 1137 |
AWS_STATIC_STRING_FROM_LITERAL(s_options_star_path, "*"); |
| 1138 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_rewritten_options_path, "http://www.uri.com:80"); |
| 1139 |
|
| 1140 |
static int s_test_http_forwarding_proxy_uri_rewrite_options_star(struct aws_allocator *allocator, void *ctx) { |
| 1141 |
(void)ctx; |
| 1142 |
|
| 1143 |
ASSERT_SUCCESS(s_do_request_rewrite_test( |
| 1144 |
allocator, s_options_request_method, s_options_star_path, s_rewrite_host, s_expected_rewritten_options_path)); |
| 1145 |
|
| 1146 |
return AWS_OP_SUCCESS; |
| 1147 |
} |
| 1148 |
AWS_TEST_CASE(http_forwarding_proxy_uri_rewrite_options_star, s_test_http_forwarding_proxy_uri_rewrite_options_star); |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
static int s_test_http_proxy_no_proxy_hosts_match(struct aws_allocator *allocator, void *ctx) { |
| 1154 |
(void)ctx; |
| 1155 |
|
| 1156 |
aws_http_connection_set_system_vtable(&s_proxy_connection_system_vtable); |
| 1157 |
aws_http_proxy_system_set_vtable(&s_proxy_table_for_tls); |
| 1158 |
|
| 1159 |
struct aws_http_proxy_options proxy_options = { |
| 1160 |
.connection_type = AWS_HPCT_HTTP_TUNNEL, |
| 1161 |
.host = aws_byte_cursor_from_c_str(s_proxy_host_name), |
| 1162 |
.port = s_proxy_port, |
| 1163 |
.no_proxy_hosts = aws_byte_cursor_from_c_str("aws.amazon.com"), |
| 1164 |
}; |
| 1165 |
|
| 1166 |
struct proxy_tester_options options = { |
| 1167 |
.alloc = allocator, |
| 1168 |
.proxy_options = &proxy_options, |
| 1169 |
.host = aws_byte_cursor_from_c_str(s_host_name), |
| 1170 |
.port = s_port, |
| 1171 |
.test_mode = PTTM_NO_PROXY, |
| 1172 |
.failure_type = PTFT_NONE, |
| 1173 |
}; |
| 1174 |
|
| 1175 |
ASSERT_SUCCESS(proxy_tester_init(&tester, &options)); |
| 1176 |
proxy_tester_wait(&tester, proxy_tester_connection_setup_pred); |
| 1177 |
|
| 1178 |
|
| 1179 |
ASSERT_SUCCESS( |
| 1180 |
proxy_tester_verify_connection_attempt_was_to_target(&tester, aws_byte_cursor_from_c_str(s_host_name), s_port)); |
| 1181 |
|
| 1182 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 1183 |
return AWS_OP_SUCCESS; |
| 1184 |
} |
| 1185 |
AWS_TEST_CASE(http_proxy_no_proxy_hosts_match, s_test_http_proxy_no_proxy_hosts_match); |
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
static int s_test_http_proxy_no_proxy_hosts_no_match(struct aws_allocator *allocator, void *ctx) { |
| 1191 |
(void)ctx; |
| 1192 |
|
| 1193 |
aws_http_connection_set_system_vtable(&s_proxy_connection_system_vtable); |
| 1194 |
aws_http_proxy_system_set_vtable(&s_proxy_table_for_tls); |
| 1195 |
|
| 1196 |
struct aws_http_proxy_options proxy_options = { |
| 1197 |
.connection_type = AWS_HPCT_HTTP_TUNNEL, |
| 1198 |
.host = aws_byte_cursor_from_c_str(s_proxy_host_name), |
| 1199 |
.port = s_proxy_port, |
| 1200 |
.no_proxy_hosts = aws_byte_cursor_from_c_str("example.com"), |
| 1201 |
}; |
| 1202 |
|
| 1203 |
struct proxy_tester_options options = { |
| 1204 |
.alloc = allocator, |
| 1205 |
.proxy_options = &proxy_options, |
| 1206 |
.host = aws_byte_cursor_from_c_str(s_host_name), |
| 1207 |
.port = s_port, |
| 1208 |
.test_mode = PTTM_HTTP_TUNNEL, |
| 1209 |
.failure_type = PTFT_NONE, |
| 1210 |
}; |
| 1211 |
|
| 1212 |
ASSERT_SUCCESS(proxy_tester_init(&tester, &options)); |
| 1213 |
proxy_tester_wait(&tester, proxy_tester_connection_setup_pred); |
| 1214 |
|
| 1215 |
|
| 1216 |
ASSERT_SUCCESS(proxy_tester_verify_connection_attempt_was_to_proxy( |
| 1217 |
&tester, aws_byte_cursor_from_c_str(s_proxy_host_name), s_proxy_port)); |
| 1218 |
|
| 1219 |
ASSERT_SUCCESS(proxy_tester_clean_up(&tester)); |
| 1220 |
return AWS_OP_SUCCESS; |
| 1221 |
} |
| 1222 |
AWS_TEST_CASE(http_proxy_no_proxy_hosts_no_match, s_test_http_proxy_no_proxy_hosts_no_match); |