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 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 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);
        }