aboutsummaryrefslogtreecommitdiff
path: root/src/regex/regex_test_random.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regex/regex_test_random.c')
-rw-r--r--src/regex/regex_test_random.c115
1 files changed, 59 insertions, 56 deletions
diff --git a/src/regex/regex_test_random.c b/src/regex/regex_test_random.c
index 7fcdd1e63..9a1e9665a 100644
--- a/src/regex/regex_test_random.c
+++ b/src/regex/regex_test_random.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file src/regex/regex_test_random.c 21 * @file src/regex/regex_test_random.c
22 * @brief functions for creating random regular expressions and strings 22 * @brief functions for creating random regular expressions and strings
@@ -34,13 +34,13 @@
34 * @return random valid literal 34 * @return random valid literal
35 */ 35 */
36static char 36static char
37get_random_literal () 37get_random_literal()
38{ 38{
39 uint32_t ridx; 39 uint32_t ridx;
40 40
41 ridx = 41 ridx =
42 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 42 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
43 (uint32_t) strlen (ALLOWED_LITERALS)); 43 (uint32_t)strlen(ALLOWED_LITERALS));
44 44
45 return ALLOWED_LITERALS[ridx]; 45 return ALLOWED_LITERALS[ridx];
46} 46}
@@ -61,7 +61,7 @@ get_random_literal ()
61 * needs to be freed, otherwise. 61 * needs to be freed, otherwise.
62 */ 62 */
63char * 63char *
64REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str) 64REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str)
65{ 65{
66 char *rx; 66 char *rx;
67 char *rx_p; 67 char *rx_p;
@@ -80,56 +80,59 @@ REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
80 else 80 else
81 matching_strp = NULL; 81 matching_strp = NULL;
82 82
83 rx = GNUNET_malloc (rx_length + 1); 83 rx = GNUNET_malloc(rx_length + 1);
84 rx_p = rx; 84 rx_p = rx;
85 current_char = 0; 85 current_char = 0;
86 last_was_op = 1; 86 last_was_op = 1;
87 87
88 for (i = 0; i < rx_length; i++) 88 for (i = 0; i < rx_length; i++)
89 {
90 char_op_switch = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
91
92 if (0 == char_op_switch && !last_was_op)
93 { 89 {
94 last_was_op = 1; 90 char_op_switch = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
95 rx_op = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4); 91
96 92 if (0 == char_op_switch && !last_was_op)
97 switch (rx_op) 93 {
98 { 94 last_was_op = 1;
99 case 0: 95 rx_op = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 4);
100 current_char = '+'; 96
101 break; 97 switch (rx_op)
102 case 1: 98 {
103 current_char = '*'; 99 case 0:
104 break; 100 current_char = '+';
105 case 2: 101 break;
106 current_char = '?'; 102
107 break; 103 case 1:
108 case 3: 104 current_char = '*';
109 if (i < rx_length - 1) /* '|' cannot be at the end */ 105 break;
110 current_char = '|'; 106
111 else 107 case 2:
112 current_char = get_random_literal (); 108 current_char = '?';
113 break; 109 break;
114 } 110
111 case 3:
112 if (i < rx_length - 1) /* '|' cannot be at the end */
113 current_char = '|';
114 else
115 current_char = get_random_literal();
116 break;
117 }
118 }
119 else
120 {
121 current_char = get_random_literal();
122 last_was_op = 0;
123 }
124
125 if (NULL != matching_strp &&
126 (current_char != '+' && current_char != '*' && current_char != '?' &&
127 current_char != '|'))
128 {
129 *matching_strp = current_char;
130 matching_strp++;
131 }
132
133 *rx_p = current_char;
134 rx_p++;
115 } 135 }
116 else
117 {
118 current_char = get_random_literal ();
119 last_was_op = 0;
120 }
121
122 if (NULL != matching_strp &&
123 (current_char != '+' && current_char != '*' && current_char != '?' &&
124 current_char != '|'))
125 {
126 *matching_strp = current_char;
127 matching_strp++;
128 }
129
130 *rx_p = current_char;
131 rx_p++;
132 }
133 *rx_p = '\0'; 136 *rx_p = '\0';
134 if (NULL != matching_strp) 137 if (NULL != matching_strp)
135 *matching_strp = '\0'; 138 *matching_strp = '\0';
@@ -148,22 +151,22 @@ REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
148 * @return random string that needs to be freed. 151 * @return random string that needs to be freed.
149 */ 152 */
150char * 153char *
151REGEX_TEST_generate_random_string (size_t max_len) 154REGEX_TEST_generate_random_string(size_t max_len)
152{ 155{
153 unsigned int i; 156 unsigned int i;
154 char *str; 157 char *str;
155 size_t len; 158 size_t len;
156 159
157 if (1 > max_len) 160 if (1 > max_len)
158 return GNUNET_strdup (""); 161 return GNUNET_strdup("");
159 162
160 len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len); 163 len = (size_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, max_len);
161 str = GNUNET_malloc (len + 1); 164 str = GNUNET_malloc(len + 1);
162 165
163 for (i = 0; i < len; i++) 166 for (i = 0; i < len; i++)
164 { 167 {
165 str[i] = get_random_literal (); 168 str[i] = get_random_literal();
166 } 169 }
167 170
168 str[i] = '\0'; 171 str[i] = '\0';
169 172