| #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 <fcntl.h> |
| #include <errno.h> |
| #include <locale.h> |
|
|
| |
| extern void usage (int status); |
| extern char const *program_name; |
|
|
| |
| |
| |
| static char *read_all_from_fd (int fd, size_t *out_len) |
| { |
| size_t cap = 4096; |
| size_t len = 0; |
| char *buf = (char *)malloc(cap + 1); |
| if (!buf) { |
| if (out_len) *out_len = 0; |
| return NULL; |
| } |
|
|
| for (;;) { |
| if (len == cap) { |
| size_t ncap = cap * 2; |
| char *nbuf = (char *)realloc(buf, ncap + 1); |
| if (!nbuf) { |
| free(buf); |
| if (out_len) *out_len = 0; |
| return NULL; |
| } |
| buf = nbuf; |
| cap = ncap; |
| } |
| ssize_t n = read(fd, buf + len, cap - len); |
| if (n > 0) { |
| len += (size_t)n; |
| continue; |
| } else if (n == 0) { |
| break; |
| } else { |
| if (errno == EINTR) |
| continue; |
| free(buf); |
| if (out_len) *out_len = 0; |
| return NULL; |
| } |
| } |
| buf[len] = '\0'; |
| if (out_len) *out_len = len; |
| return buf; |
| } |
|
|
| |
| |
| |
| |
| |
| static int spawn_usage_and_capture (const char *progname_override, int status, |
| char **out_stdout, size_t *out_stdout_len, |
| char **out_stderr, size_t *out_stderr_len, |
| int *out_exit_code) |
| { |
| 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"); |
|
|
| if (progname_override && *progname_override) { |
| |
| program_name = progname_override; |
| } else { |
| |
| program_name = "link"; |
| } |
|
|
| |
| close(out_pipe[0]); |
| close(err_pipe[0]); |
|
|
| if (dup2(out_pipe[1], STDOUT_FILENO) < 0) _exit(127); |
| if (dup2(err_pipe[1], STDERR_FILENO) < 0) _exit(127); |
|
|
| |
| close(out_pipe[1]); |
| close(err_pipe[1]); |
|
|
| |
| usage(status); |
|
|
| |
| _exit(255); |
| } |
|
|
| |
| close(out_pipe[1]); |
| close(err_pipe[1]); |
|
|
| char *cap_out = read_all_from_fd(out_pipe[0], out_stdout_len); |
| char *cap_err = read_all_from_fd(err_pipe[0], out_stderr_len); |
|
|
| close(out_pipe[0]); |
| close(err_pipe[0]); |
|
|
| int wstatus = 0; |
| int rc; |
| do { |
| rc = waitpid(pid, &wstatus, 0); |
| } while (rc < 0 && errno == EINTR); |
|
|
| int exit_code = -1; |
| if (rc >= 0) { |
| if (WIFEXITED(wstatus)) { |
| exit_code = WEXITSTATUS(wstatus); |
| } else if (WIFSIGNALED(wstatus)) { |
| exit_code = 128 + WTERMSIG(wstatus); |
| } else { |
| exit_code = -1; |
| } |
| } |
|
|
| if (!cap_out) { |
| |
| cap_out = (char *)malloc(1); |
| if (cap_out) cap_out[0] = '\0'; |
| if (out_stdout_len) *out_stdout_len = 0; |
| } |
| if (!cap_err) { |
| cap_err = (char *)malloc(1); |
| if (cap_err) cap_err[0] = '\0'; |
| if (out_stderr_len) *out_stderr_len = 0; |
| } |
|
|
| if (out_stdout) *out_stdout = cap_out; else free(cap_out); |
| if (out_stderr) *out_stderr = cap_err; else free(cap_err); |
| if (out_exit_code) *out_exit_code = exit_code; |
|
|
| return 0; |
| } |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| void test_usage_failure_prints_try_help_and_exits_failure(void) |
| { |
| char *out = NULL, *err = NULL; |
| size_t out_len = 0, err_len = 0; |
| int code = -1; |
|
|
| int rc = spawn_usage_and_capture("link", EXIT_FAILURE, &out, &out_len, &err, &err_len, &code); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Failed to spawn child for usage(EXIT_FAILURE)"); |
|
|
| TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, code); |
| TEST_ASSERT_NOT_NULL(out); |
| TEST_ASSERT_NOT_NULL(err); |
| TEST_ASSERT_EQUAL_UINT64_MESSAGE(0, (uint64_t)out_len, "Expected no stdout on failure"); |
| |
| TEST_ASSERT_TRUE_MESSAGE(strstr(err, "Try 'link --help' for more information.") != NULL, |
| "stderr should contain the try-help hint for link"); |
|
|
| free(out); |
| free(err); |
| } |
|
|
| |
| void test_usage_failure_with_arbitrary_status_propagated(void) |
| { |
| const int arbitrary = 42; |
| char *out = NULL, *err = NULL; |
| size_t out_len = 0, err_len = 0; |
| int code = -1; |
|
|
| int rc = spawn_usage_and_capture("link", arbitrary, &out, &out_len, &err, &err_len, &code); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Failed to spawn child for usage(42)"); |
|
|
| TEST_ASSERT_EQUAL_INT(arbitrary, code); |
| TEST_ASSERT_EQUAL_UINT64_MESSAGE(0, (uint64_t)out_len, "Expected no stdout on failure"); |
| TEST_ASSERT_TRUE_MESSAGE(strstr(err, "Try 'link --help' for more information.") != NULL, |
| "stderr should contain the try-help hint for link"); |
|
|
| free(out); |
| free(err); |
| } |
|
|
| |
| void test_usage_success_prints_usage_and_exits_zero(void) |
| { |
| char *out = NULL, *err = NULL; |
| size_t out_len = 0, err_len = 0; |
| int code = -1; |
|
|
| int rc = spawn_usage_and_capture("link", EXIT_SUCCESS, &out, &out_len, &err, &err_len, &code); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Failed to spawn child for usage(EXIT_SUCCESS)"); |
|
|
| TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, code); |
| TEST_ASSERT_NOT_NULL(out); |
| TEST_ASSERT_NOT_NULL(err); |
| TEST_ASSERT_EQUAL_UINT64_MESSAGE(0, (uint64_t)err_len, "Expected no stderr on success"); |
|
|
| |
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "Usage: link FILE1 FILE2") != NULL, |
| "stdout should contain the main usage line with program name"); |
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "or: link OPTION") != NULL, |
| "stdout should contain the alternative usage line"); |
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "Call the link function to create a link named FILE2") != NULL, |
| "stdout should contain the description line"); |
| |
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "--help") != NULL, |
| "stdout should mention the --help option"); |
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "--version") != NULL, |
| "stdout should mention the --version option"); |
|
|
| free(out); |
| free(err); |
| } |
|
|
| |
| void test_usage_respects_program_name_variable(void) |
| { |
| const char *custom = "linky"; |
| char *out = NULL, *err = NULL; |
| size_t out_len = 0, err_len = 0; |
| int code = -1; |
|
|
| int rc = spawn_usage_and_capture(custom, EXIT_SUCCESS, &out, &out_len, &err, &err_len, &code); |
| TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Failed to spawn child for usage(EXIT_SUCCESS) with custom program_name"); |
|
|
| TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, code); |
| TEST_ASSERT_EQUAL_UINT64_MESSAGE(0, (uint64_t)err_len, "Expected no stderr on success with custom name"); |
|
|
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "Usage: linky FILE1 FILE2") != NULL, |
| "stdout should use custom program_name in main usage line"); |
| TEST_ASSERT_TRUE_MESSAGE(strstr(out, "or: linky OPTION") != NULL, |
| "stdout should use custom program_name in alternative usage line"); |
|
|
| free(out); |
| free(err); |
| } |
|
|
| int main(void) |
| { |
| UNITY_BEGIN(); |
| RUN_TEST(test_usage_failure_prints_try_help_and_exits_failure); |
| RUN_TEST(test_usage_failure_with_arbitrary_status_propagated); |
| RUN_TEST(test_usage_success_prints_usage_and_exits_zero); |
| RUN_TEST(test_usage_respects_program_name_variable); |
| return UNITY_END(); |
| } |