2 extern crate lightning;
3 extern crate secp256k1;
5 use lightning::ln::channelmanager::ChannelDetails;
6 use lightning::ln::msgs;
7 use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
8 use lightning::ln::router::{Router, RouteHint};
9 use lightning::util::reset_rng_state;
10 use lightning::util::logger::Logger;
12 use secp256k1::key::PublicKey;
13 use secp256k1::Secp256k1;
17 use utils::test_logger;
22 pub fn slice_to_be16(v: &[u8]) -> u16 {
23 ((v[0] as u16) << 8*1) |
24 ((v[1] as u16) << 8*0)
28 pub fn slice_to_be32(v: &[u8]) -> u32 {
29 ((v[0] as u32) << 8*3) |
30 ((v[1] as u32) << 8*2) |
31 ((v[2] as u32) << 8*1) |
32 ((v[3] as u32) << 8*0)
36 pub fn slice_to_be64(v: &[u8]) -> u64 {
37 ((v[0] as u64) << 8*7) |
38 ((v[1] as u64) << 8*6) |
39 ((v[2] as u64) << 8*5) |
40 ((v[3] as u64) << 8*4) |
41 ((v[4] as u64) << 8*3) |
42 ((v[5] as u64) << 8*2) |
43 ((v[6] as u64) << 8*1) |
44 ((v[7] as u64) << 8*0)
48 pub fn do_test(data: &[u8]) {
52 macro_rules! get_slice_nonadvancing {
55 if data.len() < read_pos + $len as usize {
58 &data[read_pos..read_pos + $len as usize]
62 macro_rules! get_slice {
65 let res = get_slice_nonadvancing!($len);
72 macro_rules! decode_msg {
73 ($MsgType: path, $len: expr) => {
74 match <($MsgType)>::decode(get_slice!($len)) {
77 msgs::DecodeError::UnknownRealmByte => return,
78 msgs::DecodeError::UnknownRequiredFeature => return,
79 msgs::DecodeError::BadPublicKey => return,
80 msgs::DecodeError::BadSignature => return,
81 msgs::DecodeError::BadText => return,
82 msgs::DecodeError::ExtraAddressesPerType => return,
83 msgs::DecodeError::BadLengthDescriptor => return,
84 msgs::DecodeError::ShortRead => panic!("We picked the length..."),
90 macro_rules! decode_msg_with_len16 {
91 ($MsgType: path, $begin_len: expr, $excess: expr) => {
93 let extra_len = slice_to_be16(&get_slice_nonadvancing!($begin_len as usize + 2)[$begin_len..$begin_len + 2]);
94 decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize) + $excess)
99 let secp_ctx = Secp256k1::new();
100 macro_rules! get_pubkey {
102 match PublicKey::from_slice(&secp_ctx, get_slice!(33)) {
109 let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
111 let our_pubkey = get_pubkey!();
112 let router = Router::new(our_pubkey.clone(), Arc::clone(&logger));
115 match get_slice!(1)[0] {
117 let start_len = slice_to_be16(&get_slice_nonadvancing!(64 + 2)[64..64 + 2]) as usize;
118 let addr_len = slice_to_be16(&get_slice_nonadvancing!(64+start_len+2 + 74)[64+start_len+2 + 72..64+start_len+2 + 74]);
119 if addr_len > (37+1)*4 {
122 let _ = router.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288));
125 let _ = router.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4));
128 let _ = router.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128));
131 match get_slice!(1)[0] {
133 router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)});
136 let short_channel_id = slice_to_be64(get_slice!(8));
137 router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id});
143 let target = get_pubkey!();
144 let mut first_hops_vec = Vec::new();
145 let first_hops = match get_slice!(1)[0] {
148 let count = slice_to_be16(get_slice!(2));
150 first_hops_vec.push(ChannelDetails {
152 short_channel_id: Some(slice_to_be64(get_slice!(8))),
153 remote_network_id: get_pubkey!(),
154 channel_value_satoshis: slice_to_be64(get_slice!(8)),
158 Some(&first_hops_vec[..])
162 let mut last_hops_vec = Vec::new();
164 let count = slice_to_be16(get_slice!(2));
166 last_hops_vec.push(RouteHint {
167 src_node_id: get_pubkey!(),
168 short_channel_id: slice_to_be64(get_slice!(8)),
169 fee_base_msat: slice_to_be32(get_slice!(4)),
170 fee_proportional_millionths: slice_to_be32(get_slice!(4)),
171 cltv_expiry_delta: slice_to_be16(get_slice!(2)),
172 htlc_minimum_msat: slice_to_be64(get_slice!(8)),
177 let _ = router.get_route(&target, first_hops, last_hops, slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4)));
184 #[cfg(feature = "afl")]
185 #[macro_use] extern crate afl;
186 #[cfg(feature = "afl")]
193 #[cfg(feature = "honggfuzz")]
194 #[macro_use] extern crate honggfuzz;
195 #[cfg(feature = "honggfuzz")]
209 fn duplicate_crash() {
210 super::do_test(&::hex::decode("00").unwrap());