GNUnet 0.22.2
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. More...
 
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. More...
 
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. More...
 

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 =
43 (uint32_t) strlen (ALLOWED_LITERALS));
44
45 return ALLOWED_LITERALS[ridx];
46}
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define ALLOWED_LITERALS
char array of literals that are allowed inside a regex (apart from the operators)

References ALLOWED_LITERALS, GNUNET_CRYPTO_QUALITY_WEAK, 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 64 of file regex_test_random.c.

65{
66 char *rx;
67 char *rx_p;
68 char *matching_strp;
69 unsigned int i;
70 unsigned int char_op_switch;
71 unsigned int last_was_op;
72 int rx_op;
73 char current_char;
74
75 if (0 == rx_length)
76 return NULL;
77
78 if (NULL != matching_str)
79 matching_strp = matching_str;
80 else
81 matching_strp = NULL;
82
83 rx = GNUNET_malloc (rx_length + 1);
84 rx_p = rx;
85 current_char = 0;
86 last_was_op = 1;
87
88 for (i = 0; i < rx_length; i++)
89 {
91
92 if ((0 == char_op_switch) && ! last_was_op)
93 {
94 last_was_op = 1;
96
97 switch (rx_op)
98 {
99 case 0:
100 current_char = '+';
101 break;
102
103 case 1:
104 current_char = '*';
105 break;
106
107 case 2:
108 current_char = '?';
109 break;
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 '?') &&
128 (current_char != '|') ))
129 {
130 *matching_strp = current_char;
131 matching_strp++;
132 }
133
134 *rx_p = current_char;
135 rx_p++;
136 }
137 *rx_p = '\0';
138 if (NULL != matching_strp)
139 *matching_strp = '\0';
140
141 return rx;
142}
#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_QUALITY_WEAK, 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 155 of file regex_test_random.c.

156{
157 unsigned int i;
158 char *str;
159 size_t len;
160
161 if (1 > max_len)
162 return GNUNET_strdup ("");
163
165 str = GNUNET_malloc (len + 1);
166
167 for (i = 0; i < len; i++)
168 {
169 str[i] = get_random_literal ();
170 }
171
172 str[i] = '\0';
173
174 return str;
175}
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

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

Here is the call graph for this function: