Skip to content

Commit 27fbdf9

Browse files
committed
primitives: add unit test for incomplete transaction decoding
1 parent 93db1e0 commit 27fbdf9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

primitives/src/transaction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,16 @@ mod tests {
21162116
decoder.push_bytes(&mut slice).unwrap();
21172117
let tx = decoder.end().unwrap();
21182118

2119+
// Attempt various truncations
2120+
for i in [1, 10, 20, 50, 100, tx_bytes.len() / 2, tx_bytes.len()] {
2121+
let mut decoder = Transaction::decoder();
2122+
let mut slice = &tx_bytes[..tx_bytes.len() - i];
2123+
// push_bytes will not fail because the data is not invalid, just truncated
2124+
decoder.push_bytes(&mut slice).unwrap();
2125+
// ...but end() will fail because we will be in some incomplete state
2126+
decoder.end().unwrap_err();
2127+
}
2128+
21192129
// All these tests aren't really needed because if they fail, the hash check at the end
21202130
// will also fail. But these will show you where the failure is so I'll leave them in.
21212131
assert_eq!(tx.version, Version::TWO);

0 commit comments

Comments
 (0)