public async Task FileReadLockToETagTestAsync()
        {
            byte[]         outBuffer = new byte[1 * 1024 * 1024];
            byte[]         buffer    = GetRandomBuffer(2 * outBuffer.Length);
            CloudFileShare share     = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

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

                OperationContext opContext = new OperationContext();
                using (Stream fileStream = await file.OpenReadAsync(null, null, opContext))
                {
                    Stream fileStreamForRead = fileStream;
                    await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length);

                    await file.SetMetadataAsync();
                    await ExpectedExceptionAsync(
                        async() => await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
                        opContext,
                        "File read stream should fail if file is modified during read",
                        HttpStatusCode.PreconditionFailed);
                }

                opContext = new OperationContext();
                using (Stream fileStream = await file.OpenReadAsync(null, null, opContext))
                {
                    Stream fileStreamForRead = fileStream;
                    long   length            = fileStreamForRead.Length;
                    await file.SetMetadataAsync();
                    await ExpectedExceptionAsync(
                        async() => await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
                        opContext,
                        "File read stream should fail if file is modified during read",
                        HttpStatusCode.PreconditionFailed);
                }

                /*
                 * opContext = new OperationContext();
                 * AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1)));
                 * await file.SetMetadataAsync();
                 * await TestHelper.ExpectedExceptionAsync(
                 *  async () => await file.OpenReadAsync(accessCondition, null, opContext),
                 *  opContext,
                 *  "File read stream should fail if file is modified during read",
                 *  HttpStatusCode.PreconditionFailed);
                 */
            }
            finally
            {
                await share.DeleteAsync();
            }
        }
        public async Task FileReadStreamSeekTestAsync()
        {
            byte[]         buffer = GetRandomBuffer(3 * 1024 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamMinimumReadSizeInBytes = 2 * 1024 * 1024;
                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(wholeFile);
                }

                OperationContext opContext = new OperationContext();
                using (Stream fileStream = await file.OpenReadAsync(null, null, opContext))
                {
                    int attempts = await FileReadStreamSeekTestAsync(fileStream, file.StreamMinimumReadSizeInBytes, buffer);

                    TestHelper.AssertNAttempts(opContext, attempts);
                }
            }
            finally
            {
                await share.DeleteAsync();
            }
        }
Exemplo n.º 3
0
        public async Task FileSeekTestAsync()
        {
            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, null, null, null);

                    using (Stream fileStream = await file.OpenReadAsync())
                    {
                        Stream fileStreamForRead = fileStream;
                        fileStreamForRead.Seek(2048, 0);
                        byte[] buff    = new byte[100];
                        int    numRead = await fileStreamForRead.ReadAsync(buff, 0, 100);

                        Assert.AreEqual(numRead, 0);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
        public async Task FileReadStreamBasicTestAsync()
        {
            byte[]         buffer = GetRandomBuffer(5 * 1024 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

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

                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    Stream readStream = await file.OpenReadAsync();

                    using (Stream fileStream = readStream)
                    {
                        TestHelper.AssertStreamsAreEqual(wholeFile, fileStream);
                    }
                }
            }
            finally
            {
                await share.DeleteAsync();
            }
        }
Exemplo n.º 5
0
        public async Task FileOpenReadWriteTestAsync()
        {
            byte[]         buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

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

                using (CloudFileStream fileStream = await file.OpenWriteAsync(2048))
                {
                    Stream fileStreamForWrite = fileStream;
                    await fileStreamForWrite.WriteAsync(buffer, 0, 2048);

                    await fileStreamForWrite.FlushAsync();
                }

                using (Stream dstStream = await file.OpenReadAsync())
                {
                    Stream       dstStreamForRead = dstStream;
                    MemoryStream memoryStream     = new MemoryStream(buffer);
                    TestHelper.AssertStreamsAreEqual(memoryStream, dstStreamForRead);
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Exemplo n.º 6
0
        public async Task FileOpenReadTestAsync()
        {
            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);

                    Stream dstStream = await file.OpenReadAsync();

                    using (Stream dstStreamForRead = dstStream)
                    {
                        TestHelper.AssertStreamsAreEqual(srcStream, dstStreamForRead);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Exemplo n.º 7
0
        public async Task FileOpenWriteSeekReadTestAsync()
        {
            byte[]         buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

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

                MemoryStream memoryStream = new MemoryStream(buffer);
                using (CloudFileStream fileStream = await file.OpenWriteAsync(2048))
                {
                    Stream fileStreamForWrite = fileStream;
                    await fileStreamForWrite.WriteAsync(buffer, 0, 2048);

                    Assert.AreEqual(fileStreamForWrite.Position, 2048);

                    fileStreamForWrite.Seek(1024, 0);
                    memoryStream.Seek(1024, 0);
                    Assert.AreEqual(fileStreamForWrite.Position, 1024);

                    byte[] testBuffer = GetRandomBuffer(1024);

                    await memoryStream.WriteAsync(testBuffer, 0, 1024);

                    await fileStreamForWrite.WriteAsync(testBuffer, 0, 1024);

                    Assert.AreEqual(fileStreamForWrite.Position, memoryStream.Position);

                    await fileStreamForWrite.FlushAsync();
                }

                using (Stream dstStream = await file.OpenReadAsync())
                {
                    Stream dstStreamForRead = dstStream;
                    TestHelper.AssertStreamsAreEqual(memoryStream, dstStreamForRead);
                }
            }
            finally
            {
                await share.DeleteAsync();
            }
        }
Exemplo n.º 8
0
        public async Task FileOpenReadWithCancelTest()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            byte[]         buffer = new byte[4 * 1024 * 1024];
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();
                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    file.UploadFromStream(srcStream);
                }

                Stream dstStream = await file.OpenReadAsync(null, null, null, cts.Token);

                cts.Cancel();

                try
                {
                    Assert.IsTrue(cts.Token.IsCancellationRequested);

                    int bytesRead = 0;

                    do
                    {
                        bytesRead = await dstStream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
                    }while (bytesRead > 0);

                    Assert.Fail("Expected exception not raised");
                }
                catch (StorageException e)
                {
                    Assert.AreEqual("A task was canceled.", e.Message);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Exemplo n.º 9
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();
            }
        }
Exemplo n.º 10
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();
            }
        }
Exemplo n.º 11
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();
            }
        }