File size: 6,459 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
#include "../../unity/unity.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>

/* Helpers to capture output from a forked child running usage(). */

static char* read_all_fd(int fd, size_t* out_len)
{
    size_t cap = 0;
    size_t len = 0;
    char* buf = NULL;
    char tmp[4096];

    for (;;)
    {
        ssize_t n = read(fd, tmp, sizeof tmp);
        if (n > 0)
        {
            if (len + (size_t)n + 1 > cap)
            {
                size_t new_cap = (cap ? cap * 2 : 8192);
                while (len + (size_t)n + 1 > new_cap)
                    new_cap *= 2;
                char* new_buf = (char*)realloc(buf, new_cap);
                if (!new_buf)
                {
                    free(buf);
                    *out_len = 0;
                    return NULL;
                }
                buf = new_buf;
                cap = new_cap;
            }
            memcpy(buf + len, tmp, (size_t)n);
            len += (size_t)n;
        }
        else if (n == 0)
        {
            break; /* EOF */
        }
        else
        {
            if (errno == EINTR)
                continue;
            break; /* error, but continue with what we have */
        }
    }

    if (!buf)
    {
        buf = (char*)malloc(1);
        if (!buf)
        {
            *out_len = 0;
            return NULL;
        }
    }
    buf[len] = '\0';
    *out_len = len;
    return buf;
}

/* Run usage(status) in a child with given program name, capturing stdout/stderr and exit status. */
static void run_usage_capture_named(int status,
                                    const char* progname,
                                    char** out_str, size_t* out_len,
                                    char** err_str, size_t* err_len,
                                    int* exit_status)
{
    int out_pipe[2] = {-1, -1};
    int err_pipe[2] = {-1, -1};

    *out_str = NULL; *out_len = 0;
    *err_str = NULL; *err_len = 0;
    *exit_status = -1;

    if (pipe(out_pipe) < 0) return;
    if (pipe(err_pipe) < 0) { close(out_pipe[0]); close(out_pipe[1]); return; }

    fflush(stdout);
    fflush(stderr);
    pid_t pid = fork();
    if (pid == 0)
    {
        /* Child: redirect stdout/stderr to pipes, set program name, and call usage. */
        (void)setlocale(LC_ALL, "C");
        /* Best effort to ensure C locale if environment changes between processes. */
        dup2(out_pipe[1], STDOUT_FILENO);
        dup2(err_pipe[1], STDERR_FILENO);

        /* Close pipe fds no longer needed in child */
        close(out_pipe[0]);
        close(out_pipe[1]);
        close(err_pipe[0]);
        close(err_pipe[1]);

        /* Provided by gnulib via system.h/progname.h included in the program source. */
        set_program_name(progname ? progname : "groups");

        usage(status);

        _exit(127); /* Should not reach here. */
    }
    else if (pid > 0)
    {
        /* Parent */
        close(out_pipe[1]);
        close(err_pipe[1]);

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

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

        int wstatus = 0;
        if (waitpid(pid, &wstatus, 0) >= 0 && WIFEXITED(wstatus))
            *exit_status = WEXITSTATUS(wstatus);
        else
            *exit_status = -1;

        if (!out_buf)
        {
            out_buf = (char*)malloc(1);
            if (out_buf) { out_buf[0] = '\0'; *out_len = 0; }
        }
        if (!err_buf)
        {
            err_buf = (char*)malloc(1);
            if (err_buf) { err_buf[0] = '\0'; *err_len = 0; }
        }

        *out_str = out_buf;
        *err_str = err_buf;
    }
    else
    {
        /* fork failed */
        close(out_pipe[0]); close(out_pipe[1]);
        close(err_pipe[0]); close(err_pipe[1]);
    }
}

void setUp(void) {
    /* Ensure deterministic English messages. */
    setenv("LC_ALL", "C", 1);
    setlocale(LC_ALL, "C");
}

void tearDown(void) {
    /* Nothing to clean up */
}

/* Tests */

static void assert_contains(const char* haystack, const char* needle)
{
    TEST_ASSERT_NOT_NULL(haystack);
    TEST_ASSERT_NOT_NULL(needle);
    TEST_ASSERT_TRUE_MESSAGE(strstr(haystack, needle) != NULL, "Expected substring not found");
}

void test_usage_exit_success_outputs_help_to_stdout_and_exits_zero(void)
{
    char* out = NULL; size_t out_len = 0;
    char* err = NULL; size_t err_len = 0;
    int status = -1;

    run_usage_capture_named(EXIT_SUCCESS, "groups", &out, &out_len, &err, &err_len, &status);

    TEST_ASSERT_EQUAL_INT(0, status);
    TEST_ASSERT_TRUE(out_len > 0);
    TEST_ASSERT_EQUAL_INT(0, (int)err_len);

    assert_contains(out, "Usage: ");
    assert_contains(out, "Usage: groups");
    assert_contains(out, "--help");
    assert_contains(out, "--version");
    assert_contains(out, "USERNAME");

    free(out);
    free(err);
}

void test_usage_nonzero_status_prints_try_help_to_stderr_and_exits_status(void)
{
    char* out = NULL; size_t out_len = 0;
    char* err = NULL; size_t err_len = 0;
    int status = -1;

    run_usage_capture_named(2, "groups", &out, &out_len, &err, &err_len, &status);

    TEST_ASSERT_EQUAL_INT(2, status);
    TEST_ASSERT_EQUAL_INT(0, (int)out_len);
    TEST_ASSERT_TRUE(err_len > 0);

    assert_contains(err, "--help");
    assert_contains(err, "groups"); /* should mention the program name */

    free(out);
    free(err);
}

void test_usage_uses_program_name_in_usage_line(void)
{
    char* out = NULL; size_t out_len = 0;
    char* err = NULL; size_t err_len = 0;
    int status = -1;

    const char* custom_name = "gnugroups";
    run_usage_capture_named(EXIT_SUCCESS, custom_name, &out, &out_len, &err, &err_len, &status);

    TEST_ASSERT_EQUAL_INT(0, status);
    TEST_ASSERT_TRUE(out_len > 0);
    TEST_ASSERT_EQUAL_INT(0, (int)err_len);

    /* Ensure the program name used matches what we set. */
    char expect[128];
    snprintf(expect, sizeof(expect), "Usage: %s", custom_name);
    assert_contains(out, expect);

    free(out);
    free(err);
}

int main(void)
{
    UNITY_BEGIN();
    RUN_TEST(test_usage_exit_success_outputs_help_to_stdout_and_exits_zero);
    RUN_TEST(test_usage_nonzero_status_prints_try_help_to_stderr_and_exits_status);
    RUN_TEST(test_usage_uses_program_name_in_usage_line);
    return UNITY_END();
}