#include "../../unity/unity.h" #include #include #include #include #include #include /* The program under test has already included required headers earlier, declaring globals like `program_name` and `chown_mode`, as well as the enum values CHOWN_CHOWN and CHOWN_CHGRP, and the function usage(). */ 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 == cap) { size_t ncap = cap * 2; char *nbuf = (char *)realloc(buf, ncap); if (!nbuf) { free(buf); return NULL; } buf = nbuf; cap = ncap; } ssize_t r = read(fd, buf + len, cap - len); if (r < 0) { if (errno == EINTR) continue; free(buf); return NULL; } if (r == 0) break; len += (size_t)r; } /* Null-terminate for convenience; not counted in length */ if (len == cap) { char *nbuf = (char *)realloc(buf, cap + 1); if (!nbuf) { free(buf); return NULL; } buf = nbuf; } buf[len] = '\0'; if (out_len) *out_len = len; return buf; } /* Helper to run usage(status) in a child and capture stdout/stderr and exit code. */ static void run_usage_and_capture(int status, int mode_value, const char *progname, 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}; TEST_ASSERT_EQUAL_INT(0, pipe(out_pipe)); TEST_ASSERT_EQUAL_INT(0, pipe(err_pipe)); fflush(stdout); fflush(stderr); pid_t pid = fork(); TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork failed"); if (pid == 0) { /* Child: set locale and globals; redirect fds; call usage(). */ /* Avoid using Unity in child. */ (void)signal(SIGPIPE, SIG_DFL); setenv("LC_ALL", "C", 1); setenv("LANG", "C", 1); setenv("LANGUAGE", "C", 1); /* Globals declared by the program headers. */ extern char const *program_name; extern int chown_mode; program_name = progname; chown_mode = mode_value; /* Redirect stdout and stderr. */ 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]); /* This will call exit(status). */ usage(status); /* Should not reach here. */ _exit(126); } /* Parent: close write ends and read from pipes. */ close(out_pipe[1]); close(err_pipe[1]); char *captured_out = read_all_from_fd(out_pipe[0], out_stdout_len); char *captured_err = read_all_from_fd(err_pipe[0], out_stderr_len); close(out_pipe[0]); close(err_pipe[0]); int wstatus = 0; pid_t w = waitpid(pid, &wstatus, 0); TEST_ASSERT_TRUE_MESSAGE(w == pid, "waitpid failed"); int exit_code = -1; if (WIFEXITED(wstatus)) { exit_code = WEXITSTATUS(wstatus); } else if (WIFSIGNALED(wstatus)) { exit_code = 128 + WTERMSIG(wstatus); } /* Ensure buffers are non-NULL even on failure for easier assertions. */ if (!captured_out) { captured_out = (char *)malloc(1); TEST_ASSERT_NOT_NULL(captured_out); captured_out[0] = '\0'; if (out_stdout_len) *out_stdout_len = 0; } if (!captured_err) { captured_err = (char *)malloc(1); TEST_ASSERT_NOT_NULL(captured_err); captured_err[0] = '\0'; if (out_stderr_len) *out_stderr_len = 0; } if (out_stdout) *out_stdout = captured_out; else free(captured_out); if (out_stderr) *out_stderr = captured_err; else free(captured_err); if (out_exit_code) *out_exit_code = exit_code; } void setUp(void) { /* No global setup needed. */ } void tearDown(void) { /* No global teardown needed. */ } static void free_if(char *p) { if (p) free(p); } /* Test: non-zero status should emit brief help hint on stderr and exit with that status. For robustness across gettext, we at least check that "--help" is mentioned and stdout is empty. */ void test_usage_nonzero_status_emits_try_help_to_stderr(void) { char *out = NULL, *err = NULL; size_t out_len = 0, err_len = 0; int ec = -1; run_usage_and_capture(1, CHOWN_CHOWN, "chown", &out, &out_len, &err, &err_len, &ec); TEST_ASSERT_EQUAL_INT(1, ec); TEST_ASSERT_TRUE_MESSAGE(out_len == 0 || out[0] == '\0', "Expected no stdout for error usage"); TEST_ASSERT_TRUE_MESSAGE(err_len > 0, "Expected some stderr output"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(err, "--help"), "Expected '--help' mention in stderr"); /* Also ensure program name is present in hint */ TEST_ASSERT_NOT_NULL_MESSAGE(strstr(err, "chown"), "Expected program name in stderr"); free_if(out); free_if(err); } /* Test: successful usage for chown mode contains key options and examples. */ void test_usage_success_chown_contains_expected_content(void) { char *out = NULL, *err = NULL; size_t out_len = 0, err_len = 0; int ec = -1; run_usage_and_capture(0, CHOWN_CHOWN, "chown", &out, &out_len, &err, &err_len, &ec); TEST_ASSERT_EQUAL_INT(0, ec); TEST_ASSERT_TRUE_MESSAGE(err_len == 0 || err[0] == '\0', "Expected no stderr for success usage"); TEST_ASSERT_TRUE_MESSAGE(out_len > 0, "Expected stdout content for success usage"); /* Check key substrings defined in this source file. */ TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "Usage: chown"), "Usage header missing or wrong program name"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "[OWNER][:[GROUP]]"), "OWNER:GROUP synopsis missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "--reference=RFILE"), "--reference option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-h, --no-dereference"), "no-dereference option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "--from=CURRENT_OWNER:CURRENT_GROUP"), "--from option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-R, --recursive"), "recursive option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "Examples:"), "Examples section missing"); /* Examples tailored to chown */ TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "root:staff /u"), "chown example with group missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-hR root /u"), "chown recursive example missing"); free_if(out); free_if(err); } /* Test: successful usage for chgrp mode contains GROUP-specific text and examples. */ void test_usage_success_chgrp_contains_expected_content(void) { char *out = NULL, *err = NULL; size_t out_len = 0, err_len = 0; int ec = -1; run_usage_and_capture(0, CHOWN_CHGRP, "chgrp", &out, &out_len, &err, &err_len, &ec); TEST_ASSERT_EQUAL_INT(0, ec); TEST_ASSERT_TRUE_MESSAGE(err_len == 0 || err[0] == '\0', "Expected no stderr for success usage"); TEST_ASSERT_TRUE_MESSAGE(out_len > 0, "Expected stdout content for success usage"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "Usage: chgrp"), "Usage header missing or wrong program name"); /* Ensure the synopsis reflects GROUP rather than OWNER:GROUP. */ TEST_ASSERT_NULL_MESSAGE(strstr(out, "[OWNER][:[GROUP]]"), "OWNER:GROUP synopsis should not appear for chgrp"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "GROUP"), "GROUP synopsis likely missing"); /* Check some shared options still appear. */ TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "--reference=RFILE"), "--reference option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-h, --no-dereference"), "no-dereference option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-R, --recursive"), "recursive option missing"); /* Examples tailored to chgrp */ TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "staff /u"), "chgrp example with staff missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-hR staff /u"), "chgrp recursive example missing"); free_if(out); free_if(err); } /* Test: ensure dereference-related text appears in help output. */ void test_usage_contains_dereference_options(void) { char *out = NULL, *err = NULL; size_t out_len = 0, err_len = 0; int ec = -1; run_usage_and_capture(0, CHOWN_CHOWN, "chown", &out, &out_len, &err, &err_len, &ec); TEST_ASSERT_EQUAL_INT(0, ec); TEST_ASSERT_TRUE_MESSAGE(out_len > 0, "Expected stdout content for success usage"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "--dereference"), "--dereference option missing"); TEST_ASSERT_NOT_NULL_MESSAGE(strstr(out, "-h, --no-dereference"), "no-dereference option missing"); free_if(out); free_if(err); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_usage_nonzero_status_emits_try_help_to_stderr); RUN_TEST(test_usage_success_chown_contains_expected_content); RUN_TEST(test_usage_success_chgrp_contains_expected_content); RUN_TEST(test_usage_contains_dereference_options); return UNITY_END(); }