| 1 |
package utf8 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"unicode/utf8" |
| 10 |
|
| 11 |
"github.com/segmentio/asm/ascii" |
| 12 |
"github.com/segmentio/asm/internal/buffer" |
| 13 |
) |
| 14 |
|
| 15 |
type byteRange struct { |
| 16 |
Low byte |
| 17 |
High byte |
| 18 |
} |
| 19 |
|
| 20 |
func one(b byte) byteRange { |
| 21 |
return byteRange{b, b} |
| 22 |
} |
| 23 |
|
| 24 |
func genExamples(current string, ranges []byteRange) []string { |
| 25 |
if len(ranges) == 0 { |
| 26 |
return []string{string(current)} |
| 27 |
} |
| 28 |
r := ranges[0] |
| 29 |
var all []string |
| 30 |
|
| 31 |
elements := []byte{r.Low, r.High} |
| 32 |
|
| 33 |
mid := (r.High + r.Low) / 2 |
| 34 |
if mid != r.Low && mid != r.High { |
| 35 |
elements = append(elements, mid) |
| 36 |
} |
| 37 |
|
| 38 |
for _, x := range elements { |
| 39 |
s := current + string(x) |
| 40 |
all = append(all, genExamples(s, ranges[1:])...) |
| 41 |
if x == r.High { |
| 42 |
break |
| 43 |
} |
| 44 |
} |
| 45 |
return all |
| 46 |
} |
| 47 |
|
| 48 |
func TestValid(t *testing.T) { |
| 49 |
var examples = []string{ |
| 50 |
// Tests copied from the stdlib |
| 51 |
"", |
| 52 |
"a", |
| 53 |
"abc", |
| 54 |
"Ж", |
| 55 |
"ЖЖ", |
| 56 |
"брэд-ЛГТМ", |
| 57 |
"☺☻☹", |
| 58 |
|
| 59 |
// overlong |
| 60 |
"\xE0\x80", |
| 61 |
// unfinished continuation |
| 62 |
"aa\xE2", |
| 63 |
|
| 64 |
string([]byte{66, 250}), |
| 65 |
|
| 66 |
string([]byte{66, 250, 67}), |
| 67 |
|
| 68 |
"a\uFFFDb", |
| 69 |
|
| 70 |
"\xF4\x8F\xBF\xBF", // U+10FFFF |
| 71 |
|
| 72 |
"\xF4\x90\x80\x80", // U+10FFFF+1; out of range |
| 73 |
"\xF7\xBF\xBF\xBF", // 0x1FFFFF; out of range |
| 74 |
|
| 75 |
"\xFB\xBF\xBF\xBF\xBF", // 0x3FFFFFF; out of range |
| 76 |
|
| 77 |
"\xc0\x80", // U+0000 encoded in two bytes: incorrect |
| 78 |
"\xed\xa0\x80", // U+D800 high surrogate (sic) |
| 79 |
"\xed\xbf\xbf", // U+DFFF low surrogate (sic) |
| 80 |
|
| 81 |
// valid at boundary |
| 82 |
strings.Repeat("a", 32+28) + "☺☻☹", |
| 83 |
strings.Repeat("a", 32+29) + "☺☻☹", |
| 84 |
strings.Repeat("a", 32+30) + "☺☻☹", |
| 85 |
strings.Repeat("a", 32+31) + "☺☻☹", |
| 86 |
// invalid at boundary |
| 87 |
strings.Repeat("a", 32+31) + "\xE2a", |
| 88 |
|
| 89 |
// same inputs as benchmarks |
| 90 |
"0123456789", |
| 91 |
"日本語日本語日本語日", |
| 92 |
"\xF4\x8F\xBF\xBF", |
| 93 |
|
| 94 |
// bugs found with fuzzing |
| 95 |
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\xc60", |
| 96 |
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\xc300", |
| 97 |
"߀0000000000000000000000000000訨", |
| 98 |
"0000000000000000000000000000000˂00000000000000000000000000000000", |
| 99 |
} |
| 100 |
|
| 101 |
any := byteRange{0, 0xFF} |
| 102 |
ascii := byteRange{0, 0x7F} |
| 103 |
cont := byteRange{0x80, 0xBF} |
| 104 |
|
| 105 |
rangesToTest := [][]byteRange{ |
| 106 |
{one(0x20), ascii, ascii, ascii}, |
| 107 |
|
| 108 |
// 2-byte sequences |
| 109 |
{one(0xC2)}, |
| 110 |
{one(0xC2), ascii}, |
| 111 |
{one(0xC2), cont}, |
| 112 |
{one(0xC2), {0xC0, 0xFF}}, |
| 113 |
{one(0xC2), cont, cont}, |
| 114 |
{one(0xC2), cont, cont, cont}, |
| 115 |
|
| 116 |
// 3-byte sequences |
| 117 |
{one(0xE1)}, |
| 118 |
{one(0xE1), cont}, |
| 119 |
{one(0xE1), cont, cont}, |
| 120 |
{one(0xE1), cont, cont, ascii}, |
| 121 |
{one(0xE1), cont, ascii}, |
| 122 |
{one(0xE1), cont, cont, cont}, |
| 123 |
|
| 124 |
// 4-byte sequences |
| 125 |
{one(0xF1)}, |
| 126 |
{one(0xF1), cont}, |
| 127 |
{one(0xF1), cont, cont}, |
| 128 |
{one(0xF1), cont, cont, cont}, |
| 129 |
{one(0xF1), cont, cont, ascii}, |
| 130 |
{one(0xF1), cont, cont, cont, ascii}, |
| 131 |
|
| 132 |
// overlong |
| 133 |
{{0xC0, 0xC1}, any}, |
| 134 |
{{0xC0, 0xC1}, any, any}, |
| 135 |
{{0xC0, 0xC1}, any, any, any}, |
| 136 |
{one(0xE0), {0x0, 0x9F}, cont}, |
| 137 |
{one(0xE0), {0xA0, 0xBF}, cont}, |
| 138 |
} |
| 139 |
|
| 140 |
for _, r := range rangesToTest { |
| 141 |
examples = append(examples, genExamples("", r)...) |
| 142 |
} |
| 143 |
|
| 144 |
for _, i := range []int{300, 316} { |
| 145 |
d := bytes.Repeat(someutf8, i/len(someutf8)) |
| 146 |
examples = append(examples, string(d)) |
| 147 |
} |
| 148 |
|
| 149 |
for _, tt := range examples { |
| 150 |
t.Run(tt, func(t *testing.T) { |
| 151 |
check(t, []byte(tt)) |
| 152 |
}) |
| 153 |
|
| 154 |
// Generate variations of the input to exercise errors at the |
| 155 |
// boundary, using the vector implementation on 32-sized input, |
| 156 |
// and on non-32-sized inputs. |
| 157 |
// |
| 158 |
// Large examples don't go through those variations because they |
| 159 |
// are likely specific tests. |
| 160 |
|
| 161 |
if len(tt) >= 32 { |
| 162 |
continue |
| 163 |
} |
| 164 |
|
| 165 |
t.Run("boundary-"+tt, func(t *testing.T) { |
| 166 |
size := 32 - len(tt) |
| 167 |
prefix := strings.Repeat("a", size) |
| 168 |
b := []byte(prefix + tt) |
| 169 |
check(t, b) |
| 170 |
}) |
| 171 |
t.Run("vec-padded-"+tt, func(t *testing.T) { |
| 172 |
prefix := strings.Repeat("a", 32) |
| 173 |
padding := strings.Repeat("b", 32-(len(tt)%32)) |
| 174 |
input := prefix + padding + tt |
| 175 |
b := []byte(input) |
| 176 |
if len(b)%32 != 0 { |
| 177 |
panic("test should generate block of 32") |
| 178 |
} |
| 179 |
check(t, b) |
| 180 |
}) |
| 181 |
t.Run("vec-"+tt, func(t *testing.T) { |
| 182 |
prefix := strings.Repeat("a", 32) |
| 183 |
input := prefix + tt |
| 184 |
if len(tt)%32 == 0 { |
| 185 |
input += "x" |
| 186 |
} |
| 187 |
b := []byte(input) |
| 188 |
if len(b)%32 == 0 { |
| 189 |
panic("test should not generate block of 32") |
| 190 |
} |
| 191 |
check(t, b) |
| 192 |
}) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func TestValidPageBoundary(t *testing.T) { |
| 197 |
buf, err := buffer.New(64) |
| 198 |
if err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
defer buf.Release() |
| 202 |
|
| 203 |
head := buf.ProtectHead() |
| 204 |
tail := buf.ProtectTail() |
| 205 |
|
| 206 |
data := bytes.Repeat(someutf8, 64/len(someutf8)) |
| 207 |
|
| 208 |
copy(head, data) |
| 209 |
copy(tail, data) |
| 210 |
|
| 211 |
for i := 0; i <= 32; i++ { |
| 212 |
input := head[:i] |
| 213 |
check(t, input) |
| 214 |
} |
| 215 |
|
| 216 |
for i := 0; i <= 32; i++ { |
| 217 |
input := tail[i:] |
| 218 |
check(t, input) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
func check(t *testing.T, b []byte) { |
| 223 |
t.Helper() |
| 224 |
|
| 225 |
// Check that both Valid and Validate behave properly. Should not be |
| 226 |
// necessary given the definition of Valid, but just in case. |
| 227 |
|
| 228 |
expected := utf8.Valid(b) |
| 229 |
if Valid(b) != expected { |
| 230 |
err := ioutil.WriteFile("test.out.txt", b, 0600) |
| 231 |
if err != nil { |
| 232 |
panic(err) |
| 233 |
} |
| 234 |
|
| 235 |
t.Errorf("Valid(%q) = %v; want %v", string(b), !expected, expected) |
| 236 |
} |
| 237 |
|
| 238 |
v := Validate(b) |
| 239 |
|
| 240 |
if v.IsUTF8() != expected { |
| 241 |
t.Errorf("Validate(%q) utf8 valid: %v; want %v", string(b), !expected, expected) |
| 242 |
} |
| 243 |
|
| 244 |
expected = ascii.Valid(b) |
| 245 |
if v.IsASCII() != expected { |
| 246 |
t.Errorf("Validate(%q) ascii valid: %v; want %v", string(b), !expected, expected) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
var valid1k = bytes.Repeat([]byte("0123456789日本語日本語日本語日abcdefghijklmnopqrstuvwx"), 16) |
| 251 |
var valid1M = bytes.Repeat(valid1k, 1024) |
| 252 |
var someutf8 = []byte("\xF4\x8F\xBF\xBF") |
| 253 |
|
| 254 |
func BenchmarkValid(b *testing.B) { |
| 255 |
impls := map[string]func([]byte) bool{ |
| 256 |
"AVX": Valid, |
| 257 |
"Stdlib": utf8.Valid, |
| 258 |
} |
| 259 |
|
| 260 |
type input struct { |
| 261 |
name string |
| 262 |
data []byte |
| 263 |
} |
| 264 |
inputs := []input{ |
| 265 |
{"1kValid", valid1k}, |
| 266 |
{"1MValid", valid1M}, |
| 267 |
{"10ASCII", []byte("0123456789")}, |
| 268 |
{"10Japan", []byte("日本語日本語日本語日")}, |
| 269 |
} |
| 270 |
|
| 271 |
const KiB = 1024 |
| 272 |
const MiB = 1048576 |
| 273 |
|
| 274 |
for i := 0; i <= 400/len(someutf8); i++ { |
| 275 |
// for _, i := range []int{1 * KiB, 8 * KiB, 16 * KiB, 64 * KiB, 1 * MiB, 8 * MiB, 32 * MiB, 64 * MiB} { |
| 276 |
d := bytes.Repeat(someutf8, i) |
| 277 |
inputs = append(inputs, input{ |
| 278 |
name: fmt.Sprintf("small%d", len(d)), |
| 279 |
data: d, |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
for _, i := range []int{300, 316} { |
| 284 |
d := bytes.Repeat(someutf8, i/len(someutf8)) |
| 285 |
inputs = append(inputs, input{ |
| 286 |
name: fmt.Sprintf("tail%d", len(d)), |
| 287 |
data: d, |
| 288 |
}) |
| 289 |
} |
| 290 |
|
| 291 |
for _, input := range inputs { |
| 292 |
for implName, f := range impls { |
| 293 |
testName := fmt.Sprintf("%s/%s", input.name, implName) |
| 294 |
|
| 295 |
b.Run(testName, func(b *testing.B) { |
| 296 |
b.SetBytes(int64(len(input.data))) |
| 297 |
b.ResetTimer() |
| 298 |
for i := 0; i < b.N; i++ { |
| 299 |
f(input.data) |
| 300 |
} |
| 301 |
}) |
| 302 |
} |
| 303 |
} |
| 304 |
} |