public void Writing_Whole_File_In_Streamed_Blocks_Should_Create_Exact_Copy()
        {
            //use an output stream which works with buffers
            StreamedBlockOutputStream os = new StreamedBlockOutputStream(Token, 15000, b => UploadHandler.WriteBlockStreamed(b));
            MemoryStream ms = new MemoryStream(SourceFileContents);

            ms.WriteTo(os);

            UploadHandler.CompleteTransfer(Token.TransferId);

            TargetFile.Refresh();
            FileAssert.AreEqual(SourceFilePath, TargetFilePath);


            TargetFile.Delete();
            InitToken();

            //use a source strem
            using (var fs = SourceFile.OpenRead())
            {
                fs.WriteTo(Token, SourceFile.Length, 10000, b => UploadHandler.WriteBlockStreamed(b));
                UploadHandler.CompleteTransfer(Token.TransferId);

                TargetFile.Refresh();
                FileAssert.AreEqual(SourceFilePath, TargetFilePath);
            }
        }
Beispiel #2
0
        public void Initiating_Token_Should_Not_Delete_Existing_File_Yet()
        {
            Token = UploadHandler.RequestUploadToken(TargetFilePath, true, SourceFile.Length, "");

            TargetFile.Refresh();
            Assert.IsTrue(TargetFile.Exists);
            Assert.AreEqual(2048, TargetFile.Length);
        }
Beispiel #3
0
        public void Cancelling_Transfer_Before_Writing_Any_Data_Should_Retain_File_That_Would_Have_Overwritten()
        {
            Token = UploadHandler.RequestUploadToken(TargetFilePath, true, SourceFile.Length, "");
            UploadHandler.CancelTransfer(Token.TransferId, AbortReason.ClientAbort);

            TargetFile.Refresh();
            Assert.IsTrue(TargetFile.Exists);
            Assert.AreEqual(2048, TargetFile.Length);
        }
Beispiel #4
0
        public void Cancelling_Transfer_While_Uploading_Should_Delete_Partial_File()
        {
            Token = UploadHandler.RequestUploadToken(TargetFilePath, true, SourceFile.Length, "");

            var block = CreateBufferedBlocks().First();

            UploadHandler.WriteBlock(block);
            UploadHandler.CancelTransfer(Token.TransferId, AbortReason.ClientAbort);

            TargetFile.Refresh();
            Assert.IsFalse(TargetFile.Exists);
        }
Beispiel #5
0
        public void Writing_First_Block_Should_Replace_File()
        {
            Token = UploadHandler.RequestUploadToken(TargetFilePath, true, SourceFile.Length, "");

            var block = CreateBufferedBlocks().First();

            UploadHandler.WriteBlock(block);

            TargetFile.Refresh();
            Assert.AreNotEqual(2048, TargetFile.Length);
            Assert.AreEqual(block.BlockLength, TargetFile.Length);
        }
        public void Writing_Whole_File_In_Buffered_Blocks_Should_Create_Exact_Copy()
        {
            BufferedBlockOutputStream os = new BufferedBlockOutputStream(Token, 15000, b => UploadHandler.WriteBlock(b));
            MemoryStream ms = new MemoryStream(SourceFileContents);

            ms.WriteTo(os);

            UploadHandler.CompleteTransfer(Token.TransferId);

            TargetFile.Refresh();
            FileAssert.AreEqual(SourceFilePath, TargetFilePath);
        }
        public void Writing_A_Single_Empty_Block_Should_Create_File()
        {
            UploadHandler.CancelTransfer(Token.TransferId, AbortReason.ClientAbort);

            //get token for empty file
            Token = UploadHandler.RequestUploadToken(TargetFilePath, false, 0, "");

            TargetFile.Refresh();
            Assert.IsFalse(TargetFile.Exists);

            var block = new BufferedDataBlock {
                TransferTokenId = Token.TransferId, BlockLength = 0, BlockNumber = 0, Data = new byte[0], IsLastBlock = true
            };

            UploadHandler.WriteBlock(block);

            TargetFile.Refresh();
            Assert.IsTrue(TargetFile.Exists);
        }
        public void Retransmitted_Blocks_Should_Replace_Entries_In_Transferred_Block_Table()
        {
            var block = CreateBufferedBlocks()[10];

            //modify data in one of the blocks
            int blockLength = block.BlockLength.Value / 2;

            block.BlockLength = blockLength;
            block.Data        = block.Data.CreateCopy(blockLength);

            //write block
            UploadHandler.WriteBlock(block);

            //check block
            var blocks = UploadHandler.GetTransferredBlocks(Token.TransferId);

            Assert.AreEqual(1, blocks.Count());
            Assert.AreEqual(blockLength, blocks.Single().BlockLength);

            UploadHandler.PauseTransfer(Token.TransferId);
            TargetFile.Refresh();
            Assert.AreEqual(block.Offset + blockLength, TargetFile.Length);

            //get unmodified block and write that one
            block = CreateBufferedBlocks()[10];
            UploadHandler.WriteBlock(block);

            blocks = UploadHandler.GetTransferredBlocks(Token.TransferId);
            Assert.AreEqual(1, blocks.Count());
            Assert.AreNotEqual(blockLength, blocks.Single().BlockLength);
            Assert.AreEqual(block.BlockLength, blocks.Single().BlockLength);

            UploadHandler.PauseTransfer(Token.TransferId);
            TargetFile.Refresh();
            Assert.AreEqual(block.Offset + block.BlockLength, TargetFile.Length);

            UploadHandler.CompleteTransfer(Token.TransferId);
        }
        public void Unordered_Writing_Should_Be_Supported()
        {
            var blocks = new Dictionary <long, BufferedDataBlock>();

            Random r    = new Random();
            var    list = CreateBufferedBlocks();

            var count = list.Count;

            for (int i = 0; i < count; i++)
            {
                int index = r.Next(list.Count);
                var block = list[index];
                blocks.Add(block.BlockNumber, block);

                UploadHandler.WriteBlock(list[index]);
                list.RemoveAt(index);
            }

            UploadHandler.CompleteTransfer(Token.TransferId);
            TargetFile.Refresh();
            FileAssert.AreEqual(SourceFile, TargetFile);
        }
        public void Uploading_An_Empty_File_Should_Create_The_File()
        {
            UploadHandler.CancelTransfer(Token.TransferId, AbortReason.ClientAbort);

            SourceFile.Delete();
            using (var fs = SourceFile.Create())
            {
                fs.Close();
            }

            SourceFile.Refresh();
            Assert.AreEqual(0, SourceFile.Length);

            TargetFile.Refresh();
            Assert.IsFalse(TargetFile.Exists);

            using (var fs = SourceFile.OpenRead())
            {
                UploadHandler.WriteFile(TargetFilePath, fs, true, 0, "");
            }

            TargetFile.Refresh();
            Assert.IsTrue(TargetFile.Exists);
        }
Beispiel #11
0
        private FileResult FileCopy()
        {
            try
            {
                if (CheckCancel())
                {
                    return(new FileResult()
                    {
                        Job = this, Success = false, Reason = $"Cancelled by user"
                    });
                }
                if (IsFile)
                {
                    if (!FileCreated)
                    {
                        SourceFile.Refresh();
                        TargetFile.Refresh();
                        //FastResume(sourcefile, targetfile, null, BUFFERSIZE);
                        if ((CopyType == CopyJobType.Copy || CopyType == CopyJobType.Move) && Target.Exists)
                        {
                            switch (ExistsAction)
                            {
                            case ExistsAction.Overwrite:
                            case ExistsAction.OverwriteFix:
                                break;

                            case ExistsAction.Skip:
                                return(new FileResult()
                                {
                                    Job = this, Success = true, Reason = "Skipped"
                                });

                            case ExistsAction.Rename:
                                Rename();
                                break;

                            case ExistsAction.None:
                            default:
                                return(new FileResult()
                                {
                                    Job = this, Success = false, IsOverWriteError = true, Reason = "File already exists"
                                });
                            }
                        }
                    }
                    var sourcefile = SourceFile;
                    var targetfile = TargetFile;
                    if (CopyType == CopyJobType.Copy || CopyType == CopyJobType.Move)
                    {
                        FileCreated = true;
                        OnAction?.Invoke(COPYING);
                        FastCopy(sourcefile, targetfile, ProgressP1, BUFFERSIZE);
                        OnAction?.Invoke(COMPARING);
                        if (Compare(sourcefile, targetfile, ProgressP2, BUFFERSIZE, true))
                        {
                            if (CopyType == CopyJobType.Move)
                            {
                                OnAction?.Invoke(DELETING);
                                sourcefile.Delete();
                            }
                            return(new FileResult()
                            {
                                Job = this, Success = true
                            });
                        }
                        else
                        {
                            return(new FileResult()
                            {
                                Job = this, Success = false, Reason = "Compare NOK"
                            });
                        }
                    }
                    if (CopyType == CopyJobType.Compare)
                    {
                        OnAction?.Invoke(COMPARING);
                        if (Compare(sourcefile, targetfile, ProgressP1, BUFFERSIZE, false))
                        {
                            targetfile.Delete();
                        }
                        return(new FileResult()
                        {
                            Job = this, Success = true
                        });
                    }
                }
                else if (Target is ZlpDirectoryInfo td && CopyType != CopyJobType.Compare)
                {
                    td.Create();
                    return(new FileResult()
                    {
                        Job = this, Success = true
                    });
                }
                return(new FileResult()
                {
                    Job = this, Success = false
                });
            }
            catch (Exception e)
            {
                return(new FileResult()
                {
                    Job = this, Success = false, Reason = $"{e.GetType().Name}: {e.Message}"
                });
            }
        }