File size: 6,419 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#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>
#include <fcntl.h>

/* Target under test:
   void usage (int status);
   set_program_name is provided by the program's included headers earlier. */

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 *nb = (char *)realloc(buf, ncap);
            if (!nb)
            {
                free(buf);
                return NULL;
            }
            buf = nb;
            cap = ncap;
        }
        ssize_t n = read(fd, buf + len, cap - len);
        if (n < 0)
        {
            if (errno == EINTR)
                continue;
            free(buf);
            return NULL;
        }
        if (n == 0)
            break;
        len += (size_t)n;
    }

    /* NUL-terminate for convenience, though length is authoritative. */
    if (len == cap)
    {
        char *nb = (char *)realloc(buf, cap + 1);
        if (!nb)
        {
            free(buf);
            return NULL;
        }
        buf = nb;
        cap += 1;
    }
    buf[len] = '\0';
    if (out_len)
        *out_len = len;
    return buf;
}

static int spawn_usage_and_capture(int status,
                                   const char *progname,
                                   char **out_stdout, size_t *len_stdout,
                                   char **out_stderr, size_t *len_stderr,
                                   int *exit_code)
{
    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 to pipes, then call usage. */
        /* Close read ends in child. */
        close(out_pipe[0]);
        close(err_pipe[0]);

        /* Redirect stdout and stderr. */
        if (dup2(out_pipe[1], STDOUT_FILENO) == -1)
            _exit(255);
        if (dup2(err_pipe[1], STDERR_FILENO) == -1)
            _exit(255);

        /* Close the duplicated fds. */
        close(out_pipe[1]);
        close(err_pipe[1]);

        if (progname && *progname)
            set_program_name(progname);

        usage(status);

        /* Should not reach here; if we do, fail the child distinctly. */
        _exit(254);
    }

    /* Parent: close write ends, read from read ends. */
    close(out_pipe[1]);
    close(err_pipe[1]);

    char *captured_out = read_all_from_fd(out_pipe[0], len_stdout);
    char *captured_err = read_all_from_fd(err_pipe[0], len_stderr);

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

    int wstatus = 0;
    pid_t w = 0;
    do {
        w = waitpid(pid, &wstatus, 0);
    } while (w < 0 && errno == EINTR);

    if (w < 0)
    {
        free(captured_out);
        free(captured_err);
        return -1;
    }

    int ec = -1;
    if (WIFEXITED(wstatus))
        ec = WEXITSTATUS(wstatus);
    else if (WIFSIGNALED(wstatus))
        ec = 128 + WTERMSIG(wstatus);

    if (exit_code)
        *exit_code = ec;
    if (out_stdout)
        *out_stdout = captured_out;
    else
        free(captured_out);
    if (out_stderr)
        *out_stderr = captured_err;
    else
        free(captured_err);

    return 0;
}

static int contains_substr(const char *hay, const char *needle)
{
    if (!hay || !needle) return 0;
    return strstr(hay, needle) != NULL;
}

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

void tearDown(void) {
    /* No global teardown needed. */
}

void test_usage_success_outputs_help_to_stdout_only(void)
{
    char *sout = NULL, *serr = NULL;
    size_t lout = 0, lerr = 0;
    int ec = -1;

    int rc = spawn_usage_and_capture(EXIT_SUCCESS, "env",
                                     &sout, &lout, &serr, &lerr, &ec);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(0, ec);
    TEST_ASSERT_NOT_NULL(sout);
    TEST_ASSERT_TRUE_MESSAGE(lout > 0, "Expected non-empty stdout from usage(EXIT_SUCCESS)");
    TEST_ASSERT_NOT_NULL(serr);
    TEST_ASSERT_EQUAL_UINT32(0u, (unsigned)lerr);

    free(sout);
    free(serr);
}

void test_usage_nonzero_status_emits_try_help_to_stderr_and_exits_with_code(void)
{
    char *sout = NULL, *serr = NULL;
    size_t lout = 0, lerr = 0;
    int ec = -1;
    int desired_status = 3;

    int rc = spawn_usage_and_capture(desired_status, "env",
                                     &sout, &lout, &serr, &lerr, &ec);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(desired_status, ec);

    TEST_ASSERT_NOT_NULL(sout);
    TEST_ASSERT_EQUAL_UINT32(0u, (unsigned)lout);

    TEST_ASSERT_NOT_NULL(serr);
    TEST_ASSERT_TRUE_MESSAGE(lerr > 0, "Expected non-empty stderr for non-success usage()");
    TEST_ASSERT_TRUE_MESSAGE(contains_substr(serr, "--help"),
                             "Expected '--help' hint in stderr output");

    free(sout);
    free(serr);
}

void test_usage_uses_program_name_in_output(void)
{
    char *sout = NULL, *serr = NULL;
    size_t lout = 0, lerr = 0;
    int ec = -1;
    const char *pname = "my-env-test";

    int rc = spawn_usage_and_capture(EXIT_SUCCESS, pname,
                                     &sout, &lout, &serr, &lerr, &ec);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(0, ec);
    TEST_ASSERT_NOT_NULL(sout);
    TEST_ASSERT_TRUE_MESSAGE(lout > 0, "Expected non-empty stdout from usage(EXIT_SUCCESS)");
    TEST_ASSERT_TRUE_MESSAGE(contains_substr(sout, pname),
                             "Expected program name to appear in usage output");

    TEST_ASSERT_NOT_NULL(serr);
    TEST_ASSERT_EQUAL_UINT32(0u, (unsigned)lerr);

    free(sout);
    free(serr);
}

int main(void)
{
    UNITY_BEGIN();

    RUN_TEST(test_usage_success_outputs_help_to_stdout_only);
    RUN_TEST(test_usage_nonzero_status_emits_try_help_to_stderr_and_exits_with_code);
    RUN_TEST(test_usage_uses_program_name_in_output);

    return UNITY_END();
}