Debian Codegraph @

escape_test.go

raw symbols SCIP
1 package jsonparser
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 // Verifies: SYS-REQ-014 [boundary]
9 // MCDC SYS-REQ-014: N/A
10 func TestH2I(t *testing.T) {
11 hexChars := []byte{'0', '9', 'A', 'F', 'a', 'f', 'x', '\000'}
12 hexValues := []int{0, 9, 10, 15, 10, 15, -1, -1}
13
14 for i, c := range hexChars {
15 if v := h2I(c); v != hexValues[i] {
16 t.Errorf("h2I('%c') returned wrong value (obtained %d, expected %d)", c, v, hexValues[i])
17 }
18 }
19 }
20
21 type escapedUnicodeRuneTest struct {
22 in string
23 isErr bool
24 out rune
25 len int
26 }
27
28 var commonUnicodeEscapeTests = []escapedUnicodeRuneTest{
29 {in: `\u0041`, out: 'A', len: 6},
30 {in: `\u0000`, out: 0, len: 6},
31 {in: `\u00b0`, out: '°', len: 6},
32 {in: `\u00B0`, out: '°', len: 6},
33
34 {in: `\x1234`, out: 0x1234, len: 6}, // These functions do not check the \u prefix
35
36 {in: ``, isErr: true},
37 {in: `\`, isErr: true},
38 {in: `\u`, isErr: true},
39 {in: `\u1`, isErr: true},
40 {in: `\u11`, isErr: true},
41 {in: `\u111`, isErr: true},
42 {in: `\u123X`, isErr: true},
43 }
44
45 var singleUnicodeEscapeTests = append([]escapedUnicodeRuneTest{
46 {in: `\uD83D`, out: 0xD83D, len: 6},
47 {in: `\uDE03`, out: 0xDE03, len: 6},
48 {in: `\uFFFF`, out: 0xFFFF, len: 6},
49 {in: `\uFF11`, out: '1', len: 6},
50 }, commonUnicodeEscapeTests...)
51
52 var multiUnicodeEscapeTests = append([]escapedUnicodeRuneTest{
53 {in: `\uD83D`, isErr: true},
54 {in: `\uDE03`, isErr: true},
55 {in: `\uFFFF`, out: '\uFFFF', len: 6},
56 {in: `\uFF11`, out: '1', len: 6},
57
58 {in: `\uD83D\uDE03`, out: '\U0001F603', len: 12},
59 {in: `\uD800\uDC00`, out: '\U00010000', len: 12},
60
61 {in: `\uD800\`, isErr: true},
62 {in: `\uD800\u`, isErr: true},
63 {in: `\uD800\uD`, isErr: true},
64 {in: `\uD800\uDC`, isErr: true},
65 {in: `\uD800\uDC0`, isErr: true},
66 {in: `\uD800\uDBFF`, isErr: true}, // invalid low surrogate
67 }, commonUnicodeEscapeTests...)
68
69 // Verifies: SYS-REQ-014 [malformed]
70 // MCDC SYS-REQ-014: N/A
71 func TestDecodeSingleUnicodeEscape(t *testing.T) {
72 for _, test := range singleUnicodeEscapeTests {
73 r, ok := decodeSingleUnicodeEscape([]byte(test.in))
74 isErr := !ok
75
76 if isErr != test.isErr {
77 t.Errorf("decodeSingleUnicodeEscape(%s) returned isErr mismatch: expected %t, obtained %t", test.in, test.isErr, isErr)
78 } else if isErr {
79 continue
80 } else if r != test.out {
81 t.Errorf("decodeSingleUnicodeEscape(%s) returned rune mismatch: expected %x (%c), obtained %x (%c)", test.in, test.out, test.out, r, r)
82 }
83 }
84 }
85
86 // Verifies: SYS-REQ-014 [malformed]
87 // MCDC SYS-REQ-014: N/A
88 func TestDecodeUnicodeEscape(t *testing.T) {
89 for _, test := range multiUnicodeEscapeTests {
90 r, len := decodeUnicodeEscape([]byte(test.in))
91 isErr := (len == -1)
92
93 if isErr != test.isErr {
94 t.Errorf("decodeUnicodeEscape(%s) returned isErr mismatch: expected %t, obtained %t", test.in, test.isErr, isErr)
95 } else if isErr {
96 continue
97 } else if len != test.len {
98 t.Errorf("decodeUnicodeEscape(%s) returned length mismatch: expected %d, obtained %d", test.in, test.len, len)
99 } else if r != test.out {
100 t.Errorf("decodeUnicodeEscape(%s) returned rune mismatch: expected %x (%c), obtained %x (%c)", test.in, test.out, test.out, r, r)
101 }
102 }
103 }
104
105 type unescapeTest struct {
106 in string // escaped string
107 out string // expected unescaped string
108 canAlloc bool // can unescape cause an allocation (depending on buffer size)? true iff 'in' contains escape sequence(s)
109 isErr bool // should this operation result in an error
110 }
111
112 var unescapeTests = []unescapeTest{
113 {in: ``, out: ``, canAlloc: false},
114 {in: `a`, out: `a`, canAlloc: false},
115 {in: `abcde`, out: `abcde`, canAlloc: false},
116
117 {in: `ab\\de`, out: `ab\de`, canAlloc: true},
118 {in: `ab\"de`, out: `ab"de`, canAlloc: true},
119 {in: `ab \u00B0 de`, out: `ab ° de`, canAlloc: true},
120 {in: `ab \uFF11 de`, out: `ab 1 de`, canAlloc: true},
121 {in: `\uFFFF`, out: "\uFFFF", canAlloc: true},
122 {in: `ab \uD83D\uDE03 de`, out: "ab \U0001F603 de", canAlloc: true},
123 {in: `\u0000\u0000\u0000\u0000\u0000`, out: "\u0000\u0000\u0000\u0000\u0000", canAlloc: true},
124 {in: `\u0000 \u0000 \u0000 \u0000 \u0000`, out: "\u0000 \u0000 \u0000 \u0000 \u0000", canAlloc: true},
125 {in: ` \u0000 \u0000 \u0000 \u0000 \u0000 `, out: " \u0000 \u0000 \u0000 \u0000 \u0000 ", canAlloc: true},
126
127 {in: `\uD800`, isErr: true},
128 {in: `abcde\`, isErr: true},
129 {in: `abcde\x`, isErr: true},
130 {in: `abcde\u`, isErr: true},
131 {in: `abcde\u1`, isErr: true},
132 {in: `abcde\u12`, isErr: true},
133 {in: `abcde\u123`, isErr: true},
134 {in: `abcde\uD800`, isErr: true},
135 {in: `ab\uD800de`, isErr: true},
136 {in: `\uD800abcde`, isErr: true},
137 }
138
139 // isSameMemory checks if two slices contain the same memory pointer (meaning one is a
140 // subslice of the other, with possibly differing lengths/capacities).
141 // Test helper for SYS-REQ-014.
142 func isSameMemory(a, b []byte) bool {
143 if cap(a) == 0 || cap(b) == 0 {
144 return cap(a) == cap(b)
145 } else if a, b = a[:1], b[:1]; a[0] != b[0] {
146 return false
147 } else {
148 a[0]++
149 same := (a[0] == b[0])
150 a[0]--
151 return same
152 }
153
154 }
155
156 // Verifies: SYS-REQ-014 [malformed]
157 // MCDC SYS-REQ-014: N/A
158 func TestUnescape(t *testing.T) {
159 for _, test := range unescapeTests {
160 type bufferTestCase struct {
161 buf []byte
162 isTooSmall bool
163 }
164
165 var bufs []bufferTestCase
166
167 if len(test.in) == 0 {
168 // If the input string is length 0, only a buffer of size 0 is a meaningful test
169 bufs = []bufferTestCase{{nil, false}}
170 } else {
171 // For non-empty input strings, we can try several buffer sizes (0, len-1, len)
172 bufs = []bufferTestCase{
173 {nil, true},
174 {make([]byte, 0, len(test.in)-1), true},
175 {make([]byte, 0, len(test.in)), false},
176 }
177 }
178
179 for _, buftest := range bufs {
180 in := []byte(test.in)
181 buf := buftest.buf
182
183 out, err := Unescape(in, buf)
184 isErr := (err != nil)
185 isAlloc := !isSameMemory(out, in) && !isSameMemory(out, buf)
186
187 if isErr != test.isErr {
188 t.Errorf("Unescape(`%s`, bufsize=%d) returned isErr mismatch: expected %t, obtained %t", test.in, cap(buf), test.isErr, isErr)
189 break
190 } else if isErr {
191 continue
192 } else if !bytes.Equal(out, []byte(test.out)) {
193 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))
194 break
195 } else if isAlloc != (test.canAlloc && buftest.isTooSmall) {
196 t.Errorf("Unescape(`%s`, bufsize=%d) returned isAlloc mismatch: expected %t, obtained %t", test.in, cap(buf), buftest.isTooSmall, isAlloc)
197 break
198 }
199 }
200 }
201 }