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
|
package coding_test
import (
"io"
"strings"
"testing"
"github.com/jhillyerd/enmime/internal/coding"
)
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 := coding.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)
}
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 := coding.NewBase64Cleaner(strings.NewReader(tc.input))
n, err := cleaner.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if len(cleaner.Errors) != 1 {
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)
}
})
}
}
|