libicmp/tests/error/test_error.c
2026-01-25 15:12:53 +01:00

101 lines
2.4 KiB
C

#include <criterion/criterion.h>
#include <string.h>
#include "internal/icmp_internal.h"
#include "icmp.h"
/* Test 1: set_error sets code and message */
Test(error, set_error_basic)
{
struct icmp_handle h = {0};
icmp_set_error(&h, ICMP_ERR_SOCKET, "Test error message");
cr_assert_eq(h.last_error, ICMP_ERR_SOCKET);
cr_assert_str_eq(h.error_msg, "Test error message");
}
/* Test 3: set_error with NULL message clears error_msg */
Test(error, set_error_null_message)
{
struct icmp_handle h = {0};
strcpy(h.error_msg, "Old error");
icmp_set_error(&h, ICMP_ERR_SEND, NULL);
cr_assert_eq(h.last_error, ICMP_ERR_SEND);
cr_assert_eq(h.error_msg[0], '\0');
}
/* Test 4: strerror returns message from handle */
Test(error, strerror_returns_message)
{
struct icmp_handle h = {0};
h.last_error = ICMP_ERR_PERMISSION;
strcpy(h.error_msg, "Permission denied");
const char *msg = icmp_strerror((icmp_handle_t *)&h);
cr_assert_str_eq(msg, "Permission denied");
}
/* Test 5: strerror with NULL handle */
Test(error, strerror_null_handle)
{
const char *msg = icmp_strerror(NULL);
cr_assert_str_eq(msg, "Invalid handle");
}
/* Test 6: strerror with ICMP_OK */
Test(error, strerror_no_error)
{
struct icmp_handle h = {0};
h.last_error = ICMP_OK;
const char *msg = icmp_strerror((icmp_handle_t *)&h);
cr_assert_str_eq(msg, "No error");
}
/* Test 7: should_retry returns 1 for ICMP_ERR_EAGAIN */
Test(error, should_retry_with_eagain)
{
struct icmp_handle h = {0};
h.last_error = ICMP_ERR_EAGAIN;
strcpy(h.error_msg, "Send would block");
int should_retry = icmp_should_retry((icmp_handle_t *)&h);
cr_assert_eq(should_retry, 1, "Should return 1 for ICMP_ERR_EAGAIN");
}
/* Test 8: should_retry returns 0 for other errors */
Test(error, should_retry_with_other_error)
{
struct icmp_handle h = {0};
h.last_error = ICMP_ERR_SEND;
strcpy(h.error_msg, "sendto() failed");
int should_retry = icmp_should_retry((icmp_handle_t *)&h);
cr_assert_eq(should_retry, 0, "Should return 0 for ICMP_ERR_SEND");
}
/* Test 9: should_retry returns 0 for ICMP_OK */
Test(error, should_retry_no_error)
{
struct icmp_handle h = {0};
h.last_error = ICMP_OK;
int should_retry = icmp_should_retry((icmp_handle_t *)&h);
cr_assert_eq(should_retry, 0, "Should return 0 when no error");
}
/* Test 10: should_retry with NULL handle returns 0 */
Test(error, should_retry_null_handle)
{
int should_retry = icmp_should_retry(NULL);
cr_assert_eq(should_retry, 0, "Should return 0 for NULL handle");
}