2 extern crate lightning;
3 extern crate secp256k1;
5 use bitcoin::network::constants::Network;
7 use lightning::chain::chaininterface;
8 use lightning::ln::channelmanager::ChannelDetails;
9 use lightning::ln::msgs;
10 use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
11 use lightning::ln::router::{Router, RouteHint};
12 use lightning::util::reset_rng_state;
13 use lightning::util::logger::Logger;
15 use secp256k1::key::PublicKey;
16 use secp256k1::Secp256k1;
20 use utils::test_logger;
25 pub fn slice_to_be16(v: &[u8]) -> u16 {
26 ((v[0] as u16) << 8*1) |
27 ((v[1] as u16) << 8*0)
31 pub fn slice_to_be32(v: &[u8]) -> u32 {
32 ((v[0] as u32) << 8*3) |
33 ((v[1] as u32) << 8*2) |
34 ((v[2] as u32) << 8*1) |
35 ((v[3] as u32) << 8*0)
39 pub fn slice_to_be64(v: &[u8]) -> u64 {
40 ((v[0] as u64) << 8*7) |
41 ((v[1] as u64) << 8*6) |
42 ((v[2] as u64) << 8*5) |
43 ((v[3] as u64) << 8*4) |
44 ((v[4] as u64) << 8*3) |
45 ((v[5] as u64) << 8*2) |
46 ((v[6] as u64) << 8*1) |
47 ((v[7] as u64) << 8*0)
51 pub fn do_test(data: &[u8]) {
55 macro_rules! get_slice_nonadvancing {
58 if data.len() < read_pos + $len as usize {
61 &data[read_pos..read_pos + $len as usize]
65 macro_rules! get_slice {
68 let res = get_slice_nonadvancing!($len);
75 macro_rules! decode_msg {
76 ($MsgType: path, $len: expr) => {
77 match <($MsgType)>::decode(get_slice!($len)) {
80 msgs::DecodeError::UnknownRealmByte => return,
81 msgs::DecodeError::UnknownRequiredFeature => return,
82 msgs::DecodeError::BadPublicKey => return,
83 msgs::DecodeError::BadSignature => return,
84 msgs::DecodeError::BadText => return,
85 msgs::DecodeError::ExtraAddressesPerType => return,
86 msgs::DecodeError::BadLengthDescriptor => return,
87 msgs::DecodeError::ShortRead => panic!("We picked the length..."),
93 macro_rules! decode_msg_with_len16 {
94 ($MsgType: path, $begin_len: expr, $excess: expr) => {
96 let extra_len = slice_to_be16(&get_slice_nonadvancing!($begin_len as usize + 2)[$begin_len..$begin_len + 2]);
97 decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize) + $excess)
102 let secp_ctx = Secp256k1::new();
103 macro_rules! get_pubkey {
105 match PublicKey::from_slice(&secp_ctx, get_slice!(33)) {
112 let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
113 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
115 let our_pubkey = get_pubkey!();
116 let router = Router::new(our_pubkey.clone(), chain_monitor, Arc::clone(&logger));
119 match get_slice!(1)[0] {
121 let start_len = slice_to_be16(&get_slice_nonadvancing!(64 + 2)[64..64 + 2]) as usize;
122 let addr_len = slice_to_be16(&get_slice_nonadvancing!(64+start_len+2 + 74)[64+start_len+2 + 72..64+start_len+2 + 74]);
123 if addr_len > (37+1)*4 {
126 let _ = router.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288));
129 let _ = router.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4));
132 let _ = router.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128));
135 match get_slice!(1)[0] {
137 router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)});
140 let short_channel_id = slice_to_be64(get_slice!(8));
141 router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id});
147 let target = get_pubkey!();
148 let mut first_hops_vec = Vec::new();
149 let first_hops = match get_slice!(1)[0] {
152 let count = slice_to_be16(get_slice!(2));
154 first_hops_vec.push(ChannelDetails {
156 short_channel_id: Some(slice_to_be64(get_slice!(8))),
157 remote_network_id: get_pubkey!(),
158 channel_value_satoshis: slice_to_be64(get_slice!(8)),
162 Some(&first_hops_vec[..])
166 let mut last_hops_vec = Vec::new();
168 let count = slice_to_be16(get_slice!(2));
170 last_hops_vec.push(RouteHint {
171 src_node_id: get_pubkey!(),
172 short_channel_id: slice_to_be64(get_slice!(8)),
173 fee_base_msat: slice_to_be32(get_slice!(4)),
174 fee_proportional_millionths: slice_to_be32(get_slice!(4)),
175 cltv_expiry_delta: slice_to_be16(get_slice!(2)),
176 htlc_minimum_msat: slice_to_be64(get_slice!(8)),
181 let _ = router.get_route(&target, first_hops, last_hops, slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4)));
188 #[cfg(feature = "afl")]
189 #[macro_use] extern crate afl;
190 #[cfg(feature = "afl")]
197 #[cfg(feature = "honggfuzz")]
198 #[macro_use] extern crate honggfuzz;
199 #[cfg(feature = "honggfuzz")]
213 fn duplicate_crash() {
214 super::do_test(&::hex::decode("00").unwrap());