Relicense as dual Apache-2.0 + MIT
[rust-lightning] / fuzz / src / bin / msg_update_fee_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 extern crate lightning_fuzz;
16 use lightning_fuzz::msg_targets::msg_update_fee::*;
17
18 #[cfg(feature = "afl")]
19 #[macro_use] extern crate afl;
20 #[cfg(feature = "afl")]
21 fn main() {
22         fuzz!(|data| {
23                 msg_update_fee_run(data.as_ptr(), data.len());
24         });
25 }
26
27 #[cfg(feature = "honggfuzz")]
28 #[macro_use] extern crate honggfuzz;
29 #[cfg(feature = "honggfuzz")]
30 fn main() {
31         loop {
32                 fuzz!(|data| {
33                         msg_update_fee_run(data.as_ptr(), data.len());
34                 });
35         }
36 }
37
38 #[cfg(feature = "libfuzzer_fuzz")]
39 #[macro_use] extern crate libfuzzer_sys;
40 #[cfg(feature = "libfuzzer_fuzz")]
41 fuzz_target!(|data: &[u8]| {
42         msg_update_fee_run(data.as_ptr(), data.len());
43 });
44
45 #[cfg(feature = "stdin_fuzz")]
46 fn main() {
47         use std::io::Read;
48
49         let mut data = Vec::with_capacity(8192);
50         std::io::stdin().read_to_end(&mut data).unwrap();
51         msg_update_fee_run(data.as_ptr(), data.len());
52 }
53
54 #[test]
55 fn run_test_cases() {
56         use std::fs;
57         use std::io::Read;
58         use lightning_fuzz::utils::test_logger::StringBuffer;
59
60         use std::sync::{atomic, Arc};
61         {
62                 let data: Vec<u8> = vec![0];
63                 msg_update_fee_run(data.as_ptr(), data.len());
64         }
65         let mut threads = Vec::new();
66         let threads_running = Arc::new(atomic::AtomicUsize::new(0));
67         if let Ok(tests) = fs::read_dir("test_cases/msg_update_fee") {
68                 for test in tests {
69                         let mut data: Vec<u8> = Vec::new();
70                         let path = test.unwrap().path();
71                         fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
72                         threads_running.fetch_add(1, atomic::Ordering::AcqRel);
73
74                         let thread_count_ref = Arc::clone(&threads_running);
75                         let main_thread_ref = std::thread::current();
76                         threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
77                                 std::thread::spawn(move || {
78                                         let string_logger = StringBuffer::new();
79
80                                         let panic_logger = string_logger.clone();
81                                         let res = if ::std::panic::catch_unwind(move || {
82                                                 msg_update_fee_test(&data, panic_logger);
83                                         }).is_err() {
84                                                 Some(string_logger.into_string())
85                                         } else { None };
86                                         thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
87                                         main_thread_ref.unpark();
88                                         res
89                                 })
90                         ));
91                         while threads_running.load(atomic::Ordering::Acquire) > 32 {
92                                 std::thread::park();
93                         }
94                 }
95         }
96         for (test, thread) in threads.drain(..) {
97                 if let Some(output) = thread.join().unwrap() {
98                         println!("Output of {}:\n{}", test, output);
99                         panic!();
100                 }
101         }
102 }