Example #1
0
        public void EncryptCallsCryptoGetEncryptorWhenSerializationStateIsEmpty()
        {
            var mockCrypto    = new Mock <ICrypto>();
            var mockEncryptor = new Mock <IEncryptor>();

            mockCrypto.Setup(c => c.GetEncryptor(It.IsAny <string>())).Returns(() => mockEncryptor.Object);

            var encryptionMechanism = new CryptoEncryptionMechanism(mockCrypto.Object);

            var credentialName     = "foobar";
            var serializationState = new SerializationState();

            encryptionMechanism.Encrypt("foo", credentialName, serializationState);

            mockCrypto.Verify(c => c.GetEncryptor(It.Is <string>(obj => obj == credentialName)), Times.Once());
        }
Example #2
0
        public void GetEncryptorIsNotCalledWhenSerializationStateIsNotEmpty()
        {
            var mockCrypto    = new Mock <ICrypto>();
            var mockEncryptor = new Mock <IEncryptor>();

            mockCrypto.Setup(c => c.GetEncryptor(It.IsAny <string>())).Returns(() => mockEncryptor.Object);

            var encryptionMechanism = new CryptoEncryptionMechanism(mockCrypto.Object);

            var credentialName     = "foobar";
            var serializationState = new SerializationState();

            // Force the mock encryptor to be cached in the serialization state.
            serializationState.Get(() => mockEncryptor.Object);

            encryptionMechanism.Encrypt("foo", credentialName, serializationState);

            mockCrypto.Verify(c => c.GetEncryptor(It.Is <string>(obj => obj == credentialName)), Times.Never());
        }
        public void TheCachedEncryptorReturnsTheReturnValue()
        {
            var mockCrypto    = new Mock <ICrypto>();
            var mockEncryptor = new Mock <IEncryptor>();

            mockCrypto.Setup(c => c.GetEncryptor(It.IsAny <string>())).Returns(() => mockEncryptor.Object);
            mockEncryptor.Setup(e => e.Encrypt(It.IsAny <string>())).Returns("bar");

            var encryptionMechanism = new CryptoEncryptionMechanism(mockCrypto.Object);

            var credentialName     = "foobar";
            var serializationState = new SerializationState();

            serializationState.Get(() => mockEncryptor.Object);

            var encrypted = encryptionMechanism.Encrypt("foo", credentialName, serializationState);

            Assert.AreEqual(encrypted, "bar");
        }
Example #4
0
        public void TheCachedEncryptorReturnsTheReturnValue()
        {
            var mockCrypto    = new Mock <ICrypto>();
            var mockEncryptor = new Mock <IEncryptor>();

            mockCrypto.Setup(c => c.GetEncryptor(It.IsAny <string>())).Returns(() => mockEncryptor.Object);
            mockEncryptor.Setup(e => e.Encrypt(It.IsAny <string>())).Returns("bar");

            var encryptionMechanism = new CryptoEncryptionMechanism(mockCrypto.Object);

            var credentialName     = "foobar";
            var serializationState = new SerializationState();

            // Force the mock encryptor to be cached in the serialization state.
            serializationState.Get(() => mockEncryptor.Object);

            var encrypted = encryptionMechanism.Encrypt("foo", credentialName, serializationState);

            encrypted.Should().Be("bar");
        }