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

/* Helper to read all data from a fd into a malloc'd buffer.
   Returns buffer (NUL-terminated) and sets *len. On error, returns NULL. */
static char* tfu_read_all(int fd, size_t *len_out) {
    size_t cap = 4096;
    size_t len = 0;
    char *buf = (char*)malloc(cap + 1);
    if (!buf) return NULL;

    for (;;) {
        if (len == cap) {
            size_t new_cap = cap * 2;
            char *nb = (char*)realloc(buf, new_cap + 1);
            if (!nb) {
                free(buf);
                return NULL;
            }
            buf = nb;
            cap = new_cap;
        }
        ssize_t r = read(fd, buf + len, cap - len);
        if (r > 0) {
            len += (size_t)r;
            continue;
        } else if (r == 0) {
            break;
        } else {
            if (errno == EINTR) continue;
            free(buf);
            return NULL;
        }
    }
    buf[len] = '\0';
    if (len_out) *len_out = len;
    return buf;
}

typedef struct {
    char *out;
    size_t out_len;
    char *err;
    size_t err_len;
    int exit_code;   /* valid if term_sig == 0 */
    int term_sig;    /* nonzero if terminated by signal */
} tfu_capture_result;

/* Run usage(status) in a child, capturing stdout and stderr.
   Returns true on success, false on setup/fork errors. */
static bool tfu_run_usage_capture(int status, tfu_capture_result *res) {
    int out_pipe[2] = {-1, -1};
    int err_pipe[2] = {-1, -1};
    if (pipe(out_pipe) != 0) return false;
    if (pipe(err_pipe) != 0) {
        close(out_pipe[0]); close(out_pipe[1]);
        return false;
    }

    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 false;
    }

    if (pid == 0) {
        /* Child: redirect stdout/stderr and call usage. */
        /* Ensure C locale (but gettext defaults to C without setlocale). */
        setenv("LC_ALL", "C", 1);
        setlocale(LC_ALL, "");

        /* Redirect stdout and stderr to pipes. */
        if (dup2(out_pipe[1], STDOUT_FILENO) < 0) _exit(127);
        if (dup2(err_pipe[1], STDERR_FILENO) < 0) _exit(127);

        /* Close unused fds. */
        close(out_pipe[0]); close(out_pipe[1]);
        close(err_pipe[0]); close(err_pipe[1]);

        /* Call target (will exit). */
        usage(status);

        /* Should not reach here; ensure exit anyway. */
        _exit(127);
    }

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

    tfu_capture_result tmp = {0};
    tmp.out = tfu_read_all(out_pipe[0], &tmp.out_len);
    tmp.err = tfu_read_all(err_pipe[0], &tmp.err_len);
    close(out_pipe[0]);
    close(err_pipe[0]);

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

    if (w < 0) {
        free(tmp.out);
        free(tmp.err);
        return false;
    }

    if (WIFEXITED(wstatus)) {
        tmp.exit_code = WEXITSTATUS(wstatus);
        tmp.term_sig = 0;
    } else if (WIFSIGNALED(wstatus)) {
        tmp.term_sig = WTERMSIG(wstatus);
        tmp.exit_code = -1;
    } else {
        tmp.term_sig = -1;
        tmp.exit_code = -1;
    }

    *res = tmp;
    return true;
}

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

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

/* Helper to free capture result buffers. */
static void tfu_free_capture(tfu_capture_result *res) {
    if (!res) return;
    free(res->out);
    free(res->err);
    res->out = NULL;
    res->err = NULL;
    res->out_len = res->err_len = 0;
}

/* Test that usage(EXIT_SUCCESS) prints full help to stdout, nothing to stderr, and exits 0. */
void test_usage_success_prints_help_and_exits_zero(void) {
    tfu_capture_result r = {0};
    TEST_ASSERT_TRUE_MESSAGE(tfu_run_usage_capture(EXIT_SUCCESS, &r), "Failed to run usage in child");
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, r.term_sig, "Child terminated by signal");
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, r.exit_code, "Exit code mismatch for success usage");

    /* stdout should be non-empty and contain key option tokens and header */
    TEST_ASSERT_NOT_NULL(r.out);
    TEST_ASSERT_TRUE_MESSAGE(r.out_len > 0, "Expected stdout to be non-empty");
    TEST_ASSERT_NOT_NULL(r.err);
    TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, r.err_len, "Expected stderr to be empty on success");

    /* Check for stable substrings in help output (C locale). */
    TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "Usage:"), "Missing 'Usage:' line");
    TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "-d, --delimiters"), "Missing -d, --delimiters");
    TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "-s, --serial"), "Missing -s, --serial");
    TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.out, "-z, --zero-terminated"), "Missing -z, --zero-terminated");

    tfu_free_capture(&r);
}

/* Test that usage with nonzero status emits try-help to stderr and exits with that status. */
void test_usage_failure_emits_try_help_and_exits_with_status(void) {
    const int code = 42; /* arbitrary nonzero in 0..255 */
    tfu_capture_result r = {0};
    TEST_ASSERT_TRUE_MESSAGE(tfu_run_usage_capture(code, &r), "Failed to run usage in child");
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, r.term_sig, "Child terminated by signal");
    TEST_ASSERT_EQUAL_INT_MESSAGE(code, r.exit_code, "Exit code mismatch for failure usage");

    /* stderr should be non-empty and contain '--help' */
    TEST_ASSERT_NOT_NULL(r.err);
    TEST_ASSERT_TRUE_MESSAGE(r.err_len > 0, "Expected stderr to be non-empty on failure usage");
    TEST_ASSERT_NOT_NULL_MESSAGE(strstr(r.err, "--help"), "Expected '--help' hint in stderr");

    /* stdout should be empty */
    TEST_ASSERT_NOT_NULL(r.out);
    TEST_ASSERT_EQUAL_UINT_MESSAGE(0u, r.out_len, "Expected stdout to be empty on failure usage");

    tfu_free_capture(&r);
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_usage_success_prints_help_and_exits_zero);
    RUN_TEST(test_usage_failure_emits_try_help_and_exits_with_status);
    return UNITY_END();
}