fx-torrent 0.10.3

FX Torrent is a feature rich Bittorrent protocol implementation written in rust supporting Linux, MacOS and Windows.
Documentation
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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
use crate::{
    BitVec, File, FileAttributeFlags, FileIndex, FilePriority, Piece, PieceBlock, PieceIndex,
    PiecePriority,
};
use itertools::Itertools;
use log::error;
use std::collections::BTreeMap;
use std::ops::Range;
use std::sync::Arc;
use tokio::sync::RwLock;

/// The data pool of a torrent storing info about pieces and files.
/// It makes use of a separate loop task to handle operations on the data pool.
#[derive(Debug, Clone)]
pub struct DataPool {
    inner: Arc<RwLock<InnerDataPool>>,
}

impl DataPool {
    /// Create a new data pool for storing info about pieces and files.
    pub fn new() -> Self {
        Self::new_with_pieces(Vec::new())
    }

    /// Create a new data pool for the given pieces.
    fn new_with_pieces(pieces: Vec<Piece>) -> Self {
        Self {
            inner: Arc::new(RwLock::new(InnerDataPool::new(pieces))),
        }
    }

    /// Returns the number of pieces within the pool.
    pub async fn num_of_pieces(&self) -> usize {
        self.inner.read().await.num_of_pieces()
    }

    /// Returns the number of files within the pool.
    /// Files with the attribute [FileAttributeFlags::PaddingFile] are not counted.
    pub async fn num_of_files(&self) -> usize {
        self.inner.read().await.num_of_files()
    }

    /// Returns the number of pieces which have been completed.
    pub async fn num_completed_pieces(&self) -> usize {
        self.inner.read().await.num_completed_pieces()
    }

    /// Returns the piece for the given index.
    pub async fn piece(&self, piece: &PieceIndex) -> Option<Piece> {
        self.inner.read().await.pieces.get(piece).cloned()
    }

    /// Returns the file for the given index, if found.
    pub async fn file(&self, file: &FileIndex) -> Option<File> {
        self.inner.read().await.files.get(file).cloned()
    }

    /// Returns the file for the given name, if found.
    pub async fn file_by_name<S: AsRef<str>>(&self, name: S) -> Option<File> {
        self.inner
            .read()
            .await
            .files
            .values()
            .find(|file| file.filename() == name.as_ref())
            .cloned()
    }

    /// Returns all pieces present within the pool.
    pub async fn pieces(&self) -> Vec<Piece> {
        self.inner.read().await.pieces.values().cloned().collect()
    }

    /// Returns all files present within the pool.
    pub async fn files(&self) -> Vec<File> {
        self.inner.read().await.files.values().cloned().collect()
    }

    /// Set the pieces of the pool.
    /// This will replace all existing pieces within the pool.
    pub async fn set_pieces(&self, pieces: Vec<Piece>) {
        self.inner.write().await.set_pieces(pieces);
    }

    /// Set the files of the pool.
    /// This will replace all existing files within the pool.
    pub async fn set_files(&self, files: Vec<File>) {
        self.inner.write().await.set_files(files);
    }

    /// Returns the piece which contains the given torrent offset.
    pub async fn find_piece_at_offset(&self, offset: usize) -> Option<Piece> {
        self.inner.read().await.find_piece_at_offset(offset)
    }

    /// Returns the [PieceBlock] matching the given offset within the piece, if found.
    pub async fn find_piece_block(&self, piece: &PieceIndex, offset: usize) -> Option<PieceBlock> {
        self.inner
            .read()
            .await
            .pieces
            .iter()
            .find(|(idx, _)| *idx == piece)
            .and_then(|(_, piece)| {
                piece
                    .blocks
                    .iter()
                    .find(|part| part.begin == offset)
                    .cloned()
            })
    }

    /// Returns the piece priorities for the torrent.
    pub async fn piece_priorities(&self) -> BTreeMap<PieceIndex, PiecePriority> {
        self.inner.read().await.piece_priorities()
    }

    /// Set the priorities for the given pieces of the torrent.
    pub async fn set_piece_priorities(&self, priorities: &[(PieceIndex, PiecePriority)]) {
        self.inner.write().await.set_piece_priorities(priorities);
    }

    /// Returns `true` if the given piece is present within the pool, else `false`.
    pub async fn contains_piece(&self, piece: &PieceIndex) -> bool {
        self.inner.read().await.pieces.contains_key(piece)
    }

    /// Returns `true` if all wanted pieces have been downloaded and validated, else `false`.
    ///
    /// Every piece with anything but a [PiecePriority::None] has
    /// been downloaded and validated their data in this case.
    pub async fn is_completed(&self) -> bool {
        self.inner.read().await.is_completed()
    }

    /// Returns `true` if the given piece has been downloaded and validated, else `false`.
    pub async fn is_piece_completed(&self, piece: &PieceIndex) -> bool {
        self.inner.read().await.is_piece_completed(piece)
    }

    /// Set the completion state for the given piece slice.
    pub async fn set_completed(&self, pieces: &[PieceIndex], completed: bool) {
        self.inner
            .write()
            .await
            .set_pieces_completion_state(pieces, completed)
    }

    /// Returns `true` if the given torrent byte range is available (downloaded and validated), else `false`.
    pub async fn has_bytes(&self, bytes: Range<usize>) -> bool {
        self.inner.read().await.has_bytes(&bytes)
    }

    /// Returns the piece indexes in which the torrent is interested.
    /// These are the pieces that don't have [PiecePriority::None] as a priority.
    pub async fn interested_pieces(&self) -> Vec<PieceIndex> {
        self.inner.read().await.interested_pieces()
    }

    /// Returns the amount of bytes in which the torrent is interested.
    pub async fn interested_size(&self) -> usize {
        self.inner.read().await.interested_size()
    }

    /// Returns the piece indexes which have completed downloading.
    /// This might include pieces with [PiecePriority::None], if they've been downloaded in the past.
    pub async fn completed_pieces(&self) -> Vec<PieceIndex> {
        self.inner.read().await.completed_pieces()
    }

    /// Returns the amount of bytes which have completed downloading.
    /// This might include pieces with [PiecePriority::None], if they've been downloaded in the past.
    pub async fn completed_size(&self) -> usize {
        self.inner.read().await.completed_size()
    }

    /// Returns `true` if the given piece index is wanted by the torrent and not yet completed, else `false`.
    pub async fn is_piece_wanted(&self, piece_index: &PieceIndex) -> bool {
        self.inner.read().await.is_piece_wanted(piece_index)
    }

    /// Returns the pieces which are still wanted (need to be downloaded) by the torrent.
    /// The list is sorted based on the piece priority.
    pub async fn wanted_pieces(&self) -> Vec<Piece> {
        self.inner.read().await.wanted_pieces()
    }

    /// Modify the availability of the given piece by X peers.
    pub async fn update_availability(&self, piece: &PieceIndex, change: i32) {
        self.inner
            .write()
            .await
            .update_piece_availability(piece, change)
    }

    /// Returns the pieces bitfield, indicating which piece has completed.
    pub async fn bitfield(&self) -> BitVec {
        self.inner.read().await.completed_pieces.clone()
    }

    /// Returns the file index for the starting byte of the given piece.
    pub async fn file_index_for(&self, piece: &PieceIndex) -> Option<FileIndex> {
        self.inner.read().await.file_index_for(piece)
    }

    /// Returns `true` if the torrent is a partial seed.
    /// This means that the torrent has completed some files, but not all files are wanted.
    pub async fn is_partial_seed(&self) -> bool {
        self.inner.read().await.is_partial_seed()
    }
}

impl<S: AsRef<[Piece]>> From<S> for DataPool {
    fn from(value: S) -> Self {
        Self::new_with_pieces(value.as_ref().to_vec())
    }
}

#[derive(Debug)]
struct InnerDataPool {
    completed_pieces: BitVec,
    pieces: BTreeMap<PieceIndex, Piece>,
    files: BTreeMap<FileIndex, File>,
}

impl InnerDataPool {
    fn new(pieces: Vec<Piece>) -> Self {
        Self {
            completed_pieces: BitVec::repeat(false, pieces.len()),
            pieces: pieces
                .into_iter()
                .map(|piece| (piece.index, piece))
                .collect(),
            files: Default::default(),
        }
    }

    fn num_of_pieces(&self) -> usize {
        self.pieces.len()
    }

    fn num_of_files(&self) -> usize {
        self.files
            .iter()
            .filter(|(_, file)| !file.attributes().contains(FileAttributeFlags::PaddingFile))
            .count()
    }

    fn num_completed_pieces(&self) -> usize {
        self.completed_pieces.count_ones()
    }

    fn set_pieces(&mut self, pieces: Vec<Piece>) {
        let pieces_len = pieces.len();
        let pieces = pieces
            .into_iter()
            .map(|piece| (piece.index, piece))
            .collect();

        self.pieces = pieces;
        self.completed_pieces = BitVec::repeat(false, pieces_len);
    }

    fn set_files(&mut self, files: Vec<File>) {
        self.files = files.into_iter().map(|file| (file.index, file)).collect();
    }

    fn find_piece_at_offset(&self, offset: usize) -> Option<Piece> {
        self.pieces
            .iter()
            .find(|(_, piece)| {
                let piece_start = piece.offset;
                let piece_end = piece_start + piece.len();
                offset >= piece_start && offset < piece_end
            })
            .map(|(_, piece)| piece.clone())
    }

    fn piece_priorities(&self) -> BTreeMap<PieceIndex, PiecePriority> {
        self.pieces
            .iter()
            .map(|(index, piece)| (*index, piece.priority.clone()))
            .collect()
    }

    fn set_piece_priorities(&mut self, priorities: &[(PieceIndex, PiecePriority)]) {
        for (index, priority) in priorities {
            if let Some(piece) = self.pieces.get_mut(index) {
                piece.priority = *priority;
            }
        }

        // recalculate the file priorities
        for file in self.files.values_mut() {
            // early skip if the file was not touched by this update
            if !file
                .pieces
                .clone()
                .any(|piece| priorities.iter().any(|(k, _)| k == &piece))
            {
                continue;
            }

            let len = file.pieces.len();
            let highest_priority = match len {
                0 => {
                    // this should never happen unless the torrent is malformed
                    error!("Torrent file \"{}\" is malformed", file.filename());
                    FilePriority::None
                }
                1 => self
                    .pieces
                    .get(&file.pieces.start)
                    .map(|piece| piece.priority)
                    .unwrap_or(FilePriority::None),
                _ => {
                    let exclusive_range = file.pieces.start + 1..file.pieces.end - 1;
                    exclusive_range
                        .into_iter()
                        .map(|piece| {
                            self.pieces
                                .get(&piece)
                                .map(|piece| piece.priority)
                                .unwrap_or(FilePriority::None)
                        })
                        .max()
                        .unwrap_or(FilePriority::None)
                }
            };

            file.priority = highest_priority;
        }
    }

    fn is_piece_completed(&self, piece: &PieceIndex) -> bool {
        self.completed_pieces
            .get(*piece)
            .map(|bit| *bit)
            .unwrap_or_default()
    }

    fn is_completed(&self) -> bool {
        self.pieces
            .iter()
            .filter(|(_, piece)| piece.priority != PiecePriority::None)
            .map(|(index, _)| *index)
            .into_iter()
            .all(|piece| {
                self.completed_pieces
                    .get(piece)
                    .map(|bit| *bit)
                    .unwrap_or(false)
            })
    }

    fn set_pieces_completion_state(&mut self, pieces: &[PieceIndex], completed: bool) {
        for piece in pieces {
            self.completed_pieces.set(*piece, completed);
        }
    }

    fn interested_pieces(&self) -> Vec<PieceIndex> {
        self.pieces
            .iter()
            .filter(|(_, piece)| piece.priority != PiecePriority::None)
            .map(|(index, _)| *index)
            .collect()
    }

    fn interested_size(&self) -> usize {
        self.pieces
            .iter()
            .filter(|(_, piece)| piece.priority != PiecePriority::None)
            .map(|(_, piece)| piece.len())
            .sum()
    }

    fn completed_pieces(&self) -> Vec<PieceIndex> {
        self.completed_pieces
            .iter()
            .enumerate()
            .filter(|(_, completed)| **completed)
            .map(|(index, _)| index)
            .collect()
    }

    fn completed_size(&self) -> usize {
        self.completed_pieces
            .iter()
            .enumerate()
            .filter(|(_, completed)| **completed)
            .filter_map(|(index, _)| self.pieces.get(&index))
            .map(|piece| piece.len())
            .sum()
    }

    fn is_piece_wanted(&self, piece_index: &PieceIndex) -> bool {
        self.pieces
            .get(piece_index)
            .filter(|piece| Self::is_wanted_piece(&self.completed_pieces, piece))
            .is_some()
    }

    fn wanted_pieces(&self) -> Vec<Piece> {
        self.pieces
            .iter()
            .filter(|(_, piece)| piece.priority != PiecePriority::None)
            .map(|(_, piece)| piece)
            .into_iter()
            .filter(|piece| Self::is_wanted_piece(&self.completed_pieces, piece))
            .sorted_by(|a, b| b.priority.cmp(&a.priority))
            .cloned()
            .collect()
    }

    fn update_piece_availability(&mut self, piece: &PieceIndex, change: i32) {
        let Some(piece) = self.pieces.get_mut(piece) else {
            return;
        };

        let delta = change.unsigned_abs();
        piece.availability = if change >= 0 {
            piece.availability.saturating_add(delta)
        } else {
            piece.availability.saturating_sub(delta)
        };
    }

    fn file_index_for(&self, piece: &PieceIndex) -> Option<FileIndex> {
        let piece = self.pieces.get(piece)?;

        self.files
            .iter()
            .find(|(_, file)| {
                let file_start = file.torrent_offset;
                let file_end = file_start + file.len();
                piece.offset >= file_start && piece.offset < file_end
            })
            .map(|(index, _)| *index)
    }

    fn has_bytes(&self, bytes: &Range<usize>) -> bool {
        let piece_len = self.pieces.get(&0).map(|piece| piece.length).unwrap_or(1);
        let start_piece = bytes.start / piece_len;
        let end_piece = (bytes.end - 1) / piece_len;

        (start_piece..=end_piece)
            .into_iter()
            .all(|piece| self.is_piece_completed(&piece))
    }

    /// Check if the piece is wanted by the torrent.
    /// In such a case, the piece priority should not be [PiecePriority::None]
    /// and the piece should not have been completed yet.
    fn is_wanted_piece(bitfield: &BitVec, piece: &Piece) -> bool {
        piece.priority != PiecePriority::None
            && bitfield
                .get(piece.index)
                .map(|bit| *bit)
                .unwrap_or_default()
                == false
    }

    fn is_partial_seed(&self) -> bool {
        // early exit if the torrent is a single-file torrent
        if self.files.len() <= 1 {
            return false;
        }

        let total_pieces = self.pieces.len();
        let wanted_pieces = self.wanted_pieces().len();
        let completed_pieces = self.completed_pieces.count_ones();

        total_pieces != completed_pieces && wanted_pieces == 0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::TorrentFileInfo;
    use std::ops::Range;

    mod is_piece_wanted {
        use super::*;

        #[tokio::test]
        async fn test_piece_completed() {
            let piece = 0;
            let pool = DataPool::from(vec![
                Piece {
                    hash: Default::default(),
                    index: piece,
                    offset: 0,
                    length: 1024,
                    priority: PiecePriority::Normal,
                    blocks: vec![],
                    availability: 0,
                },
                Piece {
                    hash: Default::default(),
                    index: 1,
                    offset: 1024,
                    length: 1024,
                    priority: PiecePriority::None,
                    blocks: vec![],
                    availability: 0,
                },
            ]);

            let result = pool.is_piece_wanted(&piece).await;
            assert_eq!(true, result, "expected the piece to have been wanted");

            // set the piece as completed
            pool.set_completed(&[piece], true).await;

            let result = pool.is_piece_wanted(&piece).await;
            assert_eq!(false, result, "expected the piece to be no longer wanted");
        }

        #[tokio::test]
        async fn test_piece_priority_none() {
            let piece = 1;
            let pool = DataPool::from(vec![
                Piece {
                    hash: Default::default(),
                    index: 0,
                    offset: 0,
                    length: 1024,
                    priority: PiecePriority::Normal,
                    blocks: vec![],
                    availability: 0,
                },
                Piece {
                    hash: Default::default(),
                    index: piece,
                    offset: 1024,
                    length: 1024,
                    priority: PiecePriority::None,
                    blocks: vec![],
                    availability: 0,
                },
            ]);

            let result = pool.is_piece_wanted(&piece).await;
            assert_eq!(false, result, "expected the piece to not have been wanted");

            // set the piece as completed
            pool.set_completed(&[piece], true).await;

            let result = pool.is_piece_wanted(&piece).await;
            assert_eq!(false, result, "expected the piece to not have been wanted");
        }
    }

    mod is_piece_completed {
        use super::*;

        #[tokio::test]
        async fn test_piece_set_completed() {
            let pool = DataPool::from(vec![
                Piece {
                    hash: Default::default(),
                    index: 0,
                    offset: 0,
                    length: 1024,
                    priority: PiecePriority::Normal,
                    blocks: vec![],
                    availability: 0,
                },
                Piece {
                    hash: Default::default(),
                    index: 1,
                    offset: 1024,
                    length: 1024,
                    priority: PiecePriority::Normal,
                    blocks: vec![],
                    availability: 0,
                },
            ]);

            pool.set_completed(&[0], true).await;
            let result = pool.is_completed().await;
            assert_eq!(
                false, result,
                "expected the torrent to not have been completed yet"
            );

            pool.set_completed(&[1], true).await;
            let result = pool.is_completed().await;
            assert_eq!(true, result, "expected the torrent to have been completed");
        }
    }

    mod num_completed_pieces {
        use super::*;

        #[tokio::test]
        async fn test_num_completed_pieces() {
            let pool = DataPool::from(
                (0..5)
                    .into_iter()
                    .map(|index| create_piece(index, 256))
                    .collect::<Vec<_>>(),
            );

            // check that the initial number of completed pieces is 0
            let result = pool.num_completed_pieces().await;
            assert_eq!(
                0, result,
                "expected the initial number of completed pieces to be 0"
            );

            // complete the first 2 pieces
            for index in 0..2 {
                pool.set_completed(&[index], true).await;
            }
            let result = pool.num_completed_pieces().await;
            assert_eq!(2, result, "expected the number of completed pieces to be 2");
        }
    }

    mod pieces {
        use super::*;

        #[tokio::test]
        async fn test_pieces() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 1024),
            ];
            let pool = DataPool::new();

            // set the pieces of the pool
            pool.set_pieces(pieces.clone()).await;

            // retrieve the pieces of the pool
            let result = pool.pieces().await;
            assert_eq!(pieces, result, "expected the pieces to be retrieved");
        }
    }

    mod find_piece_at_offset {
        use super::*;

        #[tokio::test]
        async fn test_find_piece_at_offset() {
            let piece_len = 512;
            let pool = DataPool::from(
                (0..10)
                    .into_iter()
                    .map(|index| create_piece(index, piece_len))
                    .collect::<Vec<_>>(),
            );

            // check the offset of the last byte in the first piece
            let result = pool
                .find_piece_at_offset(511)
                .await
                .expect("expected a piece to be found");
            assert_eq!(
                0, result.index,
                "expected the piece at offset 511 to be piece 0"
            );

            // get the second piece on the first byte
            let result = pool
                .find_piece_at_offset(512)
                .await
                .expect("expected a piece to be found");
            assert_eq!(
                1, result.index,
                "expected the piece at offset 512 to be piece 1"
            );
        }

        #[tokio::test]
        async fn test_out_of_bounds() {
            let piece_len = 512;
            let pool = DataPool::from(
                (0..10)
                    .into_iter()
                    .map(|index| create_piece(index, piece_len))
                    .collect::<Vec<_>>(),
            );

            // retrieve the last piece through the last byte
            let result = pool
                .find_piece_at_offset(5_119)
                .await
                .expect("expected a piece to be found");
            assert_eq!(
                9, result.index,
                "expected the last piece to have been returned"
            );

            // retrieve an out-of-bounds offset
            let result = pool.find_piece_at_offset(5_120).await;
            assert!(
                result.is_none(),
                "expected no piece to be found, but got {:?}",
                result
            );
        }
    }

    mod contains_piece {
        use super::*;

        #[tokio::test]
        async fn test_contains_piece() {
            let pool = DataPool::from(
                (0..3)
                    .into_iter()
                    .map(|index| create_piece(index, 1024))
                    .collect::<Vec<_>>(),
            );

            // check a valid piece index
            let result = pool.contains_piece(&2).await;
            assert_eq!(true, result, "expected the piece to be present in the pool");

            // check an invalid piece index
            let result = pool.contains_piece(&3).await;
            assert_eq!(
                false, result,
                "expected the piece to not be present in the pool"
            );
        }
    }

    mod has_bytes {
        use super::*;

        #[tokio::test]
        async fn test_has_bytes() {
            init_logger!();
            let pool = DataPool::from(vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
            ]);

            // set pieces to completed state
            pool.set_completed(&[0, 1], true).await;

            // retrieve bytes available
            let result = pool.has_bytes(0..2048).await;
            assert_eq!(true, result, "expected the bytes to be available");

            // retrieve none of the bytes available
            let result = pool.has_bytes(2049..3094).await;
            assert_eq!(false, result, "expected the bytes to not be available");

            // retrieve some bytes available
            let result = pool.has_bytes(2040..2060).await;
            assert_eq!(false, result, "expected the bytes to not be available");
        }
    }

    mod interested {
        use super::*;

        #[tokio::test]
        async fn test_interested_pieces() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 512),
            ];
            let pool = DataPool::new();

            // set the pieces with priorities in the pool
            pool.set_pieces(pieces).await;
            pool.set_piece_priorities(&[(0, PiecePriority::None), (1, PiecePriority::None)])
                .await;

            let result = pool.interested_pieces().await;
            assert_eq!(
                vec![2, 3],
                result,
                "expected the interested pieces to match"
            );
        }

        #[tokio::test]
        async fn test_interested_size() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 512),
            ];
            let pool = DataPool::new();

            // set the pieces with priorities in the pool
            pool.set_pieces(pieces).await;
            pool.set_piece_priorities(&[(0, PiecePriority::None), (1, PiecePriority::None)])
                .await;

            let result = pool.interested_size().await;
            assert_eq!(1_536, result, "expected the interested size to match");
        }
    }

    mod completed {
        use super::*;

        #[tokio::test]
        async fn test_completed_pieces() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 512),
            ];
            let pool = DataPool::new();

            // set the pieces with priorities in the pool
            pool.set_pieces(pieces).await;
            pool.set_completed(&[0, 1], true).await;

            let result = pool.completed_pieces().await;
            assert_eq!(vec![0, 1], result, "expected the completed pieces to match");
        }

        #[tokio::test]
        async fn test_completed_size() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 512),
            ];
            let pool = DataPool::new();

            // set the pieces with priorities in the pool
            pool.set_pieces(pieces).await;
            pool.set_completed(&[1, 3], true).await;

            let result = pool.completed_size().await;
            assert_eq!(1_536, result, "expected the completed size to match");
        }
    }

    mod is_partial_seed {
        use super::*;

        #[tokio::test]
        async fn test_single_file_torrent() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 1024),
            ];
            let pool = DataPool::new();

            // set the pieces and file
            pool.set_pieces(pieces).await;
            pool.set_files(vec![create_file(0, 0, 4096, 0..4)]).await;

            // complete all pieces of the file
            pool.set_completed(&[0, 1, 2, 3], true).await;

            let result = pool.is_partial_seed().await;
            assert_eq!(
                false, result,
                "expected the torrent to not be a partial seed"
            );
        }
        #[tokio::test]
        async fn test_multi_file_torrent() {
            init_logger!();
            let pieces = vec![
                create_piece(0, 1024),
                create_piece(1, 1024),
                create_piece(2, 1024),
                create_piece(3, 1024),
            ];
            let pool = DataPool::new();

            // set the pieces and file
            pool.set_pieces(pieces).await;
            pool.set_files(vec![
                create_file(0, 0, 2048, 0..2),
                create_file(1, 2048, 2048, 2..4),
            ])
            .await;

            // check if partial seed is false, if none of the files have been completed
            let result = pool.is_partial_seed().await;
            assert_eq!(
                false, result,
                "expected the torrent to not be a partial seed"
            );

            // complete all pieces of file 1
            pool.set_completed(&[0, 1], true).await;

            // check if partial seed is false, if none of the files have been completed
            let result = pool.is_partial_seed().await;
            assert_eq!(
                false, result,
                "expected the torrent to not be a partial seed"
            );

            // set file 2 as not wanted
            let priorities = (2..4)
                .map(|piece| (piece, PiecePriority::None))
                .collect_vec();
            pool.set_piece_priorities(&priorities).await;

            // check if partial seed is true, if none of the files have been completed
            let result = pool.is_partial_seed().await;
            assert_eq!(true, result, "expected the torrent to be a partial seed");
        }
    }

    fn create_piece(index: PieceIndex, length: usize) -> Piece {
        Piece {
            hash: Default::default(),
            index,
            offset: index * length,
            length,
            priority: PiecePriority::Normal,
            blocks: vec![],
            availability: 1,
        }
    }

    fn create_file(
        index: FileIndex,
        offset: usize,
        length: usize,
        pieces: Range<PieceIndex>,
    ) -> File {
        File {
            index,
            torrent_path: Default::default(),
            torrent_offset: offset,
            info: TorrentFileInfo {
                length: length as u64,
                path: None,
                path_utf8: None,
                md5sum: None,
                attr: None,
                symlink_path: None,
                sha1: None,
            },
            priority: FilePriority::Normal,
            pieces,
        }
    }
}