Skip to content

Commit 699726a

Browse files
committed
feat(jsoniter): add tests for sub-range reuse with mark/reset
- Add test cases for iterator reuse with sub-ranges, including mark/reset handling. - Ensure iterators respect tail boundaries and prevent reading into stale content. - Test edge cases for truncated sub-ranges, ensuring proper rejection with exceptions.
1 parent fa0c8dc commit 699726a

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

  • json-iterator/src/test/java/systems/comodal/jsoniter

json-iterator/src/test/java/systems/comodal/jsoniter/TestIO.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,44 @@ public void close() {
5151
assertEquals(1, ji.readInt());
5252
}
5353

54+
@Test
55+
void test_sub_range_reuse_with_mark_reset() {
56+
// The websocket-router shape: one reused iterator, reset onto a sub-range
57+
// of a larger buffer whose content beyond tail is stale, with mark/reset
58+
// to revisit out-of-order fields. Nothing past tail may be read.
59+
final var doc = "{\"b\":2,\"a\":1}";
60+
final var padded = " " + doc + "{\"stale\":9}";
61+
final int head = 1;
62+
final int tail = head + doc.length();
63+
64+
final byte[] bytes = padded.getBytes();
65+
var ji = JsonIterator.parse("[]").reset(bytes, head, tail);
66+
final int mark = ji.mark();
67+
assertEquals(1, ji.skipUntil("a").readInt());
68+
ji.reset(mark);
69+
assertEquals(2, ji.skipUntil("b").readInt());
70+
// the sub-range ends after the document: seeking a missing field must not
71+
// walk into the stale content beyond tail
72+
ji.reset(mark);
73+
assertNull(ji.skipUntil("stale"));
74+
75+
final char[] chars = padded.toCharArray();
76+
ji = JsonIterator.parse(new char[]{'[', ']'}).reset(chars, head, tail);
77+
final int charMark = ji.mark();
78+
assertEquals(1, ji.skipUntil("a").readInt());
79+
ji.reset(charMark);
80+
assertEquals(2, ji.skipUntil("b").readInt());
81+
ji.reset(charMark);
82+
assertNull(ji.skipUntil("stale"));
83+
84+
// a sub-range cut mid-field rejects instead of completing from the stale
85+
// content beyond tail
86+
final var truncated = JsonIterator.parse(bytes, head, tail - 5);
87+
assertThrows(JsonException.class, () -> truncated.skipUntil("a"));
88+
final var truncatedChars = JsonIterator.parse(chars, head, tail - 5);
89+
assertThrows(JsonException.class, () -> truncatedChars.skipUntil("a"));
90+
}
91+
5492
@Test
5593
void test_utf8() {
5694
byte[] bytes = {'"', (byte) 0xe4, (byte) 0xb8, (byte) 0xad, (byte) 0xe6, (byte) 0x96, (byte) 0x87, '"'};

0 commit comments

Comments
 (0)