public void If_File_Size_Is_Not_Multitude_Of_Block_Size_Then_Last_Block_Should_Be_Smaller_Then_Others()
        {
            var maxSize = Downloads.MaxBlockSize ?? 2048;

            //upload a file that consists of a total of 5.5 (6) blocks
            File.WriteAllBytes(Context.DownloadFile0Template.FullName, new byte[maxSize * 5 + maxSize / 2]);
            Context.DownloadFile0Template.Refresh();
            SourceFile = Context.DownloadFolder.AddFile(Context.DownloadFile0Template.FullName, "multiple.bin", false);

            Downloads.CancelTransfer(Token.TransferId, AbortReason.ClientAbort);
            Token = Downloads.RequestDownloadToken(SourceFileInfo.FullName, false, maxSize);

            Assert.AreEqual(maxSize, Token.DownloadBlockSize);
            Assert.AreEqual(6, Token.TotalBlockCount);


            List <BufferedDataBlock> blockList = new List <BufferedDataBlock>();

            int blockNumber = 0;

            while (true)
            {
                BufferedDataBlock block = Downloads.ReadBlock(Token.TransferId, blockNumber++);
                blockList.Add(block);
                if (block.IsLastBlock)
                {
                    break;
                }
            }

            Assert.AreEqual(6, blockList.Count);
            Assert.AreEqual(maxSize / 2, blockList[5].BlockLength);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Cancels the current download, if the <see cref="Token"/>
        /// is set, and resets the <see cref="SystemTime"/>.
        /// </summary>
        protected override void CleanupInternal()
        {
            if (Token != null)
            {
                if (Downloads.GetTransferStatus(Token.TransferId) != TransferStatus.UnknownTransfer)
                {
                    Downloads.CancelTransfer(Token.TransferId, AbortReason.Undefined);
                }
            }

            SystemTime.Reset();
            base.CleanupInternal();
        }
        public void Token_Should_Calculate_MD5_File_Hash_If_Explicitly_Requested()
        {
            //cancel old download
            Assert.IsNullOrEmpty(Token.Md5FileHash);
            Downloads.CancelTransfer(Token.TransferId, AbortReason.ClientAbort);

            //get new token with hash
            Token = Downloads.RequestDownloadToken(Token.ResourceIdentifier, true);
            Assert.IsNotEmpty(Token.Md5FileHash);

            var hash = Context.DownloadFile0Template.CalculateMd5Hash();

            StringAssert.AreEqualIgnoringCase(hash, Token.Md5FileHash);
        }
        public void Aborted_Transfer_Should_Unlock_Immediately()
        {
            //behind the scenes, many blocks are read, independent of the buffer size. Accordingly,
            //the transfer will abort as soon as we want it
            string resourceId = SourceFileInfo.FullName;

            var token = InitToken();

            using (Stream stream = Downloads.DownloadFile(token.TransferId))
            {
                byte[] buffer = new byte[1234];
                stream.Read(buffer, 0, buffer.Length);
                int r = stream.Read(buffer, 1000, 234);
                Assert.AreEqual(234, r);

                //abort the transfer
                Downloads.CancelTransfer(token.TransferId, AbortReason.ClientAbort);

                //try to get upload token
                var ut = FileSystem.UploadTransfers.RequestUploadToken(resourceId, true, 1000, "");
                FileSystem.UploadTransfers.CancelTransfer(ut.TransferId, AbortReason.ClientAbort);
            }
        }
        public void Aborted_Transfer_Should_Cause_Exception_While_Reading_File_Even_If_File_Is_Streamed_At_Once()
        {
            //behind the scenes, many blocks are read, independent of the buffer size. Accordingly,
            //the transfer will abort as soon as we want it

            var token = InitToken();

            using (Stream stream = Downloads.DownloadFile(token.TransferId))
            {
                byte[] buffer = new byte[1234];
                stream.Read(buffer, 0, buffer.Length);
                int r = stream.Read(buffer, 1000, 234);
                Assert.AreEqual(234, r);

                //certain providers may already unlock the file - in this case,
                //we won't be able to complete this test
                var status = Downloads.GetTransferStatus(token.TransferId);
                if (status == TransferStatus.Completed)
                {
                    return;
                }

                //abort the transfer
                Downloads.CancelTransfer(token.TransferId, AbortReason.ClientAbort);

                //try to read again
                try
                {
                    var data = stream.ReadIntoBuffer();
                    Assert.Fail("Expected exception when attempting to read data after cancelling transfer.");
                }
                catch (TransferStatusException expected)
                {
                }
                //stream.Read(buffer, 0, buffer.Length);
            }
        }
 public virtual OperationResult <Wrapped <TransferStatus> > Post(string transferId, AbortReason reason)
 {
     return(SecureFunc(() => new Wrapped <TransferStatus>(Downloads.CancelTransfer(transferId, reason))));
 }