Skip to content

Commit 95a2089

Browse files
committed
test: add test for InfoHash
1 parent 8da9963 commit 95a2089

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/protocol/common.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,101 @@ impl<'de> serde::de::Deserialize<'de> for InfoHash {
9494
}
9595
}
9696

97+
#[cfg(test)]
98+
mod tests {
99+
use std::str::FromStr;
100+
101+
use serde::{Deserialize, Serialize};
102+
use serde_json::json;
103+
104+
use crate::InfoHash;
105+
106+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
107+
struct ContainingInfoHash {
108+
pub info_hash: InfoHash,
109+
}
110+
111+
#[test]
112+
fn an_info_hash_can_be_created_from_a_valid_40_utf8_char_string_representing_an_hexadecimal_value() {
113+
let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
114+
assert!(info_hash.is_ok());
115+
}
116+
117+
#[test]
118+
fn an_info_hash_can_not_be_created_from_a_utf8_string_representing_a_not_valid_hexadecimal_value() {
119+
let info_hash = InfoHash::from_str("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG");
120+
assert!(info_hash.is_err());
121+
}
122+
123+
#[test]
124+
fn an_info_hash_can_only_be_created_from_a_40_utf8_char_string() {
125+
let info_hash = InfoHash::from_str(&"F".repeat(39));
126+
assert!(info_hash.is_err());
127+
128+
let info_hash = InfoHash::from_str(&"F".repeat(41));
129+
assert!(info_hash.is_err());
130+
}
131+
132+
#[test]
133+
fn an_info_hash_should_by_displayed_like_a_40_utf8_lowercased_char_hex_string() {
134+
let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap();
135+
136+
let output = format!("{}", info_hash);
137+
138+
assert_eq!(output, "ffffffffffffffffffffffffffffffffffffffff");
139+
}
140+
141+
#[test]
142+
fn an_info_hash_can_be_created_from_a_valid_20_byte_array_slice() {
143+
let info_hash: InfoHash = [255u8; 20].as_slice().into();
144+
145+
assert_eq!(
146+
info_hash,
147+
InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
148+
);
149+
}
150+
151+
#[test]
152+
fn an_info_hash_can_be_created_from_a_valid_20_byte_array() {
153+
let info_hash: InfoHash = [255u8; 20].into();
154+
155+
assert_eq!(
156+
info_hash,
157+
InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
158+
);
159+
}
160+
161+
#[test]
162+
fn an_info_hash_can_be_serialized() {
163+
let s = ContainingInfoHash {
164+
info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(),
165+
};
166+
167+
let json_serialized_value = serde_json::to_string(&s).unwrap();
168+
169+
assert_eq!(
170+
json_serialized_value,
171+
r#"{"info_hash":"ffffffffffffffffffffffffffffffffffffffff"}"#
172+
);
173+
}
174+
175+
#[test]
176+
fn an_info_hash_can_be_deserialized() {
177+
let json = json!({
178+
"info_hash": "ffffffffffffffffffffffffffffffffffffffff",
179+
});
180+
181+
let s: ContainingInfoHash = serde_json::from_value(json).unwrap();
182+
183+
assert_eq!(
184+
s,
185+
ContainingInfoHash {
186+
info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
187+
}
188+
);
189+
}
190+
}
191+
97192
struct InfoHashVisitor;
98193

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

0 commit comments

Comments
 (0)