Add a parse + validate fuzzer
[dnssec-prover] / fuzz / src / parse_stream_validate.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 #![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
11
12 #[cfg(not(fuzzing))]
13 compile_error!("Fuzz targets need cfg=fuzzing");
14
15 extern crate dnssec_prover;
16 use dnssec_prover::ser::parse_rr_stream;
17 use dnssec_prover::validation::verify_rr_stream;
18
19 #[cfg(feature = "afl")]
20 #[macro_use] extern crate afl;
21 #[cfg(feature = "afl")]
22 fn main() {
23         fuzz!(|data| {
24                 let _ = parse_rr_stream(data).as_ref()
25                         .map(|rrs| verify_rr_stream(rrs));
26         });
27 }
28
29 #[cfg(feature = "honggfuzz")]
30 #[macro_use] extern crate honggfuzz;
31 #[cfg(feature = "honggfuzz")]
32 fn main() {
33         loop {
34                 fuzz!(|data| {
35                         let _ = parse_rr_stream(data).as_ref()
36                                 .map(|rrs| verify_rr_stream(rrs));
37                 });
38         }
39 }
40
41 #[cfg(feature = "libfuzzer_fuzz")]
42 #[macro_use] extern crate libfuzzer_sys;
43 #[cfg(feature = "libfuzzer_fuzz")]
44 fuzz_target!(|data: &[u8]| {
45         let _ = parse_rr_stream(data).as_ref()
46                 .map(|rrs| verify_rr_stream(rrs));
47 });
48
49 #[cfg(feature = "stdin_fuzz")]
50 fn main() {
51         use std::io::Read;
52
53         let mut data = Vec::with_capacity(8192);
54         std::io::stdin().read_to_end(&mut data).unwrap();
55         let _ = parse_rr_stream(data).as_ref()
56                 .map(|rrs| verify_rr_stream(rrs));
57 }
58
59 #[test]
60 fn run_test_cases() {
61         use std::fs;
62         use std::io::Read;
63
64         if let Ok(tests) = fs::read_dir("test_cases/parse_response") {
65                 for test in tests {
66                         let mut data: Vec<u8> = Vec::new();
67                         let path = test.unwrap().path();
68                         fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
69
70                         let _ = parse_rr_stream(data).as_ref()
71                                 .map(|rrs| verify_rr_stream(rrs));
72                 }
73         }
74 }