Debian Codegraph @

detect_test.go

raw
1 package enmime
2
3 import (
4 "net/textproto"
5 "os"
6 "path/filepath"
7 "testing"
8 )
9
10 func TestDetectSinglePart(t *testing.T) {
11 r, _ := os.Open(filepath.Join("testdata", "mail", "non-mime.raw"))
12 msg, err := ReadParts(r)
13 if err != nil {
14 t.Fatal(err)
15 }
16
17 if detectMultipartMessage(msg) {
18 t.Error("Failed to identify non-multipart message")
19 }
20 }
21
22 func TestDetectMultiPart(t *testing.T) {
23 r, _ := os.Open(filepath.Join("testdata", "mail", "html-mime-inline.raw"))
24 msg, err := ReadParts(r)
25 if err != nil {
26 t.Fatal(err)
27 }
28
29 if !detectMultipartMessage(msg) {
30 t.Error("Failed to identify multipart MIME message")
31 }
32 }
33
34 func TestDetectUnknownMultiPart(t *testing.T) {
35 r, _ := os.Open(filepath.Join("testdata", "mail", "unknown-part-type.raw"))
36 msg, err := ReadParts(r)
37 if err != nil {
38 t.Fatal(err)
39 }
40
41 if !detectMultipartMessage(msg) {
42 t.Error("Failed to identify multipart MIME message of unknown type")
43 }
44 }
45
46 func TestDetectBinaryBody(t *testing.T) {
47 ttable := []struct {
48 filename string
49 disposition string
50 }{
51 {filename: "attachment-only.raw", disposition: "attachment"},
52 {filename: "attachment-only-inline.raw", disposition: "inline"},
53 {filename: "attachment-only-no-disposition.raw", disposition: "none"},
54 {filename: "attachment-only-text-attachment.raw", disposition: "attachment"},
55 }
56 for _, tt := range ttable {
57 r, _ := os.Open(filepath.Join("testdata", "mail", tt.filename))
58 root, err := ReadParts(r)
59 if err != nil {
60 t.Fatal(err)
61 }
62
63 if !detectBinaryBody(root) {
64 t.Errorf("Failed to identify binary body %s in %q", tt.disposition, tt.filename)
65 }
66 }
67 }
68
69 func TestDetectAttachmentHeader(t *testing.T) {
70 var htests = []struct {
71 want bool
72 header textproto.MIMEHeader
73 }{
74 {
75 want: true,
76 header: textproto.MIMEHeader{
77 "Content-Disposition": []string{"attachment; filename=\"test.jpg\""}},
78 },
79 {
80 want: true,
81 header: textproto.MIMEHeader{
82 "Content-Disposition": []string{"ATTACHMENT; filename=\"test.jpg\""}},
83 },
84 {
85 want: true,
86 header: textproto.MIMEHeader{
87 "Content-Type": []string{"image/jpg; name=\"test.jpg\""},
88 "Content-Disposition": []string{"attachment; filename=\"test.jpg\""},
89 },
90 },
91 {
92 want: true,
93 header: textproto.MIMEHeader{
94 "Content-Type": []string{"attachment; filename=\"test.jpg\""}},
95 },
96 {
97 want: false,
98 header: textproto.MIMEHeader{
99 "Content-Disposition": []string{"inline"}},
100 },
101 {
102 want: false,
103 header: textproto.MIMEHeader{
104 "Content-Disposition": []string{"inline; broken"}},
105 },
106 {
107 want: true,
108 header: textproto.MIMEHeader{
109 "Content-Disposition": []string{"attachment; broken"}},
110 },
111 {
112 want: true,
113 header: textproto.MIMEHeader{
114 "Content-Disposition": []string{"inline; filename=\"frog.jpg\""}},
115 },
116 {
117 want: false,
118 header: textproto.MIMEHeader{
119 "Content-Disposition": []string{"non-attachment; filename=\"frog.jpg\""}},
120 },
121 {
122 want: false,
123 header: textproto.MIMEHeader{},
124 },
125 }
126
127 for _, s := range htests {
128 got := detectAttachmentHeader(s.header)
129 if got != s.want {
130 t.Errorf("detectAttachmentHeader(%v) == %v, want: %v", s.header, got, s.want)
131 }
132 }
133 }
134
135 func TestDetectTextHeader(t *testing.T) {
136 var htests = []struct {
137 want bool
138 header textproto.MIMEHeader
139 emptyIsPlain bool
140 }{
141 {
142 want: true,
143 header: textproto.MIMEHeader{"Content-Type": []string{"text/plain"}},
144 emptyIsPlain: true,
145 },
146 {
147 want: true,
148 header: textproto.MIMEHeader{"Content-Type": []string{"text/html"}},
149 emptyIsPlain: true,
150 },
151 {
152 want: true,
153 header: textproto.MIMEHeader{"Content-Type": []string{"text/html; charset=utf-8"}},
154 emptyIsPlain: true,
155 },
156 {
157 want: true,
158 header: textproto.MIMEHeader{},
159 emptyIsPlain: true,
160 },
161 {
162 want: false,
163 header: textproto.MIMEHeader{},
164 emptyIsPlain: false,
165 },
166 {
167 want: false,
168 header: textproto.MIMEHeader{"Content-Type": []string{"image/jpeg;"}},
169 emptyIsPlain: true,
170 },
171 {
172 want: false,
173 header: textproto.MIMEHeader{"Content-Type": []string{"application/octet-stream"}},
174 emptyIsPlain: true,
175 },
176 }
177
178 for _, s := range htests {
179 got := detectTextHeader(s.header, s.emptyIsPlain)
180 if got != s.want {
181 t.Errorf("detectTextHeader(%v, %v) == %v, want: %v",
182 s.header, s.emptyIsPlain, got, s.want)
183 }
184 }
185 }