예제 #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);
        }