]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/src/bolt11_deser.rs
Move existing BOLT11 fuzz test to the `fuzz` crate
[rust-lightning] / fuzz / src / bolt11_deser.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 use bitcoin::bech32::{u5, FromBase32, ToBase32};
11 use crate::utils::test_logger;
12 use lightning_invoice::RawDataPart;
13
14 #[inline]
15 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
16         let bech32 = data.iter().map(|x| u5::try_from_u8(x % 32).unwrap()).collect::<Vec<_>>();
17         let invoice = match RawDataPart::from_base32(&bech32) {
18                 Ok(invoice) => invoice,
19                 Err(_) => return,
20         };
21
22         // Our encoding is not worse than the input
23         assert!(invoice.to_base32().len() <= bech32.len());
24
25         // Our serialization is loss-less
26         assert_eq!(
27                 RawDataPart::from_base32(&invoice.to_base32()).expect("faild parsing out own encoding"),
28                 invoice
29         );
30 }
31
32 pub fn bolt11_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
33         do_test(data, out);
34 }
35
36 #[no_mangle]
37 pub extern "C" fn bolt11_deser_run(data: *const u8, datalen: usize) {
38         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
39 }