Debian Codegraph @

internal/encoder/string_scan_table_test.go

raw
1 package encoder
2
3 import (
4 "bytes"
5 "math/rand"
6 "testing"
7 "unicode/utf8"
8 )
9
10 // The tables must tell the same as the table of every byte, for every combination of the options.
11 func TestNibbleTables(t *testing.T) {
12 for index := range stringEscapes {
13 e := &stringEscapes[index]
14 for b := 0; b < 256; b++ {
15 got := e.tables.Lo[b&0xf]&e.tables.Hi[b>>4] != 0
16 if got != e.table[b] {
17 t.Fatalf("escape %d, byte %#x: expected %v but got %v", index, b, e.table[b], got)
18 }
19 }
20 }
21 }
22
23 // Every byte which the tables of the options without the normalization of UTF-8 tell to escape, and every byte of
24 // ASCII which the others tell to, has an escape sequence: the functions of the escapes write the sequence of such
25 // a byte without looking at it otherwise.
26 func TestEscapeSequences(t *testing.T) {
27 for index := range stringEscapes {
28 e := &stringEscapes[index]
29 for b := 0; b < 256; b++ {
30 if !e.table[b] || (b >= 0x80 && index&stringEscapeNormalize != 0) {
31 continue
32 }
33 if escapeSequences[b] == 0 {
34 t.Fatalf("escape %d, byte %#x: no escape sequence", index, b)
35 }
36 }
37 }
38 }
39
40 // utf8BlockEmulated does what the loop of the escapes by AVX2 does for a block of 32 bytes which has a byte which
41 // is not ASCII, with the tables, byte by byte ( see escapeUTF8AVX2 ): the block starts at the start of a
42 // character, so the bytes before it are taken as ASCII. problem is whether the block has invalid UTF-8 or U+2028
43 // or U+2029, and tail is the number of the bytes of the character which the last bytes start, if it goes on after
44 // the block.
45 func utf8BlockEmulated(b *[32]byte) (problem bool, tail int) {
46 t := &utf8ValidationTables
47 at := func(i int) byte {
48 if i < 0 {
49 return 0
50 }
51 return b[i]
52 }
53 for i := 0; i < 32; i++ {
54 prev1, prev2, prev3 := at(i-1), at(i-2), at(i-3)
55 special := t.Byte1High[prev1>>4] & t.Byte1Low[prev1&0xf] & t.Byte2High[b[i]>>4]
56 var must23 byte
57 if prev2 >= 0xe0 || prev3 >= 0xf0 {
58 must23 = 0x80
59 }
60 if special^must23 != 0 {
61 problem = true
62 }
63 if prev2 == 0xe2 && prev1 == 0x80 && b[i]&0xfe == 0xa8 {
64 problem = true
65 }
66 }
67 switch {
68 case b[31] >= 0xc0:
69 tail = 1
70 case b[30] >= 0xe0:
71 tail = 2
72 case b[29] >= 0xf0:
73 tail = 3
74 }
75 return problem, tail
76 }
77
78 // utf8ValidPrefix is whether p is the start of a valid character of UTF-8 which goes on after it.
79 func utf8ValidPrefix(p []byte) bool {
80 if len(p) == 0 {
81 return true
82 }
83 var n int
84 switch c := p[0]; {
85 case c >= 0xc2 && c <= 0xdf:
86 n = 2
87 case c >= 0xe0 && c <= 0xef:
88 n = 3
89 case c >= 0xf0 && c <= 0xf4:
90 n = 4
91 default:
92 return false
93 }
94 if len(p) >= n {
95 return false
96 }
97 for second := 0x80; second <= 0xbf; second++ {
98 r := append([]byte{}, p...)
99 if len(r) == 1 {
100 r = append(r, byte(second))
101 }
102 for len(r) < n {
103 r = append(r, 0x80)
104 }
105 if utf8.Valid(r) {
106 return true
107 }
108 }
109 return false
110 }
111
112 // checkUTF8Block checks what the loop of the escapes decides for a block against the decoder of UTF-8 of Go. A
113 // block without a problem must be valid UTF-8 without U+2028 and U+2029 up to its tail, which is not written but
114 // looked at again from its start by the next block. A block with a problem must have one: in the bytes up to the
115 // tail, or a tail which starts no valid character; else a valid string would be escaped by the loop of the bytes.
116 func checkUTF8Block(t *testing.T, b *[32]byte) {
117 t.Helper()
118 problem, tail := utf8BlockEmulated(b)
119 head := b[:32-tail]
120 headValid := utf8.Valid(head) && !bytes.Contains(head, []byte("\u2028")) && !bytes.Contains(head, []byte("\u2029"))
121 if !problem && !headValid {
122 t.Fatalf("% x: a problem is missed ( tail %d )", b[:], tail)
123 }
124 if problem && headValid && utf8ValidPrefix(b[32-tail:]) {
125 t.Fatalf("% x: a problem is found in valid UTF-8 ( tail %d )", b[:], tail)
126 }
127 }
128
129 func TestUTF8ValidationTables(t *testing.T) {
130 ascii := func() *[32]byte {
131 var b [32]byte
132 for i := range b {
133 b[i] = 'a'
134 }
135 return &b
136 }
137 // every two bytes at the start, across the halves of 16 bytes and at the end of a block.
138 for _, at := range []int{0, 15, 30} {
139 for x := 0; x < 256; x++ {
140 for y := 0; y < 256; y++ {
141 b := ascii()
142 b[at], b[at+1] = byte(x), byte(y)
143 checkUTF8Block(t, b)
144 }
145 }
146 }
147 // every three and four bytes of the bytes which decide the errors, anywhere near the halves and the end.
148 interesting := []byte{0x00, 0x41, 0x7f, 0x80, 0x8f, 0x90, 0x9f, 0xa0, 0xa8, 0xa9, 0xbf, 0xc0, 0xc1, 0xc2, 0xdf,
149 0xe0, 0xe2, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf4, 0xf5, 0xf8, 0xff}
150 for _, at := range []int{0, 13, 14, 15, 16, 28, 29} {
151 for _, x := range interesting {
152 for _, y := range interesting {
153 for _, z := range interesting {
154 b := ascii()
155 b[at], b[at+1], b[at+2] = x, y, z
156 checkUTF8Block(t, b)
157 if at+3 < 32 {
158 for _, w := range interesting {
159 b[at+3] = w
160 checkUTF8Block(t, b)
161 }
162 }
163 }
164 }
165 }
166 }
167 // blocks of characters of every length, cut anywhere, with invalid bytes among them.
168 r := rand.New(rand.NewSource(1))
169 pieces := []string{"a", "\"", "é", "ß", "あ", "語", "‧", "
", "
", "‪", "😀", "\U0010ffff",
170 "�", "\xed\x9f\xbf", "\xee\x80\x80", "\xed\xa0\x80", "\xc0\x80", "\xe0\x80\x80", "\xf0\x80\x80\x80",
171 "\xf4\x90\x80\x80", "\xf5\x80\x80\x80", "\xff", "\x80", "\xe3\x81"}
172 for n := 0; n < 1000000; n++ {
173 var s []byte
174 for len(s) < 32 {
175 if r.Intn(8) == 0 {
176 s = append(s, pieces[15+r.Intn(len(pieces)-15)]...)
177 } else {
178 s = append(s, pieces[r.Intn(15)]...)
179 }
180 }
181 var b [32]byte
182 copy(b[:], s)
183 checkUTF8Block(t, &b)
184 }
185 }
186
187 // The loop of the escapes by SIMD escapes the bytes of ASCII of the options which normalize UTF-8 by the tables of
188 // the options which don't ( see appendEscapedSIMD ): they must escape the same bytes of ASCII.
189 func TestEscapeTablesOfASCII(t *testing.T) {
190 for _, index := range []int{0, stringEscapeHTML} {
191 for b := 0; b < 0x80; b++ {
192 if stringEscapes[index].table[b] != stringEscapes[index|stringEscapeNormalize].table[b] {
193 t.Fatalf("escape %d, byte %#x: the tables differ", index, b)
194 }
195 }
196 }
197 }