-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathescape_test.go
More file actions
507 lines (454 loc) · 17.2 KB
/
Copy pathescape_test.go
File metadata and controls
507 lines (454 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package jsonparser
import (
"bytes"
"encoding/json"
"testing"
"unicode/utf8"
)
// Verifies: SYS-REQ-014 [boundary]
// MCDC SYS-REQ-014: N/A
func TestH2I(t *testing.T) {
hexChars := []byte{'0', '9', 'A', 'F', 'a', 'f', 'x', '\000'}
hexValues := []int{0, 9, 10, 15, 10, 15, -1, -1}
for i, c := range hexChars {
if v := h2I(c); v != hexValues[i] {
t.Errorf("h2I('%c') returned wrong value (obtained %d, expected %d)", c, v, hexValues[i])
}
}
}
type escapedUnicodeRuneTest struct {
in string
isErr bool
out rune
len int
}
var commonUnicodeEscapeTests = []escapedUnicodeRuneTest{
{in: `\u0041`, out: 'A', len: 6},
{in: `\u0000`, out: 0, len: 6},
{in: `\u00b0`, out: '°', len: 6},
{in: `\u00B0`, out: '°', len: 6},
{in: `\x1234`, out: 0x1234, len: 6}, // These functions do not check the \u prefix
{in: ``, isErr: true},
{in: `\`, isErr: true},
{in: `\u`, isErr: true},
{in: `\u1`, isErr: true},
{in: `\u11`, isErr: true},
{in: `\u111`, isErr: true},
{in: `\u123X`, isErr: true},
}
var singleUnicodeEscapeTests = append([]escapedUnicodeRuneTest{
{in: `\uD83D`, out: 0xD83D, len: 6},
{in: `\uDE03`, out: 0xDE03, len: 6},
{in: `\uFFFF`, out: 0xFFFF, len: 6},
{in: `\uFF11`, out: '1', len: 6},
}, commonUnicodeEscapeTests...)
var multiUnicodeEscapeTests = append([]escapedUnicodeRuneTest{
// Lone surrogates: malformed per RFC 8259/WHATWG. Match encoding/json by
// substituting U+FFFD (utf8.RuneError) and consuming only the 6 bytes of
// the lone-surrogate escape. See DEFECT-260727-SNGT.
{in: `\uD83D`, out: utf8.RuneError, len: 6}, // lone high surrogate
{in: `\uDE03`, out: utf8.RuneError, len: 6}, // lone low surrogate
{in: `\uFFFF`, out: '\uFFFF', len: 6},
{in: `\uFF11`, out: '1', len: 6},
{in: `\uD83D\uDE03`, out: '\U0001F603', len: 12},
{in: `\uD800\uDC00`, out: '\U00010000', len: 12},
{in: `\uD800\`, out: utf8.RuneError, len: 6}, // high surrogate not followed by "\u" → lone high
{in: `\uD800\u`, isErr: true}, // second escape truncated → malformed
{in: `\uD800\uD`, isErr: true}, // second escape truncated → malformed
{in: `\uD800\uDC`, isErr: true}, // second escape truncated → malformed
{in: `\uD800\uDC0`, isErr: true}, // second escape truncated → malformed
{in: `\uD800\uDBFF`, out: utf8.RuneError, len: 6}, // second escape is high surrogate, not low → lone high
}, commonUnicodeEscapeTests...)
// Verifies: SYS-REQ-014 [malformed]
// MCDC SYS-REQ-014: N/A
func TestDecodeSingleUnicodeEscape(t *testing.T) {
for _, test := range singleUnicodeEscapeTests {
r, ok := decodeSingleUnicodeEscape([]byte(test.in))
isErr := !ok
if isErr != test.isErr {
t.Errorf("decodeSingleUnicodeEscape(%s) returned isErr mismatch: expected %t, obtained %t", test.in, test.isErr, isErr)
} else if isErr {
continue
} else if r != test.out {
t.Errorf("decodeSingleUnicodeEscape(%s) returned rune mismatch: expected %x (%c), obtained %x (%c)", test.in, test.out, test.out, r, r)
}
}
}
// Verifies: SYS-REQ-014 [malformed]
// MCDC SYS-REQ-014: N/A
func TestDecodeUnicodeEscape(t *testing.T) {
for _, test := range multiUnicodeEscapeTests {
r, len := decodeUnicodeEscape([]byte(test.in))
isErr := (len == -1)
if isErr != test.isErr {
t.Errorf("decodeUnicodeEscape(%s) returned isErr mismatch: expected %t, obtained %t", test.in, test.isErr, isErr)
} else if isErr {
continue
} else if len != test.len {
t.Errorf("decodeUnicodeEscape(%s) returned length mismatch: expected %d, obtained %d", test.in, test.len, len)
} else if r != test.out {
t.Errorf("decodeUnicodeEscape(%s) returned rune mismatch: expected %x (%c), obtained %x (%c)", test.in, test.out, test.out, r, r)
}
}
}
type unescapeTest struct {
in string // escaped string
out string // expected unescaped string
canAlloc bool // can unescape cause an allocation (depending on buffer size)? true iff 'in' contains escape sequence(s)
isErr bool // should this operation result in an error
}
var unescapeTests = []unescapeTest{
{in: ``, out: ``, canAlloc: false},
{in: `a`, out: `a`, canAlloc: false},
{in: `abcde`, out: `abcde`, canAlloc: false},
{in: `ab\\de`, out: `ab\de`, canAlloc: true},
{in: `ab\"de`, out: `ab"de`, canAlloc: true},
{in: `ab \u00B0 de`, out: `ab ° de`, canAlloc: true},
{in: `ab \uFF11 de`, out: `ab 1 de`, canAlloc: true},
{in: `\uFFFF`, out: "\uFFFF", canAlloc: true},
{in: `ab \uD83D\uDE03 de`, out: "ab \U0001F603 de", canAlloc: true},
{in: `\u0000\u0000\u0000\u0000\u0000`, out: "\u0000\u0000\u0000\u0000\u0000", canAlloc: true},
{in: `\u0000 \u0000 \u0000 \u0000 \u0000`, out: "\u0000 \u0000 \u0000 \u0000 \u0000", canAlloc: true},
{in: ` \u0000 \u0000 \u0000 \u0000 \u0000 `, out: " \u0000 \u0000 \u0000 \u0000 \u0000 ", canAlloc: true},
// Lone surrogates: malformed per RFC 8259/WHATWG. Match encoding/json by
// substituting U+FFFD and preserving the following literal bytes. See
// DEFECT-260727-SNGT.
{in: `\uD800`, out: "\uFFFD", canAlloc: true},
{in: `abcde\uD800`, out: "abcde\uFFFD", canAlloc: true},
{in: `ab\uD800de`, out: "ab\uFFFDde", canAlloc: true},
{in: `\uD800abcde`, out: "\uFFFDabcde", canAlloc: true},
{in: `\uDB29A7FA`, out: "\uFFFDA7FA", canAlloc: true}, // high surrogate followed by non-escape bytes
{in: `\uD800\u0041`, out: "\uFFFDA", canAlloc: true}, // high surrogate followed by BMP escape
{in: `\uDC00`, out: "\uFFFD", canAlloc: true}, // lone low surrogate
{in: `\uD800\uDC00`, out: "\U00010000", canAlloc: true}, // valid pair still works
// Truncated / malformed escape sequences (non-surrogate) still error.
{in: `abcde\`, isErr: true},
{in: `abcde\x`, isErr: true},
{in: `abcde\u`, isErr: true},
{in: `abcde\u1`, isErr: true},
{in: `abcde\u12`, isErr: true},
{in: `abcde\u123`, isErr: true},
}
// isSameMemory checks if two slices contain the same memory pointer (meaning one is a
// subslice of the other, with possibly differing lengths/capacities).
// Test helper for SYS-REQ-014.
func isSameMemory(a, b []byte) bool {
if cap(a) == 0 || cap(b) == 0 {
return cap(a) == cap(b)
} else if a, b = a[:1], b[:1]; a[0] != b[0] {
return false
} else {
a[0]++
same := (a[0] == b[0])
a[0]--
return same
}
}
// Verifies: SYS-REQ-014 [malformed]
// MCDC SYS-REQ-014: N/A
func TestUnescape(t *testing.T) {
for _, test := range unescapeTests {
type bufferTestCase struct {
buf []byte
isTooSmall bool
}
var bufs []bufferTestCase
if len(test.in) == 0 {
// If the input string is length 0, only a buffer of size 0 is a meaningful test
bufs = []bufferTestCase{{nil, false}}
} else {
// For non-empty input strings, we can try several buffer sizes (0, len-1, len)
bufs = []bufferTestCase{
{nil, true},
{make([]byte, 0, len(test.in)-1), true},
{make([]byte, 0, len(test.in)), false},
}
}
for _, buftest := range bufs {
in := []byte(test.in)
buf := buftest.buf
out, err := Unescape(in, buf)
isErr := (err != nil)
isAlloc := !isSameMemory(out, in) && !isSameMemory(out, buf)
if isErr != test.isErr {
t.Errorf("Unescape(`%s`, bufsize=%d) returned isErr mismatch: expected %t, obtained %t", test.in, cap(buf), test.isErr, isErr)
break
} else if isErr {
continue
} else if !bytes.Equal(out, []byte(test.out)) {
t.Errorf("Unescape(`%s`, bufsize=%d) returned unescaped mismatch: expected `%s` (%v, len %d), obtained `%s` (%v, len %d)", test.in, cap(buf), test.out, []byte(test.out), len(test.out), string(out), out, len(out))
break
} else if isAlloc != (test.canAlloc && buftest.isTooSmall) {
t.Errorf("Unescape(`%s`, bufsize=%d) returned isAlloc mismatch: expected %t, obtained %t", test.in, cap(buf), buftest.isTooSmall, isAlloc)
break
}
}
}
}
// =============================================================================
// Lone-Unicode-surrogate regression tests (DEFECT-260727-SNGT).
//
// Root cause: decodeUnicodeEscape (escape.go:115) read a high surrogate and
// then called decodeSingleUnicodeEscape(in[6:]) to fetch the low surrogate
// WITHOUT verifying in[6:8] == "\u". decodeSingleUnicodeEscape assumes the
// "\u" prefix and reads hex at fixed offsets, so literal bytes following a
// lone high surrogate (e.g. the "A7FA" after "\uDB29") were misread as a low
// surrogate and a bogus non-BMP code point was synthesized. The fix matches
// encoding/json: a lone surrogate (high or low) is malformed → substitute
// U+FFFD and consume only the 6 bytes of the lone-surrogate escape.
// =============================================================================
const replacementChar = "\uFFFD"
// Verifies: SYS-REQ-014 [boundary] — lone high surrogate → U+FFFD, no
// following bytes consumed.
func TestUnescapeLoneHighSurrogate(t *testing.T) {
out, err := Unescape([]byte(`\uDB29`), nil)
if err != nil {
t.Fatalf("Unescape(`\\uDB29`) returned error %v; expected U+FFFD substitution", err)
}
if got := string(out); got != replacementChar {
t.Errorf("Unescape(`\\uDB29`) = %q; expected %q (U+FFFD)", got, replacementChar)
}
}
// Verifies: SYS-REQ-014 [boundary] — the bug: high surrogate followed by
// non-escape bytes was synthesizing U+DC271; must be U+FFFD + literal "A7FA".
func TestUnescapeLoneHighSurrogateFollowedByNonEscape(t *testing.T) {
out, err := Unescape([]byte(`\uDB29A7FA`), nil)
if err != nil {
t.Fatalf("Unescape(`\\uDB29A7FA`) returned error %v; expected U+FFFD + literal A7FA", err)
}
if want, got := replacementChar+"A7FA", string(out); got != want {
t.Errorf("Unescape(`\\uDB29A7FA`) = %q; expected %q", got, want)
}
}
// Verifies: SYS-REQ-014 [boundary] — lone low surrogate → U+FFFD.
func TestUnescapeLoneLowSurrogate(t *testing.T) {
out, err := Unescape([]byte(`\uDC00`), nil)
if err != nil {
t.Fatalf("Unescape(`\\uDC00`) returned error %v; expected U+FFFD substitution", err)
}
if got := string(out); got != replacementChar {
t.Errorf("Unescape(`\\uDC00`) = %q; expected %q (U+FFFD)", got, replacementChar)
}
}
// Verifies: SYS-REQ-014 [boundary] — high surrogate followed by a "\u" escape
// that is NOT a low surrogate (here a BMP codepoint U+0041 'A') → U+FFFD + 'A'.
func TestUnescapeHighSurrogateThenNonSurrogate(t *testing.T) {
out, err := Unescape([]byte(`\uD800\u0041`), nil)
if err != nil {
t.Fatalf("Unescape(`\\uD800\\u0041`) returned error %v; expected U+FFFD + A", err)
}
if want, got := replacementChar+"A", string(out); got != want {
t.Errorf("Unescape(`\\uD800\\u0041`) = %q; expected %q", got, want)
}
}
// Verifies: SYS-REQ-014 [happy-path] — a valid UTF-16 surrogate pair must
// still combine into the supplementary-plane code point (regression guard).
func TestUnescapeValidSurrogatePairStillWorks(t *testing.T) {
out, err := Unescape([]byte(`\uD800\uDC00`), nil)
if err != nil {
t.Fatalf("Unescape(`\\uD800\\uDC00`) returned error %v; expected U+10000", err)
}
if want, got := "\U00010000", string(out); got != want {
t.Errorf("Unescape(`\\uD800\\uDC00`) = %q; expected %q", got, want)
}
}
// Verifies: SYS-REQ-014 [differential] — for a corpus of lone-surrogate
// inputs, ParseString must byte-match encoding/json.Unmarshal (the oracle
// exercised by the FuzzJSONStructureAware checkParseStringGate finder).
func TestParseStringLoneSurrogateMatchesEncodingJSON(t *testing.T) {
corpus := []string{
`\uDB29`,
`\uDB29A7FA`,
`\uDB29A7FA71 a`,
`\uD800`,
`\uD83D`,
`\uDBFF`,
`\uDC00`,
`\uDFFF`,
`\uDE03`,
`\uD800\u0041`,
`\uD800\uD800`,
`\uD800\uDBFF`,
`\uD800\uE000`,
`\uD800\uFFFF`,
`\uDB29\uDC00`,
`\uD83D\uDE03 more`,
`\uD800\uDC00`,
`\u0000\uD800\u0000`,
}
for _, esc := range corpus {
// Reference oracle: encoding/json unescapes the quoted string.
var want string
jsonInput := `"` + esc + `"`
if err := json.Unmarshal([]byte(jsonInput), &want); err != nil {
// encoding/json rejects malformed escapes (e.g. truncated "\u"). The
// corpus above is chosen to all be accepted; if one is rejected we
// want to know rather than silently skip.
t.Errorf("encoding/json rejected corpus input %q: %v", jsonInput, err)
continue
}
got, err := ParseString([]byte(esc))
if err != nil {
t.Errorf("ParseString(%q) returned error %v; encoding/json accepted it as %q", esc, err, want)
continue
}
if got != want {
t.Errorf("ParseString(%q) divergence:\n got = %q (% x)\n want = %q (% x)",
esc, got, []byte(got), want, []byte(want))
}
}
}
// Verifies: SYS-REQ-014 (escape handling)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestEscape(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{name: "plain", in: "hello", want: `"hello"`},
{name: "quotation mark", in: `"`, want: `"\""`},
{name: "reverse solidus", in: `\`, want: `"\\"`},
{name: "solidus", in: `/`, want: `"/"`},
{name: "backspace", in: "\b", want: `"\b"`},
{name: "form feed", in: "\f", want: `"\f"`},
{name: "line feed", in: "\n", want: `"\n"`},
{name: "carriage return", in: "\r", want: `"\r"`},
{name: "tab", in: "\t", want: `"\t"`},
{name: "unicode escape", in: "\x01", want: `"\u0001"`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
escaped := Escape(test.in)
if got := string(escaped); got != test.want {
t.Fatalf("Escape(%q) = %q; want %q", test.in, got, test.want)
}
unescaped, err := Unescape(escaped[1:len(escaped)-1], nil)
if err != nil {
t.Fatalf("Unescape(Escape(%q)) returned error: %v", test.in, err)
}
if got := string(unescaped); got != test.in {
t.Fatalf("Unescape(Escape(%q)) = %q; want %q", test.in, got, test.in)
}
})
}
}
// Verifies: SYS-REQ-014 (escape handling)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestEscapeControlChars(t *testing.T) {
for c := byte(0); c < 0x20; c++ {
escaped := Escape(string([]byte{c}))
var want string
switch c {
case '\b':
want = `"\b"`
case '\f':
want = `"\f"`
case '\n':
want = `"\n"`
case '\r':
want = `"\r"`
case '\t':
want = `"\t"`
default:
want = string([]byte{'"', '\\', 'u', '0', '0', lowerHex[c>>4], lowerHex[c&0x0f], '"'})
}
if got := string(escaped); got != want {
t.Errorf("Escape control character 0x%02x = %q; want %q", c, got, want)
}
unescaped, err := Unescape(escaped[1:len(escaped)-1], nil)
if err != nil {
t.Errorf("Unescape(Escape(0x%02x)) returned error: %v", c, err)
continue
}
if len(unescaped) != 1 || unescaped[0] != c {
t.Errorf("Unescape(Escape(0x%02x)) = % x; want %02x", c, unescaped, c)
}
}
}
// Verifies: SYS-REQ-014 (escape handling)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestEscapeUnicode(t *testing.T) {
in := "你好,世界 🌍"
escaped := Escape(in)
if want := `"` + in + `"`; string(escaped) != want {
t.Fatalf("Escape(%q) = %q; want %q", in, escaped, want)
}
unescaped, err := Unescape(escaped[1:len(escaped)-1], nil)
if err != nil {
t.Fatalf("Unescape(Escape(%q)) returned error: %v", in, err)
}
if got := string(unescaped); got != in {
t.Fatalf("Unescape(Escape(%q)) = %q; want %q", in, got, in)
}
}
// Verifies: SYS-REQ-014 (escape handling)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestEscapeEmptyString(t *testing.T) {
if got, want := string(Escape("")), `""`; got != want {
t.Fatalf("Escape(\"\") = %q; want %q", got, want)
}
}
// Verifies: SYS-REQ-009 (Set)
// Verifies: SYS-REQ-009 (Set)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestSetString(t *testing.T) {
got, err := SetString([]byte(`{"key":"old"}`), "hello", "key")
if err != nil {
t.Fatalf("SetString returned error: %v", err)
}
if !json.Valid(got) {
t.Fatalf("SetString returned invalid JSON: %s", got)
}
if want := `{"key":"hello"}`; string(got) != want {
t.Fatalf("SetString result = %s; want %s", got, want)
}
}
// Verifies: SYS-REQ-002 (GetString)
// Verifies: SYS-REQ-009 (Set)
// Verifies: SYS-REQ-009 (Set)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestSetStringRoundTrip(t *testing.T) {
const want = "你好 🌍"
data, err := SetString([]byte(`{"key":null}`), want, "key")
if err != nil {
t.Fatalf("SetString returned error: %v", err)
}
got, err := GetString(data, "key")
if err != nil {
t.Fatalf("GetString returned error: %v", err)
}
if got != want {
t.Fatalf("GetString after SetString = %q; want %q", got, want)
}
}
// Verifies: SYS-REQ-002 (GetString)
// Verifies: SYS-REQ-009 (Set)
// Verifies: SYS-REQ-009 (Set)
// reqproof:proptest:skip targeted witness/regression test; not a property-test subject
func TestSetStringSpecialChars(t *testing.T) {
tests := []string{
`a\b`,
`a"b`,
"a\nb",
"a\tb",
}
for _, want := range tests {
t.Run(want, func(t *testing.T) {
data, err := SetString([]byte(`{"key":""}`), want, "key")
if err != nil {
t.Fatalf("SetString(%q) returned error: %v", want, err)
}
if !json.Valid(data) {
t.Fatalf("SetString(%q) returned invalid JSON: %s", want, data)
}
got, err := GetString(data, "key")
if err != nil {
t.Fatalf("GetString after SetString(%q) returned error: %v", want, err)
}
if got != want {
t.Fatalf("GetString after SetString(%q) = %q; want %q", want, got, want)
}
})
}
}