diff options
| author | Ben Harris <bjh21@bjh21.me.uk> | 2023-01-07 18:57:48 +0000 |
|---|---|---|
| committer | Ben Harris <bjh21@bjh21.me.uk> | 2023-01-12 22:21:34 +0000 |
| commit | 53a1faa0d758c86c5bc77808c4265129d627be1b (patch) | |
| tree | 20a4abd70ad18ddbb188ea4a0c333ff268779ecd /fuzzpuzz.c | |
| parent | 9478efe32ea5aa6577d11fe45df81b49199780cd (diff) | |
| download | puzzles-53a1faa0d758c86c5bc77808c4265129d627be1b.zip puzzles-53a1faa0d758c86c5bc77808c4265129d627be1b.tar.gz puzzles-53a1faa0d758c86c5bc77808c4265129d627be1b.tar.bz2 puzzles-53a1faa0d758c86c5bc77808c4265129d627be1b.tar.xz | |
Add a fuzzing harness for Puzzles
This just feeds save files into the loading code, but because of how
Puzzles is structured that actually exercises most of its parsers.
Diffstat (limited to 'fuzzpuzz.c')
| -rw-r--r-- | fuzzpuzz.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/fuzzpuzz.c b/fuzzpuzz.c new file mode 100644 index 0000000..4ee523f --- /dev/null +++ b/fuzzpuzz.c @@ -0,0 +1,65 @@ +/* + * fuzzpuzz.c: Fuzzing frontend to all puzzles. + */ + +/* + * The idea here is that this front-end supports all back-ends and can + * feed them save files. This tests the deserialiser, the code for + * loading game descriptions, and the processing of move strings, + * without all the tedium of actually rendering anything. + */ + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "puzzles.h" + +static bool savefile_read(void *wctx, void *buf, int len) +{ + FILE *fp = (FILE *)wctx; + int ret; + + ret = fread(buf, 1, len, fp); + return (ret == len); +} + +int main(int argc, char **argv) +{ + const char *err; + char *gamename; + int i; + const game * ourgame = NULL; + midend *me; + + if (argc != 1) { + fprintf(stderr, "usage: %s\n", argv[0]); + exit(1); + } + + err = identify_game(&gamename, savefile_read, stdin); + if (err != NULL) { + fprintf(stderr, "%s\n", err); + exit(1); + } + + for (i = 0; i < gamecount; i++) + if (strcmp(gamename, gamelist[i]->name) == 0) + ourgame = gamelist[i]; + if (ourgame == NULL) { + fprintf(stderr, "Game '%s' not recognised\n", gamename); + exit(1); + } + + me = midend_new(NULL, ourgame, NULL, NULL); + + rewind(stdin); + err = midend_deserialise(me, savefile_read, stdin); + if (err != NULL) { + fprintf(stderr, "%s\n", err); + exit(1); + } + midend_free(me); + return 0; +} |