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));
        }
        public void CanSerializeWithEncryptRootObjectEnabled()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<Bar>(configuration);

            var instance = new Bar
            {
                Baz = new Baz
                {
                    Qux = "abc",
                    Garply = true
                },
                Corge = 123.45
            };

            var json = serializer.Serialize(instance);

            var expected =
                @""""
                + encryptionMechanism.Encrypt(@"{""Baz"":{""Qux"":""abc"",""Garply"":true},""Corge"":123.45}")
                + @"""";

            Assert.That(json, Is.EqualTo(expected));
        }
        public void AClassDecoratedWithTheEncryptAttributeIsEncrypted()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
            };

            var serializer = new JsonSerializer<Grault>(configuration);

            var instance = new Grault
            {
                Qux = "abc",
                Garply = true
            };

            var json = serializer.Serialize(instance);

            var expected =
                @""""
                + encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
                + @"""";

            Assert.That(json, Is.EqualTo(expected));
        }
Esempio n. 4
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. 5
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));
        }
        public void CanDeserializeEncrypted(string json, bool expected)
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<bool>(configuration);

            json =
                @""""
                + encryptionMechanism.Encrypt(json)
                + @"""";

            var value = serializer.Deserialize(json);

            Assert.That(value, Is.EqualTo(expected));
        }
        public void CanSerializeEncrypted(bool value, string expectedPlainText)
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<bool>(configuration);

            var json = serializer.Serialize(value);

            var expected =
                @""""
                + encryptionMechanism.Encrypt(expectedPlainText)
                + @"""";

            Assert.That(json, Is.EqualTo(expected));
        }
Esempio n. 8
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));
        }
        public void CanSerializeEncrypted()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<double>(configuration);

            var json = serializer.Serialize(123.45);

            var expected =
                @""""
                + encryptionMechanism.Encrypt(@"123.45")
                + @"""";

            Assert.That(json, Is.EqualTo(expected));
        }
        public void CanEncryptAndDecryptIndividualElementValues()
        {
            var sb = new StringBuilder();
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();
            var options = new TestSerializeOptions { EncryptionMechanism = encryptionMechanism, SerializationState = new SerializationState() };

            using (var stringWriter = new StringWriter(sb))
            {
                using (var writer = new XSerializerXmlTextWriter(stringWriter, options))
                {
                    writer.WriteStartElement("foo");

                    writer.WriteStartElement("bar");

                    writer.WriteStartAttribute("baz");
                    writer.WriteValue("123");
                    writer.WriteEndAttribute();

                    writer.WriteStartElement("qux");
                    writer.IsEncryptionEnabled = true;
                    writer.WriteValue("abc");
                    writer.IsEncryptionEnabled = false;
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // </bar>

                    writer.WriteStartElement("rab");

                    writer.WriteStartAttribute("zab");
                    writer.WriteValue("789");
                    writer.WriteEndAttribute();

                    writer.WriteStartElement("xuq");
                    writer.IsEncryptionEnabled = true;
                    writer.WriteValue("xyz");
                    writer.IsEncryptionEnabled = false;
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // </rab>

                    writer.WriteEndElement(); // </foo>
                }
            }

            Func<string, string> e = x => encryptionMechanism.Encrypt(x, null, options.SerializationState);

            // reference xml:
            // <foo><bar baz="123"><qux>abc</qux></bar><rab zab="789"><xuq>xyz</xuq></rab></foo>

            var xml = sb.ToString();

            var expectedXml =
                "<foo>" +
                @"<bar baz=""123"">"
                + "<qux>" + e("abc") + "</qux>"
                + "</bar>" +
                @"<rab zab=""789"">"
                + "<xuq>" + e("xyz") + "</xuq>"
                + "</rab>"
                + "</foo>";

            Assert.That(xml, Is.EqualTo(expectedXml));

            using (var stringReader = new StringReader(xml))
            {
                using (var xmlReader = new XmlTextReader(stringReader))
                {
                    using (var reader = new XSerializerXmlReader(xmlReader, encryptionMechanism, options.EncryptKey, options.SerializationState))
                    {
                        reader.Read(); // None -> <foo>

                        reader.Read(); // <foo> -> <bar>

                        Assert.That(reader.GetAttribute("baz"), Is.EqualTo("123"));

                        reader.Read(); // <bar> -> <qux>

                        reader.IsDecryptionEnabled = true;
                        Assert.That(reader.ReadString(), Is.EqualTo("abc")); // <qux> -> "abc" -> </qux>
                        reader.IsDecryptionEnabled = false;

                        reader.Read(); // </qux> -> </bar>
                        reader.Read(); // </bar> -> <rab>

                        Assert.That(reader.GetAttribute("zab"), Is.EqualTo("789"));

                        reader.Read(); // <rab> -> <xuq>

                        reader.IsDecryptionEnabled = true;
                        Assert.That(reader.ReadString(), Is.EqualTo("xyz")); // <xuq> -> "xyz" -> </xuq>
                        reader.IsDecryptionEnabled = false;

                        reader.Read(); // </xuq> -> </rab>

                        reader.Read(); // </rab> -> </foo>

                        reader.Read(); // </foo> -> None

                        Assert.That(reader.NodeType, Is.EqualTo(XmlNodeType.None));
                    }
                }
            }
        }
Esempio n. 11
0
        public void DecryptDoesNothingIfTheItemIsNull()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonArray(encryptionMechanism:encryptionMechanism)
                {
                    null,
                };

            object bar1 = foo[0];
            Assert.That(bar1, Is.Null);

            foo.Decrypt(0);

            object bar2 = foo[0];
            Assert.That(bar2, Is.Null);
        }
Esempio n. 12
0
        public void ForXmlTheEncryptAttributeDefinedInInterfaceIsUsed()
        {
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();

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

            var item = new HasEncryptAttribute
            {
                Foo = "abc"
            };

            var xml = serializer.Serialize(item);

            Assert.That(xml, Is.Not.StringContaining("<Foo>abc</Foo>"));
            var encryptedValue = encryptionMechanism.Encrypt(@"abc", null, new SerializationState());
            Assert.That(xml, Is.StringContaining(string.Format("<Foo>{0}</Foo>", encryptedValue)));
        }
Esempio n. 13
0
        public void CanEncryptJsonArrayProperty()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    {
                        "bar", new JsonArray(encryptionMechanism:encryptionMechanism)
                        {
                            false,
                            new JsonNumber("123.45"),
                        }
                    },
                };

            object bar = foo.bar;
            foo.Encrypt("bar");
            string barEncrypted = foo.bar;

            Assert.That(barEncrypted, Is.Not.EqualTo(bar));

            Assert.That(barEncrypted, Is.EqualTo(encryptionMechanism.Encrypt(@"[false,123.45]")));
        }
Esempio n. 14
0
        public void CanDecryptJsonObjectProperty()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    { "bar", encryptionMechanism.Encrypt(@"{""baz"":false,""qux"":123.45}") },
                };

            string barEncrypted = foo.bar;
            foo.Decrypt("bar");
            dynamic bar = foo.bar;

            Assert.That(bar, Is.Not.EqualTo(barEncrypted));

            Assert.That(bar.baz, Is.False);
            Assert.That(bar.qux, Is.EqualTo(123.45));
        }
Esempio n. 15
0
        public void CanEncryptPrimitiveProperty(object value, string expectedPlaintextValue)
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    { "bar", value },
                };

            object bar = foo.bar;
            foo.Encrypt("bar");
            string barEncrypted = foo.bar;

            Assert.That(barEncrypted, Is.Not.EqualTo(bar));

            Assert.That(barEncrypted, Is.EqualTo(encryptionMechanism.Encrypt(expectedPlaintextValue)));
        }
        public void CanDeserializeEncrypted()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<string>(configuration);

            var json = @""""
                + encryptionMechanism.Encrypt(@"""abc""")
                + @"""";

            var value = serializer.Deserialize(json);

            Assert.That(value, Is.EqualTo("abc"));
        }
        public void DuplicatedEncryptAttributesHaveNoEffectOnSerialization()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
            };

            var serializer = new JsonSerializer<Thud>(configuration);

            var instance = new Thud
            {
                Grault = new Grault
                {
                    Qux = "abc",
                    Garply = true
                },
                Waldo = new Waldo
                {
                    Qux = "abc",
                    Garply = true
                }
            };

            var json = serializer.Serialize(instance);

            var expected =
                @"{""Grault"":"
                    + @""""
                    + encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
                    + @""""
                + @",""Waldo"":"
                    + @""""
                    + encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
                    + @""""
                + @"}";

            Assert.That(json, Is.EqualTo(expected));
        }
        public void DuplicatedEncryptAttributesHaveNoEffectOnDeserialization()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var json =
                @"{""Grault"":"
                    + @""""
                    + encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
                    + @""""
                + @",""Waldo"":"
                    + @""""
                    + encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
                    + @""""
                + @"}";

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
            };

            var serializer = new JsonSerializer<Thud>(configuration);

            var result = serializer.Deserialize(json);

            Assert.That(result.Grault.Qux, Is.EqualTo("abc"));
            Assert.That(result.Grault.Garply, Is.EqualTo(true));
            Assert.That(result.Waldo.Qux, Is.EqualTo("abc"));
            Assert.That(result.Waldo.Garply, Is.EqualTo(true));
        }
        public void APropertyDecoratedWithTheEncryptAttributeIsDecrypted()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var json =
                @"{""Qux"":"""
                + encryptionMechanism.Encrypt(@"""abc""")
                + @""",""Garply"":"""
                + encryptionMechanism.Encrypt("true")
                + @"""}";

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
            };

            var serializer = new JsonSerializer<Waldo>(configuration);

            var result = serializer.Deserialize(json);

            Assert.That(result.Garply, Is.EqualTo(true));
            Assert.That(result.Qux, Is.EqualTo("abc"));
        }
        public void CanDeserializeWithEncryptRootObjectEnabled()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var json =
                @""""
                + encryptionMechanism.Encrypt(@"{""Baz"":{""Qux"":""abc"",""Garply"":true},""Corge"":123.45}")
                + @"""";

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<Bar>(configuration);

            var result = serializer.Deserialize(json);

            Assert.That(result.Baz.Garply, Is.EqualTo(true));
            Assert.That(result.Baz.Qux, Is.EqualTo("abc"));
            Assert.That(result.Corge, Is.EqualTo(123.45));
        }
Esempio n. 21
0
        public void CanDecryptPrimitiveProperty(string jsonValue, object expectedValue)
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    { "bar", encryptionMechanism.Encrypt(jsonValue) },
                };

            string barEncrypted = foo.bar;
            foo.Decrypt("bar");
            object bar = foo.bar;

            Assert.That(bar, Is.Not.EqualTo(barEncrypted));

            Assert.That(bar, Is.EqualTo(expectedValue));
        }
Esempio n. 22
0
        public void CanDecryptPrimitiveItem(string jsonValue, object expectedValue)
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonArray(encryptionMechanism:encryptionMechanism)
                {
                    encryptionMechanism.Encrypt(jsonValue),
                };

            string barEncrypted = foo[0];
            foo.Decrypt(0);
            object bar = foo[0];

            Assert.That(bar, Is.Not.EqualTo(barEncrypted));

            Assert.That(bar, Is.EqualTo(expectedValue));
        }
Esempio n. 23
0
        public void CanDecryptJsonArrayProperty()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    { "bar", encryptionMechanism.Encrypt(@"[1,2,3]") },
                };

            string barEncrypted = foo.bar;
            foo.Decrypt("bar");
            IList<int> bar = foo.bar;

            Assert.That(bar, Is.Not.EqualTo(barEncrypted));

            Assert.That(bar[0], Is.EqualTo(1));
            Assert.That(bar[1], Is.EqualTo(2));
            Assert.That(bar[2], Is.EqualTo(3));
        }
Esempio n. 24
0
        public void CanDecryptJsonObjectItem()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonArray(encryptionMechanism:encryptionMechanism)
                {
                    encryptionMechanism.Encrypt(@"{""baz"":false,""qux"":123.45}"),
                };

            string barEncrypted = foo[0];
            foo.Decrypt(0);
            dynamic bar = foo[0];

            Assert.That(bar, Is.Not.EqualTo(barEncrypted));

            Assert.That(bar.baz, Is.False);
            Assert.That(bar.qux, Is.EqualTo(123.45));
        }
Esempio n. 25
0
        public void CanEncryptJsonObjectProperty()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    {
                        "bar", new JsonObject(encryptionMechanism:encryptionMechanism)
                        {
                            { "baz", false },
                            { "qux", new JsonNumber("123.45") },
                        }
                    },
                };

            object bar = foo.bar;
            foo.Encrypt("bar");
            string barEncrypted = foo.bar;

            Assert.That(barEncrypted, Is.Not.EqualTo(bar));

            Assert.That(barEncrypted, Is.EqualTo(encryptionMechanism.Encrypt(@"{""baz"":false,""qux"":123.45}")));
        }
Esempio n. 26
0
        public void CanDecryptJsonArrayItem()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonArray(encryptionMechanism:encryptionMechanism)
                {
                    encryptionMechanism.Encrypt(@"[1,2,3]"),
                };

            string barEncrypted = foo[0];
            foo.Decrypt(0);
            IList<int> bar = foo[0];

            Assert.That(bar, Is.Not.EqualTo(barEncrypted));

            Assert.That(bar[0], Is.EqualTo(1));
            Assert.That(bar[1], Is.EqualTo(2));
            Assert.That(bar[2], Is.EqualTo(3));
        }
Esempio n. 27
0
        public void DecryptDoesNothingIfTheValueIsNull()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonObject(encryptionMechanism:encryptionMechanism)
                {
                    { "bar", null },
                };

            object bar1 = foo.bar;
            Assert.That(bar1, Is.Null);

            foo.Decrypt("bar");

            object bar2 = foo.bar;
            Assert.That(bar2, Is.Null);
        }
Esempio n. 28
0
        public void CanEncryptPrimitiveItem(object value, string expectedPlaintextValue)
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonArray(encryptionMechanism:encryptionMechanism)
                {
                    value,
                };

            object bar = foo[0];
            foo.Encrypt(0);
            string barEncrypted = foo[0];

            Assert.That(barEncrypted, Is.Not.EqualTo(bar));

            Assert.That(barEncrypted, Is.EqualTo(encryptionMechanism.Encrypt(expectedPlaintextValue)));
        }
Esempio n. 29
0
        public void CanEncryptNumericItem()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            dynamic foo =
                new JsonArray(encryptionMechanism:encryptionMechanism)
                {
                    new JsonNumber("123.45"),
                };

            object bar = foo[0];
            foo.Encrypt(0);
            string barEncrypted = foo[0];

            Assert.That(barEncrypted, Is.Not.EqualTo(bar));

            Assert.That(barEncrypted, Is.EqualTo(encryptionMechanism.Encrypt("123.45")));
        }
Esempio n. 30
0
        public void ForJsonTheEncryptAttributeDefinedInInterfaceIsUsed()
        {
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();

            var serializer =
                new JsonSerializer<HasEncryptAttribute>(new JsonSerializerConfiguration
                {
                    ShouldUseAttributeDefinedInInterface = true,
                    EncryptionMechanism = encryptionMechanism
                });

            var item = new HasEncryptAttribute
            {
                Foo = "abc"
            };

            var json = serializer.Serialize(item);

            Assert.That(json, Is.Not.StringContaining(@"""Foo"":""abc"""));
            var encryptedValue = encryptionMechanism.Encrypt(@"""abc""", null, new SerializationState());
            Assert.That(json, Is.StringContaining(string.Format(@"""Foo"":""{0}""", encryptedValue)));
        }