|
| 1 | +package com.fasterxml.jackson.dataformat.cbor; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.JsonParser; |
| 8 | +import com.fasterxml.jackson.core.JsonToken; |
| 9 | +import com.fasterxml.jackson.core.io.SerializedString; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests for [dataformats-binary#735]: the Object property name paths used to pass |
| 15 | + * the 5-bit length marker, instead of the name's actual byte length, to |
| 16 | + * {@code shouldReferenceString()}. Markers 24 - 27 ("1/2/4/8-byte length suffix |
| 17 | + * follows") are all above every minimum-length threshold, so a name shorter than |
| 18 | + * the threshold got registered in the "stringref" table when a conformant encoder |
| 19 | + * would have skipped it -- shifting all following reference indexes by one. |
| 20 | + *<p> |
| 21 | + * Only reachable for non-canonically encoded length prefixes: canonical marker 24 |
| 22 | + * implies length >= 24, marker 25 length >= 256, and so on, all above the |
| 23 | + * thresholds. Jackson's own generator always writes minimal prefixes, so these |
| 24 | + * documents are hand-crafted. |
| 25 | + */ |
| 26 | +public class StringRef735Test extends CBORTestBase |
| 27 | +{ |
| 28 | + // How the name is to be read. Modes 1 and 2 exercise the two paths that |
| 29 | + // passed `lenMarker`; mode 3's fast path always used the real byte length, |
| 30 | + // and is included to keep it that way (it only reaches the shared paths for |
| 31 | + // markers above 24, which it does not handle itself) |
| 32 | + private final static int MODE_NEXT_TOKEN = 1; |
| 33 | + private final static int MODE_NEXT_FIELD_NAME = 2; |
| 34 | + private final static int MODE_NEXT_FIELD_NAME_MATCH = 3; |
| 35 | + |
| 36 | + /* |
| 37 | + /********************************************************** |
| 38 | + /* Test methods |
| 39 | + /********************************************************** |
| 40 | + */ |
| 41 | + |
| 42 | + // 2-byte name written with a 1-byte length suffix (marker 24): below the |
| 43 | + // 3-byte minimum for index #0, so a conformant encoder does NOT give it an |
| 44 | + // index -- making "AAA" entry #0, and reference #0 resolve to "AAA" |
| 45 | + @Test |
| 46 | + public void testShortNameWithLongMarkerNotReferenced() throws Exception |
| 47 | + { |
| 48 | + final String name = "ab"; |
| 49 | + final byte[] doc = _doc(name, _nonCanonical1ByteLen(name)); |
| 50 | + |
| 51 | + _verifyAllModes(doc, name, "AAA"); |
| 52 | + } |
| 53 | + |
| 54 | + // Same, but with a 2-byte length suffix (marker 25) for a 2-byte name |
| 55 | + @Test |
| 56 | + public void testShortNameWith2ByteMarkerNotReferenced() throws Exception |
| 57 | + { |
| 58 | + final String name = "ab"; |
| 59 | + final byte[] doc = _doc(name, _nonCanonical2ByteLen(name)); |
| 60 | + |
| 61 | + _verifyAllModes(doc, name, "AAA"); |
| 62 | + } |
| 63 | + |
| 64 | + // Conversely: a name that IS long enough must still be registered. Here the |
| 65 | + // name is entry #0 and "AAA" entry #1, so reference #0 is the name itself -- |
| 66 | + // verifies the fix did not simply stop registering names |
| 67 | + @Test |
| 68 | + public void testLongEnoughNameStillReferenced() throws Exception |
| 69 | + { |
| 70 | + final String name = generateAsciiString(30); // canonical marker 24 |
| 71 | + final byte[] doc = _doc(name, _nonCanonical1ByteLen(name)); |
| 72 | + |
| 73 | + _verifyAllModes(doc, name, name); |
| 74 | + } |
| 75 | + |
| 76 | + // Exactly at the 3-byte threshold for index #0: registered either way, but |
| 77 | + // worth pinning since it is the boundary the marker value used to mask |
| 78 | + @Test |
| 79 | + public void testNameAtThresholdReferenced() throws Exception |
| 80 | + { |
| 81 | + final String name = "abc"; |
| 82 | + final byte[] doc = _doc(name, _nonCanonical1ByteLen(name)); |
| 83 | + |
| 84 | + _verifyAllModes(doc, name, name); |
| 85 | + } |
| 86 | + |
| 87 | + // Chunked (indefinite length) names are never referenced, no matter how long |
| 88 | + // they are: the decoded length is not known when the marker is read, and a |
| 89 | + // conformant encoder only indexes definite-length strings. So "AAA" is entry |
| 90 | + // #0 here even though the name is 4 bytes |
| 91 | + @Test |
| 92 | + public void testChunkedNameNotReferenced() throws Exception |
| 93 | + { |
| 94 | + final byte[] doc = _chunkedNameDoc("ab", "cd"); |
| 95 | + |
| 96 | + _verifyAllModes(doc, "abcd", "AAA"); |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + /********************************************************** |
| 101 | + /* Helper methods, document construction |
| 102 | + /********************************************************** |
| 103 | + */ |
| 104 | + |
| 105 | + /** |
| 106 | + * Builds document |
| 107 | + *<pre> |
| 108 | + * tag(256) [ { <name> : "AAA" }, tag(25) 0 ] |
| 109 | + *</pre> |
| 110 | + * with the property name encoded using the given (possibly non-canonical) |
| 111 | + * length prefix. Reference is always to entry #0, which is what makes the |
| 112 | + * tests sensitive to whether the name took up an index: if it did, #0 is the |
| 113 | + * name, and if it did not, #0 is the {@code "AAA"} value. |
| 114 | + */ |
| 115 | + private byte[] _doc(String name, byte[] namePrefix) throws Exception |
| 116 | + { |
| 117 | + final byte[] rawName = utf8Bytes(name); |
| 118 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 119 | + b.write(0xD9); b.write(0x01); b.write(0x00); // tag 256, "stringref-namespace" |
| 120 | + b.write(0x82); // Array, 2 elements |
| 121 | + b.write(0xA1); // Object, 1 entry |
| 122 | + b.write(namePrefix, 0, namePrefix.length); |
| 123 | + b.write(rawName, 0, rawName.length); |
| 124 | + b.write(0x63); b.write('A'); b.write('A'); b.write('A'); |
| 125 | + b.write(0xD8); b.write(0x19); b.write(0x00); // tag 25, "stringref" to entry #0 |
| 126 | + return b.toByteArray(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Same shape as {@link #_doc}, but with the property name written as chunked |
| 131 | + * (indefinite length) text made up of the given chunks. |
| 132 | + */ |
| 133 | + private byte[] _chunkedNameDoc(String... chunks) throws Exception |
| 134 | + { |
| 135 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 136 | + b.write(0xD9); b.write(0x01); b.write(0x00); // tag 256, "stringref-namespace" |
| 137 | + b.write(0x82); // Array, 2 elements |
| 138 | + b.write(0xA1); // Object, 1 entry |
| 139 | + b.write(0x7F); // text, indefinite length |
| 140 | + for (String chunk : chunks) { |
| 141 | + final byte[] raw = utf8Bytes(chunk); |
| 142 | + b.write(0x60 + raw.length); // text, length in type byte |
| 143 | + b.write(raw, 0, raw.length); |
| 144 | + } |
| 145 | + b.write(0xFF); // break |
| 146 | + b.write(0x63); b.write('A'); b.write('A'); b.write('A'); |
| 147 | + b.write(0xD8); b.write(0x19); b.write(0x00); // tag 25, "stringref" to entry #0 |
| 148 | + return b.toByteArray(); |
| 149 | + } |
| 150 | + |
| 151 | + // Length prefix using marker 24, "1-byte length suffix follows" |
| 152 | + private byte[] _nonCanonical1ByteLen(String name) { |
| 153 | + final int len = utf8Bytes(name).length; |
| 154 | + return new byte[] { (byte) 0x78, (byte) len }; |
| 155 | + } |
| 156 | + |
| 157 | + // Length prefix using marker 25, "2-byte length suffix follows" |
| 158 | + private byte[] _nonCanonical2ByteLen(String name) { |
| 159 | + final int len = utf8Bytes(name).length; |
| 160 | + return new byte[] { (byte) 0x79, (byte) (len >> 8), (byte) len }; |
| 161 | + } |
| 162 | + |
| 163 | + /* |
| 164 | + /********************************************************** |
| 165 | + /* Helper methods, verification |
| 166 | + /********************************************************** |
| 167 | + */ |
| 168 | + |
| 169 | + // Runs every read mode, reporting all failures: the two name-decoding paths |
| 170 | + // (`_decodePropertyName()` and the one inlined in `nextFieldName()`) had |
| 171 | + // separate copies of the faulty check, so each needs its own coverage |
| 172 | + private void _verifyAllModes(byte[] doc, String expName, String expRef) |
| 173 | + { |
| 174 | + assertAll( |
| 175 | + () -> _verifyRef(doc, expName, expRef, MODE_NEXT_TOKEN), |
| 176 | + () -> _verifyRef(doc, expName, expRef, MODE_NEXT_FIELD_NAME), |
| 177 | + () -> _verifyRef(doc, expName, expRef, MODE_NEXT_FIELD_NAME_MATCH)); |
| 178 | + } |
| 179 | + |
| 180 | + private void _verifyRef(byte[] doc, String expName, String expRef, int mode) |
| 181 | + throws Exception |
| 182 | + { |
| 183 | + final String desc = "(mode: "+mode+")"; |
| 184 | + try (JsonParser p = cborParser(doc)) { |
| 185 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 186 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 187 | + _advanceToName(p, expName, mode); |
| 188 | + assertToken(JsonToken.FIELD_NAME, p.currentToken()); |
| 189 | + assertEquals(expName, p.currentName(), desc); |
| 190 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 191 | + assertEquals("AAA", p.getText(), desc); |
| 192 | + assertToken(JsonToken.END_OBJECT, p.nextToken()); |
| 193 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 194 | + assertEquals(expRef, p.getText(), desc); |
| 195 | + assertToken(JsonToken.END_ARRAY, p.nextToken()); |
| 196 | + assertNull(p.nextToken()); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private void _advanceToName(JsonParser p, String expName, int mode) |
| 201 | + throws Exception |
| 202 | + { |
| 203 | + switch (mode) { |
| 204 | + case MODE_NEXT_TOKEN: |
| 205 | + assertToken(JsonToken.FIELD_NAME, p.nextToken()); |
| 206 | + break; |
| 207 | + case MODE_NEXT_FIELD_NAME: |
| 208 | + assertEquals(expName, p.nextFieldName()); |
| 209 | + break; |
| 210 | + case MODE_NEXT_FIELD_NAME_MATCH: |
| 211 | + assertTrue(p.nextFieldName(new SerializedString(expName)), |
| 212 | + "Should match name '"+expName+"'"); |
| 213 | + break; |
| 214 | + default: |
| 215 | + fail("Unknown mode: "+mode); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments