Skip to content

Commit 5fa9445

Browse files
committed
Add read failure unit tests.
1 parent 1110d4d commit 5fa9445

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

lightning-graph-sync/src/processing.rs

+107
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ where
100100
mod tests {
101101
use bitcoin::blockdata::constants::genesis_block;
102102
use bitcoin::Network;
103+
use lightning::ln::msgs::DecodeError;
103104

104105
use lightning::routing::network_graph::NetworkGraph;
106+
use crate::error::GraphSyncError;
105107

106108
use crate::processing::update_network_graph;
107109

@@ -148,4 +150,109 @@ mod tests {
148150
));
149151
assert!(after.contains("channels: [779484474903625728]"));
150152
}
153+
154+
#[test]
155+
fn network_graph_fails_to_update_from_clipped_input() {
156+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
157+
let network_graph = NetworkGraph::new(block_hash);
158+
159+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
160+
161+
let example_input = vec![
162+
76, 68, 75, 2, 176, 1, 0, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162,
163+
70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0,
164+
10, 209, 73, 0, 2, 144, 0, 0, 2, 96, 250, 182, 51, 6, 110, 215, 177, 217, 185, 184,
165+
160, 250, 200, 126, 21, 121, 209, 112, 158, 135, 77, 40, 160, 209, 113, 161, 245, 196,
166+
59, 184, 119, 2, 180, 101, 113, 124, 204, 150, 157, 103, 192, 42, 110, 145, 224, 176,
167+
14, 26, 197, 136, 6, 143, 13, 46, 3, 200, 220, 34, 228, 191, 104, 238, 134, 80, 3, 167,
168+
152, 93, 150, 15, 179, 70, 61, 160, 42, 198, 253, 53, 249, 37, 29, 21, 176, 28, 136,
169+
240, 237, 138, 165, 218, 254, 14, 245, 123, 228, 145, 150, 2, 25, 120, 160, 148, 15,
170+
56, 179, 42, 198, 99, 96, 81, 85, 53, 227, 51, 62, 134, 4, 175, 0, 151, 217, 104, 180,
171+
201, 249, 117, 196, 87, 94, 150, 74, 1, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193,
172+
166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0,
173+
0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 139, 55, 91, 1, 2, 0, 40, 0, 0, 0, 0, 0, 0,
174+
3, 232, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 123, 235, 5, 192, 74, 1, 2, 111, 226,
175+
140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101,
176+
225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 138,
177+
144, 189, 1, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 0, 0, 0, 5, 220, 0, 0, 0, 0,
178+
123, 235, 5,
179+
];
180+
let update_result = update_network_graph(&network_graph, &example_input[..], &chain_source);
181+
assert!(update_result.is_err());
182+
if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
183+
// this is the expected error type
184+
} else {
185+
assert!(false);
186+
}
187+
}
188+
189+
#[test]
190+
fn network_graph_fails_to_update_from_extended_input_with_single_byte_bigsize() {
191+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
192+
let network_graph = NetworkGraph::new(block_hash);
193+
194+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
195+
196+
let example_input = vec![
197+
76, 68, 75, 2, 176, 1, 0, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162,
198+
70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0,
199+
10, 209, 73, 0, 2, 144, 0, 0, 2, 96, 250, 182, 51, 6, 110, 215, 177, 217, 185, 184,
200+
160, 250, 200, 126, 21, 121, 209, 112, 158, 135, 77, 40, 160, 209, 113, 161, 245, 196,
201+
59, 184, 119, 2, 180, 101, 113, 124, 204, 150, 157, 103, 192, 42, 110, 145, 224, 176,
202+
14, 26, 197, 136, 6, 143, 13, 46, 3, 200, 220, 34, 228, 191, 104, 238, 134, 80, 3, 167,
203+
152, 93, 150, 15, 179, 70, 61, 160, 42, 198, 253, 53, 249, 37, 29, 21, 176, 28, 136,
204+
240, 237, 138, 165, 218, 254, 14, 245, 123, 228, 145, 150, 2, 25, 120, 160, 148, 15,
205+
56, 179, 42, 198, 99, 96, 81, 85, 53, 227, 51, 62, 134, 4, 175, 0, 151, 217, 104, 180,
206+
201, 249, 117, 196, 87, 94, 150, 74, 1, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193,
207+
166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0,
208+
0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 139, 55, 91, 1, 2, 0, 40, 0, 0, 0, 0, 0, 0,
209+
3, 232, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 123, 235, 5, 192, 74, 1, 2, 111, 226,
210+
140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101,
211+
225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 138,
212+
144, 189, 1, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 0, 0, 0, 5, 220, 0, 0, 0, 0,
213+
123, 235, 5, 192, 176
214+
];
215+
let update_result = update_network_graph(&network_graph, &example_input[..], &chain_source);
216+
assert!(update_result.is_err());
217+
if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
218+
// this is the expected error type
219+
} else {
220+
assert!(false);
221+
}
222+
}
223+
224+
#[test]
225+
fn network_graph_fails_to_update_from_extended_input_with_clipped_bigsize() {
226+
let block_hash = genesis_block(Network::Bitcoin).block_hash();
227+
let network_graph = NetworkGraph::new(block_hash);
228+
229+
let chain_source: Option<Box<dyn lightning::chain::Access>> = None;
230+
231+
let example_input = vec![
232+
76, 68, 75, 2, 176, 1, 0, 0, 0, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162,
233+
70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0,
234+
10, 209, 73, 0, 2, 144, 0, 0, 2, 96, 250, 182, 51, 6, 110, 215, 177, 217, 185, 184,
235+
160, 250, 200, 126, 21, 121, 209, 112, 158, 135, 77, 40, 160, 209, 113, 161, 245, 196,
236+
59, 184, 119, 2, 180, 101, 113, 124, 204, 150, 157, 103, 192, 42, 110, 145, 224, 176,
237+
14, 26, 197, 136, 6, 143, 13, 46, 3, 200, 220, 34, 228, 191, 104, 238, 134, 80, 3, 167,
238+
152, 93, 150, 15, 179, 70, 61, 160, 42, 198, 253, 53, 249, 37, 29, 21, 176, 28, 136,
239+
240, 237, 138, 165, 218, 254, 14, 245, 123, 228, 145, 150, 2, 25, 120, 160, 148, 15,
240+
56, 179, 42, 198, 99, 96, 81, 85, 53, 227, 51, 62, 134, 4, 175, 0, 151, 217, 104, 180,
241+
201, 249, 117, 196, 87, 94, 150, 74, 1, 2, 111, 226, 140, 10, 182, 241, 179, 114, 193,
242+
166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0,
243+
0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 139, 55, 91, 1, 2, 0, 40, 0, 0, 0, 0, 0, 0,
244+
3, 232, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 123, 235, 5, 192, 74, 1, 2, 111, 226,
245+
140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101,
246+
225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 10, 209, 73, 0, 2, 144, 0, 0, 97, 138,
247+
144, 189, 1, 1, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 0, 0, 0, 0, 5, 220, 0, 0, 0, 0,
248+
123, 235, 5, 192, 254
249+
];
250+
let update_result = update_network_graph(&network_graph, &example_input[..], &chain_source);
251+
assert!(update_result.is_err());
252+
if let Err(GraphSyncError::DecodeError(DecodeError::ShortRead)) = update_result {
253+
// this is the expected error type
254+
} else {
255+
assert!(false);
256+
}
257+
}
151258
}

0 commit comments

Comments
 (0)