Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }