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
|
package jsonparser
import (
"bytes"
"testing"
)
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{
{in: `\uD83D`, isErr: true},
{in: `\uDE03`, isErr: true},
{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\`, isErr: true},
{in: `\uD800\u`, isErr: true},
{in: `\uD800\uD`, isErr: true},
{in: `\uD800\uDC`, isErr: true},
{in: `\uD800\uDC0`, isErr: true},
{in: `\uD800\uDBFF`, isErr: true}, // invalid low surrogate
}, commonUnicodeEscapeTests...)
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)
}
}
}
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},
{in: `\uD800`, isErr: true},
{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},
{in: `abcde\uD800`, isErr: true},
{in: `ab\uD800de`, isErr: true},
{in: `\uD800abcde`, 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).
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
}
}
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
}
}
}
}
|