Skip to content

Commit 70e91f9

Browse files
committed
Add tests for our newly introduced limits
This new test program attempts to a) compile regular expressions of increasing lengths and b) match a small regular expression against inputs of increasing lengths and verifies that we get the expected result (`REG_OK`, `REG_ESPACE`, or `REG_NOMATCH`) depending on the exact length being tested. This allows us to drop the `toolong` tests in `retest` and reduce `MAXSTRSIZE` back down to only what `retest` itself needs.
1 parent ad26b6d commit 70e91f9

File tree

3 files changed

+149
-7
lines changed

3 files changed

+149
-7
lines changed

tests/Makefile.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ TESTS = test-str-source retest
6868
if TRE_MULTIBYTE
6969
TESTS += wretest
7070
endif TRE_MULTIBYTE
71+
72+
# This test produces insane amounts of debugging output
73+
if !TRE_DEBUG
74+
check_PROGRAMS += test-limits
75+
TESTS += test-limits
76+
test_limits_LDADD = libxtre.la $(LDADD)
77+
endif

tests/retest.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#define CHAR_T wchar_t
6161
#define L(x) (L ## x)
6262

63-
#define MAXSTRSIZE (TRE_MAX_RE * 2)
63+
#define MAXSTRSIZE 8192
6464
static wchar_t wstr[MAXSTRSIZE];
6565
static wchar_t wregex[MAXSTRSIZE];
6666
static int woffs[MAXSTRSIZE];
@@ -1740,12 +1740,6 @@ main(int argc, char **argv)
17401740

17411741
test_comp(NULL, REG_BASIC, REG_OK);
17421742
test_comp(NULL, REG_EXTENDED, REG_OK);
1743-
#define TOOLONG (TRE_MAX_RE / 2 + 1)
1744-
static char toolong[TOOLONG + TOOLONG + 1];
1745-
memset(toolong, '(', TOOLONG);
1746-
memset(toolong + TOOLONG, ')', TOOLONG);
1747-
toolong[TOOLONG + TOOLONG] = '\0';
1748-
test_comp(toolong, REG_EXTENDED, REG_ESPACE);
17491743

17501744

17511745
/*

tests/test-limits.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#ifdef HAVE_CONFIG_H
2+
#include <config.h>
3+
#endif /* HAVE_CONFIG_H */
4+
5+
#include <limits.h>
6+
#include <stdint.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
#ifdef USE_SYSTEM_REGEX
12+
# include <regex.h>
13+
# ifndef REG_BASIC
14+
# define REG_BASIC 0
15+
# endif
16+
# ifndef REG_OK
17+
# define REG_OK 0
18+
# endif
19+
#else
20+
# include "tre-internal.h"
21+
# define regcomp tre_regcomp
22+
# define regexec tre_regexec
23+
# define regfree tre_regfree
24+
#endif
25+
26+
#define MIN_SHIFT 2
27+
#define MAX_SHIFT 32
28+
29+
static int ntests, nok;
30+
31+
static void ok(void) { fputc('+', stderr); ntests++; nok++; }
32+
static void notok(void) { fputc('-', stderr); ntests++; }
33+
static void done(void) { fputc('\n', stderr); exit(nok == ntests ? 0 : 1); }
34+
#define check(expr) do { ((expr) ? ok() : notok()); } while (0)
35+
36+
int
37+
main(void)
38+
{
39+
regmatch_t pm[9];
40+
regex_t preg;
41+
size_t npm = sizeof(pm) / sizeof(pm[0]);
42+
size_t size;
43+
char *buf;
44+
int error, expect, shift;
45+
46+
if ((size = 1ULL << MAX_SHIFT) == 0 || (buf = malloc(size + 1)) == NULL) {
47+
notok();
48+
} else {
49+
memset(buf, 'a', size);
50+
buf[size] = '\0';
51+
for (shift = MIN_SHIFT; shift <= MAX_SHIFT; shift++) {
52+
fprintf(stderr, "%d", shift);
53+
if ((size = 1ULL << shift) == 0) {
54+
notok();
55+
continue;
56+
}
57+
buf[0] = buf[size - 1] = 'x';
58+
buf[size] = '\0';
59+
60+
#ifdef USE_SYSTEM_REGEX
61+
expect = REG_OK;
62+
#else
63+
/* expected outcome of regcomp() for a regex this size */
64+
expect = size <= TRE_MAX_RE ? REG_OK : REG_ESPACE;
65+
#endif
66+
67+
/* compile a BRE size characters long */
68+
error = regcomp(&preg, buf, REG_BASIC);
69+
if (error != expect) {
70+
notok();
71+
} else if (error == REG_OK) {
72+
ok();
73+
regfree(&preg);
74+
} else {
75+
ok();
76+
}
77+
78+
/* compile an ERE size characters long */
79+
error = regcomp(&preg, buf, REG_EXTENDED);
80+
if (error != expect) {
81+
notok();
82+
} else if (error == REG_OK) {
83+
ok();
84+
regfree(&preg);
85+
} else {
86+
ok();
87+
}
88+
89+
#ifdef USE_SYSTEM_REGEX
90+
expect = REG_OK;
91+
#else
92+
/* expected outcome of regexec() for a string this size */
93+
expect = size <= TRE_MAX_STRING ? REG_OK : REG_NOMATCH;
94+
#endif
95+
96+
/* match a BRE with a string size characters long */
97+
error = regcomp(&preg, "x\\(aa*\\)x", REG_BASIC);
98+
if (error == REG_OK) {
99+
ok();
100+
error = regexec(&preg, buf, npm, pm, 0);
101+
if (error != expect) {
102+
notok();
103+
} else if (error == REG_OK) {
104+
ok();
105+
check(pm[0].rm_so == 0 && (size_t)pm[0].rm_eo == size);
106+
check(pm[1].rm_so == 1 && (size_t)pm[1].rm_eo == size - 1);
107+
} else {
108+
ok();
109+
}
110+
regfree(&preg);
111+
} else {
112+
notok();
113+
}
114+
115+
/* match an ERE with a string size characters long */
116+
error = regcomp(&preg, "x(a+)x", REG_EXTENDED);
117+
if (error == REG_OK) {
118+
ok();
119+
error = regexec(&preg, buf, npm, pm, 0);
120+
if (error != expect) {
121+
notok();
122+
} else if (error == REG_OK) {
123+
ok();
124+
check(pm[0].rm_so == 0 && (size_t)pm[0].rm_eo == size);
125+
check(pm[1].rm_so == 1 && (size_t)pm[1].rm_eo == size - 1);
126+
} else {
127+
ok();
128+
}
129+
regfree(&preg);
130+
} else {
131+
notok();
132+
}
133+
134+
/* undo our changes to the buffer */
135+
buf[0] = buf[size - 1] = buf[size] = 'a';
136+
fflush(stderr);
137+
}
138+
free(buf);
139+
}
140+
done();
141+
}

0 commit comments

Comments
 (0)