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, 58 insertions, 57 deletions
diff --git a/src/regex/regex_test_random.c b/src/regex/regex_test_random.c
index 9a1e9665a..29b5315db 100644
--- a/src/regex/regex_test_random.c
+++ b/src/regex/regex_test_random.c
@@ -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,59 +80,60 @@ 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 {
94 last_was_op = 1;
95 rx_op = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4);
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
89 { 120 {
90 char_op_switch = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2); 121 current_char = get_random_literal ();
91 122 last_was_op = 0;
92 if (0 == char_op_switch && !last_was_op)
93 {
94 last_was_op = 1;
95 rx_op = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 4);
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 current_char != '|'))
128 {
129 *matching_strp = current_char;
130 matching_strp++;
131 }
132
133 *rx_p = current_char;
134 rx_p++;
135 } 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 }
136 *rx_p = '\0'; 137 *rx_p = '\0';
137 if (NULL != matching_strp) 138 if (NULL != matching_strp)
138 *matching_strp = '\0'; 139 *matching_strp = '\0';
@@ -151,22 +152,22 @@ REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str)
151 * @return random string that needs to be freed. 152 * @return random string that needs to be freed.
152 */ 153 */
153char * 154char *
154REGEX_TEST_generate_random_string(size_t max_len) 155REGEX_TEST_generate_random_string (size_t max_len)
155{ 156{
156 unsigned int i; 157 unsigned int i;
157 char *str; 158 char *str;
158 size_t len; 159 size_t len;
159 160
160 if (1 > max_len) 161 if (1 > max_len)
161 return GNUNET_strdup(""); 162 return GNUNET_strdup ("");
162 163
163 len = (size_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, max_len); 164 len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len);
164 str = GNUNET_malloc(len + 1); 165 str = GNUNET_malloc (len + 1);
165 166
166 for (i = 0; i < len; i++) 167 for (i = 0; i < len; i++)
167 { 168 {
168 str[i] = get_random_literal(); 169 str[i] = get_random_literal ();
169 } 170 }
170 171
171 str[i] = '\0'; 172 str[i] = '\0';
172 173