forked from sorbits/go.enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_test.go
315 lines (268 loc) · 12.5 KB
/
part_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package enmime
import (
"bufio"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPlainTextPart(t *testing.T) {
r := openPart("textplain.raw")
p, err := ParseMIME(r)
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "7bit", p.Header().Get("Content-Transfer-Encoding"),
"Exepcted Header to have data")
assert.Equal(t, "text/plain", p.ContentType(), "Expected type to be set")
assert.Contains(t, string(p.Content()), "Test of text/plain section",
"Expected correct data in p.Content")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
}
func TestQuotedPrintablePart(t *testing.T) {
r := openPart("quoted-printable.raw")
p, err := ParseMIME(r)
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "quoted-printable", p.Header().Get("Content-Transfer-Encoding"),
"Exepcted Header to have data")
assert.Equal(t, "text/plain", p.ContentType(), "Expected type to be set")
assert.Equal(t, "Start=ABC=Finish", string(p.Content()),
"Expected correct data in p.Content")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
}
func TestMultiAlternParts(t *testing.T) {
r := openPart("multialtern.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/alternative", p.ContentType(), "Expected type to be set")
assert.Equal(t, 0, len(p.Content()), "Root should not have Content")
assert.NotNil(t, p.FirstChild(), "Root should have a FirstChild")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.Contains(t, string(p.Content()), "A text section", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/html", p.ContentType(), "Second child should have been html")
assert.Contains(t, string(p.Content()), "An HTML section", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
func TestMultiMixedParts(t *testing.T) {
r := openPart("multimixed.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/mixed", p.ContentType(), "Expected type to be set")
assert.Equal(t, 0, len(p.Content()), "Root should not have Content")
assert.NotNil(t, p.FirstChild(), "Root should have a FirstChild")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.Contains(t, string(p.Content()), "Section one", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/plain", p.ContentType(), "Second child should have been html")
assert.Contains(t, string(p.Content()), "Section two", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
func TestMultiOtherParts(t *testing.T) {
r := openPart("multiother.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/x-enmime", p.ContentType(), "Expected type to be set")
assert.Equal(t, 0, len(p.Content()), "Root should not have Content")
assert.NotNil(t, p.FirstChild(), "Root should have a FirstChild")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.Contains(t, string(p.Content()), "Section one", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/plain", p.ContentType(), "Second child should have been html")
assert.Contains(t, string(p.Content()), "Section two", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
func TestNestedAlternParts(t *testing.T) {
r := openPart("nestedmulti.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/alternative", p.ContentType(), "Expected type to be set")
assert.Equal(t, 0, len(p.Content()), "Root should not have Content")
assert.NotNil(t, p.FirstChild(), "Root should have a FirstChild")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.Contains(t, string(p.Content()), "A text section", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "multipart/related", p.ContentType(), "Second child should have been another multipart")
assert.Equal(t, 0, len(p.Content()), "Second child should not have Content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
assert.NotNil(t, p.FirstChild(), "Second child should have a child")
// First nested
p = p.FirstChild()
assert.Equal(t, "text/html", p.ContentType(), "First nested should have been html")
assert.Contains(t, string(p.Content()), "An HTML section", "First nested contains wrong content")
assert.NotNil(t, p.NextSibling(), "First nested should have a sibling")
// Second nested
p = p.NextSibling()
assert.Equal(t, "text/plain", p.ContentType(), "Second nested should have been text")
assert.Equal(t, "inline", p.Disposition(), "Second nested should be inline disposition")
assert.Equal(t, "attach.txt", p.FileName(), "Second nested should have correct filename")
assert.Contains(t, string(p.Content()), "An inline text attachment", "Second nested contains wrong content")
assert.NotNil(t, p.NextSibling(), "Second nested should have a sibling")
// Third nested
p = p.NextSibling()
assert.Equal(t, "text/plain", p.ContentType(), "Third nested should have been text")
assert.Equal(t, "inline", p.Disposition(), "Third nested should be inline disposition")
assert.Equal(t, "attach2.txt", p.FileName(), "Third nested should have correct filename")
assert.Contains(t, string(p.Content()), "Another inline text attachment",
"Third nested contains wrong content")
assert.Nil(t, p.NextSibling(), "Third nested should not have a sibling")
}
func TestMultiBase64Parts(t *testing.T) {
r := openPart("multibase64.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/mixed", p.ContentType(), "Expected type to be set")
assert.Equal(t, 0, len(p.Content()), "Root should not have Content")
assert.NotNil(t, p.FirstChild(), "Root should have a FirstChild")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.Contains(t, string(p.Content()), "A text section", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/html", p.ContentType(), "Second child should be html")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
assert.Nil(t, p.FirstChild(), "Second child should not have a child")
assert.Contains(t, string(p.Content()), "<html>",
"Second child should have <html> as decoded content")
}
func TestBadBoundaryTerm(t *testing.T) {
r := openPart("badboundary.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/alternative", p.ContentType(), "Expected type to be set")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/html", p.ContentType(), "Second child should have been html")
assert.Contains(t, string(p.Content()), "An HTML section", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
// new tests from forked version
func TestNoHeaderPart(t *testing.T) {
r := openPart("noheader.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/alternative", p.ContentType(), "Expected type to be set")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been default text")
assert.Contains(t, string(p.Content()), "A text section", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/html", p.ContentType(), "Second child should have been html")
assert.Contains(t, string(p.Content()), "An HTML section", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
func TestCorruptHeaderPart(t *testing.T) {
r := openPart("corruptheader.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/mixed", p.ContentType(), "Expected type to be set")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been default text")
assert.Contains(t, string(p.Content()), "A text section", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/plain", p.ContentType(), "Second child should have been default to text")
assert.Contains(t, string(p.Content()), "An HTML section", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
func TestMultiAnyParts(t *testing.T) {
r := openPart("multiany.raw")
p, err := ParseMIME(r)
// Examine root
if !assert.Nil(t, err, "Parsing should not have generated an error") {
t.FailNow()
}
assert.NotNil(t, p, "Root node should not be nil")
assert.Equal(t, "multipart/reports", p.ContentType(), "Expected type to be set")
assert.Equal(t, 0, len(p.Content()), "Root should not have Content")
assert.NotNil(t, p.FirstChild(), "Root should have a FirstChild")
assert.Nil(t, p.NextSibling(), "Root should never have a sibling")
// Examine first child
p = p.FirstChild()
assert.Equal(t, "text/plain", p.ContentType(), "First child should have been text")
assert.Contains(t, string(p.Content()), "Section one", "First child contains wrong content")
assert.NotNil(t, p.NextSibling(), "First child should have a sibling")
// Examine sibling
p = p.NextSibling()
assert.Equal(t, "text/plain", p.ContentType(), "Second child should have been html")
assert.Contains(t, string(p.Content()), "Section two", "Second child contains wrong content")
assert.Nil(t, p.NextSibling(), "Second child should not have a sibling")
}
// openPart is a test utility function to open a part as a reader
func openPart(filename string) *bufio.Reader {
// Open test part for parsing
raw, err := os.Open(filepath.Join("test-data", "parts", filename))
if err != nil {
panic(fmt.Sprintf("Failed to open test data: %v", err))
}
// Wrap in a buffer
return bufio.NewReader(raw)
}