Skip to content

Commit c593383

Browse files
committed
encoding/decoding tests added for BigSize
removed unnecessary debugging line using io::Cursor in place of the std one encoding/decoding tests added for BigSize made the code concise encoding/decoding tests added for BigSize
1 parent c5cc1ed commit c593383

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lightning/src/util/ser.rs

+42
Original file line numberDiff line numberDiff line change
@@ -1272,4 +1272,46 @@ mod tests {
12721272
hostname.write(&mut buf).unwrap();
12731273
assert_eq!(Hostname::read(&mut buf.as_slice()).unwrap().as_str(), "test");
12741274
}
1275+
1276+
#[test]
1277+
fn bigsize_encoding_decoding() {
1278+
let values = vec![0, 252, 253, 65535, 65536, 4294967295, 4294967296, 18446744073709551615];
1279+
let bytes = vec![
1280+
"00",
1281+
"fc",
1282+
"fd00fd",
1283+
"fdffff",
1284+
"fe00010000",
1285+
"feffffffff",
1286+
"ff0000000100000000",
1287+
"ffffffffffffffffff"
1288+
];
1289+
for i in 0..=7 {
1290+
let mut stream = crate::io::Cursor::new(::hex::decode(bytes[i]).unwrap());
1291+
assert_eq!(super::BigSize::read(&mut stream).unwrap().0, values[i]);
1292+
let mut stream = super::VecWriter(Vec::new());
1293+
super::BigSize(values[i]).write(&mut stream).unwrap();
1294+
assert_eq!(stream.0, ::hex::decode(bytes[i]).unwrap());
1295+
}
1296+
let err_bytes = vec![
1297+
"fd00fc",
1298+
"fe0000ffff",
1299+
"ff00000000ffffffff",
1300+
"fd00",
1301+
"feffff",
1302+
"ffffffffff",
1303+
"fd",
1304+
"fe",
1305+
"ff",
1306+
""
1307+
];
1308+
for i in 0..=9 {
1309+
let mut stream = crate::io::Cursor::new(::hex::decode(err_bytes[i]).unwrap());
1310+
if i < 3 {
1311+
assert_eq!(super::BigSize::read(&mut stream).err(), Some(crate::ln::msgs::DecodeError::InvalidValue));
1312+
} else {
1313+
assert_eq!(super::BigSize::read(&mut stream).err(), Some(crate::ln::msgs::DecodeError::ShortRead));
1314+
}
1315+
}
1316+
}
12751317
}

0 commit comments

Comments
 (0)