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