GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
regex_test_random.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
25#include "platform.h"
26#include "regex_test_lib.h"
27#include "gnunet_util_lib.h"
28#include "regex_internal.h"
29
30
36static char
38{
39 uint32_t ridx;
40
41 ridx =
42 GNUNET_CRYPTO_random_u32 ((uint32_t) strlen (ALLOWED_LITERALS));
43
44 return ALLOWED_LITERALS[ridx];
45}
46
47
62char *
63REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
64{
65 char *rx;
66 char *rx_p;
67 char *matching_strp;
68 unsigned int i;
69 unsigned int char_op_switch;
70 unsigned int last_was_op;
71 int rx_op;
72 char current_char;
73
74 if (0 == rx_length)
75 return NULL;
76
77 if (NULL != matching_str)
78 matching_strp = matching_str;
79 else
80 matching_strp = NULL;
81
82 rx = GNUNET_malloc (rx_length + 1);
83 rx_p = rx;
84 current_char = 0;
85 last_was_op = 1;
86
87 for (i = 0; i < rx_length; i++)
88 {
89 char_op_switch = GNUNET_CRYPTO_random_u32 (2);
90
91 if ((0 == char_op_switch) && ! last_was_op)
92 {
93 last_was_op = 1;
94 rx_op = GNUNET_CRYPTO_random_u32 (4);
95
96 switch (rx_op)
97 {
98 case 0:
99 current_char = '+';
100 break;
101
102 case 1:
103 current_char = '*';
104 break;
105
106 case 2:
107 current_char = '?';
108 break;
109
110 case 3:
111 if (i < rx_length - 1) /* '|' cannot be at the end */
112 current_char = '|';
113 else
114 current_char = get_random_literal ();
115 break;
116 }
117 }
118 else
119 {
120 current_char = get_random_literal ();
121 last_was_op = 0;
122 }
123
124 if ((NULL != matching_strp) &&
125 ((current_char != '+') && (current_char != '*') && (current_char !=
126 '?') &&
127 (current_char != '|') ))
128 {
129 *matching_strp = current_char;
130 matching_strp++;
131 }
132
133 *rx_p = current_char;
134 rx_p++;
135 }
136 *rx_p = '\0';
137 if (NULL != matching_strp)
138 *matching_strp = '\0';
139
140 return rx;
141}
142
143
153char *
155{
156 unsigned int i;
157 char *str;
158 size_t len;
159
160 if (1 > max_len)
161 return GNUNET_strdup ("");
162
163 len = (size_t) GNUNET_CRYPTO_random_u32 (max_len);
164 str = GNUNET_malloc (len + 1);
165
166 for (i = 0; i < len; i++)
167 {
168 str[i] = get_random_literal ();
169 }
170
171 str[i] = '\0';
172
173 return str;
174}
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define ALLOWED_LITERALS
char array of literals that are allowed inside a regex (apart from the operators)
static char get_random_literal()
Get a (pseudo) random valid literal for building a regular expression.
char * REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str)
Generate a (pseudo) random regular expression of length 'rx_length', as well as a (optional) string t...
char * REGEX_TEST_generate_random_string(size_t max_len)
Generate a random string of maximum length 'max_len' that only contains literals allowed in a regular...
const char * str
Definition time.c:1252