GNUnet 0.28.0-dev.3-20-gf1136b0b8
 
Loading...
Searching...
No Matches
regex_test_random.c File Reference
#include "platform.h"
#include "regex_test_lib.h"
#include "gnunet_util_lib.h"
#include "regex_internal.h"
Include dependency graph for regex_test_random.c:

Go to the source code of this file.

Functions

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 that will be matched by the generated regex.
 
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 expression.
 

Function Documentation

◆ get_random_literal()

static char get_random_literal ( )
static

Get a (pseudo) random valid literal for building a regular expression.

Returns
random valid literal

Definition at line 37 of file regex_test_random.c.

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}
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
#define ALLOWED_LITERALS
char array of literals that are allowed inside a regex (apart from the operators)

References ALLOWED_LITERALS, and GNUNET_CRYPTO_random_u32().

Referenced by REGEX_TEST_generate_random_regex(), and REGEX_TEST_generate_random_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_TEST_generate_random_regex()

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 that will be matched by the generated regex.

The returned regex needs to be freed.

Parameters
rx_lengthlength of the random regex.
matching_str(optional) pointer to a string that will contain a string that will be matched by the generated regex, if 'matching_str' pointer was not NULL. Make sure you allocated at least rx_length+1 bytes for this string.
Returns
NULL if 'rx_length' is 0, a random regex of length 'rx_length', which needs to be freed, otherwise.

Definition at line 63 of file regex_test_random.c.

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}
#define GNUNET_malloc(size)
Wrapper around malloc.
static char get_random_literal()
Get a (pseudo) random valid literal for building a regular expression.

References get_random_literal(), GNUNET_CRYPTO_random_u32(), and GNUNET_malloc.

Here is the call graph for this function:

◆ REGEX_TEST_generate_random_string()

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 expression.

The string might be 0 chars long but is garantueed to be shorter or equal to 'max_len'.

Parameters
max_lenmaximum length of the string that should be generated.
Returns
random string that needs to be freed.

Definition at line 154 of file regex_test_random.c.

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}
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
const char * str
Definition time.c:1252

References get_random_literal(), GNUNET_CRYPTO_random_u32(), GNUNET_malloc, GNUNET_strdup, and str.

Here is the call graph for this function: