Skip to content

Commit

Permalink
Add bit more testing for names with null bytes (inspired by [dataform…
Browse files Browse the repository at this point in the history
…ats-binary#312])
  • Loading branch information
cowtowncoder committed Feb 26, 2022
1 parent f3f1f78 commit 2b65ea1
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,68 @@ private void _testSymbolsWithNull(JsonFactory f, boolean useBytes) throws Except
assertToken(JsonToken.END_OBJECT, parser.nextToken());
parser.close();
}

// // Additional testing inspired by [dataformats-binary#312]; did not
// // affect JSON backend but wanted to ensure

public void testSymbolsWithNullOnlyNameBytes() throws Exception {
JsonFactory f = new JsonFactory();
_testSymbolsWithNullOnlyNameBytes(f, true);
// and repeat with same factory, just for fun, and to ensure symbol table is fine
_testSymbolsWithNullOnlyNameBytes(f, true);
}

public void testSymbolsWithNullOnlyNameChars() throws Exception {
JsonFactory f = new JsonFactory();
_testSymbolsWithNullOnlyNameBytes(f, false);
_testSymbolsWithNullOnlyNameBytes(f, false);
}

private void _testSymbolsWithNullOnlyNameBytes(JsonFactory f, boolean useBytes) throws Exception
{
final String FIELD1 = "\u0000";
final String FIELD2 = FIELD1 + FIELD1;
final String FIELD3 = FIELD2 + FIELD1;
final String FIELD4 = FIELD3 + FIELD1;
final String QUOTED_NULL = "\\u0000";

final String INPUT = a2q(String.format("{'%s':1, '%s':2, '%s':3, '%s':4}",
QUOTED_NULL, QUOTED_NULL + QUOTED_NULL,
QUOTED_NULL + QUOTED_NULL + QUOTED_NULL,
QUOTED_NULL + QUOTED_NULL + QUOTED_NULL + QUOTED_NULL
));
JsonParser p = useBytes ? f.createParser(INPUT.getBytes("UTF-8"))
: f.createParser(INPUT);

assertToken(JsonToken.START_OBJECT, p.nextToken());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
_assertNullStrings(FIELD1, p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1, p.getIntValue());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
_assertNullStrings(FIELD2, p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(2, p.getIntValue());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
_assertNullStrings(FIELD3, p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(3, p.getIntValue());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
_assertNullStrings(FIELD4, p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(4, p.getIntValue());

assertToken(JsonToken.END_OBJECT, p.nextToken());
}

private void _assertNullStrings(String exp, String actual) {
if (exp.length() != actual.length()) {
fail("Expected "+exp.length()+" nulls, got "+actual.length());
}
assertEquals(exp, actual);
}
}

0 comments on commit 2b65ea1

Please sign in to comment.