public void PieceIndex_FirstFile()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024);

            Assert.AreEqual(0, files.FindFileByPieceIndex(0));
        }
        public void PieceIndex_EmptyFileAtEnd()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Piece.BlockSize, 0);

            Assert.AreEqual(files.Length - 2, files.FindFileByPieceIndex(1));
        }
Ejemplo n.º 3
0
        public void PieceIndex_LastFile()
        {
            var pieceLength = Constants.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Constants.BlockSize);

            Assert.AreEqual(files.Length - 1, files.FindFileByPieceIndex(1));
        }
        public void PieceIndex_LastFile()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Piece.BlockSize);

            Assert.AreEqual(files.Length - 1, IPieceWriterExtensions.FindFileByPieceIndex(files, 1));
        }
        public void Offset_EmptyFileAtEnd()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Piece.BlockSize, 0);

            Assert.AreEqual(files.Length - 1, IPieceWriterExtensions.FindFileByOffset(files, Piece.BlockSize * 2 - 1, pieceLength));
        }
Ejemplo n.º 6
0
        public void PieceIndex_TwoEmptyFilesAtEnd()
        {
            var pieceLength = Constants.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Constants.BlockSize, 0, 0);

            Assert.AreEqual(files.Length - 3, files.FindFileByPieceIndex(1));
        }
Ejemplo n.º 7
0
        public void Offset_EmptyFileAtEnd()
        {
            var pieceLength = Constants.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Constants.BlockSize, 0);

            Assert.AreEqual(files.Length - 1, files.FindFileByOffset(Constants.BlockSize * 2 - 1));
        }
Ejemplo n.º 8
0
        public void OnetimeSetup()
        {
            var random     = new Random();
            var filePieces = new long[] {
                Piece.BlockSize / 2,
                Piece.BlockSize,
                Piece.BlockSize + Piece.BlockSize / 2,
                Piece.BlockSize * 10 + Piece.BlockSize / 2,
            };

            int pieceLength = Piece.BlockSize * 3;

            var  files     = TorrentFileInfo.Create(pieceLength, filePieces);
            long total     = files.Sum(f => f.Length);
            var  fileBytes = files
                             .Select(f => { var b = new byte[f.Length]; random.NextBytes(b); return(b); })
                             .ToArray();

            // Turn all the files into one byte array. Group the byte array into bittorrent pieces. Hash that piece.
            var hashes = fileBytes
                         .SelectMany(t => t)
                         .Partition(pieceLength)
                         .Select(t => SHA1.Create().ComputeHash(t))
                         .ToArray();

            fileData = new TestTorrentData {
                Data        = fileBytes,
                Files       = files.ToArray(),
                Hashes      = hashes,
                Size        = files.Sum(f => f.Length),
                PieceLength = pieceLength
            };
        }
Ejemplo n.º 9
0
        public void PieceIndex_OverlappingFiles()
        {
            var pieceLength = Constants.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, Constants.BlockSize - 1, Constants.BlockSize, Constants.BlockSize);

            Assert.AreEqual(0, files.FindFileByPieceIndex(0));
            Assert.AreEqual(1, files.FindFileByPieceIndex(1));
        }
        public void Offset_LastFile()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Piece.BlockSize);

            Assert.AreEqual(files.Length - 1, files.FindFileByOffset(Piece.BlockSize));
            Assert.AreEqual(files.Length - 1, files.FindFileByOffset(Piece.BlockSize + 1));
        }
Ejemplo n.º 11
0
        public void Offset_TwoPiecesHaveEmptyFilesAtEnd()
        {
            var pieceLength = Constants.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, 0, Constants.BlockSize, 0);

            Assert.AreEqual(files.Length - 2, files.FindFileByOffset(Constants.BlockSize));
            Assert.AreEqual(0, files.FindFileByOffset(0));
        }
        public void PieceIndex_TwoPiecesHaveEmptyFilesAtEnd()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, 0, Piece.BlockSize, 0);

            Assert.AreEqual(files.Length - 2, IPieceWriterExtensions.FindFileByPieceIndex(files, 1));
            Assert.AreEqual(0, IPieceWriterExtensions.FindFileByPieceIndex(files, 0));
        }
Ejemplo n.º 13
0
        public void Offset_TwoEmptyFilesAtEnd()
        {
            var pieceLength = Constants.BlockSize;
            // If two files start at the same offset (which zero length files do), then the files are ordered based on
            // their length. This way zero length files are never the last file, unless the whole torrent is empty. Which is nonsense :p
            var files = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024, Constants.BlockSize, 0, 0);

            Assert.AreEqual(files.Length - 1, files.FindFileByOffset(Constants.BlockSize * 2 - 1));
        }
        public void Offset_FirstFile()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024);

            Assert.AreEqual(0, IPieceWriterExtensions.FindFileByOffset(files, 0, pieceLength));
            Assert.AreEqual(0, IPieceWriterExtensions.FindFileByOffset(files, 1023, pieceLength));
            Assert.AreEqual(1, IPieceWriterExtensions.FindFileByOffset(files, 1024, pieceLength));
        }
Ejemplo n.º 15
0
        public void Offset_FirstFile()
        {
            var pieceLength = Constants.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, 1024, 1024, 1024, pieceLength - 3 * 1024);

            Assert.AreEqual(0, files.FindFileByOffset(0));
            Assert.AreEqual(0, files.FindFileByOffset(1023));
            Assert.AreEqual(1, files.FindFileByOffset(1024));
        }
        public void PieceIndex_OverlappingFiles2()
        {
            var pieceLength = Piece.BlockSize;
            var files       = TorrentFileInfo.Create(pieceLength, Piece.BlockSize + 1, Piece.BlockSize, Piece.BlockSize);

            Assert.AreEqual(0, files.FindFileByPieceIndex(0));
            Assert.AreEqual(0, files.FindFileByPieceIndex(1));
            Assert.AreEqual(1, files.FindFileByPieceIndex(2));
        }
Ejemplo n.º 17
0
        public async Task MoveFile_SamePath()
        {
            using var tmp = TempDir.Create();
            var file = TorrentFileInfo.Create(Constants.BlockSize, ("file.txt", 123456, Path.Combine(tmp.Path, "orig.txt"))).Single();

            File.OpenWrite(file.FullPath).Close();

            using var writer  = new TestPieceWriter();
            using var manager = new DiskManager(new EngineSettings(), Factories.Default, writer);

            await manager.MoveFileAsync(file, file.FullPath);

            Assert.IsTrue(File.Exists(file.FullPath));
        }
Ejemplo n.º 18
0
        public async Task MoveFile_ConvertsToFullPath()
        {
            using var writer  = new TestPieceWriter();
            using var manager = new DiskManager(new EngineSettings(), Factories.Default, writer);

            var file = TorrentFileInfo.Create(Constants.BlockSize, 123456).Single();

            Assert.IsFalse(File.Exists(file.FullPath));

            await manager.MoveFileAsync(file, "NewPath");

            Assert.AreEqual(Path.GetFullPath("NewPath"), file.FullPath);
            Assert.IsFalse(File.Exists(file.FullPath));
        }
Ejemplo n.º 19
0
        public async Task MoveFile_TargetDirectoryDoesNotExist()
        {
            using var tmp = TempDir.Create();
            var file = TorrentFileInfo.Create(Constants.BlockSize, ("file.txt", 123456, Path.Combine(tmp.Path, "orig.txt"))).Single();

            File.OpenWrite(file.FullPath).Close();

            using var writer  = new TestPieceWriter();
            using var manager = new DiskManager(new EngineSettings(), Factories.Default, writer);

            var fullPath = Path.Combine(tmp.Path, "New", "Path", "file.txt");
            await manager.MoveFileAsync(file, fullPath);

            Assert.AreEqual(fullPath, file.FullPath);
        }
Ejemplo n.º 20
0
        public async Task MoveFiles_Overwrite_SameDir()
        {
            using var tmp = TempDir.Create();

            var file = TorrentFileInfo.Create(Constants.BlockSize, (Path.Combine("sub_dir", "orig.txt"), 123456, Path.Combine(tmp.Path, "sub_dir", "orig.txt"))).Single();

            Directory.CreateDirectory(Path.GetDirectoryName(file.FullPath));
            File.OpenWrite(file.FullPath).Close();

            using var writer  = new TestPieceWriter();
            using var manager = new DiskManager(new EngineSettings(), Factories.Default, writer);

            await manager.MoveFilesAsync(new[] { file }, tmp.Path, true);

            Assert.AreEqual(Path.Combine(tmp.Path, file.Path), file.FullPath);
            Assert.IsTrue(File.Exists(file.FullPath));
        }
        public void Setup()
        {
            var pieceLength = Piece.BlockSize * 2;
            var files       = TorrentFileInfo.Create(pieceLength,
                                                     Piece.BlockSize / 2,
                                                     Piece.BlockSize,
                                                     Piece.BlockSize + Piece.BlockSize / 2,
                                                     Piece.BlockSize * 2 + Piece.BlockSize / 2
                                                     );

            buffer = new byte[Piece.BlockSize];
            data   = new TestTorrentData {
                Files       = files,
                Size        = files.Sum(f => f.Length),
                PieceLength = pieceLength
            };

            writer      = new ExceptionWriter();
            diskManager = new DiskManager(new EngineSettings(), writer);
        }
        public void Setup()
        {
            var pieceLength = Constants.BlockSize * 2;
            var files       = TorrentFileInfo.Create(pieceLength,
                                                     Constants.BlockSize / 2,
                                                     Constants.BlockSize,
                                                     Constants.BlockSize + Constants.BlockSize / 2,
                                                     Constants.BlockSize * 2 + Constants.BlockSize / 2
                                                     );

            buffer = new byte[Constants.BlockSize];
            data   = new TestTorrentData {
                Files       = files,
                Size        = files.Sum(f => f.Length),
                PieceLength = pieceLength
            };

            writer      = new ExceptionWriter();
            diskManager = new DiskManager(new EngineSettings(), Factories.Default, writer);
        }
        public void PieceIndex_Invalid()
        {
            var files = TorrentFileInfo.Create(Piece.BlockSize, 1024);

            Assert.Less(IPieceWriterExtensions.FindFileByPieceIndex(files, 1), 0);
        }
        public void Offset_Invalid()
        {
            var files = TorrentFileInfo.Create(Piece.BlockSize, 1024);

            Assert.Less(IPieceWriterExtensions.FindFileByOffset(files, Piece.BlockSize * 5, Piece.BlockSize), 0);
        }
Ejemplo n.º 25
0
        public void Offset_Invalid()
        {
            var files = TorrentFileInfo.Create(Constants.BlockSize, 1024);

            Assert.Less(files.FindFileByOffset(Constants.BlockSize * 5), 0);
        }
Ejemplo n.º 26
0
        public void PieceIndex_Invalid()
        {
            var files = TorrentFileInfo.Create(Constants.BlockSize, 1024);

            Assert.Less(files.FindFileByPieceIndex(1), 0);
        }