2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file src/regex/regex_test_random.c
22 * @brief functions for creating random regular expressions and strings
23 * @author Maximilian Szengel
26 #include "regex_test_lib.h"
27 #include "gnunet_crypto_lib.h"
28 #include "regex_internal.h"
32 * Get a (pseudo) random valid literal for building a regular expression.
34 * @return random valid literal
42 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
43 (uint32_t) strlen (ALLOWED_LITERALS));
45 return ALLOWED_LITERALS[ridx];
50 * Generate a (pseudo) random regular expression of length 'rx_length', as well
51 * as a (optional) string that will be matched by the generated regex. The
52 * returned regex needs to be freed.
54 * @param rx_length length of the random regex.
55 * @param matching_str (optional) pointer to a string that will contain a string
56 * that will be matched by the generated regex, if
57 * 'matching_str' pointer was not NULL. Make sure you
58 * allocated at least rx_length+1 bytes for this sting.
60 * @return NULL if 'rx_length' is 0, a random regex of length 'rx_length', which
61 * needs to be freed, otherwise.
64 REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
70 unsigned int char_op_switch;
71 unsigned int last_was_op;
78 if (NULL != matching_str)
79 matching_strp = matching_str;
83 rx = GNUNET_malloc (rx_length + 1);
88 for (i = 0; i < rx_length; i++)
90 char_op_switch = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
92 if (0 == char_op_switch && !last_was_op)
95 rx_op = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4);
109 if (i < rx_length - 1) /* '|' cannot be at the end */
112 current_char = get_random_literal ();
118 current_char = get_random_literal ();
122 if (NULL != matching_strp &&
123 (current_char != '+' && current_char != '*' && current_char != '?' &&
124 current_char != '|'))
126 *matching_strp = current_char;
130 *rx_p = current_char;
134 if (NULL != matching_strp)
135 *matching_strp = '\0';
142 * Generate a random string of maximum length 'max_len' that only contains literals allowed
143 * in a regular expression. The string might be 0 chars long but is garantueed
144 * to be shorter or equal to 'max_len'.
146 * @param max_len maximum length of the string that should be generated.
148 * @return random string that needs to be freed.
151 REGEX_TEST_generate_random_string (size_t max_len)
158 return GNUNET_strdup ("");
160 len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len);
161 str = GNUNET_malloc (len + 1);
163 for (i = 0; i < len; i++)
165 str[i] = get_random_literal ();