0e7f4527125406c92323ec375721b69a91cec1cf
[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 = "^a{2,3}$";
188   SILC_LOG_DEBUG(("Regex %s", regex));
189   string = "aaa";
190   SILC_LOG_DEBUG(("Match %s", string));
191   if (!silc_regex(string, regex, &bmatch, NULL))
192     goto err;
193   silc_file_write(1, silc_buffer_data(&bmatch), silc_buffer_len(&bmatch));
194   fflush(stdout);
195
196   regex = "(H..).(o..)";
197   SILC_LOG_DEBUG(("Regex %s", regex));
198   if (!silc_regex_compile(&reg, regex, 0))
199     goto err;
200
201   string = "Hello World";
202   SILC_LOG_DEBUG(("Match %s", string));
203   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
204     goto err;
205   for (i = 0; i < num_match; i++) {
206     if (match[i].start != -1) {
207       SILC_LOG_DEBUG(("Match start %d, end %d", match[i].start,
208                       match[i].end));
209       sub = silc_memdup(string + match[i].start, match[i].end - 
210                         match[i].start);
211       SILC_LOG_DEBUG(("Match substring '%s'", sub));
212       silc_free(sub);
213     }
214   }
215
216   silc_regex_free(&reg);
217
218   regex = "foo[0-9]*";
219   SILC_LOG_DEBUG(("Regex %s", regex));
220   if (!silc_regex_compile(&reg, regex, 0))
221     goto err;
222
223   string = "foo";
224   SILC_LOG_DEBUG(("Match %s", string));
225   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
226     goto err;
227
228   string = "foo20";
229   SILC_LOG_DEBUG(("Match %s", string));
230   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
231     goto err;
232
233   string = "foo20, bar, foo100, foo";
234   SILC_LOG_DEBUG(("Match all substrings in %s", string));
235   while (silc_regex_match(&reg, string, strlen(string), 1, match, 0)) {
236     SILC_LOG_DEBUG(("Match start %d", match[0].start));
237     sub = silc_memdup(string + match[0].start, match[0].end - match[0].start);
238     SILC_LOG_DEBUG(("Match substring '%s'", sub));
239     silc_free(sub);
240     string += match[0].end;
241   }
242
243   string = "foo20, bar, foo100, Foo, foo0";
244   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
245   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
246     goto err;
247
248   for (i = 0; i < num_match; i++) {
249     if (match[i].start != -1) {
250       SILC_LOG_DEBUG(("Match start %d", match[i].start));
251       sub = silc_memdup(string + match[i].start, match[i].end - 
252                         match[i].start);
253       SILC_LOG_DEBUG(("Match substring '%s'", sub));
254       silc_free(sub);
255     }
256   }
257
258   silc_regex_free(&reg);
259
260   regex = "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)";
261   SILC_LOG_DEBUG(("Regex %s", regex));
262   if (!silc_regex_compile(&reg, regex, 0))
263     goto err;
264
265   string = "http://silcnet.org:443/foobar/pelle.html";
266   SILC_LOG_DEBUG(("Parse URI"));
267   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
268     goto err;
269
270   for (i = 0; i < num_match; i++) {
271     if (match[i].start != -1) {
272       SILC_LOG_DEBUG(("Match start %d", match[i].start));
273       sub = silc_memdup(string + match[i].start, match[i].end - 
274                         match[i].start);
275       SILC_LOG_DEBUG(("Match substring '%s'", sub));
276       silc_free(sub);
277     }
278   }
279
280   string = "http://silcnet.org/";
281   SILC_LOG_DEBUG(("Parse URI"));
282   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
283     goto err;
284
285   for (i = 0; i < num_match; i++) {
286     if (match[i].start != -1) {
287       SILC_LOG_DEBUG(("Match start %d", match[i].start));
288       sub = silc_memdup(string + match[i].start, match[i].end - 
289                         match[i].start);
290       SILC_LOG_DEBUG(("Match substring '%s'", sub));
291       silc_free(sub);
292     }
293   }
294
295   silc_regex_free(&reg);
296
297   regex = "((a)(b))";
298   SILC_LOG_DEBUG(("Regex %s", regex));
299   if (!silc_regex_compile(&reg, regex, 0))
300     goto err;
301
302   string = "ab";
303   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
304   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
305     goto err;
306
307   for (i = 0; i < num_match; i++) {
308     if (match[i].start != -1) {
309       SILC_LOG_DEBUG(("Match start %d", match[i].start));
310       sub = silc_memdup(string + match[i].start, match[i].end - 
311                         match[i].start);
312       SILC_LOG_DEBUG(("Match substring '%s'", sub));
313       silc_free(sub);
314     }
315   }
316
317   silc_regex_free(&reg);
318
319   regex = "^a";
320   SILC_LOG_DEBUG(("Regex %s", regex));
321   if (!silc_regex_compile(&reg, regex, 0))
322     goto err;
323
324   string = "a";
325   SILC_LOG_DEBUG(("Test NOTBOL flag", string));
326   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
327                        SILC_REGEX_NOTBOL))
328     goto err;
329   if (silc_errno != SILC_ERR_NOT_FOUND)
330     goto err;
331   SILC_LOG_DEBUG(("Did not match (OK)"));
332
333   silc_regex_free(&reg);
334
335   regex = "a$";
336   SILC_LOG_DEBUG(("Regex %s", regex));
337   if (!silc_regex_compile(&reg, regex, 0))
338     goto err;
339
340   string = "a";
341   SILC_LOG_DEBUG(("Test NOTEOL flag", string));
342   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
343                        SILC_REGEX_NOTEOL))
344     goto err;
345   if (silc_errno != SILC_ERR_NOT_FOUND)
346     goto err;
347   SILC_LOG_DEBUG(("Did not match (OK)"));
348
349   silc_regex_free(&reg);
350
351   success = TRUE;
352
353  err:
354   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
355   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
356
357   return success;
358 }
359