public async Task FileStoreContentMD5TestAsync() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { StoreFileContentMD5 = false, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { StoreFileContentMD5 = true, }; CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file4"); using (Stream stream = new MemoryStream()) { await file.UploadFromStreamAsync(stream, null, optionsWithMD5, null); } await file.FetchAttributesAsync(); Assert.IsNotNull(file.Properties.ContentMD5); file = share.GetRootDirectoryReference().GetFileReference("file5"); using (Stream stream = new MemoryStream()) { await file.UploadFromStreamAsync(stream, null, optionsWithNoMD5, null); } await file.FetchAttributesAsync(); Assert.IsNull(file.Properties.ContentMD5); file = share.GetRootDirectoryReference().GetFileReference("file6"); using (Stream stream = new MemoryStream()) { await file.UploadFromStreamAsync(stream); } await file.FetchAttributesAsync(); Assert.IsNull(file.Properties.ContentMD5); } finally { share.DeleteIfExistsAsync().Wait(); } }
public async Task TestFileAttributesEncryptionAsync() { CloudFileShare share = GetRandomShareReference(); try { await share.CreateIfNotExistsAsync(); CloudFileDirectory directory = share.GetRootDirectoryReference(); CloudFile file = directory.GetFileReference("file"); await file.UploadTextAsync("test"); await file.FetchAttributesAsync(); Assert.IsTrue(file.Properties.IsServerEncrypted); CloudFile testFile = directory.GetFileReference(file.Name); await testFile.DownloadTextAsync(); Assert.IsTrue(testFile.Properties.IsServerEncrypted); } finally { share.DeleteIfExistsAsync().Wait(); } }
public async Task FileWriteStreamOpenAndCloseAsync() { CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file"); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await file.OpenWriteAsync(null, null, null, opContext), opContext, "Opening a file stream with no size should fail on a file that does not exist", HttpStatusCode.NotFound); using (CloudFileStream writeStream = await file.OpenWriteAsync(1024)) { } using (CloudFileStream writeStream = await file.OpenWriteAsync(null)) { } CloudFile file2 = share.GetRootDirectoryReference().GetFileReference("file"); await file2.FetchAttributesAsync(); Assert.AreEqual(1024, file2.Properties.Length); } finally { share.DeleteAsync().Wait(); } }
public void CloudFileCopyTestTask() { CloudFileShare share = GetRandomShareReference(); try { share.CreateAsync().Wait(); CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; UploadTextTask(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadataAsync().Wait(); CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy"); string copyId = copy.StartCopyAsync(TestHelper.Defiddler(source)).Result; WaitForCopyTask(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); TestHelper.ExpectedExceptionTask( copy.AbortCopyAsync(copyId), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); source.FetchAttributesAsync().Wait(); Assert.IsNotNull(copy.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag); Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = DownloadTextTask(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of file not similar"); copy.FetchAttributesAsync().Wait(); FileProperties prop1 = copy.Properties; FileProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same"); copy.DeleteAsync().Wait(); } finally { share.DeleteIfExistsAsync().Wait(); } }
public async Task CloudFileCopyTestWithMetadataOverrideAsync() { CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy"); copy.Metadata["Test2"] = "value2"; string copyId = await copy.StartCopyAsync(TestHelper.Defiddler(source)); await WaitForCopyAsync(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = await DownloadTextAsync(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of file not similar"); await copy.FetchAttributesAsync(); await source.FetchAttributesAsync(); FileProperties prop1 = copy.Properties; FileProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value2", copy.Metadata["Test2"], false, "Copied metadata not same"); Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination file"); await copy.DeleteAsync(); } finally { await share.DeleteAsync(); } }
public static void WaitForCopyTask(CloudFile file) { bool copyInProgress = true; while (copyInProgress) { Thread.Sleep(1000); file.FetchAttributesAsync().Wait(); copyInProgress = (file.CopyState.Status == CopyStatus.Pending); } }
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); } }
public async Task FileWriteStreamRandomSeekTestAsync() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CloudFileShare share = GetRandomShareReference(); share.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; try { await share.CreateAsync(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); using (MemoryStream wholeFile = new MemoryStream()) { using (CloudFileStream writeStream = await file.OpenWriteAsync(buffer.Length)) { Stream fileStream = writeStream; await fileStream.WriteAsync(buffer, 0, buffer.Length); await wholeFile.WriteAsync(buffer, 0, buffer.Length); Random random = new Random(); for (int i = 0; i < 10; i++) { int offset = random.Next(buffer.Length); TestHelper.SeekRandomly(fileStream, offset); await fileStream.WriteAsync(buffer, 0, buffer.Length - offset); wholeFile.Seek(offset, SeekOrigin.Begin); await wholeFile.WriteAsync(buffer, 0, buffer.Length - offset); } } wholeFile.Seek(0, SeekOrigin.End); await file.FetchAttributesAsync(); Assert.IsNull(file.Properties.ContentMD5); using (MemoryOutputStream downloadedFile = new MemoryOutputStream()) { await file.DownloadToStreamAsync(downloadedFile); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile.UnderlyingStream); } } } finally { share.DeleteIfExistsAsync().Wait(); } }
public async Task FileUploadWithoutMD5ValidationAndStoreFileContentTestAsync() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); FileRequestOptions options = new FileRequestOptions(); options.DisableContentMD5Validation = false; options.StoreFileContentMD5 = false; OperationContext context = new OperationContext(); using (MemoryStream srcStream = new MemoryStream(buffer)) { await file.UploadFromStreamAsync(srcStream, null, options, context); await file.FetchAttributesAsync(); string md5 = file.Properties.ContentMD5; file.Properties.ContentMD5 = "MDAwMDAwMDA="; await file.SetPropertiesAsync(null, options, context); byte[] testBuffer = new byte[2048]; MemoryStream dstStream = new MemoryStream(testBuffer); await TestHelper.ExpectedExceptionAsync(async() => await file.DownloadRangeToStreamAsync(dstStream, null, null, null, options, context), context, "Try to Download a stream with a corrupted md5 and DisableMD5Validation set to false", HttpStatusCode.OK); options.DisableContentMD5Validation = true; await file.SetPropertiesAsync(null, options, context); byte[] testBuffer2 = new byte[2048]; MemoryStream dstStream2 = new MemoryStream(testBuffer2); await file.DownloadRangeToStreamAsync(dstStream2, null, null, null, options, context); } } finally { share.DeleteIfExistsAsync().Wait(); } }
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 (CloudFileStream 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 (CloudFileStream 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(); } }
public async Task CloudFileSASSharedProtocolsQueryParamAsync() { CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); 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]; await fileWithKey.UploadFromByteArrayAsync(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 = "HTTP", port = httpPort }, new { scheme = "HTTPS", 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, "HTTP") == 0) { file = new CloudFile(fileSASUri); OperationContext context = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await file.FetchAttributesAsync(null /* accessCondition */, null /* options */, context), context, "Access a file using SAS with a shared protocols that does not match", HttpStatusCode.Unused, ""); file = new CloudFile(fileSASStorageUri, null); context = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await file.FetchAttributesAsync(null /* accessCondition */, null /* options */, context), context, "Access a file using SAS with a shared protocols that does not match", HttpStatusCode.Unused, ""); } else { file = new CloudFile(fileSASUri); await file.DownloadRangeToByteArrayAsync(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); await file.DownloadRangeToByteArrayAsync(target, 0, 0, 4, null, null, null); for (int i = 0; i < 4; i++) { Assert.AreEqual(data[i], target[i]); } } } } } finally { share.DeleteIfExistsAsync().Wait(); } }
private static async Task CloudFileCopyAsync(bool sourceIsSas, bool destinationIsSas) { CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); // Create Source on server CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); // Create Destination on server CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination"); await destination.CreateAsync(1); CloudFile copySource = source; CloudFile copyDestination = destination; if (sourceIsSas) { // Source SAS must have read permissions SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Read; SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = source.GetSharedAccessSignature(policy); // Get source StorageCredentials credentials = new StorageCredentials(sasToken); copySource = new CloudFile(credentials.TransformUri(source.Uri)); } if (destinationIsSas) { Assert.IsTrue(sourceIsSas); // Destination SAS must have write permissions SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Write; SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = destination.GetSharedAccessSignature(policy); // Get destination StorageCredentials credentials = new StorageCredentials(sasToken); copyDestination = new CloudFile(credentials.TransformUri(destination.Uri)); } // Start copy and wait for completion string copyId = await copyDestination.StartCopyAsync(TestHelper.Defiddler(copySource)); await WaitForCopyAsync(destination); // Check original file references for equality Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, destination.CopyState.TotalBytes); Assert.AreEqual(data.Length, destination.CopyState.BytesCopied); Assert.AreEqual(copyId, destination.CopyState.CopyId); Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); if (!destinationIsSas) { // Abort Copy is not supported for SAS destination OperationContext context = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await copyDestination.AbortCopyAsync(copyId, null, null, context), context, "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); } await source.FetchAttributesAsync(); Assert.IsNotNull(destination.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag); Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = await DownloadTextAsync(destination, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of file not equal."); await destination.FetchAttributesAsync(); FileProperties prop1 = destination.Properties; FileProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same"); await destination.DeleteAsync(); await source.DeleteAsync(); } finally { await share.DeleteAsync(); } }