Ejemplo n.º 1
0
        public void FileOpenReadTest()
        {
            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 dstStream = file.OpenRead();
                    TestHelper.AssertStreamsAreEqual(srcStream, dstStream);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 2
0
        public void FileOpenReadWriteTest()
        {
            byte[]         buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();
                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");

                Stream fileStream = file.OpenWrite(2048);
                fileStream.Write(buffer, 0, 2048);
                fileStream.Close();

                MemoryStream memoryStream = new MemoryStream(buffer);
                Stream       dstStream    = file.OpenRead();
                TestHelper.AssertStreamsAreEqual(memoryStream, dstStream);
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        public async Task CloudFileDirectoryApisInShareSnapshotAsync()
        {
            CloudFileShare share = GetRandomShareReference();
            await share.CreateAsync();

            CloudFileDirectory dir = share.GetRootDirectoryReference().GetDirectoryReference("dir1");
            await dir.CreateAsync();

            dir.Metadata["key1"] = "value1";
            await dir.SetMetadataAsync(null, null, null);

            CloudFileShare snapshot = await share.SnapshotAsync();

            CloudFileDirectory snapshotDir = snapshot.GetRootDirectoryReference().GetDirectoryReference("dir1");

            dir.Metadata["key2"] = "value2";
            await dir.SetMetadataAsync(null, null, null);

            await snapshotDir.FetchAttributesAsync();

            Assert.IsTrue(snapshotDir.Metadata.Count == 1 && snapshotDir.Metadata["key1"].Equals("value1"));
            // Metadata keys should be case-insensitive
            Assert.IsTrue(snapshotDir.Metadata["KEY1"].Equals("value1"));
            Assert.IsNotNull(snapshotDir.Properties.ETag);

            await dir.FetchAttributesAsync();

            Assert.IsTrue(dir.Metadata.Count == 2 && dir.Metadata["key2"].Equals("value2"));
            // Metadata keys should be case-insensitive
            Assert.IsTrue(dir.Metadata["KEY2"].Equals("value2"));
            Assert.IsNotNull(dir.Properties.ETag);
            Assert.AreNotEqual(dir.Properties.ETag, snapshotDir.Properties.ETag);

            await snapshot.DeleteAsync();

            await share.DeleteAsync();
        }
Ejemplo n.º 4
0
        public void CopyFileUsingUnicodeFileName()
        {
            string _unicodeFileName    = "繁体字14a6c";
            string _nonUnicodeFileName = "sample_file";

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();
                CloudFile fileUnicodeSource = share.GetRootDirectoryReference().GetFileReference(_unicodeFileName);
                string    data = "Test content";
                UploadText(fileUnicodeSource, data, Encoding.UTF8);
                CloudFile fileAsciiSource = share.GetRootDirectoryReference().GetFileReference(_nonUnicodeFileName);
                UploadText(fileAsciiSource, data, Encoding.UTF8);

                //Copy files over
                CloudFile fileAsciiDest = share.GetRootDirectoryReference().GetFileReference(_nonUnicodeFileName + "_copy");
                string    copyId        = fileAsciiDest.StartCopy(TestHelper.Defiddler(fileAsciiSource));
                WaitForCopy(fileAsciiDest);

                CloudFile fileUnicodeDest = share.GetRootDirectoryReference().GetFileReference(_unicodeFileName + "_copy");
                copyId = fileUnicodeDest.StartCopy(TestHelper.Defiddler(fileUnicodeSource));
                WaitForCopy(fileUnicodeDest);

                Assert.AreEqual(CopyStatus.Success, fileUnicodeDest.CopyState.Status);
                Assert.AreEqual(fileUnicodeSource.Uri.AbsolutePath, fileUnicodeDest.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, fileUnicodeDest.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, fileUnicodeDest.CopyState.BytesCopied);
                Assert.AreEqual(copyId, fileUnicodeDest.CopyState.CopyId);
                Assert.IsTrue(fileUnicodeDest.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 5
0
        public async Task FileWriteWhenOpenReadAsync()
        {
            byte[]         buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(srcStream);

                    bool   thrown     = false;
                    byte[] testBuffer = new byte[2048];
                    using (Stream fileStream = await file.OpenReadAsync())
                    {
                        Stream fileStreamForRead = fileStream;
                        try
                        {
                            await fileStreamForRead.WriteAsync(testBuffer, 0, 2048);
                        }
                        catch (NotSupportedException)
                        {
                            thrown = true;
                        }

                        Assert.IsTrue(thrown);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Ejemplo n.º 6
0
        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();
            }
        }
Ejemplo n.º 7
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;
            }
        }
Ejemplo n.º 8
0
        public void FileStoreContentMD5TestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                StoreFileContentMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                StoreFileContentMD5 = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file4");
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginUploadFromStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);
                    }
                    file.FetchAttributes();
                    Assert.IsNotNull(file.Properties.ContentMD5);

                    file = share.GetRootDirectoryReference().GetFileReference("file5");
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginUploadFromStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);
                    }
                    file.FetchAttributes();
                    Assert.IsNull(file.Properties.ContentMD5);

                    file = share.GetRootDirectoryReference().GetFileReference("file6");
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginUploadFromStream(stream,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);
                    }
                    file.FetchAttributes();
                    Assert.IsNull(file.Properties.ContentMD5);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        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 (CloudFileStream 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
                        Task.Delay(500).GetAwaiter().GetResult();

                        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();
            }
        }
Ejemplo n.º 10
0
 public void TestInitialize()
 {
     this.testShare = GetRandomShareReference();
     this.testShare.CreateIfNotExistsAsync().Wait();
 }
Ejemplo n.º 11
0
        public async Task CloudFileCopyTestTask()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadTextTask(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                await source.SetMetadataAsync();

                CloudFile copy   = share.GetRootDirectoryReference().GetFileReference("copy");
                string    copyId = await copy.StartCopyAsync(TestHelper.Defiddler(source));

                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");

                await source.FetchAttributesAsync();

                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");

                await copy.FetchAttributesAsync();

                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");

                await copy.DeleteAsync();
            }
            finally
            {
                await share.DeleteAsync();
            }
        }
        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();
            }
        }
Ejemplo n.º 13
0
        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 void FileReadLockToETagTestAPM()
        {
            byte[]         outBuffer = new byte[1 * 1024 * 1024];
            byte[]         buffer    = GetRandomBuffer(2 * outBuffer.Length);
            CloudFileShare share     = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamMinimumReadSizeInBytes = outBuffer.Length;
                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    file.UploadFromStream(wholeFile);
                }

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result = file.BeginOpenRead(
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    using (Stream fileStream = file.EndOpenRead(result))
                    {
                        fileStream.Read(outBuffer, 0, outBuffer.Length);
                        file.SetMetadata();
                        TestHelper.ExpectedException(
                            () => fileStream.Read(outBuffer, 0, outBuffer.Length),
                            "File read stream should fail if file is modified during read",
                            HttpStatusCode.PreconditionFailed);
                    }

                    result = file.BeginOpenRead(
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    using (Stream fileStream = file.EndOpenRead(result))
                    {
                        long length = fileStream.Length;
                        file.SetMetadata();
                        TestHelper.ExpectedException(
                            () => fileStream.Read(outBuffer, 0, outBuffer.Length),
                            "File read stream should fail if file is modified during read",
                            HttpStatusCode.PreconditionFailed);
                    }

                    /*
                     * AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1)));
                     * file.SetMetadata();
                     * result = file.BeginOpenRead(
                     *  accessCondition,
                     *  null,
                     *  null,
                     *  ar => waitHandle.Set(),
                     *  null);
                     * waitHandle.WaitOne();
                     * TestHelper.ExpectedException(
                     *  () => file.EndOpenRead(result),
                     *  "File read stream should fail if file is modified during read",
                     *  HttpStatusCode.PreconditionFailed);
                     */
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 15
0
        public void CloudFileCopyIgnoreReadOnlyAndSetArchiveTest()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                // Create Source on server
                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                // Create Destination on server
                CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination");
                destination.Create(1);

                destination.Properties.NtfsAttributes = CloudFileNtfsAttributes.Hidden | CloudFileNtfsAttributes.ReadOnly;
                destination.SetProperties();

                string permission    = "O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-1604012920-1887927527-513D:AI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;S-1-5-21-397955417-626881126-188441444-3053964)";
                string permissionKey = share.CreateFilePermission(permission);
                destination.Properties.FilePermissionKey = permissionKey;

                CloudFile copySource      = source;
                CloudFile copyDestination = destination;

                // Start copy and wait for completion
                string copyId = copyDestination.StartCopy(TestHelper.Defiddler(copySource),
                                                          null,
                                                          null,
                                                          new FileCopyOptions()
                {
                    PreservePermissions = false,
                    IgnoreReadOnly      = true,
                    SetArchive          = true
                },
                                                          null,
                                                          null);

                WaitForCopy(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)));

                TestHelper.ExpectedException(
                    () => copyDestination.AbortCopy(copyId),
                    "Aborting a copy operation after completion should fail",
                    HttpStatusCode.Conflict,
                    "NoPendingCopyOperation");

                source.FetchAttributes();
                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 = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not equal.");

                destination.FetchAttributes();
                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(CloudFileNtfsAttributes.Archive, destination.Properties.NtfsAttributes.Value);
                Assert.IsNotNull(destination.Properties.FilePermissionKey);
                Assert.IsNull(destination.FilePermission);

                destination.Delete();
                source.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 16
0
        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();
            }
        }
Ejemplo n.º 17
0
        public void CloudFileCopyTestAPM()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy");
                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result = copy.BeginStartCopy(TestHelper.Defiddler(source),
                                                              ar => waitHandle.Set(),
                                                              null);
                    waitHandle.WaitOne();
                    string copyId = copy.EndStartCopy(result);
                    WaitForCopy(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)));

                    result = copy.BeginAbortCopy(copyId,
                                                 ar => waitHandle.Set(),
                                                 null);
                    waitHandle.WaitOne();
                    TestHelper.ExpectedException(
                        () => copy.EndAbortCopy(result),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                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 = DownloadText(copy, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not similar");

                copy.FetchAttributes();
                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("value", copy.Metadata["Test"], false, "Copied metadata not same");

                copy.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 18
0
        private static void CloudFileCopy(bool sourceIsSas, bool destinationIsSas)
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                // Create Source on server
                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                string permission    = "O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-1604012920-1887927527-513D:AI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;S-1-5-21-397955417-626881126-188441444-3053964)";
                string permissionKey = share.CreateFilePermission(permission);
                source.Properties.FilePermissionKey = permissionKey;

                var attributes   = CloudFileNtfsAttributes.NoScrubData | CloudFileNtfsAttributes.Temporary;
                var creationTime = DateTimeOffset.UtcNow.AddDays(-1);

                source.Properties.NtfsAttributes = attributes;
                DateTimeOffset lastWriteTime = DateTimeOffset.UtcNow;
                source.Properties.LastWriteTime = lastWriteTime;

                source.Properties.CreationTime = creationTime;
                source.SetProperties();

                // Create Destination on server
                CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination");
                destination.Create(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 = share.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 = copyDestination.StartCopy(TestHelper.Defiddler(copySource),
                                                          null,
                                                          null,
                                                          new FileCopyOptions()
                {
                    PreservePermissions    = true,
                    PreserveCreationTime   = true,
                    PreserveLastWriteTime  = true,
                    PreserveNtfsAttributes = true,
                    SetArchive             = false
                },
                                                          null, null);

                WaitForCopy(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
                    TestHelper.ExpectedException(
                        () => copyDestination.AbortCopy(copyId),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                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 = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not equal.");

                destination.FetchAttributes();
                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(lastWriteTime, destination.Properties.LastWriteTime.Value);
                Assert.AreEqual(creationTime, destination.Properties.CreationTime.Value);
                Assert.AreEqual(attributes, destination.Properties.NtfsAttributes.Value);
                Assert.IsNotNull(destination.Properties.FilePermissionKey);
                Assert.IsNull(destination.FilePermission);

                Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same");

                destination.Delete();
                source.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 19
0
        public async Task FileDisableContentMD5ValidationTestAsync()
        {
            byte[] buffer = new byte[1024];
            Random random = new Random();

            random.NextBytes(buffer);

            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreFileContentMD5         = true,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreFileContentMD5         = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2");
                using (Stream stream = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(stream, null, optionsWithMD5, null);
                }

                using (Stream stream = new MemoryStream())
                {
                    await file.DownloadToStreamAsync(stream, null, optionsWithMD5, null);

                    await file.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null);

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        int    read;
                        do
                        {
                            read = await fileStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        int    read;
                        do
                        {
                            read = await fileStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    file.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    await file.SetPropertiesAsync();

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await file.DownloadToStreamAsync(stream, null, optionsWithMD5, opContext),
                        opContext,
                        "Downloading a file with invalid MD5 should fail",
                        HttpStatusCode.OK);

                    await file.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null);

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = fileStreamForRead.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a file with invalid MD5 should fail");
                    }

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        int    read;
                        do
                        {
                            read = await fileStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Ejemplo n.º 20
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();
            }
        }
        public void CloudFileDownloadToStreamAPMRetry()
        {
            byte[]         buffer = GetRandomBuffer(1 * 1024 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream originalFile = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = file.BeginUploadFromStream(originalFile,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);

                        using (MemoryStream downloadedFile = new MemoryStream())
                        {
                            Exception manglerEx = null;
                            using (HttpMangler proxy = new HttpMangler(false,
                                                                       new[]
                            {
                                TamperBehaviors.TamperNRequestsIf(
                                    session => ThreadPool.QueueUserWorkItem(state =>
                                {
                                    Thread.Sleep(1000);
                                    try
                                    {
                                        session.Abort();
                                    }
                                    catch (Exception e)
                                    {
                                        manglerEx = e;
                                    }
                                }),
                                    2,
                                    AzureStorageSelectors.FileTraffic().IfHostNameContains(share.ServiceClient.Credentials.AccountName))
                            }))
                            {
                                OperationContext operationContext = new OperationContext();
                                result = file.BeginDownloadToStream(downloadedFile, null, null, operationContext,
                                                                    ar => waitHandle.Set(),
                                                                    null);
                                waitHandle.WaitOne();
                                file.EndDownloadToStream(result);
                                TestHelper.AssertStreamsAreEqual(originalFile, downloadedFile);
                            }

                            if (manglerEx != null)
                            {
                                throw manglerEx;
                            }
                        }
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 22
0
        public void FileDisableContentMD5ValidationTest()
        {
            byte[] buffer = new byte[1024];
            Random random = new Random();

            random.NextBytes(buffer);

            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreFileContentMD5         = true,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreFileContentMD5         = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2");
                using (Stream stream = new MemoryStream(buffer))
                {
                    file.UploadFromStream(stream, null, optionsWithMD5);
                }

                using (Stream stream = new MemoryStream())
                {
                    file.DownloadToStream(stream, null, optionsWithMD5);
                    file.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream fileStream = file.OpenRead(null, optionsWithMD5))
                    {
                        int read;
                        do
                        {
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    file.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    file.SetProperties();

                    TestHelper.ExpectedException(
                        () => file.DownloadToStream(stream, null, optionsWithMD5),
                        "Downloading a file with invalid MD5 should fail",
                        HttpStatusCode.OK);
                    file.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream fileStream = file.OpenRead(null, optionsWithMD5))
                    {
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = fileStream.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a file with invalid MD5 should fail");
                    }

                    using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 23
0
        private static async Task TestAccess(string sasToken, SharedAccessFilePermissions permissions, SharedAccessFileHeaders headers, CloudFileShare share, CloudFile file)
        {
            CloudFileShare     SASshare = null;
            CloudFile          SASfile;
            OperationContext   context     = new OperationContext();
            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)
            {
                await UploadTextAsync(SASfile, fileText, Encoding.UTF8);
            }
            else if ((permissions & SharedAccessFilePermissions.Create) == SharedAccessFilePermissions.Create)
            {
                await SASfile.CreateAsync(Encoding.UTF8.GetBytes(fileText).Length);

                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await UploadTextAsync(SASfile, fileText, Encoding.UTF8, operationContext: context),
                    context,
                    "UploadText SAS does not allow for writing",
                    HttpStatusCode.Forbidden,
                    "");
                await UploadTextAsync(file, fileText, Encoding.UTF8);
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.CreateAsync(Encoding.UTF8.GetBytes(fileText).Length, null /* accessCondition */, null /* options */, context),
                    context,
                    "Create file succeeded but SAS does not allow for writing/creating",
                    HttpStatusCode.Forbidden,
                    "");

                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await UploadTextAsync(SASfile, fileText, Encoding.UTF8, operationContext: context),
                    context,
                    "UploadText SAS does not allow for writing/creating",
                    HttpStatusCode.Forbidden,
                    "");
                await UploadTextAsync(file, fileText, Encoding.UTF8);
            }

            if (SASshare != null)
            {
                if ((permissions & SharedAccessFilePermissions.List) == SharedAccessFilePermissions.List)
                {
                    FileResultSegment results = await SASshare.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(null);
                }
                else
                {
                    context = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => { FileResultSegment results = await SASshare.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(null /* maxResults */, null /* currentToken */, null /* options */, context); },
                        context,
                        "List files while SAS does not allow for listing",
                        HttpStatusCode.Forbidden,
                        "");
                }
            }

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

                // 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
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.FetchAttributesAsync(null /* accessCondition */, null /* options */, context),
                    context,
                    "Fetch file attributes while SAS does not allow for reading",
                    HttpStatusCode.Forbidden,
                    "");
            }

            if ((permissions & SharedAccessFilePermissions.Write) == SharedAccessFilePermissions.Write)
            {
                await SASfile.SetMetadataAsync();
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.SetMetadataAsync(null /* accessCondition */, null /* options */, context),
                    context,
                    "Set file metadata while SAS does not allow for writing",
                    HttpStatusCode.Forbidden,
                    "");
            }

            if ((permissions & SharedAccessFilePermissions.Delete) == SharedAccessFilePermissions.Delete)
            {
                await SASfile.DeleteAsync();
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.DeleteAsync(null /* accessCondition */, null /* options */, context),
                    context,
                    "Delete file while SAS does not allow for deleting",
                    HttpStatusCode.Forbidden,
                    "");
            }
        }
Ejemplo n.º 24
0
        public void FileDisableContentMD5ValidationTestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreFileContentMD5         = true,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreFileContentMD5         = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file2");
                    using (Stream stream = new MemoryStream())
                    {
                        file.UploadFromStream(stream, null, optionsWithMD5);
                    }

                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadToStream(result);
                        result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadToStream(result);

                        file.Properties.ContentMD5 = "MDAwMDAwMDA=";
                        file.SetProperties();

                        result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        TestHelper.ExpectedException(
                            () => file.EndDownloadToStream(result),
                            "Downloading a file with invalid MD5 should fail",
                            HttpStatusCode.OK);
                        result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadToStream(result);
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 25
0
        public async Task CloudFileClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync()
        {
            CloudFileClient    fileClient    = GenerateCloudFileClient();
            CloudFileShare     share         = fileClient.GetShareReference(Guid.NewGuid().ToString("N"));
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

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

            try
            {
                await share.CreateAsync();

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

                using (CloudFileStream fileStream = await file.OpenWriteAsync(8 * 1024 * 1024))
                {
                    Stream fos = fileStream;

                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await fos.WriteAsync(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)
                    {
                        await Task.Delay(msRemaining);
                    }

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

                    await fileStream.CommitAsync();
                }

                using (Stream fileStream = await file.OpenReadAsync())
                {
                    Stream fis = fileStream;

                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await fis.ReadAsync(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)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await fis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                fileClient.DefaultRequestOptions.MaximumExecutionTime = null;
                await share.DeleteAsync();
            }
        }
Ejemplo n.º 26
0
        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();
            }
        }
Ejemplo n.º 27
0
        public async Task CloudFileListSharesWithSnapshotAsync()
        {
            CloudFileShare share = GetRandomShareReference();
            await share.CreateAsync();

            share.Metadata["key1"] = "value1";
            await share.SetMetadataAsync();

            CloudFileShare snapshot = await share.SnapshotAsync();

            share.Metadata["key2"] = "value2";
            await share.SetMetadataAsync();

            CloudFileClient       client       = GenerateCloudFileClient();
            List <CloudFileShare> listedShares = new List <CloudFileShare>();
            FileContinuationToken token        = null;

            do
            {
                ShareResultSegment resultSegment = await client.ListSharesSegmentedAsync(share.Name, ShareListingDetails.All, null, token, null, null);

                token = resultSegment.ContinuationToken;

                foreach (CloudFileShare listResultShare in resultSegment.Results)
                {
                    listedShares.Add(listResultShare);
                }
            }while (token != null);

            int  count         = 0;
            bool originalFound = false;
            bool snapshotFound = false;

            foreach (CloudFileShare listShareItem in listedShares)
            {
                if (listShareItem.Name.Equals(share.Name) && !listShareItem.IsSnapshot && !originalFound)
                {
                    count++;
                    originalFound = true;
                    Assert.AreEqual(2, listShareItem.Metadata.Count);
                    Assert.AreEqual("value2", listShareItem.Metadata["key2"]);
                    // Metadata keys should be case-insensitive
                    Assert.AreEqual("value2", listShareItem.Metadata["KEY2"]);
                    Assert.AreEqual("value1", listShareItem.Metadata["key1"]);
                    Assert.AreEqual(share.StorageUri, listShareItem.StorageUri);
                }
                else if (listShareItem.Name.Equals(share.Name) &&
                         listShareItem.IsSnapshot && !snapshotFound)
                {
                    count++;
                    snapshotFound = true;
                    Assert.AreEqual(1, listShareItem.Metadata.Count);
                    Assert.AreEqual("value1", listShareItem.Metadata["key1"]);
                    Assert.AreEqual(snapshot.StorageUri, listShareItem.StorageUri);
                }
            }

            Assert.AreEqual(2, count);

            await snapshot.DeleteAsync();

            await share.DeleteAsync();
        }
Ejemplo n.º 28
0
        private static void CloudFileCopy(bool sourceIsSas, bool destinationIsSas)
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                // Create Source on server
                CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                // Create Destination on server
                CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination");
                destination.Create(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 = copyDestination.StartCopy(TestHelper.Defiddler(copySource));
                WaitForCopy(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
                    TestHelper.ExpectedException(
                        () => copyDestination.AbortCopy(copyId),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                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 = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of file not equal.");

                destination.FetchAttributes();
                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");

                destination.Delete();
                source.Delete();
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 29
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);
            }
        }