|
|
#include "../../unity/unity.h" |
|
|
|
|
|
#include <stdio.h> |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <unistd.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/wait.h> |
|
|
#include <errno.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
char *output; |
|
|
size_t length; |
|
|
int exited; |
|
|
int exit_code; |
|
|
int signaled; |
|
|
int term_signal; |
|
|
} ChildResult; |
|
|
|
|
|
static void free_child_result(ChildResult *r) { |
|
|
if (!r) return; |
|
|
free(r->output); |
|
|
r->output = NULL; |
|
|
r->length = 0; |
|
|
} |
|
|
|
|
|
|
|
|
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); |
|
|
if (!buf) { |
|
|
return NULL; |
|
|
} |
|
|
for (;;) { |
|
|
if (len + 2048 > cap) { |
|
|
size_t new_cap = cap * 2; |
|
|
char *new_buf = (char *)realloc(buf, new_cap); |
|
|
if (!new_buf) { |
|
|
free(buf); |
|
|
return NULL; |
|
|
} |
|
|
buf = new_buf; |
|
|
cap = new_cap; |
|
|
} |
|
|
ssize_t n = read(fd, buf + len, cap - len - 1); |
|
|
if (n < 0) { |
|
|
if (errno == EINTR) continue; |
|
|
free(buf); |
|
|
return NULL; |
|
|
} |
|
|
if (n == 0) break; |
|
|
len += (size_t)n; |
|
|
} |
|
|
buf[len] = '\0'; |
|
|
if (out_len) *out_len = len; |
|
|
return buf; |
|
|
} |
|
|
|
|
|
|
|
|
static ChildResult run_usage_and_capture(int status, int capture_stdout) { |
|
|
ChildResult res; |
|
|
memset(&res, 0, sizeof(res)); |
|
|
|
|
|
int pipefd[2]; |
|
|
if (pipe(pipefd) != 0) { |
|
|
|
|
|
return res; |
|
|
} |
|
|
|
|
|
fflush(stdout); |
|
|
fflush(stderr); |
|
|
pid_t pid = fork(); |
|
|
if (pid == 0) { |
|
|
|
|
|
|
|
|
if (capture_stdout) { |
|
|
if (dup2(pipefd[1], STDOUT_FILENO) < 0) _exit(127); |
|
|
} else { |
|
|
if (dup2(pipefd[1], STDERR_FILENO) < 0) _exit(127); |
|
|
} |
|
|
close(pipefd[0]); |
|
|
close(pipefd[1]); |
|
|
|
|
|
|
|
|
if (status == EXIT_SUCCESS) { |
|
|
setenv("LC_ALL", "C", 1); |
|
|
|
|
|
extern char const *program_name; |
|
|
program_name = "dirname"; |
|
|
} |
|
|
|
|
|
|
|
|
usage(status); |
|
|
|
|
|
|
|
|
_exit(255); |
|
|
} |
|
|
|
|
|
|
|
|
close(pipefd[1]); |
|
|
|
|
|
|
|
|
res.output = read_all_from_fd(pipefd[0], &res.length); |
|
|
close(pipefd[0]); |
|
|
|
|
|
int wstatus = 0; |
|
|
if (waitpid(pid, &wstatus, 0) >= 0) { |
|
|
if (WIFEXITED(wstatus)) { |
|
|
res.exited = 1; |
|
|
res.exit_code = WEXITSTATUS(wstatus); |
|
|
} else if (WIFSIGNALED(wstatus)) { |
|
|
res.signaled = 1; |
|
|
res.term_signal = WTERMSIG(wstatus); |
|
|
} |
|
|
} |
|
|
return res; |
|
|
} |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void test_usage_nonzero_status_exits_and_writes_stderr(void) { |
|
|
int status_to_test = 2; |
|
|
ChildResult r = run_usage_and_capture(status_to_test, 0); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(r.exited || r.signaled, "Child did not terminate properly"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(!r.signaled, "Child terminated by signal"); |
|
|
TEST_ASSERT_EQUAL_INT(status_to_test, r.exit_code); |
|
|
|
|
|
|
|
|
TEST_ASSERT_NOT_NULL(r.output); |
|
|
TEST_ASSERT_TRUE_MESSAGE(r.length > 0, "Expected some stderr output for nonzero status"); |
|
|
|
|
|
free_child_result(&r); |
|
|
} |
|
|
|
|
|
|
|
|
void test_usage_nonzero_status_no_stdout_output(void) { |
|
|
int status_to_test = 1; |
|
|
ChildResult r = run_usage_and_capture(status_to_test, 1); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(r.exited || r.signaled, "Child did not terminate properly"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(!r.signaled, "Child terminated by signal"); |
|
|
TEST_ASSERT_EQUAL_INT(status_to_test, r.exit_code); |
|
|
|
|
|
|
|
|
TEST_ASSERT_NOT_NULL(r.output); |
|
|
TEST_ASSERT_EQUAL_UINT64_MESSAGE(0u, (unsigned long long)r.length, "Expected no stdout output for nonzero status"); |
|
|
|
|
|
free_child_result(&r); |
|
|
} |
|
|
|
|
|
|
|
|
void test_usage_success_prints_help_and_exits_zero(void) { |
|
|
ChildResult r = run_usage_and_capture(EXIT_SUCCESS, 1); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(r.exited || r.signaled, "Child did not terminate properly"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(!r.signaled, "Child terminated by signal"); |
|
|
TEST_ASSERT_EQUAL_INT(0, r.exit_code); |
|
|
|
|
|
TEST_ASSERT_NOT_NULL(r.output); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(strstr(r.output, "Usage: ") != NULL, "Help text should contain 'Usage:'"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(strstr(r.output, "Examples:") != NULL, "Help text should contain 'Examples:'"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(strstr(r.output, "-z, --zero") != NULL, "Help text should document the -z, --zero option"); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(strstr(r.output, "dirname") != NULL, "Help text should mention the program name"); |
|
|
|
|
|
free_child_result(&r); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
RUN_TEST(test_usage_nonzero_status_exits_and_writes_stderr); |
|
|
RUN_TEST(test_usage_nonzero_status_no_stdout_output); |
|
|
RUN_TEST(test_usage_success_prints_help_and_exits_zero); |
|
|
return UNITY_END(); |
|
|
} |