Esempio n. 1
0
        public void EncryptsAndDecryptsPropertyWhenTheClassOfThePropertyTypeIsDecoratedWithEncryptAttribute()
        {
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();

            var serializer = new XmlSerializer <Container <EncryptedThing> >(x => x.WithEncryptionMechanism(encryptionMechanism));

            var instance = new Container <EncryptedThing>
            {
                Item = new EncryptedThing
                {
                    Foo = 123,
                    Bar = true
                }
            };

            var xml = serializer.Serialize(instance);

            var doc = XDocument.Parse(xml);

            Assert.That(encryptionMechanism.Decrypt(doc.Root.Element("Item").Value, null, _serializationState), Is.EqualTo("<Bar>true</Bar>"));
            Assert.That(encryptionMechanism.Decrypt(doc.Root.Element("Item").Attribute("Foo").Value, null, _serializationState), Is.EqualTo("123"));

            var roundTrip = serializer.Deserialize(xml);

            Assert.That(roundTrip.Item.Foo, Is.EqualTo(instance.Item.Foo));
            Assert.That(roundTrip.Item.Bar, Is.EqualTo(instance.Item.Bar));
        }
Esempio n. 2
0
        public void EncryptsAndDecryptsGenericDictionaryPropertyWhenTheKeyClassIsDecoratedWithEncryptAttribute()
        {
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();

            var serializer = new XmlSerializer <Container <Dictionary <EncryptedThing, int> > >(x => x.WithEncryptionMechanism(encryptionMechanism));

            var instance = new Container <Dictionary <EncryptedThing, int> >
            {
                Item = new Dictionary <EncryptedThing, int>
                {
                    {
                        new EncryptedThing
                        {
                            Foo = 123,
                            Bar = true
                        },
                        1
                    },
                    {
                        new EncryptedThing
                        {
                            Foo = 789,
                            Bar = false
                        },
                        2
                    },
                }
            };

            var xml = serializer.Serialize(instance);

            var doc = XDocument.Parse(xml);

            var          actualDecryptedItemElementValue   = encryptionMechanism.Decrypt(doc.Root.Element("Item").Value, null, _serializationState);
            const string expectedDecryptedItemElementValue =
                @"<Item><Key Foo=""123""><Bar>true</Bar></Key><Value>1</Value></Item>"
                + @"<Item><Key Foo=""789""><Bar>false</Bar></Key><Value>2</Value></Item>";

            Assert.That(actualDecryptedItemElementValue, Is.EqualTo(expectedDecryptedItemElementValue));

            var roundTrip = serializer.Deserialize(xml);

            Assert.That(roundTrip.Item.Keys, Is.EquivalentTo(instance.Item.Keys));

            var key = new EncryptedThing {
                Foo = 123, Bar = true
            };

            Assert.That(roundTrip.Item[key], Is.EqualTo(instance.Item[key]));

            key = new EncryptedThing {
                Foo = 789, Bar = false
            };
            Assert.That(roundTrip.Item[key], Is.EqualTo(instance.Item[key]));
        }
Esempio n. 3
0
        public void CanEncryptAndDecryptEntireRootObjectViaEncryptAttribute()
        {
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();

            var serializer = new XmlSerializer <EncryptedThing>(x => x.WithEncryptionMechanism(encryptionMechanism));

            var instance = new EncryptedThing
            {
                Foo = 123,
                Bar = true
            };

            var xml = serializer.Serialize(instance);

            var doc = XDocument.Parse(xml);

            Assert.That(encryptionMechanism.Decrypt(doc.Root.Value, null, _serializationState), Is.EqualTo("<Bar>true</Bar>"));
            Assert.That(encryptionMechanism.Decrypt(doc.Root.Attribute("Foo").Value, null, _serializationState), Is.EqualTo("123"));

            var roundTrip = serializer.Deserialize(xml);

            Assert.That(roundTrip.Foo, Is.EqualTo(instance.Foo));
            Assert.That(roundTrip.Bar, Is.EqualTo(instance.Bar));
        }
Esempio n. 4
0
        public void EncryptsAndDecryptsGenericListPropertyWhenTheListGenericArgumentClassIsDecoratedWithEncryptAttribute()
        {
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();

            var serializer = new XmlSerializer <Container <List <EncryptedThing> > >(x => x.WithEncryptionMechanism(encryptionMechanism));

            var instance = new Container <List <EncryptedThing> >
            {
                Item = new List <EncryptedThing>
                {
                    new EncryptedThing
                    {
                        Foo = 123,
                        Bar = true
                    },
                    new EncryptedThing
                    {
                        Foo = 789,
                        Bar = false
                    },
                }
            };

            var xml = serializer.Serialize(instance);

            var doc = XDocument.Parse(xml);

            var          actualDecryptedItemElementValue   = encryptionMechanism.Decrypt(doc.Root.Element("Item").Value, null, _serializationState);
            const string expectedDecryptedItemElementValue =
                @"<EncryptedThing Foo=""123""><Bar>true</Bar></EncryptedThing>"
                + @"<EncryptedThing Foo=""789""><Bar>false</Bar></EncryptedThing>";

            Assert.That(actualDecryptedItemElementValue, Is.EqualTo(expectedDecryptedItemElementValue));

            var roundTrip = serializer.Deserialize(xml);

            Assert.That(roundTrip.Item.Count, Is.EqualTo(2));
            Assert.That(roundTrip.Item[0].Foo, Is.EqualTo(instance.Item[0].Foo));
            Assert.That(roundTrip.Item[0].Bar, Is.EqualTo(instance.Item[0].Bar));
            Assert.That(roundTrip.Item[1].Foo, Is.EqualTo(instance.Item[1].Foo));
            Assert.That(roundTrip.Item[1].Bar, Is.EqualTo(instance.Item[1].Bar));
        }