File size: 8,764 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>

/* Access to internal globals and function since this file is included
   into the same translation unit after the function definitions. */

/* Prototypes of tested function are not needed; we can call it directly:
   static void parse_patterns (int argc, int start, char **argv);
*/

/* Helper: run parse_patterns in a child process, capturing stderr, and
   return the child's exit status. Does not use Unity assertions in the child. */
static int run_parse_patterns_in_child(int argc, int start, char **argv,
                                       char *errbuf, size_t errbufsz)
{
  int pipefd[2];
  if (pipe(pipefd) != 0)
    return -1;

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

  if (pid == 0)
    {
      /* Child: redirect stderr to pipe write end. */
      (void)dup2(pipefd[1], STDERR_FILENO);
      close(pipefd[0]);
      close(pipefd[1]);

      /* Ensure clean control state for this invocation. */
      /* control_used is visible; controls pointer is reused by xpalloc. */
      extern idx_t control_used;
      extern char **global_argv;
      control_used = 0;
      global_argv = argv;

      /* Call the function. If it returns, exit success. */
      parse_patterns(argc, start, argv);
      _exit(0);
    }

  /* Parent: read child's stderr and wait for it. */
  close(pipefd[1]);
  ssize_t total = 0;
  while (total < (ssize_t)errbufsz - 1)
    {
      ssize_t n = read(pipefd[0], errbuf + total, errbufsz - 1 - total);
      if (n > 0)
        total += n;
      else
        break;
    }
  errbuf[total] = '\0';
  close(pipefd[0]);

  int status = 0;
  if (waitpid(pid, &status, 0) < 0)
    return -1;

  if (WIFEXITED(status))
    return WEXITSTATUS(status);
  else if (WIFSIGNALED(status))
    return 128 + WTERMSIG(status);
  else
    return -1;
}

void setUp(void) {
  /* Reset state used by parse_patterns that is safe to reset between tests. */
  extern idx_t control_used;
  control_used = 0;

  /* Align global_argv to avoid null dereferences in error paths that might
     use it (though we avoid such checks in parent without forking). */
  extern char **global_argv;
  global_argv = NULL;
}

void tearDown(void) {
  /* Nothing specific to do. */
}

/* Test: regexp patterns with and without ignore, including offset parsing. */
void test_parse_patterns_regexp_with_offset_and_ignore(void)
{
  extern struct control *controls;
  extern idx_t control_used;
  extern char **global_argv;

  char *argv[] = { "/foo/", "%bar%+3" };
  int argc = 2;
  int start = 0;
  global_argv = argv;

  parse_patterns(argc, start, argv);

  TEST_ASSERT_EQUAL_INT64(2, control_used);

  /* First: /foo/ */
  TEST_ASSERT_TRUE(controls[0].regexpr);
  TEST_ASSERT_FALSE(controls[0].ignore);
  TEST_ASSERT_EQUAL_INT(0, controls[0].offset);
  TEST_ASSERT_EQUAL_INT(0, controls[0].argnum);

  /* Second: %bar%+3 */
  TEST_ASSERT_TRUE(controls[1].regexpr);
  TEST_ASSERT_TRUE(controls[1].ignore);
  TEST_ASSERT_EQUAL_INT(3, controls[1].offset);
  TEST_ASSERT_EQUAL_INT(1, controls[1].argnum);
}

/* Test: numeric pattern with integer repeat count {3}. */
void test_parse_patterns_numeric_with_repeat_integer(void)
{
  extern struct control *controls;
  extern idx_t control_used;
  extern char **global_argv;

  char rep[] = "{3}";
  char *argv[] = { "5", rep };
  int argc = 2;
  int start = 0;
  global_argv = argv;

  parse_patterns(argc, start, argv);

  TEST_ASSERT_EQUAL_INT64(1, control_used);
  TEST_ASSERT_FALSE(controls[0].regexpr);
  TEST_ASSERT_EQUAL_INT64(5, controls[0].lines_required);
  TEST_ASSERT_FALSE(controls[0].repeat_forever);
  TEST_ASSERT_EQUAL_INT64(3, controls[0].repeat);
}

/* Test: numeric pattern with star repeat count {*}. */
void test_parse_patterns_repeat_star(void)
{
  extern struct control *controls;
  extern idx_t control_used;
  extern char **global_argv;

  char rep[] = "{*}";
  char *argv[] = { "6", rep };
  int argc = 2;
  int start = 0;
  global_argv = argv;

  parse_patterns(argc, start, argv);

  TEST_ASSERT_EQUAL_INT64(1, control_used);
  TEST_ASSERT_FALSE(controls[0].regexpr);
  TEST_ASSERT_EQUAL_INT64(6, controls[0].lines_required);
  TEST_ASSERT_TRUE(controls[0].repeat_forever);
}

/* Test: equal line number is allowed (warns but nonfatal). */
void test_parse_patterns_equal_line_ok_warning(void)
{
  extern struct control *controls;
  extern idx_t control_used;
  extern char **global_argv;

  char *argv[] = { "6" }; /* Equal to previous test's last numeric value. */
  int argc = 1;
  int start = 0;
  global_argv = argv;

  parse_patterns(argc, start, argv);

  TEST_ASSERT_EQUAL_INT64(1, control_used);
  TEST_ASSERT_FALSE(controls[0].regexpr);
  TEST_ASSERT_EQUAL_INT64(6, controls[0].lines_required);
}

/* Test: argnum indices correspond to original argv indices when start > 0. */
void test_parse_patterns_argnum_indices(void)
{
  extern struct control *controls;
  extern idx_t control_used;
  extern char **global_argv;

  char *argv[] = { "FILE", "IGNORED", "/x/", "10" };
  int argc = 4;
  int start = 2;
  global_argv = argv;

  parse_patterns(argc, start, argv);

  TEST_ASSERT_EQUAL_INT64(2, control_used);
  TEST_ASSERT_TRUE(controls[0].regexpr);
  TEST_ASSERT_EQUAL_INT(2, controls[0].argnum);

  TEST_ASSERT_FALSE(controls[1].regexpr);
  TEST_ASSERT_EQUAL_INT(3, controls[1].argnum);
  TEST_ASSERT_EQUAL_INT64(10, controls[1].lines_required);
}

/* Test: regexp pattern followed by integer repeat {2}. */
void test_parse_patterns_regex_with_repeat_integer(void)
{
  extern struct control *controls;
  extern idx_t control_used;
  extern char **global_argv;

  char rep[] = "{2}";
  char *argv[] = { "/baz/", rep };
  int argc = 2;
  int start = 0;
  global_argv = argv;

  parse_patterns(argc, start, argv);

  TEST_ASSERT_EQUAL_INT64(1, control_used);
  TEST_ASSERT_TRUE(controls[0].regexpr);
  TEST_ASSERT_FALSE(controls[0].repeat_forever);
  TEST_ASSERT_EQUAL_INT64(2, controls[0].repeat);
}

/* Error case: zero line number -> exit failure. */
void test_parse_patterns_zero_line_error(void)
{
  char *argv[] = { "0" };
  char errbuf[1024];
  int status = run_parse_patterns_in_child(1, 0, argv, errbuf, sizeof errbuf);
  TEST_ASSERT_NOT_EQUAL(0, status);
}

/* Error case: decreasing line numbers -> exit failure. */
void test_parse_patterns_decreasing_line_error(void)
{
  char *argv[] = { "5", "3" };
  char errbuf[1024];
  int status = run_parse_patterns_in_child(2, 0, argv, errbuf, sizeof errbuf);
  TEST_ASSERT_NOT_EQUAL(0, status);
}

/* Error case: invalid non-number pattern -> exit failure. */
void test_parse_patterns_invalid_pattern_non_number_error(void)
{
  char *argv[] = { "abc" };
  char errbuf[1024];
  int status = run_parse_patterns_in_child(1, 0, argv, errbuf, sizeof errbuf);
  TEST_ASSERT_NOT_EQUAL(0, status);
}

/* Error case: missing closing delimiter in regexp "/abc" -> exit failure. */
void test_parse_patterns_missing_closing_delimiter_error(void)
{
  char *argv[] = { "/abc" };
  char errbuf[1024];
  int status = run_parse_patterns_in_child(1, 0, argv, errbuf, sizeof errbuf);
  TEST_ASSERT_NOT_EQUAL(0, status);
}

/* Error case: invalid regular expression "/[/" -> exit failure. */
void test_parse_patterns_invalid_regex_error(void)
{
  char *argv[] = { "/[/" };
  char errbuf[1024];
  int status = run_parse_patterns_in_child(1, 0, argv, errbuf, sizeof errbuf);
  TEST_ASSERT_NOT_EQUAL(0, status);
}

/* Error case: malformed repeat count missing '}' -> exit failure. */
void test_parse_patterns_repeat_missing_right_brace_error(void)
{
  /* Use a mutable buffer for the malformed repeat. */
  char badrep[] = "{3";
  char *argv[] = { "1", badrep };
  char errbuf[1024];
  int status = run_parse_patterns_in_child(2, 0, argv, errbuf, sizeof errbuf);
  TEST_ASSERT_NOT_EQUAL(0, status);
}

int main(void)
{
  UNITY_BEGIN();

  RUN_TEST(test_parse_patterns_regexp_with_offset_and_ignore);
  RUN_TEST(test_parse_patterns_numeric_with_repeat_integer);
  RUN_TEST(test_parse_patterns_repeat_star);
  RUN_TEST(test_parse_patterns_equal_line_ok_warning);
  RUN_TEST(test_parse_patterns_argnum_indices);
  RUN_TEST(test_parse_patterns_regex_with_repeat_integer);

  RUN_TEST(test_parse_patterns_zero_line_error);
  RUN_TEST(test_parse_patterns_decreasing_line_error);
  RUN_TEST(test_parse_patterns_invalid_pattern_non_number_error);
  RUN_TEST(test_parse_patterns_missing_closing_delimiter_error);
  RUN_TEST(test_parse_patterns_invalid_regex_error);
  RUN_TEST(test_parse_patterns_repeat_missing_right_brace_error);

  return UNITY_END();
}