diff options
| author | Mosder <xmosder12@gmail.com> | 2026-08-21 14:41:26 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-08-21 14:41:26 +0200 |
| commit | 06954da1e847072f9e95df0f183d9327a0f05b5b (patch) | |
| tree | bd10a6e5e960dc73d11de0b91ae73d8b6fb1a1a0 /tests/safe_tests.c | |
| parent | b6b7a01987dca604a161c8c73a240ec4430baed0 (diff) | |
feat: added some for safe wrappers + added ability to capture and test whether output is what was expected
Diffstat (limited to 'tests/safe_tests.c')
| -rw-r--r-- | tests/safe_tests.c | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/tests/safe_tests.c b/tests/safe_tests.c index 1d48a48..3d46a37 100644 --- a/tests/safe_tests.c +++ b/tests/safe_tests.c @@ -3,6 +3,7 @@ #include <limits.h> #include <stdlib.h> +#include <unistd.h> Test malloc_exit() { safe_malloc(ULLONG_MAX); @@ -17,11 +18,6 @@ Test test_safe_malloc() { unsuppress_outputs(); } -Test realloc_exit() { - void *p = NULL; - safe_realloc(p, ULLONG_MAX); -} - Test calloc_exit() { safe_calloc(ULLONG_MAX, ULLONG_MAX); } @@ -35,6 +31,11 @@ Test test_safe_calloc() { unsuppress_outputs(); } +Test realloc_exit() { + void *p = NULL; + safe_realloc(p, ULLONG_MAX); +} + Test test_safe_realloc() { void *p = NULL; p = safe_realloc(p, 1024); @@ -48,9 +49,65 @@ Test test_safe_realloc() { unsuppress_outputs(); } +Test fopen_exit() { + safe_fopen("/non-existing-file-name", "r"); +} + +Test test_safe_fopen() { + FILE *fp = safe_fopen(__FILE__, "r"); + test_assert(fp != NULL, "Failed to fopen current file"); + + suppress_output(stderr); + test_assert_exit(fopen_exit, EXIT_FAILURE); + unsuppress_outputs(); +} + +Test test_safe_tmpfile() { + FILE *fp = safe_tmpfile(); + test_assert(fp != NULL, "Failed to create a tmpfile"); +} + +Test dup_exit() { + safe_dup(-1); +} + +Test test_safe_dup() { + int fd = safe_dup(STDOUT_FILENO); + test_assert(fd != -1, "Failed to dup STDOUT_FILENO"); + close(fd); + + suppress_output(stderr); + test_assert_exit(dup_exit, EXIT_FAILURE); + unsuppress_outputs(); +} + +Test dup2_exit() { + safe_dup2(-1, STDOUT_FILENO); +} + +Test test_safe_dup2() { + int res = dup2(STDOUT_FILENO, STDOUT_FILENO); + test_assert(res != -1, "Failed to dup STDOUT_FILENO 2 itself"); + + int fd = dup(STDOUT_FILENO); + res = dup2(STDERR_FILENO, STDOUT_FILENO); + test_assert(res != -1, "Failed to dup STDERR_FILENO 2 STDOUT_FILENO"); + res = dup2(fd, STDOUT_FILENO); + test_assert(res != -1, "Failed to restore STDOUT_FILENO"); + close(fd); + + suppress_output(stderr); + test_assert_exit(dup2_exit, EXIT_FAILURE); + unsuppress_outputs(); +} + void safe_tests(Tester *tester) { TestGroup *group = add_test_group(tester, "moslib/safe.h"); add_test(group, test_safe_malloc); add_test(group, test_safe_calloc); add_test(group, test_safe_realloc); + add_test(group, test_safe_fopen); + add_test(group, test_safe_tmpfile); + add_test(group, test_safe_dup); + add_test(group, test_safe_dup2); } |
