| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
"unsafe" |
| 8 |
) |
| 9 |
|
| 10 |
// referenceMasks computes the masks of a block one byte at a time. |
| 11 |
func referenceMasks(block []byte) scanMasks { |
| 12 |
var m scanMasks |
| 13 |
for i, c := range block { |
| 14 |
bit := uint64(1) << uint(i) |
| 15 |
switch c { |
| 16 |
case '"': |
| 17 |
m.quote |= bit |
| 18 |
case '\\': |
| 19 |
m.backslash |= bit |
| 20 |
case '{', '[': |
| 21 |
m.open |= bit |
| 22 |
case '}', ']': |
| 23 |
m.closing |= bit |
| 24 |
} |
| 25 |
} |
| 26 |
return m |
| 27 |
} |
| 28 |
|
| 29 |
func randomBlock(r *rand.Rand) []byte { |
| 30 |
const chars = `{}[]"\abc01 ,:{}[]"\\\` |
| 31 |
block := make([]byte, scanBlockSize) |
| 32 |
for i := range block { |
| 33 |
if r.Intn(8) == 0 { |
| 34 |
block[i] = byte(r.Intn(256)) |
| 35 |
} else { |
| 36 |
block[i] = chars[r.Intn(len(chars))] |
| 37 |
} |
| 38 |
} |
| 39 |
return block |
| 40 |
} |
| 41 |
|
| 42 |
func TestScanBlock(t *testing.T) { |
| 43 |
r := rand.New(rand.NewSource(1)) |
| 44 |
for i := 0; i < 10000; i++ { |
| 45 |
block := randomBlock(r) |
| 46 |
want := referenceMasks(block) |
| 47 |
var got scanMasks |
| 48 |
scanBlock(unsafe.Pointer(&block[0]), &got) |
| 49 |
if got != want { |
| 50 |
t.Fatalf("scanBlock(%q) = %+v, want %+v", block, got, want) |
| 51 |
} |
| 52 |
scanBlockWords(unsafe.Pointer(&block[0]), &got) |
| 53 |
if got != want { |
| 54 |
t.Fatalf("scanBlockWords(%q) = %+v, want %+v", block, got, want) |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// referenceSkipCompound finds the end of the object or the array open at cursor one byte at a time. |
| 60 |
// A byte after a backslash is escaped wherever it is: it is neither a quote nor a bracket. |
| 61 |
func referenceSkipCompound(buf []byte, cursor, depth int64) (int64, bool) { |
| 62 |
inString, escaped := false, false |
| 63 |
for pos := cursor; pos < int64(len(buf)); pos++ { |
| 64 |
c := buf[pos] |
| 65 |
switch { |
| 66 |
case escaped: |
| 67 |
escaped = false |
| 68 |
continue |
| 69 |
case c == '\\': |
| 70 |
escaped = true |
| 71 |
continue |
| 72 |
case inString: |
| 73 |
if c == '"' { |
| 74 |
inString = false |
| 75 |
} |
| 76 |
continue |
| 77 |
} |
| 78 |
switch c { |
| 79 |
case '"': |
| 80 |
inString = true |
| 81 |
case '{', '[': |
| 82 |
depth++ |
| 83 |
case '}', ']': |
| 84 |
depth-- |
| 85 |
if depth == 0 { |
| 86 |
return pos + 1, true |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
return int64(len(buf)), false |
| 91 |
} |
| 92 |
|
| 93 |
// randomValue returns a random object or array, well formed or not, with strings, escapes and |
| 94 |
// random bytes, of a length which crosses the blocks in every way. |
| 95 |
func randomValue(r *rand.Rand) []byte { |
| 96 |
var b strings.Builder |
| 97 |
n := r.Intn(300) |
| 98 |
pieces := []string{`{`, `}`, `[`, `]`, `"`, `\`, `\"`, `\\`, `\\\`, `"a"`, `"\"b\""`, `:`, `,`, `1`, ` `, "\n", `"\\"`, `"\\\"`} |
| 99 |
b.WriteByte("{["[r.Intn(2)]) |
| 100 |
for i := 0; i < n; i++ { |
| 101 |
if r.Intn(10) == 0 { |
| 102 |
b.WriteByte(byte(r.Intn(256))) |
| 103 |
} else { |
| 104 |
b.WriteString(pieces[r.Intn(len(pieces))]) |
| 105 |
} |
| 106 |
} |
| 107 |
return []byte(b.String()) |
| 108 |
} |
| 109 |
|
| 110 |
func TestScanCompoundAgainstReference(t *testing.T) { |
| 111 |
r := rand.New(rand.NewSource(2)) |
| 112 |
for i := 0; i < 20000; i++ { |
| 113 |
value := randomValue(r) |
| 114 |
buf := NewInput(value) |
| 115 |
wantEnd, wantFound := referenceSkipCompound(buf, 1, 1) |
| 116 |
sc := compoundScanner{depth: 1, maxDepth: maxDecodeNestingDepth} |
| 117 |
gotEnd, gotFound, err := sc.scan(buf, 1, int64(len(buf))) |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("%q: %v", value, err) |
| 120 |
} |
| 121 |
if gotFound != wantFound || (gotFound && gotEnd != wantEnd) { |
| 122 |
t.Fatalf("%q: got (%d, %v), want (%d, %v)", value, gotEnd, gotFound, wantEnd, wantFound) |
| 123 |
} |
| 124 |
// The same value scanned in pieces, as a stream does, ends at the same place. |
| 125 |
sc = compoundScanner{depth: 1, maxDepth: maxDecodeNestingDepth} |
| 126 |
pos := int64(1) |
| 127 |
for pos < int64(len(buf)) { |
| 128 |
lim := min(pos+int64(r.Intn(70)), int64(len(buf))) |
| 129 |
end, found, err := sc.scan(buf, pos, lim) |
| 130 |
if err != nil { |
| 131 |
t.Fatalf("%q: %v", value, err) |
| 132 |
} |
| 133 |
if found { |
| 134 |
if !wantFound || end != wantEnd { |
| 135 |
t.Fatalf("%q in pieces: got %d, want (%d, %v)", value, end, wantEnd, wantFound) |
| 136 |
} |
| 137 |
break |
| 138 |
} |
| 139 |
if wantFound && wantEnd <= lim { |
| 140 |
t.Fatalf("%q in pieces: not found before %d, want %d", value, lim, wantEnd) |
| 141 |
} |
| 142 |
pos = end |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestScanCompoundMaxDepth(t *testing.T) { |
| 148 |
for _, n := range []int{9999, 10000, 10001} { |
| 149 |
buf := NewInput([]byte(strings.Repeat("[", n) + strings.Repeat("]", n))) |
| 150 |
_, err := skipValue(buf, 0, 0) |
| 151 |
if (err != nil) != (n > maxDecodeNestingDepth) { |
| 152 |
t.Fatalf("%d brackets: err = %v", n, err) |
| 153 |
} |
| 154 |
_, err = skipValue(buf, 0, 1) |
| 155 |
if (err != nil) != (n+1 > maxDecodeNestingDepth) { |
| 156 |
t.Fatalf("%d brackets nested once: err = %v", n, err) |
| 157 |
} |
| 158 |
} |
| 159 |
} |