Skip to content

Commit

Permalink
ground work for #253
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 13, 2021
1 parent fe2e029 commit c8bb517
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
public class CBORParserBootstrapper
{
/*
/**********************************************************
/**********************************************************************
/* Configuration
/**********************************************************
/**********************************************************************
*/

protected final IOContext _context;
protected final InputStream _in;

/*
/**********************************************************
/**********************************************************************
/* Input buffering
/**********************************************************
/**********************************************************************
*/

protected final byte[] _inputBuffer;
Expand All @@ -38,9 +38,9 @@ public class CBORParserBootstrapper
protected final boolean _bufferRecyclable;

/*
/**********************************************************
/**********************************************************************
/* Input location
/**********************************************************
/**********************************************************************
*/

/**
Expand All @@ -53,9 +53,9 @@ public class CBORParserBootstrapper
protected int _inputProcessed;

/*
/**********************************************************
/**********************************************************************
/* Life-cycle
/**********************************************************
/**********************************************************************
*/

public CBORParserBootstrapper(IOContext ctxt, InputStream in)
Expand Down Expand Up @@ -104,9 +104,9 @@ public CBORParser constructParser(int factoryFeatures,
}

/*
/**********************************************************
/**********************************************************************
/* Encoding detection for data format auto-detection
/**********************************************************
/**********************************************************************
*/

public static MatchStrength hasCBORFormat(InputAccessor acc) throws IOException
Expand Down Expand Up @@ -175,11 +175,11 @@ public static MatchStrength hasCBORFormat(InputAccessor acc) throws IOException
}
return MatchStrength.INCONCLUSIVE;
}

/*
/**********************************************************
/**********************************************************************
/* Internal methods, raw input access
/**********************************************************
/**********************************************************************
*/

protected boolean ensureLoaded(int minimum) throws IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package com.fasterxml.jackson.dataformat.cbor.parse;

import java.lang.reflect.Field;
import java.util.Random;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;

import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;

public class SymbolTableTest extends CBORTestBase
{
static class Point {
public int x, y;
}

private final CBORMapper NO_CAN_MAPPER = CBORMapper.builder(CBORFactory.builder()
.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)
.build())
.build();

public void testSimpleDefault() throws Exception
{
final CBORMapper vanillaMapper = cborMapper();
Expand All @@ -21,6 +33,8 @@ public void testSimpleDefault() throws Exception
// First: should have empty symbol table
try (JsonParser p = vanillaMapper.createParser(doc)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
assertTrue(syms.isCanonicalizing()); // added in 2.13

assertEquals(0, syms.size());
assertEquals(0, _findParent(syms).size());

Expand Down Expand Up @@ -66,22 +80,146 @@ public void testSimpleDefault() throws Exception
}
}

// !!! TODO:
// [dataformats-binary#253]: should be able to prevent canonicalization
// Assumption: there is still non-null symbol table, but has "no canonicalization"
public void testNoCanonicalizeWithMapper() throws Exception
{
final byte[] doc = cborDoc(a2q("{ 'x':13, 'y':-999}"));
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
Point point = NO_CAN_MAPPER.readValue(p, Point.class);
assertEquals(13, point.x);
assertEquals(-999, point.y);
}
}

// [dataformats-binary#252]: should be able to prevent canonicalization
public void testSimpleNoCanonicalize() throws Exception
{
final byte[] doc = cborDoc("{\"a\":1,\"b\":2}");
final CBORMapper mapper = CBORMapper.builder(CBORFactory.builder()
.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)
.build())
.build();
// !!! TODO: enable
if (true) {
return;
}
final String[] fieldNames = new String[] {
// Ascii, various lengths
"abc", "abcd123", "abcdefghi123940963", "",
// Unicode, also (2-byte ones ought to be ok)
"F\u00F6\u00F6", "F\u00F6\u00F6bar", "Longer F\u00F6\u00F6bar",

// and then couple of longer names; total needs to exceed 64k
generateName(77),
generateName(2000),
generateName(17000),
generateName(23000),
generateName(33033),
"end", // just simple end marker
};
final byte[] doc = cborDoc(jsonFrom(fieldNames));

try (JsonParser p = mapper.createParser(doc)) {
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
ByteQuadsCanonicalizer syms = _findSymbols(p);
assertEquals(0, syms.size());
assertFalse(syms.isCanonicalizing()); // added in 2.13
assertEquals(-1, syms.size());
// also, should not have parent:
assertNull(_findParent(syms));

assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals(fieldNames[0], p.currentName());
// should NOT add
assertEquals(-1, syms.size());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(0, p.getIntValue());

// and from thereon...
for (int i = 1; i < fieldNames.length; ++i) {
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals(fieldNames[i], p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(i, p.getIntValue());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());

assertEquals(-1, syms.size());
}

// But let's also try other accessors: first, nextName()
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertEquals(fieldNames[0], p.nextFieldName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(0, p.getIntValue());

// and from thereon...
for (int i = 1; i < fieldNames.length; ++i) {
assertEquals(fieldNames[i], p.nextFieldName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(i, p.getIntValue());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());

assertEquals(-1, _findSymbols(p).size());
}

// and then nextName(match)
try (JsonParser p = NO_CAN_MAPPER.createParser(doc)) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertTrue(p.nextFieldName(new SerializedString(fieldNames[0])));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(0, p.getIntValue());

// and then negative match
assertFalse(p.nextFieldName(new SerializedString("bogus")));
assertEquals(fieldNames[1], p.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1, p.getIntValue());

// and from thereon...
for (int i = 2; i < fieldNames.length; ++i) {
assertEquals(fieldNames[i], p.nextFieldName());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(i, p.getIntValue());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.nextToken());

assertEquals(-1, _findSymbols(p).size());
}
}


private String jsonFrom(String... fields) {
StringBuilder sb = new StringBuilder();
sb.append("{");

for (int i = 0, len = fields.length; i < len; ++i) {
if (i > 0) {
sb.append(",\n");
}
sb.append('"').append(fields[i]).append('"')
.append(" : ").append(i);
}
sb.append("}");
return sb.toString();
}

private String generateName(int minLen)
{
StringBuilder sb = new StringBuilder();
Random rnd = new Random(123);
while (sb.length() < minLen) {
int ch = rnd.nextInt(96);
if (ch < 32) { // ascii (single byte)
sb.append((char) (48 + ch));
} else if (ch < 64) { // 2 byte
sb.append((char) (128 + ch));
} else { // 3 byte
sb.append((char) (4000 + ch));
}
}
return sb.toString();
}

// Helper method to dig up symbol table reference for tests, without
// exposing it to user code
private ByteQuadsCanonicalizer _findSymbols(JsonParser p) throws Exception
Expand Down

0 comments on commit c8bb517

Please sign in to comment.