File size: 6,617 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

/* usage(int) is defined in the included program source. We also rely on
   set_program_name() and program_name being available via earlier includes
   in the translation unit (system.h/progname.h). */

/* Helper: read all from fd into a NUL-terminated buffer. Caller must free. */
static char *read_all_fd(int fd) {
    size_t cap = 4096;
    size_t len = 0;
    char *buf = (char *)malloc(cap + 1);
    if (!buf) return NULL;

    for (;;) {
        if (len + 2048 > cap) {
            size_t new_cap = cap * 2;
            char *new_buf = (char *)realloc(buf, new_cap + 1);
            if (!new_buf) {
                free(buf);
                return NULL;
            }
            buf = new_buf;
            cap = new_cap;
        }
        ssize_t r = read(fd, buf + len, cap - len);
        if (r > 0) {
            len += (size_t)r;
        } else if (r == 0) {
            break; /* EOF */
        } else {
            if (errno == EINTR) continue;
            /* On error, stop and return what we have so far */
            break;
        }
    }
    buf[len] = '\0';
    return buf;
}

/* Run usage(status) in a child, capturing stdout/stderr and exit status.
   Returns 0 on success (child ran and exited), -1 on error.
   On success, out_str and err_str are malloc'd NUL-terminated strings. */
static int run_usage_and_capture(int status_code, char **out_str, char **err_str, int *exit_status) {
    int out_pipe[2];
    int err_pipe[2];
    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;
    } else if (pid == 0) {
        /* Child: redirect stdout/stderr and call usage. */
        /* Ensure English messages for deterministic tests. */
        setenv("LC_ALL", "C", 1);

        /* Ensure program_name is set properly for output formatting. */
        /* Prototype is available from earlier includes in the TU. */
        set_program_name("expr");

        /* Redirect to pipes. */
        dup2(out_pipe[1], STDOUT_FILENO);
        dup2(err_pipe[1], STDERR_FILENO);

        /* Close all pipe fds we don't need. */
        close(out_pipe[0]); close(out_pipe[1]);
        close(err_pipe[0]); close(err_pipe[1]);

        /* Call the function under test; it will call exit(). */
        usage(status_code);

        /* Should not reach here, but be safe. */
        _exit(255);
    } else {
        /* Parent: close write ends, read outputs, wait for child. */
        close(out_pipe[1]);
        close(err_pipe[1]);

        char *out_buf = read_all_fd(out_pipe[0]);
        char *err_buf = read_all_fd(err_pipe[0]);

        close(out_pipe[0]);
        close(err_pipe[0]);

        int wstatus = 0;
        if (waitpid(pid, &wstatus, 0) < 0) {
            if (out_buf) free(out_buf);
            if (err_buf) free(err_buf);
            return -1;
        }

        if (WIFEXITED(wstatus)) {
            *exit_status = WEXITSTATUS(wstatus);
        } else if (WIFSIGNALED(wstatus)) {
            /* Child terminated by signal; emulate a nonzero code. */
            *exit_status = 128 + WTERMSIG(wstatus);
        } else {
            *exit_status = -1;
        }

        *out_str = out_buf ? out_buf : strdup("");
        *err_str = err_buf ? err_buf : strdup("");
        return 0;
    }
}

/* Unity fixtures */
void setUp(void) {
    /* No global setup needed */
}

void tearDown(void) {
    /* No global cleanup needed */
}

/* Tests */

static void assert_contains(const char *haystack, const char *needle, const char *context) {
    if (!haystack || !needle) {
        TEST_FAIL_MESSAGE("Null pointer passed to assert_contains");
    }
    if (strstr(haystack, needle) == NULL) {
        char msg[512];
        snprintf(msg, sizeof(msg), "Expected to find substring \"%s\" in %s output, but did not.", needle, context);
        TEST_FAIL_MESSAGE(msg);
    }
}

void test_usage_success_prints_full_help_and_exits_zero(void) {
    char *out = NULL;
    char *err = NULL;
    int code = -1;

    int rc = run_usage_and_capture(0, &out, &err, &code);
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Failed to execute usage() in child process");
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, code, "usage(EXIT_SUCCESS) did not exit with code 0");

    /* stdout should contain the main usage lines with program name */
    assert_contains(out, "Usage: expr EXPRESSION", "stdout");
    assert_contains(out, "or:  expr OPTION", "stdout");

    /* stderr should be empty for full help */
    TEST_ASSERT_TRUE_MESSAGE(err != NULL, "err buffer is NULL");
    TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, (unsigned)strlen(err), "stderr should be empty for full help");

    free(out);
    free(err);
}

void test_usage_nonzero_prints_try_help_on_stderr_and_exits_with_status(void) {
    char *out = NULL;
    char *err = NULL;
    int code = -1;

    int rc = run_usage_and_capture(7, &out, &err, &code);
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "Failed to execute usage() in child process");
    TEST_ASSERT_EQUAL_INT_MESSAGE(7, code, "usage(nonzero) did not exit with the given status");

    /* stdout should be empty or minimal for nonzero status */
    TEST_ASSERT_TRUE_MESSAGE(out != NULL, "out buffer is NULL");
    TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, (unsigned)strlen(out), "stdout should be empty for nonzero status");

    /* stderr should contain a prompt to use --help and program name */
    assert_contains(err, "--help", "stderr");
    assert_contains(err, "expr", "stderr");

    free(out);
    free(err);
}

void test_usage_success_includes_exit_status_explanation(void) {
    char *out = NULL;
    char *err = NULL;
    int code = -1;

    int rc = run_usage_and_capture(0, &out, &err, &code);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(0, code);

    /* Check that the full help text includes the exit status explanation line */
    assert_contains(out, "Exit status is 0 if EXPRESSION is neither null nor 0", "stdout");

    free(out);
    free(err);
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_usage_success_prints_full_help_and_exits_zero);
    RUN_TEST(test_usage_nonzero_prints_try_help_on_stderr_and_exits_with_status);
    RUN_TEST(test_usage_success_includes_exit_status_explanation);
    return UNITY_END();
}