Exemple #1
0
        public void DisplayName_UnknownFileMode_ThrowsBencodeException()
        {
            var torrent = new Torrent();

            Func<string> act = () => torrent.DisplayName;

            torrent.FileMode.Should().Be(TorrentFileMode.Unknown);
            act.Invoking(x => x()).ShouldThrow<BencodeException>();
        }
Exemple #2
0
        public void DisplayName_MultiFile_IsDirectoryName(string directoryName)
        {
            var torrent = new Torrent
            {
                Files = new MultiFileInfoList(directoryName)
                {
                    new MultiFileInfo()
                }
            };

            torrent.FileMode.Should().Be(TorrentFileMode.Multi);
            torrent.DisplayName.Should().Be(directoryName);
        }
Exemple #3
0
        public void DisplayName_SingleFile_IsFileName(string fileName)
        {
            var torrent = new Torrent
            {
                File = new SingleFileInfo
                {
                    FileName = fileName
                }
            };

            torrent.FileMode.Should().Be(TorrentFileMode.Single);
            torrent.DisplayName.Should().Be(fileName);
        }
Exemple #4
0
        public void Equals_ComparesContent(long fileSize, string fileName, string comment, string createdBy, DateTime creationDate, long pieceSize, IList<IList<string>> trackers)
        {
            var t1 = new Torrent
            {
                Encoding = Encoding.UTF8,
                File = new SingleFileInfo
                {
                    FileSize = fileSize,
                    FileName = fileName
                },
                Comment = comment,
                CreatedBy = createdBy,
                CreationDate = creationDate,
                IsPrivate = true,
                PieceSize = pieceSize,
                Trackers = trackers
            };

            var t2 = new Torrent
            {
                Encoding = Encoding.UTF8,
                File = new SingleFileInfo
                {
                    FileSize = fileSize,
                    FileName = fileName
                },
                Comment = comment,
                CreatedBy = createdBy,
                CreationDate = creationDate,
                IsPrivate = true,
                PieceSize = pieceSize,
                Trackers = trackers
            };

            t1.Equals(t2).Should().BeTrue();
            t2.Equals(t1).Should().BeTrue();
            (t1 == t2).Should().BeTrue();
        }
Exemple #5
0
        public void ToBDictionary_Encoding_DoesNotAddNull()
        {
            var torrent = new Torrent {Encoding = null};

            var result = torrent.ToBDictionary();

            result.Should().NotContainKey(TorrentFields.Encoding);
        }
Exemple #6
0
        public void ToBDictionary_CreationDate_DoesNotAddNull()
        {
            var torrent = new Torrent {CreationDate = null};

            var result = torrent.ToBDictionary();

            result.Should().NotContainKey(TorrentFields.CreationDate);
        }
Exemple #7
0
        public void ToBDictionary_Encoding_AddsToCorrectField()
        {
            var torrent = new Torrent {Encoding = Encoding.UTF8};

            var result = torrent.ToBDictionary();

            result.Should().Contain(TorrentFields.Encoding, (BString)Encoding.UTF8.WebName.ToUpper());
        }
Exemple #8
0
        public void FileMode_IsMultiFile_IfFilesIsNotEmpty()
        {
            var torrent = new Torrent
            {
                Files = new MultiFileInfoList
                {
                    new MultiFileInfo()
                }
            };

            torrent.FileMode.Should().Be(TorrentFileMode.Multi);
        }
Exemple #9
0
        /// <summary>
        /// Calculates the info hash of the torrent.
        /// The info hash is a 20-byte SHA1 hash of the 'info'-dictionary of the torrent
        /// used to uniquely identify it and it's contents.
        ///
        /// <para>Example: 6D60711ECF005C1147D8973A67F31A11454AB3F5</para>
        /// </summary>
        /// <param name="torrent">The torrent to calculate the info hash for.</param>
        /// <returns>A byte-array of the 20-byte SHA1 hash.</returns>
        public static byte[] CalculateInfoHashBytes(Torrent torrent)
        {
            var info = torrent.ToBDictionary().Get <BDictionary>("info");

            return(CalculateInfoHashBytes(info));
        }
Exemple #10
0
        public void ToBDictionary_ExtraFields_AreAdded(BString key, BString value)
        {
            // Arrange
            var torrent = new Torrent
            {
                ExtraFields = new BDictionary
                {
                    [key] = value
                }
            };

            // Act
            var result = torrent.ToBDictionary();

            // Assert
            result.Should().Contain(key, value);
        }
Exemple #11
0
        public void NumberOfPieces_ShouldBeTotalSizeDividedByPieceSizeRoundedUp(long fileSize, long pieceSize)
        {
            var expected = (int)Math.Ceiling((double) fileSize/pieceSize);

            var torrent = new Torrent
            {
                File = new SingleFileInfo
                {
                    FileSize = fileSize
                },
                PieceSize = pieceSize
            };

            torrent.TotalSize.Should().Be(fileSize);
            torrent.NumberOfPieces.Should().Be(expected);
        }
Exemple #12
0
 public void PiecesAsHexString_Set_ValueWithNonUppercaseHexCharacterShouldThrowArgumentException(string value)
 {
     var torrent = new Torrent();
     Action action = () => torrent.PiecesAsHexString = value;
     action.ShouldThrow<ArgumentException>();
 }
Exemple #13
0
        public void ToBDictionary_ExtraFields_OverwritesExistingData(string comment, BString extraValue)
        {
            // Arrange
            var torrent = new Torrent
            {
                Comment = comment,
                ExtraFields = new BDictionary
                {
                    {TorrentFields.Comment, extraValue}
                }
            };

            // Act
            var result = torrent.ToBDictionary();

            // Assert
            result.Should().Contain(TorrentFields.Comment, extraValue);
        }
Exemple #14
0
        public void PiecesAsHexString_Set_ValidUppercaseHexSetsPiecesProperty()
        {
            var pieces = new byte[] { 66, 115, 135, 19, 149, 125, 229, 85, 68, 117, 252, 185, 243, 247, 139, 38, 11, 37, 60, 112 };
            var torrent = new Torrent();

            torrent.PiecesAsHexString = "42738713957DE5554475FCB9F3F78B260B253C70";

            torrent.Pieces.Should().Equal(pieces);
        }
Exemple #15
0
        public void FileMode_IsSingleFile_IfFilesIsEmptyAndFileIsNotNull()
        {
            var torrent = new Torrent
            {
                File = new SingleFileInfo(),
                Files = new MultiFileInfoList()
            };

            torrent.FileMode.Should().Be(TorrentFileMode.Single);
        }
Exemple #16
0
 public void PiecesAsHexString_Set_NullThrowsArgumentException()
 {
     var torrent = new Torrent();
     Action action = () => torrent.PiecesAsHexString = null;
     action.ShouldThrow<ArgumentException>();
 }
Exemple #17
0
 public void PiecesAsHexString_Set_LengthNotMultipleOf40ShouldThrowArgumentException(string value)
 {
     var torrent = new Torrent();
     Action action = () => torrent.PiecesAsHexString = value;
     action.ShouldThrow<ArgumentException>();
 }
Exemple #18
0
        public void PiecesAsHexString_Get_ReturnsHexUppercaseWithoutDashes()
        {
            var pieces = new byte[] { 66, 115, 135, 19, 149, 125, 229, 85, 68, 117, 252, 185, 243, 247, 139, 38, 11, 37, 60, 112 };
            var torrent = new Torrent
            {
                Pieces = pieces
            };

            torrent.PiecesAsHexString.Should().Be("42738713957DE5554475FCB9F3F78B260B253C70");
        }
Exemple #19
0
        public void ToBDictionary_Encoding_UsesWebNameToUpper()
        {
            var torrent = new Torrent { Encoding = Encoding.UTF8 };

            var result = torrent.ToBDictionary();

            result.Get<BString>(TorrentFields.Encoding).Should().Be(Encoding.UTF8.WebName.ToUpper());
        }
Exemple #20
0
        public void ToBDictionary_Info_SingleFile_AddsNameAndLength(long fileSize, string fileName)
        {
            // Arrange
            var torrent = new Torrent
            {
                File = new SingleFileInfo
                {
                    FileSize = fileSize,
                    FileName = fileName
                }
            };

            // Act
            var info = torrent.ToBDictionary().Get<BDictionary>("info");

            // Assert
            info.Should().Contain(TorrentInfoFields.Length, (BNumber)fileSize);
            info.Should().Contain(TorrentInfoFields.Name, (BString)fileName);
        }
Exemple #21
0
 public void ToBDictionary_EqualsEmptyBDictionaryForEmptyTorrent()
 {
     var torrent = new Torrent();
     var result = torrent.ToBDictionary();
     result.Should().HaveCount(0);
     result.ShouldBeEquivalentTo(new BDictionary());
 }
Exemple #22
0
        public void TotalSize_SingleFile_ShouldBeFileSize(long fileSize)
        {
            var torrent = new Torrent
            {
                File = new SingleFileInfo
                {
                    FileSize = fileSize
                }
            };

            torrent.TotalSize.Should().Be(fileSize);
        }
Exemple #23
0
        public void ToBDictionary_Announce_NoTrackers_DoesNotAddAnnounce()
        {
            var torrent = new Torrent
            {
                Trackers = new List<IList<string>>
                {
                    new List<string>()
                }
            };

            var result = torrent.ToBDictionary();

            result.Should().NotContainKey(TorrentFields.Announce);
        }
Exemple #24
0
        public void FileMode_IsUnknown_IfFileIsNullAndFilesIsEmpty()
        {
            var torrent = new Torrent
            {
                File = null,
                Files = new MultiFileInfoList()
            };

            torrent.FileMode.Should().Be(TorrentFileMode.Unknown);
        }
Exemple #25
0
        public void ToBDictionary_Info_MultiFile_AddsFiles(string directoryName, long fileSize, IList<string> path)
        {
            // Arrange
            var torrent = new Torrent
            {
                Files = new MultiFileInfoList(directoryName)
                {
                    new MultiFileInfo
                    {
                        FileSize = fileSize,
                        Path = path
                    }
                }
            };

            // Act
            var info = torrent.ToBDictionary().Get<BDictionary>("info");
            var files = info.Get<BList>(TorrentInfoFields.Files).AsType<BDictionary>();

            // Assert
            info.Should().Contain(TorrentInfoFields.Name, (BString) directoryName);
            info.Should().ContainKey(TorrentInfoFields.Files);
            info[TorrentInfoFields.Files].Should().BeOfType<BList<BDictionary>>();
            files[0].Should().BeOfType<BDictionary>();
            files[0].Should().Contain(TorrentFilesFields.Length, (BNumber) fileSize);
            files[0].Should().ContainKey(TorrentFilesFields.Path);
            files[0].Get<BList>(TorrentFilesFields.Path).Should().HaveCount(path.Count);
        }
Exemple #26
0
        public void ToBDictionary_Announce_SingleTracker_AddsAnnounceButNotAddAnnounceList()
        {
            var torrent = new Torrent
            {
                Trackers = new List<IList<string>>
                {
                    new List<string>
                    {
                        "http://sometracker.com"
                    }
                }
            };

            var result = torrent.ToBDictionary();

            result.Should().ContainKey(TorrentFields.Announce);
            result.Should().NotContainKey(TorrentFields.AnnounceList);
        }
Exemple #27
0
        public void TotalSize_MultiFile_ShouldBeSumOfFileSizes(long fileSize1, long fileSize2)
        {
            var torrent = new Torrent
            {
                Files = new MultiFileInfoList
                {
                    new MultiFileInfo {FileSize = fileSize1},
                    new MultiFileInfo {FileSize = fileSize2},
                }
            };

            torrent.TotalSize.Should().Be(fileSize1 + fileSize2);
        }
Exemple #28
0
        public void ToBDictionary_Comment_AddsToCorrectField(string comment)
        {
            var torrent = new Torrent {Comment = comment
            };

            var result = torrent.ToBDictionary();

            result.Should().Contain(TorrentFields.Comment, (BString)comment);
        }
Exemple #29
0
        public void TotalSize_UnknownFileMode_IsZero()
        {
            var torrent = new Torrent();

            torrent.TotalSize.Should().Be(0);
        }
Exemple #30
0
        public void ToBDictionary_CreatedBy_AddsToCorrectField(string createdBy)
        {
            var torrent = new Torrent {CreatedBy = createdBy};

            var result = torrent.ToBDictionary();

            result.Should().Contain(TorrentFields.CreatedBy, (BString)createdBy);
        }
Exemple #31
0
        public void ToBDictionary_CreationDate_AddsToCorrectField(DateTime creationDate)
        {
            var torrent = new Torrent {CreationDate = creationDate};

            var result = torrent.ToBDictionary();

            result.Should().Contain(TorrentFields.CreationDate, (BNumber)creationDate);
        }
Exemple #32
0
        /// <summary>
        /// Calculates the info hash of the torrent.
        /// The info hash is a 20-byte SHA1 hash of the 'info'-dictionary of the torrent
        /// used to uniquely identify it and it's contents.
        ///
        /// <para>Example: 6D60711ECF005C1147D8973A67F31A11454AB3F5</para>
        /// </summary>
        /// <param name="torrent">The torrent to calculate the info hash for.</param>
        /// <returns>A string representation of the 20-byte SHA1 hash without dashes.</returns>
        public static string CalculateInfoHash(Torrent torrent)
        {
            var info = torrent.ToBDictionary().Get <BDictionary>("info");

            return(CalculateInfoHash(info));
        }