Example #1
0
        public void CanGetSpecificEncryptorAndDecryptorWhenMultipleCredentialsExist()
        {
            var defaultCredential = new Credential(() => GetSequentialByteArray(16));
            var credential1       = new Credential(() => GetSequentialByteArray(16), name: "encryptor1");
            var credential2       = new Credential(() => GetSequentialByteArray(16), name: "encryptor2");

            var crypto = new SymmetricCrypto(new[] { defaultCredential, credential1, credential2 });

            crypto.CanEncrypt(null).Should().Be(true);
            crypto.CanEncrypt("encryptor1").Should().Be(true);
            crypto.CanEncrypt("encryptor2").Should().Be(true);
            crypto.CanEncrypt("encryptor3").Should().Be(false);
            crypto.CanEncrypt("something").Should().Be(false);

            crypto.GetEncryptor(null).Should().NotBe(null);
            crypto.GetEncryptor("encryptor1").Should().NotBe(null);
            crypto.GetEncryptor("encryptor2").Should().NotBe(null);
            crypto.Invoking(c => c.GetEncryptor("encryptor3")).ShouldThrow <KeyNotFoundException>().WithMessage("The specified credential was not found: encryptor3.");
            crypto.Invoking(c => c.GetEncryptor("something")).ShouldThrow <KeyNotFoundException>().WithMessage("The specified credential was not found: something.");

            crypto.CanDecrypt(null).Should().Be(true);
            crypto.CanDecrypt("encryptor1").Should().Be(true);
            crypto.CanDecrypt("encryptor2").Should().Be(true);
            crypto.CanDecrypt("encryptor3").Should().Be(false);
            crypto.CanDecrypt("something").Should().Be(false);

            crypto.GetDecryptor(null).Should().NotBe(null);
            crypto.GetDecryptor("encryptor1").Should().NotBe(null);
            crypto.GetDecryptor("encryptor2").Should().NotBe(null);
            crypto.Invoking(c => c.GetDecryptor("encryptor3")).ShouldThrow <KeyNotFoundException>().WithMessage("The specified credential was not found: encryptor3.");
            crypto.Invoking(c => c.GetDecryptor("something")).ShouldThrow <KeyNotFoundException>().WithMessage("The specified credential was not found: something.");
        }
        public void CanGetSpecificEncryptorAndDecryptorWhenMultipleCredentialsExist()
        {
            var credentialMock = new Mock <ICredential>();

            credentialMock.Setup(cm => cm.Algorithm).Returns(SymmetricAlgorithm.Aes);
            credentialMock.Setup(cm => cm.IVSize).Returns(16);
            credentialMock.Setup(cm => cm.GetKey()).Returns(GetSequentialByteArray(16));

            var credentialRepoMock = new Mock <ICredentialRepository>();

            ICredential outCredential;

            credentialRepoMock
            .Setup(cr => cr.TryGet(null, out outCredential))
            .OutCallback((object keyIdentifier, out ICredential credential) => credential = credentialMock.Object)
            .Returns(true);

            credentialRepoMock
            .Setup(cr => cr.TryGet("encryptor1", out outCredential))
            .OutCallback((object keyIdentifier, out ICredential credential) => credential = credentialMock.Object)
            .Returns(true);

            credentialRepoMock
            .Setup(cr => cr.TryGet("encryptor2", out outCredential))
            .OutCallback((object keyIdentifier, out ICredential credential) => credential = credentialMock.Object)
            .Returns(true);

            var crypto = new SymmetricCrypto(credentialRepoMock.Object);

            crypto.CanEncrypt(null).Should().Be(true);
            crypto.CanEncrypt("encryptor1").Should().Be(true);
            crypto.CanEncrypt("encryptor2").Should().Be(true);
            crypto.CanEncrypt("encryptor3").Should().Be(false);
            crypto.CanEncrypt("something").Should().Be(false);

            crypto.GetEncryptor(null).Should().NotBe(null);
            crypto.GetEncryptor("encryptor1").Should().NotBe(null);
            crypto.GetEncryptor("encryptor2").Should().NotBe(null);
            crypto.Invoking(c => c.GetEncryptor("encryptor3")).ShouldThrow <KeyNotFoundException>().WithMessage("Unable to locate credential using keyIdentifier: encryptor3");
            crypto.Invoking(c => c.GetEncryptor("something")).ShouldThrow <KeyNotFoundException>().WithMessage("Unable to locate credential using keyIdentifier: something");

            crypto.CanDecrypt(null).Should().Be(true);
            crypto.CanDecrypt("encryptor1").Should().Be(true);
            crypto.CanDecrypt("encryptor2").Should().Be(true);
            crypto.CanDecrypt("encryptor3").Should().Be(false);
            crypto.CanDecrypt("something").Should().Be(false);

            crypto.GetDecryptor(null).Should().NotBe(null);
            crypto.GetDecryptor("encryptor1").Should().NotBe(null);
            crypto.GetDecryptor("encryptor2").Should().NotBe(null);
            crypto.Invoking(c => c.GetDecryptor("encryptor3")).ShouldThrow <KeyNotFoundException>().WithMessage("Unable to locate credential using keyIdentifier: encryptor3");
            crypto.Invoking(c => c.GetDecryptor("something")).ShouldThrow <KeyNotFoundException>().WithMessage("Unable to locate credential using keyIdentifier: something");
        }