98d354bdb897deafe49a22edf70b39ada9056cbe
[oweals/gnunet.git] / src / regex / test_regex_eval_api.c
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file regex/test_regex_eval_api.c
22  * @brief test for regex.c
23  * @author Maximilian Szengel
24  */
25 #include <regex.h>
26 #include <time.h>
27 #include "platform.h"
28 #include "gnunet_regex_lib.h"
29 #include "regex_internal.h"
30
31 enum Match_Result
32 {
33   match = 0,
34   nomatch = 1
35 };
36
37 struct Regex_String_Pair
38 {
39   char *regex;
40   int string_count;
41   char *strings[20];
42   enum Match_Result expected_results[20];
43 };
44
45
46 /**
47  * Random regex test. Generate a random regex as well as 'str_count' strings to
48  * match it against. Will match using GNUNET_REGEX implementation and compare
49  * the result to glibc regex result. 'rx_length' has to be smaller then
50  * 'max_str_len'.
51  *
52  * @param rx_length length of the regular expression.
53  * @param max_str_len maximum length of the random strings.
54  * @param str_count number of generated random strings.
55  *
56  * @return 0 on success, non 0 otherwise.
57  */
58 int
59 test_random (unsigned int rx_length, unsigned int max_str_len,
60              unsigned int str_count)
61 {
62   unsigned int i;
63   char *rand_rx;
64   char *matching_str;
65   int eval;
66   int eval_check;
67   int eval_canonical;
68   int eval_canonical_check;
69   struct GNUNET_REGEX_Automaton *dfa;
70   regex_t rx;
71   regmatch_t matchptr[1];
72   char error[200];
73   int result;
74   char *canonical_regex = NULL;
75
76   /* At least one string is needed for matching */
77   GNUNET_assert (str_count > 0);
78   /* The string should be at least as long as the regex itself */
79   GNUNET_assert (max_str_len >= rx_length);
80
81   /* Generate random regex and a string that matches the regex */
82   matching_str = GNUNET_malloc (rx_length + 1);
83   rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, matching_str);
84
85   /* Now match */
86   result = 0;
87   for (i = 0; i < str_count; i++)
88   {
89     if (0 < i)
90     {
91       matching_str = GNUNET_REGEX_generate_random_string (max_str_len);
92     }
93
94     /* Match string using DFA */
95     dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx));
96     if (NULL == dfa)
97     {
98       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
99       GNUNET_free (matching_str);
100       goto error;
101     }
102
103     eval = GNUNET_REGEX_eval (dfa, matching_str);
104     /* save the canonical regex for later comparison */
105     canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
106     GNUNET_REGEX_automaton_destroy (dfa);
107
108     /* Match string using glibc regex */
109     if (0 != regcomp (&rx, rand_rx, REG_EXTENDED))
110     {
111       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112                   "Could not compile regex using regcomp: %s\n", rand_rx);
113       goto error;
114     }
115
116     eval_check = regexec (&rx, matching_str, 1, matchptr, 0);
117     regfree (&rx);
118
119     /* We only want to match the whole string, because that's what our DFA does,
120      * too. */
121     if (eval_check == 0 &&
122         (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str)))
123       eval_check = 1;
124
125     /* Match canonical regex */
126     dfa =
127         GNUNET_REGEX_construct_dfa (canonical_regex, strlen (canonical_regex));
128     if (NULL == dfa)
129     {
130       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
131       goto error;
132     }
133
134     eval_canonical = GNUNET_REGEX_eval (dfa, matching_str);
135     GNUNET_REGEX_automaton_destroy (dfa);
136
137     if (0 != regcomp (&rx, canonical_regex, REG_EXTENDED))
138     {
139       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140                   "Could not compile regex using regcomp: %s\n",
141                   canonical_regex);
142       goto error;
143     }
144
145     eval_canonical_check = regexec (&rx, matching_str, 1, matchptr, 0);
146     regfree (&rx);
147
148     /* We only want to match the whole string, because that's what our DFA does,
149      * too. */
150     if (eval_canonical_check == 0 &&
151         (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str)))
152       eval_canonical_check = 1;
153
154     /* compare results */
155     if (eval_check != eval || eval_canonical != eval_canonical_check)
156     {
157       regerror (eval_check, &rx, error, sizeof error);
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected result:\nregex: %s\ncanonical_regex: %s\n\
159                    string: %s\ngnunet regex: %i\nglibc regex: %i\n\
160                    canonical regex: %i\ncanonical regex glibc: %i\n\
161                    glibc error: %s\n\n", rand_rx, canonical_regex, matching_str,
162                   eval, eval_check, eval_canonical, eval_canonical_check, error);
163       result += 1;
164     }
165     GNUNET_free (canonical_regex);
166     GNUNET_free (matching_str);
167   }
168
169   GNUNET_free (rand_rx);
170
171   return result;
172
173 error:
174   GNUNET_free_non_null (matching_str);
175   GNUNET_free_non_null (rand_rx);
176   GNUNET_free_non_null (canonical_regex);
177   return -1;
178 }
179
180 /**
181  * Automaton test that compares the result of matching regular expression 'rx'
182  * with the strings and expected results in 'rxstr' with the result of matching
183  * the same strings with glibc regex.
184  *
185  * @param a automaton.
186  * @param rx compiled glibc regex.
187  * @param rxstr regular expression and strings with expected results to
188  *              match against.
189  *
190  * @return 0 on successfull, non 0 otherwise
191  */
192 int
193 test_automaton (struct GNUNET_REGEX_Automaton *a, regex_t * rx,
194                 struct Regex_String_Pair *rxstr)
195 {
196   int result;
197   int eval;
198   int eval_check;
199   char error[200];
200   regmatch_t matchptr[1];
201   int i;
202
203   if (NULL == a)
204   {
205     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n");
206     return 1;
207   }
208
209   result = 0;
210
211   for (i = 0; i < rxstr->string_count; i++)
212   {
213     eval = GNUNET_REGEX_eval (a, rxstr->strings[i]);
214     eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0);
215
216     /* We only want to match the whole string, because that's what our DFA does,
217      * too. */
218     if (eval_check == 0 &&
219         (matchptr[0].rm_so != 0 ||
220          matchptr[0].rm_eo != strlen (rxstr->strings[i])))
221       eval_check = 1;
222
223     if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check))
224         || (rxstr->expected_results[i] == nomatch &&
225             (0 == eval || 0 == eval_check)))
226     {
227       result = 1;
228       regerror (eval_check, rx, error, sizeof error);
229       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                   "Unexpected result:\nregex: %s\ncanonical_regex: %s\n"
231                   "string: %s\nexpected result: %i\n"
232                   "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\n"
233                   "rm_so: %i\nrm_eo: %i\n\n", rxstr->regex,
234                   GNUNET_REGEX_get_canonical_regex (a), rxstr->strings[i],
235                   rxstr->expected_results[i], eval, eval_check, error,
236                   matchptr[0].rm_so, matchptr[0].rm_eo);
237     }
238   }
239   return result;
240 }
241
242 int
243 main (int argc, char *argv[])
244 {
245   GNUNET_log_setup ("test-regex",
246 #if VERBOSE
247                     "DEBUG",
248 #else
249                     "WARNING",
250 #endif
251                     NULL);
252
253   struct GNUNET_REGEX_Automaton *a;
254   regex_t rx;
255   int i;
256   int check_nfa;
257   int check_dfa;
258   int check_rand;
259   char *check_proof;
260
261   struct Regex_String_Pair rxstr[18] = {
262     {"ab?(abcd)?", 5,
263      {"ababcd", "abab", "aabcd", "a", "abb"},
264      {match, nomatch, match, match, nomatch}},
265     {"ab(c|d)+c*(a(b|c)d)+", 5,
266      {"abcdcdcdcdddddabd", "abcd",
267       "abcddddddccccccccccccccccccccccccabdacdabd",
268       "abccccca", "abcdcdcdccdabdabd"},
269      {match, nomatch, match, nomatch, match}},
270     {"ab+c*(a(bx|c)d)+", 5,
271      {"abcdcdcdcdddddabd", "abcd",
272       "abcddddddccccccccccccccccccccccccabdacdabd",
273       "abccccca", "abcdcdcdccdabdabd"},
274      {nomatch, nomatch, nomatch, nomatch, nomatch}},
275     {"a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1,
276      {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
277      {nomatch}},
278     {"k|a+X*y+c|Q*e|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*g|N+V|t+L|P*j*3*9+X*h*J|J*6|b|E*i*f*R+S|Z|R|Y*Z|g*", 1,
279      {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
280      {nomatch}},
281     {"F?W+m+2*6*c*s|P?U?a|B|y*i+t+A|V|6*C*7*e?Z*n*i|J?5+g?W*V?7*j?p?1|r?B?C+E+3+6*i+W*P?K?0|D+7?y*m+3?g?K?", 1,
282      {"osfjsodfonONONOnosndfsdnfsd"},
283      {nomatch}},
284     {"V|M*o?x*p*d+h+b|E*m?h?Y*E*O?W*W*P+o?Z+H*M|I*q+C*a+5?5*9|b?z|G*y*k?R|p+u|8*h?B+l*H|e|L*O|1|F?v*0?5|C+", 1,
285      {"VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C"},
286      {nomatch}},
287     {"(bla)*", 8,
288      {"", "bla", "blabla", "bl", "la", "b", "l", "a"},
289      {match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
290     {"ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8,
291      {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b",
292       "l",
293       "a"},
294      {nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
295     {"a|aa*a", 6,
296      {"", "a", "aa", "aaa", "aaaa", "aaaaa"},
297      {nomatch, match, match, match, match, match}},
298     {"ab(c|d)+c*(a(b|c)+d)+(bla)+", 1,
299      {"abcabdblaacdbla"},
300      {nomatch}},
301     {"(ac|b)+", 8,
302      {"b", "bb", "ac", "", "acb", "bacbacac", "acacac", "abc"},
303      {match, match, match, nomatch, match, match, match, nomatch}},
304     {"(ab|c)+", 7,
305      {"", "ab", "c", "abc", "ababcc", "acc", "abac"},
306      {nomatch, match, match, match, match, nomatch, nomatch}},
307     {"((j|2j)K|(j|2j)AK|(j|2j)(D|e|(j|2j)A(D|e))D*K)", 1,
308      {"", "2j2jADK", "j2jADK"},
309      {nomatch, match, match}},
310     {"((j|2j)K|(j|2j)(D|e|((j|2j)j|(j|2j)2j)A(D|e))D*K|(j|2j)AK)", 2,
311      {"", "2j2jjADK", "j2jADK"},
312      {nomatch, match, match}},
313     {"ab(c|d)+c*(a(b|c)d)+", 1,
314      {"abacd"},
315      {nomatch}},
316     {"d|5kl", 1,
317      {"d5kl"},
318      {nomatch}},
319     {"a()b", 1,
320      {"ab"},
321      {match}}
322   };
323
324   check_nfa = 0;
325   check_dfa = 0;
326   check_rand = 0;
327
328   for (i = 0; i < 18; i++)
329   {
330     if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
331     {
332       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333                   "Could not compile regex using regcomp()\n");
334       return 1;
335     }
336
337     /* NFA test */
338     a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
339     check_nfa += test_automaton (a, &rx, &rxstr[i]);
340     GNUNET_REGEX_automaton_destroy (a);
341
342     /* DFA test */
343     a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
344     check_dfa += test_automaton (a, &rx, &rxstr[i]);
345     check_proof = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (a));
346     GNUNET_REGEX_automaton_destroy (a);
347
348     a = GNUNET_REGEX_construct_dfa (check_proof, strlen (check_proof));
349     check_dfa += test_automaton (a, &rx, &rxstr[i]);
350     GNUNET_REGEX_automaton_destroy (a);
351     if (0 != check_dfa)
352       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof);
353     GNUNET_free_non_null (check_proof);
354
355     regfree (&rx);
356   }
357
358   /* Random tests */
359   srand (time (NULL));
360   for (i = 0; i < 20; i++)
361     check_rand += test_random (50, 60, 10);
362
363   return check_nfa + check_dfa + check_rand;
364 }