Skip to content

Commit

Permalink
Merge pull request #1145 from jskeet/conformance
Browse files Browse the repository at this point in the history
JSON Conformance improvements
  • Loading branch information
jskeet committed Jan 16, 2016
2 parents 1e113df + 030c268 commit d522479
Show file tree
Hide file tree
Showing 17 changed files with 537 additions and 233 deletions.
73 changes: 0 additions & 73 deletions conformance/failure_list_csharp.txt
Original file line number Diff line number Diff line change
@@ -1,89 +1,16 @@
DurationProtoInputTooLarge.JsonOutput
DurationProtoInputTooSmall.JsonOutput
FieldMaskNumbersDontRoundTrip.JsonOutput
FieldMaskPathsDontRoundTrip.JsonOutput
FieldMaskTooManyUnderscore.JsonOutput
JsonInput.AnyWithValueForInteger.JsonOutput
JsonInput.AnyWithValueForJsonObject.JsonOutput
JsonInput.BoolFieldAllCapitalFalse
JsonInput.BoolFieldAllCapitalTrue
JsonInput.BoolFieldCamelCaseFalse
JsonInput.BoolFieldCamelCaseTrue
JsonInput.BoolMapFieldKeyNotQuoted
JsonInput.BytesFieldInvalidBase64Characters
JsonInput.BytesFieldNoPadding
JsonInput.DoubleFieldInfinityNotQuoted
JsonInput.DoubleFieldNanNotQuoted
JsonInput.DoubleFieldNegativeInfinityNotQuoted
JsonInput.DoubleFieldTooLarge
JsonInput.DoubleFieldTooSmall
JsonInput.DurationHas3FractionalDigits.Validator
JsonInput.DurationHas6FractionalDigits.Validator
JsonInput.DurationHas9FractionalDigits.Validator
JsonInput.DurationMaxValue.JsonOutput
JsonInput.DurationMaxValue.ProtobufOutput
JsonInput.DurationMinValue.JsonOutput
JsonInput.DurationMinValue.ProtobufOutput
JsonInput.EnumFieldNotQuoted
JsonInput.EnumFieldNumericValueNonZero.JsonOutput
JsonInput.EnumFieldNumericValueNonZero.ProtobufOutput
JsonInput.EnumFieldNumericValueZero.JsonOutput
JsonInput.EnumFieldNumericValueZero.ProtobufOutput
JsonInput.EnumFieldUnknownValue.Validator
JsonInput.FieldMaskInvalidCharacter
JsonInput.FieldNameDuplicate
JsonInput.FieldNameDuplicateDifferentCasing2
JsonInput.FieldNameInLowerCamelCase.Validator
JsonInput.FieldNameInSnakeCase.JsonOutput
JsonInput.FieldNameInSnakeCase.ProtobufOutput
JsonInput.FieldNameNotQuoted
JsonInput.FieldNameWithMixedCases.JsonOutput
JsonInput.FieldNameWithMixedCases.ProtobufOutput
JsonInput.FieldNameWithMixedCases.Validator
JsonInput.FloatFieldInfinityNotQuoted
JsonInput.FloatFieldNanNotQuoted
JsonInput.FloatFieldNegativeInfinityNotQuoted
JsonInput.Int32FieldLeadingZero
JsonInput.Int32FieldMinFloatValue.JsonOutput
JsonInput.Int32FieldMinValue.JsonOutput
JsonInput.Int32FieldNegativeWithLeadingZero
JsonInput.Int32FieldPlusSign
JsonInput.Int32MapFieldKeyNotQuoted
JsonInput.Int64FieldMaxValueNotQuoted.JsonOutput
JsonInput.Int64FieldMaxValueNotQuoted.ProtobufOutput
JsonInput.Int64MapFieldKeyNotQuoted
JsonInput.JsonWithComments
JsonInput.MapFieldKeyIsNull
JsonInput.MapFieldValueIsNull
JsonInput.OneofFieldDuplicate
JsonInput.OriginalProtoFieldName.JsonOutput
JsonInput.OriginalProtoFieldName.ProtobufOutput
JsonInput.RepeatedBoolWrapper.ProtobufOutput
JsonInput.RepeatedDoubleWrapper.ProtobufOutput
JsonInput.RepeatedFieldMessageElementIsNull
JsonInput.RepeatedFieldPrimitiveElementIsNull
JsonInput.RepeatedFieldTrailingComma
JsonInput.RepeatedFloatWrapper.ProtobufOutput
JsonInput.RepeatedInt32Wrapper.ProtobufOutput
JsonInput.RepeatedInt64Wrapper.ProtobufOutput
JsonInput.RepeatedUint32Wrapper.ProtobufOutput
JsonInput.RepeatedUint64Wrapper.ProtobufOutput
JsonInput.StringFieldInvalidEscape
JsonInput.StringFieldSurrogateInWrongOrder
JsonInput.StringFieldSurrogatePair.JsonOutput
JsonInput.StringFieldUnpairedHighSurrogate
JsonInput.StringFieldUnpairedLowSurrogate
JsonInput.StringFieldUnterminatedEscape
JsonInput.StringFieldUppercaseEscapeLetter
JsonInput.TimestampHas3FractionalDigits.Validator
JsonInput.TimestampHas6FractionalDigits.Validator
JsonInput.TimestampHas9FractionalDigits.Validator
JsonInput.TrailingCommaInAnObject
JsonInput.Uint32MapFieldKeyNotQuoted
JsonInput.Uint64FieldMaxValueNotQuoted.JsonOutput
JsonInput.Uint64FieldMaxValueNotQuoted.ProtobufOutput
JsonInput.Uint64MapFieldKeyNotQuoted
JsonInput.ValueAcceptNull.JsonOutput
JsonInput.ValueAcceptNull.ProtobufOutput
TimestampProtoInputTooLarge.JsonOutput
TimestampProtoInputTooSmall.JsonOutput
27 changes: 19 additions & 8 deletions csharp/src/Google.Protobuf.Conformance/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,26 @@ private static ConformanceResponse PerformRequest(ConformanceRequest request, Ty
{
return new ConformanceResponse { ParseError = e.Message };
}
switch (request.RequestedOutputFormat)
catch (InvalidJsonException e)
{
case global::Conformance.WireFormat.JSON:
var formatter = new JsonFormatter(new JsonFormatter.Settings(false, typeRegistry));
return new ConformanceResponse { JsonPayload = formatter.Format(message) };
case global::Conformance.WireFormat.PROTOBUF:
return new ConformanceResponse { ProtobufPayload = message.ToByteString() };
default:
throw new Exception("Unsupported request output format: " + request.PayloadCase);
return new ConformanceResponse { ParseError = e.Message };
}
try
{
switch (request.RequestedOutputFormat)
{
case global::Conformance.WireFormat.JSON:
var formatter = new JsonFormatter(new JsonFormatter.Settings(false, typeRegistry));
return new ConformanceResponse { JsonPayload = formatter.Format(message) };
case global::Conformance.WireFormat.PROTOBUF:
return new ConformanceResponse { ProtobufPayload = message.ToByteString() };
default:
throw new Exception("Unsupported request output format: " + request.PayloadCase);
}
}
catch (InvalidOperationException e)
{
return new ConformanceResponse { SerializeError = e.Message };
}
}

Expand Down
57 changes: 43 additions & 14 deletions csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,34 +165,31 @@ public void DoubleRepresentations(double value, string expectedValueText)
}

[Test]
public void UnknownEnumValueOmitted_SingleField()
public void UnknownEnumValueNumeric_SingleField()
{
var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
AssertJson("{ }", JsonFormatter.Default.Format(message));
AssertJson("{ 'singleForeignEnum': 100 }", JsonFormatter.Default.Format(message));
}

[Test]
public void UnknownEnumValueOmitted_RepeatedField()
public void UnknownEnumValueNumeric_RepeatedField()
{
var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } };
AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 100, 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
}

[Test]
public void UnknownEnumValueOmitted_MapField()
public void UnknownEnumValueNumeric_MapField()
{
// This matches the C++ behaviour.
var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } };
AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '2': 100, '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
}

[Test]
public void UnknownEnumValueOmitted_RepeatedField_AllEntriesUnknown()
public void UnknownEnumValue_RepeatedField_AllEntriesUnknown()
{
// *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write...
// but this is what happens at the moment, and it doesn't seem too awful.
var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
AssertJson("{ 'repeatedForeignEnum': [ ] }", JsonFormatter.Default.Format(message));
AssertJson("{ 'repeatedForeignEnum': [ 200, 100 ] }", JsonFormatter.Default.Format(message));
}

[Test]
Expand Down Expand Up @@ -315,6 +312,15 @@ public void OutputIsInNumericFieldOrder_WithDefaults()

[Test]
[TestCase("1970-01-01T00:00:00Z", 0)]
[TestCase("1970-01-01T00:00:00.000000001Z", 1)]
[TestCase("1970-01-01T00:00:00.000000010Z", 10)]
[TestCase("1970-01-01T00:00:00.000000100Z", 100)]
[TestCase("1970-01-01T00:00:00.000001Z", 1000)]
[TestCase("1970-01-01T00:00:00.000010Z", 10000)]
[TestCase("1970-01-01T00:00:00.000100Z", 100000)]
[TestCase("1970-01-01T00:00:00.001Z", 1000000)]
[TestCase("1970-01-01T00:00:00.010Z", 10000000)]
[TestCase("1970-01-01T00:00:00.100Z", 100000000)]
[TestCase("1970-01-01T00:00:00.100Z", 100000000)]
[TestCase("1970-01-01T00:00:00.120Z", 120000000)]
[TestCase("1970-01-01T00:00:00.123Z", 123000000)]
Expand Down Expand Up @@ -350,6 +356,14 @@ public void TimestampField()
[TestCase(0, 0, "0s")]
[TestCase(1, 0, "1s")]
[TestCase(-1, 0, "-1s")]
[TestCase(0, 1, "0.000000001s")]
[TestCase(0, 10, "0.000000010s")]
[TestCase(0, 100, "0.000000100s")]
[TestCase(0, 1000, "0.000001s")]
[TestCase(0, 10000, "0.000010s")]
[TestCase(0, 100000, "0.000100s")]
[TestCase(0, 1000000, "0.001s")]
[TestCase(0, 10000000, "0.010s")]
[TestCase(0, 100000000, "0.100s")]
[TestCase(0, 120000000, "0.120s")]
[TestCase(0, 123000000, "0.123s")]
Expand All @@ -362,14 +376,19 @@ public void TimestampField()
[TestCase(0, -100000000, "-0.100s")]
[TestCase(1, 100000000, "1.100s")]
[TestCase(-1, -100000000, "-1.100s")]
// Non-normalized examples
[TestCase(1, 2123456789, "3.123456789s")]
[TestCase(1, -100000000, "0.900s")]
public void DurationStandalone(long seconds, int nanoseconds, string expected)
{
Assert.AreEqual(WrapInQuotes(expected), new Duration { Seconds = seconds, Nanos = nanoseconds }.ToString());
}

[Test]
[TestCase(1, 2123456789)]
[TestCase(1, -100000000)]
public void DurationStandalone_NonNormalized(long seconds, int nanoseconds)
{
Assert.Throws<InvalidOperationException>(() => new Duration { Seconds = seconds, Nanos = nanoseconds }.ToString());
}

[Test]
public void DurationField()
{
Expand All @@ -395,6 +414,16 @@ public void StructSample()
AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString());
}

[Test]
[TestCase("foo__bar")]
[TestCase("foo_3_ar")]
[TestCase("fooBar")]
public void FieldMaskInvalid(string input)
{
var mask = new FieldMask { Paths = { input } };
Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(mask));
}

[Test]
public void FieldMaskStandalone()
{
Expand Down
102 changes: 100 additions & 2 deletions csharp/src/Google.Protobuf.Test/JsonParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public void IntegerMapKeysAreStrict(string keyText)
Assert.Throws<InvalidProtocolBufferException>(() => JsonParser.Default.Parse<TestMap>(json));
}

[Test]
public void OriginalFieldNameAccepted()
{
var json = "{ \"single_int32\": 10 }";
var expected = new TestAllTypes { SingleInt32 = 10 };
Assert.AreEqual(expected, TestAllTypes.Parser.ParseJson(json));
}

[Test]
public void SourceContextRoundtrip()
{
Expand Down Expand Up @@ -116,7 +124,9 @@ public void SingularWrappers_NonDefaultValues()
[Test]
public void SingularWrappers_ExplicitNulls()
{
var message = new TestWellKnownTypes();
// When we parse the "valueField": null part, we remember it... basically, it's one case
// where explicit default values don't fully roundtrip.
var message = new TestWellKnownTypes { ValueField = Value.ForNull() };
var json = new JsonFormatter(new JsonFormatter.Settings(true)).Format(message);
var parsed = JsonParser.Default.Parse<TestWellKnownTypes>(json);
Assert.AreEqual(message, parsed);
Expand All @@ -142,6 +152,14 @@ public void Wrappers_Standalone(System.Type wrapperType, string json, object exp
Assert.AreEqual(expected, parsed);
}

[Test]
public void ExplicitNullValue()
{
string json = "{\"valueField\": null}";
var message = JsonParser.Default.Parse<TestWellKnownTypes>(json);
Assert.AreEqual(new TestWellKnownTypes { ValueField = Value.ForNull() }, message);
}

[Test]
public void BytesWrapper_Standalone()
{
Expand Down Expand Up @@ -170,6 +188,36 @@ public void RepeatedWrappers()
AssertRoundtrip(message);
}

[Test]
public void RepeatedField_NullElementProhibited()
{
string json = "{ \"repeated_foreign_message\": [null] }";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
}

[Test]
public void RepeatedField_NullOverallValueAllowed()
{
string json = "{ \"repeated_foreign_message\": null }";
Assert.AreEqual(new TestAllTypes(), TestAllTypes.Parser.ParseJson(json));
}

[Test]
[TestCase("{ \"mapInt32Int32\": { \"10\": null }")]
[TestCase("{ \"mapStringString\": { \"abc\": null }")]
[TestCase("{ \"mapInt32ForeignMessage\": { \"10\": null }")]
public void MapField_NullValueProhibited(string json)
{
Assert.Throws<InvalidProtocolBufferException>(() => TestMap.Parser.ParseJson(json));
}

[Test]
public void MapField_NullOverallValueAllowed()
{
string json = "{ \"mapInt32Int32\": null }";
Assert.AreEqual(new TestMap(), TestMap.Parser.ParseJson(json));
}

[Test]
public void IndividualWrapperTypes()
{
Expand Down Expand Up @@ -715,7 +763,6 @@ public void Duration_Valid(string jsonValue, string expectedFormatted)
[TestCase("--0.123456789s", Description = "Double minus sign")]
// Violate upper/lower bounds in various ways
[TestCase("315576000001s", Description = "Integer part too large")]
[TestCase("315576000000.000000001s", Description = "Integer part is upper bound; non-zero fraction")]
[TestCase("3155760000000s", Description = "Integer part too long (positive)")]
[TestCase("-3155760000000s", Description = "Integer part too long (negative)")]
public void Duration_Invalid(string jsonValue)
Expand All @@ -741,6 +788,14 @@ public void FieldMask_Valid(string jsonValue, params string[] expectedPaths)
CollectionAssert.AreEqual(expectedPaths, parsed.Paths);
}

[Test]
[TestCase("foo_bar")]
public void FieldMask_Invalid(string jsonValue)
{
string json = WrapInQuotes(jsonValue);
Assert.Throws<InvalidProtocolBufferException>(() => FieldMask.Parser.ParseJson(json));
}

[Test]
public void Any_RegularMessage()
{
Expand All @@ -762,6 +817,13 @@ public void Any_UnknownType()
Assert.Throws<InvalidOperationException>(() => Any.Parser.ParseJson(json));
}

[Test]
public void Any_NoTypeUrl()
{
string json = "{ \"foo\": \"bar\" }";
Assert.Throws<InvalidProtocolBufferException>(() => Any.Parser.ParseJson(json));
}

[Test]
public void Any_WellKnownType()
{
Expand Down Expand Up @@ -814,6 +876,42 @@ public void MaliciousRecursion()
Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<TestRecursiveMessage>(data64));
}

[Test]
[TestCase("AQI")]
[TestCase("_-==")]
public void Bytes_InvalidBase64(string badBase64)
{
string json = "{ \"singleBytes\": \"" + badBase64 + "\" }";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
}

[Test]
[TestCase("\"FOREIGN_BAR\"", ForeignEnum.FOREIGN_BAR)]
[TestCase("5", ForeignEnum.FOREIGN_BAR)]
[TestCase("100", (ForeignEnum) 100)]
public void EnumValid(string value, ForeignEnum expectedValue)
{
string json = "{ \"singleForeignEnum\": " + value + " }";
var parsed = TestAllTypes.Parser.ParseJson(json);
Assert.AreEqual(new TestAllTypes { SingleForeignEnum = expectedValue }, parsed);
}

[Test]
[TestCase("\"NOT_A_VALID_VALUE\"")]
[TestCase("5.5")]
public void Enum_Invalid(string value)
{
string json = "{ \"singleForeignEnum\": " + value + " }";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
}

[Test]
public void OneofDuplicate_Invalid()
{
string json = "{ \"oneofString\": \"x\", \"oneofUint32\": 10 }";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
}

/// <summary>
/// Various tests use strings which have quotes round them for parsing or as the result
/// of formatting, but without those quotes being specified in the tests (for the sake of readability).
Expand Down
Loading

0 comments on commit d522479

Please sign in to comment.