X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=fuzz%2Fsrc%2Fbin%2Fmsg_decoded_onion_error_packet_target.rs;h=0be01b95cf36c280caa9ad2b13484fe4c205e89d;hb=4395b92cc8bfe0cc803e70bba11f4db58d5d0dbf;hp=c5c9d4a5399be2955474c9d06fb0d6108f68c480;hpb=e28fd78e67b92de9f42dae3636c5dbea5e9b1f86;p=rust-lightning diff --git a/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs b/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs index c5c9d4a5..0be01b95 100644 --- a/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs +++ b/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs @@ -1,14 +1,20 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + // This file is auto-generated by gen_target.sh based on target_template.txt // To modify it, modify target_template.txt and run gen_target.sh instead. -//Uncomment this for libfuzzer builds: -//#![no_main] +#![cfg_attr(feature = "libfuzzer_fuzz", no_main)] extern crate lightning_fuzz; use lightning_fuzz::msg_targets::msg_decoded_onion_error_packet::*; -use std::io::Read; - #[cfg(feature = "afl")] #[macro_use] extern crate afl; #[cfg(feature = "afl")] @@ -38,7 +44,59 @@ fuzz_target!(|data: &[u8]| { #[cfg(feature = "stdin_fuzz")] fn main() { + use std::io::Read; + let mut data = Vec::with_capacity(8192); std::io::stdin().read_to_end(&mut data).unwrap(); msg_decoded_onion_error_packet_run(data.as_ptr(), data.len()); } + +#[test] +fn run_test_cases() { + use std::fs; + use std::io::Read; + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_decoded_onion_error_packet_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); + if let Ok(tests) = fs::read_dir("test_cases/msg_decoded_onion_error_packet") { + for test in tests { + let mut data: Vec = Vec::new(); + let path = test.unwrap().path(); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_decoded_onion_error_packet_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); + } + } +}