From 26d78ed2f03a76dca47fc160a4d8c38319b1248b Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 13 Jan 2025 15:58:23 +0000 Subject: [PATCH] Add tests for private field --- crates/librqbit_core/src/torrent_metainfo.rs | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/librqbit_core/src/torrent_metainfo.rs b/crates/librqbit_core/src/torrent_metainfo.rs index 024635a..1d026ef 100644 --- a/crates/librqbit_core/src/torrent_metainfo.rs +++ b/crates/librqbit_core/src/torrent_metainfo.rs @@ -93,7 +93,7 @@ impl TorrentMetaV1 { } /// Main torrent information, shared by .torrent files and magnet link contents. -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct TorrentMetaV1Info { #[serde(skip_serializing_if = "Option::is_none")] pub name: Option, @@ -415,6 +415,8 @@ where mod tests { use std::io::Read; + use bencode::BencodeValue; + use super::*; const TORRENT_FILENAME: &str = "../librqbit/resources/ubuntu-21.04-desktop-amd64.iso.torrent"; @@ -476,4 +478,35 @@ mod tests { assert_eq!(torrent, deserialized); } + + #[test] + fn test_private_serialize_deserialize() { + for private in [false, true] { + let info: TorrentMetaV1Info = TorrentMetaV1Info { + private, + ..Default::default() + }; + let mut buf = Vec::new(); + bencode::bencode_serialize_to_writer(&info, &mut buf).unwrap(); + + let deserialized = TorrentMetaV1Info::::deserialize( + &mut BencodeDeserializer::new_from_buf(&buf), + ) + .unwrap(); + assert_eq!(info.private, deserialized.private); + + let deserialized_dyn = ::bencode::dyn_from_bytes::(&buf).unwrap(); + let hm = match deserialized_dyn { + bencode::BencodeValue::Dict(hm) => hm, + _ => panic!("expected dict"), + }; + match (private, hm.get(&ByteBuf(b"private"))) { + (true, Some(BencodeValue::Integer(1))) => {} + (false, None) => {} + (_, v) => { + panic!("unexpected value for \"private\": {v:?}") + } + } + } + } }