| 1 |
package coding_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/jhillyerd/enmime/internal/coding" |
| 9 |
) |
| 10 |
|
| 11 |
func TestBase64Cleaner(t *testing.T) { |
| 12 |
buf := make([]byte, 1024) |
| 13 |
testCases := []struct { |
| 14 |
input, want string |
| 15 |
}{ |
| 16 |
{"", ""}, |
| 17 |
{"\tA B\r\nC", "ABC"}, |
| 18 |
{"XYZ===", "XYZ"}, |
| 19 |
} |
| 20 |
for _, tc := range testCases { |
| 21 |
t.Run(tc.want, func(t *testing.T) { |
| 22 |
cleaner := coding.NewBase64Cleaner(strings.NewReader(tc.input)) |
| 23 |
n, err := cleaner.Read(buf) |
| 24 |
if err != nil && err != io.EOF { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
for _, e := range cleaner.Errors { |
| 28 |
t.Error(e) |
| 29 |
} |
| 30 |
got := string(buf[:n]) |
| 31 |
if got != tc.want { |
| 32 |
t.Error("got:", got, "want:", tc.want) |
| 33 |
} |
| 34 |
}) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// TestBase64CleanerErrors sends invalid characters and tests error messages |
| 39 |
func TestBase64CleanerErrors(t *testing.T) { |
| 40 |
buf := make([]byte, 1024) |
| 41 |
testCases := []struct { |
| 42 |
input, want string |
| 43 |
}{ |
| 44 |
{"a!", "a"}, |
| 45 |
{"@b", "b"}, |
| 46 |
{"#c", "c"}, |
| 47 |
{"d$d", "dd"}, |
| 48 |
{"ee\b", "ee"}, |
| 49 |
} |
| 50 |
for _, tc := range testCases { |
| 51 |
t.Run(tc.want, func(t *testing.T) { |
| 52 |
cleaner := coding.NewBase64Cleaner(strings.NewReader(tc.input)) |
| 53 |
n, err := cleaner.Read(buf) |
| 54 |
if err != nil && err != io.EOF { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
if len(cleaner.Errors) != 1 { |
| 58 |
t.Errorf("got %d Errors, wanted 1", len(cleaner.Errors)) |
| 59 |
} |
| 60 |
got := string(buf[:n]) |
| 61 |
if got != tc.want { |
| 62 |
t.Error("got:", got, "want:", tc.want) |
| 63 |
} |
| 64 |
}) |
| 65 |
} |
| 66 |
} |