Skip to content

Commit 0d93db7

Browse files
committed
fuzz: when running as unit tests, allow -v and [corpus...] args.
Hacky parser, not a real one, but this is for devs, so they can clean it up with ccan/opt themselves if the want to be fancy! 🎩 Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent e95e5f9 commit 0d93db7

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

tests/fuzz/libfuzz.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22

33
#include <assert.h>
4+
#include <ccan/err/err.h>
45
#include <ccan/isaac/isaac64.h>
56
#include <ccan/short_types/short_types.h>
67
#include <ccan/tal/grab_file/grab_file.h>
@@ -130,24 +131,40 @@ size_t cross_over(const u8 *in1, size_t in1_size, const u8 *in2,
130131
/* In non-fuzzing builds, these become unit tests which just run the corpora:
131132
* this is also good for attaching a debugger to! */
132133
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
134+
static size_t find_opt(char *argv[], const char *opt)
135+
{
136+
for (size_t i = 1; argv[i]; i++) {
137+
if (streq(argv[i], opt))
138+
return i;
139+
}
140+
return 0;
141+
}
142+
133143
int main(int argc, char *argv[])
134144
{
135145
DIR *d;
136146
struct dirent *di;
147+
int verbose_flag;
137148

138149
common_setup(argv[0]);
139150
assert(chdir("tests/fuzz/corpora") == 0);
140151
assert(chdir(path_basename(tmpctx, argv[0])) == 0);
141152

142-
/* FIXME: Support explicit path args? */
143153
init(&argc, &argv);
154+
verbose_flag = find_opt(argv, "-v");
144155
d = opendir(".");
145156
while ((di = readdir(d)) != NULL) {
146157
u8 *contents;
147158
if (streq(di->d_name, ".") || streq(di->d_name, ".."))
148159
continue;
160+
/* If you specify options other than -v, they're test names */
161+
if (argv[verbose_flag + 1] && !find_opt(argv, di->d_name))
162+
continue;
163+
if (verbose_flag)
164+
printf("%s\n", di->d_name);
149165
contents = grab_file_raw(tmpctx, di->d_name);
150-
assert(contents);
166+
if (!contents)
167+
err(1, "Could not read %s", di->d_name);
151168
run(contents, tal_bytelen(contents));
152169
}
153170
closedir(d);

0 commit comments

Comments
 (0)