Skip to content

Commit

Permalink
Merge #89: Add tests for InfoHash
Browse files Browse the repository at this point in the history
3f617eb test: add test for InfoHash (Jose Celano)

Pull request description:

ACKs for top commit:
  josecelano:
    ACK 3f617eb

Tree-SHA512: 4de5cc470c201c3217643f6c66b97472b0d138b9f02b502039562249e921f19226548e199fa0ff642c7d5d24d4fd1649f173fc476c4d140356d932783d5238e8
  • Loading branch information
josecelano committed Sep 29, 2022
2 parents c487095 + 3f617eb commit 319bdf8
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/protocol/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,101 @@ impl<'de> serde::de::Deserialize<'de> for InfoHash {
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::InfoHash;

#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
struct ContainingInfoHash {
pub info_hash: InfoHash,
}

#[test]
fn an_info_hash_can_be_created_from_a_valid_40_utf8_char_string_representing_an_hexadecimal_value() {
let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
assert!(info_hash.is_ok());
}

#[test]
fn an_info_hash_can_not_be_created_from_a_utf8_string_representing_a_not_valid_hexadecimal_value() {
let info_hash = InfoHash::from_str("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG");
assert!(info_hash.is_err());
}

#[test]
fn an_info_hash_can_only_be_created_from_a_40_utf8_char_string() {
let info_hash = InfoHash::from_str(&"F".repeat(39));
assert!(info_hash.is_err());

let info_hash = InfoHash::from_str(&"F".repeat(41));
assert!(info_hash.is_err());
}

#[test]
fn an_info_hash_should_by_displayed_like_a_40_utf8_lowercased_char_hex_string() {
let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap();

let output = format!("{}", info_hash);

assert_eq!(output, "ffffffffffffffffffffffffffffffffffffffff");
}

#[test]
fn an_info_hash_can_be_created_from_a_valid_20_byte_array_slice() {
let info_hash: InfoHash = [255u8; 20].as_slice().into();

assert_eq!(
info_hash,
InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
);
}

#[test]
fn an_info_hash_can_be_created_from_a_valid_20_byte_array() {
let info_hash: InfoHash = [255u8; 20].into();

assert_eq!(
info_hash,
InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
);
}

#[test]
fn an_info_hash_can_be_serialized() {
let s = ContainingInfoHash {
info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(),
};

let json_serialized_value = serde_json::to_string(&s).unwrap();

assert_eq!(
json_serialized_value,
r#"{"info_hash":"ffffffffffffffffffffffffffffffffffffffff"}"#
);
}

#[test]
fn an_info_hash_can_be_deserialized() {
let json = json!({
"info_hash": "ffffffffffffffffffffffffffffffffffffffff",
});

let s: ContainingInfoHash = serde_json::from_value(json).unwrap();

assert_eq!(
s,
ContainingInfoHash {
info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
}
);
}
}

struct InfoHashVisitor;

impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
Expand Down

0 comments on commit 319bdf8

Please sign in to comment.