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)); }
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)); }
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)); }
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)); }
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"))); }
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))); }
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)); }
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))); }
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)); }
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}"))); }
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 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 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)); }
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 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 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)); } } } }
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 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 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)); }
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]"))); }
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)); }
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 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))); }