Handle anychar (.) correctly with bounded repeaat regex expression.
[crypto.git] / lib / silcutil / tests / test_silcregex.c
1 /* Regex tests */
2
3 #include "silc.h"
4
5 int main(int argc, char **argv)
6 {
7   SilcBool success = FALSE;
8   SilcRegexStruct reg;
9   SilcRegexMatchStruct match[10];
10   int i, num_match = 10;
11   char *regex, *string, *sub;
12   SilcBufferStruct bmatch;
13
14   if (argc > 1 && !strcmp(argv[1], "-d")) {
15     silc_log_debug(TRUE);
16     silc_log_quick(TRUE);
17     silc_log_debug_hexdump(TRUE);
18     silc_log_set_debug_string("*regex*,*errno*");
19   }
20
21   regex = ".{5}";
22   SILC_LOG_DEBUG(("Regex %s", regex));
23   string = "abcdefghijklmn";
24   SILC_LOG_DEBUG(("Match %s", string));
25   if (!silc_regex(string, regex, &bmatch, NULL))
26     goto err;
27   silc_buffer_printf(&bmatch, TRUE);
28
29   regex = ".....";
30   SILC_LOG_DEBUG(("Regex %s", regex));
31   string = "abcdefghijklmn";
32   SILC_LOG_DEBUG(("Match %s", string));
33   if (!silc_regex(string, regex, &bmatch, NULL))
34     goto err;
35   silc_buffer_printf(&bmatch, TRUE);
36
37   regex = "^a{0}$";
38   SILC_LOG_DEBUG(("Regex %s", regex));
39   string = "";
40   SILC_LOG_DEBUG(("Match %s", string));
41   if (!silc_regex(string, regex, &bmatch, NULL))
42     goto err;
43   silc_buffer_printf(&bmatch, TRUE);
44
45   regex = "^a{0,}$";
46   SILC_LOG_DEBUG(("Regex %s", regex));
47   string = "bbbb";
48   SILC_LOG_DEBUG(("Match %s", string));
49   if (!silc_regex(string, regex, &bmatch, NULL))
50     goto err;
51   silc_buffer_printf(&bmatch, TRUE);
52
53   regex = "^a{0,}$";
54   SILC_LOG_DEBUG(("Regex %s", regex));
55   string = "aaaaaaaaa";
56   SILC_LOG_DEBUG(("Match %s", string));
57   if (!silc_regex(string, regex, &bmatch, NULL))
58     goto err;
59   silc_buffer_printf(&bmatch, TRUE);
60
61   regex = "^a{0,0}$";
62   SILC_LOG_DEBUG(("Regex %s", regex));
63   string = "a";
64   SILC_LOG_DEBUG(("DO NOT Match %s", string));
65   if (silc_regex(string, regex, &bmatch, NULL))
66     goto err;
67
68   regex = "^a{3}$";
69   SILC_LOG_DEBUG(("Regex %s", regex));
70   string = "aaa";
71   SILC_LOG_DEBUG(("Match %s", string));
72   if (!silc_regex(string, regex, &bmatch, NULL))
73     goto err;
74   silc_buffer_printf(&bmatch, TRUE);
75
76   regex = "^a{3}$";
77   SILC_LOG_DEBUG(("Regex %s", regex));
78   string = "aaaa";
79   SILC_LOG_DEBUG(("DO NOT Match %s", string));
80   if (silc_regex(string, regex, &bmatch, NULL))
81     goto err;
82
83   regex = "^a{3,5}$";
84   SILC_LOG_DEBUG(("Regex %s", regex));
85   string = "aaa";
86   SILC_LOG_DEBUG(("Match %s", string));
87   if (!silc_regex(string, regex, &bmatch, NULL))
88     goto err;
89   silc_buffer_printf(&bmatch, TRUE);
90
91   regex = "^a{3,5}$";
92   SILC_LOG_DEBUG(("Regex %s", regex));
93   string = "aaaa";
94   SILC_LOG_DEBUG(("Match %s", string));
95   if (!silc_regex(string, regex, &bmatch, NULL))
96     goto err;
97   silc_buffer_printf(&bmatch, TRUE);
98
99   regex = "^a{3,5}$";
100   SILC_LOG_DEBUG(("Regex %s", regex));
101   string = "aaaaaa";
102   SILC_LOG_DEBUG(("DO NOT Match %s", string));
103   if (silc_regex(string, regex, &bmatch, NULL))
104     goto err;
105
106   regex = "^a{3,}$";
107   SILC_LOG_DEBUG(("Regex %s", regex));
108   string = "aaa";
109   SILC_LOG_DEBUG(("Match %s", string));
110   if (!silc_regex(string, regex, &bmatch, NULL))
111     goto err;
112   silc_buffer_printf(&bmatch, TRUE);
113
114   regex = "^a{3,}$";
115   SILC_LOG_DEBUG(("Regex %s", regex));
116   string = "aaaaaaaaaaaaa";
117   SILC_LOG_DEBUG(("Match %s", string));
118   if (!silc_regex(string, regex, &bmatch, NULL))
119     goto err;
120   silc_buffer_printf(&bmatch, TRUE);
121
122   regex = "^a{3,}$";
123   SILC_LOG_DEBUG(("Regex %s", regex));
124   string = "aa";
125   SILC_LOG_DEBUG(("DO NOT Match %s", string));
126   if (silc_regex(string, regex, &bmatch, NULL))
127     goto err;
128
129
130   regex = "a*b";
131   SILC_LOG_DEBUG(("Regex %s", regex));
132   string = "b";
133   SILC_LOG_DEBUG(("Match %s", string));
134   if (!silc_regex(string, regex, &bmatch, NULL))
135     goto err;
136   silc_buffer_printf(&bmatch, TRUE);
137
138   regex = "a*b";
139   SILC_LOG_DEBUG(("Regex %s", regex));
140   string = "ab";
141   SILC_LOG_DEBUG(("Match %s", string));
142   if (!silc_regex(string, regex, &bmatch, NULL))
143     goto err;
144   silc_buffer_printf(&bmatch, TRUE);
145
146   regex = "a*b";
147   SILC_LOG_DEBUG(("Regex %s", regex));
148   string = "aaaab";
149   SILC_LOG_DEBUG(("Match %s", string));
150   if (!silc_regex(string, regex, &bmatch, NULL))
151     goto err;
152   silc_buffer_printf(&bmatch, TRUE);
153
154
155   regex = "a+b";
156   SILC_LOG_DEBUG(("Regex %s", regex));
157   string = "ab";
158   SILC_LOG_DEBUG(("Match %s", string));
159   if (!silc_regex(string, regex, &bmatch, NULL))
160     goto err;
161   silc_buffer_printf(&bmatch, TRUE);
162
163   regex = "a+b";
164   SILC_LOG_DEBUG(("Regex %s", regex));
165   string = "aaaab";
166   SILC_LOG_DEBUG(("Match %s", string));
167   if (!silc_regex(string, regex, &bmatch, NULL))
168     goto err;
169   silc_buffer_printf(&bmatch, TRUE);
170
171   regex = "a+b";
172   SILC_LOG_DEBUG(("Regex %s", regex));
173   string = "b";
174   SILC_LOG_DEBUG(("DO NOT Match %s", string));
175   if (silc_regex(string, regex, &bmatch, NULL))
176     goto err;
177
178
179   regex = "ca?b";
180   SILC_LOG_DEBUG(("Regex %s", regex));
181   string = "cb";
182   SILC_LOG_DEBUG(("Match %s", string));
183   if (!silc_regex(string, regex, &bmatch, NULL))
184     goto err;
185   silc_buffer_printf(&bmatch, TRUE);
186
187   regex = "ca?b";
188   SILC_LOG_DEBUG(("Regex %s", regex));
189   string = "cab";
190   SILC_LOG_DEBUG(("Match %s", string));
191   if (!silc_regex(string, regex, &bmatch, NULL))
192     goto err;
193   silc_buffer_printf(&bmatch, TRUE);
194
195   regex = "ca?b";
196   SILC_LOG_DEBUG(("Regex %s", regex));
197   string = "caab";
198   SILC_LOG_DEBUG(("DO NOT Match %s", string));
199   if (silc_regex(string, regex, &bmatch, NULL))
200     goto err;
201
202
203   regex = "(H..).(o..)";
204   SILC_LOG_DEBUG(("Regex %s", regex));
205   if (!silc_regex_compile(&reg, regex, 0))
206     goto err;
207
208   string = "Hello World";
209   SILC_LOG_DEBUG(("Match %s", string));
210   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
211     goto err;
212   for (i = 0; i < num_match; i++) {
213     if (match[i].start != -1) {
214       SILC_LOG_DEBUG(("Match start %d, end %d", match[i].start,
215                       match[i].end));
216       sub = silc_memdup(string + match[i].start, match[i].end - 
217                         match[i].start);
218       SILC_LOG_DEBUG(("Match substring '%s'", sub));
219       silc_free(sub);
220     }
221   }
222
223   silc_regex_free(&reg);
224
225   regex = "foo[0-9]*";
226   SILC_LOG_DEBUG(("Regex %s", regex));
227   if (!silc_regex_compile(&reg, regex, 0))
228     goto err;
229
230   string = "foo";
231   SILC_LOG_DEBUG(("Match %s", string));
232   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
233     goto err;
234
235   string = "foo20";
236   SILC_LOG_DEBUG(("Match %s", string));
237   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
238     goto err;
239
240   string = "foo20, bar, foo100, foo";
241   SILC_LOG_DEBUG(("Match all substrings in %s", string));
242   while (silc_regex_match(&reg, string, strlen(string), 1, match, 0)) {
243     SILC_LOG_DEBUG(("Match start %d", match[0].start));
244     sub = silc_memdup(string + match[0].start, match[0].end - match[0].start);
245     SILC_LOG_DEBUG(("Match substring '%s'", sub));
246     silc_free(sub);
247     string += match[0].end;
248   }
249
250   string = "foo20, bar, foo100, Foo, foo0";
251   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
252   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
253     goto err;
254
255   for (i = 0; i < num_match; i++) {
256     if (match[i].start != -1) {
257       SILC_LOG_DEBUG(("Match start %d", match[i].start));
258       sub = silc_memdup(string + match[i].start, match[i].end - 
259                         match[i].start);
260       SILC_LOG_DEBUG(("Match substring '%s'", sub));
261       silc_free(sub);
262     }
263   }
264
265   silc_regex_free(&reg);
266
267   regex = "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)";
268   SILC_LOG_DEBUG(("Regex %s", regex));
269   if (!silc_regex_compile(&reg, regex, 0))
270     goto err;
271
272   string = "http://silcnet.org:443/foobar/pelle.html";
273   SILC_LOG_DEBUG(("Parse URI"));
274   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
275     goto err;
276
277   for (i = 0; i < num_match; i++) {
278     if (match[i].start != -1) {
279       SILC_LOG_DEBUG(("Match start %d", match[i].start));
280       sub = silc_memdup(string + match[i].start, match[i].end - 
281                         match[i].start);
282       SILC_LOG_DEBUG(("Match substring '%s'", sub));
283       silc_free(sub);
284     }
285   }
286
287   string = "http://silcnet.org/";
288   SILC_LOG_DEBUG(("Parse URI"));
289   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
290     goto err;
291
292   for (i = 0; i < num_match; i++) {
293     if (match[i].start != -1) {
294       SILC_LOG_DEBUG(("Match start %d", match[i].start));
295       sub = silc_memdup(string + match[i].start, match[i].end - 
296                         match[i].start);
297       SILC_LOG_DEBUG(("Match substring '%s'", sub));
298       silc_free(sub);
299     }
300   }
301
302   silc_regex_free(&reg);
303
304   regex = "((a)(b))";
305   SILC_LOG_DEBUG(("Regex %s", regex));
306   if (!silc_regex_compile(&reg, regex, 0))
307     goto err;
308
309   string = "ab";
310   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
311   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
312     goto err;
313
314   for (i = 0; i < num_match; i++) {
315     if (match[i].start != -1) {
316       SILC_LOG_DEBUG(("Match start %d", match[i].start));
317       sub = silc_memdup(string + match[i].start, match[i].end - 
318                         match[i].start);
319       SILC_LOG_DEBUG(("Match substring '%s'", sub));
320       silc_free(sub);
321     }
322   }
323
324   silc_regex_free(&reg);
325
326   regex = "^a";
327   SILC_LOG_DEBUG(("Regex %s", regex));
328   if (!silc_regex_compile(&reg, regex, 0))
329     goto err;
330
331   string = "a";
332   SILC_LOG_DEBUG(("Test NOTBOL flag", string));
333   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
334                        SILC_REGEX_NOTBOL))
335     goto err;
336   if (silc_errno != SILC_ERR_NOT_FOUND)
337     goto err;
338   SILC_LOG_DEBUG(("Did not match (OK)"));
339
340   silc_regex_free(&reg);
341
342   regex = "a$";
343   SILC_LOG_DEBUG(("Regex %s", regex));
344   if (!silc_regex_compile(&reg, regex, 0))
345     goto err;
346
347   string = "a";
348   SILC_LOG_DEBUG(("Test NOTEOL flag", string));
349   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
350                        SILC_REGEX_NOTEOL))
351     goto err;
352   if (silc_errno != SILC_ERR_NOT_FOUND)
353     goto err;
354   SILC_LOG_DEBUG(("Did not match (OK)"));
355
356   silc_regex_free(&reg);
357
358   success = TRUE;
359
360  err:
361   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
362   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
363
364   return success;
365 }
366