-
Notifications
You must be signed in to change notification settings - Fork 13
/
utfbom_test.go
351 lines (293 loc) · 8.7 KB
/
utfbom_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
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
package utfbom_test
import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
"testing"
"testing/iotest"
"time"
"github.com/dimchansky/utfbom"
)
var testCases = []struct {
name string
input []byte
inputError error
encoding utfbom.Encoding
output []byte
}{
{"1", []byte{}, nil, utfbom.Unknown, []byte{}},
{"2", []byte("hello"), nil, utfbom.Unknown, []byte("hello")},
{"3", []byte("\xEF\xBB\xBF"), nil, utfbom.UTF8, []byte{}},
{"4", []byte("\xEF\xBB\xBFhello"), nil, utfbom.UTF8, []byte("hello")},
{"5", []byte("\xFE\xFF"), nil, utfbom.UTF16BigEndian, []byte{}},
{"6", []byte("\xFF\xFE"), nil, utfbom.UTF16LittleEndian, []byte{}},
{"7", []byte("\x00\x00\xFE\xFF"), nil, utfbom.UTF32BigEndian, []byte{}},
{"8", []byte("\xFF\xFE\x00\x00"), nil, utfbom.UTF32LittleEndian, []byte{}},
{
"5", []byte("\xFE\xFF\x00\x68\x00\x65\x00\x6C\x00\x6C\x00\x6F"), nil,
utfbom.UTF16BigEndian,
[]byte{0x00, 0x68, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F},
},
{
"6", []byte("\xFF\xFE\x68\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00"), nil,
utfbom.UTF16LittleEndian,
[]byte{0x68, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00},
},
{
"7", []byte("\x00\x00\xFE\xFF\x00\x00\x00\x68\x00\x00\x00\x65\x00\x00\x00\x6C\x00\x00\x00\x6C\x00\x00\x00\x6F"), nil,
utfbom.UTF32BigEndian,
[]byte{0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F},
},
{
"8", []byte("\xFF\xFE\x00\x00\x68\x00\x00\x00\x65\x00\x00\x00\x6C\x00\x00\x00\x6C\x00\x00\x00\x6F\x00\x00\x00"), nil,
utfbom.UTF32LittleEndian,
[]byte{0x68, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00},
},
{"9", []byte("\xEF"), nil, utfbom.Unknown, []byte("\xEF")},
{"10", []byte("\xEF\xBB"), nil, utfbom.Unknown, []byte("\xEF\xBB")},
{"11", []byte("\xEF\xBB\xBF"), io.ErrClosedPipe, utfbom.UTF8, []byte{}},
{"12", []byte("\xFE\xFF"), io.ErrClosedPipe, utfbom.Unknown, []byte("\xFE\xFF")},
{"13", []byte("\xFE"), io.ErrClosedPipe, utfbom.Unknown, []byte("\xFE")},
{"14", []byte("\xFF\xFE"), io.ErrClosedPipe, utfbom.Unknown, []byte("\xFF\xFE")},
{"15", []byte("\x00\x00\xFE\xFF"), io.ErrClosedPipe, utfbom.UTF32BigEndian, []byte{}},
{"16", []byte("\x00\x00\xFE"), io.ErrClosedPipe, utfbom.Unknown, []byte{0x00, 0x00, 0xFE}},
{"17", []byte("\x00\x00"), io.ErrClosedPipe, utfbom.Unknown, []byte{0x00, 0x00}},
{"18", []byte("\x00"), io.ErrClosedPipe, utfbom.Unknown, []byte{0x00}},
{"19", []byte("\xFF\xFE\x00\x00"), io.ErrClosedPipe, utfbom.UTF32LittleEndian, []byte{}},
{"20", []byte("\xFF\xFE\x00"), io.ErrClosedPipe, utfbom.Unknown, []byte{0xFF, 0xFE, 0x00}},
{"21", []byte("\xFF\xFE"), io.ErrClosedPipe, utfbom.Unknown, []byte{0xFF, 0xFE}},
{"22", []byte("\xFF"), io.ErrClosedPipe, utfbom.Unknown, []byte{0xFF}},
{"23", []byte("\x68\x65"), nil, utfbom.Unknown, []byte{0x68, 0x65}},
}
type sliceReader struct {
input []byte
inputError error
}
func (r *sliceReader) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return
}
if err = r.getError(); err != nil {
return
}
n = copy(p, r.input)
r.input = r.input[n:]
err = r.getError()
return
}
func (r *sliceReader) getError() (err error) {
if len(r.input) == 0 {
if r.inputError == nil {
err = io.EOF
} else {
err = r.inputError
}
}
return
}
var readMakers = []struct {
name string
fn func(io.Reader) io.Reader
}{
{"full", func(r io.Reader) io.Reader { return r }},
{"byte", iotest.OneByteReader},
}
func TestSkip(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
tc := tc
t.Run("test "+tc.name, func(t *testing.T) {
t.Parallel()
for _, readMaker := range readMakers {
readMaker := readMaker
t.Run("reader="+readMaker.name, func(t *testing.T) {
t.Parallel()
r := readMaker.fn(&sliceReader{tc.input, tc.inputError})
sr, enc := utfbom.Skip(r)
if enc != tc.encoding {
t.Fatalf("expected encoding %v, but got %v", tc.encoding, enc)
}
output, err := io.ReadAll(sr)
if !reflect.DeepEqual(output, tc.output) {
t.Fatalf("expected to read %+#v, but got %+#v", tc.output, output)
}
if !errors.Is(err, tc.inputError) {
t.Fatalf("expected to get %+#v error, but got %+#v", tc.inputError, err)
}
})
}
})
}
}
func TestSkipSkip(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
tc := tc
t.Run("test "+tc.name, func(t *testing.T) {
t.Parallel()
for _, readMaker := range readMakers {
readMaker := readMaker
t.Run("reader="+readMaker.name, func(t *testing.T) {
t.Parallel()
r := readMaker.fn(&sliceReader{tc.input, tc.inputError})
sr0, _ := utfbom.Skip(r)
sr, enc := utfbom.Skip(sr0)
if enc != tc.encoding {
t.Fatalf("expected encoding %v, but got %v", tc.encoding, enc)
}
output, err := io.ReadAll(sr)
if !reflect.DeepEqual(output, tc.output) {
t.Fatalf("expected to read %+#v, but got %+#v", tc.output, output)
}
if !errors.Is(err, tc.inputError) {
t.Fatalf("expected to get %+#v error, but got %+#v", tc.inputError, err)
}
})
}
})
}
}
func TestSkipOnly(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
tc := tc
t.Run("test "+tc.name, func(t *testing.T) {
t.Parallel()
for _, readMaker := range readMakers {
readMaker := readMaker
t.Run("reader="+readMaker.name, func(t *testing.T) {
t.Parallel()
r := readMaker.fn(&sliceReader{tc.input, tc.inputError})
sr := utfbom.SkipOnly(r)
output, err := io.ReadAll(sr)
if !reflect.DeepEqual(output, tc.output) {
t.Fatalf("expected to read %+#v, but got %+#v", tc.output, output)
}
if !errors.Is(err, tc.inputError) {
t.Fatalf("expected to get %+#v error, but got %+#v", tc.inputError, err)
}
})
}
})
}
}
type zeroReader struct{}
func (zeroReader) Read(p []byte) (int, error) {
return 0, nil
}
type readerEncoding struct {
Rd *utfbom.Reader
Enc utfbom.Encoding
}
func TestSkipZeroReader(t *testing.T) {
t.Parallel()
var z zeroReader
c := make(chan readerEncoding)
go func() {
r, enc := utfbom.Skip(z)
c <- readerEncoding{r, enc}
}()
select {
case re := <-c:
if re.Enc != utfbom.Unknown {
t.Error("utfbom.Unknown encoding expected")
} else {
var b [1]byte
n, err := re.Rd.Read(b[:])
if n != 0 {
t.Error("unexpected bytes count:", n)
}
if err != io.ErrNoProgress {
t.Error("unexpected error:", err)
}
}
case <-time.After(time.Second):
t.Error("test timed out (endless loop in utfbom.Skip?)")
}
}
func TestSkipOnlyZeroReader(t *testing.T) {
t.Parallel()
var z zeroReader
c := make(chan *utfbom.Reader)
go func() {
r := utfbom.SkipOnly(z)
c <- r
}()
select {
case r := <-c:
var b [1]byte
n, err := r.Read(b[:])
if n != 0 {
t.Error("unexpected bytes count:", n)
}
if err != io.ErrNoProgress {
t.Error("unexpected error:", err)
}
case <-time.After(time.Second):
t.Error("test timed out (endless loop in utfbom.Skip?)")
}
}
func TestReader_ReadEmpty(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
tc := tc
t.Run("test "+tc.name, func(t *testing.T) {
t.Parallel()
for _, readMaker := range readMakers {
readMaker := readMaker
t.Run("reader="+readMaker.name, func(t *testing.T) {
t.Parallel()
r := readMaker.fn(&sliceReader{tc.input, tc.inputError})
sr := utfbom.SkipOnly(r)
n, err := sr.Read(nil)
if n != 0 {
t.Fatalf("test %v reader=%s: expected to read zero bytes, but got %v", tc.name, readMaker.name, n)
}
if err != nil {
t.Fatalf("test %v reader=%s: expected to get <nil> error, but got %+#v", tc.name, readMaker.name, err)
}
})
}
})
}
}
func TestEncoding_String(t *testing.T) {
t.Parallel()
for e := utfbom.Unknown; e <= utfbom.UTF32LittleEndian; e++ {
s := e.String()
if s == "" {
t.Errorf("no string for %#v", e)
}
}
s := utfbom.Encoding(999).String()
if s != "Unknown" {
t.Errorf("wrong string '%s' for invalid encoding", s)
}
}
func ExampleSkipOnly() {
byteData := []byte("\xEF\xBB\xBFhello")
fmt.Println("Input:", byteData)
output, err := io.ReadAll(utfbom.SkipOnly(bytes.NewReader(byteData)))
if err != nil {
panic(err)
}
fmt.Println("ReadAll with BOM skipping", output)
// Output:
// Input: [239 187 191 104 101 108 108 111]
// ReadAll with BOM skipping [104 101 108 108 111]
}
func ExampleSkip() {
byteData := []byte("\xEF\xBB\xBFhello")
sr, enc := utfbom.Skip(bytes.NewReader(byteData))
fmt.Printf("Detected encoding: %s\n", enc)
output, err := io.ReadAll(sr)
if err != nil {
panic(err)
}
fmt.Println("ReadAll with BOM detection and skipping", output)
// Output:
// Detected encoding: UTF8
// ReadAll with BOM detection and skipping [104 101 108 108 111]
}