| 1 |
package enmime |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestErrorStringConversion(t *testing.T) { |
| 10 |
e := &Error{ |
| 11 |
Name: "WarnName", |
| 12 |
Detail: "Warn Details", |
| 13 |
Severe: false, |
| 14 |
} |
| 15 |
|
| 16 |
want := "[W] WarnName: Warn Details" |
| 17 |
got := e.Error() |
| 18 |
if got != want { |
| 19 |
t.Error("got:", got, "want:", want) |
| 20 |
} |
| 21 |
|
| 22 |
e = &Error{ |
| 23 |
Name: "ErrorName", |
| 24 |
Detail: "Error Details", |
| 25 |
Severe: true, |
| 26 |
} |
| 27 |
|
| 28 |
// Using deprecated String() method here for test coverage |
| 29 |
want = "[E] ErrorName: Error Details" |
| 30 |
got = e.String() |
| 31 |
if got != want { |
| 32 |
t.Error("got:", got, "want:", want) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func TestErrorAddError(t *testing.T) { |
| 37 |
p := &Part{} |
| 38 |
p.addError(ErrorMalformedHeader, "1 %v %q", 2, "three") |
| 39 |
|
| 40 |
if len(p.Errors) != 1 { |
| 41 |
t.Fatal("len(p.Errors) ==", len(p.Errors), ", want: 1") |
| 42 |
} |
| 43 |
e := p.Errors[0] |
| 44 |
|
| 45 |
if e.Name != ErrorMalformedHeader { |
| 46 |
t.Errorf("e.Name == %q, want: %q", e.Name, ErrorMalformedHeader) |
| 47 |
} |
| 48 |
if !e.Severe { |
| 49 |
t.Errorf("e.Severe == %v, want: true", e.Severe) |
| 50 |
} |
| 51 |
want := "1 2 \"three\"" |
| 52 |
if e.Detail != want { |
| 53 |
t.Errorf("e.Detail == %q, want: %q", e.Detail, want) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestErrorAddWarning(t *testing.T) { |
| 58 |
p := &Part{} |
| 59 |
p.addWarning(ErrorMalformedHeader, "1 %v %q", 2, "three") |
| 60 |
|
| 61 |
if len(p.Errors) != 1 { |
| 62 |
t.Fatal("len(p.Errors) ==", len(p.Errors), ", want: 1") |
| 63 |
} |
| 64 |
e := p.Errors[0] |
| 65 |
|
| 66 |
if e.Name != ErrorMalformedHeader { |
| 67 |
t.Errorf("e.Name == %q, want: %q", e.Name, ErrorMalformedHeader) |
| 68 |
} |
| 69 |
if e.Severe { |
| 70 |
t.Errorf("e.Severe == %v, want: false", e.Severe) |
| 71 |
} |
| 72 |
want := "1 2 \"three\"" |
| 73 |
if e.Detail != want { |
| 74 |
t.Errorf("e.Detail == %q, want: %q", e.Detail, want) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestErrorEnvelopeWarnings(t *testing.T) { |
| 79 |
// To pass each file below must error one or more times with the specified errorName, and no |
| 80 |
// other errorNames. |
| 81 |
var files = []struct { |
| 82 |
filename string |
| 83 |
perror string |
| 84 |
}{ |
| 85 |
{"bad-final-boundary.raw", ErrorMissingBoundary}, |
| 86 |
{"bad-header-wrap.raw", ErrorMalformedHeader}, |
| 87 |
{"html-only-inline.raw", ErrorPlainTextFromHTML}, |
| 88 |
{"missing-content-type2.raw", ErrorMissingContentType}, |
| 89 |
{"empty-header.raw", ErrorMissingContentType}, |
| 90 |
{"unk-encoding-part.raw", ErrorContentEncoding}, |
| 91 |
{"unk-charset-html-only.raw", ErrorCharsetConversion}, |
| 92 |
{"unk-charset-part.raw", ErrorCharsetConversion}, |
| 93 |
{"malformed-base64-attach.raw", ErrorMalformedBase64}, |
| 94 |
{"incorrect-charset.raw", ErrorCharsetDeclaration}, |
| 95 |
} |
| 96 |
|
| 97 |
for _, tt := range files { |
| 98 |
t.Run(tt.filename, func(t *testing.T) { |
| 99 |
r, _ := os.Open(filepath.Join("testdata", "low-quality", tt.filename)) |
| 100 |
e, err := ReadEnvelope(r) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("Failed to parse MIME: %+v", err) |
| 103 |
} |
| 104 |
|
| 105 |
if len(e.Errors) == 0 { |
| 106 |
t.Error("Got 0 warnings, expected at least one for:", tt.filename) |
| 107 |
} |
| 108 |
|
| 109 |
satisfied := false |
| 110 |
for _, perr := range e.Errors { |
| 111 |
if perr.Name == tt.perror { |
| 112 |
satisfied = true |
| 113 |
if perr.Severe { |
| 114 |
t.Errorf("Expected Severe to be false, got true for %q", perr.Name) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
if !satisfied { |
| 119 |
var errorList string |
| 120 |
for _, perr := range e.Errors { |
| 121 |
errorList += perr.Error() |
| 122 |
errorList += "\n" |
| 123 |
} |
| 124 |
t.Errorf( |
| 125 |
"File %q should have error of type %q, got these instead:\n%s", |
| 126 |
tt.filename, |
| 127 |
tt.perror, |
| 128 |
errorList) |
| 129 |
} |
| 130 |
}) |
| 131 |
} |
| 132 |
} |