| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/connection.h> |
| 7 |
#include <aws/http/proxy.h> |
| 8 |
|
| 9 |
#include <aws/common/environment.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
#include <aws/io/uri.h> |
| 12 |
|
| 13 |
#include <aws/http/private/no_proxy.h> |
| 14 |
#include <aws/testing/aws_test_harness.h> |
| 15 |
#include <aws/testing/io_testing_channel.h> |
| 16 |
|
| 17 |
static int s_test_no_proxy_helper( |
| 18 |
struct aws_allocator *allocator, |
| 19 |
const char *host, |
| 20 |
const char *no_proxy_value, |
| 21 |
bool expected_bypass) { |
| 22 |
|
| 23 |
struct aws_string *no_proxy_str = aws_string_new_from_c_str(allocator, no_proxy_value); |
| 24 |
|
| 25 |
|
| 26 |
ASSERT_UINT_EQUALS( |
| 27 |
aws_http_host_matches_no_proxy(allocator, aws_byte_cursor_from_c_str(host), no_proxy_str), expected_bypass); |
| 28 |
|
| 29 |
|
| 30 |
aws_string_destroy(no_proxy_str); |
| 31 |
|
| 32 |
return AWS_OP_SUCCESS; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static int s_test_no_proxy_subdomain_matching(struct aws_allocator *allocator, void *ctx) { |
| 39 |
(void)ctx; |
| 40 |
const char *no_proxy_value = ".example.com"; |
| 41 |
|
| 42 |
aws_http_library_init(allocator); |
| 43 |
|
| 44 |
|
| 45 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "sub.example.com", no_proxy_value, true)); |
| 46 |
|
| 47 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "sub.subexample.com", no_proxy_value, false)); |
| 48 |
|
| 49 |
aws_http_library_clean_up(); |
| 50 |
return AWS_OP_SUCCESS; |
| 51 |
} |
| 52 |
AWS_TEST_CASE(test_no_proxy_subdomain_matching, s_test_no_proxy_subdomain_matching); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static int s_test_no_proxy_wildcard_patterns(struct aws_allocator *allocator, void *ctx) { |
| 58 |
(void)ctx; |
| 59 |
const char *no_proxy_value = "*"; |
| 60 |
|
| 61 |
aws_http_library_init(allocator); |
| 62 |
|
| 63 |
|
| 64 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "any.example.com", no_proxy_value, true)); |
| 65 |
|
| 66 |
aws_http_library_clean_up(); |
| 67 |
return AWS_OP_SUCCESS; |
| 68 |
} |
| 69 |
AWS_TEST_CASE(test_no_proxy_wildcard_patterns, s_test_no_proxy_wildcard_patterns); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static int s_test_no_proxy_case_insensitivity(struct aws_allocator *allocator, void *ctx) { |
| 75 |
(void)ctx; |
| 76 |
const char *no_proxy_value = "example.COM"; |
| 77 |
|
| 78 |
aws_http_library_init(allocator); |
| 79 |
|
| 80 |
|
| 81 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "EXAMPLE.com", no_proxy_value, true)); |
| 82 |
|
| 83 |
aws_http_library_clean_up(); |
| 84 |
return AWS_OP_SUCCESS; |
| 85 |
} |
| 86 |
AWS_TEST_CASE(test_no_proxy_case_insensitivity, s_test_no_proxy_case_insensitivity); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static int s_test_no_proxy_ipv6_address(struct aws_allocator *allocator, void *ctx) { |
| 92 |
(void)ctx; |
| 93 |
|
| 94 |
const char *no_proxy_value = "2001:db8::1, ::1, [2001:db8::2]"; |
| 95 |
|
| 96 |
aws_http_library_init(allocator); |
| 97 |
|
| 98 |
|
| 99 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::1]", no_proxy_value, true)); |
| 100 |
|
| 101 |
|
| 102 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "::1", no_proxy_value, true)); |
| 103 |
|
| 104 |
|
| 105 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::2]", no_proxy_value, false)); |
| 106 |
|
| 107 |
aws_http_library_clean_up(); |
| 108 |
return AWS_OP_SUCCESS; |
| 109 |
} |
| 110 |
AWS_TEST_CASE(test_no_proxy_ipv6_address, s_test_no_proxy_ipv6_address); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static int s_test_no_proxy_multiple_patterns(struct aws_allocator *allocator, void *ctx) { |
| 116 |
(void)ctx; |
| 117 |
|
| 118 |
|
| 119 |
const char *no_proxy_value = ",,foo.bar,example.com,other.net,*,"; |
| 120 |
|
| 121 |
aws_http_library_init(allocator); |
| 122 |
|
| 123 |
|
| 124 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true)); |
| 125 |
|
| 126 |
|
| 127 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "foo.bar", no_proxy_value, true)); |
| 128 |
|
| 129 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "foo.", no_proxy_value, false)); |
| 130 |
|
| 131 |
aws_http_library_clean_up(); |
| 132 |
return AWS_OP_SUCCESS; |
| 133 |
} |
| 134 |
AWS_TEST_CASE(test_no_proxy_multiple_patterns, s_test_no_proxy_multiple_patterns); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static int s_test_no_proxy_whitespace_handling(struct aws_allocator *allocator, void *ctx) { |
| 140 |
(void)ctx; |
| 141 |
const char *no_proxy_value = " example.com , foo.bar "; |
| 142 |
|
| 143 |
aws_http_library_init(allocator); |
| 144 |
|
| 145 |
|
| 146 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true)); |
| 147 |
|
| 148 |
|
| 149 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "foo.bar", no_proxy_value, true)); |
| 150 |
|
| 151 |
aws_http_library_clean_up(); |
| 152 |
return AWS_OP_SUCCESS; |
| 153 |
} |
| 154 |
AWS_TEST_CASE(test_no_proxy_whitespace_handling, s_test_no_proxy_whitespace_handling); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static int s_test_no_proxy_ip_address(struct aws_allocator *allocator, void *ctx) { |
| 160 |
(void)ctx; |
| 161 |
const char *no_proxy_value = "192.168.1.1,10.0.0.0"; |
| 162 |
|
| 163 |
aws_http_library_init(allocator); |
| 164 |
|
| 165 |
|
| 166 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", no_proxy_value, true)); |
| 167 |
|
| 168 |
|
| 169 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.2.1", no_proxy_value, false)); |
| 170 |
|
| 171 |
aws_http_library_clean_up(); |
| 172 |
return AWS_OP_SUCCESS; |
| 173 |
} |
| 174 |
AWS_TEST_CASE(test_no_proxy_ip_address, s_test_no_proxy_ip_address); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
static int s_test_no_proxy_port_specific(struct aws_allocator *allocator, void *ctx) { |
| 184 |
(void)ctx; |
| 185 |
const char *no_proxy_value = "example.com:8080"; |
| 186 |
|
| 187 |
aws_http_library_init(allocator); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true)); |
| 193 |
|
| 194 |
aws_http_library_clean_up(); |
| 195 |
return AWS_OP_SUCCESS; |
| 196 |
} |
| 197 |
AWS_TEST_CASE(test_no_proxy_port_specific, s_test_no_proxy_port_specific); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static int s_test_no_proxy_cidr_notation(struct aws_allocator *allocator, void *ctx) { |
| 206 |
(void)ctx; |
| 207 |
const char *no_proxy_value_16 = "192.168.0.0/16"; |
| 208 |
const char *no_proxy_value_24 = "192.168.5.0/24"; |
| 209 |
|
| 210 |
aws_http_library_init(allocator); |
| 211 |
|
| 212 |
|
| 213 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.5.10", no_proxy_value_16, true)); |
| 214 |
|
| 215 |
|
| 216 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "10.0.5.10", no_proxy_value_16, false)); |
| 217 |
|
| 218 |
|
| 219 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.5.10", no_proxy_value_24, true)); |
| 220 |
|
| 221 |
|
| 222 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.6.10", no_proxy_value_24, false)); |
| 223 |
|
| 224 |
aws_http_library_clean_up(); |
| 225 |
return AWS_OP_SUCCESS; |
| 226 |
} |
| 227 |
AWS_TEST_CASE(test_no_proxy_cidr_notation, s_test_no_proxy_cidr_notation); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static int s_test_no_proxy_ipv6_cidr_notation(struct aws_allocator *allocator, void *ctx) { |
| 236 |
(void)ctx; |
| 237 |
const char *no_proxy_value_32 = "2001:db8::/32"; |
| 238 |
const char *no_proxy_value_64 = "2001:db8:1:2::/64"; |
| 239 |
|
| 240 |
aws_http_library_init(allocator); |
| 241 |
|
| 242 |
|
| 243 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8:1:2::3]", no_proxy_value_32, true)); |
| 244 |
|
| 245 |
|
| 246 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db9:1:2::3]", no_proxy_value_32, false)); |
| 247 |
|
| 248 |
|
| 249 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8:1:2::3]", no_proxy_value_64, true)); |
| 250 |
|
| 251 |
|
| 252 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8:1:3::3]", no_proxy_value_64, false)); |
| 253 |
|
| 254 |
aws_http_library_clean_up(); |
| 255 |
return AWS_OP_SUCCESS; |
| 256 |
} |
| 257 |
AWS_TEST_CASE(test_no_proxy_ipv6_cidr_notation, s_test_no_proxy_ipv6_cidr_notation); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static int s_test_no_proxy_invalid_patterns(struct aws_allocator *allocator, void *ctx) { |
| 266 |
(void)ctx; |
| 267 |
|
| 268 |
aws_http_library_init(allocator); |
| 269 |
|
| 270 |
|
| 271 |
const char *no_proxy_value = "example.com,999.999.999.999,192.168.1.3/33,192.168.b.c," |
| 272 |
"2001:xyz::bad:ipv6,2001:db8::/129,not:a:valid:ip/64," |
| 273 |
"[malformed],192.168.1.2," |
| 274 |
"192.168.1.1/99999999999999999," |
| 275 |
"192.168.1.1/9/9/9," |
| 276 |
"2001:db8::/999999"; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.2", no_proxy_value, true)); |
| 282 |
|
| 283 |
|
| 284 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true)); |
| 285 |
|
| 286 |
|
| 287 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", no_proxy_value, false)); |
| 288 |
|
| 289 |
|
| 290 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "999.999.999.999", no_proxy_value, true)); |
| 291 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "999.999.999.999.999", no_proxy_value, true)); |
| 292 |
|
| 293 |
|
| 294 |
const char *invalid_only = "999.999.999.999,192.168.1.1/33,not:an:ip:addr,2001:xyz::bad"; |
| 295 |
|
| 296 |
|
| 297 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", invalid_only, false)); |
| 298 |
|
| 299 |
|
| 300 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "", invalid_only, false)); |
| 301 |
|
| 302 |
|
| 303 |
const char *malformed_cidr = "192.168.1.1/abcdef,2001:db8::/xyz"; |
| 304 |
|
| 305 |
|
| 306 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "2001:db8::", malformed_cidr, false)); |
| 307 |
|
| 308 |
|
| 309 |
char large_value[200]; |
| 310 |
memset(large_value, 'x', sizeof(large_value) - 1); |
| 311 |
large_value[sizeof(large_value) - 1] = '\0'; |
| 312 |
|
| 313 |
char large_cidr[256]; |
| 314 |
snprintf(large_cidr, sizeof(large_cidr), "192.168.1.1/%s", large_value); |
| 315 |
|
| 316 |
|
| 317 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", large_cidr, false)); |
| 318 |
|
| 319 |
aws_http_library_clean_up(); |
| 320 |
return AWS_OP_SUCCESS; |
| 321 |
} |
| 322 |
AWS_TEST_CASE(test_no_proxy_invalid_patterns, s_test_no_proxy_invalid_patterns); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
static int s_test_no_proxy_invalid_host_inputs(struct aws_allocator *allocator, void *ctx) { |
| 332 |
(void)ctx; |
| 333 |
|
| 334 |
aws_http_library_init(allocator); |
| 335 |
|
| 336 |
|
| 337 |
const char *no_proxy_value = "example.com,192.168.1.0/24,2001:db8::/32"; |
| 338 |
|
| 339 |
|
| 340 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "999.999.999.999", no_proxy_value, false)); |
| 341 |
|
| 342 |
|
| 343 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1", no_proxy_value, false)); |
| 344 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168..1", no_proxy_value, false)); |
| 345 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.", no_proxy_value, false)); |
| 346 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, ".192.168.1", no_proxy_value, false)); |
| 347 |
|
| 348 |
|
| 349 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::xyz]", no_proxy_value, false)); |
| 350 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::]:", no_proxy_value, false)); |
| 351 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "2001:db8:::", no_proxy_value, false)); |
| 352 |
|
| 353 |
|
| 354 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::1", no_proxy_value, false)); |
| 355 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "2001:db8::1]", no_proxy_value, false)); |
| 356 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[[2001:db8::1]]", no_proxy_value, false)); |
| 357 |
|
| 358 |
|
| 359 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "", no_proxy_value, false)); |
| 360 |
|
| 361 |
|
| 362 |
char long_host[1024]; |
| 363 |
memset(long_host, 'a', sizeof(long_host) - 1); |
| 364 |
long_host[sizeof(long_host) - 1] = '\0'; |
| 365 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, long_host, no_proxy_value, false)); |
| 366 |
|
| 367 |
|
| 368 |
char long_ipv4[150] = "192.168.1.1"; |
| 369 |
for (int i = 0; i < 130; i++) { |
| 370 |
long_ipv4[11 + i] = '9'; |
| 371 |
} |
| 372 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, long_ipv4, no_proxy_value, false)); |
| 373 |
|
| 374 |
|
| 375 |
char long_ipv6[150] = "[2001:db8::1"; |
| 376 |
for (int i = 0; i < 130; i++) { |
| 377 |
long_ipv6[11 + i] = '1'; |
| 378 |
} |
| 379 |
long_ipv6[141] = ']'; |
| 380 |
long_ipv6[142] = '\0'; |
| 381 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, long_ipv6, no_proxy_value, false)); |
| 382 |
|
| 383 |
aws_http_library_clean_up(); |
| 384 |
return AWS_OP_SUCCESS; |
| 385 |
} |
| 386 |
AWS_TEST_CASE(test_no_proxy_invalid_host_inputs, s_test_no_proxy_invalid_host_inputs); |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
static int s_test_no_proxy_empty(struct aws_allocator *allocator, void *ctx) { |
| 395 |
(void)ctx; |
| 396 |
|
| 397 |
aws_http_library_init(allocator); |
| 398 |
|
| 399 |
|
| 400 |
const char *empty_no_proxy_value = ""; |
| 401 |
|
| 402 |
|
| 403 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", empty_no_proxy_value, false)); |
| 404 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", empty_no_proxy_value, false)); |
| 405 |
ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::1]", empty_no_proxy_value, false)); |
| 406 |
|
| 407 |
aws_http_library_clean_up(); |
| 408 |
return AWS_OP_SUCCESS; |
| 409 |
} |
| 410 |
AWS_TEST_CASE(test_no_proxy_empty, s_test_no_proxy_empty); |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static int s_test_no_proxy_direct_parameter(struct aws_allocator *allocator, void *ctx) { |
| 418 |
(void)ctx; |
| 419 |
|
| 420 |
aws_http_library_init(allocator); |
| 421 |
|
| 422 |
|
| 423 |
const char *direct_no_proxy_value = "direct.example.com"; |
| 424 |
const char *wildcard_no_proxy_value = "*"; |
| 425 |
|
| 426 |
|
| 427 |
ASSERT_TRUE(aws_http_host_matches_no_proxy( |
| 428 |
allocator, |
| 429 |
aws_byte_cursor_from_c_str("direct.example.com"), |
| 430 |
aws_string_new_from_c_str(allocator, direct_no_proxy_value))); |
| 431 |
|
| 432 |
ASSERT_FALSE(aws_http_host_matches_no_proxy( |
| 433 |
allocator, |
| 434 |
aws_byte_cursor_from_c_str("other.example.com"), |
| 435 |
aws_string_new_from_c_str(allocator, direct_no_proxy_value))); |
| 436 |
|
| 437 |
|
| 438 |
ASSERT_TRUE(aws_http_host_matches_no_proxy( |
| 439 |
allocator, |
| 440 |
aws_byte_cursor_from_c_str("any.host.com"), |
| 441 |
aws_string_new_from_c_str(allocator, wildcard_no_proxy_value))); |
| 442 |
|
| 443 |
aws_http_library_clean_up(); |
| 444 |
return AWS_OP_SUCCESS; |
| 445 |
} |
| 446 |
AWS_TEST_CASE(test_no_proxy_direct_parameter, s_test_no_proxy_direct_parameter); |