1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
//! Module for `.torrent` files ([v1](http://bittorrent.org/beps/bep_0003.html))
//! related parsing/encoding/creation.

use bencode::BencodeElem;
use itertools::Itertools;
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use sha1::{Digest, Sha1};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use LavaTorrentError;

mod build;
mod read;
mod write;

const PIECE_STRING_LENGTH: usize = 20;

// The escaping rules for magnet URIs are not specified in BEP9,
// so we simply escape '&'. We do not escape space here, since
// percent_encoding escapes it to '%20' instead of '+'.
// Instead, we manually replace it with '+' later in the code.
// This means that we do have to escape actual '+'s though!
const MAGNET_COMPONENT: &AsciiSet = &CONTROLS.add(b'&').add(b'+');

/// Corresponds to a bencode dictionary.
pub type Dictionary = HashMap<String, BencodeElem>;
/// Corresponds to the `announce-list` in [BEP 12](http://bittorrent.org/beps/bep_0012.html).
pub type AnnounceList = Vec<Vec<String>>;
/// A piece in `pieces`--the SHA1 hash of a torrent block.
pub type Piece = Vec<u8>;
/// Corresponds to a bencode integer. The underlying type is `i64`.
/// Technically a bencode integer has no size limit, but it is not
/// so in the current implementation. By using a type alias it is
/// easier to change the underlying type in the future.
pub type Integer = i64;

/// A file contained in a torrent.
///
/// Modeled after the specifications
/// in [BEP 3](http://bittorrent.org/beps/bep_0003.html). Unknown/extension
/// fields will be placed in `extra_fields`. If you need
/// any of those extra fields you would have to parse it yourself.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct File {
    /// File size in bytes.
    pub length: Integer,
    /// File path, relative to [`Torrent`](struct.Torrent.html)'s `name` field.
    pub path: PathBuf,
    /// Fields not defined in [BEP 3](http://bittorrent.org/beps/bep_0003.html).
    pub extra_fields: Option<Dictionary>,
}

/// Everything found in a *.torrent* file.
///
/// Modeled after the specifications
/// in [BEP 3](http://bittorrent.org/beps/bep_0003.html) and
/// [BEP 12](http://bittorrent.org/beps/bep_0012.html). Unknown/extension
/// fields will be placed in `extra_fields` (if the unknown
/// fields are found in the `info` dictionary then they are placed in
/// `extra_info_fields`). If you need any of those extra fields you would
/// have to parse it yourself.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Torrent {
    /// URL of the torrent's tracker.
    pub announce: Option<String>,
    /// Announce list as defined in [BEP 12](http://bittorrent.org/beps/bep_0012.html).
    pub announce_list: Option<AnnounceList>,
    /// Total torrent size in bytes (i.e. sum of all files' sizes).
    pub length: Integer,
    /// If the torrent contains only 1 file then `files` is `None`.
    pub files: Option<Vec<File>>,
    /// If the torrent contains only 1 file then `name` is the file name.
    /// Otherwise it's the suggested root directory's name.
    pub name: String,
    /// Block size in bytes.
    pub piece_length: Integer,
    /// SHA1 hashs of each block.
    pub pieces: Vec<Piece>,
    /// Top-level fields not defined in [BEP 3](http://bittorrent.org/beps/bep_0003.html).
    pub extra_fields: Option<Dictionary>,
    /// Fields in `info` not defined in [BEP 3](http://bittorrent.org/beps/bep_0003.html).
    pub extra_info_fields: Option<Dictionary>,
}

/// Builder for creating `Torrent`s from files.
///
/// This struct is used for **creating** `Torrent`s, so that you can
/// encode/serialize them to *.torrent* files. If you want to read
/// existing *.torrent* files then use [`Torrent::read_from_file()`]
/// or [`Torrent::read_from_bytes()`].
///
/// Required fields: `path` and `piece_length`.
/// They are set when calling the constructor [`new()`].
///
/// Optional fields can be set by calling the corresponding methods
///  (e.g. [`set_announce()`]). Fields can be updated in the same way.
///
/// # Notes
/// **\*nix hidden files/dirs are ignored.**
///
/// Reasoning:
/// when handling these special "files", there are many decisions to make:
/// - Should they be ignored, included, or selectively ignored/included?
/// - Should included/ignored entries be marked specially (e.g. [BEP 47])?
/// - Should users be allowed to configure the settings?
/// - If users can configure the settings, what would be the ideal defaults?
/// - ...
///
/// Apparently it's not easy to make these decisions.
/// Therefore these files are ignored for now.
/// Clients like Deluge and qBittorrent also ignore hidden entries.
///
/// [`Torrent::read_from_file()`]: struct.Torrent.html#method.read_from_file
/// [`Torrent::read_from_bytes()`]: struct.Torrent.html#method.read_from_bytes
/// [`new()`]: #method.new
/// [`set_announce()`]: #method.set_announce
/// [BEP 47]: http://bittorrent.org/beps/bep_0047.html
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TorrentBuilder {
    announce: Option<String>,
    announce_list: Option<AnnounceList>,
    name: Option<String>,
    path: PathBuf,
    piece_length: Integer,
    extra_fields: Option<Dictionary>,
    extra_info_fields: Option<Dictionary>,
    is_private: bool,
}

impl File {
    /// Construct the `File`'s absolute path using `parent`.
    ///
    /// Caller has to ensure that `parent` is an absolute path.
    /// Otherwise an error would be returned.
    ///
    /// This method effectively appends/joins `self.path` to `parent`.
    pub fn absolute_path<P>(&self, parent: P) -> Result<PathBuf, LavaTorrentError>
    where
        P: AsRef<Path>,
    {
        let result = parent.as_ref().join(&self.path);
        if result.is_absolute() {
            Ok(result)
        } else {
            Err(LavaTorrentError::InvalidArgument(Cow::Borrowed(
                "Joined path is not absolute.",
            )))
        }
    }
}

impl Torrent {
    /// Construct the `info` dict based on the fields of `self`.
    ///
    /// Certain operations on torrents, such as calculating info
    /// hashs, require the extracted `info` dict. This
    /// convenience method does that.
    ///
    /// Note that the `info` dict
    /// is constructed each time this method is called (i.e.
    /// the return value is not cached). If caching is needed
    /// then the caller should handle that.
    ///
    /// Since `self` is taken by reference, and the result is
    /// returned by value, certain values will be cloned. Please
    /// be aware of this overhead.
    pub fn construct_info(&self) -> BencodeElem {
        let mut info: HashMap<String, BencodeElem> = HashMap::new();

        if let Some(ref files) = self.files {
            info.insert(
                "files".to_owned(),
                BencodeElem::List(
                    files
                        .clone()
                        .into_iter()
                        .map(|file| file.into_bencode_elem())
                        .collect(),
                ),
            );
        } else {
            info.insert("length".to_owned(), BencodeElem::Integer(self.length));
        }

        info.insert("name".to_owned(), BencodeElem::String(self.name.clone()));
        info.insert(
            "piece length".to_owned(),
            BencodeElem::Integer(self.piece_length),
        );
        info.insert(
            "pieces".to_owned(),
            BencodeElem::Bytes(self.pieces.clone().into_iter().flatten().collect()),
        );

        if let Some(ref extra_info_fields) = self.extra_info_fields {
            info.extend(extra_info_fields.clone());
        }

        BencodeElem::Dictionary(info)
    }

    /// Calculate the `Torrent`'s info hash as defined in
    /// [BEP 3](http://bittorrent.org/beps/bep_0003.html).
    ///
    /// Note that the calculated info hash is not cached.
    /// So if this method is called multiple times, multiple
    /// calculations will be performed. To avoid that, the
    /// caller should cache the return value as needed.
    pub fn info_hash(&self) -> String {
        format!("{:x}", Sha1::digest(&self.construct_info().encode()))
    }

    /// Calculate the `Torrent`'s info hash as defined in
    /// [BEP 3](http://bittorrent.org/beps/bep_0003.html).
    ///
    /// Note that the calculated info hash is not cached.
    /// So if this method is called multiple times, multiple
    /// calculations will be performed. To avoid that, the
    /// caller should cache the return value as needed.
    pub fn info_hash_bytes(&self) -> Vec<u8> {
        Sha1::digest(&self.construct_info().encode()).to_vec()
    }

    /// Calculate the `Torrent`'s magnet link as defined in
    /// [BEP 9](http://bittorrent.org/beps/bep_0009.html).
    ///
    /// The `dn` parameter is set to `self.name`.
    ///
    /// Either `self.announce` or all trackers in `self.announce_list` will be used,
    /// meaning that there might be multiple `tr` entries. We don't use both because
    /// per [BEP 12](http://bittorrent.org/beps/bep_0012.html):
    /// "If the client is compatible with the multitracker specification, and if the
    /// `announce-list` key is present, the client will ignore the `announce` key
    /// and only use the URLs in `announce-list`."
    ///
    /// If neither `self.announce` nor `self.announce_list` is present, the output
    /// won't contain any `tr` parameter.
    ///
    /// The `x.pe` parameter (for peer addresses) is currently not supported.
    ///
    /// `self.extra_fields["url-list"]` will be used to construct `ws` parameters.
    /// It must be either a string or a list of strings.
    pub fn magnet_link(&self) -> Result<String, LavaTorrentError> {
        fn encode_component(from: &str) -> String {
            // percent_encoding escapes space as '%20', which is not accepted
            // by clients such as transmission, so we escape it manually to '+'.
            utf8_percent_encode(from, MAGNET_COMPONENT)
                .to_string()
                .replace(' ', "+")
        }

        let tr = if let Some(ref list) = self.announce_list {
            list.iter()
                .format_with("", |tier, f| {
                    f(&format_args!(
                        "{}",
                        tier.iter().format_with("", |url, f| f(&format_args!(
                            "&tr={}",
                            encode_component(url)
                        )))
                    ))
                })
                .to_string()
        } else if let Some(ref announce) = self.announce {
            format!("&tr={}", encode_component(announce))
        } else {
            String::new()
        };

        let ws = match self
            .extra_fields
            .as_ref()
            .and_then(|fields| fields.get("url-list"))
        {
            Some(BencodeElem::String(seed)) => Some(vec![seed]),
            Some(BencodeElem::List(ref seeds)) => Some(
                seeds
                    .iter()
                    .map(|elem| match elem {
                        BencodeElem::String(url) => Ok(url),
                        _ => {
                            return Err(LavaTorrentError::MalformedTorrent(Cow::Borrowed(
                                r#""url-list" is a list but contains a non-string element."#,
                            )))
                        }
                    })
                    .collect::<Result<Vec<&String>, LavaTorrentError>>()?,
            ),
            Some(_) => {
                return Err(LavaTorrentError::MalformedTorrent(Cow::Borrowed(
                    r#""url-list" is neither a string nor a list."#,
                )))
            }
            None => None,
        };
        let ws = match ws {
            Some(ws) => ws
                .iter()
                .format_with("", |&url, f| {
                    f(&format_args!("&ws={}", encode_component(url)))
                })
                .to_string(),
            None => String::new(),
        };

        Ok(format!(
            "magnet:?xt=urn:btih:{}&dn={}{}{}",
            self.info_hash(),
            self.name,
            tr,
            ws,
        ))
    }

    /// Check if this torrent is private as defined in
    /// [BEP 27](http://bittorrent.org/beps/bep_0027.html).
    ///
    /// Returns `true` if `private` maps to a bencode integer `1`.
    /// Returns `false` otherwise.
    pub fn is_private(&self) -> bool {
        if let Some(ref dict) = self.extra_info_fields {
            match dict.get("private") {
                Some(&BencodeElem::Integer(val)) => val == 1,
                Some(_) => false,
                None => false,
            }
        } else {
            false
        }
    }
}

impl fmt::Display for File {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
            f,
            "{}\n\
             -size: {} bytes",
            self.path.as_path().display(),
            self.length
        )?;

        if let Some(ref fields) = self.extra_fields {
            write!(
                f,
                "{}",
                fields
                    .iter()
                    .sorted_by_key(|&(key, _)| key.as_bytes())
                    .format_with("", |(k, v), f| f(&format_args!("-{}: {}\n", k, v)))
            )?;
        }

        writeln!(f, "========================================")
    }
}

impl fmt::Display for Torrent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{}.torrent", self.name)?;
        if let Some(ref announce) = self.announce {
            writeln!(f, "-announce: {}", announce)?;
        }
        if let Some(ref tiers) = self.announce_list {
            writeln!(
                f,
                "-announce-list: [{}]",
                tiers.iter().format_with(", ", |tier, f| f(&format_args!(
                    "[{}]",
                    ::itertools::join(tier, ", ")
                )))
            )?;
        }
        writeln!(f, "-size: {} bytes", self.length)?;
        writeln!(f, "-piece length: {} bytes", self.piece_length)?;

        if let Some(ref fields) = self.extra_fields {
            write!(
                f,
                "{}",
                fields
                    .iter()
                    .sorted_by_key(|&(key, _)| key.as_bytes())
                    .format_with("", |(k, v), f| f(&format_args!("-{}: {}\n", k, v)))
            )?;
        }

        if let Some(ref fields) = self.extra_info_fields {
            write!(
                f,
                "{}",
                fields
                    .iter()
                    .sorted_by_key(|&(key, _)| key.as_bytes())
                    .format_with("", |(k, v), f| f(&format_args!("-{}: {}\n", k, v)))
            )?;
        }

        if let Some(ref files) = self.files {
            writeln!(f, "-files:")?;
            for (counter, file) in files.iter().enumerate() {
                writeln!(f, "[{}] {}", counter + 1, file)?;
            }
        }

        writeln!(
            f,
            "-pieces: [{}]",
            self.pieces
                .iter()
                .format_with(", ", |piece, f| f(&format_args!(
                    "[{:02x}]",
                    piece.iter().format("")
                ))),
        )
    }
}

#[cfg(test)]
mod file_tests {
    use super::*;

    #[test]
    fn absolute_path_ok() {
        let file = File {
            length: 42,
            path: PathBuf::from("dir1/file"),
            extra_fields: None,
        };

        assert_eq!(
            file.absolute_path("/root").unwrap(),
            PathBuf::from("/root/dir1/file")
        );
    }

    #[test]
    fn absolute_path_not_absolute() {
        let file = File {
            length: 42,
            path: PathBuf::from("dir1/file"),
            extra_fields: None,
        };

        match file.absolute_path("root") {
            Err(LavaTorrentError::InvalidArgument(m)) => {
                assert_eq!(m, "Joined path is not absolute.");
            }
            _ => assert!(false),
        }
    }
}

#[cfg(test)]
mod torrent_tests {
    use super::*;
    use std::iter::FromIterator;

    #[test]
    fn construct_info_ok() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: Some(HashMap::from_iter(
                vec![("key".to_owned(), bencode_elem!("val"))].into_iter(),
            )),
        };

        assert_eq!(
            torrent.construct_info(),
            bencode_elem!({
                ("length", 4),
                ("name", "sample"),
                ("piece length", 2),
                ("pieces", (1, 2, 3, 4)),
                ("key", "val"),
            }),
        );
    }

    #[test]
    fn info_hash_ok() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.info_hash(),
            "074f42efaf8267f137f114f722d4e7d1dcbfbda5".to_owned(),
        );
    }

    #[test]
    fn magnet_link_ok() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.magnet_link().unwrap(),
            "magnet:?xt=urn:btih:074f42efaf8267f137f114f722d4e7d1dcbfbda5\
             &dn=sample&tr=url"
                .to_owned()
        );
    }

    #[test]
    fn magnet_link_with_announce_list() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: Some(vec![
                vec!["url1".to_owned()],
                vec!["url2".to_owned(), "url3".to_owned()],
            ]),
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.magnet_link().unwrap(),
            "magnet:?xt=urn:btih:074f42efaf8267f137f114f722d4e7d1dcbfbda5\
             &dn=sample&tr=url1&tr=url2&tr=url3"
                .to_owned()
        );
    }

    #[test]
    fn magnet_link_with_web_seed() {
        let torrent = Torrent {
            announce: None,
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: Some(HashMap::from([(
                "url-list".to_owned(),
                BencodeElem::String("https://example.org/path".to_owned()),
            )])),
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.magnet_link().unwrap(),
            "magnet:?xt=urn:btih:074f42efaf8267f137f114f722d4e7d1dcbfbda5\
             &dn=sample&ws=https://example.org/path"
                .to_owned()
        );
    }

    #[test]
    fn magnet_link_with_web_seeds() {
        let torrent = Torrent {
            announce: None,
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: Some(HashMap::from([(
                "url-list".to_owned(),
                BencodeElem::List(vec![
                    BencodeElem::String("https://example.org/path1".to_owned()),
                    BencodeElem::String("https://example.org/path2".to_owned()),
                ]),
            )])),
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.magnet_link().unwrap(),
            "magnet:?xt=urn:btih:074f42efaf8267f137f114f722d4e7d1dcbfbda5\
             &dn=sample&ws=https://example.org/path1&ws=https://example.org/path2"
                .to_owned()
        );
    }

    #[test]
    fn magnet_link_escape() {
        let torrent = Torrent {
            announce: Some("https://example.org/path?a=1&b=hello world".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: Some(HashMap::from([(
                "url-list".to_owned(),
                BencodeElem::String("https://example.org/path?a=1&b=hello world".to_owned()),
            )])),
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.magnet_link().unwrap(),
            "magnet:?xt=urn:btih:074f42efaf8267f137f114f722d4e7d1dcbfbda5\
             &dn=sample&tr=https://example.org/path?a=1%26b=hello+world\
             &ws=https://example.org/path?a=1%26b=hello+world"
                .to_owned()
        );
    }

    #[test]
    fn is_private_ok() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: Some(HashMap::from_iter(
                vec![("private".to_owned(), bencode_elem!(1))].into_iter(),
            )),
        };

        assert!(torrent.is_private());
    }

    #[test]
    fn is_private_no_extra_fields() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert!(!torrent.is_private());
    }

    #[test]
    fn is_private_no_key() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: Some(HashMap::from_iter(
                vec![("".to_owned(), bencode_elem!(1))].into_iter(),
            )),
        };

        assert!(!torrent.is_private());
    }

    #[test]
    fn is_private_incorrect_val_type() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: Some(HashMap::from_iter(
                vec![("private".to_owned(), bencode_elem!("1"))].into_iter(),
            )),
        };

        assert!(!torrent.is_private());
    }

    #[test]
    fn is_private_incorrect_val() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: Some(HashMap::from_iter(
                vec![("private".to_owned(), bencode_elem!(2))].into_iter(),
            )),
        };

        assert!(!torrent.is_private());
    }
}

#[cfg(test)]
mod file_display_tests {
    use super::*;
    use std::iter::FromIterator;

    #[test]
    fn file_display_ok() {
        let file = File {
            length: 42,
            path: PathBuf::from("dir1/file"),
            extra_fields: None,
        };

        assert_eq!(
            file.to_string(),
            "dir1/file\n\
             -size: 42 bytes\n\
             ========================================\n"
        );
    }

    #[test]
    fn file_display_with_extra_fields() {
        let file = File {
            length: 42,
            path: PathBuf::from("dir1/file"),
            extra_fields: Some(HashMap::from_iter(
                vec![
                    ("comment2".to_owned(), bencode_elem!("no comment")),
                    ("comment1".to_owned(), bencode_elem!("no comment")),
                ]
                .into_iter(),
            )),
        };

        assert_eq!(
            file.to_string(),
            "dir1/file\n\
             -size: 42 bytes\n\
             -comment1: \"no comment\"\n\
             -comment2: \"no comment\"\n\
             ========================================\n"
        );
    }
}

#[cfg(test)]
mod torrent_display_tests {
    use super::*;
    use std::iter::FromIterator;

    #[test]
    fn torrent_display_ok() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.to_string(),
            "sample.torrent\n\
             -announce: url\n\
             -size: 4 bytes\n\
             -piece length: 2 bytes\n\
             -pieces: [[0102], [0304]]\n"
        );
    }

    #[test]
    fn torrent_display_with_announce_list() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: Some(vec![
                vec!["url1".to_owned(), "url2".to_owned()],
                vec!["url3".to_owned(), "url4".to_owned()],
            ]),
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.to_string(),
            "sample.torrent\n\
             -announce: url\n\
             -announce-list: [[url1, url2], [url3, url4]]\n\
             -size: 4 bytes\n\
             -piece length: 2 bytes\n\
             -pieces: [[0102], [0304]]\n"
        );
    }

    #[test]
    fn torrent_display_with_extra_fields() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: Some(HashMap::from_iter(
                vec![
                    ("comment2".to_owned(), bencode_elem!("no comment")),
                    ("comment1".to_owned(), bencode_elem!("no comment")),
                ]
                .into_iter(),
            )),
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.to_string(),
            "sample.torrent\n\
             -announce: url\n\
             -size: 4 bytes\n\
             -piece length: 2 bytes\n\
             -comment1: \"no comment\"\n\
             -comment2: \"no comment\"\n\
             -pieces: [[0102], [0304]]\n"
        );
    }

    #[test]
    fn torrent_display_with_extra_info_fields() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: None,
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: Some(HashMap::from_iter(
                vec![
                    ("comment2".to_owned(), bencode_elem!("no comment")),
                    ("comment1".to_owned(), bencode_elem!("no comment")),
                ]
                .into_iter(),
            )),
        };

        assert_eq!(
            torrent.to_string(),
            "sample.torrent\n\
             -announce: url\n\
             -size: 4 bytes\n\
             -piece length: 2 bytes\n\
             -comment1: \"no comment\"\n\
             -comment2: \"no comment\"\n\
             -pieces: [[0102], [0304]]\n"
        );
    }

    #[test]
    fn torrent_display_with_multiple_files() {
        let torrent = Torrent {
            announce: Some("url".to_owned()),
            announce_list: None,
            length: 4,
            files: Some(vec![
                File {
                    length: 2,
                    path: PathBuf::from("dir1/dir2/file1"),
                    extra_fields: None,
                },
                File {
                    length: 2,
                    path: PathBuf::from("dir1/dir2/file2"),
                    extra_fields: None,
                },
            ]),
            name: "sample".to_owned(),
            piece_length: 2,
            pieces: vec![vec![1, 2], vec![3, 4]],
            extra_fields: None,
            extra_info_fields: None,
        };

        assert_eq!(
            torrent.to_string(),
            "sample.torrent\n\
             -announce: url\n\
             -size: 4 bytes\n\
             -piece length: 2 bytes\n\
             -files:\n\
             [1] dir1/dir2/file1\n\
             -size: 2 bytes\n\
             ========================================\n\
             \n\
             [2] dir1/dir2/file2\n\
             -size: 2 bytes\n\
             ========================================\n\
             \n\
             -pieces: [[0102], [0304]]\n"
        );
    }
}