1 /*************************************************
2 * PCRE2 POSIX interface test program *
3 *************************************************/
4
5 /*
6 Written by Philip Hazel, December 2022
7 Copyright (c) 2022
8 File last edited: December 2022
9
10 This program tests the POSIX wrapper to the PCRE2 regular expression library.
11 The main PCRE2 test program is pcre2test, which also tests these function
12 calls. This little program is needed to test the case where the client includes
13 pcre2posix.h but not pcre2.h, mainly to make sure that it builds successfully.
14 However, the code is written as a flexible test program to which extra tests
15 can be added.
16
17 Compile with -lpcre2-posix -lpcre2-8
18
19 If run with no options, there is no output on success, and the return code is
20 zero. If any test fails there is output to stderr, and the return code is 1.
21
22 For testing purposes, the "-v" option causes verification output to be written
23 to stdout. */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <pcre2posix.h>
28
29 #define CAPCOUNT 5 /* Number of captures supported */
30 #define PRINTF if (v) printf /* Shorthand for testing output */
31
32 /* This vector contains compiler flags for each pattern that is tested. */
33
34 static int cflags[] = {
35 0, /* Test 0 */
36 REG_ICASE, /* Test 1 */
37 0, /* Test 2 */
38 REG_NEWLINE, /* Test 3 */
39 0 /* Test 4 */
40 };
41
42 /* This vector contains match flags for each pattern that is tested. */
43
44 static int mflags[] = {
45 0, /* Test 0 */
46 0, /* Test 1 */
47 0, /* Test 2 */
48 REG_NOTBOL, /* Test 3 */
49 0 /* Test 4 */
50 };
51
52 /* Automate the number of patterns */
53
54 #define count (int)(sizeof(cflags)/sizeof(int))
55
56 /* The data for each pattern consists of a pattern string, followed by any
57 number of subject strings, terminated by NULL. Some tests share data, but use
58 different flags. */
59
60 static const char *data0_1[] = { "posix", "lower posix", "upper POSIX", NULL };
61 static const char *data2_3[] = { "(*LF)^(cat|dog)", "catastrophic\ncataclysm",
62 "dogfight", "no animals", NULL };
63 static const char *data4[] = { "*badpattern", NULL };
64
65 /* Index the data strings */
66
67 static char **data[] = {
68 (char **)(&data0_1),
69 (char **)(&data0_1),
70 (char **)(&data2_3),
71 (char **)(&data2_3),
72 (char **)(&data4)
73 };
74
75 /* The expected results for each pattern consist of a compiler return code,
76 optionally followed, for each subject string, by a match return code and, for a
77 successful match, up to CAPCOUNT pairs of returned match data. */
78
79 static int results0[] = {
80 0, /* Compiler rc */
81 0, 6, 11, /* 1st match */
82 REG_NOMATCH /* 2nd match */
83 };
84
85 static int results1[] = {
86 0, /* Compiler rc */
87 0, 6, 11, /* 1st match */
88 0, 6, 11 /* 2nd match */
89 };
90
91 static int results2[] = {
92 0, /* Compiler rc */
93 0, 0, 3, 0, 3, /* 1st match */
94 0, 0, 3, 0, 3, /* 2nd match */
95 REG_NOMATCH /* 3rd match */
96 };
97
98 static int results3[] = {
99 0, /* Compiler rc */
100 0, 13, 16, 13, 16, /* 1st match */
101 REG_NOMATCH, /* 2nd match */
102 REG_NOMATCH /* 3rd match */
103 };
104
105 static int results4[] = {
106 REG_BADRPT /* Compiler rc */
107 };
108
109 /* Index the result vectors */
110
111 static int *results[] = {
112 (int *)(&results0),
113 (int *)(&results1),
114 (int *)(&results2),
115 (int *)(&results3),
116 (int *)(&results4)
117 };
118
119 /* And here is the program */
120
main(int argc,char ** argv)121 int main(int argc, char **argv)
122 {
123 regex_t re;
124 regmatch_t match[CAPCOUNT];
125 int v = argc > 1 && strcmp(argv[1], "-v") == 0;
126
127 PRINTF("Test of pcre2posix.h without pcre2.h\n");
128
129 for (int i = 0; i < count; i++)
130 {
131 char *pattern = data[i][0];
132 char **subjects = data[i] + 1;
133 int *rd = results[i];
134 int rc = regcomp(&re, pattern, cflags[i]);
135
136 PRINTF("Pattern: %s flags=0x%02x\n", pattern, cflags[i]);
137
138 if (rc != *rd)
139 {
140 fprintf(stderr, "Unexpected compile error %d (expected %d)\n", rc, *rd);
141 fprintf(stderr, "Pattern is: %s\n", pattern);
142 return 1;
143 }
144
145 if (rc != 0)
146 {
147 if (v)
148 {
149 char buffer[256];
150 (void)regerror(rc, &re, buffer, sizeof(buffer));
151 PRINTF("Compile error %d: %s (expected)\n", rc, buffer);
152 }
153 continue;
154 }
155
156 for (; *subjects != NULL; subjects++)
157 {
158 rc = regexec(&re, *subjects, CAPCOUNT, match, mflags[i]);
159
160 PRINTF("Subject: %s\n", *subjects);
161 PRINTF("Return: %d", rc);
162
163 if (rc != *(++rd))
164 {
165 PRINTF("\n");
166 fprintf(stderr, "Unexpected match error %d (expected %d)\n", rc, *rd);
167 fprintf(stderr, "Pattern is: %s\n", pattern);
168 fprintf(stderr, "Subject is: %s\n", *subjects);
169 return 1;
170 }
171
172 if (rc == 0)
173 {
174 for (int j = 0; j < CAPCOUNT; j++)
175 {
176 regmatch_t *m = match + j;
177 if (m->rm_so < 0) continue;
178 if (m->rm_so != *(++rd) || m->rm_eo != *(++rd))
179 {
180 PRINTF("\n");
181 fprintf(stderr, "Mismatched results for successful match\n");
182 fprintf(stderr, "Pattern is: %s\n", pattern);
183 fprintf(stderr, "Subject is: %s\n", *subjects);
184 fprintf(stderr, "Result %d: expected %d %d received %d %d\n",
185 j, rd[-1], rd[0], m->rm_so, m->rm_eo);
186 return 1;
187 }
188 PRINTF(" (%d %d %d)", j, m->rm_so, m->rm_eo);
189 }
190 }
191
192 else if (v)
193 {
194 char buffer[256];
195 (void)regerror(rc, &re, buffer, sizeof(buffer));
196 PRINTF(": %s (expected)", buffer);
197 }
198
199 PRINTF("\n");
200 }
201
202 regfree(&re);
203 }
204
205 PRINTF("End of test\n");
206 return 0;
207 }
208
209 /* End of pcre2posix_test.c */
210