-
Notifications
You must be signed in to change notification settings - Fork 17
/
spec_test.go
180 lines (164 loc) · 3.9 KB
/
spec_test.go
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
package multibase
import (
"encoding/csv"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"unicode/utf8"
)
func TestSpec(t *testing.T) {
file, err := os.Open("spec/multibase.csv")
if err != nil {
t.Fatal(err)
}
defer file.Close()
reader := csv.NewReader(file)
reader.LazyQuotes = false
reader.FieldsPerRecord = 4
reader.TrimLeadingSpace = true
values, err := reader.ReadAll()
if err != nil {
t.Error(err)
}
expectedEncodings := make(map[Encoding]string, len(values)-1)
for _, v := range values[1:] {
encoding := v[0]
codeStr := v[1]
var code Encoding
if strings.HasPrefix(codeStr, "0x") {
i, err := strconv.ParseUint(codeStr[2:], 16, 64)
if err != nil {
t.Errorf("invalid multibase byte %q", codeStr)
continue
}
code = Encoding(i)
} else {
codeRune, length := utf8.DecodeRuneInString(codeStr)
if code == utf8.RuneError {
t.Errorf("multibase %q wasn't valid utf8", codeStr)
continue
}
if length != len(codeStr) {
t.Errorf("multibase %q wasn't a single character", codeStr)
continue
}
code = Encoding(codeRune)
}
expectedEncodings[code] = encoding
}
for name, enc := range Encodings {
expectedName, ok := expectedEncodings[enc]
if !ok {
t.Errorf("encoding %q (%c) not defined in the spec", name, enc)
continue
}
if expectedName != name {
t.Errorf("encoding %q (%c) has unexpected name %q", expectedName, enc, name)
}
}
}
func TestSpecVectors(t *testing.T) {
files, err := filepath.Glob("spec/tests/*.csv")
if err != nil {
t.Fatal(err)
}
for _, fname := range files {
t.Run(fname, func(t *testing.T) {
file, err := os.Open(fname)
if err != nil {
t.Error(err)
return
}
defer file.Close()
reader := csv.NewReader(file)
reader.LazyQuotes = false
reader.FieldsPerRecord = 2
reader.TrimLeadingSpace = true
values, err := reader.ReadAll()
if err != nil {
t.Error(err)
}
if len(values) == 0 {
t.Error("no test values")
return
}
header := values[0]
var decodeOnly bool
switch header[0] {
case "encoding":
case "non-canonical encoding":
decodeOnly = true
default:
t.Errorf("invalid test spec %q", fname)
return
}
testValue, err := strconv.Unquote("\"" + header[1] + "\"")
if err != nil {
t.Error("failed to unquote testcase:", err)
return
}
for _, testCase := range values[1:] {
encodingName := testCase[0]
expected := testCase[1]
t.Run(encodingName, func(t *testing.T) {
encoder, err := EncoderByName(encodingName)
if err != nil {
t.Skipf("skipping %s: not supported", encodingName)
return
}
if !decodeOnly {
t.Logf("encoding %q with %s", testValue, encodingName)
actual := encoder.Encode([]byte(testValue))
if expected != actual {
t.Errorf("expected %q, got %q", expected, actual)
}
}
t.Logf("decoding %q", expected)
encoding, decoded, err := Decode(expected)
if err != nil {
t.Error("failed to decode:", err)
return
}
expectedEncoding := Encodings[encodingName]
if encoding != expectedEncoding {
t.Errorf("expected encoding to be %c, got %c", expectedEncoding, encoding)
}
if string(decoded) != testValue {
t.Errorf("failed to decode %q to %q, got %q", expected, testValue, string(decoded))
}
})
}
})
}
}
func FuzzDecode(f *testing.F) {
files, err := filepath.Glob("spec/tests/*.csv")
if err != nil {
f.Fatal(err)
}
for _, fname := range files {
func() {
file, err := os.Open(fname)
if err != nil {
f.Fatal(err)
}
defer file.Close()
reader := csv.NewReader(file)
reader.LazyQuotes = false
reader.FieldsPerRecord = 2
reader.TrimLeadingSpace = true
values, err := reader.ReadAll()
if err != nil {
f.Fatal(err)
}
for _, tc := range values[1:] {
f.Add(tc[1])
}
}()
}
f.Fuzz(func(_ *testing.T, data string) {
Decode(data)
})
}