Пример #1
0
        public void SingleFileInfo_IsParsed(long length, string fileName, string fileNameUtf8, string md5Sum)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

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

            // Act
            var parser  = new TorrentParser(BencodeParser);
            var torrent = parser.Parse((BencodeReader)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.FileNameUtf8.Should().Be(fileNameUtf8);
            torrent.File.Md5Sum.Should().Be(md5Sum);
        }
Пример #2
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((BencodeReader)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].Should().BeEquivalentTo(primary);
            torrent.Trackers[1].Should().HaveCount(announceList2.Count);
            torrent.Trackers[1].Should().BeEquivalentTo(announceList2);
        }
Пример #3
0
        private void UpdateTorrentList()
        {
            var config = Plugin.Instance.Configuration;

            if (config.userName is null)
            {
                return;
            }
            var url = $"http://{config.ipAddress}:{config.port}{gui}{token}{Token()}{list}";

            url += CacheId == null ? string.Empty : $"{cache}{CacheId}";

            var client   = new TorrentClient();
            var response = client.Get(url, "application/json");

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return;
            }

            using (var receiveStream = response.GetResponseStream())
            {
                if (receiveStream == null)
                {
                    return;
                }

                using (var sr = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet ?? throw new InvalidOperationException())))
                {
                    var data = sr.ReadToEnd();

                    var results = JsonSerializer.DeserializeFromString <UTorrentResponse>(data);

                    CacheId = results.torrentc;

                    //List<Torrent> torrents count would be empty on our first request
                    //We fill our "torrents" list with the entire torrent data from the API.

                    //"torrentp" results are only items that have changed since the last request using the cacheId from the prior request

                    //This means we have to replace the data stored in our "List<Torrent> torrents" which matches the new data returned in torrentp list from the api.

                    var torrentListChanges = TorrentParser.ParseTorrentListInfo(Torrents.Count <= 0 ? results.torrents : results.torrentp);

                    if (Torrents.Count <= 0)
                    {
                        Torrents = torrentListChanges; // add torrents to the master list.
                    }
                    else
                    {
                        //Remove any torrent data that has changed from the master list by comparing torrent Hash's
                        Torrents = Torrents.Where(t1 => torrentListChanges.Any(t2 => t1.Hash != t2.Hash)).ToList();
                        Torrents.AddRange(torrentListChanges); //Add the new data to the master list
                    }
                }
            }
        }
Пример #4
0
        // Use BencodeNET library to parse torrent
        static void ParseTorrent(byte[] torrentBytes)
        {
            var ms            = new MemoryStream(torrentBytes);
            var torrentParser = new TorrentParser(TorrentParserMode.Strict);
            var torrent       = torrentParser.Parse(ms);

            Console.WriteLine($"Here are '{torrent.DisplayName}' torrent files:");
            foreach (var file in torrent.Files)
            {
                Console.WriteLine(file.FullPath);
            }
        }
Пример #5
0
            private Torrent ReadAndParseTorrent()
            {
                var bencodeParser = new BencodeParser();
                var torrentParser = new TorrentParser(bencodeParser,
                                                      Strict ? TorrentParserMode.Strict : TorrentParserMode.Tolerant);

                using (var stream = System.IO.File.OpenRead(Path))
                {
                    var torrent = torrentParser.Parse(stream);
                    return(torrent);
                }
            }
Пример #6
0
        public void Encoding_InvalidValidAsNull(string encoding)
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.Encoding] = (BString)encoding;

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

            // Assert
            torrent.Encoding.Should().Be(null);
        }
Пример #7
0
        public void CreationDate_InvalidValue_ReturnsEpoch()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.CreationDate] = (BNumber)long.MaxValue;

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

            // Assert
            torrent.CreationDate.Should().Be(new DateTime(1970, 1, 1));
        }
Пример #8
0
        public void OriginalInfoHash_IsSet()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var expectedInfoHash = TorrentUtil.CalculateInfoHash(ParsedData.Get <BDictionary>("info"));

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

            // Assert
            torrent.OriginalInfoHash.Should().Be(expectedInfoHash);
        }
Пример #9
0
        public void Encoding_ASCII_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.ASCII);
        }
Пример #10
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((BencodeReader)null);

            // Assert
            torrent.Comment.Should().Be(comment);
        }
Пример #11
0
        public void OriginalInfoHashBytes_IsSet()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var expectedInfoHashBytes = TorrentUtil.CalculateInfoHashBytes(ParsedData.Get <BDictionary>("info"));

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

            // Assert
            torrent.OriginalInfoHashBytes.Should().Equal(expectedInfoHashBytes);
        }
Пример #12
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((BencodeReader)null);

            // Assert
            torrent.CreatedBy.Should().Be(createdBy);
        }
Пример #13
0
        public void CreationDate_IsParsed()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData[TorrentFields.CreationDate] = (BNumber)1451606400;

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

            // Assert
            torrent.CreationDate.Should().Be(new DateTime(2016, 1, 1));
        }
Пример #14
0
        public void Root_MissingInfoField_Strict_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            ParsedData.Remove(TorrentFields.Info);

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

            // Assert
            action.Should().Throw <InvalidTorrentException>()
            .Where(ex => ex.InvalidField == TorrentFields.Info);
        }
Пример #15
0
        public static void Main(string[] args)
        {
            ConsoleManager cmgr = new ConsoleManager(Console.CursorLeft, Console.CursorTop, Console.BufferWidth, Console.BufferWidth);
            Out            o    = new Out(cmgr);
            In             i    = new In(cmgr);

            o.WriteLine("torrentchk v2.2");
            o.WriteLine("Note: pieces is always SHA-1 calculated");
            o.Write("Enter path to torrent file: ");
            string loc;

            i.ReadLine(out loc);
            byte[] content = File.ReadAllBytes(loc);
            o.Write("Enter download path: ");
            string dlLoc;

            i.ReadLine(out dlLoc);

            bool ok = TorrentParser.CheckTorrentIntegrityV2(loc, dlLoc, (JObject status) => {
                switch ((string)status["status"])
                {
                case "parsing":
                    o.WriteLine("Parsing torrent file...");
                    cmgr.UpdateNextWrite();
                    break;

                case "processing_piece":
                    int currentPiece = (int)status["processing_piece"]["current_piece"];
                    int totalPieces  = (int)status["processing_piece"]["total_pieces"];
                    o.WriteLine("Processing piece " + currentPiece + " of " + totalPieces + ". All processed pieces so far are OK.");
                    break;
                }
            });

            if (ok)
            {
                o.WriteLine("Every piece passed the hash checking.");
            }
            else
            {
                o.WriteLine("Last piece did NOT pass the hash checking.");
            }

            cmgr.DisableUpdate();

            o.WriteLine("Press any key to exit...");
            string key;

            i.ReadKey(out key);
        }
Пример #16
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((BencodeReader)null);

            // Assert
            torrent.PieceSize.Should().Be(pieceSize);
        }
Пример #17
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((BencodeReader)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(1);
            torrent.Trackers[0][0].Should().Be(announceUrl);
        }
Пример #18
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((BencodeReader)null);

            // Assert
            torrent.ExtraFields.Should().Contain(extraKey, (BString)extraValue);
            torrent.ExtraFields.Get <BDictionary>(TorrentFields.Info).Should().Contain(extraInfoKey, (BString)extraInfoValue);
        }
Пример #19
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((BencodeReader)null);

            // Assert
            torrent.Pieces.Should().Equal(pieces);
        }
Пример #20
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((BencodeReader)null);

            // Assert
            torrent.IsPrivate.Should().Be(expectedResult);
        }
Пример #21
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);
        }
Пример #22
0
        public void Info_MissingNameField_ThrowsInvalidTorrentException()
        {
            // Arrange
            ParsedData = ValidSingleFileTorrentData;
            var info = ParsedData.Get <BDictionary>(TorrentFields.Info);

            info.Remove(TorrentInfoFields.Name);

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

            // Assert
            action.ShouldThrow <InvalidTorrentException>()
            .Where(ex => ex.InvalidField == TorrentInfoFields.Name);
        }
Пример #23
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}*");
        }
Пример #24
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);
        }
Пример #25
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);
        }
Пример #26
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);
        }
Пример #27
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);
        }
Пример #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((BencodeReader)null);

            // Assert
            torrent.Trackers.Should().HaveCount(1);
            torrent.Trackers[0].Should().HaveCount(2);
            torrent.Trackers[0].Should().ContainInOrder(announceUrl1, announceUrl2);
        }
Пример #29
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);
        }
Пример #30
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);
        }
Пример #31
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);
        }
Пример #32
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);
        }
Пример #33
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}*");
        }
Пример #34
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);
        }
Пример #35
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);
        }
Пример #36
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));
        }
Пример #37
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);
        }
Пример #38
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);
        }
Пример #39
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);
        }
Пример #40
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);
        }
Пример #41
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);
        }
Пример #42
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);
        }
Пример #43
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);
        }
Пример #44
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);
        }
Пример #45
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);
        }
Пример #46
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);
        }
Пример #47
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);
        }
Пример #48
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);
        }