public async Task CreateBlob(string containerName, string blobName, bool isPublic) { Properties = new BlobProperties() { BlobType = BlobType.BlockBlob }; HttpRequestMessage request = BlobTests.PutBlobRequest(BlobContext, containerName, blobName, Properties, BlobType.BlockBlob, Content, 0, null); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); //HttpRequestHandler.SetContentLength(request, Content.Length); CancellationTokenSource timeout = new CancellationTokenSource(30000); request.Content = new ByteArrayContent(Content); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext, timeout)) { HttpStatusCode statusCode = response.StatusCode; string statusDescription = response.ReasonPhrase; if (statusCode != HttpStatusCode.Created) { Assert.Fail(string.Format("Failed to create blob: {0}, Status: {1}, Status Description: {2}", containerName, statusCode, statusDescription)); } } }
public async Task BlobProtocolGetContainerWithEmptyHeader() { ListingContext listingContext = new ListingContext("default", null); await cloudOwnerAsync.CreateContainer("emptyheadercontainer", true); HttpRequestMessage request = BlobTests.ListContainersRequest(cloudOwnerAsync.BlobContext, listingContext); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); if (cloudOwnerAsync.BlobContext.Credentials != null) { request.Headers.Add("x-ms-blob-application-metadata", ""); } using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, cloudOwnerAsync.BlobContext)) { BlobTests.ListContainersResponse(response, cloudOwnerAsync.BlobContext, HttpStatusCode.OK /*HttpStatusCode.Forbidden*/); } request = BlobTests.ListContainersRequest(cloudOwnerAsync.BlobContext, listingContext); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); if (cloudOwnerAsync.BlobContext.Credentials != null) { request.Headers.Add("x-ms-blob-application-metadata", ""); } using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, cloudOwnerAsync.BlobContext)) { BlobTests.ListContainersResponse(response, cloudOwnerAsync.BlobContext, HttpStatusCode.OK); } }
/// <summary> /// Validates a lease ID header in a response. /// </summary> /// <param name="response">The response.</param> public static void LeaseIdHeader(HttpResponseMessage response) { string leaseId = BlobHttpResponseParsers.GetLeaseId(response); Assert.IsNotNull(leaseId); Assert.IsTrue(BlobTests.LeaseIdValidator(AccessCondition.GenerateLeaseCondition(leaseId))); }
/// <summary> /// Generates a get blob request over the specified range, and checks the request for consistency. /// </summary> /// <param name="context">The testing context.</param> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob.</param> /// <param name="offset">The offset to the range.</param> /// <param name="count">The number of elements in the range.</param> /// <param name="leaseId">The lease ID, or null if the blob is not leased.</param> /// <returns>A web request for getting a blob range.</returns> public static HttpRequestMessage GetBlobRangeRequest(BlobContext context, string containerName, string blobName, long offset, long?count, AccessCondition accessCondition) { bool valid = BlobTests.ContainerNameValidator(containerName) && BlobTests.BlobNameValidator(blobName) && BlobTests.LeaseIdValidator(accessCondition); Uri uri = BlobTests.ConstructGetUri(context.Address, containerName, blobName); HttpRequestMessage request = null; OperationContext opContext = new OperationContext(); try { request = BlobHttpRequestMessageFactory.Get(uri, context.Timeout, null /* snapshot */, offset, count, ChecksumRequested.None, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials, null); } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual(HttpMethod.Get, request.Method); BlobTestUtils.RangeHeader(request, offset, count.HasValue ? (long?)(count.Value + offset - 1) : null); BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId); } return(request); }
public async Task ListContainersScenarioTest(ListingContext listingContext, HttpStatusCode?expectedError, params string[] expectedContainers) { HttpRequestMessage request = BlobTests.ListContainersRequest(BlobContext, listingContext); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.ListContainersResponse(response, BlobContext, expectedError); ListContainersResponse listContainersResponse = await ListContainersResponse.ParseAsync(HttpResponseParsers.GetResponseStream(response), CancellationToken.None); int i = 0; foreach (BlobContainerEntry item in listContainersResponse.Containers) { if (expectedContainers == null) { Assert.Fail("Should not have containers."); } Assert.IsTrue(i < expectedContainers.Length, "Unexpected container: " + item.Name); Assert.AreEqual <string>(expectedContainers[i++], item.Name, "Incorrect container."); } if (expectedContainers != null && i < expectedContainers.Length) { Assert.Fail("Missing container: " + expectedContainers[i] + "(and " + (expectedContainers.Length - i - 1) + " more)."); } } }
public async Task GetBlockListScenarioTest(string containerName, string blobName, BlockListingFilter typesOfBlocks, string leaseId, HttpStatusCode?expectedError, params string[] expectedBlocks) { HttpRequestMessage request = BlobTests.GetBlockListRequest(BlobContext, containerName, blobName, typesOfBlocks, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.GetBlockListResponse(response, BlobContext, expectedError); IEnumerable <ListBlockItem> getBlockListResponse = await GetBlockListResponse.ParseAsync(HttpResponseParsers.GetResponseStream(response), CancellationToken.None); int i = 0; foreach (ListBlockItem item in getBlockListResponse) { if (expectedBlocks == null) { Assert.Fail("Should not have blocks."); } Assert.IsTrue(i < expectedBlocks.Length, "Unexpected block: " + item.Name); Assert.AreEqual <string>(expectedBlocks[i++], item.Name, "Incorrect block."); } if (expectedBlocks != null && i < expectedBlocks.Length) { Assert.Fail("Missing block: " + expectedBlocks[i] + "(and " + (expectedBlocks.Length - i - 1) + " more)."); } } }
public async Task DeleteBlob(string containerName, string blobName) { HttpRequestMessage request = BlobTests.DeleteBlobRequest(BlobContext, containerName, blobName, null); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); await BlobTestUtils.GetResponse(request, BlobContext); }
public async Task CreateContainer(string containerName, bool isPublic, int retries, int millisecondsBetweenRetries) { HttpStatusCode statusCode; StorageExtendedErrorInformation error; while (true) { HttpRequestMessage request = BlobTests.CreateContainerRequest(BlobContext, containerName); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); if (isPublic) { request.Headers.Add("x-ms-blob-public-access", "container"); } using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { statusCode = response.StatusCode; string statusDescription = response.ReasonPhrase; error = await StorageExtendedErrorInformation.ReadFromStreamAsync( HttpResponseParsers.GetResponseStream(response)); } // if the container is being deleted, retry up to the specified times. if (statusCode == HttpStatusCode.Conflict && error != null && error.ErrorCode == BlobErrorCodeStrings.ContainerBeingDeleted && retries > 0) { await Task.Delay(millisecondsBetweenRetries); retries--; continue; } break; } }
/// <summary> /// Scenario test for releasing a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="leaseId">The lease ID.</param> /// <param name="expectedError">The error status code to expect.</param> public async Task ReleaseLeaseScenarioTest(string containerName, string blobName, string leaseId, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.ReleaseLeaseRequest(BlobContext, containerName, blobName, AccessCondition.GenerateLeaseCondition(leaseId)); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.ReleaseLeaseResponse(response, expectedError); } }
public async Task PutBlockScenarioTest(string containerName, string blobName, string blockId, string leaseId, byte[] content, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.PutBlockRequest(BlobContext, containerName, blobName, blockId, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); //HttpRequestHandler.SetContentLength(request, content.Length); request.Content = new ByteArrayContent(content); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.PutBlockResponse(response, BlobContext, expectedError); } }
/// <summary> /// Scenario test for changing a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="leaseId">The lease ID.</param> /// <param name="proposedLeaseId">The proposed lease ID.</param> /// <param name="expectedError">The error status code to expect.</param> /// <returns>The lease ID.</returns> public async Task <string> ChangeLeaseScenarioTest(string containerName, string blobName, string leaseId, string proposedLeaseId, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.ChangeLeaseRequest(BlobContext, containerName, blobName, proposedLeaseId, AccessCondition.GenerateLeaseCondition(leaseId)); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.ChangeLeaseResponse(response, proposedLeaseId, expectedError); return(BlobHttpResponseParsers.GetLeaseId(response)); } }
public async Task GetBlobScenarioTest(string containerName, string blobName, BlobProperties properties, string leaseId, byte[] content, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.GetBlobRequest(BlobContext, containerName, blobName, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.GetBlobResponse(response, BlobContext, properties, content, expectedError); } }
/// <summary> /// Scenario test for acquiring a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="leaseDuration">The lease duration.</param> /// <param name="proposedLeaseId">The proposed lease ID.</param> /// <param name="expectedError">The error status code to expect.</param> /// <returns>The lease ID.</returns> public async Task <string> AcquireLeaseScenarioTest(string containerName, string blobName, int leaseDuration, string proposedLeaseId, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.AcquireLeaseRequest(BlobContext, containerName, blobName, leaseDuration, proposedLeaseId, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.AcquireLeaseResponse(response, proposedLeaseId, expectedError); return(BlobHttpResponseParsers.GetLeaseId(response)); } }
public async Task PutBlobScenarioTest(string containerName, string blobName, BlobProperties properties, BlobType blobType, byte[] content, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.PutBlobRequest(BlobContext, containerName, blobName, properties, blobType, content, content.Length, null); request.Content = new ByteArrayContent(content); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); //HttpRequestHandler.SetContentLength(request, content.Length); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.PutBlobResponse(response, BlobContext, expectedError); } }
/// <summary> /// Scenario test for breaking a lease. /// </summary> /// <param name="containerName">The name of the container.</param> /// <param name="blobName">The name of the blob, if any.</param> /// <param name="breakPeriod">The break period.</param> /// <param name="expectedRemainingTime">The expected remaining time.</param> /// <param name="expectedError">The error status code to expect.</param> /// <returns>The remaining lease time.</returns> public async Task <int> BreakLeaseScenarioTest(string containerName, string blobName, int?breakPeriod, int?expectedRemainingTime, HttpStatusCode?expectedError) { // Create and validate the web request HttpRequestMessage request = BlobTests.BreakLeaseRequest(BlobContext, containerName, blobName, breakPeriod, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { int expectedTime = expectedRemainingTime ?? breakPeriod.Value; int errorMargin = 10; BlobTests.BreakLeaseResponse(response, expectedTime, errorMargin, expectedError); return(expectedError.HasValue ? 0 : BlobHttpResponseParsers.GetRemainingLeaseTime(response).Value); } }
/// <summary> /// Generates and validates a request to break a lease. /// </summary> /// <param name="context">The blob context.</param> /// <param name="containerName">The container name.</param> /// <param name="blobName">The blob name.</param> /// <param name="breakPeriod">The break period.</param> /// <returns>A web request for the operation.</returns> public static HttpRequestMessage BreakLeaseRequest(BlobContext context, string containerName, string blobName, int?breakPeriod, AccessCondition accessCondition) { HttpRequestMessage request; OperationContext opContext = new OperationContext(); if (blobName != null) { // blob lease Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); request = BlobHttpRequestMessageFactory.Lease( uri, context.Timeout, LeaseAction.Break, null /* proposed lease ID */, null /* lease duration */, breakPeriod, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials); } else { // container lease Uri uri = BlobClientTests.ConstructUri(context.Address, containerName); request = ContainerHttpRequestMessageFactory.Lease( uri, context.Timeout, LeaseAction.Break, null /* proposed lease ID */, null /* lease duration */, breakPeriod, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials); } Assert.IsNotNull(request); Assert.AreEqual(HttpMethod.Put, request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.LeaseIdHeader(request, null); BlobTestUtils.LeaseActionHeader(request, "break"); BlobTestUtils.LeaseDurationHeader(request, null); BlobTestUtils.ProposedLeaseIdHeader(request, null); BlobTestUtils.BreakPeriodHeader(request, breakPeriod.HasValue ? breakPeriod.Value.ToString() : null); return(request); }
public static HttpRequestMessage PutBlockListRequest(BlobContext context, string containerName, string blobName, BlobProperties blobProperties, AccessCondition accessCondition) { Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); HttpRequestMessage request = null; OperationContext opContext = new OperationContext(); request = BlobHttpRequestMessageFactory.PutBlockList(uri, context.Timeout, blobProperties, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials); Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual(HttpMethod.Put, request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.ContentLanguageHeader(request, null); BlobTestUtils.ContentMd5Header(request, null); return(request); }
public static HttpRequestMessage PutBlobRequest(BlobContext context, string containerName, string blobName, BlobProperties properties, BlobType blobType, byte[] content, long pageBlobSize, AccessCondition accessCondition) { bool valid = BlobTests.ContainerNameValidator(containerName) && BlobTests.BlobNameValidator(blobName) && BlobTests.PutPropertiesValidator(properties) && BlobTestUtils.ContentValidator(content); bool fatal = !BlobTests.PutPropertiesValidator(properties); Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); HttpRequestMessage request = null; OperationContext opContext = new OperationContext(); try { request = BlobHttpRequestMessageFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, null, accessCondition, null, opContext, SharedKeyCanonicalizer.Instance, context.Credentials, null); if (fatal) { Assert.Fail(); } } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual(HttpMethod.Put, request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.ContentTypeHeader(request, null); BlobTestUtils.ContentDispositionHeader(request, properties.ContentDisposition); BlobTestUtils.ContentEncodingHeader(request, properties.ContentEncoding); BlobTestUtils.ContentLanguageHeader(request, null); BlobTestUtils.ContentMd5Header(request, null); BlobTestUtils.ContentCrc64Header(request, null); BlobTestUtils.CacheControlHeader(request, null); BlobTestUtils.BlobTypeHeader(request, properties.BlobType); BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null); } return(request); }
public async Task PutBlockListScenarioTest(string containerName, string blobName, List <PutBlockListItem> blocks, BlobProperties blobProperties, string leaseId, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.PutBlockListRequest(BlobContext, containerName, blobName, blobProperties, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); byte[] content; using (MemoryStream stream = new MemoryStream()) { BlobRequest.WriteBlockListBody(blocks, stream); stream.Seek(0, SeekOrigin.Begin); content = new byte[stream.Length]; stream.Read(content, 0, content.Length); } //HttpRequestHandler.SetContentLength(request, content.Length); request.Content = new ByteArrayContent(content); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { BlobTests.PutBlockListResponse(response, BlobContext, expectedError); } }
/// <summary> /// Sends a get blob range request with the given parameters and validates both request and response. /// </summary> /// <param name="containerName">The blob's container's name.</param> /// <param name="blobName">The blob's name.</param> /// <param name="leaseId">The lease ID, or null if there is no lease.</param> /// <param name="content">The total contents of the blob.</param> /// <param name="offset">The offset of the contents we will get.</param> /// <param name="count">The number of bytes we will get, or null to get the rest of the blob.</param> /// <param name="expectedError">The error code we expect from this operation, or null if we expect it to succeed.</param> public async Task GetBlobRangeScenarioTest(string containerName, string blobName, string leaseId, byte[] content, long offset, long?count, HttpStatusCode?expectedError) { HttpRequestMessage request = BlobTests.GetBlobRangeRequest(BlobContext, containerName, blobName, offset, count, AccessCondition.GenerateLeaseCondition(leaseId)); Assert.IsTrue(request != null, "Failed to create HttpRequestMessage"); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext)) { long endRange = count.HasValue ? count.Value + offset - 1 : content.Length - 1; byte[] selectedContent = null; // Compute expected content only if call is expected to succeed. if (expectedError == null) { selectedContent = new byte[endRange - offset + 1]; Array.Copy(content, offset, selectedContent, 0, selectedContent.Length); } BlobTests.CheckBlobRangeResponse(response, BlobContext, selectedContent, offset, endRange, content.Length, expectedError); } }
public async Task ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode?expectedError) { // 1. Create Sparse Page Blob int blobSize = 128 * 1024; BlobProperties properties = new BlobProperties() { BlobType = BlobType.PageBlob }; Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName); OperationContext opContext = new OperationContext(); HttpRequestMessage webRequest = BlobHttpRequestMessageFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, null, null, opContext, null, null); using (HttpResponseMessage response = await BlobTestUtils.GetResponse(webRequest, BlobContext)) { BlobTests.PutBlobResponse(response, BlobContext, expectedError); } // 2. Now upload some page ranges for (int m = 0; m * 512 * 4 < blobSize; m++) { int startOffset = 512 * 4 * m; int length = 512; PageRange range = new PageRange(startOffset, startOffset + length - 1); opContext = new OperationContext(); HttpRequestMessage pageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(pageRequest, 512); byte[] content = new byte[length]; random.NextBytes(content); pageRequest.Content = new ByteArrayContent(content); using (HttpResponseMessage pageResponse = await BlobTestUtils.GetResponse(pageRequest, BlobContext)) { } } // 3. Now do a Get Page Ranges List <PageRange> pageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpRequestMessage pageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage pageRangeResponse = await BlobTestUtils.GetResponse(pageRangeRequest, BlobContext)) { IEnumerable <PageRange> ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(pageRangeResponse), CancellationToken.None); pageRanges.AddRange(ranges); } // 4. Now Clear some pages bool skipFlag = false; foreach (PageRange pRange in pageRanges) { skipFlag = !skipFlag; if (skipFlag) { continue; } opContext = new OperationContext(); HttpRequestMessage clearPageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, null, opContext, null, null); HttpRequestHandler.SetContentLength(clearPageRequest, 0); using (HttpResponseMessage clearResponse = await BlobTestUtils.GetResponse(clearPageRequest, BlobContext)) { } } // 5. Get New Page ranges and verify List <PageRange> newPageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpRequestMessage newPageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null); using (HttpResponseMessage newPageRangeResponse = await BlobTestUtils.GetResponse(newPageRangeRequest, BlobContext)) { IEnumerable <PageRange> ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(newPageRangeResponse), CancellationToken.None); newPageRanges.AddRange(ranges); } Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2); for (int l = 0; l < newPageRanges.Count(); l++) { Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset); Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset); } }
public static Uri ConstructGetUri(string address, string containerName, string blobName) { return(BlobTests.GenericUri(address, containerName, blobName)); }