Disable fast-fail to let CI actually run even though beta is broken
[rust-lightning] / lightning-block-sync / src / utils.rs
1 use bitcoin::hashes::hex::FromHex;
2 use bitcoin::util::uint::Uint256;
3
4 pub fn hex_to_uint256(hex: &str) -> Result<Uint256, bitcoin::hashes::hex::Error> {
5         let bytes = <[u8; 32]>::from_hex(hex)?;
6         Ok(Uint256::from_be_bytes(bytes))
7 }
8
9 #[cfg(test)]
10 mod tests {
11         use super::*;
12         use bitcoin::util::uint::Uint256;
13
14         #[test]
15         fn hex_to_uint256_empty_str() {
16                 assert!(hex_to_uint256("").is_err());
17         }
18
19         #[test]
20         fn hex_to_uint256_too_short_str() {
21                 let hex = String::from_utf8(vec![b'0'; 32]).unwrap();
22                 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::InvalidLength(64, 32)));
23         }
24
25         #[test]
26         fn hex_to_uint256_too_long_str() {
27                 let hex = String::from_utf8(vec![b'0'; 128]).unwrap();
28                 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::InvalidLength(64, 128)));
29         }
30
31         #[test]
32         fn hex_to_uint256_odd_length_str() {
33                 let hex = String::from_utf8(vec![b'0'; 65]).unwrap();
34                 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::OddLengthString(65)));
35         }
36
37         #[test]
38         fn hex_to_uint256_invalid_char() {
39                 let hex = String::from_utf8(vec![b'G'; 64]).unwrap();
40                 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::InvalidChar(b'G')));
41         }
42
43         #[test]
44         fn hex_to_uint256_lowercase_str() {
45                 let hex: String = std::iter::repeat("0123456789abcdef").take(4).collect();
46                 assert_eq!(hex_to_uint256(&hex).unwrap(), Uint256([0x0123456789abcdefu64; 4]));
47         }
48
49         #[test]
50         fn hex_to_uint256_uppercase_str() {
51                 let hex: String = std::iter::repeat("0123456789ABCDEF").take(4).collect();
52                 assert_eq!(hex_to_uint256(&hex).unwrap(), Uint256([0x0123456789abcdefu64; 4]));
53         }
54 }