Skip to content

Commit c0c0744

Browse files
committed
Add fuzz testing for Writeable
1 parent a6fb12c commit c0c0744

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

fuzz/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,7 @@ path = "fuzz_targets/msg_targets/msg_channel_reestablish_target.rs"
123123
[[bin]]
124124
name = "msg_error_message_target"
125125
path = "fuzz_targets/msg_targets/msg_error_message_target.rs"
126+
127+
[[bin]]
128+
name = "msg_accept_channel_target_writeable"
129+
path = "fuzz_targets/msg_accept_channel_target_writeable.rs"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
2+
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
3+
4+
extern crate lightning;
5+
6+
use lightning::ln::msgs;
7+
use lightning::util::reset_rng_state;
8+
9+
use lightning::ln::msgs::{MsgEncodable, MsgDecodable, AcceptChannel};
10+
use lightning::util::ser::{Writeable, Readable, Writer, Reader};
11+
12+
mod utils;
13+
14+
#[inline]
15+
pub fn do_test(data: &[u8]) {
16+
reset_rng_state();
17+
let mut r = Reader::new(::std::io::Cursor::new(data));
18+
if let Ok(msg) = AcceptChannel::read(&mut r) {
19+
let p = r.get_ref().position() as usize;
20+
if p > r.get_ref().get_ref().len() {
21+
return
22+
}
23+
let mut w = Writer::new(::std::io::Cursor::new(vec![]));
24+
msg.write(&mut w).unwrap();
25+
26+
let buf = w.into_inner().into_inner();
27+
assert_eq!(&r.into_inner().into_inner()[..p], &buf[..p]);
28+
29+
let msg2 = AcceptChannel::decode(&buf[..]).unwrap();
30+
let enc = msg2.encode();
31+
assert_eq!(p, enc.len());
32+
assert_eq!(&data[..p], &enc[..]);
33+
34+
}
35+
}
36+
37+
#[cfg(feature = "afl")]
38+
extern crate afl;
39+
#[cfg(feature = "afl")]
40+
fn main() {
41+
afl::read_stdio_bytes(|data| {
42+
do_test(&data);
43+
});
44+
}
45+
46+
#[cfg(feature = "honggfuzz")]
47+
#[macro_use] extern crate honggfuzz;
48+
#[cfg(feature = "honggfuzz")]
49+
fn main() {
50+
loop {
51+
fuzz!(|data| {
52+
do_test(data);
53+
});
54+
}
55+
}
56+
57+
extern crate hex;
58+
#[cfg(test)]
59+
mod tests {
60+
#[test]
61+
fn duplicate_crash() {
62+
super::do_test(&::hex::decode("00").unwrap());
63+
}
64+
}

src/ln/msgs.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::fmt;
99
use std::result::Result;
1010

1111
use util::{byte_utils, internal_traits, events};
12+
use util::ser::{Readable, Reader, Writeable, Writer};
1213

1314
pub trait MsgEncodable {
1415
fn encode(&self) -> Vec<u8>;
@@ -1665,6 +1666,24 @@ impl MsgDecodable for ErrorMessage {
16651666
}
16661667
}
16671668

1669+
impl_writeable!(AcceptChannel, {
1670+
temporary_channel_id,
1671+
dust_limit_satoshis,
1672+
max_htlc_value_in_flight_msat,
1673+
channel_reserve_satoshis,
1674+
htlc_minimum_msat,
1675+
minimum_depth,
1676+
to_self_delay,
1677+
max_accepted_htlcs,
1678+
funding_pubkey,
1679+
revocation_basepoint,
1680+
payment_basepoint,
1681+
delayed_payment_basepoint,
1682+
htlc_basepoint,
1683+
first_per_commitment_point,
1684+
shutdown_scriptpubkey
1685+
});
1686+
16681687
#[cfg(test)]
16691688
mod tests {
16701689
use hex;

src/util/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ pub(crate) mod test_utils;
2121
#[macro_use]
2222
pub(crate) mod macro_logger;
2323

24+
#[cfg(feature = "fuzztarget")]
25+
#[macro_use]
26+
pub mod ser;
27+
#[cfg(not(feature = "fuzztarget"))]
28+
#[macro_use]
29+
pub(crate) mod ser;
30+
2431
pub mod logger;

0 commit comments

Comments
 (0)