File size: 7,772 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <signal.h>

/* program_name is declared by coreutils headers included before this test */
extern char const *program_name;

/* Helper: read all data from fd into a dynamically-allocated buffer.
   Returns 0 on success, -1 on error. On success, *buf points to malloc'd
   memory (NUL-terminated for convenience) and *len to its length. */
static int read_all_fd(int fd, char **buf, size_t *len)
{
  size_t cap = 1024;
  size_t used = 0;
  char *out = (char *)malloc(cap);
  if (!out) return -1;

  for (;;)
  {
    if (used == cap)
    {
      size_t ncap = cap * 2;
      char *tmp = (char *)realloc(out, ncap);
      if (!tmp) { free(out); return -1; }
      out = tmp;
      cap = ncap;
    }
    ssize_t n = read(fd, out + used, cap - used);
    if (n > 0)
    {
      used += (size_t)n;
      continue;
    }
    if (n == 0)
      break;
    if (errno == EINTR)
      continue;
    free(out);
    return -1;
  }
  /* NUL-terminate for strstr convenience */
  if (used == cap)
  {
    char *tmp = (char *)realloc(out, cap + 1);
    if (!tmp) { free(out); return -1; }
    out = tmp;
  }
  out[used] = '\0';
  *buf = out;
  *len = used;
  return 0;
}

/* Helper: create a pipe with CLOEXEC if available, else set it manually. */
static int make_pipe_cloexec(int p[2])
{
#ifdef O_CLOEXEC
  if (pipe2(p, O_CLOEXEC) == 0)
    return 0;
  if (errno != ENOSYS && errno != EINVAL)
    return -1;
#endif
  if (pipe(p) != 0)
    return -1;
  /* set CLOEXEC on both ends */
  for (int i = 0; i < 2; i++)
  {
    int flags = fcntl(p[i], F_GETFD);
    if (flags >= 0)
      fcntl(p[i], F_SETFD, flags | FD_CLOEXEC);
  }
  return 0;
}

/* Run usage(status) in a child, capturing its stdout and stderr.
   On success returns 0 and populates outputs (caller must free *out and *err).
   On failure returns -1. */
static int run_usage_and_capture(int status,
                                 char **out, size_t *out_len,
                                 char **err, size_t *err_len,
                                 int *exit_code)
{
  int out_p[2] = {-1,-1};
  int err_p[2] = {-1,-1};
  if (make_pipe_cloexec(out_p) != 0) return -1;
  if (make_pipe_cloexec(err_p) != 0)
  {
    close(out_p[0]); close(out_p[1]);
    return -1;
  }

  fflush(stdout);
    fflush(stderr);
    pid_t pid = fork();
  if (pid < 0)
  {
    close(out_p[0]); close(out_p[1]);
    close(err_p[0]); close(err_p[1]);
    return -1;
  }

  if (pid == 0)
  {
    /* Child: redirect stdout/stderr to pipes */
    /* Avoid stdio buffering issues by setting line buffering or unbuffered */
    /* But since we dup the fds, exit() will flush appropriately */
    if (dup2(out_p[1], STDOUT_FILENO) < 0) _exit(127);
    if (dup2(err_p[1], STDERR_FILENO) < 0) _exit(127);
    close(out_p[0]); close(out_p[1]);
    close(err_p[0]); close(err_p[1]);

    /* Ensure program_name has a value */
    if (!program_name || program_name[0] == '\0')
      program_name = "mknod";

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

    /* If it returns, exit with sentinel code. */
    _exit(123);
  }

  /* Parent */
  close(out_p[1]);
  close(err_p[1]);

  char *out_buf = NULL, *err_buf = NULL;
  size_t out_sz = 0, err_sz = 0;

  int r1 = read_all_fd(out_p[0], &out_buf, &out_sz);
  int r2 = read_all_fd(err_p[0], &err_buf, &err_sz);

  close(out_p[0]);
  close(err_p[0]);

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

  if (r1 != 0 || r2 != 0 || w < 0)
  {
    free(out_buf);
    free(err_buf);
    return -1;
  }

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

  *out = out_buf; *out_len = out_sz;
  *err = err_buf; *err_len = err_sz;
  *exit_code = code;
  return 0;
}

/* Helper: check if buffer contains substring (ASCII) */
static int buf_contains(const char *buf, const char *needle)
{
  if (!buf || !needle) return 0;
  return strstr(buf, needle) != NULL;
}

void setUp(void) {
  /* Force C locale for deterministic messages */
  setenv("LC_ALL", "C", 1);
  setlocale(LC_ALL, "C");

  /* Ensure program_name is set consistently for messages */
  if (!program_name || program_name[0] == '\0')
    program_name = "mknod";
  else
    program_name = "mknod";
}

void tearDown(void) {
  /* Nothing to cleanup */
}

static void free_captured(char *out, char *err)
{
  free(out);
  free(err);
}

void test_usage_success_outputs_usage_and_exits_zero(void)
{
  char *out = NULL, *err = NULL;
  size_t out_len = 0, err_len = 0;
  int code = -1;

  int rc = run_usage_and_capture(EXIT_SUCCESS, &out, &out_len, &err, &err_len, &code);
  TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "run_usage_and_capture failed");

  /* Check exit code */
  TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, code);

  /* stderr should be empty for success usage */
  TEST_ASSERT_TRUE_MESSAGE(err_len == 0 || (err && err[0] == '\0'),
                           "Expected no stderr output for successful usage()");

  /* stdout should contain usage header and key content */
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(out, "Usage: mknod"),
                           "stdout should contain 'Usage: mknod'");
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(out, "TYPE may be"),
                           "stdout should mention 'TYPE may be'");
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(out, "block (buffered) special file"),
                           "stdout should describe block special file");

  free_captured(out, err);
}

void test_usage_nonzero_status_emits_try_help_and_exits_status(void)
{
  char *out = NULL, *err = NULL;
  size_t out_len = 0, err_len = 0;
  int code = -1;

  int wanted = 2;
  int rc = run_usage_and_capture(wanted, &out, &out_len, &err, &err_len, &code);
  TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "run_usage_and_capture failed");

  /* Expect the exact exit status */
  TEST_ASSERT_EQUAL_INT(wanted, code);

  /* stdout should be empty in the error path */
  TEST_ASSERT_TRUE_MESSAGE(out_len == 0 || (out && out[0] == '\0'),
                           "Expected no stdout output when status != EXIT_SUCCESS");

  /* stderr should contain a try-help message mentioning '--help' and program name */
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(err, "--help"),
                           "stderr should contain '--help' hint");
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(err, "mknod"),
                           "stderr should mention program name 'mknod'");
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(err, "Try"),
                           "stderr should start with a 'Try' hint");

  free_captured(out, err);
}

void test_usage_success_includes_mode_option_line(void)
{
  char *out = NULL, *err = NULL;
  size_t out_len = 0, err_len = 0;
  int code = -1;

  int rc = run_usage_and_capture(EXIT_SUCCESS, &out, &out_len, &err, &err_len, &code);
  TEST_ASSERT_EQUAL_INT_MESSAGE(0, rc, "run_usage_and_capture failed");

  TEST_ASSERT_EQUAL_INT(EXIT_SUCCESS, code);
  TEST_ASSERT_TRUE_MESSAGE(buf_contains(out, "-m, --mode"),
                           "stdout should document the -m, --mode option");

  /* Ensure no unexpected stderr output */
  TEST_ASSERT_TRUE_MESSAGE(err_len == 0 || (err && err[0] == '\0'),
                           "Expected no stderr output for successful usage()");

  free_captured(out, err);
}

int main(void)
{
  UNITY_BEGIN();
  RUN_TEST(test_usage_success_outputs_usage_and_exits_zero);
  RUN_TEST(test_usage_nonzero_status_emits_try_help_and_exits_status);
  RUN_TEST(test_usage_success_includes_mode_option_line);
  return UNITY_END();
}