7b93fd38db1a37ce4a32410f79266b50ef75dc1a
[rust-lightning] / fuzz / src / invoice_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 crate::utils::test_logger;
11 use lightning::offers::invoice::Invoice;
12 use lightning::util::ser::Writeable;
13 use std::convert::TryFrom;
14
15 #[inline]
16 pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
17         if let Ok(invoice) = Invoice::try_from(data.to_vec()) {
18                 let mut bytes = Vec::with_capacity(data.len());
19                 invoice.write(&mut bytes).unwrap();
20                 assert_eq!(data, bytes);
21         }
22 }
23
24 pub fn invoice_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
25         do_test(data, out);
26 }
27
28 #[no_mangle]
29 pub extern "C" fn invoice_deser_run(data: *const u8, datalen: usize) {
30         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
31 }