File size: 6,646 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
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <locale.h>

/* Helper to read all data from an fd into a malloc'd buffer. */
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 new_cap = cap * 2;
            char *new_buf = (char*)realloc(buf, new_cap);
            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) {
            if (errno == EINTR) continue;
            free(buf);
            return NULL;
        } else if (r == 0) {
            break;
        } else {
            len += (size_t)r;
        }
    }

    /* NUL-terminate for convenience (not counted in out_len). */
    if (len == cap) {
        char *new_buf = (char*)realloc(buf, cap + 1);
        if (!new_buf) {
            free(buf);
            return NULL;
        }
        buf = new_buf;
    }
    buf[len] = '\0';
    if (out_len) *out_len = len;
    return buf;
}

/* Run usage(status) in a child, capturing stdout and stderr, and the exit code. */
static int run_usage_and_capture(int status,
                                 char **out_buf, size_t *out_len,
                                 char **err_buf, size_t *err_len,
                                 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;
    }

    if (pid == 0) {
        /* Child: redirect stdout/stderr and call usage. No Unity asserts here. */
        /* Force C locale for predictable messages (best-effort). */
        setenv("LC_ALL", "C", 1);
        setenv("LANG", "C", 1);
        setlocale(LC_ALL, "C");

        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]);

        /* Call the function under test; it should exit(status). */
        usage(status);

        /* Should never reach here. */
        _exit(123);
    }

    /* Parent: close write ends, read outputs, wait for child. */
    close(out_pipe[1]);
    close(err_pipe[1]);

    char *child_out = read_all_from_fd(out_pipe[0], out_len);
    char *child_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 (child_out) free(child_out);
        if (child_err) free(child_err);
        return -1;
    }

    int code = -1;
    if (WIFEXITED(wstatus)) {
        code = WEXITSTATUS(wstatus);
    } else if (WIFSIGNALED(wstatus)) {
        /* Encode signal as negative for clarity. */
        code = -WTERMSIG(wstatus);
    }
    if (exit_code) *exit_code = code;

    if (out_buf) *out_buf = child_out; else free(child_out);
    if (err_buf) *err_buf = child_err; else free(child_err);

    return 0;
}

void setUp(void) {
    /* No global state to set explicitly. */
}

void tearDown(void) {
    /* No global state to clean. */
}

/* Test that usage(EXIT_SUCCESS) prints full help to stdout and exits with 0. */
void test_usage_success_prints_help_and_exits_success(void) {
    char *out = NULL, *err = NULL;
    size_t outlen = 0, errlen = 0;
    int exit_code = -1;

    int rc = run_usage_and_capture(EXIT_SUCCESS, &out, &outlen, &err, &errlen, &exit_code);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(0, exit_code);

    /* On success, help should go to stdout and stderr should be empty. */
    TEST_ASSERT_NOT_NULL(out);
    TEST_ASSERT_TRUE(outlen > 0);
    TEST_ASSERT_NOT_NULL(err);
    TEST_ASSERT_EQUAL_size_t(0, errlen);

    /* Look for invariant option names that should be present in basenc help. */
    TEST_ASSERT_NOT_NULL(strstr(out, "--decode"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--wrap"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base64"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--z85"));    /* basenc-specific option */

    free(out);
    free(err);
}

/* Test that usage(EXIT_FAILURE) emits a 'try --help' hint to stderr, and exits with failure. */
void test_usage_failure_emits_try_help_on_stderr_and_exits_failure(void) {
    char *out = NULL, *err = NULL;
    size_t outlen = 0, errlen = 0;
    int exit_code = -1;

    int rc = run_usage_and_capture(EXIT_FAILURE, &out, &outlen, &err, &errlen, &exit_code);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(EXIT_FAILURE, exit_code);

    /* On failure, stdout should be empty, stderr should contain the hint with "--help". */
    TEST_ASSERT_NOT_NULL(out);
    TEST_ASSERT_EQUAL_size_t(0, outlen);
    TEST_ASSERT_NOT_NULL(err);
    TEST_ASSERT_TRUE(errlen > 0);
    TEST_ASSERT_NOT_NULL(strstr(err, "--help"));

    free(out);
    free(err);
}

/* Test that basenc's multi-base options appear in the success help text. */
void test_usage_success_contains_multiple_basenc_modes(void) {
    char *out = NULL, *err = NULL;
    size_t outlen = 0, errlen = 0;
    int exit_code = -1;

    int rc = run_usage_and_capture(EXIT_SUCCESS, &out, &outlen, &err, &errlen, &exit_code);
    TEST_ASSERT_EQUAL_INT(0, rc);
    TEST_ASSERT_EQUAL_INT(0, exit_code);

    /* Ensure several basenc variants appear in the help text. */
    TEST_ASSERT_NOT_NULL(out);
    TEST_ASSERT_NOT_NULL(strstr(out, "--base64"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base64url"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base58"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base32"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base32hex"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base16"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base2msbf"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--base2lsbf"));
    TEST_ASSERT_NOT_NULL(strstr(out, "--z85"));

    free(out);
    free(err);
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_usage_success_prints_help_and_exits_success);
    RUN_TEST(test_usage_failure_emits_try_help_on_stderr_and_exits_failure);
    RUN_TEST(test_usage_success_contains_multiple_basenc_modes);
    return UNITY_END();
}