| #include "../../unity/unity.h" |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <unistd.h> |
| #include <sys/types.h> |
| #include <sys/wait.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <stdbool.h> |
| #include <locale.h> |
|
|
| |
| |
| static char* tfu_read_all(int fd, size_t *len_out) { |
| size_t cap = 4096; |
| size_t len = 0; |
| char *buf = (char*)malloc(cap + 1); |
| if (!buf) return NULL; |
|
|
| for (;;) { |
| if (len == cap) { |
| size_t new_cap = cap * 2; |
| char *nb = (char*)realloc(buf, new_cap + 1); |
| if (!nb) { |
| free(buf); |
| return NULL; |
| } |
| buf = nb; |
| cap = new_cap; |
| } |
| ssize_t r = read(fd, buf + len, cap - len); |
| if (r > 0) { |
| len += (size_t)r; |
| continue; |
| } else if (r == 0) { |
| break; |
| } else { |
| if (errno == EINTR) continue; |
| free(buf); |
| return NULL; |
| } |
| } |
| buf[len] = '\0'; |
| if (len_out) *len_out = len; |
| return buf; |
| } |
|
|
| typedef struct { |
| char *out; |
| size_t out_len; |
| char *err; |
| size_t err_len; |
| int exit_code; |
| int term_sig; |
| } tfu_capture_result; |
|
|
| |
| |
| static bool tfu_run_usage_capture(int status, tfu_capture_result *res) { |
| int out_pipe[2] = {-1, -1}; |
| int err_pipe[2] = {-1, -1}; |
| if (pipe(out_pipe) != 0) return false; |
| if (pipe(err_pipe) != 0) { |
| close(out_pipe[0]); close(out_pipe[1]); |
| return false; |
| } |
|
|
| fflush(stdout); |
| fflush(stderr); |
| pid_t pid = fork(); |
| if (pid < 0) { |
| close(out_pipe[0]); close(out_pipe[1]); |
| close(err_pipe[0]); close(err_pipe[1]); |
| return false; |
| } |
|
|
| if (pid == 0) { |
| |
| |
| setenv("LC_ALL", "C", 1); |
| setlocale(LC_ALL, ""); |
|
|
| |
| if (dup2(out_pipe[1], STDOUT_FILENO) < 0) _exit(127); |
| if (dup2(err_pipe[1], STDERR_FILENO) < 0) _exit(127); |
|
|
| |
| close(out_pipe[0]); close(out_pipe[1]); |
| close(err_pipe[0]); close(err_pipe[1]); |
|
|
| |
| usage(status); |
|
|
| |
| _exit(127); |
| } |
|
|
| |
| close(out_pipe[1]); |
| close(err_pipe[1]); |
|
|
| tfu_capture_result tmp = {0}; |
| tmp.out = tfu_read_all(out_pipe[0], &tmp.out_len); |
| tmp.err = tfu_read_all(err_pipe[0], &tmp.err_len); |
| close(out_pipe[0]); |
| close(err_pipe[0]); |
|
|
| int wstatus = 0; |
| pid_t w = -1; |
| do { |
| w = waitpid(pid, &wstatus, 0); |
| } while (w < 0 && errno == EINTR); |
|
|
| if (w < 0) { |
| free(tmp.out); |
| free(tmp.err); |
| return false; |
| } |
|
|
| if (WIFEXITED(wstatus)) { |
| tmp.exit_code = WEXITSTATUS(wstatus); |
| tmp.term_sig = 0; |
| } else if (WIFSIGNALED(wstatus)) { |
| tmp.term_sig = WTERMSIG(wstatus); |
| tmp.exit_code = -1; |
| } else { |
| tmp.term_sig = -1; |
| tmp.exit_code = -1; |
| } |
|
|
| *res = tmp; |
| return true; |
| } |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| static void tfu_free_capture(tfu_capture_result *res) { |
| if (!res) return; |
| free(res->out); |
| free(res->err); |
| res->out = NULL; |
| res->err = NULL; |
| res->out_len = res->err_len = 0; |
| } |
|
|
| |
| void test_usage_success_prints_help_and_exits_zero(void) { |
| tfu_capture_result r = {0}; |
| TEST_ASSERT_TRUE_MESSAGE(tfu_run_usage_capture(EXIT_SUCCESS, &r), "Failed to run usage in child"); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, r.term_sig, "Child terminated by signal"); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, r.exit_code, "Exit code mismatch for success usage"); |
|
|
| |
| TEST_ASSERT_NOT_NULL(r.out); |
| TEST_ASSERT_TRUE_MESSAGE(r.out_len > 0, "Expected stdout to be non-empty"); |
| TEST_ASSERT_NOT_NULL(r.err); |
| TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, r.err_len, "Expected stderr to be empty on success"); |
|
|
| |
| TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "Usage:"), "Missing 'Usage:' line"); |
| TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "-d, --delimiters"), "Missing -d, --delimiters"); |
| TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "-s, --serial"), "Missing -s, --serial"); |
| TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "-z, --zero-terminated"), "Missing -z, --zero-terminated"); |
|
|
| tfu_free_capture(&r); |
| } |
|
|
| |
| void test_usage_failure_emits_try_help_and_exits_with_status(void) { |
| const int code = 42; |
| tfu_capture_result r = {0}; |
| TEST_ASSERT_TRUE_MESSAGE(tfu_run_usage_capture(code, &r), "Failed to run usage in child"); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, r.term_sig, "Child terminated by signal"); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(code, r.exit_code, "Exit code mismatch for failure usage"); |
|
|
| |
| TEST_ASSERT_NOT_NULL(r.err); |
| TEST_ASSERT_TRUE_MESSAGE(r.err_len > 0, "Expected stderr to be non-empty on failure usage"); |
| TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.err, "--help"), "Expected '--help' hint in stderr"); |
|
|
| |
| TEST_ASSERT_NOT_NULL(r.out); |
| TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, r.out_len, "Expected stdout to be empty on failure usage"); |
|
|
| tfu_free_capture(&r); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_usage_success_prints_help_and_exits_zero); |
| RUN_TEST(test_usage_failure_emits_try_help_and_exits_with_status); |
| return UNITY_END(); |
| } |