Ejemplo n.º 1
0
        public void CloudQueueMessageSetContent()
        {
            string s = "1234";

            byte[] bytes = Encoding.UTF8.GetBytes(s);
            string s64   = Convert.ToBase64String(Encoding.UTF8.GetBytes(s));

            CloudQueueMessage message = new CloudQueueMessage();

            Assert.IsNull(message.RawBytes);
            Assert.IsNull(message.RawString);

            message.SetMessageContent2(bytes);

            Assert.AreSame(bytes, message.RawBytes);
            Assert.IsNull(message.RawString);
            Assert.AreEqual(QueueMessageType.RawBytes, message.MessageType);
            Assert.AreEqual(s, message.AsString);
            Assert.IsTrue(bytes.SequenceEqual(message.AsBytes));

            message.SetMessageContent2(s64, true);

            Assert.IsNull(message.RawBytes);
            Assert.AreEqual(s64, message.RawString);
            Assert.AreEqual(QueueMessageType.Base64Encoded, message.MessageType);
            Assert.AreEqual(s, message.AsString);
            Assert.IsTrue(bytes.SequenceEqual(message.AsBytes));

            message.SetMessageContent2(s, false);

            Assert.IsNull(message.RawBytes);
            Assert.AreEqual(s, message.RawString);
            Assert.AreEqual(QueueMessageType.RawString, message.MessageType);
            Assert.AreEqual(s, message.AsString);
            Assert.IsTrue(bytes.SequenceEqual(message.AsBytes));

            // obsolete APIs

            message.SetMessageContent(bytes);

            Assert.IsNull(message.RawBytes);
            Assert.AreEqual(s64, message.RawString);
            Assert.AreEqual(QueueMessageType.Base64Encoded, message.MessageType);
            Assert.AreEqual(s, message.AsString);
            Assert.IsTrue(bytes.SequenceEqual(message.AsBytes));

            message.SetMessageContent(s);

            Assert.IsNull(message.RawBytes);
            Assert.AreEqual(s, message.RawString);
            Assert.AreEqual(QueueMessageType.RawString, message.MessageType);
            Assert.AreEqual(s, message.AsString);
            Assert.IsTrue(bytes.SequenceEqual(message.AsBytes));
        }
        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();
            }
        }
        private void DoCloudQueueAddUpdateEncryptedMessage(IKey key, DictionaryKeyResolver keyResolver)
        {
            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = "salam";//s 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 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();
            }
        }