Install custom RAND_METHOD for fuzzing
[openssl.git] / fuzz / test-corpus.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL licenses, (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * https://www.openssl.org/source/license.html
8  * or in the file LICENSE in the source distribution.
9  */
10
11 /*
12  * Given a list of files, run each of them through the fuzzer.  Note that
13  * failure will be indicated by some kind of crash. Switching on things like
14  * asan improves the test.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <openssl/crypto.h>
21 #include <openssl/rand.h>
22 #include "fuzzer.h"
23
24 static int fuzz_bytes(unsigned char *buf, int num)
25 {
26     unsigned char val = 1;
27
28     while (--num >= 0)
29         *buf++ = val++;
30     return 1;
31 }
32
33 static int fuzz_status(void)
34 {
35     return 1;
36 }
37
38 static RAND_METHOD fuzz_rand_method = {
39     NULL,
40     fuzz_bytes,
41     NULL,
42     NULL,
43     fuzz_bytes,
44     fuzz_status
45 };
46
47 void FuzzerSetRand(void)
48 {
49     RAND_set_rand_method(&fuzz_rand_method);
50 }
51
52
53
54 int main(int argc, char **argv) {
55     int n;
56
57     FuzzerInitialize(&argc, &argv);
58
59     for (n = 1; n < argc; ++n) {
60         struct stat st;
61         FILE *f;
62         unsigned char *buf;
63         size_t s;
64
65         stat(argv[n], &st);
66         f = fopen(argv[n], "rb");
67         if (f == NULL)
68             continue;
69         buf = malloc(st.st_size);
70         s = fread(buf, 1, st.st_size, f);
71         OPENSSL_assert(s == (size_t)st.st_size);
72         FuzzerTestOneInput(buf, s);
73         free(buf);
74         fclose(f);
75     }
76
77     FuzzerCleanup();
78
79     return 0;
80 }