public void StorageCredentialsSharedKey() { StorageCredentials cred = new StorageCredentials(TestBase.TargetTenantConfig.AccountName, TestBase.TargetTenantConfig.AccountKey); Assert.AreEqual(TestBase.TargetTenantConfig.AccountName, cred.AccountName, false); Assert.IsFalse(cred.IsAnonymous); Assert.IsFalse(cred.IsSAS); Assert.IsTrue(cred.IsSharedKey); Uri testUri = new Uri("http://test/abc?querya=1"); Assert.AreEqual(testUri, cred.TransformUri(testUri)); Assert.AreEqual(TestBase.TargetTenantConfig.AccountKey, Convert.ToBase64String(cred.ExportKey())); byte[] dummyKey = { 0, 1, 2 }; string base64EncodedDummyKey = Convert.ToBase64String(dummyKey); cred.UpdateKey(base64EncodedDummyKey, null); Assert.AreEqual(base64EncodedDummyKey, Convert.ToBase64String(cred.ExportKey())); #if !RTMD dummyKey[0] = 3; base64EncodedDummyKey = Convert.ToBase64String(dummyKey); cred.UpdateKey(dummyKey, null); Assert.AreEqual(base64EncodedDummyKey, Convert.ToBase64String(cred.ExportKey())); #endif }
public void CloudStorageAccountExportKey() { string accountKeyString = "abc2564="; string accountString = "BlobEndpoint=http://blobs/;AccountName=test;AccountKey=" + accountKeyString; CloudStorageAccount account = CloudStorageAccount.Parse(accountString); StorageCredentials accountAndKey = (StorageCredentials)account.Credentials; byte[] keyBytes = accountAndKey.ExportKey(); byte[] expectedKeyBytes = Convert.FromBase64String(accountKeyString); for (int i = 0; i < expectedKeyBytes.Length; i++) { Assert.AreEqual(expectedKeyBytes[i], keyBytes[i]); } Assert.AreEqual(expectedKeyBytes.Length, keyBytes.Length); }
public void StorageCredentialsNullKeyValue() { string accountName = TestBase.TargetTenantConfig.AccountName; string keyValue = TestBase.TargetTenantConfig.AccountKey; string nullKeyValueAsString = null; TestHelper.ExpectedException <ArgumentNullException>(() => { StorageCredentials credentials1 = new StorageCredentials(accountName, nullKeyValueAsString); }, "Cannot create key with a null value."); StorageCredentials credentials2 = new StorageCredentials(accountName, keyValue); Assert.AreEqual(accountName, credentials2.AccountName); Assert.IsFalse(credentials2.IsAnonymous); Assert.IsFalse(credentials2.IsSAS); Assert.IsTrue(credentials2.IsSharedKey); Assert.AreEqual(keyValue, Convert.ToBase64String(credentials2.ExportKey())); TestHelper.ExpectedException <ArgumentNullException>(() => { credentials2.UpdateKey(nullKeyValueAsString, null); }, "Cannot update key with a null string value."); #if !(WINDOWS_RT || ASPNET_K) byte[] nullKeyValueAsByteArray = null; StorageCredentials credentials3 = new StorageCredentials(accountName, keyValue); Assert.AreEqual(accountName, credentials3.AccountName); Assert.IsFalse(credentials3.IsAnonymous); Assert.IsFalse(credentials3.IsSAS); Assert.IsTrue(credentials3.IsSharedKey); Assert.AreEqual(keyValue, Convert.ToBase64String(credentials3.ExportKey())); TestHelper.ExpectedException <ArgumentNullException>(() => { credentials3.UpdateKey(nullKeyValueAsByteArray, null); }, "Cannot update key with a null byte array value."); #endif }
public void StorageCredentialsEmptyKeyValue() { string accountName = TestBase.TargetTenantConfig.AccountName; string keyValue = TestBase.TargetTenantConfig.AccountKey; string emptyKeyValueAsString = string.Empty; string emptyKeyConnectionString = string.Format(CultureInfo.InvariantCulture, "DefaultEndpointsProtocol=https;AccountName={0};AccountKey=", accountName); StorageCredentials credentials1 = new StorageCredentials(accountName, emptyKeyValueAsString); Assert.AreEqual(accountName, credentials1.AccountName); Assert.IsFalse(credentials1.IsAnonymous); Assert.IsFalse(credentials1.IsSAS); Assert.IsTrue(credentials1.IsSharedKey); Assert.AreEqual(emptyKeyValueAsString, Convert.ToBase64String(credentials1.ExportKey())); CloudStorageAccount account1 = new CloudStorageAccount(credentials1, true); Assert.AreEqual(emptyKeyConnectionString, account1.ToString(true)); Assert.IsNotNull(account1.Credentials); Assert.AreEqual(accountName, account1.Credentials.AccountName); Assert.IsFalse(account1.Credentials.IsAnonymous); Assert.IsFalse(account1.Credentials.IsSAS); Assert.IsTrue(account1.Credentials.IsSharedKey); Assert.AreEqual(emptyKeyValueAsString, Convert.ToBase64String(account1.Credentials.ExportKey())); CloudStorageAccount account2 = CloudStorageAccount.Parse(emptyKeyConnectionString); Assert.AreEqual(emptyKeyConnectionString, account2.ToString(true)); Assert.IsNotNull(account2.Credentials); Assert.AreEqual(accountName, account2.Credentials.AccountName); Assert.IsFalse(account2.Credentials.IsAnonymous); Assert.IsFalse(account2.Credentials.IsSAS); Assert.IsTrue(account2.Credentials.IsSharedKey); Assert.AreEqual(emptyKeyValueAsString, Convert.ToBase64String(account2.Credentials.ExportKey())); CloudStorageAccount account3; bool isValidAccount3 = CloudStorageAccount.TryParse(emptyKeyConnectionString, out account3); Assert.IsTrue(isValidAccount3); Assert.IsNotNull(account3); Assert.AreEqual(emptyKeyConnectionString, account3.ToString(true)); Assert.IsNotNull(account3.Credentials); Assert.AreEqual(accountName, account3.Credentials.AccountName); Assert.IsFalse(account3.Credentials.IsAnonymous); Assert.IsFalse(account3.Credentials.IsSAS); Assert.IsTrue(account3.Credentials.IsSharedKey); Assert.AreEqual(emptyKeyValueAsString, Convert.ToBase64String(account3.Credentials.ExportKey())); StorageCredentials credentials2 = new StorageCredentials(accountName, keyValue); Assert.AreEqual(accountName, credentials2.AccountName); Assert.IsFalse(credentials2.IsAnonymous); Assert.IsFalse(credentials2.IsSAS); Assert.IsTrue(credentials2.IsSharedKey); Assert.AreEqual(keyValue, Convert.ToBase64String(credentials2.ExportKey())); credentials2.UpdateKey(emptyKeyValueAsString, null); Assert.AreEqual(emptyKeyValueAsString, Convert.ToBase64String(credentials2.ExportKey())); #if !(WINDOWS_RT || ASPNET_K) byte[] emptyKeyValueAsByteArray = new byte[0]; StorageCredentials credentials3 = new StorageCredentials(accountName, keyValue); Assert.AreEqual(accountName, credentials3.AccountName); Assert.IsFalse(credentials3.IsAnonymous); Assert.IsFalse(credentials3.IsSAS); Assert.IsTrue(credentials3.IsSharedKey); Assert.AreEqual(keyValue, Convert.ToBase64String(credentials3.ExportKey())); credentials3.UpdateKey(emptyKeyValueAsByteArray, null); Assert.AreEqual(Convert.ToBase64String(emptyKeyValueAsByteArray), Convert.ToBase64String(credentials3.ExportKey())); #endif }