public void CreateFromResponseMessageExceptionAndErrorCode()
        {
            var response = new MockResponse(400, "reason");
            var inner    = new Exception("Boom!");
            var ex       = new StorageRequestFailedException(response, "message", inner, "storagecode");

            Assert.AreEqual(400, ex.Status);
            StringAssert.StartsWith("message", ex.Message);
            StringAssert.Contains("reason", ex.Message);
            Assert.AreEqual(inner, ex.InnerException);
            Assert.AreEqual("storagecode", ex.ErrorCode);
            StringAssert.Contains("storagecode", ex.Message);
        }
        public async Task SharedAccessSignatureAuthAsync()
        {
            // Create a service level SAS that only allows reading from service
            // level APIs
            AccountSasBuilder sas = new AccountSasBuilder
            {
                // Allow access to queues
                Services = new AccountSasServices()
                {
                    Queues = true
                }.ToString(),

                // Allow access to the service level APIs
                              ResourceTypes = new AccountSasResourceTypes()
                {
                    Service = true
                }.ToString(),

                // Allow read access
                              Permissions = new AccountSasPermissions()
                {
                    Read = true
                }.ToString(),

                // Access expires in 1 hour!
                              ExpiryTime = DateTimeOffset.UtcNow.AddHours(1)
            };

            // Create a SharedKeyCredential that we can use to sign the SAS token
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey);

            // Build a SAS URI
            UriBuilder sasUri = new UriBuilder(StorageAccountQueueUri)
            {
                Query = sas.ToSasQueryParameters(credential).ToString()
            };

            // Create a client that can authenticate with the SAS URI
            QueueServiceClient service = new QueueServiceClient(sasUri.Uri);

            // Make a service request to verify we've succesfully authenticated
            await service.GetPropertiesAsync();

            // Try to create a new container (which is beyond our
            // delegated permission)
            StorageRequestFailedException ex =
                Assert.ThrowsAsync <StorageRequestFailedException>(
                    async() => await service.CreateQueueAsync(Randomize("sample-queue")));

            Assert.AreEqual(403, ex.Status);
        }
Example #3
0
        public async Task Batch_SasUri_NotOwner()
        {
            // Create a container using SAS for Auth
            string containerName = GetNewContainerName();

            using IDisposable _ = GetNewContainer(out BlobContainerClient container);

            using TestScenario scenario = Scenario(GetServiceClient_BlobServiceSas_Container(containerName));
            Uri[] blobs = await scenario.CreateBlobUrisAsync(container, 2);

            BlobBatchClient client           = scenario.GetBlobBatchClient();
            StorageRequestFailedException ex = Assert.ThrowsAsync <StorageRequestFailedException>(
                async() => await client.DeleteBlobsAsync(blobs));

            Assert.AreEqual(403, ex.Status);
        }
Example #4
0
        public async Task Batch_Limit()
        {
            using TestScenario scenario = Scenario();
            Uri[] blobs = await scenario.CreateBlobUrisAsync(257);

            BlobBatchClient client = scenario.GetBlobBatchClient();

            // One over the limit
            StorageRequestFailedException ex = Assert.ThrowsAsync <StorageRequestFailedException>(
                async() => await client.DeleteBlobsAsync(blobs));

            Assert.AreEqual(400, ex.Status);
            Assert.AreEqual("ExceedsMaxBatchRequestCount", ex.ErrorCode);

            // The exact limit
            await client.DeleteBlobsAsync(blobs.Take(256).ToArray());
        }
        public void CreateFromEverything()
        {
            var response   = new MockResponse(400, "reason");
            var inner      = new Exception("Boom!");
            var additional = new Dictionary <string, string>
            {
                { "foo", "bar" },
                { "qux", "quux" }
            };
            var ex = new StorageRequestFailedException(response, "message", inner, "storagecode", additional);

            Assert.AreEqual(400, ex.Status);
            StringAssert.StartsWith("message", ex.Message);
            StringAssert.Contains("reason", ex.Message);
            Assert.AreEqual(inner, ex.InnerException);
            Assert.AreEqual("storagecode", ex.ErrorCode);
            StringAssert.Contains("storagecode", ex.Message);
            StringAssert.Contains("foo: bar", ex.Message);
            StringAssert.Contains("qux: quux", ex.Message);
        }
Example #6
0
        public async Task SetBlobAccessTier_OneFails_Convenience()
        {
            using TestScenario scenario = Scenario();
            BlobClient[] good = await scenario.CreateBlobsAsync(2);

            Uri[] bad  = scenario.GetInvalidBlobUris(1);
            Uri[] uris = good.Select(b => b.Uri).Concat(bad).ToArray();

            BlobBatchClient    client = scenario.GetBlobBatchClient();
            AggregateException exes   = Assert.ThrowsAsync <AggregateException>(
                async() => await client.SetBlobsAccessTierAsync(uris, AccessTier.Cool));

            StorageRequestFailedException ex = exes.InnerException as StorageRequestFailedException;

            Assert.IsNotNull(ex);
            Assert.AreEqual(404, ex.Status);
            Assert.IsTrue(BlobErrorCode.ContainerNotFound == ex.ErrorCode);

            await scenario.AssertTiers(AccessTier.Cool, good);
        }
Example #7
0
        public async Task SetBlobAccessTier_OneFails()
        {
            using TestScenario scenario = Scenario();
            BlobClient[] good = await scenario.CreateBlobsAsync(2);

            Uri[] bad = scenario.GetInvalidBlobUris(1);

            BlobBatchClient    client    = scenario.GetBlobBatchClient();
            BlobBatch          batch     = client.CreateBatch();
            Response           response1 = batch.SetBlobAccessTier(good[0].Uri, AccessTier.Cool);
            Response           response2 = batch.SetBlobAccessTier(good[1].Uri, AccessTier.Cool);
            Response           response3 = batch.SetBlobAccessTier(bad[0], AccessTier.Cool);
            AggregateException exes      = Assert.ThrowsAsync <AggregateException>(
                async() => await client.SubmitBatchAsync(batch));

            StorageRequestFailedException ex = exes.InnerException as StorageRequestFailedException;

            Assert.IsNotNull(ex);
            Assert.AreEqual(404, ex.Status);
            Assert.IsTrue(BlobErrorCode.ContainerNotFound == ex.ErrorCode);
            await scenario.AssertTiers(AccessTier.Cool, good);
        }
Example #8
0
        public void BatchErrors()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a container named "sample-container" and then create it
            BlobServiceClient   service   = new BlobServiceClient(connectionString);
            BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container"));

            container.Create();
            try
            {
                // Create a blob named "valid"
                BlobClient valid = container.GetBlobClient("valid");
                valid.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Valid!")));

                // Get a reference to a blob named "invalid", but never create it
                BlobClient invalid = container.GetBlobClient("invalid");

                // Delete both blobs at the same time
                BlobBatchClient batch = service.GetBlobBatchClient();
                batch.DeleteBlobs(new Uri[] { valid.Uri, invalid.Uri });
            }
            catch (AggregateException ex)
            {
                // An aggregate exception is thrown for all the indivudal failures
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                StorageRequestFailedException failure = ex.InnerException as StorageRequestFailedException;
                Assert.IsTrue(BlobErrorCode.BlobNotFound == failure.ErrorCode);
            }
            finally
            {
                // Clean up after the test when we're finished
                container.Delete();
            }
        }