|
|
#include "../../unity/unity.h" |
|
|
|
|
|
#include <errno.h> |
|
|
#include <fcntl.h> |
|
|
#include <signal.h> |
|
|
#include <stdarg.h> |
|
|
#include <stdbool.h> |
|
|
#include <stdint.h> |
|
|
#include <stdio.h> |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/wait.h> |
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
*out_len = 0; |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
while (1) { |
|
|
if (len == cap) { |
|
|
size_t new_cap = cap * 2; |
|
|
char* nb = (char*)realloc(buf, new_cap + 1); |
|
|
if (!nb) { |
|
|
free(buf); |
|
|
*out_len = 0; |
|
|
return NULL; |
|
|
} |
|
|
buf = nb; |
|
|
cap = new_cap; |
|
|
} |
|
|
ssize_t r = read(fd, buf + len, cap - len); |
|
|
if (r < 0) { |
|
|
if (errno == EINTR) continue; |
|
|
free(buf); |
|
|
*out_len = 0; |
|
|
return NULL; |
|
|
} |
|
|
if (r == 0) break; |
|
|
len += (size_t)r; |
|
|
} |
|
|
buf[len] = '\0'; |
|
|
*out_len = len; |
|
|
return buf; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int run_usage_and_capture(int status_to_pass, |
|
|
const char* progname, |
|
|
char** out_buf, size_t* out_len, |
|
|
char** err_buf, size_t* err_len, |
|
|
int* exit_code, int* term_sig) |
|
|
{ |
|
|
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 -2; |
|
|
} |
|
|
|
|
|
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 -3; |
|
|
} |
|
|
|
|
|
if (pid == 0) { |
|
|
|
|
|
|
|
|
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]); |
|
|
|
|
|
|
|
|
if (progname) { |
|
|
|
|
|
extern void set_program_name (const char *); |
|
|
set_program_name(progname); |
|
|
} |
|
|
|
|
|
|
|
|
usage(status_to_pass); |
|
|
|
|
|
|
|
|
_exit(126); |
|
|
} |
|
|
|
|
|
|
|
|
close(out_pipe[1]); |
|
|
close(err_pipe[1]); |
|
|
|
|
|
char* out = read_all_from_fd(out_pipe[0], out_len); |
|
|
char* err = read_all_from_fd(err_pipe[0], err_len); |
|
|
|
|
|
close(out_pipe[0]); |
|
|
close(err_pipe[0]); |
|
|
|
|
|
int wstatus = 0; |
|
|
if (waitpid(pid, &wstatus, 0) < 0) { |
|
|
if (out) free(out); |
|
|
if (err) free(err); |
|
|
return -4; |
|
|
} |
|
|
|
|
|
*exit_code = -1; |
|
|
*term_sig = 0; |
|
|
if (WIFEXITED(wstatus)) { |
|
|
*exit_code = WEXITSTATUS(wstatus); |
|
|
} else if (WIFSIGNALED(wstatus)) { |
|
|
*term_sig = WTERMSIG(wstatus); |
|
|
} |
|
|
|
|
|
*out_buf = out ? out : strdup(""); |
|
|
*err_buf = err ? err : strdup(""); |
|
|
|
|
|
if (!*out_buf || !*err_buf) { |
|
|
if (*out_buf) free(*out_buf); |
|
|
if (*err_buf) free(*err_buf); |
|
|
return -5; |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
static bool contains_substr(const char* haystack, const char* needle) { |
|
|
if (!haystack || !needle) return false; |
|
|
return strstr(haystack, needle) != NULL; |
|
|
} |
|
|
|
|
|
|
|
|
static void test_usage_success_outputs_help_and_exits_zero(void) { |
|
|
char *out = NULL, *err = NULL; |
|
|
size_t out_len = 0, err_len = 0; |
|
|
int code = -1, sig = 0; |
|
|
|
|
|
int rc = run_usage_and_capture(EXIT_SUCCESS, "dd", |
|
|
&out, &out_len, &err, &err_len, |
|
|
&code, &sig); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Harness failed to run usage(EXIT_SUCCESS)"); |
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(0, sig, "Child terminated by signal unexpectedly"); |
|
|
TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, code); |
|
|
|
|
|
|
|
|
TEST_ASSERT_MESSAGE(out_len > 0, "Expected help text on stdout"); |
|
|
TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, err_len, "Expected no stderr output for success usage"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(contains_substr(out, "dd"), |
|
|
"Expected program name 'dd' in help output"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(contains_substr(out, "--help"), |
|
|
"Expected '--help' mention in full help output"); |
|
|
|
|
|
free(out); |
|
|
free(err); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_usage_failure_emits_try_help_and_exits_status(void) { |
|
|
char *out = NULL, *err = NULL; |
|
|
size_t out_len = 0, err_len = 0; |
|
|
int code = -1, sig = 0; |
|
|
|
|
|
int rc = run_usage_and_capture(EXIT_FAILURE, "dd", |
|
|
&out, &out_len, &err, &err_len, |
|
|
&code, &sig); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Harness failed to run usage(EXIT_FAILURE)"); |
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(0, sig, "Child terminated by signal unexpectedly"); |
|
|
TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, code); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, out_len, "Expected no stdout output for failure usage"); |
|
|
TEST_ASSERT_MESSAGE(err_len > 0, "Expected message on stderr for failure usage"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, "dd"), |
|
|
"Expected program name 'dd' in error hint"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, "--help"), |
|
|
"Expected '--help' mention in error hint"); |
|
|
|
|
|
free(out); |
|
|
free(err); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_usage_uses_program_name_variable(void) { |
|
|
char *out = NULL, *err = NULL; |
|
|
size_t out_len = 0, err_len = 0; |
|
|
int code = -1, sig = 0; |
|
|
|
|
|
const char* custom = "mydd"; |
|
|
int rc = run_usage_and_capture(2 , |
|
|
custom, |
|
|
&out, &out_len, &err, &err_len, |
|
|
&code, &sig); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Harness failed to run usage(non-zero) with custom program name"); |
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(0, sig, "Child terminated by signal unexpectedly"); |
|
|
TEST_ASSERT_EQUAL_INT(2, code); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, out_len, "Expected no stdout for failure usage"); |
|
|
TEST_ASSERT_MESSAGE(err_len > 0, "Expected stderr output for failure usage"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, custom), |
|
|
"Expected custom program name in failure message"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(contains_substr(err, "--help"), |
|
|
"Expected '--help' mention in failure message"); |
|
|
|
|
|
free(out); |
|
|
free(err); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_usage_success_outputs_help_and_exits_zero); |
|
|
RUN_TEST(test_usage_failure_emits_try_help_and_exits_status); |
|
|
RUN_TEST(test_usage_uses_program_name_variable); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |