b650f6e6e8bd706418dd6cc0aaa8f60550566ffe
[silc.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 = "^a{0}$";
22   SILC_LOG_DEBUG(("Regex %s", regex));
23   string = "";
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 = "^a{0,}$";
30   SILC_LOG_DEBUG(("Regex %s", regex));
31   string = "bbbb";
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 = "aaaaaaaaa";
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,0}$";
46   SILC_LOG_DEBUG(("Regex %s", regex));
47   string = "a";
48   SILC_LOG_DEBUG(("DO NOT Match %s", string));
49   if (silc_regex(string, regex, &bmatch, NULL))
50     goto err;
51
52   regex = "^a{3}$";
53   SILC_LOG_DEBUG(("Regex %s", regex));
54   string = "aaa";
55   SILC_LOG_DEBUG(("Match %s", string));
56   if (!silc_regex(string, regex, &bmatch, NULL))
57     goto err;
58   silc_buffer_printf(&bmatch, TRUE);
59
60   regex = "^a{3}$";
61   SILC_LOG_DEBUG(("Regex %s", regex));
62   string = "aaaa";
63   SILC_LOG_DEBUG(("DO NOT Match %s", string));
64   if (silc_regex(string, regex, &bmatch, NULL))
65     goto err;
66
67   regex = "^a{3,5}$";
68   SILC_LOG_DEBUG(("Regex %s", regex));
69   string = "aaa";
70   SILC_LOG_DEBUG(("Match %s", string));
71   if (!silc_regex(string, regex, &bmatch, NULL))
72     goto err;
73   silc_buffer_printf(&bmatch, TRUE);
74
75   regex = "^a{3,5}$";
76   SILC_LOG_DEBUG(("Regex %s", regex));
77   string = "aaaa";
78   SILC_LOG_DEBUG(("Match %s", string));
79   if (!silc_regex(string, regex, &bmatch, NULL))
80     goto err;
81   silc_buffer_printf(&bmatch, TRUE);
82
83   regex = "^a{3,5}$";
84   SILC_LOG_DEBUG(("Regex %s", regex));
85   string = "aaaaaa";
86   SILC_LOG_DEBUG(("DO NOT Match %s", string));
87   if (silc_regex(string, regex, &bmatch, NULL))
88     goto err;
89
90   regex = "^a{3,}$";
91   SILC_LOG_DEBUG(("Regex %s", regex));
92   string = "aaa";
93   SILC_LOG_DEBUG(("Match %s", string));
94   if (!silc_regex(string, regex, &bmatch, NULL))
95     goto err;
96   silc_buffer_printf(&bmatch, TRUE);
97
98   regex = "^a{3,}$";
99   SILC_LOG_DEBUG(("Regex %s", regex));
100   string = "aaaaaaaaaaaaa";
101   SILC_LOG_DEBUG(("Match %s", string));
102   if (!silc_regex(string, regex, &bmatch, NULL))
103     goto err;
104   silc_buffer_printf(&bmatch, TRUE);
105
106   regex = "^a{3,}$";
107   SILC_LOG_DEBUG(("Regex %s", regex));
108   string = "aa";
109   SILC_LOG_DEBUG(("DO NOT Match %s", string));
110   if (silc_regex(string, regex, &bmatch, NULL))
111     goto err;
112
113
114   regex = "a*b";
115   SILC_LOG_DEBUG(("Regex %s", regex));
116   string = "b";
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*b";
123   SILC_LOG_DEBUG(("Regex %s", regex));
124   string = "ab";
125   SILC_LOG_DEBUG(("Match %s", string));
126   if (!silc_regex(string, regex, &bmatch, NULL))
127     goto err;
128   silc_buffer_printf(&bmatch, TRUE);
129
130   regex = "a*b";
131   SILC_LOG_DEBUG(("Regex %s", regex));
132   string = "aaaab";
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
139   regex = "a+b";
140   SILC_LOG_DEBUG(("Regex %s", regex));
141   string = "ab";
142   SILC_LOG_DEBUG(("Match %s", string));
143   if (!silc_regex(string, regex, &bmatch, NULL))
144     goto err;
145   silc_buffer_printf(&bmatch, TRUE);
146
147   regex = "a+b";
148   SILC_LOG_DEBUG(("Regex %s", regex));
149   string = "aaaab";
150   SILC_LOG_DEBUG(("Match %s", string));
151   if (!silc_regex(string, regex, &bmatch, NULL))
152     goto err;
153   silc_buffer_printf(&bmatch, TRUE);
154
155   regex = "a+b";
156   SILC_LOG_DEBUG(("Regex %s", regex));
157   string = "b";
158   SILC_LOG_DEBUG(("DO NOT Match %s", string));
159   if (silc_regex(string, regex, &bmatch, NULL))
160     goto err;
161
162
163   regex = "ca?b";
164   SILC_LOG_DEBUG(("Regex %s", regex));
165   string = "cb";
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 = "ca?b";
172   SILC_LOG_DEBUG(("Regex %s", regex));
173   string = "cab";
174   SILC_LOG_DEBUG(("Match %s", string));
175   if (!silc_regex(string, regex, &bmatch, NULL))
176     goto err;
177   silc_buffer_printf(&bmatch, TRUE);
178
179   regex = "ca?b";
180   SILC_LOG_DEBUG(("Regex %s", regex));
181   string = "caab";
182   SILC_LOG_DEBUG(("DO NOT Match %s", string));
183   if (silc_regex(string, regex, &bmatch, NULL))
184     goto err;
185
186
187   regex = "(H..).(o..)";
188   SILC_LOG_DEBUG(("Regex %s", regex));
189   if (!silc_regex_compile(&reg, regex, 0))
190     goto err;
191
192   string = "Hello World";
193   SILC_LOG_DEBUG(("Match %s", string));
194   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
195     goto err;
196   for (i = 0; i < num_match; i++) {
197     if (match[i].start != -1) {
198       SILC_LOG_DEBUG(("Match start %d, end %d", match[i].start,
199                       match[i].end));
200       sub = silc_memdup(string + match[i].start, match[i].end - 
201                         match[i].start);
202       SILC_LOG_DEBUG(("Match substring '%s'", sub));
203       silc_free(sub);
204     }
205   }
206
207   silc_regex_free(&reg);
208
209   regex = "foo[0-9]*";
210   SILC_LOG_DEBUG(("Regex %s", regex));
211   if (!silc_regex_compile(&reg, regex, 0))
212     goto err;
213
214   string = "foo";
215   SILC_LOG_DEBUG(("Match %s", string));
216   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
217     goto err;
218
219   string = "foo20";
220   SILC_LOG_DEBUG(("Match %s", string));
221   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
222     goto err;
223
224   string = "foo20, bar, foo100, foo";
225   SILC_LOG_DEBUG(("Match all substrings in %s", string));
226   while (silc_regex_match(&reg, string, strlen(string), 1, match, 0)) {
227     SILC_LOG_DEBUG(("Match start %d", match[0].start));
228     sub = silc_memdup(string + match[0].start, match[0].end - match[0].start);
229     SILC_LOG_DEBUG(("Match substring '%s'", sub));
230     silc_free(sub);
231     string += match[0].end;
232   }
233
234   string = "foo20, bar, foo100, Foo, foo0";
235   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
236   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
237     goto err;
238
239   for (i = 0; i < num_match; i++) {
240     if (match[i].start != -1) {
241       SILC_LOG_DEBUG(("Match start %d", match[i].start));
242       sub = silc_memdup(string + match[i].start, match[i].end - 
243                         match[i].start);
244       SILC_LOG_DEBUG(("Match substring '%s'", sub));
245       silc_free(sub);
246     }
247   }
248
249   silc_regex_free(&reg);
250
251   regex = "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)";
252   SILC_LOG_DEBUG(("Regex %s", regex));
253   if (!silc_regex_compile(&reg, regex, 0))
254     goto err;
255
256   string = "http://silcnet.org:443/foobar/pelle.html";
257   SILC_LOG_DEBUG(("Parse URI"));
258   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
259     goto err;
260
261   for (i = 0; i < num_match; i++) {
262     if (match[i].start != -1) {
263       SILC_LOG_DEBUG(("Match start %d", match[i].start));
264       sub = silc_memdup(string + match[i].start, match[i].end - 
265                         match[i].start);
266       SILC_LOG_DEBUG(("Match substring '%s'", sub));
267       silc_free(sub);
268     }
269   }
270
271   string = "http://silcnet.org/";
272   SILC_LOG_DEBUG(("Parse URI"));
273   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
274     goto err;
275
276   for (i = 0; i < num_match; i++) {
277     if (match[i].start != -1) {
278       SILC_LOG_DEBUG(("Match start %d", match[i].start));
279       sub = silc_memdup(string + match[i].start, match[i].end - 
280                         match[i].start);
281       SILC_LOG_DEBUG(("Match substring '%s'", sub));
282       silc_free(sub);
283     }
284   }
285
286   silc_regex_free(&reg);
287
288   regex = "((a)(b))";
289   SILC_LOG_DEBUG(("Regex %s", regex));
290   if (!silc_regex_compile(&reg, regex, 0))
291     goto err;
292
293   string = "ab";
294   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
295   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
296     goto err;
297
298   for (i = 0; i < num_match; i++) {
299     if (match[i].start != -1) {
300       SILC_LOG_DEBUG(("Match start %d", match[i].start));
301       sub = silc_memdup(string + match[i].start, match[i].end - 
302                         match[i].start);
303       SILC_LOG_DEBUG(("Match substring '%s'", sub));
304       silc_free(sub);
305     }
306   }
307
308   silc_regex_free(&reg);
309
310   regex = "^a";
311   SILC_LOG_DEBUG(("Regex %s", regex));
312   if (!silc_regex_compile(&reg, regex, 0))
313     goto err;
314
315   string = "a";
316   SILC_LOG_DEBUG(("Test NOTBOL flag", string));
317   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
318                        SILC_REGEX_NOTBOL))
319     goto err;
320   if (silc_errno != SILC_ERR_NOT_FOUND)
321     goto err;
322   SILC_LOG_DEBUG(("Did not match (OK)"));
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 NOTEOL flag", string));
333   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
334                        SILC_REGEX_NOTEOL))
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   success = TRUE;
343
344  err:
345   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
346   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
347
348   return success;
349 }
350