-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathdetect_test.go
206 lines (187 loc) · 4.89 KB
/
detect_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package enmime
import (
"os"
"path/filepath"
"testing"
"github.com/jhillyerd/enmime/v2/internal/textproto"
)
func TestDetectSinglePart(t *testing.T) {
r, _ := os.Open(filepath.Join("testdata", "mail", "non-mime.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if detectMultipartMessage(msg, false) {
t.Error("Failed to identify non-multipart message")
}
}
func TestDetectMultiPart(t *testing.T) {
r, _ := os.Open(filepath.Join("testdata", "mail", "html-mime-inline.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !detectMultipartMessage(msg, false) {
t.Error("Failed to identify multipart MIME message")
}
}
func TestDetectUnknownMultiPart(t *testing.T) {
r, _ := os.Open(filepath.Join("testdata", "mail", "unknown-part-type.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !detectMultipartMessage(msg, false) {
t.Error("Failed to identify multipart MIME message of unknown type")
}
}
func TestDetectMultipartWithoutBoundary(t *testing.T) {
r, _ := os.Open(filepath.Join("testdata", "mail", "multipart-wo-boundary.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !detectMultipartMessage(msg, false) {
t.Error("Failed to identify multipart MIME message")
}
if detectMultipartMessage(msg, true) {
t.Error("Failed to identify multipart MIME message without boundaries as single-part")
}
}
func TestDetectBinaryBody(t *testing.T) {
ttable := []struct {
filename string
disposition string
}{
{filename: "attachment-only.raw", disposition: "attachment"},
{filename: "attachment-only-inline.raw", disposition: "inline"},
{filename: "attachment-only-no-disposition.raw", disposition: "none"},
{filename: "attachment-only-text-attachment.raw", disposition: "attachment"},
}
for _, tt := range ttable {
r, _ := os.Open(filepath.Join("testdata", "mail", tt.filename))
root, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !detectBinaryBody(root) {
t.Errorf("Failed to identify binary body %s in %q", tt.disposition, tt.filename)
}
}
}
func TestDetectAttachmentHeader(t *testing.T) {
var htests = []struct {
want bool
header textproto.MIMEHeader
}{
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"attachment; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"ATTACHMENT; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Type": []string{"image/jpg; name=\"test.jpg\""},
"Content-Disposition": []string{"attachment; filename=\"test.jpg\""},
},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Type": []string{"attachment; filename=\"test.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"inline"}},
},
{
want: false,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"inline; broken"}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"attachment; broken"}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"inline; filename=\"frog.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"non-attachment; filename=\"frog.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{},
},
}
root := &Part{parser: &defaultParser}
for _, s := range htests {
got := detectAttachmentHeader(root, s.header)
if got != s.want {
t.Errorf("detectAttachmentHeader(%v) == %v, want: %v", s.header, got, s.want)
}
}
}
func TestDetectTextHeader(t *testing.T) {
var htests = []struct {
want bool
header textproto.MIMEHeader
emptyIsPlain bool
}{
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/plain"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/html"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/html; charset=utf-8"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{},
emptyIsPlain: true,
},
{
want: false,
header: textproto.MIMEHeader{},
emptyIsPlain: false,
},
{
want: false,
header: textproto.MIMEHeader{"Content-Type": []string{"image/jpeg;"}},
emptyIsPlain: true,
},
{
want: false,
header: textproto.MIMEHeader{"Content-Type": []string{"application/octet-stream"}},
emptyIsPlain: true,
},
}
root := &Part{parser: &defaultParser}
for _, s := range htests {
got := detectTextHeader(root, s.header, s.emptyIsPlain)
if got != s.want {
t.Errorf("detectTextHeader(%v, %v) == %v, want: %v",
s.header, s.emptyIsPlain, got, s.want)
}
}
}