| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"math/bits" |
| 6 |
"math/rand" |
| 7 |
"testing" |
| 8 |
"unicode/utf8" |
| 9 |
) |
| 10 |
|
| 11 |
// firstByteOf returns the position in the word of its first byte which is special, or 8 if it has none. |
| 12 |
func firstByteOf(w uint64, special func(c byte) bool) int { |
| 13 |
for i := 0; i < 8; i++ { |
| 14 |
if special(byte(w >> (8 * i))) { |
| 15 |
return i |
| 16 |
} |
| 17 |
} |
| 18 |
return 8 |
| 19 |
} |
| 20 |
|
| 21 |
func TestKeyEndMasks(t *testing.T) { |
| 22 |
// The first byte of the masks is the first byte which ends a simple key ( specialKeyBytes ) or a string |
| 23 |
// ( keyEndBytes ): the bytes after it may be wrong. |
| 24 |
// Every byte at every position, followed and preceded by random bytes, and random words whose bytes are |
| 25 |
// mostly the ones of keys. |
| 26 |
r := rand.New(rand.NewSource(1)) |
| 27 |
var b [8]byte |
| 28 |
check := func() { |
| 29 |
t.Helper() |
| 30 |
w := binary.LittleEndian.Uint64(b[:]) |
| 31 |
if got, want := bits.TrailingZeros64(specialKeyBytes(w))/8, firstByteOf(w, func(c byte) bool { |
| 32 |
return c == '"' || c == '\\' || c < 0x20 || c >= 0x80 |
| 33 |
}); got != want { |
| 34 |
t.Fatalf("specialKeyBytes %q: got %d, want %d", b[:], got, want) |
| 35 |
} |
| 36 |
if got, want := bits.TrailingZeros64(keyEndBytes(w))/8, firstByteOf(w, func(c byte) bool { |
| 37 |
return c == '"' || c == '\\' || c < 0x20 |
| 38 |
}); got != want { |
| 39 |
t.Fatalf("keyEndBytes %q: got %d, want %d", b[:], got, want) |
| 40 |
} |
| 41 |
} |
| 42 |
for pos := 0; pos < 8; pos++ { |
| 43 |
for c := 0; c < 256; c++ { |
| 44 |
for i := 0; i < 16; i++ { |
| 45 |
for j := range b { |
| 46 |
switch { |
| 47 |
case j < pos: |
| 48 |
b[j] = byte(0x20 + r.Intn(0x60)) |
| 49 |
case j == pos: |
| 50 |
b[j] = byte(c) |
| 51 |
default: |
| 52 |
b[j] = byte(r.Intn(256)) |
| 53 |
} |
| 54 |
} |
| 55 |
check() |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
chars := []byte("aZ09_-\"\\\x00\x1f\x20\x7f\x80\xff") |
| 60 |
for i := 0; i < 1000000; i++ { |
| 61 |
for j := range b { |
| 62 |
b[j] = chars[r.Intn(len(chars))] |
| 63 |
} |
| 64 |
check() |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestStructKeysShortWithNul(t *testing.T) { |
| 69 |
// A key of a field with a nul byte is not found by a shorter key whose word is the same. |
| 70 |
d := newStructDecoder("", "") |
| 71 |
withNul := &structFieldSet{key: "a\x00"} |
| 72 |
plain := &structFieldSet{key: "b"} |
| 73 |
d.setFields([]*structFieldSet{withNul, plain}) |
| 74 |
for _, tc := range []struct { |
| 75 |
in string |
| 76 |
want *structFieldSet |
| 77 |
}{ |
| 78 |
{`"a":1`, nil}, |
| 79 |
{`"A":1`, nil}, |
| 80 |
{`"b":1`, plain}, |
| 81 |
{`"B":1`, plain}, |
| 82 |
{`"a\u0000":1`, withNul}, |
| 83 |
} { |
| 84 |
buf := make([]byte, len(tc.in)+1, len(tc.in)+64) |
| 85 |
copy(buf, tc.in) |
| 86 |
field, _, err := d.decodeKey(buf, 0, false) |
| 87 |
if err != nil || field != tc.want { |
| 88 |
t.Fatalf("%s: got %v %v, want %v", tc.in, field, err, tc.want) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestStructKeysMayFold(t *testing.T) { |
| 94 |
// mayFold tells by the bytes of a key what mayFoldDecoded tells by its runes, for a key of valid UTF-8. |
| 95 |
var fields []*structFieldSet |
| 96 |
for _, key := range []string{"k", "s", "名前", "Ärger", "ſ", "Ωmega", "𝔸x"} { |
| 97 |
fields = append(fields, &structFieldSet{key: key}) |
| 98 |
} |
| 99 |
k := newStructKeys(fields) |
| 100 |
if k.hasRuneError { |
| 101 |
t.Fatal("no key has utf8.RuneError") |
| 102 |
} |
| 103 |
r := rand.New(rand.NewSource(1)) |
| 104 |
runes := []rune{'a', 'K', 'S', 'K', 'ſ', '名', '前', '後', 'Ä', 'ä', 'Ω', 'ω', 'Ω', '𝔸', '𝔹', 'é', 'ÿ', 'Ā'} |
| 105 |
for i := 0; i < 100000; i++ { |
| 106 |
var key []byte |
| 107 |
for j := r.Intn(4) + 1; j > 0; j-- { |
| 108 |
if r.Intn(4) == 0 { |
| 109 |
key = utf8.AppendRune(key, rune(r.Intn(0x10ffff))) |
| 110 |
} else { |
| 111 |
key = utf8.AppendRune(key, runes[r.Intn(len(runes))]) |
| 112 |
} |
| 113 |
} |
| 114 |
if got, want := k.mayFold(key), k.mayFoldDecoded(key); got != want { |
| 115 |
t.Fatalf("%q: got %v, want %v", key, got, want) |
| 116 |
} |
| 117 |
// no check rejects a key which folds to a key of a field |
| 118 |
var w [8]byte |
| 119 |
copy(w[:], key) |
| 120 |
w0 := binary.LittleEndian.Uint64(w[:]) |
| 121 |
if k.find(appendFoldedKey(nil, key)) != nil && !(k.leadMayFold(w0) && k.firstRuneMayFold(key, w0) && k.mayFold(key)) { |
| 122 |
t.Fatalf("%q: the key of a field is rejected", key) |
| 123 |
} |
| 124 |
} |
| 125 |
// a key of runes which fold to a key may be of it |
| 126 |
for _, key := range []string{"K", "ſ", "名前", "äRGER", "Ωmega", "𝔸X"} { |
| 127 |
if !k.mayFold([]byte(key)) { |
| 128 |
t.Fatalf("%q: not found", key) |
| 129 |
} |
| 130 |
} |
| 131 |
} |