forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64_test.go
68 lines (65 loc) · 1.45 KB
/
base64_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
package enmime
import (
"io"
"strings"
"testing"
)
func TestBase64Cleaner(t *testing.T) {
buf := make([]byte, 1024)
testCases := []struct {
input, want string
}{
{"", ""},
{"\tA B\r\nC", "ABC"},
{"XYZ===", "XYZ"},
}
for _, tc := range testCases {
t.Run(tc.want, func(t *testing.T) {
cleaner := newBase64Cleaner(strings.NewReader(tc.input))
n, err := cleaner.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
for _, e := range cleaner.Errors {
t.Error(e.String())
}
got := string(buf[:n])
if got != tc.want {
t.Error("got:", got, "want:", tc.want)
}
})
}
}
// TestBase64CleanerErrors sends invalid characters and tests error messages
func TestBase64CleanerErrors(t *testing.T) {
buf := make([]byte, 1024)
testCases := []struct {
input, want string
}{
{"a!", "a"},
{"@b", "b"},
{"#c", "c"},
{"d$d", "dd"},
{"ee\b", "ee"},
}
for _, tc := range testCases {
t.Run(tc.want, func(t *testing.T) {
cleaner := newBase64Cleaner(strings.NewReader(tc.input))
n, err := cleaner.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if len(cleaner.Errors) == 1 {
if cleaner.Errors[0].Name != ErrorMalformedBase64 {
t.Errorf("got: %q, want: %q", cleaner.Errors[0].Name, ErrorMalformedBase64)
}
} else {
t.Errorf("got %d Errors, wanted 1", len(cleaner.Errors))
}
got := string(buf[:n])
if got != tc.want {
t.Error("got:", got, "want:", tc.want)
}
})
}
}