|
6 | 6 | #include <aws/common/string.h> |
7 | 7 | #include <aws/http/request_response.h> |
8 | 8 | #include <aws/http/status_code.h> |
9 | | -#include <aws/testing/aws_test_allocators.h> |
| 9 | +#include <aws/testing/aws_test_harness.h> |
10 | 10 |
|
11 | 11 | #define TEST_CASE(NAME) \ |
12 | 12 | AWS_TEST_CASE(NAME, s_test_##NAME); \ |
@@ -368,78 +368,3 @@ TEST_CASE(message_with_existing_headers) { |
368 | 368 | aws_http_message_release(message); |
369 | 369 | return AWS_OP_SUCCESS; |
370 | 370 | } |
371 | | - |
372 | | -/* Do every operation that involves allocating some memory */ |
373 | | -static int s_message_handles_oom_attempt(struct aws_http_message *request) { |
374 | | - ASSERT_NOT_NULL(request); |
375 | | - |
376 | | - /* Set, and then overwrite, method and path */ |
377 | | - ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("POST"))); |
378 | | - ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/"))); |
379 | | - ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET"))); |
380 | | - ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/chat"))); |
381 | | - |
382 | | - /* Add a lot of headers, enough to force the underlying array-list to expand. |
383 | | - * (just loop through the list above again and again) */ |
384 | | - char name_buf[16]; |
385 | | - char value_buf[16]; |
386 | | - for (size_t i = 0; i < 128; ++i) { |
387 | | - snprintf(name_buf, sizeof(name_buf), "Name-%zu", i); |
388 | | - snprintf(name_buf, sizeof(name_buf), "Value-%zu", i); |
389 | | - struct aws_http_header header = {.name = aws_byte_cursor_from_c_str(name_buf), |
390 | | - .value = aws_byte_cursor_from_c_str(value_buf)}; |
391 | | - ASSERT_SUCCESS(aws_http_message_add_header(request, header)); |
392 | | - } |
393 | | - |
394 | | - /* Overwrite all the headers */ |
395 | | - for (size_t i = 0; i < 128; ++i) { |
396 | | - snprintf(name_buf, sizeof(name_buf), "New-Name-%zu", i); |
397 | | - snprintf(name_buf, sizeof(name_buf), "New-Value-%zu", i); |
398 | | - struct aws_http_header header = {.name = aws_byte_cursor_from_c_str(name_buf), |
399 | | - .value = aws_byte_cursor_from_c_str(value_buf)}; |
400 | | - ASSERT_SUCCESS(aws_http_headers_set(aws_http_message_get_headers(request), header.name, header.value)); |
401 | | - } |
402 | | - |
403 | | - return AWS_OP_SUCCESS; |
404 | | -} |
405 | | - |
406 | | -TEST_CASE(message_handles_oom) { |
407 | | - (void)ctx; |
408 | | - struct aws_allocator timebomb_alloc; |
409 | | - ASSERT_SUCCESS(aws_timebomb_allocator_init(&timebomb_alloc, allocator, SIZE_MAX)); |
410 | | - |
411 | | - bool test_succeeded = false; |
412 | | - size_t allocations_until_failure; |
413 | | - for (allocations_until_failure = 0; allocations_until_failure < 10000; ++allocations_until_failure) { |
414 | | - /* Allow one more allocation each time we loop. */ |
415 | | - aws_timebomb_allocator_reset_countdown(&timebomb_alloc, allocations_until_failure); |
416 | | - |
417 | | - /* Create a request, then do a bunch of stuff with it. */ |
418 | | - struct aws_http_message *request = aws_http_message_new_request(&timebomb_alloc); |
419 | | - int err = 0; |
420 | | - if (request) { |
421 | | - err = s_message_handles_oom_attempt(request); |
422 | | - if (err) { |
423 | | - /* Ensure failure was due to OOM */ |
424 | | - ASSERT_INT_EQUALS(AWS_ERROR_OOM, aws_last_error()); |
425 | | - } else { |
426 | | - test_succeeded = true; |
427 | | - } |
428 | | - |
429 | | - aws_http_message_destroy(request); |
430 | | - } else { |
431 | | - /* Ensure failure was due to OOM */ |
432 | | - ASSERT_INT_EQUALS(AWS_ERROR_OOM, aws_last_error()); |
433 | | - } |
434 | | - |
435 | | - if (test_succeeded) { |
436 | | - break; |
437 | | - } |
438 | | - } |
439 | | - |
440 | | - ASSERT_TRUE(test_succeeded); |
441 | | - ASSERT_TRUE(allocations_until_failure > 2); /* Assert that this did fail a few times */ |
442 | | - |
443 | | - aws_timebomb_allocator_clean_up(&timebomb_alloc); |
444 | | - return AWS_OP_SUCCESS; |
445 | | -} |
0 commit comments