-
Notifications
You must be signed in to change notification settings - Fork 103
/
error_test.go
171 lines (150 loc) · 3.94 KB
/
error_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
package enmime
import (
"os"
"path/filepath"
"testing"
)
func TestErrorStringConversion(t *testing.T) {
e := &Error{
Name: "WarnName",
Detail: "Warn Details",
Severe: false,
}
want := "[W] WarnName: Warn Details"
got := e.Error()
if got != want {
t.Error("got:", got, "want:", want)
}
e = &Error{
Name: "ErrorName",
Detail: "Error Details",
Severe: true,
}
// Using deprecated String() method here for test coverage
want = "[E] ErrorName: Error Details"
got = e.String()
if got != want {
t.Error("got:", got, "want:", want)
}
}
func TestErrorAddError(t *testing.T) {
p := &Part{}
p.addError(ErrorMalformedHeader, "1 %v %q", 2, "three")
if len(p.Errors) != 1 {
t.Fatal("len(p.Errors) ==", len(p.Errors), ", want: 1")
}
e := p.Errors[0]
if e.Name != ErrorMalformedHeader {
t.Errorf("e.Name == %q, want: %q", e.Name, ErrorMalformedHeader)
}
if !e.Severe {
t.Errorf("e.Severe == %v, want: true", e.Severe)
}
want := "1 2 \"three\""
if e.Detail != want {
t.Errorf("e.Detail == %q, want: %q", e.Detail, want)
}
}
func TestErrorAddWarning(t *testing.T) {
p := &Part{}
p.addWarning(ErrorMalformedHeader, "1 %v %q", 2, "three")
if len(p.Errors) != 1 {
t.Fatal("len(p.Errors) ==", len(p.Errors), ", want: 1")
}
e := p.Errors[0]
if e.Name != ErrorMalformedHeader {
t.Errorf("e.Name == %q, want: %q", e.Name, ErrorMalformedHeader)
}
if e.Severe {
t.Errorf("e.Severe == %v, want: false", e.Severe)
}
want := "1 2 \"three\""
if e.Detail != want {
t.Errorf("e.Detail == %q, want: %q", e.Detail, want)
}
}
func TestErrorEnvelopeWarnings(t *testing.T) {
// To pass each file below must error one or more times with the specified errorName, and no
// other errorNames.
var files = []struct {
filename string
perror string
}{
{"bad-final-boundary.raw", ErrorMissingBoundary},
{"bad-header-wrap.raw", ErrorMalformedHeader},
{"html-only-inline.raw", ErrorPlainTextFromHTML},
{"missing-content-type2.raw", ErrorMissingContentType},
{"empty-header.raw", ErrorMissingContentType},
{"unk-encoding-part.raw", ErrorContentEncoding},
{"unk-charset-html-only.raw", ErrorCharsetConversion},
{"unk-charset-part.raw", ErrorCharsetConversion},
{"malformed-base64-attach.raw", ErrorMalformedBase64},
{"incorrect-charset.raw", ErrorCharsetDeclaration},
}
for _, tt := range files {
t.Run(tt.filename, func(t *testing.T) {
r, _ := os.Open(filepath.Join("testdata", "low-quality", tt.filename))
e, err := ReadEnvelope(r)
if err != nil {
t.Fatalf("Failed to parse MIME: %+v", err)
}
if len(e.Errors) == 0 {
t.Error("Got 0 warnings, expected at least one for:", tt.filename)
}
satisfied := false
for _, perr := range e.Errors {
if perr.Name == tt.perror {
satisfied = true
if perr.Severe {
t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
}
}
}
if !satisfied {
var errorList string
for _, perr := range e.Errors {
errorList += perr.Error()
errorList += "\n"
}
t.Errorf(
"File %q should have error of type %q, got these instead:\n%s",
tt.filename,
tt.perror,
errorList)
}
})
}
}
func TestErrorLimit(t *testing.T) {
// Backup global variable
originalMaxPartErros := MaxPartErrors
defer func() {
MaxPartErrors = originalMaxPartErros
}()
addThreeErrors := func() int {
part := &Part{}
part.addError("test1", "test1")
part.addError("test2", "test2")
part.addError("test3", "test3")
return len(part.Errors)
}
// Check unlimited
var errCount int
MaxPartErrors = 0
errCount = addThreeErrors()
if errCount != 3 {
t.Errorf("Expected unlimited errors (3), got %d", errCount)
}
// Check limit
MaxPartErrors = 1
errCount = addThreeErrors()
if errCount != 1 {
t.Errorf("Expected limited errors (1), got %d", errCount)
}
// Check limit matching count
MaxPartErrors = 3
errCount = addThreeErrors()
if errCount != 3 {
t.Errorf("Expected limited errors (3), got %d", errCount)
}
}