Skip to main content

irontide_core/
resume_data.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::WebSeedStats;
6
7fn default_neg_one() -> i64 {
8    -1
9}
10
11/// A partial piece that was in progress when the torrent was paused/stopped.
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct UnfinishedPiece {
14    /// Piece index.
15    pub piece: i64,
16    /// Bitmask of which blocks within the piece have been downloaded.
17    #[serde(with = "serde_bytes")]
18    pub bitmask: Vec<u8>,
19}
20
21/// libtorrent-compatible fast-resume data in bencode format.
22///
23/// This struct matches libtorrent's resume file format so that resume data
24/// can be read/written by both Torrent and libtorrent-based clients.
25/// Every field uses `#[serde(rename = "...")]` to match libtorrent's exact
26/// bencode dictionary keys.
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct FastResumeData {
29    /// Always "libtorrent resume file".
30    #[serde(rename = "file-format")]
31    pub file_format: String,
32
33    /// Always 1.
34    #[serde(rename = "file-version")]
35    pub file_version: i64,
36
37    /// 20-byte SHA1 info hash.
38    #[serde(rename = "info-hash")]
39    #[serde(with = "serde_bytes")]
40    pub info_hash: Vec<u8>,
41
42    /// Torrent name.
43    #[serde(rename = "name")]
44    pub name: String,
45
46    /// Path where files are saved.
47    #[serde(rename = "save_path")]
48    pub save_path: String,
49
50    /// Bitfield indicating which pieces are complete.
51    #[serde(rename = "pieces")]
52    #[serde(with = "serde_bytes")]
53    pub pieces: Vec<u8>,
54
55    /// Partially downloaded pieces.
56    #[serde(rename = "unfinished")]
57    #[serde(skip_serializing_if = "Vec::is_empty", default)]
58    pub unfinished: Vec<UnfinishedPiece>,
59
60    /// Total bytes uploaded.
61    #[serde(rename = "total_uploaded")]
62    pub total_uploaded: i64,
63
64    /// Total bytes downloaded.
65    #[serde(rename = "total_downloaded")]
66    pub total_downloaded: i64,
67
68    /// Total time active in seconds.
69    #[serde(rename = "active_time")]
70    pub active_time: i64,
71
72    /// Total time spent seeding in seconds.
73    #[serde(rename = "seeding_time")]
74    pub seeding_time: i64,
75
76    /// Total time in finished state in seconds.
77    #[serde(rename = "finished_time")]
78    pub finished_time: i64,
79
80    /// POSIX timestamp when the torrent was added.
81    #[serde(rename = "added_time")]
82    pub added_time: i64,
83
84    /// POSIX timestamp when the torrent completed.
85    #[serde(rename = "completed_time")]
86    #[serde(default)]
87    pub completed_time: i64,
88
89    /// POSIX timestamp of last download activity.
90    #[serde(rename = "last_download")]
91    #[serde(default)]
92    pub last_download: i64,
93
94    /// POSIX timestamp of last upload activity.
95    #[serde(rename = "last_upload")]
96    #[serde(default)]
97    pub last_upload: i64,
98
99    /// Whether the torrent is paused (0 or 1).
100    #[serde(rename = "paused")]
101    #[serde(default)]
102    pub paused: i64,
103
104    /// Whether the torrent is queued by auto-manage (0 or 1).
105    #[serde(rename = "queued")]
106    #[serde(default)]
107    pub queued: i64,
108
109    /// Whether the torrent is auto-managed.
110    #[serde(rename = "auto_managed")]
111    #[serde(default)]
112    pub auto_managed: i64,
113
114    /// Queue position (-1 = not queued).
115    #[serde(rename = "queue_position")]
116    #[serde(default = "default_neg_one")]
117    pub queue_position: i64,
118
119    /// Whether sequential download is enabled.
120    #[serde(rename = "sequential_download")]
121    #[serde(default)]
122    pub sequential_download: i64,
123
124    /// Whether seed mode is enabled.
125    #[serde(rename = "seed_mode")]
126    #[serde(default)]
127    pub seed_mode: i64,
128
129    /// Tracker tiers (list of lists of tracker URLs).
130    #[serde(rename = "trackers")]
131    #[serde(skip_serializing_if = "Vec::is_empty", default)]
132    pub trackers: Vec<Vec<String>>,
133
134    /// Compact IPv4 peers (6 bytes each: 4 IP + 2 port).
135    #[serde(rename = "peers")]
136    #[serde(with = "serde_bytes")]
137    #[serde(default)]
138    pub peers: Vec<u8>,
139
140    /// Compact IPv6 peers (18 bytes each: 16 IP + 2 port).
141    #[serde(rename = "peers6")]
142    #[serde(with = "serde_bytes")]
143    #[serde(default)]
144    pub peers6: Vec<u8>,
145
146    /// Per-file priority values.
147    #[serde(rename = "file_priority")]
148    #[serde(skip_serializing_if = "Vec::is_empty", default)]
149    pub file_priority: Vec<i64>,
150
151    /// Per-piece priority values.
152    #[serde(rename = "piece_priority")]
153    #[serde(skip_serializing_if = "Vec::is_empty", default)]
154    pub piece_priority: Vec<i64>,
155
156    /// Upload rate limit in bytes/sec (-1 = unlimited).
157    #[serde(rename = "upload_rate_limit")]
158    #[serde(default)]
159    pub upload_rate_limit: i64,
160
161    /// Download rate limit in bytes/sec (-1 = unlimited).
162    #[serde(rename = "download_rate_limit")]
163    #[serde(default)]
164    pub download_rate_limit: i64,
165
166    /// Max connections for this torrent (-1 = unlimited).
167    #[serde(rename = "max_connections")]
168    #[serde(default)]
169    pub max_connections: i64,
170
171    /// Max upload slots for this torrent (-1 = unlimited).
172    #[serde(rename = "max_uploads")]
173    #[serde(default)]
174    pub max_uploads: i64,
175
176    /// Raw bencoded info dictionary (for magnet links that have resolved).
177    #[serde(rename = "info")]
178    #[serde(with = "serde_bytes")]
179    #[serde(skip_serializing_if = "Option::is_none", default)]
180    pub info: Option<Vec<u8>>,
181
182    /// BEP 16: whether super seeding was enabled.
183    #[serde(rename = "super_seeding")]
184    #[serde(default)]
185    pub super_seeding: i64,
186
187    /// BEP 19 web seed URLs (GetRight-style).
188    #[serde(rename = "url_seeds")]
189    #[serde(skip_serializing_if = "Vec::is_empty", default)]
190    pub url_seeds: Vec<String>,
191
192    /// BEP 17 HTTP seed URLs (Hoffman-style).
193    #[serde(rename = "http_seeds")]
194    #[serde(skip_serializing_if = "Vec::is_empty", default)]
195    pub http_seeds: Vec<String>,
196
197    /// SHA-256 v2 info hash (32 bytes, BEP 52).
198    #[serde(rename = "info-hash2")]
199    #[serde(with = "serde_bytes")]
200    #[serde(skip_serializing_if = "Option::is_none", default)]
201    pub info_hash2: Option<Vec<u8>>,
202
203    /// Cached piece-layer Merkle hashes per file.
204    /// Key: hex-encoded file root hash. Value: concatenated 32-byte piece hashes.
205    /// Allows skipping piece-layer hash requests on resume.
206    #[serde(rename = "trees")]
207    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
208    pub trees: HashMap<String, Vec<u8>>,
209
210    // ── M170: qBt v2 *arr-minimal surface ──
211    //
212    // These three fields persist the per-torrent category label and torrent
213    // metadata (creator + creation timestamp) so that they survive session
214    // restart. All three use `skip_serializing_if = "Option::is_none"` so
215    // older resume files without them deserialize cleanly (missing key →
216    // `None`), and newer resume files only include them when a value is set.
217    /// User-assigned category label (qBt-compat). `None` = uncategorised.
218    #[serde(rename = "category")]
219    #[serde(skip_serializing_if = "Option::is_none", default)]
220    pub category: Option<String>,
221    /// Torrent creator string from `TorrentMetaV1.created_by`. `None` if the
222    /// torrent was added via magnet before metadata resolved.
223    #[serde(rename = "created_by")]
224    #[serde(skip_serializing_if = "Option::is_none", default)]
225    pub created_by: Option<String>,
226    /// UNIX timestamp (seconds) when the torrent was authored, from
227    /// `TorrentMetaV1.creation_date`. `None` if not present in the .torrent
228    /// file or if metadata has not yet resolved for a magnet-added torrent.
229    #[serde(rename = "creation_date")]
230    #[serde(skip_serializing_if = "Option::is_none", default)]
231    pub creation_date: Option<i64>,
232
233    // ── M171: qBt v2 parity ──
234    /// User-assigned tags (qBt-compat). Multi-valued per torrent. Empty vec
235    /// when no tags set; `skip_serializing_if = "Vec::is_empty"` keeps older
236    /// resume files (which have no `tags` key) bit-identical on save.
237    #[serde(rename = "tags")]
238    #[serde(skip_serializing_if = "Vec::is_empty", default)]
239    pub tags: Vec<String>,
240
241    // ── M178: web seed stats ──
242    /// Per-URL web-seed stats (BEP 17/19), keyed by URL. Empty map when no
243    /// stats accumulated; `skip_serializing_if` keeps older resume files
244    /// (which have no `web_seed_stats` key) bit-identical on save.
245    /// Backward-compat: `#[serde(default)]` means legacy resume files load
246    /// with an empty map.
247    #[serde(rename = "web_seed_stats")]
248    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
249    pub web_seed_stats: HashMap<String, WebSeedStats>,
250}
251
252impl FastResumeData {
253    /// Create a new `FastResumeData` with format markers pre-filled and all
254    /// other fields zeroed/empty. Rate limits default to -1 (unlimited).
255    #[must_use]
256    pub fn new(info_hash: Vec<u8>, name: String, save_path: String) -> Self {
257        Self {
258            file_format: "libtorrent resume file".into(),
259            file_version: 1,
260            info_hash,
261            name,
262            save_path,
263            pieces: Vec::new(),
264            unfinished: Vec::new(),
265            total_uploaded: 0,
266            total_downloaded: 0,
267            active_time: 0,
268            seeding_time: 0,
269            finished_time: 0,
270            added_time: 0,
271            completed_time: 0,
272            last_download: 0,
273            last_upload: 0,
274            paused: 0,
275            queued: 0,
276            auto_managed: 0,
277            queue_position: -1,
278            sequential_download: 0,
279            seed_mode: 0,
280            trackers: Vec::new(),
281            peers: Vec::new(),
282            peers6: Vec::new(),
283            file_priority: Vec::new(),
284            piece_priority: Vec::new(),
285            upload_rate_limit: -1,
286            download_rate_limit: -1,
287            max_connections: -1,
288            max_uploads: -1,
289            super_seeding: 0,
290            info: None,
291            url_seeds: Vec::new(),
292            http_seeds: Vec::new(),
293            info_hash2: None,
294            trees: HashMap::new(),
295            // M170 fields default to None.
296            category: None,
297            created_by: None,
298            creation_date: None,
299            // M171: tags default to empty vec.
300            tags: Vec::new(),
301            // M178: web seed stats default to empty map.
302            web_seed_stats: HashMap::new(),
303        }
304    }
305}
306
307#[cfg(test)]
308mod tests {
309    use super::*;
310    use pretty_assertions::assert_eq;
311
312    #[test]
313    fn fast_resume_data_bencode_round_trip() {
314        let mut resume =
315            FastResumeData::new(vec![0xAA; 20], "test-torrent".into(), "/downloads".into());
316        resume.total_uploaded = 1024 * 1024;
317        resume.total_downloaded = 2048 * 1024;
318        resume.active_time = 3600;
319        resume.added_time = 1_700_000_000;
320        resume.trackers = vec![
321            vec!["http://tracker1.example.com/announce".into()],
322            vec![
323                "http://tracker2.example.com/announce".into(),
324                "http://tracker3.example.com/announce".into(),
325            ],
326        ];
327        resume.pieces = vec![0xFF; 10];
328
329        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
330        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
331        assert_eq!(resume, decoded);
332    }
333
334    #[test]
335    fn unfinished_piece_bencode_round_trip() {
336        let piece = UnfinishedPiece {
337            piece: 42,
338            bitmask: vec![0b1010_1010, 0b0101_0101],
339        };
340
341        let encoded = irontide_bencode::to_bytes(&piece).unwrap();
342        let decoded: UnfinishedPiece = irontide_bencode::from_bytes(&encoded).unwrap();
343        assert_eq!(piece, decoded);
344    }
345
346    #[test]
347    fn resume_data_with_unfinished_pieces() {
348        let mut resume = FastResumeData::new(
349            vec![0xBB; 20],
350            "partial-torrent".into(),
351            "/downloads".into(),
352        );
353        resume.unfinished = vec![
354            UnfinishedPiece {
355                piece: 5,
356                bitmask: vec![0xFF, 0x0F],
357            },
358            UnfinishedPiece {
359                piece: 12,
360                bitmask: vec![0xF0],
361            },
362        ];
363
364        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
365        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
366        assert_eq!(resume, decoded);
367    }
368
369    #[test]
370    fn default_fields_serialize_correctly() {
371        let resume = FastResumeData::new(vec![0x00; 20], "minimal".into(), "/tmp".into());
372
373        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
374        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
375        assert_eq!(resume, decoded);
376
377        // Verify default values survived the round-trip.
378        assert_eq!(decoded.total_uploaded, 0);
379        assert_eq!(decoded.total_downloaded, 0);
380        assert_eq!(decoded.paused, 0);
381        assert_eq!(decoded.upload_rate_limit, -1);
382        assert_eq!(decoded.download_rate_limit, -1);
383        assert_eq!(decoded.max_connections, -1);
384        assert_eq!(decoded.max_uploads, -1);
385        assert!(decoded.trackers.is_empty());
386        assert!(decoded.unfinished.is_empty());
387        assert!(decoded.file_priority.is_empty());
388        assert!(decoded.info.is_none());
389    }
390
391    #[test]
392    fn info_dict_embedding_round_trip() {
393        let mut resume =
394            FastResumeData::new(vec![0xCC; 20], "with-info".into(), "/downloads".into());
395        // Simulate a raw bencoded info dict.
396        resume.info = Some(
397            b"d4:name10:test-torte12:piece lengthi262144e6:pieces20:AAAAAAAAAAAAAAAAAAAAe".to_vec(),
398        );
399
400        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
401        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
402        assert_eq!(resume, decoded);
403        assert!(decoded.info.is_some());
404        assert_eq!(decoded.info.unwrap().len(), resume.info.unwrap().len());
405    }
406
407    #[test]
408    fn resume_data_queue_position_default() {
409        let rd = FastResumeData::new(vec![0; 20], "test".into(), "/tmp".into());
410        assert_eq!(rd.queue_position, -1);
411    }
412
413    #[test]
414    fn format_markers_correct() {
415        let resume = FastResumeData::new(vec![0x00; 20], "test".into(), "/tmp".into());
416        assert_eq!(resume.file_format, "libtorrent resume file");
417        assert_eq!(resume.file_version, 1);
418    }
419
420    #[test]
421    fn resume_data_url_seeds_round_trip() {
422        let mut resume =
423            FastResumeData::new(vec![0xDD; 20], "web-seed-test".into(), "/downloads".into());
424        resume.url_seeds = vec![
425            "http://example.com/files".into(),
426            "http://mirror.example.com/".into(),
427        ];
428
429        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
430        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
431        assert_eq!(decoded.url_seeds, resume.url_seeds);
432    }
433
434    #[test]
435    fn resume_data_http_seeds_round_trip() {
436        let mut resume =
437            FastResumeData::new(vec![0xEE; 20], "http-seed-test".into(), "/downloads".into());
438        resume.http_seeds = vec!["http://seed.example.com/seed".into()];
439
440        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
441        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
442        assert_eq!(decoded.http_seeds, resume.http_seeds);
443    }
444
445    #[test]
446    fn resume_data_super_seeding_round_trip() {
447        let mut resume = FastResumeData::new(
448            vec![0xFF; 20],
449            "super-seed-test".into(),
450            "/downloads".into(),
451        );
452        resume.super_seeding = 1;
453
454        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
455        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
456        assert_eq!(decoded.super_seeding, 1);
457
458        // Default should be 0
459        let default_resume = FastResumeData::new(vec![0; 20], "test".into(), "/tmp".into());
460        assert_eq!(default_resume.super_seeding, 0);
461    }
462
463    #[test]
464    fn resume_data_v2_fields_round_trip() {
465        let mut resume =
466            FastResumeData::new(vec![0xAA; 20], "v2-torrent".into(), "/downloads".into());
467        resume.info_hash2 = Some(vec![0xBB; 32]);
468        resume.trees.insert(
469            hex::encode([0xCC; 32]),
470            vec![0xDD; 64], // 2 piece hashes
471        );
472
473        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
474        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
475        assert_eq!(decoded.info_hash2, Some(vec![0xBB; 32]));
476        assert_eq!(decoded.trees.len(), 1);
477    }
478
479    #[test]
480    fn resume_data_v1_backward_compat() {
481        let resume = FastResumeData::new(vec![0x00; 20], "v1-torrent".into(), "/tmp".into());
482        assert!(resume.info_hash2.is_none());
483        assert!(resume.trees.is_empty());
484
485        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
486        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
487        assert!(decoded.info_hash2.is_none());
488        assert!(decoded.trees.is_empty());
489    }
490
491    #[test]
492    fn resume_data_v2_empty_trees_not_serialized() {
493        let resume = FastResumeData::new(vec![0x00; 20], "minimal".into(), "/tmp".into());
494        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
495        // "5:trees" (bencode key) should not appear in output when empty
496        let encoded_str = String::from_utf8_lossy(&encoded);
497        assert!(!encoded_str.contains("5:trees"));
498    }
499
500    #[test]
501    fn resume_data_empty_seeds_not_serialized() {
502        let resume = FastResumeData::new(vec![0x00; 20], "no-seeds".into(), "/tmp".into());
503        assert!(resume.url_seeds.is_empty());
504        assert!(resume.http_seeds.is_empty());
505
506        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
507        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
508        assert!(decoded.url_seeds.is_empty());
509        assert!(decoded.http_seeds.is_empty());
510    }
511
512    #[test]
513    fn resume_data_m170_fields_round_trip() {
514        // M170: category, created_by, creation_date must survive a
515        // bencode round-trip exactly.
516        let mut resume =
517            FastResumeData::new(vec![0xA1; 20], "m170-torrent".into(), "/downloads".into());
518        resume.category = Some("sonarr".into());
519        resume.created_by = Some("irontide/0.170".into());
520        resume.creation_date = Some(1_700_000_000);
521
522        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
523        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
524        assert_eq!(decoded.category.as_deref(), Some("sonarr"));
525        assert_eq!(decoded.created_by.as_deref(), Some("irontide/0.170"));
526        assert_eq!(decoded.creation_date, Some(1_700_000_000));
527        assert_eq!(resume, decoded);
528    }
529
530    #[test]
531    fn resume_data_m170_backward_compat_missing_fields() {
532        // An "old" resume file has no category/created_by/creation_date
533        // keys at all. Deserialising it must produce None for all three
534        // fields, not fail.
535        let mut resume =
536            FastResumeData::new(vec![0xB2; 20], "legacy-torrent".into(), "/downloads".into());
537        // Synthesise the "pre-M170" shape: encode with the new type but
538        // with all M170 fields None (skip_serializing_if strips them), then
539        // decode back. The wire form must not contain the M170 keys.
540        resume.category = None;
541        resume.created_by = None;
542        resume.creation_date = None;
543
544        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
545        let encoded_str = String::from_utf8_lossy(&encoded);
546        // skip_serializing_if must strip each missing field.
547        assert!(!encoded_str.contains("8:category"));
548        assert!(!encoded_str.contains("10:created_by"));
549        assert!(!encoded_str.contains("13:creation_date"));
550
551        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
552        assert!(decoded.category.is_none());
553        assert!(decoded.created_by.is_none());
554        assert!(decoded.creation_date.is_none());
555    }
556
557    #[test]
558    fn resume_data_m171_tags_round_trip() {
559        // M171: tags must round-trip through bencode cleanly.
560        let mut resume =
561            FastResumeData::new(vec![0xC3; 20], "m171-tags".into(), "/downloads".into());
562        resume.tags = vec!["sonarr".into(), "kids".into()];
563
564        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
565        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
566        assert_eq!(decoded.tags, vec!["sonarr".to_string(), "kids".to_string()]);
567        assert_eq!(resume, decoded);
568    }
569
570    #[test]
571    fn resume_data_m171_tags_backward_compat_missing_field() {
572        // Empty tags vec must not appear in the wire form (skip_serializing_if
573        // gate) so older decoders are none the wiser. And a decode round-trip
574        // yields an empty vec on the new-style end.
575        let resume =
576            FastResumeData::new(vec![0xD4; 20], "legacy-no-tags".into(), "/downloads".into());
577        assert!(resume.tags.is_empty());
578
579        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
580        let encoded_str = String::from_utf8_lossy(&encoded);
581        assert!(
582            !encoded_str.contains("4:tags"),
583            "empty tags vec must not serialize: got {encoded_str:?}",
584        );
585
586        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
587        assert!(decoded.tags.is_empty());
588    }
589
590    #[test]
591    fn resume_data_hybrid_both_hashes() {
592        // Hybrid torrents store both v1 (SHA-1, 20 bytes) and v2 (SHA-256, 32 bytes)
593        let mut resume =
594            FastResumeData::new(vec![0x11; 20], "hybrid-torrent".into(), "/downloads".into());
595        resume.info_hash2 = Some(vec![0x22; 32]);
596        resume.trees.insert(
597            hex::encode([0x33; 32]),
598            vec![0x44; 96], // 3 piece hashes
599        );
600
601        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
602        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
603
604        // Both hashes present and distinct
605        assert_eq!(decoded.info_hash, vec![0x11; 20]);
606        assert_eq!(decoded.info_hash2.as_deref(), Some([0x22; 32].as_ref()));
607
608        // Trees preserved
609        assert_eq!(decoded.trees.len(), 1);
610        let layer = decoded.trees.values().next().unwrap();
611        assert_eq!(layer.len(), 96);
612    }
613
614    #[test]
615    fn resume_data_missing_queued_field_defaults_to_zero() {
616        let resume = FastResumeData::new(vec![0xaa; 20], "test".into(), "/tmp".into());
617        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
618
619        // Decode and verify queued defaults to 0
620        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
621        assert_eq!(decoded.queued, 0);
622    }
623
624    #[test]
625    fn resume_data_queued_field_round_trips() {
626        let mut resume = FastResumeData::new(vec![0xbb; 20], "queued-test".into(), "/dl".into());
627        resume.queued = 1;
628
629        let encoded = irontide_bencode::to_bytes(&resume).unwrap();
630        let decoded: FastResumeData = irontide_bencode::from_bytes(&encoded).unwrap();
631        assert_eq!(decoded.queued, 1);
632        assert_eq!(decoded.paused, 0);
633    }
634}