Exemplo n.º 1
0
        public void Given_ValidAESKey_When_Mapped_Then_ValuesMappedCorrectly()
        {
            // Arrange
            const string keyName = "My Test Key";
            var          xmlPoco = new EnvCryptKey()
            {
                Name = keyName,
                Rsa  = null,
                Aes  = new []
                {
                    new EnvCryptKeyAes()
                    {
                        Iv  = "Iv",
                        Key = "Key"
                    }
                },
            };

            var strConverterMock = new Mock <IKeyDetailsPersistConverter>(MockBehavior.Strict);
            {
                var counter = 1;
                strConverterMock.Setup(c => c.Decode("Iv")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("Key")).Returns(new byte[counter++]);
            }
            // Act
            var mapper = new XmlToAesKeyMapper(strConverterMock.Object);
            var res    = mapper.Map(xmlPoco);

            // Assert
            {
                var counter = 1;
                res.Iv.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.Should().BeEquivalentTo(new byte[counter++]);
            }
        }
        public void Given_ValidAESKey_When_MapToXMLPOCO_Then_EncodingHelperClassMustBeused()
        {
            // Arrange
            var key = new AesKey()
            {
                Iv  = RandomByteArrayUtils.CreateRandomByteArray(512),
                Key = RandomByteArrayUtils.CreateRandomByteArray(1024),
            };
            var ivMapsTo  = "myIVString";
            var keyMapsTo = "myIVString";
            var converter = new Mock <IKeyDetailsPersistConverter>(MockBehavior.Strict);

            converter.Setup(c => c.Encode(key.Iv)).Returns(ivMapsTo);
            converter.Setup(c => c.Encode(key.Key)).Returns(keyMapsTo);

            // Act
            var aesKeyToXmlMapper = new AesKeyToXmlMapper(converter.Object);
            var xmlPoco           = new EnvCryptKey();

            aesKeyToXmlMapper.Map(key, xmlPoco);

            // Assert
            xmlPoco.Aes.Should().NotBeNull().And.HaveCount(1);
            xmlPoco.Aes[0].Iv.Should().Be(ivMapsTo);
            xmlPoco.Aes[0].Key.Should().Be(keyMapsTo);
            converter.Verify(c => c.Encode(key.Iv), Times.Once);
            converter.Verify(c => c.Encode(key.Key), Times.Once);
        }
Exemplo n.º 3
0
        public void Given_RSAPublicKey_When_Mapped_Then_NoException()
        {
            // Arrange
            var xmlPoco = new EnvCryptKey()
            {
                Name = "My Test Key",
                Rsa  = new[]
                {
                    new EnvCryptKeyRsa()
                    {
                        Exponent = "Exponent",
                        Modulus  = "Modulus",
                    }
                },
                Aes = null,
            };

            var strConverterMock = new Mock <IKeyDetailsPersistConverter>(MockBehavior.Strict);
            {
                var counter = 1;
                strConverterMock.Setup(c => c.Decode("Exponent")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("Modulus")).Returns(new byte[counter++]);
            }
            // Act
            var mapper = new XmlToRsaKeyMapper(strConverterMock.Object);
            var res    = mapper.Map(xmlPoco);

            // Assert
            {
                var counter = 1;
                res.Key.Exponent.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.Modulus.Should().BeEquivalentTo(new byte[counter++]);
            }
        }
Exemplo n.º 4
0
        protected override TKey MapToKeyPoco(EnvCryptKey fromXml)
        {
            Contract.Requires <ArgumentNullException>(fromXml != null, "fromXml");
            Contract.Ensures(Contract.Result <TKey>() != null);

            return(default(TKey));
        }
        public void Given_ValidAESKeyPOCO_When_SerializeAndDeserialize_Then_POCOContentIsTheSame()
        {
            // Arrange
            var aesKeyXmlPoco = new EnvCryptKey()
            {
                Name = "My AES Key",
                Aes  = new[]
                {
                    new EnvCryptKeyAes()
                    {
                        Iv  = "aesIV",
                        Key = "aesKey"
                    }
                },
                Encryption = "Aes",
                Type       = null
            };

            // Act
            var xmlUtil      = new XmlSerializationUtils <EnvCryptKey>();
            var serialized   = xmlUtil.Serialize(aesKeyXmlPoco);
            var deserialized = xmlUtil.Deserialize(serialized);

            // Assert
            deserialized.Should().NotBeNull();
            deserialized.Name.Should().Be(aesKeyXmlPoco.Name);
            deserialized.Encryption.Should().Be(aesKeyXmlPoco.Encryption);
            deserialized.Type.Should().BeNull();
            deserialized.Aes.Should().HaveCount(1);
            deserialized.Aes[0].Iv.Should().Be("aesIV");
            deserialized.Aes[0].Key.Should().Be("aesKey");
        }
Exemplo n.º 6
0
        public void Given_ValidRSAKey_When_Mapped_Then_ValuesMappedCorrectly()
        {
            // Arrange
            const string keyName = "My Test Key";
            var          xmlPoco = new EnvCryptKey()
            {
                Name = keyName,
                Rsa  = new[]
                {
                    new EnvCryptKeyRsa()
                    {
                        D           = "D",
                        Dp          = "DP",
                        Exponent    = "Exponent",
                        Dq          = "DQ",
                        InverseQ    = "InverseQ",
                        Modulus     = "Modulus",
                        OaepPadding = true,
                        P           = "P",
                        Q           = "Q"
                    }
                },
                Aes = null,
            };

            var strConverterMock = new Mock <IKeyDetailsPersistConverter>(MockBehavior.Strict);
            {
                var counter = 1;
                strConverterMock.Setup(c => c.Decode("D")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("DP")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("Exponent")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("DQ")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("InverseQ")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("Modulus")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("P")).Returns(new byte[counter++]);
                strConverterMock.Setup(c => c.Decode("Q")).Returns(new byte[counter++]);
            }
            // Act
            var mapper = new XmlToRsaKeyMapper(strConverterMock.Object);
            var res    = mapper.Map(xmlPoco);

            // Assert
            {
                var counter = 1;
                res.UseOaepPadding.Should().BeTrue();
                res.Key.D.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.DP.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.Exponent.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.DQ.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.InverseQ.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.Modulus.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.P.Should().BeEquivalentTo(new byte[counter++]);
                res.Key.Q.Should().BeEquivalentTo(new byte[counter++]);
            }
        }
Exemplo n.º 7
0
        public void Given_ValidKeyPathAndNonEmptyKeyFile_When_Run_Then_KeyFileDeserialised()
        {
            // Arrange
            const string keyfile         = @"C:\some\path\keyfile.eckey";
            const string keyFileContents = @"<?xml version=...";
            var          xmlPoco         = new EnvCryptKey();
            var          rsaKey          = new RsaKey(new RSAParameters(), true)
            {
                Name = "key"
            };

            var myFileMock = new Mock <IMyFile>(MockBehavior.Strict);

            myFileMock.Setup(f => f.Exists(keyfile)).Returns(true);

            var textReaderMock = new Mock <ITextReader>(MockBehavior.Strict);

            textReaderMock.Setup(r => r.ReadAllText(keyfile))
            .Returns(keyFileContents);

            var serialisationMock = new Mock <IXmlSerializationUtils <EnvCryptKey> >(MockBehavior.Strict);

            serialisationMock.Setup(u => u.Deserialize(keyFileContents))
            .Returns(xmlPoco);

            var mapperMock = new Mock <IExternalRepresentationToKeyMapper <EnvCryptKey, RsaKey> >(MockBehavior.Strict);

            mapperMock.Setup(m => m.Map(xmlPoco)).Returns(rsaKey);

            // Act
            var workflow = new RsaKeyFromXmlFileLoader(myFileMock.Object, textReaderMock.Object, serialisationMock.Object, mapperMock.Object);
            var keyPoco  = workflow.Load(new KeyFromFileDetails()
            {
                FilePath = keyfile
            });

            // Assert
            keyPoco.Should().Be(rsaKey);
        }
        public void Given_ValidAESKey_When_MapToXMLPOCO_Then_NameMappedCorrectly()
        {
            // Arrange
            var keyName = "MyAESKey";
            var key     = new AesKey()
            {
                Name = keyName,
                Iv   = new byte[1],
                Key  = new byte[1],
            };
            var converter = new Mock <IKeyDetailsPersistConverter>();

            converter.Setup(c => c.Encode(It.IsAny <byte[]>())).Returns("a string not important to the test, but required to satisfy the method's post condition");

            // Act
            var aesKeyToXmlMapper = new AesKeyToXmlMapper(converter.Object);
            var xmlPoco           = new EnvCryptKey();

            aesKeyToXmlMapper.Map(key, xmlPoco);

            // Assert
            xmlPoco.Aes.Should().NotBeNull().And.HaveCount(1);
            xmlPoco.Name.Should().Be(keyName);
        }
Exemplo n.º 9
0
 protected override RsaKey MapToKeyPoco(EnvCryptKey fromXml)
 {
     return(_mapper.Map(fromXml));
 }
Exemplo n.º 10
0
 protected abstract TKey MapToKeyPoco(EnvCryptKey fromXml);