public FileAsyncCopyController(
            TransferScheduler transferScheduler,
            TransferJob transferJob,
            CancellationToken cancellationToken)
            : base(transferScheduler, transferJob, cancellationToken)
        {
            if (transferJob.Destination.Type != TransferLocationType.AzureFile)
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ParameterCannotBeNullException,
                        "Dest.AzureFile"),
                    "transferJob");
            }

            if (transferJob.Source.Type != TransferLocationType.SourceUri &&
                transferJob.Source.Type != TransferLocationType.AzureBlob &&
                transferJob.Source.Type != TransferLocationType.AzureFile)
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ProvideExactlyOneOfThreeParameters,
                        "Source.SourceUri",
                        "Source.Blob",
                        "Source.AzureFile"),
                    "transferJob");
            }

            this.destLocation = this.TransferJob.Destination as AzureFileLocation;
            this.destFile = this.destLocation.AzureFile;
        }
        private void CopyFromFile2File(Action <CloudFileShare> SetSourceContext, Func <CloudFileShare, object> getDestContext)
        {
            string         destShareName = Utility.GenNameString("destshare");
            CloudFileShare destShare     = fileUtil.EnsureFileShareExists(destShareName);

            string         sourceShareName = Utility.GenNameString("sourceshare");
            CloudFileShare sourceShare     = fileUtil.EnsureFileShareExists(sourceShareName);

            try
            {
                StorageFile.CloudFile sourceFile = fileUtil.CreateFile(sourceShare.GetRootDirectoryReference(), Utility.GenNameString("SourceFile"));

                object destContext = getDestContext(destShare);

                SetSourceContext(sourceShare);

                string destFileName = Utility.GenNameString("destfile");
                Test.Assert(CommandAgent.StartFileCopyFromFile(sourceShareName, sourceFile.Name, destShareName, destFileName, destContext), "Copy to file with sas token credential should succeed.");

                var destFile = fileUtil.GetFileReference(destShare.GetRootDirectoryReference(), destFileName);

                Test.Assert(CommandAgent.GetFileCopyState(destFile, destContext, true), "Get file copy state should succeed.");

                CloudFileUtil.ValidateCopyResult(sourceFile, destFile);
            }
            finally
            {
                fileUtil.DeleteFileShareIfExists(sourceShareName);
                fileUtil.DeleteFileShareIfExists(destShareName);
            }
        }
        public void StartCopyFromNonPublicUriCrossAccount()
        {
            string         shareName = Utility.GenNameString("share");
            CloudFileShare share     = fileUtil2.EnsureFileShareExists(shareName);

            string         destShareName = Utility.GenNameString("destshare");
            CloudFileShare destShare     = fileUtil.EnsureFileShareExists(destShareName);

            try
            {
                string fileName = Utility.GenNameString("fileName");
                StorageFile.CloudFile sourceFile = fileUtil2.CreateFile(share, fileName);

                StorageFile.CloudFile destFile = fileUtil.GetFileReference(destShare.GetRootDirectoryReference(), fileName);

                Agent.Context = null;

                Test.Assert(!CommandAgent.StartFileCopy(sourceFile.Uri.ToString(), destFile), "Copy from non public non sas uri should fail.");
                ExpectedContainErrorMessage("The specified resource does not exist.");
            }
            finally
            {
                fileUtil2.DeleteFileShareIfExists(shareName);
                fileUtil.DeleteFileShareIfExists(destShareName);
            }
        }
        public void CopyCrossAccountFromFile2SASFile()
        {
            string         destShareName = Utility.GenNameString("destshare");
            CloudFileShare destShare     = fileUtil2.EnsureFileShareExists(destShareName);

            string         sourceShareName = Utility.GenNameString("sourceshare");
            CloudFileShare sourceShare     = fileUtil.EnsureFileShareExists(sourceShareName);

            try
            {
                StorageFile.CloudFile sourceFile = fileUtil.CreateFile(sourceShare.GetRootDirectoryReference(), Utility.GenNameString("SourceFile"));

                string destFileName = Utility.GenNameString("destfile");
                string destSasToken = destShare.GetSharedAccessSignature(new SharedAccessFilePolicy()
                {
                    Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write,
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddHours(1)
                });

                object destContext = CommandAgent.GetStorageContextWithSASToken(StorageAccount2, destSasToken);

                Test.Assert(CommandAgent.StartFileCopy(sourceFile, destShareName, destFileName, destContext), "Copy to file with sas token credential should fail.");

                Test.Assert(CommandAgent.GetFileCopyState(destShareName, destFileName, destContext, true), "Get file copy state should succeed.");

                CloudFileUtil.ValidateCopyResult(sourceFile, destShare.GetRootDirectoryReference().GetFileReference(destFileName));
            }
            finally
            {
                fileUtil.DeleteFileShareIfExists(sourceShareName);
                fileUtil2.DeleteFileShareIfExists(destShareName);
            }
        }
 internal CloudFileWriter(
     TransferScheduler scheduler,
     SyncTransferController controller,
     CancellationToken cancellationToken)
     : base(scheduler, controller, cancellationToken)
 {
     this.cloudFile = this.TransferJob.Destination.AzureFile;
 }
Example #6
0
 public static async Task<string> DownloadTextAsync(CloudFile file, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         await file.DownloadToStreamAsync(stream, accessCondition, options, operationContext);
         return encoding.GetString(stream.ToArray(), 0, (int)stream.Length);
     }
 }
 public CloudFileReader(
     TransferScheduler scheduler,
     SyncTransferController controller,
     CancellationToken cancellationToken)
     :base(scheduler, controller, cancellationToken)
 {
     this.file = this.SharedTransferData.TransferJob.Source.AzureFile;
     Debug.Assert(null != this.file, "Initializing a CloudFileReader, the source location should be a CloudFile instance.");
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureFileLocation"/> class.
        /// </summary>
        /// <param name="azureFile">CloudFile instance as a location in a transfer job. 
        /// It could be a source, a destination.</param>
        public AzureFileLocation(CloudFile azureFile)
        { 
            if (null == azureFile)
            {
                throw new ArgumentNullException("azureFile");
            }

            this.AzureFile = azureFile;
        }
 public static void WaitForCopyTask(CloudFile file)
 {
     bool copyInProgress = true;
     while (copyInProgress)
     {
         Thread.Sleep(1000);
         file.FetchAttributesAsync().Wait();
         copyInProgress = (file.CopyState.Status == CopyStatus.Pending);
     }
 }
Example #10
0
 public static async Task WaitForCopyAsync(CloudFile file)
 {
     bool copyInProgress = true;
     while (copyInProgress)
     {
         await Task.Delay(1000);
         await file.FetchAttributesAsync();
         copyInProgress = (file.CopyState.Status == CopyStatus.Pending);
     }
 }
Example #11
0
 public static async Task UploadTextAsync(CloudFile file, string text, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     byte[] textAsBytes = encoding.GetBytes(text);
     using (MemoryStream stream = new MemoryStream())
     {
         stream.Write(textAsBytes, 0, textAsBytes.Length);
         stream.Seek(0, SeekOrigin.Begin);
         file.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;
         await file.UploadFromStreamAsync(stream, accessCondition, options, operationContext);
     }
 }
 public static void AssertAreEqual(CloudFile expected, CloudFile actual)
 {
     if (expected == null)
     {
         Assert.IsNull(actual);
     }
     else
     {
         Assert.IsNotNull(actual);
         Assert.AreEqual(expected.Uri, actual.Uri);
         Assert.AreEqual(expected.StorageUri, actual.StorageUri);
         AssertAreEqual(expected.Properties, actual.Properties);
     }
 }
        public void GetCopyStateWithSAS()
        {
            string         destShareName = Utility.GenNameString("destshare");
            CloudFileShare destShare     = fileUtil.EnsureFileShareExists(destShareName);

            try
            {
                string fileName = Utility.GenNameString("DestFile");
                StorageFile.CloudFile destFile = fileUtil.GetFileReference(destShare.GetRootDirectoryReference(), fileName);

                object destContext;
                if (lang == Language.PowerShell)
                {
                    destContext = PowerShellAgent.GetStorageContext(StorageAccount.ToString(true));
                }
                else
                {
                    destContext = NodeJSAgent.GetStorageContext(StorageAccount.ToString(true));
                }

                string bigBlobUri = Test.Data.Get("BigBlobUri");

                Test.Assert(CommandAgent.StartFileCopy(bigBlobUri, destShareName, fileName, destContext), "Copy to file should succeed.");

                string sasToken = destShare.GetSharedAccessSignature(new SharedAccessFilePolicy()
                {
                    Permissions            = SharedAccessFilePermissions.Read,
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddHours(1)
                });

                CommandAgent.SetStorageContextWithSASToken(StorageAccount.Credentials.AccountName, sasToken);

                Test.Assert(CommandAgent.GetFileCopyState(destShareName, fileName, destContext), "Get copy state with sas token should succeed.");

                string copyId = null;
                if (lang == Language.NodeJS)
                {
                    copyId = ((JObject)CommandAgent.Output[0]["copy"])["id"].ToString();
                }

                NodeJSAgent.AgentConfig.ConnectionString = StorageAccount.ToString(true);
                Test.Assert(CommandAgent.StopFileCopy(destFile, copyId), "Stop file copy should succeed.");
            }
            finally
            {
                fileUtil.DeleteFileShareIfExists(destShareName);
            }
        }
Example #14
0
        public void StopFileCopyWithInvalidCredential()
        {
            string         shareName = Utility.GenNameString("share");
            CloudFileShare share     = fileUtil.EnsureFileShareExists(shareName);

            try
            {
                StorageFile.CloudFile file = fileUtil.CreateFile(share.GetRootDirectoryReference(), Utility.GenNameString(""));
                Test.Assert(!CommandAgent.StopFileCopy(shareName, file.Name, Guid.NewGuid().ToString()), "Stop file copy with invalid credential should fail.");
                CheckErrorMessage();
            }
            finally
            {
                fileUtil.DeleteFileShareIfExists(shareName);
            }
        }
        /// <summary>
        /// Stop copy operation by CloudBlob object
        /// </summary>
        /// <param name="blob">CloudBlob object</param>
        /// <param name="copyId">Copy id</param>
        private async Task StopCopyFile(long taskId, IStorageFileManagement localChannel, CloudFile file, string copyId)
        {
            FileRequestOptions requestOptions = RequestOptions;

            //Set no retry to resolve the 409 conflict exception
            requestOptions.RetryPolicy = new NoRetry();

            string abortCopyId = string.Empty;

            if (string.IsNullOrEmpty(copyId) || Force)
            {
                //Make sure we use the correct copy id to abort
                //Use default retry policy for FetchBlobAttributes
                FileRequestOptions options = RequestOptions;
                await localChannel.FetchFileAttributesAsync(file, null, options, OperationContext, CmdletCancellationToken);

                if (file.CopyState == null || string.IsNullOrEmpty(file.CopyState.CopyId))
                {
                    ArgumentException e = new ArgumentException(String.Format(Resources.FileCopyTaskNotFound, file.Uri.ToString()));
                    OutputStream.WriteError(taskId, e);
                }
                else
                {
                    abortCopyId = file.CopyState.CopyId;
                }

                if (!Force)
                {
                    string confirmation = String.Format(Resources.ConfirmAbortFileCopyOperation, file.Uri.ToString(), abortCopyId);
                    if (!await OutputStream.ConfirmAsync(confirmation))
                    {
                        string cancelMessage = String.Format(Resources.StopCopyOperationCancelled, file.Uri.ToString());
                        OutputStream.WriteVerbose(taskId, cancelMessage);
                    }
                }
            }
            else
            {
                abortCopyId = copyId;
            }

            await localChannel.AbortCopyAsync(file, abortCopyId, null, requestOptions, OperationContext, CmdletCancellationToken);
            string message = String.Format(Resources.StopCopyFileSuccessfully, file.Uri.ToString());
            OutputStream.WriteObject(taskId, message);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FileReadStreamBase"/> class.
        /// </summary>
        /// <param name="file">File reference to read from</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the file. If <c>null</c>, no condition is used.</param>
        /// <param name="options">An <see cref="FileRequestOptions"/> object that specifies additional options for the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        protected FileReadStreamBase(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
        {
            if (options.UseTransactionalMD5.Value)
            {
                CommonUtility.AssertInBounds("StreamMinimumReadSizeInBytes", file.StreamMinimumReadSizeInBytes, 1, Constants.MaxRangeGetContentMD5Size);
            }

            this.file = file;
            this.fileProperties = new FileProperties(file.Properties);
            this.currentOffset = 0;
            this.streamMinimumReadSizeInBytes = this.file.StreamMinimumReadSizeInBytes;
            this.internalBuffer = new MultiBufferMemoryStream(file.ServiceClient.BufferManager);
            this.accessCondition = accessCondition;
            this.options = options;
            this.operationContext = operationContext;
            this.fileMD5 = (this.options.DisableContentMD5Validation.Value || string.IsNullOrEmpty(this.fileProperties.ContentMD5)) ? null : new MD5Wrapper();
            this.lastException = null;
        }
        internal static void SetFile(ref SerializableCloudFile fileSerialization, CloudFile value)
        {
            if (null == fileSerialization
                && null == value)
            {
                return;
            }

            if (null != fileSerialization)
            {
                fileSerialization.File = value;
            }
            else
            {
                fileSerialization = new SerializableCloudFile()
                {
                    File = value
                };
            }
        }
        public void StartCopyFromInvalidContext()
        {
            string         destShareName = Utility.GenNameString("destshare");
            CloudFileShare destShare     = fileUtil.EnsureFileShareExists(destShareName);

            string         sourceShareName = Utility.GenNameString("sourceshare");
            CloudFileShare sourceShare     = fileUtil.EnsureFileShareExists(sourceShareName);

            try
            {
                StorageFile.CloudFile sourceFile = fileUtil.CreateFile(sourceShare.GetRootDirectoryReference(), Utility.GenNameString("SourceFile"));

                object destContext;
                if (lang == Language.PowerShell)
                {
                    destContext = PowerShellAgent.GetStorageContext(StorageAccount.ToString(true));
                }
                else
                {
                    destContext = NodeJSAgent.GetStorageContext(StorageAccount.ToString(true));
                }

                string sasToken = sourceShare.GetSharedAccessSignature(new SharedAccessFilePolicy()
                {
                    Permissions            = SharedAccessFilePermissions.Write,
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddHours(1)
                });

                CommandAgent.SetStorageContextWithSASToken(StorageAccount.Credentials.AccountName, sasToken);

                string destFileName = Utility.GenNameString("destfile");
                Test.Assert(!CommandAgent.StartFileCopyFromFile(sourceShareName, sourceFile.Name, destShareName, destFileName, destContext), "Copy to file with invalid sas token credential should fail.");

                ExpectedContainErrorMessage("This request is not authorized to perform this operation using this permission.");
            }
            finally
            {
                fileUtil.DeleteFileShareIfExists(sourceShareName);
                fileUtil.DeleteFileShareIfExists(destShareName);
            }
        }
 /// <summary>
 /// Initializes a new instance of the FileWriteStreamBase class for a file.
 /// </summary>
 /// <param name="file">File reference to write to.</param>
 /// <param name="fileSize">Size of the file.</param>
 /// <param name="createNew">Use <c>true</c> if the file is newly created, <c>false</c> otherwise.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the file. If <c>null</c>, no condition is used.</param>
 /// <param name="options">An <see cref="FileRequestOptions"/> object that specifies additional options for the request.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
 protected FileWriteStreamBase(CloudFile file, long fileSize, bool createNew, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
     : base()
 {
     this.internalBuffer = new MultiBufferMemoryStream(file.ServiceClient.BufferManager);
     this.currentOffset = 0;
     this.accessCondition = accessCondition;
     this.options = options;
     this.operationContext = operationContext;
     this.noPendingWritesEvent = new CounterEvent();
     this.fileMD5 = this.options.StoreFileContentMD5.Value ? new MD5Wrapper() : null;
     this.rangeMD5 = this.options.UseTransactionalMD5.Value ? new MD5Wrapper() : null;
     this.parallelOperationSemaphore = new AsyncSemaphore(options.ParallelOperationThreadCount.Value);
     this.lastException = null;
     this.committed = false;
     this.disposed = false;
     this.currentFileOffset = 0;
     this.file = file;
     this.fileSize = fileSize;
     this.streamWriteSizeInBytes = file.StreamWriteSizeInBytes;
     this.newFile = createNew;
 }
Example #20
0
        public void StopFinishedCopyFromFileTest()
        {
            string         srcShareName = Utility.GenNameString("share");
            CloudFileShare srcShare     = fileUtil.EnsureFileShareExists(srcShareName);

            string             destContainerName = Utility.GenNameString("container");
            CloudBlobContainer destContainer     = blobUtil.CreateContainer(destContainerName);

            try
            {
                string fileName            = Utility.GenNameString("fileName");
                StorageFile.CloudFile file = fileUtil.CreateFile(srcShare.GetRootDirectoryReference(), fileName);

                CloudBlockBlob blob = destContainer.GetBlockBlobReference(Utility.GenNameString("destBlobName"));

                Test.Assert(CommandAgent.StartAzureStorageBlobCopy(file, destContainer.Name, blob.Name, PowerShellAgent.Context), "Start azure storage copy from file to blob should succeed.");

                while (true)
                {
                    blob.FetchAttributes();

                    if (blob.CopyState.Status != CopyStatus.Pending)
                    {
                        break;
                    }

                    Thread.Sleep(2000);
                }

                Test.Assert(!CommandAgent.StopAzureStorageBlobCopy(destContainerName, blob.Name, null, true), "Stop blob copy should fail");

                ExpectedContainErrorMessage("There is currently no pending copy operation.");
            }
            finally
            {
                fileUtil.DeleteFileShareIfExists(srcShareName);
                blobUtil.RemoveContainer(destContainerName);
            }
        }
        private static string GetFileSASToken(CloudFile file)
        {
            if (null == file.ServiceClient.Credentials
                || file.ServiceClient.Credentials.IsAnonymous)
            {
                return string.Empty;
            }
            else if (file.ServiceClient.Credentials.IsSAS)
            {
                return file.ServiceClient.Credentials.SASToken;
            }

            // SAS life time is at least 10 minutes.
            TimeSpan sasLifeTime = TimeSpan.FromMinutes(CopySASLifeTimeInMinutes);

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
            {
                SharedAccessExpiryTime = DateTime.Now.Add(sasLifeTime),
                Permissions = SharedAccessFilePermissions.Read,
            };

            return file.GetSharedAccessSignature(policy);
        }
Example #22
0
        private bool CloudFileDirectorySetup(CloudFileShare share)
        {
            try
            {
                CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
                for (int i = 1; i < 3; i++)
                {
                    CloudFileDirectory topDirectory = rootDirectory.GetDirectoryReference("TopDir" + i);
                    topDirectory.Create();

                    for (int j = 1; j < 3; j++)
                    {
                        CloudFileDirectory midDirectory = topDirectory.GetDirectoryReference("MidDir" + j);
                        midDirectory.Create();

                        for (int k = 1; k < 3; k++)
                        {
                            CloudFileDirectory endDirectory = midDirectory.GetDirectoryReference("EndDir" + k);
                            endDirectory.Create();

                            CloudFile file1 = endDirectory.GetFileReference("EndFile" + k);
                            file1.Create(0);
                        }
                    }

                    CloudFile file2 = topDirectory.GetFileReference("File" + i);
                    file2.Create(0);
                }

                return(true);
            }
            catch (StorageException e)
            {
                throw e;
            }
        }
Example #23
0
        public void CloudFileDirectoryDelimitersInARow()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);

            CloudFile file = share.GetRootDirectoryReference().GetFileReference(NavigationHelper.Slash + NavigationHelper.Slash + NavigationHelper.Slash + "File1");

            ////Traverse from leaf to root
            CloudFileDirectory directory1 = file.Parent;

            Assert.AreEqual(directory1.Name, NavigationHelper.Slash + NavigationHelper.Slash + NavigationHelper.Slash);

            CloudFileDirectory directory2 = directory1.Parent;

            Assert.AreEqual(directory2.Name, NavigationHelper.Slash + NavigationHelper.Slash);

            CloudFileDirectory directory3 = directory2.Parent;

            Assert.AreEqual(directory3.Name, NavigationHelper.Slash);

            ////Traverse from root to leaf
            CloudFileDirectory directory4 = share.GetRootDirectoryReference().GetDirectoryReference(NavigationHelper.Slash);
            CloudFileDirectory directory5 = directory4.GetDirectoryReference(NavigationHelper.Slash);

            Assert.AreEqual(directory5.Name, NavigationHelper.Slash + NavigationHelper.Slash);

            CloudFileDirectory directory6 = directory5.GetDirectoryReference(NavigationHelper.Slash);

            Assert.AreEqual(directory6.Name, NavigationHelper.Slash + NavigationHelper.Slash + NavigationHelper.Slash);

            CloudFile file2 = directory6.GetFileReference("File1");

            Assert.AreEqual(file2.Name, NavigationHelper.Slash + NavigationHelper.Slash + NavigationHelper.Slash + "File1");
            Assert.AreEqual(file2.Uri, file.Uri);
        }
Example #24
0
        private async Task DoDownloadRangeToByteArrayNegativeTestsAsync(CloudFile file)
        {
            int fileLength = 1024;
            int resultBufSize = 1024;
            byte[] buffer = GetRandomBuffer(fileLength);
            byte[] resultBuffer = new byte[resultBufSize];

            using (MemoryStream stream = new MemoryStream(buffer))
            {
                await file.UploadFromStreamAsync(stream);

                OperationContext context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 0, 1024, 1, null, null, context), context, "Try invalid length", HttpStatusCode.RequestedRangeNotSatisfiable);
                StorageException ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadToByteArrayAsync(resultBuffer, 1024), "Provide invalid offset");
                Assert.IsInstanceOfType(ex.InnerException, typeof(NotSupportedException));
                ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 1023, 0, 2), "Should fail when offset + length required is greater than size of the buffer");
                Assert.IsInstanceOfType(ex.InnerException, typeof(NotSupportedException));
                ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 0, 0, -10), "Fail when a negative length is specified");
                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
                await TestHelper.ExpectedExceptionAsync<ArgumentOutOfRangeException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, -10, 0, 20), "Fail if a negative offset is provided");
                ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 0, -10, 20), "Fail if a negative file offset is provided");
                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
            }
        }
        public void FileSeekTest()
        {
            byte[]         buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();
                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    file.UploadFromStream(srcStream);
                    Stream fileStream = file.OpenRead();
                    fileStream.Seek(2048, 0);
                    byte[] buff    = new byte[100];
                    int    numRead = fileStream.Read(buff, 0, 100);
                    Assert.AreEqual(numRead, 0);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Example #26
0
        private async Task <bool> CloudFileDirectorySetupAsync(CloudFileShare share)
        {
            try
            {
                CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
                for (int i = 1; i < 3; i++)
                {
                    CloudFileDirectory topDirectory = rootDirectory.GetDirectoryReference("TopDir" + i);
                    await topDirectory.CreateAsync();

                    for (int j = 1; j < 3; j++)
                    {
                        CloudFileDirectory midDirectory = topDirectory.GetDirectoryReference("MidDir" + j);
                        await midDirectory.CreateAsync();

                        for (int k = 1; k < 3; k++)
                        {
                            CloudFileDirectory endDirectory = midDirectory.GetDirectoryReference("EndDir" + k);
                            await endDirectory.CreateAsync();

                            CloudFile file1 = endDirectory.GetFileReference("EndFile" + k);
                            await file1.CreateAsync(0);
                        }
                    }

                    CloudFile file2 = topDirectory.GetFileReference("File" + i);
                    await file2.CreateAsync(0);
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public async Task FileDownloadToStreamRangeTestAsync()
        {
            byte[] buffer = GetRandomBuffer(2 * 1024);

            CloudFile file = this.testShare.GetRootDirectoryReference().GetFileReference("file1");

            using (MemoryStream wholeFile = new MemoryStream(buffer))
            {
                await file.UploadFromStreamAsync(wholeFile);

                byte[]       testBuffer = new byte[1024];
                MemoryStream fileStream = new MemoryStream(testBuffer);
                Exception    ex         = await TestHelper.ExpectedExceptionAsync <Exception>(
                    async() => await file.DownloadRangeToStreamAsync(fileStream, 0, 0),
                    "Requesting 0 bytes when downloading range should not work");

                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
                await file.DownloadRangeToStreamAsync(fileStream, 0, 1024);

                Assert.AreEqual(fileStream.Position, 1024);
                TestHelper.AssertStreamsAreEqualAtIndex(fileStream, wholeFile, 0, 0, 1024);

                CloudFile    file2       = this.testShare.GetRootDirectoryReference().GetFileReference("file1");
                MemoryStream fileStream2 = new MemoryStream(testBuffer);
                ex = await TestHelper.ExpectedExceptionAsync <Exception>(
                    async() => await file2.DownloadRangeToStreamAsync(fileStream, 1024, 0),
                    "Requesting 0 bytes when downloading range should not work");

                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
                await file2.DownloadRangeToStreamAsync(fileStream2, 1024, 1024);

                TestHelper.AssertStreamsAreEqualAtIndex(fileStream2, wholeFile, 0, 1024, 1024);

                AssertAreEqual(file, file2);
            }
        }
Example #28
0
        public static void UploadTextTask(CloudFile file, string text, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
        {
            byte[] textAsBytes = encoding.GetBytes(text);
            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(textAsBytes, 0, textAsBytes.Length);

                stream.Seek(0, SeekOrigin.Begin);
                file.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;
                try
                {
                    file.UploadFromStreamAsync(stream, accessCondition, options, operationContext).Wait();
                }
                catch (AggregateException ex)
                {
                    if (ex.InnerException != null)
                    {
                        throw ex.InnerException;
                    }

                    throw;
                }
            }
        }
        /// <summary>
        /// Write copy progress
        /// </summary>
        /// <param name="file">CloudFile instance</param>
        /// <param name="progress">Progress record</param>
        internal void WriteCopyProgress(CloudFile file, ProgressRecord progress)
        {
            if (file.CopyState == null) return;
            long bytesCopied = file.CopyState.BytesCopied ?? 0;
            long totalBytes = file.CopyState.TotalBytes ?? 0;
            int percent = 0;

            if (totalBytes != 0)
            {
                percent = (int)(bytesCopied * 100 / totalBytes);
                progress.PercentComplete = percent;
            }

            string activity = String.Format(Resources.CopyFileStatus, file.CopyState.Status.ToString(), file.GetFullPath(), file.Share.Name, file.CopyState.Source.ToString());
            progress.Activity = activity;
            string message = String.Format(Resources.CopyPendingStatus, percent, file.CopyState.BytesCopied, file.CopyState.TotalBytes);
            progress.StatusDescription = message;
            OutputStream.WriteProgress(progress);
        }
 public Task<bool> FileExistsAsync(CloudFile file, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return file.ExistsAsync(options, operationContext, cancellationToken);
 }
Example #31
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileReadStream"/> class.
 /// </summary>
 /// <param name="file">File reference to read from.</param>
 /// <param name="accessCondition">An object that represents the access conditions for the file. If null, no condition is used.</param>
 /// <param name="options">An object that specifies additional options for the request.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 internal FileReadStream(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
     : base(file, accessCondition, options, operationContext)
 {
 }
 public virtual ICancellableAsyncResult BeginStartCopy(CloudFile source, AccessCondition sourceAccessCondition, AccessCondition destAccessCondition, BlobRequestOptions options, OperationContext operationContext, AsyncCallback callback, object state)
 {
     return this.BeginStartCopy(CloudFile.SourceFileToUri(source), sourceAccessCondition, destAccessCondition, options, operationContext, callback, state);
 }
 public virtual Task<string> StartCopyAsync(CloudFile source, CancellationToken cancellationToken)
 {
     return AsyncExtensions.TaskFromApm(this.BeginStartCopy, this.EndStartCopy, source, cancellationToken);
 }
        public async Task CloudFileListRangesAsync()
        {
            byte[]         buffer = GetRandomBuffer(1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                await file.CreateAsync(4 * 1024);

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    await file.WriteRangeAsync(memoryStream.AsInputStream(), 512, null);
                }

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    await file.WriteRangeAsync(memoryStream.AsInputStream(), 3 * 1024, null);
                }

                await file.ClearRangeAsync(1024, 1024);

                await file.ClearRangeAsync(0, 512);

                IEnumerable <FileRange> fileRanges = await file.ListRangesAsync();

                List <string> expectedFileRanges = new List <string>()
                {
                    new FileRange(512, 1023).ToString(),
                    new FileRange(3 * 1024, 4 * 1024 - 1).ToString(),
                };
                foreach (FileRange fileRange in fileRanges)
                {
                    Assert.IsTrue(expectedFileRanges.Remove(fileRange.ToString()));
                }
                Assert.AreEqual(0, expectedFileRanges.Count);

                fileRanges = await file.ListRangesAsync(1024, 1024, null, null, null);

                Assert.AreEqual(0, fileRanges.Count());

                fileRanges = await file.ListRangesAsync(512, 3 * 1024, null, null, null);

                expectedFileRanges = new List <string>()
                {
                    new FileRange(512, 1023).ToString(),
                    new FileRange(3 * 1024, 7 * 512 - 1).ToString(),
                };
                foreach (FileRange fileRange in fileRanges)
                {
                    Assert.IsTrue(expectedFileRanges.Remove(fileRange.ToString()));
                }
                Assert.AreEqual(0, expectedFileRanges.Count);

                OperationContext opContext = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await file.ListRangesAsync(1024, null, null, null, opContext),
                    opContext,
                    "List Ranges with an offset but no count should fail",
                    HttpStatusCode.Unused);

                Assert.IsInstanceOfType(opContext.LastResult.Exception.InnerException, typeof(ArgumentNullException));
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
 public virtual string StartCopy(CloudFile source, AccessCondition sourceAccessCondition = null, AccessCondition destAccessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     return this.StartCopy(CloudFile.SourceFileToUri(source), sourceAccessCondition, destAccessCondition, options, operationContext);
 }
Example #36
0
 /// <summary>
 /// Initializes a new instance of the FileWriteStream class for a file.
 /// </summary>
 /// <param name="file">File reference to write to.</param>
 /// <param name="fileSize">Size of the file.</param>
 /// <param name="createNew">Use <c>true</c> if the file is newly created, <c>false</c> otherwise.</param>
 /// <param name="accessCondition">An object that represents the access conditions for the file. If null, no condition is used.</param>
 /// <param name="options">An object that specifies additional options for the request.</param>
 internal FileWriteStream(CloudFile file, long fileSize, bool createNew, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
     : base(file, fileSize, createNew, accessCondition, options, operationContext)
 {
 }
 internal FileReadStream(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
     : base(file, accessCondition, options, operationContext)
 {
     throw new System.NotImplementedException();
 }
Example #38
0
        public void CloudFileSASSharedProtocolsQueryParam()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();
                CloudFile file;
                SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
                {
                    Permissions            = SharedAccessFilePermissions.Read,
                    SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                };

                CloudFile fileWithKey = share.GetRootDirectoryReference().GetFileReference("filefile");
                byte[]    data        = new byte[] { 0x1, 0x2, 0x3, 0x4 };
                byte[]    target      = new byte[4];
                fileWithKey.UploadFromByteArray(data, 0, 4);

                foreach (SharedAccessProtocol?protocol in new SharedAccessProtocol?[] { null, SharedAccessProtocol.HttpsOrHttp, SharedAccessProtocol.HttpsOnly })
                {
                    string             fileToken = fileWithKey.GetSharedAccessSignature(policy, null, null, protocol, null);
                    StorageCredentials fileSAS   = new StorageCredentials(fileToken);
                    Uri        fileSASUri        = new Uri(fileWithKey.Uri + fileSAS.SASToken);
                    StorageUri fileSASStorageUri = new StorageUri(new Uri(fileWithKey.StorageUri.PrimaryUri + fileSAS.SASToken), new Uri(fileWithKey.StorageUri.SecondaryUri + fileSAS.SASToken));

                    int securePort = 443;
                    int httpPort   = (fileSASUri.Port == securePort) ? 80 : fileSASUri.Port;

                    if (!string.IsNullOrEmpty(TestBase.TargetTenantConfig.FileSecurePortOverride))
                    {
                        securePort = Int32.Parse(TestBase.TargetTenantConfig.FileSecurePortOverride);
                    }

                    var schemesAndPorts = new[] {
                        new { scheme = Uri.UriSchemeHttp, port = httpPort },
                        new { scheme = Uri.UriSchemeHttps, port = securePort }
                    };

                    foreach (var item in schemesAndPorts)
                    {
                        fileSASUri        = TransformSchemeAndPort(fileSASUri, item.scheme, item.port);
                        fileSASStorageUri = new StorageUri(TransformSchemeAndPort(fileSASStorageUri.PrimaryUri, item.scheme, item.port), TransformSchemeAndPort(fileSASStorageUri.SecondaryUri, item.scheme, item.port));

                        if (protocol.HasValue && protocol == SharedAccessProtocol.HttpsOnly && string.CompareOrdinal(item.scheme, Uri.UriSchemeHttp) == 0)
                        {
                            file = new CloudFile(fileSASUri);
                            TestHelper.ExpectedException(() => file.FetchAttributes(), "Access a file using SAS with a shared protocols that does not match", HttpStatusCode.Unused);

                            file = new CloudFile(fileSASStorageUri, null);
                            TestHelper.ExpectedException(() => file.FetchAttributes(), "Access a file using SAS with a shared protocols that does not match", HttpStatusCode.Unused);
                        }
                        else
                        {
                            file = new CloudFile(fileSASUri);
                            file.DownloadRangeToByteArray(target, 0, 0, 4, null, null, null);
                            for (int i = 0; i < 4; i++)
                            {
                                Assert.AreEqual(data[i], target[i]);
                            }

                            file = new CloudFile(fileSASStorageUri, null);
                            file.DownloadRangeToByteArray(target, 0, 0, 4, null, null, null);
                            for (int i = 0; i < 4; i++)
                            {
                                Assert.AreEqual(data[i], target[i]);
                            }
                        }
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Example #39
0
        private static void TestAccess(string sasToken, SharedAccessFilePermissions permissions, SharedAccessFileHeaders headers, CloudFileShare share, CloudFile file)
        {
            CloudFileShare     SASshare = null;
            CloudFile          SASfile;
            StorageCredentials credentials = string.IsNullOrEmpty(sasToken) ?
                                             new StorageCredentials() :
                                             new StorageCredentials(sasToken);
            string fileText = "file";

            if (share != null)
            {
                SASshare = new CloudFileShare(credentials.TransformUri(share.Uri));
                SASfile  = SASshare.GetRootDirectoryReference().GetFileReference(file.Name);
            }
            else
            {
                SASfile = new CloudFile(credentials.TransformUri(file.Uri));
            }

            if ((permissions & SharedAccessFilePermissions.Write) == SharedAccessFilePermissions.Write)
            {
                UploadText(SASfile, fileText, Encoding.UTF8);
            }
            else if ((permissions & SharedAccessFilePermissions.Create) == SharedAccessFilePermissions.Create)
            {
                SASfile.Create(Encoding.UTF8.GetBytes(fileText).Length);
                TestHelper.ExpectedException(
                    () => UploadText(SASfile, fileText, Encoding.UTF8),
                    "UploadText SAS does not allow for writing",
                    HttpStatusCode.Forbidden);
                UploadText(file, fileText, Encoding.UTF8);
            }
            else
            {
                TestHelper.ExpectedException(
                    () => SASfile.Create(Encoding.UTF8.GetBytes(fileText).Length),
                    "Create file succeeded but SAS does not allow for writing/creating",
                    HttpStatusCode.Forbidden);
                TestHelper.ExpectedException(
                    () => UploadText(SASfile, fileText, Encoding.UTF8),
                    "UploadText SAS does not allow for writing/creating",
                    HttpStatusCode.Forbidden);
                UploadText(file, fileText, Encoding.UTF8);
            }

            if (SASshare != null)
            {
                if ((permissions & SharedAccessFilePermissions.List) == SharedAccessFilePermissions.List)
                {
                    SASshare.GetRootDirectoryReference().ListFilesAndDirectories().ToArray();
                }
                else
                {
                    TestHelper.ExpectedException(
                        () => SASshare.GetRootDirectoryReference().ListFilesAndDirectories().ToArray(),
                        "List files while SAS does not allow for listing",
                        HttpStatusCode.Forbidden);
                }
            }

            if ((permissions & SharedAccessFilePermissions.Read) == SharedAccessFilePermissions.Read)
            {
                SASfile.FetchAttributes();

                // Test headers
                if (headers != null)
                {
                    if (headers.CacheControl != null)
                    {
                        Assert.AreEqual(headers.CacheControl, SASfile.Properties.CacheControl);
                    }

                    if (headers.ContentDisposition != null)
                    {
                        Assert.AreEqual(headers.ContentDisposition, SASfile.Properties.ContentDisposition);
                    }

                    if (headers.ContentEncoding != null)
                    {
                        Assert.AreEqual(headers.ContentEncoding, SASfile.Properties.ContentEncoding);
                    }

                    if (headers.ContentLanguage != null)
                    {
                        Assert.AreEqual(headers.ContentLanguage, SASfile.Properties.ContentLanguage);
                    }

                    if (headers.ContentType != null)
                    {
                        Assert.AreEqual(headers.ContentType, SASfile.Properties.ContentType);
                    }
                }
            }
            else
            {
                TestHelper.ExpectedException(
                    () => SASfile.FetchAttributes(),
                    "Fetch file attributes while SAS does not allow for reading",
                    HttpStatusCode.Forbidden);
            }

            if ((permissions & SharedAccessFilePermissions.Write) == SharedAccessFilePermissions.Write)
            {
                SASfile.SetMetadata();
            }
            else
            {
                TestHelper.ExpectedException(
                    () => SASfile.SetMetadata(),
                    "Set file metadata while SAS does not allow for writing",
                    HttpStatusCode.Forbidden);
            }

            if ((permissions & SharedAccessFilePermissions.Delete) == SharedAccessFilePermissions.Delete)
            {
                SASfile.Delete();
            }
            else
            {
                TestHelper.ExpectedException(
                    () => SASfile.Delete(),
                    "Delete file while SAS does not allow for deleting",
                    HttpStatusCode.Forbidden);
            }
        }
Example #40
0
        public void CloudFileSASIPAddressHelper(Func <IPAddressOrRange> generateInitialIPAddressOrRange, Func <IPAddress, IPAddressOrRange> generateFinalIPAddressOrRange)
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();
                CloudFile file;
                SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
                {
                    Permissions            = SharedAccessFilePermissions.Read,
                    SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                };

                CloudFile fileWithKey = share.GetRootDirectoryReference().GetFileReference("filefile");
                byte[]    data        = new byte[] { 0x1, 0x2, 0x3, 0x4 };
                fileWithKey.UploadFromByteArray(data, 0, 4);

                // We need an IP address that will never be a valid source
                IPAddressOrRange   ipAddressOrRange = generateInitialIPAddressOrRange();
                string             fileToken        = fileWithKey.GetSharedAccessSignature(policy, null, null, null, ipAddressOrRange);
                StorageCredentials fileSAS          = new StorageCredentials(fileToken);
                Uri        fileSASUri        = fileSAS.TransformUri(fileWithKey.Uri);
                StorageUri fileSASStorageUri = fileSAS.TransformUri(fileWithKey.StorageUri);

                file = new CloudFile(fileSASUri);
                byte[]           target    = new byte[4];
                OperationContext opContext = new OperationContext();
                IPAddress        actualIP  = null;
                opContext.ResponseReceived += (sender, e) =>
                {
                    Stream stream = HttpResponseParsers.GetResponseStream(e.Response);
                    stream.Seek(0, SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string    text      = reader.ReadToEnd();
                        XDocument xdocument = XDocument.Parse(text);
                        actualIP = IPAddress.Parse(xdocument.Descendants("SourceIP").First().Value);
                    }
                };

                bool exceptionThrown = false;
                try
                {
                    file.DownloadRangeToByteArray(target, 0, 0, 4, null, null, opContext);
                }
                catch (StorageException)
                {
                    exceptionThrown = true;
                    //The IP should not be included in the error details for security reasons
                    Assert.IsNull(actualIP);
                }

                Assert.IsTrue(exceptionThrown);
                ipAddressOrRange  = null;
                fileToken         = fileWithKey.GetSharedAccessSignature(policy, null, null, null, ipAddressOrRange);
                fileSAS           = new StorageCredentials(fileToken);
                fileSASUri        = fileSAS.TransformUri(fileWithKey.Uri);
                fileSASStorageUri = fileSAS.TransformUri(fileWithKey.StorageUri);

                file = new CloudFile(fileSASUri);
                file.DownloadRangeToByteArray(target, 0, 0, 4, null, null, null);
                for (int i = 0; i < 4; i++)
                {
                    Assert.AreEqual(data[i], target[i]);
                }

                Assert.IsTrue(file.StorageUri.PrimaryUri.Equals(fileWithKey.Uri));
                Assert.IsNull(file.StorageUri.SecondaryUri);

                file = new CloudFile(fileSASStorageUri, null);
                file.DownloadRangeToByteArray(target, 0, 0, 4, null, null, null);
                for (int i = 0; i < 4; i++)
                {
                    Assert.AreEqual(data[i], target[i]);
                }

                Assert.IsTrue(file.StorageUri.Equals(fileWithKey.StorageUri));
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        private async Task DoDownloadRangeToByteArrayNegativeTestsAsync(CloudFile file)
        {
            int fileLength = 1024;
            int resultBufSize = 1024;
            byte[] buffer = GetRandomBuffer(fileLength);
            byte[] resultBuffer = new byte[resultBufSize];

            using (MemoryStream stream = new MemoryStream(buffer))
            {
                await file.UploadFromStreamAsync(stream);

                OperationContext context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 0, 1024, 1, null, null, context), context, "Try invalid length", HttpStatusCode.RequestedRangeNotSatisfiable);
                StorageException ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadToByteArrayAsync(resultBuffer, 1024), "Provide invalid offset");
                Assert.IsInstanceOfType(ex.InnerException, typeof(NotSupportedException));
                ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 1023, 0, 2), "Should fail when offset + length required is greater than size of the buffer");
                Assert.IsInstanceOfType(ex.InnerException, typeof(NotSupportedException));
                ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 0, 0, -10), "Fail when a negative length is specified");
                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
                await TestHelper.ExpectedExceptionAsync<ArgumentOutOfRangeException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, -10, 0, 20), "Fail if a negative offset is provided");
                ex = await TestHelper.ExpectedExceptionAsync<StorageException>(async () => await file.DownloadRangeToByteArrayAsync(resultBuffer, 0, -10, 20), "Fail if a negative file offset is provided");
                Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
            }
        }
 public virtual Task<string> StartCopyAsync(CloudFile source, AccessCondition sourceAccessCondition, AccessCondition destAccessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return AsyncExtensions.TaskFromApm(this.BeginStartCopy, this.EndStartCopy, source, sourceAccessCondition, destAccessCondition, options, operationContext, cancellationToken);
 }
Example #43
0
        public async Task FileWriteStreamBasicTestAsync()
        {
            byte[] buffer = GetRandomBuffer(6 * 512);

#if NETCORE
            MD5 hasher = MD5.Create();
#else
            CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();
#endif
            CloudFileShare share = GetRandomShareReference();
            share.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamWriteSizeInBytes = 8 * 512;

                using (MemoryStream wholeFile = new MemoryStream())
                {
                    FileRequestOptions options = new FileRequestOptions()
                    {
                        StoreFileContentMD5 = true,
                    };
                    using (var writeStream = await file.OpenWriteAsync(buffer.Length * 3, null, options, null))
                    {
                        Stream fileStream = writeStream;

                        for (int i = 0; i < 3; i++)
                        {
                            await fileStream.WriteAsync(buffer, 0, buffer.Length);

                            await wholeFile.WriteAsync(buffer, 0, buffer.Length);

                            Assert.AreEqual(wholeFile.Position, fileStream.Position);
#if !NETCORE
                            hasher.Append(buffer.AsBuffer());
#endif
                        }

                        await fileStream.FlushAsync();
                    }

#if NETCORE
                    string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeFile.ToArray()));
#else
                    string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());
#endif
                    await file.FetchAttributesAsync();

                    Assert.AreEqual(md5, file.Properties.ContentMD5);

                    using (MemoryOutputStream downloadedFile = new MemoryOutputStream())
                    {
                        await file.DownloadToStreamAsync(downloadedFile);

                        TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile.UnderlyingStream);
                    }

                    await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                        async() => await file.OpenWriteAsync(null, null, options, null),
                        "OpenWrite with StoreFileContentMD5 on an existing file should fail");

                    using (var writeStream = await file.OpenWriteAsync(null))
                    {
                        Stream fileStream = writeStream;
                        fileStream.Seek(buffer.Length / 2, SeekOrigin.Begin);
                        wholeFile.Seek(buffer.Length / 2, SeekOrigin.Begin);

                        for (int i = 0; i < 2; i++)
                        {
                            fileStream.Write(buffer, 0, buffer.Length);
                            wholeFile.Write(buffer, 0, buffer.Length);
                            Assert.AreEqual(wholeFile.Position, fileStream.Position);
                        }

                        await fileStream.FlushAsync();
                    }

                    await file.FetchAttributesAsync();

                    Assert.AreEqual(md5, file.Properties.ContentMD5);

                    using (MemoryOutputStream downloadedFile = new MemoryOutputStream())
                    {
                        options.DisableContentMD5Validation = true;
                        await file.DownloadToStreamAsync(downloadedFile, null, options, null);

                        TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile.UnderlyingStream);
                    }
                }
            }
            finally
            {
                share.DeleteAsync().Wait();
            }
        }
Example #44
0
        public async Task CloudFileWriteRangeAsync()
        {
            byte[] buffer = GetRandomBuffer(4 * 1024 * 1024);

            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                await file.CreateAsync(4 * 1024 * 1024);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    await TestHelper.ExpectedExceptionAsync <ArgumentOutOfRangeException>(
                        async() => await file.WriteRangeAsync(memoryStream, 0, null),
                        "Zero-length WriteRange should fail");
                }

                using (MemoryStream resultingData = new MemoryStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream(buffer))
                    {
                        OperationContext opContext = new OperationContext();
                        await TestHelper.ExpectedExceptionAsync(
                            async() => await file.WriteRangeAsync(memoryStream, 512, null, null, null, opContext),
                            opContext,
                            "Writing out-of-range ranges should fail",
                            HttpStatusCode.RequestedRangeNotSatisfiable,
                            "InvalidRange");

                        memoryStream.Seek(0, SeekOrigin.Begin);
                        await file.WriteRangeAsync(memoryStream, 0, null);

                        resultingData.Write(buffer, 0, buffer.Length);

                        int offset = buffer.Length - 1024;
                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        await file.WriteRangeAsync(memoryStream, 0, null);

                        resultingData.Seek(0, SeekOrigin.Begin);
                        resultingData.Write(buffer, offset, buffer.Length - offset);

                        offset = buffer.Length - 2048;
                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        await file.WriteRangeAsync(memoryStream, 1024, null);

                        resultingData.Seek(1024, SeekOrigin.Begin);
                        resultingData.Write(buffer, offset, buffer.Length - offset);
                    }

                    using (MemoryStream fileData = new MemoryStream())
                    {
                        await file.DownloadToStreamAsync(fileData);

                        Assert.AreEqual(resultingData.Length, fileData.Length);

                        Assert.IsTrue(fileData.ToArray().SequenceEqual(resultingData.ToArray()));
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Example #45
0
        public async Task FileWriteStreamFlushTestAsync()
        {
            byte[] buffer = GetRandomBuffer(512);

            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamWriteSizeInBytes = 1024;
                using (MemoryStream wholeFile = new MemoryStream())
                {
                    FileRequestOptions options = new FileRequestOptions()
                    {
                        StoreFileContentMD5 = true
                    };
                    OperationContext opContext = new OperationContext();
                    using (var fileStream = await file.OpenWriteAsync(4 * 512, null, options, opContext))
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            await fileStream.WriteAsync(buffer, 0, buffer.Length);

                            await wholeFile.WriteAsync(buffer, 0, buffer.Length);
                        }

#if NETCORE
                        // todo: Make some other better logic for this test to be reliable.
                        System.Threading.Thread.Sleep(500);
#endif

                        Assert.AreEqual(2, opContext.RequestResults.Count);

                        await fileStream.FlushAsync();

                        Assert.AreEqual(3, opContext.RequestResults.Count);

                        await fileStream.FlushAsync();

                        Assert.AreEqual(3, opContext.RequestResults.Count);

                        await fileStream.WriteAsync(buffer, 0, buffer.Length);

                        await wholeFile.WriteAsync(buffer, 0, buffer.Length);

                        Assert.AreEqual(3, opContext.RequestResults.Count);

                        await fileStream.CommitAsync();

                        Assert.AreEqual(5, opContext.RequestResults.Count);
                    }

                    Assert.AreEqual(5, opContext.RequestResults.Count);

                    using (MemoryOutputStream downloadedFile = new MemoryOutputStream())
                    {
                        await file.DownloadToStreamAsync(downloadedFile);

                        TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile.UnderlyingStream);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Example #46
0
        public void CloudFileClientMaximumExecutionTimeoutShouldNotBeHonoredForStreams()
        {
            CloudFileClient fileClient = GenerateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference(Guid.NewGuid().ToString("N"));

            byte[] buffer = FileTestBase.GetRandomBuffer(1024 * 1024);

            try
            {
                share.Create();

                fileClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30);
                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file");
                file.StreamWriteSizeInBytes       = 1024 * 1024;
                file.StreamMinimumReadSizeInBytes = 1024 * 1024;

                using (CloudFileStream bos = file.OpenWrite(8 * 1024 * 1024))
                {
                    DateTime start = DateTime.Now;

                    for (int i = 0; i < 7; i++)
                    {
                        bos.Write(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        Thread.Sleep(msRemaining);
                    }

                    bos.Write(buffer, 0, buffer.Length);
                }

                using (Stream bis = file.OpenRead())
                {
                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += bis.Read(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        Thread.Sleep(msRemaining);
                    }

                    while (true)
                    {
                        int count = bis.Read(buffer, 0, buffer.Length);
                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                fileClient.DefaultRequestOptions.MaximumExecutionTime = null;
                share.DeleteIfExists();
            }
        }
        public async Task CloudFileWriteRangeAsync()
        {
            byte[]            buffer = GetRandomBuffer(4 * 1024 * 1024);
            CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();

            hasher.Append(buffer.AsBuffer());
            string contentMD5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());

            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                await file.CreateAsync(4 * 1024 * 1024);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    await TestHelper.ExpectedExceptionAsync <ArgumentOutOfRangeException>(
                        async() => await file.WriteRangeAsync(memoryStream.AsInputStream(), 0, null),
                        "Zero-length WriteRange should fail");

                    memoryStream.SetLength(4 * 1024 * 1024 + 1);
                    await TestHelper.ExpectedExceptionAsync <ArgumentOutOfRangeException>(
                        async() => await file.WriteRangeAsync(memoryStream.AsInputStream(), 0, null),
                        ">4MB WriteRange should fail");
                }

                using (MemoryStream resultingData = new MemoryStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream(buffer))
                    {
                        OperationContext opContext = new OperationContext();
                        await TestHelper.ExpectedExceptionAsync(
                            async() => await file.WriteRangeAsync(memoryStream.AsInputStream(), 512, null, null, null, opContext),
                            opContext,
                            "Writing out-of-range ranges should fail",
                            HttpStatusCode.RequestedRangeNotSatisfiable,
                            "InvalidFileRange");

                        memoryStream.Seek(0, SeekOrigin.Begin);
                        await file.WriteRangeAsync(memoryStream.AsInputStream(), 0, contentMD5);

                        resultingData.Write(buffer, 0, buffer.Length);

                        int offset = buffer.Length - 1024;
                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        await TestHelper.ExpectedExceptionAsync(
                            async() => await file.WriteRangeAsync(memoryStream.AsInputStream(), 0, contentMD5, null, null, opContext),
                            opContext,
                            "Invalid MD5 should fail with mismatch",
                            HttpStatusCode.BadRequest,
                            "Md5Mismatch");

                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        await file.WriteRangeAsync(memoryStream.AsInputStream(), 0, null);

                        resultingData.Seek(0, SeekOrigin.Begin);
                        resultingData.Write(buffer, offset, buffer.Length - offset);

                        offset = buffer.Length - 2048;
                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        await file.WriteRangeAsync(memoryStream.AsInputStream(), 1024, null);

                        resultingData.Seek(1024, SeekOrigin.Begin);
                        resultingData.Write(buffer, offset, buffer.Length - offset);
                    }

                    using (MemoryStream fileData = new MemoryStream())
                    {
                        await file.DownloadToStreamAsync(fileData.AsOutputStream());

                        Assert.AreEqual(resultingData.Length, fileData.Length);

                        Assert.IsTrue(fileData.ToArray().SequenceEqual(resultingData.ToArray()));
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudFileInvalidApisInShareSnapshotAsync()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);
            await share.CreateAsync();

            CloudFileShare snapshot = share.SnapshotAsync().Result;
            CloudFile      file     = snapshot.GetRootDirectoryReference().GetDirectoryReference("dir1").GetFileReference("file");

            try
            {
                await file.CreateAsync(1024);

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                await file.DeleteAsync();

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                await file.SetMetadataAsync();

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                await file.AbortCopyAsync(null);

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                await file.ClearRangeAsync(0, 1024);

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                await file.StartCopyAsync(file);

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }
            try
            {
                await file.UploadFromByteArrayAsync(new byte[1024], 0, 1024);

                Assert.Fail("API should fail in a snapshot");
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(SR.CannotModifyShareSnapshot, e.Message);
            }

            await snapshot.DeleteAsync();

            await share.DeleteAsync();
        }
 public virtual ICancellableAsyncResult BeginStartCopy(CloudFile source, AsyncCallback callback, object state)
 {
     return this.BeginStartCopy(CloudFile.SourceFileToUri(source), callback, state);
 }
Example #50
0
        public async Task CloudFileSetPropertiesAsync()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                await file.CreateAsync(1024);

                string         eTag         = file.Properties.ETag;
                DateTimeOffset lastModified = file.Properties.LastModified.Value;

                await Task.Delay(1000);

                file.Properties.CacheControl = "no-transform";
#if !ASPNET_K
                file.Properties.ContentEncoding = "gzip";
#endif
                file.Properties.ContentLanguage = "tr,en";
                file.Properties.ContentMD5      = "MDAwMDAwMDA=";
                file.Properties.ContentType     = "text/html";
                await file.SetPropertiesAsync();

                Assert.IsTrue(file.Properties.LastModified > lastModified);
                Assert.AreNotEqual(eTag, file.Properties.ETag);

                CloudFile file2 = share.GetRootDirectoryReference().GetFileReference("file1");
                await file2.FetchAttributesAsync();

                Assert.AreEqual("no-transform", file2.Properties.CacheControl);
#if !ASPNET_K
                Assert.AreEqual("gzip", file2.Properties.ContentEncoding);
#endif
                Assert.AreEqual("tr,en", file2.Properties.ContentLanguage);
                Assert.AreEqual("MDAwMDAwMDA=", file2.Properties.ContentMD5);
                Assert.AreEqual("text/html", file2.Properties.ContentType);

                CloudFile file3 = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream stream = new MemoryStream())
                {
                    FileRequestOptions options = new FileRequestOptions()
                    {
                        DisableContentMD5Validation = true,
                    };
                    await file3.DownloadToStreamAsync(stream, null, options, null);
                }
                AssertAreEqual(file2.Properties, file3.Properties);

                CloudFileDirectory          rootDirectory = share.GetRootDirectoryReference();
                IEnumerable <IListFileItem> results       = await ListFilesAndDirectoriesAsync(rootDirectory, null, null, null);

                CloudFile file4 = (CloudFile)results.First();
                Assert.AreEqual(file2.Properties.Length, file4.Properties.Length);
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
 public virtual Task<string> StartCopyAsync(CloudFile source)
 {
     return this.StartCopyAsync(source, CancellationToken.None);
 }
 /// <summary>
 /// Initializes a new instance of the FileWriteStream class for a file.
 /// </summary>
 /// <param name="file">File reference to write to.</param>
 /// <param name="fileSize">Size of the file.</param>
 /// <param name="createNew">Use <c>true</c> if the file is newly created, <c>false</c> otherwise.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the file. If <c>null</c>, no condition is used.</param>
 /// <param name="options">An <see cref="FileRequestOptions"/> object that specifies additional options for the request.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
 internal FileWriteStream(CloudFile file, long fileSize, bool createNew, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
     : base(file, fileSize, createNew, accessCondition, options, operationContext)
 {
 }
 public virtual Task<string> StartCopyAsync(CloudFile source, AccessCondition sourceAccessCondition, AccessCondition destAccessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return this.StartCopyAsync(source, sourceAccessCondition, destAccessCondition, options, operationContext, CancellationToken.None);
 }
 public Task<bool> FileExistsAsync(CloudFile file, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return TaskEx.FromResult(true);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FileReadStream"/> class.
 /// </summary>
 /// <param name="file">File reference to read from.</param>
 /// <param name="accessCondition">An object that represents the access conditions for the file. If null, no condition is used.</param>
 /// <param name="options">An object that specifies additional options for the request.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 internal FileReadStream(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext)
     : base(file, accessCondition, options, operationContext)
 {
 }
        public void FileUseTransactionalMD5PutTestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.SendingRequest += (_, args) =>
            {
                if (HttpRequestParsers.GetContentLength(args.Request) >= buffer.Length)
                {
                    lastCheckMD5 = HttpRequestParsers.GetContentMD5(args.Request);
                    checkCount++;
                }
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file2");
                    file.Create(buffer.Length);
                    checkCount = 0;
                    using (Stream fileData = new MemoryStream(buffer))
                    {
                        result = file.BeginWriteRange(fileData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check,
                                                      ar => waitHandle.Set(),
                                                      null);
                        waitHandle.WaitOne();
                        file.EndWriteRange(result);
                        Assert.IsNull(lastCheckMD5);

                        fileData.Seek(0, SeekOrigin.Begin);
                        result = file.BeginWriteRange(fileData, 0, null, null, optionsWithMD5, opContextWithMD5Check,
                                                      ar => waitHandle.Set(),
                                                      null);
                        waitHandle.WaitOne();
                        file.EndWriteRange(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        fileData.Seek(0, SeekOrigin.Begin);
                        result = file.BeginWriteRange(fileData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check,
                                                      ar => waitHandle.Set(),
                                                      null);
                        waitHandle.WaitOne();
                        file.EndWriteRange(result);
                        Assert.AreEqual(md5, lastCheckMD5);
                    }
                    Assert.AreEqual(3, checkCount);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
 public Task DeleteFileAsync(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return TaskEx.FromResult(true);
 }
        public void FileUseTransactionalMD5GetTestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(3 * 1024 * 1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.ResponseReceived += (_, args) =>
            {
                if (long.Parse(HttpResponseParsers.GetContentLength(args.Response)) >= buffer.Length)
                {
                    lastCheckMD5 = HttpResponseParsers.GetContentMD5(args.Response);
                    checkCount++;
                }
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file2");
                    using (Stream fileStream = file.OpenWrite(buffer.Length * 2))
                    {
                        fileStream.Write(buffer, 0, buffer.Length);
                        fileStream.Write(buffer, 0, buffer.Length);
                    }

                    checkCount = 0;
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = file.BeginDownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                            () => file.EndDownloadRangeToStream(result),
                            "File will not have MD5 set by default; with UseTransactional, download should fail");

                        result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        storageEx = TestHelper.ExpectedException <StorageException>(
                            () => file.EndDownloadRangeToStream(result),
                            "Downloading more than 4MB with transactional MD5 should not be supported");
                        Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));

                        result = file.BeginOpenRead(null, optionsWithMD5, opContextWithMD5Check,
                                                    ar => waitHandle.Set(),
                                                    null);
                        waitHandle.WaitOne();
                        using (Stream fileStream = file.EndOpenRead(result))
                        {
                            fileStream.CopyTo(stream);
                            Assert.IsNotNull(lastCheckMD5);
                        }

                        result = file.BeginOpenRead(null, optionsWithNoMD5, opContextWithMD5Check,
                                                    ar => waitHandle.Set(),
                                                    null);
                        waitHandle.WaitOne();
                        using (Stream fileStream = file.EndOpenRead(result))
                        {
                            fileStream.CopyTo(stream);
                            Assert.IsNull(lastCheckMD5);
                        }
                    }
                    Assert.AreEqual(9, checkCount);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
 public Task DeleteFileAsync(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return file.DeleteAsync(accessCondition, options, operationContext, cancellationToken);
 }
        /// <summary>
        /// Single put file and get file.
        /// </summary>
        /// <param name="fileSize">The file size.</param>
        /// <param name="bufferSize">The output buffer size.</param>
        /// <param name="bufferOffset">The output buffer offset.</param>
        /// <param name="fileOffset">The file offset.</param>
        /// <param name="length">Length of the data range to download.</param>
        /// <param name="isOverload">True when the overloaded method for DownloadRangeToByteArrayAsync is called. False when the basic method is called.</param>
        private async Task DoDownloadRangeToByteArrayAsyncTest(CloudFile file, int fileSize, int bufferSize, int bufferOffset, long? fileOffset, long? length, bool isOverload)
        {
            int downloadLength;
            byte[] buffer = GetRandomBuffer(fileSize);
            byte[] resultBuffer = new byte[bufferSize];
            byte[] resultBuffer2 = new byte[bufferSize];

            using (MemoryStream originalFile = new MemoryStream(buffer))
            {
                if (!isOverload)
                {
                    await file.UploadFromStreamAsync(originalFile);
                    downloadLength = await file.DownloadRangeToByteArrayAsync(resultBuffer, bufferOffset, fileOffset, length);
                }
                else
                {
                    await file.UploadFromStreamAsync(originalFile);
                    OperationContext context = new OperationContext();
                    downloadLength = await file.DownloadRangeToByteArrayAsync(resultBuffer, bufferOffset, fileOffset, length, null, null, context);
                }

                int downloadSize = Math.Min(fileSize - (int)(fileOffset.HasValue ? fileOffset.Value : 0), bufferSize - bufferOffset);
                if (length.HasValue && (length.Value < downloadSize))
                {
                    downloadSize = (int)length.Value;
                }

                Assert.AreEqual(downloadSize, downloadLength);

                for (int i = 0; i < bufferOffset; i++)
                {
                    Assert.AreEqual(0, resultBuffer[i]);
                }

                for (int j = 0; j < downloadLength; j++)
                {
                    Assert.AreEqual(buffer[(fileOffset.HasValue ? fileOffset.Value : 0) + j], resultBuffer[bufferOffset + j]);
                }

                for (int k = bufferOffset + downloadLength; k < bufferSize; k++)
                {
                    Assert.AreEqual(0, resultBuffer[k]);
                }
            }
        }