public void CloudQueueDeleteIfExists()
        {
            string     name  = GenerateNewQueueName();
            CloudQueue queue = DefaultQueueClient.GetQueueReference(name);

            Assert.IsFalse(queue.DeleteIfExists());
            queue.Create();
            Assert.IsTrue(queue.DeleteIfExists());
            Assert.IsFalse(queue.DeleteIfExists());
        }
Пример #2
0
        public void CloudQueueCreateMessage()
        {
            CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.CreateIfNotExists();

                CloudQueueMessage message = new CloudQueueMessage(Guid.NewGuid().ToString());
                queue.AddMessage(message);

                CloudQueueMessage retrMessage = queue.GetMessage();
                string            messageId   = retrMessage.Id;
                string            popReceipt  = retrMessage.PopReceipt;

                // Recreate the message using the messageId and popReceipt.
                CloudQueueMessage newMessage = new CloudQueueMessage(messageId, popReceipt);
                Assert.AreEqual(messageId, newMessage.Id);
                Assert.AreEqual(popReceipt, newMessage.PopReceipt);

                queue.UpdateMessage(newMessage, TimeSpan.FromSeconds(30), MessageUpdateFields.Visibility);
                CloudQueueMessage retrMessage2 = queue.GetMessage();
                Assert.AreEqual(null, retrMessage2);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void CloudQueueSetGetMetadata()
        {
            CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();

                CloudQueue queueToRetrieve = DefaultQueueClient.GetQueueReference(queue.Name);
                queueToRetrieve.FetchAttributes();
                Assert.AreEqual <int>(0, queueToRetrieve.Metadata.Count);

                queue.Metadata.Add("key1", "value1");
                queue.SetMetadata();

                queueToRetrieve.FetchAttributes();
                Assert.AreEqual(1, queueToRetrieve.Metadata.Count);
                Assert.AreEqual("value1", queueToRetrieve.Metadata["key1"]);

                queue.Metadata.Clear();
                queue.SetMetadata();

                queueToRetrieve.FetchAttributes();
                Assert.AreEqual <int>(0, queueToRetrieve.Metadata.Count);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
Пример #4
0
        public void CloudQueueMessageValidateEncryption()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                byte[] messageBytes = new byte[100];
                Random rand         = new Random();
                rand.NextBytes(messageBytes);

                string            inputMessage = Convert.ToBase64String(messageBytes);
                CloudQueueMessage message      = new CloudQueueMessage(inputMessage);
                queue.EncodeMessage = false;

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, options, null);

                // Retrieve message without decrypting
                CloudQueueMessage retrMessage = queue.GetMessage(null, null, null);

                // Decrypt locally
                CloudQueueMessage          decryptedMessage;
                CloudQueueEncryptedMessage encryptedMessage = JsonConvert.DeserializeObject <CloudQueueEncryptedMessage>(retrMessage.AsString);
                EncryptionData             encryptionData   = encryptedMessage.EncryptionData;

                byte[] contentEncryptionKey = aesKey.UnwrapKeyAsync(encryptionData.WrappedContentKey.EncryptedKey, encryptionData.WrappedContentKey.Algorithm, CancellationToken.None).Result;

                using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
                {
                    myAes.Key = contentEncryptionKey;
                    myAes.IV  = encryptionData.ContentEncryptionIV;

                    byte[] src = Convert.FromBase64String(encryptedMessage.EncryptedMessageContents);
                    using (ICryptoTransform decryptor = myAes.CreateDecryptor())
                    {
                        decryptedMessage = new CloudQueueMessage(decryptor.TransformFinalBlock(src, 0, src.Length));
                    }
                }

                TestHelper.AssertBuffersAreEqual(message.AsBytes, decryptedMessage.AsBytes);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
Пример #5
0
        private void DoCloudQueueAddUpdateEncryptedMessageAPM(IKey key, DictionaryKeyResolver keyResolver)
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue       queue  = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                QueueEncryptionPolicy policy        = new QueueEncryptionPolicy(key, null);
                QueueRequestOptions   createOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    // Add message
                    IAsyncResult result = queue.BeginAddMessage(message, null, null, createOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    queue.EndAddMessage(result);

                    policy = new QueueEncryptionPolicy(null, keyResolver);
                    QueueRequestOptions retrieveOptions = new QueueRequestOptions()
                    {
                        EncryptionPolicy = policy
                    };

                    // Retrieve message
                    result = queue.BeginGetMessage(null, retrieveOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    CloudQueueMessage retrMessage = queue.EndGetMessage(result);
                    Assert.AreEqual(messageStr, retrMessage.AsString);

                    // Update message
                    string updatedMessage = Guid.NewGuid().ToString("N");
                    retrMessage.SetMessageContent(updatedMessage);
                    result = queue.BeginUpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility, createOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    queue.EndUpdateMessage(result);

                    // Retrieve updated message
                    result = queue.BeginGetMessage(null, retrieveOptions, null, ar => waitHandle.Set(), null);
                    waitHandle.WaitOne();
                    retrMessage = queue.EndGetMessage(result);
                    Assert.AreEqual(updatedMessage, retrMessage.AsString);
                }
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void CloudQueueCreateUsingDifferenctVersionHeader()
        {
            string     name  = GenerateNewQueueName();
            CloudQueue queue = DefaultQueueClient.GetQueueReference(name);

            OperationContext opContext = new OperationContext();

            opContext.SendingRequest += (obj, args) => args.Request.Headers[Constants.HeaderConstants.StorageVersionHeader] = "2011-08-18";

            queue.Create(null, opContext);
            Assert.AreEqual((int)HttpStatusCode.Created, opContext.LastResult.HttpStatusCode);

            queue.DeleteIfExists();
        }
Пример #7
0
        public void QueueRegionalSASTest()
        {
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");

            CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();
                string            messageContent = Guid.NewGuid().ToString();
                CloudQueueMessage message        = new CloudQueueMessage(messageContent);
                queue.AddMessage(message);

                // Prepare SAS authentication with full permissions
                string                       id          = Guid.NewGuid().ToString();
                DateTime                     start       = DateTime.UtcNow;
                DateTime                     expiry      = start.AddMinutes(30);
                QueuePermissions             permissions = new QueuePermissions();
                SharedAccessQueuePermissions queuePerm   = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update;
                permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = queuePerm
                });

                queue.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                string             sasTokenFromId   = queue.GetSharedAccessSignature(null, id);
                StorageCredentials sasCredsFromId   = new StorageCredentials(sasTokenFromId);
                CloudQueue         sasQueueFromId   = new CloudQueue(queue.Uri, sasCredsFromId);
                CloudQueueMessage  receivedMessage1 = sasQueueFromId.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage1.AsString);

                string             sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null);
                StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy);
                CloudQueue         sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy);
                CloudQueueMessage  receivedMessage2   = sasQueueFromPolicy.PeekMessage();
                Assert.AreEqual(messageContent, receivedMessage2.AsString);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;
                queue.DeleteIfExists();
            }
        }
Пример #8
0
        private void DoCloudQueueAddUpdateEncryptedMessage(IKey key, DictionaryKeyResolver keyResolver)
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                QueueEncryptionPolicy policy = new QueueEncryptionPolicy(key, null);

                // Add message
                QueueRequestOptions createOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };
                queue.AddMessage(message, null, null, createOptions, null);

                // Retrieve message
                QueueEncryptionPolicy retrPolicy      = new QueueEncryptionPolicy(null, keyResolver);
                QueueRequestOptions   retrieveOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = retrPolicy
                };
                CloudQueueMessage retrMessage = queue.GetMessage(null, retrieveOptions, null);
                Assert.AreEqual(messageStr, retrMessage.AsString);

                // Update message
                string updatedMessage = Guid.NewGuid().ToString("N");
                retrMessage.SetMessageContent(updatedMessage);
                queue.UpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility, createOptions, null);

                // Retrieve updated message
                retrMessage = queue.GetMessage(null, retrieveOptions, null);
                Assert.AreEqual(updatedMessage, retrMessage.AsString);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void QueueEmptyHeaderSigningTest()
        {
            CloudQueue       queue   = DefaultQueueClient.GetQueueReference(Guid.NewGuid().ToString("N"));
            OperationContext context = new OperationContext();

            try
            {
                context.UserHeaders = new Dictionary <string, string>();
                context.UserHeaders.Add("x-ms-foo", String.Empty);
                queue.Create(null, context);
                CloudQueueMessage message = new CloudQueueMessage("Hello Signing");
                queue.AddMessage(message, null, null, null, context);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
Пример #10
0
        public void CloudQueueAddUpdateEncryptedEncodedMessage()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                byte[] messageBytes = new byte[100];
                Random rand         = new Random();
                rand.NextBytes(messageBytes);

                string            inputMessage = Convert.ToBase64String(messageBytes);
                CloudQueueMessage message      = new CloudQueueMessage(inputMessage);
                queue.EncodeMessage = false;

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, options, null);

                // Retrieve message
                CloudQueueMessage retrMessage = queue.GetMessage(null, options, null);
                Assert.AreEqual(inputMessage, retrMessage.AsString);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void CloudQueueGetSetPermissions()
        {
            CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName());

            try
            {
                queue.Create();
                QueuePermissions emptyPermission = queue.GetPermissions();
                Assert.AreEqual(emptyPermission.SharedAccessPolicies.Count, 0);
                string                       id          = Guid.NewGuid().ToString();
                DateTime                     start       = DateTime.UtcNow;
                DateTime                     expiry      = start.AddMinutes(30);
                QueuePermissions             permissions = new QueuePermissions();
                SharedAccessQueuePermissions queuePerm   = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update;
                permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = queuePerm
                });

                queue.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                CloudQueue       queueToRetrieve       = DefaultQueueClient.GetQueueReference(queue.Name);
                QueuePermissions permissionsToRetrieve = queueToRetrieve.GetPermissions();

                Assert.AreEqual(permissions.SharedAccessPolicies.Count, permissionsToRetrieve.SharedAccessPolicies.Count);
                //Assert.AreEqual(start, permissionsToRetrieve.SharedAccessPolicies[id].SharedAccessStartTime.Value.UtcDateTime);
                //Assert.AreEqual(expiry, permissionsToRetrieve.SharedAccessPolicies[id].SharedAccessExpiryTime.Value.UtcDateTime);
                Assert.AreEqual(permissions.SharedAccessPolicies[id].Permissions, permissionsToRetrieve.SharedAccessPolicies[id].Permissions);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
Пример #12
0
        public void CloudQueueAddEncrypted64KMessage()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            inputMessage = new string('a', 64 * 1024);
                CloudQueueMessage message      = new CloudQueueMessage(inputMessage);
                queue.EncodeMessage = false;

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, null, null);

                // Add encrypted Message
                TestHelper.ExpectedException <ArgumentException>(
                    () => queue.AddMessage(message, null, null, options, null),
                    "Adding an encrypted message that exceeds message limits should throw");
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
Пример #13
0
        public void CloudQueueMessageEncryptionWithStrictMode()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            // Create the resolver to be used for unwrapping.
            DictionaryKeyResolver resolver = new DictionaryKeyResolver();

            resolver.Add(aesKey);

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                QueueEncryptionPolicy policy = new QueueEncryptionPolicy(aesKey, null);

                // Add message with policy.
                QueueRequestOptions createOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };
                createOptions.RequireEncryption = true;

                queue.AddMessage(message, null, null, createOptions, null);

                // Set policy to null and add message while RequireEncryption flag is still set to true. This should throw.
                createOptions.EncryptionPolicy = null;

                TestHelper.ExpectedException <InvalidOperationException>(
                    () => queue.AddMessage(message, null, null, createOptions, null),
                    "Not specifying a policy when RequireEnryption is set to true should throw.");

                // Retrieve message
                QueueEncryptionPolicy retrPolicy      = new QueueEncryptionPolicy(null, resolver);
                QueueRequestOptions   retrieveOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = retrPolicy
                };
                retrieveOptions.RequireEncryption = true;

                CloudQueueMessage retrMessage = queue.GetMessage(null, retrieveOptions, null);

                // Update message with plain text.
                string updatedMessage = Guid.NewGuid().ToString("N");
                retrMessage.SetMessageContent(updatedMessage);

                queue.UpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility);

                // Retrieve updated message with RequireEncryption flag but no metadata on the service. This should throw.
                TestHelper.ExpectedException <StorageException>(
                    () => queue.GetMessage(null, retrieveOptions, null),
                    "Retrieving with RequireEncryption set to true and no metadata on the service should fail.");

                // Set RequireEncryption to false and retrieve.
                retrieveOptions.RequireEncryption = false;
                queue.GetMessage(null, retrieveOptions, null);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }