mirror of
https://github.com/lightningdevkit/rust-lightning.git
synced 2025-02-25 07:17:40 +01:00
An invoice is serialized as a TLV stream and encoded as bytes. Add a fuzz test that parses the TLV stream and deserializes the underlying Invoice. Then compare the original bytes with those obtained by re-serializing the Invoice.
31 lines
1 KiB
Rust
31 lines
1 KiB
Rust
// This file is Copyright its original authors, visible in version control
|
|
// history.
|
|
//
|
|
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
|
|
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
|
|
// You may not use this file except in accordance with one or both of these
|
|
// licenses.
|
|
|
|
use crate::utils::test_logger;
|
|
use lightning::offers::invoice::Invoice;
|
|
use lightning::util::ser::Writeable;
|
|
use std::convert::TryFrom;
|
|
|
|
#[inline]
|
|
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
|
|
if let Ok(invoice) = Invoice::try_from(data.to_vec()) {
|
|
let mut bytes = Vec::with_capacity(data.len());
|
|
invoice.write(&mut bytes).unwrap();
|
|
assert_eq!(data, bytes);
|
|
}
|
|
}
|
|
|
|
pub fn invoice_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
|
|
do_test(data, out);
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn invoice_deser_run(data: *const u8, datalen: usize) {
|
|
do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
|
|
}
|