-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathinspect_test.go
150 lines (139 loc) · 3.44 KB
/
inspect_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
package enmime_test
import (
"bytes"
"io"
"net/mail"
"net/textproto"
"strings"
"testing"
"github.com/jhillyerd/enmime/v2"
"github.com/jhillyerd/enmime/v2/internal/test"
)
func TestDecodeRFC2047(t *testing.T) {
t.Run("rfc2047 basic", func(t *testing.T) {
s := enmime.DecodeRFC2047("=?UTF-8?Q?Miros=C5=82aw_Marczak?=")
if s != "Mirosław Marczak" {
t.Error("Wrong decoded result")
}
})
t.Run("rfc2047 unknown", func(t *testing.T) {
s := enmime.DecodeRFC2047("=?ABC-1?Q?FooBar?=")
if s != "=?ABC-1?Q?FooBar?=" {
t.Error("Expected unmodified result for unknown charset")
}
})
t.Run("rfc2047 pass-through", func(t *testing.T) {
s := enmime.DecodeRFC2047("Hello World")
if s != "Hello World" {
t.Error("Expected unmodified result")
}
})
}
func TestDecodeHeaders(t *testing.T) {
t.Run("rfc2047 sample", func(t *testing.T) {
r := test.OpenTestData("mail", "qp-utf8-header.raw")
b, err := io.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
h, err := enmime.DecodeHeaders(b)
if err != nil {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("To"), "Mirosław Marczak") {
t.Error("Error decoding RFC2047 header value")
}
})
t.Run("no break between headers and content", func(t *testing.T) {
r := test.OpenTestData("mail", "qp-utf8-header-no-break.raw")
b, err := io.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
h, err := enmime.DecodeHeaders(b)
if err != nil {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("To"), "Mirosław Marczak") {
t.Error("Error decoding RFC2047 header value")
}
})
t.Run("textproto header read error", func(t *testing.T) {
r := test.OpenTestData("low-quality", "bad-header-start.raw")
b, err := io.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
_, err = enmime.DecodeHeaders(b)
switch err.(type) {
case textproto.ProtocolError:
// carry on
default:
t.Fatalf("Did return expected error: %T:%+v", err, err)
}
})
t.Run("rfc2047 recursive sample", func(t *testing.T) {
r := test.OpenTestData("mail", "qp-utf8-header-recursed.raw")
b, err := io.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
h, err := enmime.DecodeHeaders(b)
if err != nil {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("From"), "WirelessCaller (203) 402-5984 WirelessCaller (203) 402-5984 WirelessCaller (203) 402-5984") {
t.Error("Error decoding recursive RFC2047 header value")
}
})
}
func BenchmarkHumanHeadersOnly(b *testing.B) {
r := test.OpenTestData("mail", "qp-utf8-header.raw")
eml, err := io.ReadAll(r)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
h, err := enmime.DecodeHeaders(eml)
if err != nil {
b.Fatal(err)
}
_, err = mail.ParseAddressList(h.Get("From"))
if err != nil {
b.Fatal(err)
}
_, err = mail.ParseAddressList(h.Get("To"))
if err != nil {
b.Fatal(err)
}
_ = h.Get("Subject")
}
}
func BenchmarkReadEnvelope(b *testing.B) {
r := test.OpenTestData("mail", "qp-utf8-header.raw")
eml, err := io.ReadAll(r)
if err != nil {
b.Fatal(err)
}
reusedReader := bytes.NewReader(eml)
for i := 0; i < b.N; i++ {
env, err := enmime.ReadEnvelope(reusedReader)
if err != nil {
b.Fatal(err)
}
_, err = env.AddressList("From")
if err != nil {
b.Fatal(err)
}
_, err = env.AddressList("To")
if err != nil {
b.Fatal(err)
}
env.GetHeader("Subject")
// reset reader for next run
_, err = reusedReader.Seek(0, io.SeekStart)
if err != nil {
b.Fatal(err)
}
}
}