|
| 1 | +#ifndef TEST_HANDLE_H |
| 2 | +#define TEST_HANDLE_H |
| 3 | + |
| 4 | +#include "acquire_handle.h" |
| 5 | +#include <greatest.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +TEST test_handle_initialization(void) { |
| 9 | + struct acquire_handle *h = acquire_handle_init(); |
| 10 | + ASSERT(h != NULL); |
| 11 | + ASSERT_EQ_FMT(-1L, (long)h->total_size, "%ld"); |
| 12 | + ASSERT_EQ_FMT(ACQUIRE_IDLE, h->status, "%d"); |
| 13 | + ASSERT_EQ(ACQUIRE_OK, h->error.code); |
| 14 | + ASSERT_EQ_FMT(0, h->cancel_flag, "%d"); |
| 15 | + ASSERT_EQ(NULL, h->backend_handle); |
| 16 | + acquire_handle_free(h); |
| 17 | + PASS(); |
| 18 | +} |
| 19 | + |
| 20 | +TEST test_handle_set_and_get_error(void) { |
| 21 | + struct acquire_handle *h = acquire_handle_init(); |
| 22 | + const char *test_msg = "A test error occurred"; |
| 23 | + ASSERT(h != NULL); |
| 24 | + |
| 25 | + acquire_handle_set_error(h, ACQUIRE_ERROR_UNKNOWN, "%s", test_msg); |
| 26 | + |
| 27 | + ASSERT_EQ_FMT(ACQUIRE_ERROR, h->status, "%d"); |
| 28 | + ASSERT_EQ_FMT(ACQUIRE_ERROR_UNKNOWN, acquire_handle_get_error_code(h), "%d"); |
| 29 | + ASSERT_STR_EQ(test_msg, acquire_handle_get_error_string(h)); |
| 30 | + |
| 31 | + acquire_handle_free(h); |
| 32 | + PASS(); |
| 33 | +} |
| 34 | + |
| 35 | +TEST test_handle_null_safety(void) { |
| 36 | + /* These functions should not crash when given a NULL handle. */ |
| 37 | + acquire_handle_free(NULL); |
| 38 | + ASSERT_EQ(ACQUIRE_ERROR_INVALID_ARGUMENT, |
| 39 | + acquire_handle_get_error_code(NULL)); |
| 40 | + ASSERT_STR_EQ("Invalid handle provided.", |
| 41 | + acquire_handle_get_error_string(NULL)); |
| 42 | + acquire_handle_set_error(NULL, ACQUIRE_ERROR_UNKNOWN, "test"); |
| 43 | + /* No crash is a pass. */ |
| 44 | + PASS(); |
| 45 | +} |
| 46 | + |
| 47 | +TEST test_handle_set_error_with_formatting(void) { |
| 48 | + struct acquire_handle *h = acquire_handle_init(); |
| 49 | + ASSERT(h != NULL); |
| 50 | + |
| 51 | + acquire_handle_set_error(h, ACQUIRE_ERROR_HTTP_FAILURE, "HTTP status was %d", |
| 52 | + 404); |
| 53 | + |
| 54 | + ASSERT_EQ_FMT(ACQUIRE_ERROR_HTTP_FAILURE, acquire_handle_get_error_code(h), |
| 55 | + "%d"); |
| 56 | + ASSERT_STR_EQ("HTTP status was 404", acquire_handle_get_error_string(h)); |
| 57 | + |
| 58 | + acquire_handle_free(h); |
| 59 | + PASS(); |
| 60 | +} |
| 61 | + |
| 62 | +SUITE(handle_suite) { |
| 63 | + RUN_TEST(test_handle_initialization); |
| 64 | + RUN_TEST(test_handle_set_and_get_error); |
| 65 | + RUN_TEST(test_handle_null_safety); |
| 66 | + RUN_TEST(test_handle_set_error_with_formatting); |
| 67 | +} |
| 68 | + |
| 69 | +#endif /* !TEST_HANDLE_H */ |
0 commit comments