Debian Codegraph @

tests/api/regexp.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2022 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Regexp API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = permanent_pool = make_sub_pool(NULL);
34 }
35
36 init_regexp();
37
38 if (getenv("TEST_VERBOSE") != NULL) {
39 pr_trace_set_levels("regexp", 1, 20);
40 }
41 }
42
43 static void tear_down(void) {
44 if (getenv("TEST_VERBOSE") != NULL) {
45 pr_trace_set_levels("regexp", 0, 0);
46 }
47
48 if (p) {
49 destroy_pool(p);
50 p = permanent_pool = NULL;
51 }
52 }
53
54 START_TEST (regexp_alloc_test) {
55 pr_regex_t *res;
56
57 res = pr_regexp_alloc(NULL);
58 ck_assert_msg(res != NULL, "Failed to allocate regex: %s", strerror(errno));
59 pr_regexp_free(NULL, res);
60 }
61 END_TEST
62
63 START_TEST (regexp_free_test) {
64 mark_point();
65 pr_regexp_free(NULL, NULL);
66 }
67 END_TEST
68
69 START_TEST (regexp_error_test) {
70 size_t bufsz, res;
71 const pr_regex_t *pre;
72 char *buf;
73
74 mark_point();
75 res = pr_regexp_error(0, NULL, NULL, 0);
76 ck_assert_msg(res == 0, "Failed to handle null regexp");
77
78 pre = (const pr_regex_t *) 3;
79
80 mark_point();
81 res = pr_regexp_error(0, pre, NULL, 0);
82 ck_assert_msg(res == 0, "Failed to handle null buf");
83
84 bufsz = 256;
85 buf = pcalloc(p, bufsz);
86
87 mark_point();
88 res = pr_regexp_error(0, pre, buf, 0);
89 ck_assert_msg(res == 0, "Failed to handle zero bufsz");
90 }
91 END_TEST
92
93 START_TEST (regexp_compile_test) {
94 pr_regex_t *pre = NULL;
95 int res;
96 char errstr[256], *pattern;
97 size_t errstrlen;
98
99 mark_point();
100 res = pr_regexp_compile(NULL, NULL, 0);
101 ck_assert_msg(res < 0, "Failed to handle null arguments");
102 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
103 strerror(errno), errno);
104
105 mark_point();
106 pre = pr_regexp_alloc(NULL);
107 res = pr_regexp_compile(pre, NULL, 0);
108 ck_assert_msg(res < 0, "Failed to handle null pattern");
109 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
110 strerror(errno), errno);
111
112 pattern = "[=foo";
113 res = pr_regexp_compile(pre, pattern, 0);
114 ck_assert_msg(res != 0, "Successfully compiled pattern unexpectedly");
115
116 errstrlen = pr_regexp_error(1, NULL, NULL, 0);
117 ck_assert_msg(errstrlen == 0, "Failed to handle null arguments");
118
119 errstrlen = pr_regexp_error(1, pre, NULL, 0);
120 ck_assert_msg(errstrlen == 0, "Failed to handle null buffer");
121
122 errstrlen = pr_regexp_error(1, pre, errstr, 0);
123 ck_assert_msg(errstrlen == 0, "Failed to handle zero buffer length");
124
125 errstrlen = pr_regexp_error(res, pre, errstr, sizeof(errstr));
126 ck_assert_msg(errstrlen > 0, "Failed to get regex compilation error string");
127
128 mark_point();
129 pattern = "foo";
130 res = pr_regexp_compile(pre, pattern, 0);
131 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
132
133 mark_point();
134 pr_regexp_free(NULL, pre);
135 pre = pr_regexp_alloc(NULL);
136 pattern = "foo";
137 res = pr_regexp_compile(pre, pattern, REG_ICASE);
138 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
139 pr_regexp_free(NULL, pre);
140 }
141 END_TEST
142
143 START_TEST (regexp_compile_posix_test) {
144 pr_regex_t *pre = NULL;
145 int res;
146 char errstr[256], *pattern;
147 size_t errstrlen;
148
149 res = pr_regexp_compile_posix(NULL, NULL, 0);
150 ck_assert_msg(res < 0, "Failed to handle null arguments");
151 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
152 strerror(errno), errno);
153
154 pre = pr_regexp_alloc(NULL);
155
156 res = pr_regexp_compile_posix(pre, NULL, 0);
157 ck_assert_msg(res < 0, "Failed to handle null pattern");
158 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
159 strerror(errno), errno);
160
161 pattern = "[=foo";
162 res = pr_regexp_compile_posix(pre, pattern, 0);
163 ck_assert_msg(res != 0, "Successfully compiled pattern unexpectedly");
164
165 errstrlen = pr_regexp_error(res, pre, errstr, sizeof(errstr));
166 ck_assert_msg(errstrlen > 0, "Failed to get regex compilation error string");
167
168 pattern = "foo";
169 res = pr_regexp_compile_posix(pre, pattern, 0);
170 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
171
172 pattern = "foo";
173 res = pr_regexp_compile_posix(pre, pattern, REG_ICASE);
174 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
175
176 pr_regexp_free(NULL, pre);
177 }
178 END_TEST
179
180 START_TEST (regexp_get_pattern_test) {
181 pr_regex_t *pre = NULL;
182 int res;
183 const char *str;
184 char *pattern;
185
186 str = pr_regexp_get_pattern(NULL);
187 ck_assert_msg(str == NULL, "Failed to handle null arguments");
188 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
189 strerror(errno), errno);
190
191 pre = pr_regexp_alloc(NULL);
192
193 str = pr_regexp_get_pattern(pre);
194 ck_assert_msg(str == NULL, "Failed to handle null pattern");
195 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
196 strerror(errno), errno);
197
198 pattern = "^foo";
199 res = pr_regexp_compile(pre, pattern, 0);
200 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
201
202 str = pr_regexp_get_pattern(pre);
203 ck_assert_msg(str != NULL, "Failed to get regex pattern: %s", strerror(errno));
204 ck_assert_msg(strcmp(str, pattern) == 0, "Expected '%s', got '%s'", pattern,
205 str);
206
207 pr_regexp_free(NULL, pre);
208 }
209 END_TEST
210
211 START_TEST (regexp_set_limits_test) {
212 int res;
213 pr_regex_t *pre = NULL;
214 const char *pattern, *str;
215
216 res = pr_regexp_set_limits(0, 0);
217 ck_assert_msg(res == 0, "Failed to set limits: %s", strerror(errno));
218
219 /* Set the limits, and compile/execute a regex. */
220 res = pr_regexp_set_limits(1, 1);
221 ck_assert_msg(res == 0, "Failed to set limits: %s", strerror(errno));
222
223 pre = pr_regexp_alloc(NULL);
224
225 pattern = "^foo";
226 res = pr_regexp_compile(pre, pattern, REG_ICASE);
227 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
228
229 str = "fooBAR";
230 (void) pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
231
232 pr_regexp_free(NULL, pre);
233 }
234 END_TEST
235
236 START_TEST (regexp_exec_test) {
237 pr_regex_t *pre = NULL;
238 int res;
239 char *pattern, *str;
240
241 res = pr_regexp_exec(NULL, NULL, 0, NULL, 0, 0, 0);
242 ck_assert_msg(res < 0, "Failed to handle null arguments");
243 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
244 strerror(errno), errno);
245
246 pre = pr_regexp_alloc(NULL);
247
248 pattern = "^foo";
249 res = pr_regexp_compile(pre, pattern, REG_ICASE);
250 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
251
252 res = pr_regexp_exec(pre, NULL, 0, NULL, 0, 0, 0);
253 ck_assert_msg(res != 0, "Failed to handle null string");
254
255 str = "bar";
256 res = pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
257 ck_assert_msg(res != 0, "Matched string unexpectedly");
258
259 str = "foobar";
260 res = pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
261 ck_assert_msg(res == 0, "Failed to match string");
262
263 str = "FOOBAR";
264 res = pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
265 ck_assert_msg(res == 0, "Failed to match string");
266
267 pr_regexp_free(NULL, pre);
268 pre = pr_regexp_alloc(NULL);
269
270 pattern = "^foo";
271 res = pr_regexp_compile_posix(pre, pattern, REG_ICASE);
272 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
273
274 res = pr_regexp_exec(pre, NULL, 0, NULL, 0, 0, 0);
275 ck_assert_msg(res != 0, "Failed to handle null string");
276
277 str = "BAR";
278 res = pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
279 ck_assert_msg(res != 0, "Matched string unexpectedly");
280
281 str = "foobar";
282 res = pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
283 ck_assert_msg(res == 0, "Failed to match string");
284
285 #if !defined(PR_USE_PCRE2) && \
286 !defined(PR_USE_PCRE)
287 /* Note that when PCRE support is used, behavior of POSIX matching may be
288 * surprising; I suspect it relates to the overrides in <pcreposix.h>.
289 */
290 str = "FOOBAR";
291 res = pr_regexp_exec(pre, str, 0, NULL, 0, 0, 0);
292 ck_assert_msg(res == 0, "Failed to match string");
293 #endif /* !PR_USE_PCRE2 and !PR_USE_PCRE */
294
295 pr_regexp_free(NULL, pre);
296 }
297 END_TEST
298
299 #if !defined(PR_USE_PCRE2) && \
300 !defined(PR_USE_PCRE)
301 START_TEST (regexp_capture_posix_test) {
302 register unsigned int i;
303 pr_regex_t *pre = NULL;
304 int captured = FALSE, res;
305 char *pattern, *str;
306 size_t nmatches;
307 regmatch_t *matches;
308
309 pre = pr_regexp_alloc(NULL);
310
311 pattern = "(.*)";
312 res = pr_regexp_compile_posix(pre, pattern, 0);
313 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
314
315 nmatches = 10;
316 matches = pcalloc(p, sizeof(regmatch_t) * nmatches);
317
318 str = "foobar";
319 res = pr_regexp_exec(pre, str, nmatches, matches, 0, 0, 0);
320 ck_assert_msg(res == 0, "Failed to match string");
321
322 for (i = 0; i < nmatches; i++) {
323 int match_len;
324 const char *match_text;
325
326 if (matches[i].rm_so == -1 ||
327 matches[i].rm_eo == -1) {
328 break;
329 }
330
331 match_text = &(str[matches[i].rm_so]);
332 match_len = matches[i].rm_eo - matches[i].rm_so;
333
334 ck_assert_msg(strcmp(match_text, str) == 0,
335 "Expected matched text '%s', got '%s'", str, match_text);
336 ck_assert_msg(match_len == 6,
337 "Expected match text len 6, got %d", match_len);
338
339 captured = TRUE;
340 }
341
342 ck_assert_msg(captured == TRUE,
343 "POSIX regex failed to capture expected groups");
344
345 pr_regexp_free(NULL, pre);
346 }
347 END_TEST
348 #endif /* !PR_USE_PCRE2 and !PR_USE_PCRE */
349
350 #if defined(PR_USE_PCRE)
351 START_TEST (regexp_capture_pcre_test) {
352 register unsigned int i;
353 pr_regex_t *pre = NULL;
354 int captured = FALSE, res;
355 char *pattern, *str;
356 size_t nmatches;
357 regmatch_t *matches;
358
359 pre = pr_regexp_alloc(NULL);
360
361 pattern = "(.*)";
362 res = pr_regexp_compile(pre, pattern, 0);
363 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
364
365 nmatches = 10;
366 matches = pcalloc(p, sizeof(regmatch_t) * nmatches);
367
368 str = "foobar";
369 res = pr_regexp_exec(pre, str, nmatches, matches, 0, 0, 0);
370 ck_assert_msg(res == 0, "Failed to match string");
371
372 for (i = 0; i < nmatches; i++) {
373 int match_len;
374 const char *match_text;
375
376 if (matches[i].rm_so == -1 ||
377 matches[i].rm_eo == -1) {
378 break;
379 }
380
381 match_text = &(str[matches[i].rm_so]);
382 match_len = matches[i].rm_eo - matches[i].rm_so;
383
384 ck_assert_msg(strcmp(match_text, str) == 0,
385 "Expected matched text '%s', got '%s' (i = %u)", str, match_text, i);
386 ck_assert_msg(match_len == 6,
387 "Expected match text len 6, got %d (i = %u)", match_len, i);
388
389 captured = TRUE;
390 }
391
392 ck_assert_msg(captured == TRUE,
393 "PCRE regex failed to capture expected groups");
394
395 pr_regexp_free(NULL, pre);
396 }
397 END_TEST
398 #endif /* PR_USE_PCRE */
399
400 #if defined(PR_USE_PCRE2)
401 START_TEST (regexp_capture_pcre2_test) {
402 register unsigned int i;
403 pr_regex_t *pre = NULL;
404 int captured = FALSE, res;
405 char *pattern, *str;
406 size_t nmatches;
407 regmatch_t *matches;
408
409 pre = pr_regexp_alloc(NULL);
410
411 pattern = "(.*)";
412 res = pr_regexp_compile(pre, pattern, 0);
413 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
414
415 nmatches = 10;
416 matches = pcalloc(p, sizeof(regmatch_t) * nmatches);
417
418 str = "foobar";
419 res = pr_regexp_exec(pre, str, nmatches, matches, 0, 0, 0);
420 ck_assert_msg(res == 0, "Failed to match string");
421
422 for (i = 0; i < nmatches; i++) {
423 int match_len;
424 const char *match_text;
425
426 if (matches[i].rm_so == -1 ||
427 matches[i].rm_eo == -1) {
428 break;
429 }
430
431 match_text = &(str[matches[i].rm_so]);
432 match_len = matches[i].rm_eo - matches[i].rm_so;
433
434 ck_assert_msg(strcmp(match_text, str) == 0,
435 "Expected matched text '%s', got '%s' (i = %u)", str, match_text, i);
436 ck_assert_msg(match_len == 6,
437 "Expected match text len 6, got %d (i = %u)", match_len, i);
438
439 captured = TRUE;
440 }
441
442 ck_assert_msg(captured == TRUE,
443 "PCRE2 regex failed to capture expected groups");
444
445 pr_regexp_free(NULL, pre);
446 }
447 END_TEST
448
449 START_TEST (regexp_capture_pcre2_not_enough_matches_test) {
450 register unsigned int i;
451 pr_regex_t *pre = NULL;
452 int captured = FALSE, res;
453 char *pattern, *str;
454 size_t nmatches;
455 regmatch_t *matches;
456
457 pre = pr_regexp_alloc(NULL);
458
459 mark_point();
460 pattern = "((.*):(.*))";
461 res = pr_regexp_compile(pre, pattern, 0);
462 ck_assert_msg(res == 0, "Failed to compile regex pattern '%s'", pattern);
463
464 nmatches = 1;
465 matches = pcalloc(p, sizeof(regmatch_t) * nmatches);
466
467 mark_point();
468 str = "foo:bar";
469 res = pr_regexp_exec(pre, str, nmatches, matches, 0, 0, 0);
470 ck_assert_msg(res == 0, "Failed to match string");
471
472 mark_point();
473 for (i = 0; i < nmatches; i++) {
474 int match_len;
475 const char *match_text;
476
477 if (matches[i].rm_so == -1 ||
478 matches[i].rm_eo == -1) {
479 break;
480 }
481
482 match_text = &(str[matches[i].rm_so]);
483 match_len = matches[i].rm_eo - matches[i].rm_so;
484
485 ck_assert_msg(strcmp(match_text, str) == 0,
486 "Expected matched text '%s', got '%s' (i = %u)", str, match_text, i);
487 ck_assert_msg(match_len == 7,
488 "Expected match text len 7, got %d (i = %u)", match_len, i);
489
490 captured = TRUE;
491 }
492
493 mark_point();
494 ck_assert_msg(captured == TRUE,
495 "PCRE2 regex failed to capture expected groups");
496
497 pr_regexp_free(NULL, pre);
498 }
499 END_TEST
500 #endif /* PR_USE_PCRE2 */
501
502 START_TEST (regexp_cleanup_test) {
503 pr_regex_t *pre, *pre2, *pre3;
504 int res;
505 char *pattern;
506
507 pattern = "^foo";
508
509 pre = pr_regexp_alloc(NULL);
510 res = pr_regexp_compile(pre, pattern, 0);
511 ck_assert_msg(res == 0, "Failed to compile regexp pattern '%s'", pattern);
512
513 pattern = "bar$";
514 pre2 = pr_regexp_alloc(NULL);
515 res = pr_regexp_compile(pre2, pattern, 0);
516 ck_assert_msg(res == 0, "Failed to compile regexp pattern '%s'", pattern);
517
518 pattern = "&baz$";
519 pre3 = pr_regexp_alloc(NULL);
520 res = pr_regexp_compile_posix(pre3, pattern, 0);
521 ck_assert_msg(res == 0, "Failed to compile POSIX regexp pattern '%s'", pattern);
522
523 mark_point();
524 pr_event_generate("core.restart", NULL);
525
526 mark_point();
527 pr_event_generate("core.exit", NULL);
528
529 mark_point();
530 pr_regexp_free(NULL, pre);
531
532 mark_point();
533 pr_regexp_free(NULL, pre2);
534 }
535 END_TEST
536
537 START_TEST (regexp_set_engine_test) {
538 int res;
539
540 mark_point();
541 res = pr_regexp_set_engine(NULL);
542 ck_assert_msg(res == 0,
543 "Failed to restore default engine: %s", strerror(errno));
544
545 mark_point();
546 res = pr_regexp_set_engine("foobar");
547 ck_assert_msg(res < 0, "Failed to handle unknown engine");
548 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
549 strerror(errno), errno);
550
551 mark_point();
552 res = pr_regexp_set_engine("posix");
553 ck_assert_msg(res == 0, "Failed to handle POSIX engine: %s", strerror(errno));
554
555 mark_point();
556 res = pr_regexp_set_engine("pcre");
557 #if defined(PR_USE_PCRE)
558 ck_assert_msg(res == 0, "Failed to handle PCRE engine: %s", strerror(errno));
559 #else
560 ck_assert_msg(res < 0,
561 "Failed to handle PCRE engine when lacking PCRE support");
562 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
563 strerror(errno), errno);
564 #endif /* PR_USE_PCRE */
565
566 mark_point();
567 res = pr_regexp_set_engine("pcre2");
568 #if defined(PR_USE_PCRE2)
569 ck_assert_msg(res == 0, "Failed to handle PCRE2 engine: %s", strerror(errno));
570 #else
571 ck_assert_msg(res < 0,
572 "Failed to handle PCRE2 engine when lacking PCRE2 support");
573 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
574 strerror(errno), errno);
575 #endif /* PR_USE_PCRE2 */
576
577 (void) pr_regexp_set_engine(NULL);
578 }
579 END_TEST
580
581 Suite *tests_get_regexp_suite(void) {
582 Suite *suite;
583 TCase *testcase;
584
585 suite = suite_create("regexp");
586
587 testcase = tcase_create("base");
588 tcase_add_checked_fixture(testcase, set_up, tear_down);
589
590 tcase_add_test(testcase, regexp_alloc_test);
591 tcase_add_test(testcase, regexp_free_test);
592 tcase_add_test(testcase, regexp_error_test);
593 tcase_add_test(testcase, regexp_compile_test);
594 tcase_add_test(testcase, regexp_compile_posix_test);
595 tcase_add_test(testcase, regexp_exec_test);
596 #if !defined(PR_USE_PCRE2) && \
597 !defined(PR_USE_PCRE)
598 tcase_add_test(testcase, regexp_capture_posix_test);
599 #endif /* !PR_USE_PCRE2 and !PR_USE_PCRE */
600 #if defined(PR_USE_PCRE)
601 tcase_add_test(testcase, regexp_capture_pcre_test);
602 #endif /* PR_USE_PCRE */
603 #if defined(PR_USE_PCRE2)
604 tcase_add_test(testcase, regexp_capture_pcre2_test);
605 tcase_add_test(testcase, regexp_capture_pcre2_not_enough_matches_test);
606 #endif /* PR_USE_PCRE */
607 tcase_add_test(testcase, regexp_get_pattern_test);
608 tcase_add_test(testcase, regexp_set_limits_test);
609 tcase_add_test(testcase, regexp_cleanup_test);
610 tcase_add_test(testcase, regexp_set_engine_test);
611
612 suite_add_tcase(suite, testcase);
613 return suite;
614 }