| #include "../../unity/unity.h" |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <unistd.h> |
| #include <sys/wait.h> |
| #include <errno.h> |
| #include <locale.h> |
|
|
| |
| void setUp(void) { |
| |
| setenv("LC_ALL", "C", 1); |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| |
| static char *read_all_from_fd(int fd, size_t *len_out) { |
| size_t cap = 4096; |
| size_t len = 0; |
| char *buf = (char *)malloc(cap); |
| if (!buf) return NULL; |
|
|
| while (1) { |
| if (len + 2048 > cap) { |
| size_t new_cap = cap * 2; |
| char *nb = (char *)realloc(buf, new_cap); |
| if (!nb) { |
| free(buf); |
| return NULL; |
| } |
| buf = nb; |
| cap = new_cap; |
| } |
| ssize_t r = read(fd, buf + len, cap - len - 1); |
| if (r < 0) { |
| if (errno == EINTR) continue; |
| free(buf); |
| return NULL; |
| } |
| if (r == 0) break; |
| len += (size_t)r; |
| } |
| buf[len] = '\0'; |
| if (len_out) *len_out = len; |
| return buf; |
| } |
|
|
| |
| |
| |
| static int run_usage_and_capture(int status, |
| int *exit_code, |
| char **out_stdout, size_t *out_stdout_len, |
| char **out_stderr, size_t *out_stderr_len) { |
| int out_pipe[2] = {-1, -1}; |
| int err_pipe[2] = {-1, -1}; |
| if (pipe(out_pipe) != 0) return -1; |
| if (pipe(err_pipe) != 0) { |
| close(out_pipe[0]); close(out_pipe[1]); |
| return -1; |
| } |
|
|
| 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 -1; |
| } |
|
|
| if (pid == 0) { |
| |
| |
| setenv("LC_ALL", "C", 1); |
| setlocale(LC_ALL, "C"); |
|
|
| |
| set_program_name("nl"); |
|
|
| |
| dup2(out_pipe[1], STDOUT_FILENO); |
| dup2(err_pipe[1], STDERR_FILENO); |
|
|
| |
| close(out_pipe[0]); close(out_pipe[1]); |
| close(err_pipe[0]); close(err_pipe[1]); |
|
|
| |
| usage(status); |
|
|
| |
| _exit(255); |
| } |
|
|
| |
| close(out_pipe[1]); |
| close(err_pipe[1]); |
|
|
| size_t so_len = 0, se_len = 0; |
| char *so = read_all_from_fd(out_pipe[0], &so_len); |
| char *se = read_all_from_fd(err_pipe[0], &se_len); |
|
|
| close(out_pipe[0]); |
| close(err_pipe[0]); |
|
|
| int wstatus = 0; |
| int wait_ok = 0; |
| do { |
| wait_ok = waitpid(pid, &wstatus, 0); |
| } while (wait_ok < 0 && errno == EINTR); |
|
|
| if (wait_ok < 0) { |
| free(so); |
| free(se); |
| return -1; |
| } |
|
|
| int code = -1; |
| if (WIFEXITED(wstatus)) { |
| code = WEXITSTATUS(wstatus); |
| } else if (WIFSIGNALED(wstatus)) { |
| |
| code = -WTERMSIG(wstatus); |
| } |
|
|
| if (exit_code) *exit_code = code; |
| if (out_stdout) *out_stdout = so; else free(so); |
| if (out_stdout_len) *out_stdout_len = so_len; |
| if (out_stderr) *out_stderr = se; else free(se); |
| if (out_stderr_len) *out_stderr_len = se_len; |
|
|
| return 0; |
| } |
|
|
| |
| static int contains_substr(const char *haystack, const char *needle) { |
| return haystack && needle && strstr(haystack, needle) != NULL; |
| } |
|
|
| |
|
|
| void test_usage_success_prints_full_usage_to_stdout_and_exits_zero(void) { |
| int exit_code = -1; |
| char *out = NULL; |
| char *err = NULL; |
| size_t out_len = 0, err_len = 0; |
|
|
| int rc = run_usage_and_capture(EXIT_SUCCESS, &exit_code, &out, &out_len, &err, &err_len); |
| TEST_ASSERT_EQUAL_INT(0, rc); |
| TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, exit_code); |
|
|
| |
| TEST_ASSERT_NOT_NULL(out); |
| TEST_ASSERT_TRUE(out_len > 0); |
| TEST_ASSERT_TRUE_MESSAGE(contains_substr(out, "Usage: nl [OPTION]... [FILE]..."), |
| "stdout should contain the usage synopsis line"); |
| TEST_ASSERT_TRUE_MESSAGE(contains_substr(out, "Default options are:"), |
| "stdout should contain the default options section"); |
|
|
| |
| if (err && err_len > 0) { |
| |
| size_t i; |
| int non_ws = 0; |
| for (i = 0; i < err_len; i++) { |
| if (!(err[i] == ' ' || err[i] == '\t' || err[i] == '\n' || err[i] == '\r' || err[i] == '\f' || err[i] == '\v')) |
| { non_ws = 1; break; } |
| } |
| TEST_ASSERT_FALSE_MESSAGE(non_ws, "stderr should be empty for EXIT_SUCCESS"); |
| } |
|
|
| free(out); |
| free(err); |
| } |
|
|
| void test_usage_failure_prints_try_help_to_stderr_and_exits_status(void) { |
| int exit_code = -1; |
| char *out = NULL; |
| char *err = NULL; |
| size_t out_len = 0, err_len = 0; |
|
|
| int nonzero_status = 2; |
| int rc = run_usage_and_capture(nonzero_status, &exit_code, &out, &out_len, &err, &err_len); |
| TEST_ASSERT_EQUAL_INT(0, rc); |
| TEST_ASSERT_EQUAL_INT(nonzero_status, exit_code); |
|
|
| |
| if (out && out_len > 0) { |
| size_t i; |
| int non_ws = 0; |
| for (i = 0; i < out_len; i++) { |
| if (!(out[i] == ' ' || out[i] == '\t' || out[i] == '\n' || out[i] == '\r' || out[i] == '\f' || out[i] == '\v')) |
| { non_ws = 1; break; } |
| } |
| TEST_ASSERT_FALSE_MESSAGE(non_ws, "stdout should be empty for non-success status"); |
| } |
|
|
| |
| TEST_ASSERT_NOT_NULL_MESSAGE(err, "stderr capture failed"); |
| TEST_ASSERT_TRUE_MESSAGE(err_len > 0, "stderr should not be empty for non-success status"); |
| TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, "nl --help"), |
| "stderr should mention 'nl --help'"); |
| TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, "Try '"), |
| "stderr should begin with 'Try ...' guidance"); |
| TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, "more information."), |
| "stderr should contain 'more information.' guidance"); |
|
|
| free(out); |
| free(err); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_usage_success_prints_full_usage_to_stdout_and_exits_zero); |
| RUN_TEST(test_usage_failure_prints_try_help_to_stderr_and_exits_status); |
| return UNITY_END(); |
| } |