Ejemplo n.º 1
0
        public void Info_MissingPieceLengthField_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info.Remove(TorrentInfoFields.PieceLength);

            // Act
            var    parser = new TorrentParser(BencodeParser);
            Action action = () => parser.Parse((BencodeStream)null);

            // Assert
            action.ShouldThrow <InvalidTorrentException>()
            .Where(ex => ex.InvalidField == TorrentInfoFields.PieceLength);
        }
Ejemplo n.º 2
0
        public void MultiFileInfo_MissingFilesField_Strict_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidMultiFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info.Remove(TorrentInfoFields.Files);

            // Act
            var    parser = new TorrentParser(BencodeParser, TorrentParserMode.Strict);
            Action action = () => parser.Parse((BencodeReader)null);

            // Assert
            action.Should().Throw <InvalidTorrentException>()
            .Where(ex => ex.InvalidField == TorrentInfoFields.Files);
        }
Ejemplo n.º 3
0
        public void AnnounceList_Single_IsParsed(IList <string> announceList)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BList(announceList)
            };

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeReader)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(announceList.Count);
            torrent.Trackers[0].Should().BeEquivalentTo(announceList);
        }
Ejemplo n.º 4
0
        public void Info_ContainingBothLengthAndFilesField_Strict_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info[TorrentInfoFields.Length] = (BNumber)1;
            info[TorrentInfoFields.Files]  = new BList();

            // Act
            var    parser = new TorrentParser(BencodeParser, TorrentParserMode.Strict);
            Action action = () => parser.Parse((BencodeReader)null);

            // Assert
            action.Should().Throw <InvalidTorrentException>()
            .WithMessage($"*{TorrentInfoFields.Length}*")
            .WithMessage($"*{TorrentInfoFields.Files}*");
        }
Ejemplo n.º 5
0
        public void MultiFileInfo_IsParsed(string directoryName, long length1, IList <string> paths1, IList <string> paths1Utf8, string md5Sum1, long length2, IList <string> paths2, IList <string> paths2Utf8, string md5Sum2)
        {
            // Arrange
            ParsedData = ValidMultiFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info[TorrentInfoFields.Name]  = (BString)directoryName;
            info[TorrentInfoFields.Files] = new BList <BDictionary>
            {
                new BDictionary
                {
                    [TorrentFilesFields.Length]   = (BNumber)length1,
                    [TorrentFilesFields.Path]     = new BList(paths1),
                    [TorrentFilesFields.PathUtf8] = new BList(paths1Utf8),
                    [TorrentFilesFields.Md5Sum]   = (BString)md5Sum1
                },
                new BDictionary
                {
                    [TorrentFilesFields.Length]   = (BNumber)length2,
                    [TorrentFilesFields.Path]     = new BList(paths2),
                    [TorrentFilesFields.PathUtf8] = new BList(paths2Utf8),
                    [TorrentFilesFields.Md5Sum]   = (BString)md5Sum2
                }
            };

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeReader)null);

            // Assert
            torrent.File.Should().BeNull();
            torrent.FileMode.Should().Be(TorrentFileMode.Multi);
            torrent.TotalSize.Should().Be(length1 + length2);
            torrent.Files.DirectoryName.Should().Be(directoryName);
            torrent.Files.Should().HaveCount(2);
            torrent.Files[0].FileSize.Should().Be(length1);
            torrent.Files[0].Path.Should().BeEquivalentTo(paths1);
            torrent.Files[0].PathUtf8.Should().BeEquivalentTo(paths1Utf8);
            torrent.Files[0].Md5Sum.Should().Be(md5Sum1);
            torrent.Files[1].FileSize.Should().Be(length2);
            torrent.Files[1].Path.Should().BeEquivalentTo(paths2);
            torrent.Files[1].PathUtf8.Should().BeEquivalentTo(paths2Utf8);
            torrent.Files[1].Md5Sum.Should().Be(md5Sum2);
        }
Ejemplo n.º 6
0
        public void SingleFileInfo_NameUtf8_IsParsedAsUtf8EncodingIndependentlyOfActualEncoding()
        {
            var encoding = "ISO-8859-1";
            var fileName = "øæå"; // Use characters with different byte values for UTF8 and ISO-8859-1

            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Encoding] = (BString)encoding;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info[TorrentInfoFields.Name]     = new BString(fileName, Encoding.GetEncoding(encoding));
            info[TorrentInfoFields.NameUtf8] = new BString(fileName, Encoding.UTF8);

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeReader)null);

            // Assert
            torrent.File.FileName.Should().Be(fileName);
            torrent.File.FileNameUtf8.Should().Be(fileName);
        }
Ejemplo n.º 7
0
        public void AnnounceAndAnnounceList_DoesNotContainDuplicatesInPrimaryList(string announceUrl1, string announceUrl2)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Announce]     = (BString)announceUrl1;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BList {
                    announceUrl1, announceUrl2
                }
            };

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeReader)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(2);
            torrent.Trackers[0].Should().ContainInOrder(announceUrl1, announceUrl2);
        }
Ejemplo n.º 8
0
        public void AnnounceList_Multiple_IsParsed(IList <string> announceList1, IList <string> announceList2)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BList(announceList1),
                new BList(announceList2)
            };

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Trackers.Should().HaveCount(2);
            torrent.Trackers[0].Should().HaveCount(announceList1.Count);
            torrent.Trackers[0].ShouldAllBeEquivalentTo(announceList1);
            torrent.Trackers[1].Should().HaveCount(announceList2.Count);
            torrent.Trackers[1].ShouldAllBeEquivalentTo(announceList2);
        }
Ejemplo n.º 9
0
        public void AnnounceList_Multiple_StringsInsteadOfLists_IsParsed(string announce1, string announce2, string announce3)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BString(announce1),
                new BString(announce2),
                new BString(announce3),
            };

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeReader)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(3);
            torrent.Trackers[0][0].Should().Be(announce1);
            torrent.Trackers[0][1].Should().Be(announce2);
            torrent.Trackers[0][2].Should().Be(announce3);
        }
Ejemplo n.º 10
0
        public void MultiFile_Files_MissingPathField_Strict_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidMultiFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info[TorrentInfoFields.Files] = new BList <BDictionary>
            {
                new BDictionary
                {
                    [TorrentFilesFields.Length] = (BNumber)1
                }
            };

            // Act
            var    parser = new TorrentParser(BencodeParser, TorrentParserMode.Strict);
            Action action = () => parser.Parse((BencodeReader)null);

            // Assert
            action.Should().Throw <InvalidTorrentException>()
            .Where(ex => ex.InvalidField == TorrentFilesFields.Path);
        }
Ejemplo n.º 11
0
        public void SingleFileInfo_IsParsed(long length, string fileName, string md5Sum)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info[TorrentInfoFields.Length] = (BNumber)length;
            info[TorrentInfoFields.Name]   = (BString)fileName;
            info[TorrentInfoFields.Md5Sum] = (BString)md5Sum;

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Files.Should().BeNull();
            torrent.FileMode.Should().Be(TorrentFileMode.Single);
            torrent.TotalSize.Should().Be(length);
            torrent.File.Should().NotBeNull();
            torrent.File.FileSize.Should().Be(length);
            torrent.File.FileName.Should().Be(fileName);
            torrent.File.Md5Sum.Should().Be(md5Sum);
        }
Ejemplo n.º 12
0
        public void Info_PieceLength_IsParsed(long pieceSize)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.PieceLength] = (BNumber) pieceSize;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.PieceSize.Should().Be(pieceSize);
        }
Ejemplo n.º 13
0
        public void Info_ContainingBothLengthAndFilesField_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.Length] = (BNumber) 1;
            info[TorrentInfoFields.Files] = new BList();

            // Act
            var parser = new TorrentParser(BencodeParser);
            Action action = () => parser.Parse((BencodeStream)null);

            // Assert
            action.ShouldThrow<InvalidTorrentException>()
                .WithMessage($"*{TorrentInfoFields.Length}*")
                .WithMessage($"*{TorrentInfoFields.Files}*");
        }
Ejemplo n.º 14
0
        public void ExtraFields_IsParsed(string extraKey, string extraValue, string extraInfoKey, string extraInfoValue)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[extraKey] = (BString) extraValue;
            ParsedData.Get<BDictionary>(TorrentFields.Info)[extraInfoKey] = (BString) extraInfoValue;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.ExtraFields.Should().Contain(extraKey, (BString) extraValue);
            torrent.ExtraFields.Get<BDictionary>(TorrentFields.Info).Should().Contain(extraInfoKey, (BString) extraInfoValue);
        }
Ejemplo n.º 15
0
        public void Encoding_UTF8_CanBeParsed(string encoding)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Encoding] = (BString) encoding;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream) null);

            // Assert
            torrent.Encoding.Should().Be(Encoding.UTF8);
        }
Ejemplo n.º 16
0
        public void CreationDate_IsParsed()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.CreationDate] = (BNumber) 1451606400;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream) null);

            // Assert
            torrent.CreationDate.Should().Be(new DateTime(2016, 1, 1));
        }
Ejemplo n.º 17
0
        public void CreatedBy_IsParsed(string createdBy)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.CreatedBy] = (BString) createdBy;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream) null);

            // Assert
            torrent.CreatedBy.Should().Be(createdBy);
        }
Ejemplo n.º 18
0
        public void SingleFileInfo_IsParsed(long length, string fileName, string md5Sum)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.Length] = (BNumber) length;
            info[TorrentInfoFields.Name] = (BString) fileName;
            info[TorrentInfoFields.Md5Sum] = (BString) md5Sum;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Files.Should().BeNull();
            torrent.FileMode.Should().Be(TorrentFileMode.Single);
            torrent.TotalSize.Should().Be(length);
            torrent.File.Should().NotBeNull();
            torrent.File.FileSize.Should().Be(length);
            torrent.File.FileName.Should().Be(fileName);
            torrent.File.Md5Sum.Should().Be(md5Sum);
        }
Ejemplo n.º 19
0
        public void MultiFile_Files_MissingPathField_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidMultiFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.Files] = new BList<BDictionary>
            {
                new BDictionary
                {
                    [TorrentFilesFields.Length] = (BNumber) 1
                }
            };

            // Act
            var parser = new TorrentParser(BencodeParser);
            Action action = () => parser.Parse((BencodeStream)null);

            // Assert
            action.ShouldThrow<InvalidTorrentException>()
                .Where(ex => ex.InvalidField == TorrentFilesFields.Path);
        }
Ejemplo n.º 20
0
        public void MultiFileInfo_IsParsed(string directoryName, long length1, IList<string> paths1, string md5Sum1, long length2, IList<string> paths2, string md5Sum2)
        {
            // Arrange
            ParsedData = ValidMultiFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.Name] = (BString) directoryName;
            info[TorrentInfoFields.Files] = new BList<BDictionary>
            {
                new BDictionary
                {
                    [TorrentFilesFields.Length] = (BNumber) length1,
                    [TorrentFilesFields.Path] = new BList(paths1),
                    [TorrentFilesFields.Md5Sum] = (BString) md5Sum1
                },
                new BDictionary
                {
                    [TorrentFilesFields.Length] = (BNumber) length2,
                    [TorrentFilesFields.Path] = new BList(paths2),
                    [TorrentFilesFields.Md5Sum] = (BString) md5Sum2
                }
            };

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.File.Should().BeNull();
            torrent.FileMode.Should().Be(TorrentFileMode.Multi);
            torrent.TotalSize.Should().Be(length1 + length2);
            torrent.Files.DirectoryName.Should().Be(directoryName);
            torrent.Files.Should().HaveCount(2);
            torrent.Files[0].FileSize.Should().Be(length1);
            torrent.Files[0].Path.ShouldAllBeEquivalentTo(paths1);
            torrent.Files[0].Md5Sum.Should().Be(md5Sum1);
            torrent.Files[1].FileSize.Should().Be(length2);
            torrent.Files[1].Path.ShouldAllBeEquivalentTo(paths2);
            torrent.Files[1].Md5Sum.Should().Be(md5Sum2);
        }
Ejemplo n.º 21
0
        public void Info_Pieces_IsParsed(byte[] pieces)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.Pieces] = new BString(pieces);

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Pieces.Should().Equal(pieces);
        }
Ejemplo n.º 22
0
        public void Info_Private_ShouldBeTrueOnlyIfValueIsOne(int value, bool expectedResult)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info[TorrentInfoFields.Private] = (BNumber) value;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.IsPrivate.Should().Be(expectedResult);
        }
Ejemplo n.º 23
0
        public void AnnounceAndAnnounceList_IsParsed(string announceUrl, IList<string> announceList1, IList<string> announceList2)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Announce] = (BString) announceUrl;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BList(announceList1),
                new BList(announceList2)
            };

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            var primary = new List<string> {announceUrl};
            primary.AddRange(announceList1);
            torrent.Trackers.Should().HaveCount(2);
            torrent.Trackers[0].Should().HaveCount(primary.Count);
            torrent.Trackers[0].ShouldAllBeEquivalentTo(primary);
            torrent.Trackers[1].Should().HaveCount(announceList2.Count);
            torrent.Trackers[1].ShouldAllBeEquivalentTo(announceList2);
        }
Ejemplo n.º 24
0
        public void MultiFileInfo_MissingFilesField_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidMultiFileTorrentData;
            var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
            info.Remove(TorrentInfoFields.Files);

            // Act
            var parser = new TorrentParser(BencodeParser);
            Action action = () => parser.Parse((BencodeStream)null);

            // Assert
            action.ShouldThrow<InvalidTorrentException>()
                .Where(ex => ex.InvalidField == TorrentInfoFields.Files);
        }
Ejemplo n.º 25
0
        public void AnnounceList_Single_IsParsed(IList<string> announceList)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BList(announceList)
            };

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(announceList.Count);
            torrent.Trackers[0].ShouldAllBeEquivalentTo(announceList);
        }
Ejemplo n.º 26
0
        public void Root_MissingInfoField_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData.Remove(TorrentFields.Info);

            // Act
            var parser = new TorrentParser(BencodeParser);
            Action action = () => parser.Parse((BencodeStream)null);

            // Assert
            action.ShouldThrow<InvalidTorrentException>()
                .Where(ex => ex.InvalidField == TorrentFields.Info);
        }
Ejemplo n.º 27
0
        public void Announce_IsParsed(string announceUrl)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Announce] = (BString) announceUrl;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(1);
            torrent.Trackers[0][0].Should().Be(announceUrl);
        }
Ejemplo n.º 28
0
        public void AnnounceAndAnnounceList_DoesNotContainDuplicatesInPrimaryList(string announceUrl1, string announceUrl2)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Announce] = (BString) announceUrl1;
            ParsedData[TorrentFields.AnnounceList] = new BList
            {
                new BList { announceUrl1, announceUrl2}
            };

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(2);
            torrent.Trackers[0].Should().ContainInOrder(announceUrl1, announceUrl2);
        }
Ejemplo n.º 29
0
        public void Comment_IsParsed(string comment)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Comment] = (BString) comment;

            // Act
            var parser = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeStream) null);

            // Assert
            torrent.Comment.Should().Be(comment);
        }