aboutsummaryrefslogtreecommitdiff
path: root/src/regex/regex_test_random.c
blob: 02d59f5dc5332627140ce06710390373ba808ffb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
     This file is part of GNUnet
     Copyright (C) 2012 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @file src/regex/regex_test_random.c
 * @brief functions for creating random regular expressions and strings
 * @author Maximilian Szengel
 */
#include "platform.h"
#include "regex_test_lib.h"
#include "gnunet_crypto_lib.h"
#include "regex_internal.h"


/**
 * Get a (pseudo) random valid literal for building a regular expression.
 *
 * @return random valid literal
 */
static char
get_random_literal ()
{
  uint32_t ridx;

  ridx =
    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
                              (uint32_t) strlen (ALLOWED_LITERALS));

  return ALLOWED_LITERALS[ridx];
}


/**
 * 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.
 *
 * @param rx_length length of the random regex.
 * @param 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 sting.
 *
 * @return NULL if 'rx_length' is 0, a random regex of length 'rx_length', which
 *         needs to be freed, otherwise.
 */
char *
REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
{
  char *rx;
  char *rx_p;
  char *matching_strp;
  unsigned int i;
  unsigned int char_op_switch;
  unsigned int last_was_op;
  int rx_op;
  char current_char;

  if (0 == rx_length)
    return NULL;

  if (NULL != matching_str)
    matching_strp = matching_str;
  else
    matching_strp = NULL;

  rx = GNUNET_malloc (rx_length + 1);
  rx_p = rx;
  current_char = 0;
  last_was_op = 1;

  for (i = 0; i < rx_length; i++)
  {
    char_op_switch = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);

    if ((0 == char_op_switch) && ! last_was_op)
    {
      last_was_op = 1;
      rx_op = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4);

      switch (rx_op)
      {
      case 0:
        current_char = '+';
        break;

      case 1:
        current_char = '*';
        break;

      case 2:
        current_char = '?';
        break;

      case 3:
        if (i < rx_length - 1)       /* '|' cannot be at the end */
          current_char = '|';
        else
          current_char = get_random_literal ();
        break;
      }
    }
    else
    {
      current_char = get_random_literal ();
      last_was_op = 0;
    }

    if ((NULL != matching_strp) &&
        ((current_char != '+') && (current_char != '*') && (current_char !=
                                                            '?') &&
         (current_char != '|') ))
    {
      *matching_strp = current_char;
      matching_strp++;
    }

    *rx_p = current_char;
    rx_p++;
  }
  *rx_p = '\0';
  if (NULL != matching_strp)
    *matching_strp = '\0';

  return rx;
}


/**
 * 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'.
 *
 * @param max_len maximum length of the string that should be generated.
 *
 * @return random string that needs to be freed.
 */
char *
REGEX_TEST_generate_random_string (size_t max_len)
{
  unsigned int i;
  char *str;
  size_t len;

  if (1 > max_len)
    return GNUNET_strdup ("");

  len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len);
  str = GNUNET_malloc (len + 1);

  for (i = 0; i < len; i++)
  {
    str[i] = get_random_literal ();
  }

  str[i] = '\0';

  return str;
}