Upgrade rust-bitcoin to 0.31
[rust-lightning] / fuzz / src / bin / fromstr_to_netaddress_target.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 // This file is auto-generated by gen_target.sh based on target_template.txt
11 // To modify it, modify target_template.txt and run gen_target.sh instead.
12
13 #![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14
15 #[cfg(not(fuzzing))]
16 compile_error!("Fuzz targets need cfg=fuzzing");
17
18 #[cfg(not(hashes_fuzz))]
19 compile_error!("Fuzz targets need cfg=hashes_fuzz");
20
21 #[cfg(not(secp256k1_fuzz))]
22 compile_error!("Fuzz targets need cfg=secp256k1_fuzz");
23
24 extern crate lightning_fuzz;
25 use lightning_fuzz::fromstr_to_netaddress::*;
26
27 #[cfg(feature = "afl")]
28 #[macro_use] extern crate afl;
29 #[cfg(feature = "afl")]
30 fn main() {
31         fuzz!(|data| {
32                 fromstr_to_netaddress_run(data.as_ptr(), data.len());
33         });
34 }
35
36 #[cfg(feature = "honggfuzz")]
37 #[macro_use] extern crate honggfuzz;
38 #[cfg(feature = "honggfuzz")]
39 fn main() {
40         loop {
41                 fuzz!(|data| {
42                         fromstr_to_netaddress_run(data.as_ptr(), data.len());
43                 });
44         }
45 }
46
47 #[cfg(feature = "libfuzzer_fuzz")]
48 #[macro_use] extern crate libfuzzer_sys;
49 #[cfg(feature = "libfuzzer_fuzz")]
50 fuzz_target!(|data: &[u8]| {
51         fromstr_to_netaddress_run(data.as_ptr(), data.len());
52 });
53
54 #[cfg(feature = "stdin_fuzz")]
55 fn main() {
56         use std::io::Read;
57
58         let mut data = Vec::with_capacity(8192);
59         std::io::stdin().read_to_end(&mut data).unwrap();
60         fromstr_to_netaddress_run(data.as_ptr(), data.len());
61 }
62
63 #[test]
64 fn run_test_cases() {
65         use std::fs;
66         use std::io::Read;
67         use lightning_fuzz::utils::test_logger::StringBuffer;
68
69         use std::sync::{atomic, Arc};
70         {
71                 let data: Vec<u8> = vec![0];
72                 fromstr_to_netaddress_run(data.as_ptr(), data.len());
73         }
74         let mut threads = Vec::new();
75         let threads_running = Arc::new(atomic::AtomicUsize::new(0));
76         if let Ok(tests) = fs::read_dir("test_cases/fromstr_to_netaddress") {
77                 for test in tests {
78                         let mut data: Vec<u8> = Vec::new();
79                         let path = test.unwrap().path();
80                         fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
81                         threads_running.fetch_add(1, atomic::Ordering::AcqRel);
82
83                         let thread_count_ref = Arc::clone(&threads_running);
84                         let main_thread_ref = std::thread::current();
85                         threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
86                                 std::thread::spawn(move || {
87                                         let string_logger = StringBuffer::new();
88
89                                         let panic_logger = string_logger.clone();
90                                         let res = if ::std::panic::catch_unwind(move || {
91                                                 fromstr_to_netaddress_test(&data, panic_logger);
92                                         }).is_err() {
93                                                 Some(string_logger.into_string())
94                                         } else { None };
95                                         thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
96                                         main_thread_ref.unpark();
97                                         res
98                                 })
99                         ));
100                         while threads_running.load(atomic::Ordering::Acquire) > 32 {
101                                 std::thread::park();
102                         }
103                 }
104         }
105         let mut failed_outputs = Vec::new();
106         for (test, thread) in threads.drain(..) {
107                 if let Some(output) = thread.join().unwrap() {
108                         println!("\nOutput of {}:\n{}\n", test, output);
109                         failed_outputs.push(test);
110                 }
111         }
112         if !failed_outputs.is_empty() {
113                 println!("Test cases which failed: ");
114                 for case in failed_outputs {
115                         println!("{}", case);
116                 }
117                 panic!();
118         }
119 }