forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_test.go
185 lines (172 loc) · 4.32 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
package enmime
import (
"net/textproto"
"os"
"path/filepath"
"testing"
)
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) {
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) {
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) {
t.Error("Failed to identify multipart MIME message of unknown type")
}
}
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{},
},
}
for _, s := range htests {
got := detectAttachmentHeader(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,
},
}
for _, s := range htests {
got := detectTextHeader(s.header, s.emptyIsPlain)
if got != s.want {
t.Errorf("detectTextHeader(%v, %v) == %v, want: %v",
s.header, s.emptyIsPlain, got, s.want)
}
}
}