]> git.proxmox.com Git - mirror_frr.git/blob - tests/helpers/c/prng.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[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 * Copyright (C) 2017 Christian Franke
8 *
9 * This file is part of Quagga
10 *
11 * Quagga is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
14 * later version.
15 *
16 * Quagga is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; see the file COPYING; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <zebra.h>
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "prng.h"
33
34 struct prng {
35 uint64_t state;
36 };
37
38 struct prng *prng_new(unsigned long long seed)
39 {
40 struct prng *rv = calloc(sizeof(*rv), 1);
41 assert(rv);
42
43 rv->state = seed;
44
45 return rv;
46 }
47
48 /*
49 * This implementation has originally been provided to musl libc by
50 * Szabolcs Nagy <nsz at port70 dot net> in 2013 under the terms of
51 * the MIT license.
52 * It is a simple LCG which D.E. Knuth attributes to C.E. Haynes in
53 * TAOCP Vol2 3.3.4
54 */
55 int prng_rand(struct prng *prng)
56 {
57 prng->state = 6364136223846793005ULL * prng->state + 1;
58 return prng->state >> 33;
59 }
60
61 const char *prng_fuzz(struct prng *prng, const char *string,
62 const char *charset, unsigned int operations)
63 {
64 static char buf[256];
65 unsigned int charset_len;
66 unsigned int i;
67 unsigned int offset;
68 unsigned int op;
69 unsigned int character;
70
71 assert(strlen(string) < sizeof(buf));
72
73 strncpy(buf, string, sizeof(buf));
74 charset_len = strlen(charset);
75
76 for (i = 0; i < operations; i++) {
77 offset = prng_rand(prng) % strlen(buf);
78 op = prng_rand(prng) % 3;
79
80 switch (op) {
81 case 0:
82 /* replace */
83 character = prng_rand(prng) % charset_len;
84 buf[offset] = charset[character];
85 break;
86 case 1:
87 /* remove */
88 memmove(buf + offset, buf + offset + 1,
89 strlen(buf) - offset);
90 break;
91 case 2:
92 /* insert */
93 assert(strlen(buf) + 1 < sizeof(buf));
94
95 memmove(buf + offset + 1, buf + offset,
96 strlen(buf) + 1 - offset);
97 character = prng_rand(prng) % charset_len;
98 buf[offset] = charset[character];
99 break;
100 }
101 }
102 return buf;
103 }
104
105 void prng_free(struct prng *prng)
106 {
107 free(prng);
108 }