forked from ElementsProject/rust-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeserialize_transaction.rs
More file actions
79 lines (72 loc) · 2.08 KB
/
Copy pathdeserialize_transaction.rs
File metadata and controls
79 lines (72 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
extern crate elements;
fn do_test(data: &[u8]) {
let tx_result: Result<elements::Transaction, _> = elements::encode::deserialize(data);
match tx_result {
Err(_) => {},
Ok(mut tx) => {
let reser = elements::encode::serialize(&tx);
assert_eq!(data, &reser[..]);
let len = reser.len();
let calculated_weight = tx.weight();
for input in &mut tx.input {
input.witness = elements::TxInWitness::default();
}
for output in &mut tx.output {
output.witness = elements::TxOutWitness::default();
}
assert_eq!(tx.has_witness(), false);
let no_witness_len = elements::encode::serialize(&tx).len();
assert_eq!(no_witness_len * 3 + len, calculated_weight.to_wu() as usize);
for output in &tx.output {
output.is_null_data();
output.is_pegout();
output.pegout_data();
output.is_fee();
output.minimum_value();
}
},
}
}
#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}
#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
do_test(data);
});
}
}
#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}
#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00", &mut a);
super::do_test(&a);
}
}