access list: use hex crate for info hash parsing

This commit is contained in:
Joakim Frostegård 2021-10-15 10:09:02 +02:00
parent 752873280b
commit f370dac330
4 changed files with 16 additions and 15 deletions

1
Cargo.lock generated
View file

@ -76,6 +76,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"hashbrown 0.11.2", "hashbrown 0.11.2",
"hex",
"indexmap", "indexmap",
"rand", "rand",
"serde", "serde",

View file

@ -6,7 +6,6 @@
* rename Allow to Require? * rename Allow to Require?
* serde-rename AccessListTypes to lowercase * serde-rename AccessListTypes to lowercase
* serde-rename list_type to type? * serde-rename list_type to type?
* add tests for info hash parsing
* implement for aquatic_http and aquatic_ws * implement for aquatic_http and aquatic_ws
* Don't unwrap peer_address * Don't unwrap peer_address

View file

@ -13,6 +13,7 @@ name = "aquatic_common"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
hashbrown = "0.11.2" hashbrown = "0.11.2"
hex = "0.4"
indexmap = "1" indexmap = "1"
rand = { version = "0.8", features = ["small_rng"] } rand = { version = "0.8", features = ["small_rng"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }

View file

@ -36,22 +36,9 @@ pub struct AccessList(HashSet<[u8; 20]>);
impl AccessList { impl AccessList {
fn parse_info_hash(line: String) -> anyhow::Result<[u8; 20]> { fn parse_info_hash(line: String) -> anyhow::Result<[u8; 20]> {
if line.len() != 20 {
return Err(anyhow::anyhow!("Info hash is not 20 bytes long: {}", line));
}
let mut bytes = [0u8; 20]; let mut bytes = [0u8; 20];
for (byte, c) in bytes.iter_mut().zip(line.chars()) { hex::decode_to_slice(line, &mut bytes)?;
if c as u32 > 255 {
return Err(anyhow::anyhow!(
"Info hash character is not ASCII: {:#?}",
c
));
}
*byte = c as u8;
}
Ok(bytes) Ok(bytes)
} }
@ -170,3 +157,16 @@ pub fn convert_ipv4_mapped_ipv6(ip_address: IpAddr) -> IpAddr {
ip_address ip_address
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_access_list_parse_info_hash(){
assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeee".to_owned()).is_ok());
assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeeef".to_owned()).is_err());
assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeee".to_owned()).is_err());
assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeeö".to_owned()).is_err());
}
}