| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package json |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
"unicode" |
| 12 |
|
| 13 |
jsonv1 "encoding/json" |
| 14 |
) |
| 15 |
|
| 16 |
var equalFoldTestdata = []struct { |
| 17 |
in1, in2 string |
| 18 |
want bool |
| 19 |
}{ |
| 20 |
{"", "", true}, |
| 21 |
{"abc", "abc", true}, |
| 22 |
{"ABcd", "ABcd", true}, |
| 23 |
{"123abc", "123ABC", true}, |
| 24 |
{"_1_2_-_3__--a-_-b-c-", "123ABC", true}, |
| 25 |
{"αβδ", "ΑΒΔ", true}, |
| 26 |
{"abc", "xyz", false}, |
| 27 |
{"abc", "XYZ", false}, |
| 28 |
{"abcdefghijk", "abcdefghijX", false}, |
| 29 |
{"abcdefghijk", "abcdefghij\u212A", true}, |
| 30 |
{"abcdefghijK", "abcdefghij\u212A", true}, |
| 31 |
{"abcdefghijkz", "abcdefghij\u212Ay", false}, |
| 32 |
{"abcdefghijKz", "abcdefghij\u212Ay", false}, |
| 33 |
{"1", "2", false}, |
| 34 |
{"utf-8", "US-ASCII", false}, |
| 35 |
{"hello, world!", "hello, world!", true}, |
| 36 |
{"hello, world!", "Hello, World!", true}, |
| 37 |
{"hello, world!", "HELLO, WORLD!", true}, |
| 38 |
{"hello, world!", "jello, world!", false}, |
| 39 |
{"γειά, κόσμε!", "γειά, κόσμε!", true}, |
| 40 |
{"γειά, κόσμε!", "Γειά, Κόσμε!", true}, |
| 41 |
{"γειά, κόσμε!", "ΓΕΙΆ, ΚΌΣΜΕ!", true}, |
| 42 |
{"γειά, κόσμε!", "ΛΕΙΆ, ΚΌΣΜΕ!", false}, |
| 43 |
{"AESKey", "aesKey", true}, |
| 44 |
{"γειά, κόσμε!", "Γ\xce_\xb5ιά, Κόσμε!", false}, |
| 45 |
{"aeskey", "AESKEY", true}, |
| 46 |
{"AESKEY", "aes_key", true}, |
| 47 |
{"aes_key", "AES_KEY", true}, |
| 48 |
{"AES_KEY", "aes-key", true}, |
| 49 |
{"aes-key", "AES-KEY", true}, |
| 50 |
{"AES-KEY", "aesKey", true}, |
| 51 |
{"aesKey", "AesKey", true}, |
| 52 |
{"AesKey", "AESKey", true}, |
| 53 |
{"AESKey", "aeskey", true}, |
| 54 |
{"DESKey", "aeskey", false}, |
| 55 |
{"AES Key", "aeskey", false}, |
| 56 |
{"aes﹏key", "aeskey", false}ed |
| 57 |
{"aes〰key", "aeskey", false}ed |
| 58 |
} |
| 59 |
|
| 60 |
func TestEqualFold(t *testing.T) { |
| 61 |
for _, tt := range equalFoldTestdata { |
| 62 |
got := equalFold([]byte(tt.in1), []byte(tt.in2)) |
| 63 |
if got != tt.want { |
| 64 |
t.Errorf("equalFold(%q, %q) = %v, want %v", tt.in1, tt.in2, got, tt.want) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func equalFold(x, y []byte) bool { |
| 70 |
return string(foldName(x)) == string(foldName(y)) |
| 71 |
} |
| 72 |
|
| 73 |
func TestFoldRune(t *testing.T) { |
| 74 |
if testing.Short() { |
| 75 |
t.Skip() |
| 76 |
} |
| 77 |
|
| 78 |
var foldSet []rune |
| 79 |
for r := rune(0); r <= unicode.MaxRune; r++ { |
| 80 |
|
| 81 |
foldSet = foldSet[:0] |
| 82 |
for r0 := r; r != r0 || len(foldSet) == 0; r = unicode.SimpleFold(r) { |
| 83 |
foldSet = append(foldSet, r) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
var withinSet bool |
| 89 |
rr0 := foldRune(foldSet[0]) |
| 90 |
for _, r := range foldSet { |
| 91 |
withinSet = withinSet || rr0 == r |
| 92 |
rr := foldRune(r) |
| 93 |
if rr0 != rr { |
| 94 |
t.Errorf("foldRune(%q) = %q, want %q", r, rr, rr0) |
| 95 |
} |
| 96 |
} |
| 97 |
if !withinSet { |
| 98 |
t.Errorf("foldRune(%q) = %q not in fold set %q", foldSet[0], rr0, string(foldSet)) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
func TestBenchmarkUnmarshalUnknown(t *testing.T) { runUnmarshalUnknown(t) } |
| 107 |
func BenchmarkUnmarshalUnknown(b *testing.B) { runUnmarshalUnknown(b) } |
| 108 |
|
| 109 |
func runUnmarshalUnknown(tb testing.TB) { |
| 110 |
in := []byte(`{"NameUnknown":null}`) |
| 111 |
for _, n := range []int{1, 2, 5, 10, 20, 50, 100} { |
| 112 |
unmarshal := Unmarshal |
| 113 |
if benchV1 { |
| 114 |
unmarshal = jsonv1.Unmarshal |
| 115 |
} |
| 116 |
|
| 117 |
var fields []reflect.StructField |
| 118 |
for i := 0; i < n; i++ { |
| 119 |
fields = append(fields, reflect.StructField{ |
| 120 |
Name: fmt.Sprintf("Name%d", i), |
| 121 |
Type: reflect.TypeOf(0), |
| 122 |
Tag: `json:",nocase"`, |
| 123 |
}) |
| 124 |
} |
| 125 |
out := reflect.New(reflect.StructOf(fields)).Interface() |
| 126 |
|
| 127 |
runTestOrBench(tb, fmt.Sprintf("N%d", n), int64(len(in)), func(tb testing.TB) { |
| 128 |
if err := unmarshal(in, out); err != nil { |
| 129 |
tb.Fatalf("Unmarshal error: %v", err) |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
} |