-Fixed reading invalid unicode surrogate pairs

This commit is contained in:
James Newton-King
2012-04-10 20:48:42 +12:00
parent 2e1211f689
commit 436c3f9a93
8 changed files with 167 additions and 33 deletions
@@ -58,11 +58,59 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual(JsonToken.String, reader.TokenType);
string s = reader.Value.ToString();
Assert.AreEqual(2, s.Length);
StringInfo stringInfo = new StringInfo(s);
Assert.AreEqual(1, stringInfo.LengthInTextElements);
}
[Test]
public void SurrogatePairReplacement()
{
// existing good surrogate pair
Assert.AreEqual("ABC \ud800\udc00 DEF", ReadString("ABC \\ud800\\udc00 DEF"));
// invalid surrogates (two high back-to-back)
Assert.AreEqual("ABC \ufffd\ufffd DEF", ReadString("ABC \\ud800\\ud800 DEF"));
// invalid surrogates (two high back-to-back)
Assert.AreEqual("ABC \ufffd\ufffd\u1234 DEF", ReadString("ABC \\ud800\\ud800\\u1234 DEF"));
// invalid surrogates (three high back-to-back)
Assert.AreEqual("ABC \ufffd\ufffd\ufffd DEF", ReadString("ABC \\ud800\\ud800\\ud800 DEF"));
// invalid surrogates (high followed by a good surrogate pair)
Assert.AreEqual("ABC \ufffd\ud800\udc00 DEF", ReadString("ABC \\ud800\\ud800\\udc00 DEF"));
// invalid high surrogate at end of string
Assert.AreEqual("ABC \ufffd", ReadString("ABC \\ud800"));
// high surrogate not followed by low surrogate
Assert.AreEqual("ABC \ufffd DEF", ReadString("ABC \\ud800 DEF"));
// low surrogate not preceded by high surrogate
Assert.AreEqual("ABC \ufffd\ufffd DEF", ReadString("ABC \\udc00\\ud800 DEF"));
// make sure unencoded invalid surrogate characters don't make it through
Assert.AreEqual("\ufffd\ufffd\ufffd", ReadString("\udc00\ud800\ud800"));
Assert.AreEqual("ABC \ufffd\b", ReadString("ABC \\ud800\\b"));
Assert.AreEqual("ABC \ufffd ", ReadString("ABC \\ud800 "));
Assert.AreEqual("ABC \b\ufffd", ReadString("ABC \\b\\ud800"));
}
private string ReadString(string input)
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(@"""" + input + @""""));
JsonTextReader reader = new JsonTextReader(new StreamReader(ms));
reader.Read();
string s = (string)reader.Value;
return s;
}
[Test]
public void CloseInput()
{
@@ -2227,7 +2275,7 @@ bye", reader.Value);
Assert.IsTrue(reader.Read());
ExceptionAssert.Throws<JsonReaderException>(
"This reader's MaxDepth of 1 has been exceeded.",
"The reader's MaxDepth of 1 has been exceeded. Line 1, position 2.",
() =>
{
Assert.IsTrue(reader.Read());
@@ -1580,7 +1580,7 @@ Parameter name: arrayIndex"
[Test]
public void NumberTooBigForInt64()
{
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64.",
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Line 1, position 30.",
() =>
{
string json = @"{""code"": 307953220000517141511}";
@@ -2872,7 +2872,7 @@ To force JSON objects to deserialize add the JsonObjectAttribute to the type. Li
{
ExceptionAssert.Throws<JsonSerializationException>(
@"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.",
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Line 1, position 14.",
() =>
{
JsonTypeReflector.SetFullyTrusted(false);
@@ -5679,7 +5679,7 @@ To fix this error either change the environment to be fully trusted, change the
[Test]
[ExpectedException(typeof (JsonSerializationException)
#if !NETFX_CORE
, ExpectedMessage = "No JSON content found and type 'System.Double' is not nullable."
, ExpectedMessage = "No JSON content found and type 'System.Double' is not nullable. Line 0, position 0."
#endif
)]
public void DeserializeDoubleFromEmptyString()
@@ -5690,7 +5690,7 @@ To fix this error either change the environment to be fully trusted, change the
[Test]
[ExpectedException(typeof (JsonSerializationException)
#if !NETFX_CORE
, ExpectedMessage = "No JSON content found and type 'System.StringComparison' is not nullable."
, ExpectedMessage = "No JSON content found and type 'System.StringComparison' is not nullable. Line 0, position 0."
#endif
)]
public void DeserializeEnumFromEmptyString()
@@ -5701,7 +5701,7 @@ To fix this error either change the environment to be fully trusted, change the
[Test]
[ExpectedException(typeof (JsonSerializationException)
#if !NETFX_CORE
, ExpectedMessage = "No JSON content found and type 'System.Int32' is not nullable."
, ExpectedMessage = "No JSON content found and type 'System.Int32' is not nullable. Line 0, position 0."
#endif
)]
public void DeserializeInt32FromEmptyString()