Esempio n. 1
0
        public void CalculatedShaAndTorrentShaSame()
        {
            var(totalLength, files) = GetFiles();
            byte[] bytes;
            using (var ms = ReadWholeContent(files))
            {
                bytes = ms.ToArray();
            }
            Torrent torrent;

            using (var file = File.Open("torrent.torrent", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    torrent = new TorrentSerializer().Deserialize(ms.ToArray());
                }
            }
            // assuming the file lengths is less than piece key;
            byte[] computedHash;
            using (var sha = SHA1.Create())
            {
                computedHash = sha.ComputeHash(bytes);
            }
            var b = new BString(computedHash);

            Assert.Equal(torrent.Info.Pieces, b);
        }
Esempio n. 2
0
 public void RealTorrentParsed()
 {
     using (var file = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         var parser = new TorrentSerializer();
         var trt    = parser.Deserialize(file);
     }
 }
Esempio n. 3
0
 public void ReturnsCreator()
 {
     using (var file = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         var parser = new TorrentSerializer();
         var trt    = parser.Deserialize(file);
         Assert.True(trt.TryGetExtension(ExtensionKeys.CreatedBy, out BString value));
         Assert.NotNull(value);
     }
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                PrintUsage();
                return;
            }

            string filePath         = args[0];
            string torrentDirectory = args[1];
            bool   auditMode        = true;

            if (!Boolean.TryParse(args[2], out auditMode))
            {
                PrintUsage();
                return;
            }

            var fileSystemEntryList = Directory.EnumerateFileSystemEntries(filePath);
            var torrentList         = Directory.EnumerateFiles(torrentDirectory);
            var torrentSerializer   = new TorrentSerializer();


            var fsEntryQuery = fileSystemEntryList.Where(file => {
                var fileName = file.Replace("/data/transmission-downloads/", "");
                return(!(torrentList.Select(torrentFile =>
                                            torrentSerializer.Deserialize(File.Open(torrentFile, FileMode.Open,
                                                                                    FileAccess.Read, FileShare.Read)))
                         .Where(t => t.Info.Name == fileName).Any()));
            });

            foreach (var fsEntry in fsEntryQuery)
            {
                Console.WriteLine($"{((auditMode) ? "AUDIT MODE: " : "")} Deleting {fsEntry}...");
                try {
                    if (File.Exists(fsEntry) && !auditMode)
                    {
                        File.Delete(fsEntry);
                    }
                    if (Directory.Exists(fsEntry) && !auditMode)
                    {
                        Directory.Delete(fsEntry, true);
                    }
                }
                catch {
                    Console.WriteLine($"\tFailed to delete {fsEntry}...");
                }
                if (!File.Exists(fsEntry) && !Directory.Exists(fsEntry))
                {
                    Console.WriteLine($"\tSuccesfully deleted {fsEntry}");
                }
            }
        }
Esempio n. 5
0
 public void SavesFile()
 {
     using (var file = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         var parser  = new TorrentSerializer();
         var trt     = parser.Deserialize(file);
         var builder = TorrentBuilder.FromExisting(trt);
         builder.SetName("русский беларускі ў");
         var modified = builder.Build();
         using (var file2 = File.Create("output.torrent"))
         {
             parser.Serialize(file2, modified);
         }
     }
 }
Esempio n. 6
0
        public void InfoHashCorrect()
        {
            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                using (var file = File.Open("torrent.torrent", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    file.CopyTo(ms);
                }
                bytes = ms.ToArray();
            }
            var torrent  = new TorrentSerializer().Deserialize(bytes);
            var expected = "6f69efbd1d5aeea35aeb90365afb949d8d0f3c09";
            var hash     = torrent.GetInfoHash();

            Assert.Equal(expected, BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant());
        }
Esempio n. 7
0
        public void TorrentBuilderCorrectlyCalculateHash()
        {
            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                using (var file = File.Open("torrent.torrent", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    file.CopyTo(ms);
                }
                bytes = ms.ToArray();
            }
            var torrent = new TorrentSerializer().Deserialize(bytes);
            var before  = torrent.Info.Pieces;
            var builder = TorrentBuilder.FromExisting(torrent);

            builder.CalculatePieces(new FSProvider());
            var builded = builder.Build();

            Assert.Equal(before, builded.Info.Pieces);
        }
Esempio n. 8
0
        public void TorrentBuilderBuildsCorrectTorrent()
        {
            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                using (var file = File.Open("torrent.torrent", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    file.CopyTo(ms);
                }
                bytes = ms.ToArray();
            }
            var torrent = new TorrentSerializer().Deserialize(bytes);
            var builder = new TorrentBuilder(torrent.Encoding ?? Encoding.UTF8);

            foreach (var(path, len) in torrent.Info.Files)
            {
                builder.AddFile(string.Join(Path.DirectorySeparatorChar, path.Select(_ => ((BString)_).ToString())), len);
            }
            builder.SetPieceLength(torrent.Info.PieceLength);
            builder.CalculatePieces(new FSProvider());
            var builded = builder.Build();

            Assert.Equal(builded.Info.Pieces, torrent.Info.Pieces);
        }