Debian Codegraph @

header_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package enmime
2
3 import (
4 "bufio"
5 "net/textproto"
6 "strings"
7 "testing"
8 )
9
10 // Test re-encoding to base64
11 func TestDecodeToUTF8Base64Header(t *testing.T) {
12 var testTable = []struct {
13 in, want string
14 }{
15 {"no encoding", "no encoding"},
16 {"=?utf-8?q?abcABC_=24_=c2=a2_=e2=82=ac?=", "=?UTF-8?b?YWJjQUJDICQgwqIg4oKs?="},
17 {"=?iso-8859-1?q?#=a3_c=a9_r=ae_u=b5?=", "=?UTF-8?b?I8KjIGPCqSBywq4gdcK1?="},
18 {"=?big5?q?=a1=5d_=a1=61_=a1=71?=", "=?UTF-8?b?77yIIO+9myDjgIg=?="},
19 // Must respect separate tokens
20 {"=?UTF-8?Q?Miros=C5=82aw?= <u@h>", "=?UTF-8?b?TWlyb3PFgmF3?= <u@h>"},
21 {"First Last <u@h> (=?iso-8859-1?q?#=a3_c=a9_r=ae_u=b5?=)",
22 "First Last <u@h> (=?UTF-8?b?I8KjIGPCqSBywq4gdcK1?=)"},
23 // Quoted display name without space before angle-addr spec, Issue #112
24 {"\"=?UTF-8?b?TWlyb3PFgmF3?=\"<u@h>", "=?UTF-8?b?Ik1pcm9zxYJhdyI=?= <u@h>"},
25 }
26
27 for _, tt := range testTable {
28 got := decodeToUTF8Base64Header(tt.in)
29 if got != tt.want {
30 t.Errorf("DecodeHeader(%q) == %q, want: %q", tt.in, got, tt.want)
31 }
32 }
33 }
34
35 func TestReadHeader(t *testing.T) {
36 // These values will surround the test table input string.
37 prefix := "From: hooman\n \n being\n"
38 suffix := "Subject: hi\n\nPart body\n"
39
40 data := make([]byte, 16*1024)
41 for i := 0; i < len(data); i++ {
42 data[i] = 'x'
43 }
44 sdata := string(data)
45 var ttable = []struct {
46 label, input, hname, want string
47 correct bool
48 extras []string
49 }{
50 {
51 label: "basic crlf",
52 input: "Foo: bar\r\n",
53 hname: "Foo",
54 want: "bar",
55 correct: true,
56 },
57 {
58 label: "basic lf",
59 input: "To: anybody\n",
60 hname: "To",
61 want: "anybody",
62 correct: true,
63 },
64 {
65 label: "hyphenated",
66 input: "Content-Language: en\r\n",
67 hname: "Content-Language",
68 want: "en",
69 correct: true,
70 },
71 {
72 label: "numeric",
73 input: "Privilege: 127\n",
74 hname: "Privilege",
75 want: "127",
76 correct: true,
77 },
78 {
79 label: "space before colon",
80 input: "SID : 0\r\n",
81 hname: "SID",
82 want: "0",
83 correct: true,
84 },
85 {
86 label: "space in name",
87 input: "Audio Mode : None\r\n",
88 hname: "Audio Mode",
89 want: "None",
90 correct: true,
91 },
92 {
93 label: "sdata",
94 input: "Cookie: " + sdata + "\r\n",
95 hname: "Cookie",
96 want: sdata,
97 correct: true,
98 },
99 {
100 label: "missing name",
101 input: ": line1=foo\r\n",
102 hname: "",
103 want: "",
104 correct: false,
105 },
106 {
107 label: "blank line in continuation",
108 input: "X-Continuation: line1=foo\r\n" +
109 " \r\n" +
110 " line2=bar\r\n",
111 hname: "X-Continuation",
112 want: "line1=foo line2=bar",
113 correct: true,
114 },
115 {
116 label: "lf-space continuation",
117 input: "Content-Type: text/plain;\n charset=us-ascii\n",
118 hname: "Content-Type",
119 want: "text/plain; charset=us-ascii",
120 correct: true,
121 },
122 {
123 label: "lf-tab continuation",
124 input: "X-Tabbed-Continuation: line1=foo;\n\tline2=bar\n",
125 hname: "X-Tabbed-Continuation",
126 want: "line1=foo; line2=bar",
127 correct: true,
128 },
129 {
130 label: "equals in name",
131 input: "name=value:text\n",
132 hname: "name=value",
133 want: "text",
134 correct: true,
135 },
136 {
137 label: "no space before continuation",
138 input: "X-Bad-Continuation: line1=foo;\nline2=bar\n",
139 hname: "X-Bad-Continuation",
140 want: "line1=foo; line2=bar",
141 correct: false,
142 },
143 {
144 label: "not really a continuation",
145 input: "X-Not-Continuation: line1=foo;\nline2: bar\n",
146 hname: "X-Not-Continuation",
147 want: "line1=foo;",
148 correct: true,
149 extras: []string{"line2"},
150 },
151 {
152 label: "continuation with header style",
153 input: "X-Continuation: line1=foo;\n not-a-header 15 X-Not-Header: bar\n",
154 hname: "X-Continuation",
155 want: "line1=foo; not-a-header 15 X-Not-Header: bar",
156 correct: true,
157 },
158 {
159 label: "multiline continuation with header style, few spaces",
160 input: "X-Continuation-DKIM-like: line1=foo;\n" +
161 " h=Subject:From:Reply-To:To:Date:Message-ID: List-ID:List-Unsubscribe:\n" +
162 " Content-Type:MIME-Version;\n",
163 hname: "X-Continuation-DKIM-like",
164 want: "line1=foo;" +
165 " h=Subject:From:Reply-To:To:Date:Message-ID: List-ID:List-Unsubscribe:" +
166 " Content-Type:MIME-Version;",
167 correct: true,
168 },
169 {
170 label: "multiline continuation, few colons",
171 input: "Authentication-Results: mx.google.com;\n" +
172 " spf=pass (google.com: sender)\n" +
173 " dkim=pass header.i=@1;\n" +
174 " dkim=pass header.i=@2\n",
175 hname: "Authentication-Results",
176 want: "mx.google.com;" +
177 " spf=pass (google.com: sender)" +
178 " dkim=pass header.i=@1;" +
179 " dkim=pass header.i=@2",
180 correct: true,
181 },
182 {
183 label: "continuation containing early name-colon",
184 input: "DKIM-Signature: a=rsa-sha256; v=1; q=dns/txt;\r\n" +
185 " s=krs; t=1603674005; h=Content-Transfer-Encoding: Mime-Version:\r\n" +
186 " Content-Type: Subject: From: To: Message-Id: Sender: Date;\r\n",
187 hname: "DKIM-Signature",
188 want: "a=rsa-sha256; v=1; q=dns/txt;" +
189 " s=krs; t=1603674005; h=Content-Transfer-Encoding: Mime-Version:" +
190 " Content-Type: Subject: From: To: Message-Id: Sender: Date;",
191 correct: true,
192 },
193 }
194
195 for _, tt := range ttable {
196 t.Run(tt.label, func(t *testing.T) {
197 if lastc := tt.input[len(tt.input)-1]; lastc != '\r' && lastc != '\n' {
198 t.Fatalf("Malformed test case, %q input does not end with a CR or LF", tt.label)
199 }
200
201 // Reader we will share with readHeader()
202 r := bufio.NewReader(strings.NewReader(prefix + tt.input + suffix))
203
204 p := &Part{}
205 header, err := readHeader(r, p)
206 if err != nil {
207 t.Fatal(err)
208 }
209
210 // Check exepcted prefix header.
211 got := header.Get("From")
212 want := "hooman being"
213 if got != want {
214 t.Errorf("Prefix (From) header mangled\ngot: %q, want: %q", got, want)
215 }
216
217 // Check exepcted suffix header.
218 got = header.Get("Subject")
219 want = "hi"
220 if got != want {
221 t.Errorf("Suffix (Subject) header mangled\ngot: %q, want: %q", got, want)
222 }
223
224 // Check exepcted header from ttable.
225 got = header.Get(tt.hname)
226 if got != tt.want {
227 t.Errorf(
228 "Stripped %q header value mismatch\ngot : %q,\nwant: %q", tt.hname, got, tt.want)
229 }
230
231 // Check error count.
232 wantErrs := 0
233 if !tt.correct {
234 wantErrs = 1
235 }
236 gotErrs := len(p.Errors)
237 if gotErrs != wantErrs {
238 t.Errorf("Got %v p.Errors, want %v", gotErrs, wantErrs)
239 }
240
241 // Check for extra headers by removing expected ones.
242 delete(header, "From")
243 delete(header, "Subject")
244 delete(header, textproto.CanonicalMIMEHeaderKey(tt.hname))
245 for _, hname := range tt.extras {
246 delete(header, textproto.CanonicalMIMEHeaderKey(hname))
247 }
248 for hname := range header {
249 t.Errorf("Found unexpected header %q after parsing", hname)
250 }
251
252 // Output input if any check failed.
253 if t.Failed() {
254 t.Errorf("input: %q", tt.input)
255 }
256
257 // readHeader should have consumed the two header lines, and the blank line, but not the
258 // body
259 want = "Part body"
260 line, isPrefix, err := r.ReadLine()
261 got = string(line)
262 if err != nil {
263 t.Fatal(err)
264 }
265 if isPrefix {
266 t.Fatal("isPrefix was true, wanted false")
267 }
268 if got != want {
269 t.Errorf("Line got: %q, want: %q", got, want)
270 }
271 })
272 }
273 }
274
275 func TestCommaDelimitedAddressLists(t *testing.T) {
276 testData := []struct {
277 have string
278 want string
279 }{
280 {
281 have: `"Joe @ Company" <[email protected]> <[email protected]>`,
282 want: `"Joe @ Company" <[email protected]>, <[email protected]>`,
283 },
284 {
285 have: `Joe Company <[email protected]> <[email protected]>`,
286 want: `Joe Company <[email protected]>, <[email protected]>`,
287 },
288 {
289 have: `Joe Company:Joey <[email protected]> John <[email protected]>;`,
290 want: `Joe Company:Joey <[email protected]>, John <[email protected]>;`,
291 },
292 {
293 have: `Joe Company:Joey <[email protected]> John <[email protected]>; Jimmy John <[email protected]>`,
294 want: `Joe Company:Joey <[email protected]>, John <[email protected]>;`,
295 },
296 {
297 have: `Joe Company <[email protected]> John Company <[email protected]>`,
298 want: `Joe Company <[email protected]>, John Company <[email protected]>`,
299 },
300 {
301 have: `Joe Company <[email protected]>,John Company <[email protected]>`,
302 want: `Joe Company <[email protected]>,John Company <[email protected]>`,
303 },
304 {
305 have: `[email protected] [email protected]`,
306 want: `[email protected], [email protected]`,
307 },
308 {
309 have: `Jimmy John <[email protected]> [email protected] [email protected]`,
310 want: `Jimmy John <[email protected]>, [email protected], [email protected]`,
311 },
312 {
313 have: `Jimmy John <[email protected]> [email protected] John Company <[email protected]>`,
314 want: `Jimmy John <[email protected]>, [email protected], John Company <[email protected]>`,
315 },
316 {
317 have: `<[email protected]> "Giant; \"Big\" Box" <[email protected]>`,
318 want: `<[email protected]>, "Giant; \"Big\" Box" <[email protected]>`,
319 },
320 {
321 have: `A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]>;`,
322 want: `A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]>;`,
323 },
324 {
325 have: `A Group:Ed Jones <[email protected]> [email protected] John <[email protected]>;`,
326 want: `A Group:Ed Jones <[email protected]>, [email protected], John <[email protected]>;`,
327 },
328 }
329 for i := range testData {
330 v := ensureCommaDelimitedAddresses(testData[i].have)
331 if testData[i].want != v {
332 t.Fatalf("Expected %s, but got %s", testData[i].want, v)
333 }
334 }
335 }