public void RegisterCipherTest() { var cipher1 = new CipherStub("cipherStub1"); var cipher2 = new CipherStub("cipherStub2"); using var provider = new CryptoProvider(cipher1); provider.RegisterCipher(cipher2); var ex = Assert.Throws <StorageClientException>(() => provider.RegisterCipher(cipher1)); Assert.AreEqual("Custom cipher with name cipherStub1 is already registered", ex.Message); ex = Assert.Throws <StorageClientException>(() => provider.UnregisterCipher(cipher1)); Assert.AreEqual("Can't unregister default cipher with name cipherStub1", ex.Message); var unregisterStatus2 = provider.UnregisterCipher(cipher2); Assert.IsTrue(unregisterStatus2); var unregisterStatus4 = provider.UnregisterCipher(cipher2); Assert.IsFalse(unregisterStatus4); var unregisterStatus5 = provider.UnregisterCipher(null); Assert.IsFalse(unregisterStatus5); var unregisterStatus6 = provider.UnregisterCipher(new CipherStub("")); Assert.IsFalse(unregisterStatus6); }
public void ValidateCipherTest() { var cipher1 = new CipherStub("cipherStub1"); var cipher2 = new CipherStub("cipherStub2"); var cipher3 = new FernetCipher("Fernet cipher"); using var provider = new CryptoProvider(cipher1); provider.RegisterCipher(cipher2) .RegisterCipher(cipher3); var customEncryptionKey = new CustomEncryptionKey(1, Encoding.UTF8.GetBytes("Custom Encryption Key 32 symbols")); provider.ValidateCustomCiphers(new SecretsData(new List <Secret> { customEncryptionKey }, customEncryptionKey)); Assert.IsNotNull(provider); var encryptionSecret = new EncryptionSecret(2, Encoding.UTF8.GetBytes("EncryptionSecret")); provider.ValidateCustomCiphers(new SecretsData(new List <Secret> { customEncryptionKey, encryptionSecret }, encryptionSecret)); Assert.IsNotNull(provider); }
public void RegisterCipherNegativeTest() { var cipher = new CipherStub(""); using var provider = new CryptoProvider(); var exception = Assert.Throws <StorageClientException>(() => provider.RegisterCipher(cipher)); Assert.AreEqual("Custom cipher has null name", exception.Message); exception = Assert.Throws <StorageClientException>(() => provider.RegisterCipher(null)); Assert.AreEqual("Custom cipher is null", exception.Message); exception = Assert.Throws <StorageClientException>(() => { var unused = new CryptoProvider(cipher); }); Assert.AreEqual("Custom cipher has null name", exception.Message); }
public void ValidateCipherNegativeTest() { var cipher1 = new CipherStub("cipherStub1"); var cipher2 = new WrongCipher("WrongCipher"); using var provider = new CryptoProvider(cipher1); provider.RegisterCipher(cipher2); var customEncryptionKey = new CustomEncryptionKey(1, Encoding.UTF8.GetBytes("CustomEncryptionKey")); var exception = Assert.Throws <StorageClientException>(() => { provider.ValidateCustomCiphers(new SecretsData(new List <Secret> { customEncryptionKey }, customEncryptionKey)); }); Assert.AreEqual("Validation failed for custom cipher with version 'WrongCipher'", exception.Message); provider.UnregisterCipher(cipher2); var cipher3 = new CipherWithException("CipherWithException"); provider.RegisterCipher(cipher3); exception = Assert.Throws <StorageClientException>(() => { provider.ValidateCustomCiphers(new SecretsData(new List <Secret> { customEncryptionKey }, customEncryptionKey)); }); Assert.AreEqual("Validation failed for custom cipher with version 'CipherWithException'", exception.Message); Assert.NotNull(exception.InnerException); Assert.IsInstanceOf <NotImplementedException>(exception.InnerException); var secretData = SecretsDataGenerator.FromPassword("password"); exception = Assert.Throws <StorageClientException>(() => provider.ValidateCustomCiphers(secretData)); Assert.AreEqual("There is no custom encryption key for the custom ciphers", exception.Message); }
public void EncryptWithCustomCipherNegativeTest() { var cipher = new CipherWithException("some cipher"); const string exceptionMessage = "some exception message"; #pragma warning disable CA1303 var anotherCipher = new CipherWithException("another cipher", new StorageCryptoException(exceptionMessage)); #pragma warning restore CA1303 using var provider = new CryptoProvider(cipher); provider.RegisterCipher(anotherCipher); var key = new CustomEncryptionKey(1, Encoding.UTF8.GetBytes("Custom Encryption Key 32 symbols")); var secretData = new SecretsData(new List <Secret> { key }, key); var exception = Assert.Throws <StorageCryptoException>(() => provider.Decrypt("c" + Guid.NewGuid() + ":123", secretData, 1)); Assert.AreEqual("Unknown cipher format", exception.Message); exception = Assert.Throws <StorageCryptoException>(() => provider.Decrypt("z" + Guid.NewGuid() + ":123", secretData, 1)); Assert.AreEqual("Unknown cipher format", exception.Message); exception = Assert.Throws <StorageCryptoException>(() => provider.Encrypt(Guid.NewGuid().ToString(), secretData)); Assert.AreEqual("Unexpected exception during encryption", exception.Message); Assert.NotNull(exception.InnerException); exception = Assert.Throws <StorageCryptoException>(() => provider.Decrypt("cc29tZSBjaXBoZXI=:c29tZSBjaXBoZXI=", secretData, 1)); Assert.AreEqual("Unexpected error", exception.Message); Assert.IsNotNull(exception.InnerException); exception = Assert.Throws <StorageCryptoException>(() => provider.Decrypt("cYW5vdGhlciBjaXBoZXI=:YW5vdGhlciBjaXBoZXI=", secretData, 1)); Assert.AreEqual(exceptionMessage, exception.Message); using var anotherProvider = new CryptoProvider(anotherCipher); exception = Assert.Throws <StorageCryptoException>(() => anotherProvider.Encrypt(Guid.NewGuid().ToString(), secretData)); Assert.AreEqual(exceptionMessage, exception.Message); }