]> git.proxmox.com Git - mirror_frr.git/blob - tests/helpers/c/prng.c
Merge branch 'stable/3.0'
[mirror_frr.git] / tests / helpers / c / prng.c
1 /*
2 * Very simple prng to allow for randomized tests with reproducable
3 * results.
4 *
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
7 *
8 * This file is part of Quagga
9 *
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <zebra.h>
26
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "prng.h"
32
33 struct prng
34 {
35 unsigned long long state1;
36 unsigned long long state2;
37 };
38
39 static char
40 prng_bit(struct prng *prng)
41 {
42 prng->state1 *= 2416;
43 prng->state1 += 374441;
44 prng->state1 %= 1771875;
45
46 if (prng->state1 % 2)
47 {
48 prng->state2 *= 84589;
49 prng->state2 += 45989;
50 prng->state2 %= 217728;
51 }
52
53 return prng->state2 % 2;
54 }
55
56 struct prng*
57 prng_new(unsigned long long seed)
58 {
59 struct prng *rv = calloc(sizeof(*rv), 1);
60 assert(rv);
61
62 rv->state1 = rv->state2 = seed;
63
64 return rv;
65 }
66
67 unsigned int
68 prng_rand(struct prng *prng)
69 {
70 unsigned int i, rv = 0;
71
72 for (i = 0; i < 32; i++)
73 {
74 rv |= prng_bit(prng);
75 rv <<= 1;
76 }
77 return rv;
78 }
79
80 const char *
81 prng_fuzz(struct prng *prng,
82 const char *string,
83 const char *charset,
84 unsigned int operations)
85 {
86 static char buf[256];
87 unsigned int charset_len;
88 unsigned int i;
89 unsigned int offset;
90 unsigned int op;
91 unsigned int character;
92
93 assert(strlen(string) < sizeof(buf));
94
95 strncpy(buf, string, sizeof(buf));
96 charset_len = strlen(charset);
97
98 for (i = 0; i < operations; i++)
99 {
100 offset = prng_rand(prng) % strlen(buf);
101 op = prng_rand(prng) % 3;
102
103 switch (op)
104 {
105 case 0:
106 /* replace */
107 character = prng_rand(prng) % charset_len;
108 buf[offset] = charset[character];
109 break;
110 case 1:
111 /* remove */
112 memmove(buf + offset, buf + offset + 1, strlen(buf) - offset);
113 break;
114 case 2:
115 /* insert */
116 assert(strlen(buf) + 1 < sizeof(buf));
117
118 memmove(buf + offset + 1, buf + offset, strlen(buf) + 1 - offset);
119 character = prng_rand(prng) % charset_len;
120 buf[offset] = charset[character];
121 break;
122 }
123 }
124 return buf;
125 }
126
127 void
128 prng_free(struct prng *prng)
129 {
130 free(prng);
131 }