1 // This file is Copyright its original authors, visible in version control
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
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.
13 #![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
16 compile_error!("Fuzz targets need cfg=fuzzing");
18 #[cfg(not(hashes_fuzz))]
19 compile_error!("Fuzz targets need cfg=hashes_fuzz");
21 #[cfg(not(secp256k1_fuzz))]
22 compile_error!("Fuzz targets need cfg=secp256k1_fuzz");
24 extern crate lightning_fuzz;
25 use lightning_fuzz::onion_message::*;
27 #[cfg(feature = "afl")]
28 #[macro_use] extern crate afl;
29 #[cfg(feature = "afl")]
32 onion_message_run(data.as_ptr(), data.len());
36 #[cfg(feature = "honggfuzz")]
37 #[macro_use] extern crate honggfuzz;
38 #[cfg(feature = "honggfuzz")]
42 onion_message_run(data.as_ptr(), data.len());
47 #[cfg(feature = "libfuzzer_fuzz")]
48 #[macro_use] extern crate libfuzzer_sys;
49 #[cfg(feature = "libfuzzer_fuzz")]
50 fuzz_target!(|data: &[u8]| {
51 onion_message_run(data.as_ptr(), data.len());
54 #[cfg(feature = "stdin_fuzz")]
58 let mut data = Vec::with_capacity(8192);
59 std::io::stdin().read_to_end(&mut data).unwrap();
60 onion_message_run(data.as_ptr(), data.len());
67 use lightning_fuzz::utils::test_logger::StringBuffer;
69 use std::sync::{atomic, Arc};
71 let data: Vec<u8> = vec![0];
72 onion_message_run(data.as_ptr(), data.len());
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/onion_message") {
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);
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();
89 let panic_logger = string_logger.clone();
90 let res = if ::std::panic::catch_unwind(move || {
91 onion_message_test(&data, panic_logger);
93 Some(string_logger.into_string())
95 thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
96 main_thread_ref.unpark();
100 while threads_running.load(atomic::Ordering::Acquire) > 32 {
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);
112 if !failed_outputs.is_empty() {
113 println!("Test cases which failed: ");
114 for case in failed_outputs {
115 println!("{}", case);