1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package textproto
6
7import (
8	"bufio"
9	"bytes"
10	"io"
11	"reflect"
12	"strings"
13	"testing"
14)
15
16func reader(s string) *Reader {
17	return NewReader(bufio.NewReader(strings.NewReader(s)))
18}
19
20func TestReadLine(t *testing.T) {
21	r := reader("line1\nline2\n")
22	s, err := r.ReadLine()
23	if s != "line1" || err != nil {
24		t.Fatalf("Line 1: %s, %v", s, err)
25	}
26	s, err = r.ReadLine()
27	if s != "line2" || err != nil {
28		t.Fatalf("Line 2: %s, %v", s, err)
29	}
30	s, err = r.ReadLine()
31	if s != "" || err != io.EOF {
32		t.Fatalf("EOF: %s, %v", s, err)
33	}
34}
35
36func TestReadContinuedLine(t *testing.T) {
37	r := reader("line1\nline\n 2\nline3\n")
38	s, err := r.ReadContinuedLine()
39	if s != "line1" || err != nil {
40		t.Fatalf("Line 1: %s, %v", s, err)
41	}
42	s, err = r.ReadContinuedLine()
43	if s != "line 2" || err != nil {
44		t.Fatalf("Line 2: %s, %v", s, err)
45	}
46	s, err = r.ReadContinuedLine()
47	if s != "line3" || err != nil {
48		t.Fatalf("Line 3: %s, %v", s, err)
49	}
50	s, err = r.ReadContinuedLine()
51	if s != "" || err != io.EOF {
52		t.Fatalf("EOF: %s, %v", s, err)
53	}
54}
55
56func TestReadCodeLine(t *testing.T) {
57	r := reader("123 hi\n234 bye\n345 no way\n")
58	code, msg, err := r.ReadCodeLine(0)
59	if code != 123 || msg != "hi" || err != nil {
60		t.Fatalf("Line 1: %d, %s, %v", code, msg, err)
61	}
62	code, msg, err = r.ReadCodeLine(23)
63	if code != 234 || msg != "bye" || err != nil {
64		t.Fatalf("Line 2: %d, %s, %v", code, msg, err)
65	}
66	code, msg, err = r.ReadCodeLine(346)
67	if code != 345 || msg != "no way" || err == nil {
68		t.Fatalf("Line 3: %d, %s, %v", code, msg, err)
69	}
70	if e, ok := err.(*Error); !ok || e.Code != code || e.Msg != msg {
71		t.Fatalf("Line 3: wrong error %v\n", err)
72	}
73	code, msg, err = r.ReadCodeLine(1)
74	if code != 0 || msg != "" || err != io.EOF {
75		t.Fatalf("EOF: %d, %s, %v", code, msg, err)
76	}
77}
78
79func TestReadDotLines(t *testing.T) {
80	r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanother\n")
81	s, err := r.ReadDotLines()
82	want := []string{"dotlines", "foo", ".bar", "..baz", "quux", ""}
83	if !reflect.DeepEqual(s, want) || err != nil {
84		t.Fatalf("ReadDotLines: %v, %v", s, err)
85	}
86
87	s, err = r.ReadDotLines()
88	want = []string{"another"}
89	if !reflect.DeepEqual(s, want) || err != io.ErrUnexpectedEOF {
90		t.Fatalf("ReadDotLines2: %v, %v", s, err)
91	}
92}
93
94func TestReadDotBytes(t *testing.T) {
95	r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n")
96	b, err := r.ReadDotBytes()
97	want := []byte("dotlines\nfoo\n.bar\n..baz\nquux\n\n")
98	if !reflect.DeepEqual(b, want) || err != nil {
99		t.Fatalf("ReadDotBytes: %q, %v", b, err)
100	}
101
102	b, err = r.ReadDotBytes()
103	want = []byte("anot.her\n")
104	if !reflect.DeepEqual(b, want) || err != io.ErrUnexpectedEOF {
105		t.Fatalf("ReadDotBytes2: %q, %v", b, err)
106	}
107}
108
109func TestReadMIMEHeader(t *testing.T) {
110	r := reader("my-key: Value 1  \r\nLong-key: Even \n Longer Value\r\nmy-Key: Value 2\r\n\n")
111	m, err := r.ReadMIMEHeader()
112	want := MIMEHeader{
113		"My-Key":   {"Value 1", "Value 2"},
114		"Long-Key": {"Even Longer Value"},
115	}
116	if !reflect.DeepEqual(m, want) || err != nil {
117		t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
118	}
119}
120
121func TestReadMIMEHeaderSingle(t *testing.T) {
122	r := reader("Foo: bar\n\n")
123	m, err := r.ReadMIMEHeader()
124	want := MIMEHeader{"Foo": {"bar"}}
125	if !reflect.DeepEqual(m, want) || err != nil {
126		t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
127	}
128}
129
130func TestReadMIMEHeaderNoKey(t *testing.T) {
131	r := reader(": bar\ntest-1: 1\n\n")
132	m, err := r.ReadMIMEHeader()
133	want := MIMEHeader{"Test-1": {"1"}}
134	if !reflect.DeepEqual(m, want) || err != nil {
135		t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
136	}
137}
138
139func TestLargeReadMIMEHeader(t *testing.T) {
140	data := make([]byte, 16*1024)
141	for i := 0; i < len(data); i++ {
142		data[i] = 'x'
143	}
144	sdata := string(data)
145	r := reader("Cookie: " + sdata + "\r\n\n")
146	m, err := r.ReadMIMEHeader()
147	if err != nil {
148		t.Fatalf("ReadMIMEHeader: %v", err)
149	}
150	cookie := m.Get("Cookie")
151	if cookie != sdata {
152		t.Fatalf("ReadMIMEHeader: %v bytes, want %v bytes", len(cookie), len(sdata))
153	}
154}
155
156// TestReadMIMEHeaderNonCompliant checks that we don't normalize headers
157// with spaces before colons, and accept spaces in keys.
158func TestReadMIMEHeaderNonCompliant(t *testing.T) {
159	// These invalid headers will be rejected by net/http according to RFC 7230.
160	r := reader("Foo: bar\r\n" +
161		"Content-Language: en\r\n" +
162		"SID : 0\r\n" +
163		"Audio Mode : None\r\n" +
164		"Privilege : 127\r\n\r\n")
165	m, err := r.ReadMIMEHeader()
166	want := MIMEHeader{
167		"Foo":              {"bar"},
168		"Content-Language": {"en"},
169		"SID ":             {"0"},
170		"Audio Mode ":      {"None"},
171		"Privilege ":       {"127"},
172	}
173	if !reflect.DeepEqual(m, want) || err != nil {
174		t.Fatalf("ReadMIMEHeader =\n%v, %v; want:\n%v", m, err, want)
175	}
176}
177
178func TestReadMIMEHeaderMalformed(t *testing.T) {
179	inputs := []string{
180		"No colon first line\r\nFoo: foo\r\n\r\n",
181		" No colon first line with leading space\r\nFoo: foo\r\n\r\n",
182		"\tNo colon first line with leading tab\r\nFoo: foo\r\n\r\n",
183		" First: line with leading space\r\nFoo: foo\r\n\r\n",
184		"\tFirst: line with leading tab\r\nFoo: foo\r\n\r\n",
185		"Foo: foo\r\nNo colon second line\r\n\r\n",
186		"Foo-\n\tBar: foo\r\n\r\n",
187		"Foo-\r\n\tBar: foo\r\n\r\n",
188		"Foo\r\n\t: foo\r\n\r\n",
189		"Foo-\n\tBar",
190	}
191
192	for _, input := range inputs {
193		r := reader(input)
194		if m, err := r.ReadMIMEHeader(); err == nil {
195			t.Errorf("ReadMIMEHeader(%q) = %v, %v; want nil, err", input, m, err)
196		}
197	}
198}
199
200// Test that continued lines are properly trimmed. Issue 11204.
201func TestReadMIMEHeaderTrimContinued(t *testing.T) {
202	// In this header, \n and \r\n terminated lines are mixed on purpose.
203	// We expect each line to be trimmed (prefix and suffix) before being concatenated.
204	// Keep the spaces as they are.
205	r := reader("" + // for code formatting purpose.
206		"a:\n" +
207		" 0 \r\n" +
208		"b:1 \t\r\n" +
209		"c: 2\r\n" +
210		" 3\t\n" +
211		"  \t 4  \r\n\n")
212	m, err := r.ReadMIMEHeader()
213	if err != nil {
214		t.Fatal(err)
215	}
216	want := MIMEHeader{
217		"A": {"0"},
218		"B": {"1"},
219		"C": {"2 3 4"},
220	}
221	if !reflect.DeepEqual(m, want) {
222		t.Fatalf("ReadMIMEHeader mismatch.\n got: %q\nwant: %q", m, want)
223	}
224}
225
226type readResponseTest struct {
227	in       string
228	inCode   int
229	wantCode int
230	wantMsg  string
231}
232
233var readResponseTests = []readResponseTest{
234	{"230-Anonymous access granted, restrictions apply\n" +
235		"Read the file README.txt,\n" +
236		"230  please",
237		23,
238		230,
239		"Anonymous access granted, restrictions apply\nRead the file README.txt,\n please",
240	},
241
242	{"230 Anonymous access granted, restrictions apply\n",
243		23,
244		230,
245		"Anonymous access granted, restrictions apply",
246	},
247
248	{"400-A\n400-B\n400 C",
249		4,
250		400,
251		"A\nB\nC",
252	},
253
254	{"400-A\r\n400-B\r\n400 C\r\n",
255		4,
256		400,
257		"A\nB\nC",
258	},
259}
260
261// See https://www.ietf.org/rfc/rfc959.txt page 36.
262func TestRFC959Lines(t *testing.T) {
263	for i, tt := range readResponseTests {
264		r := reader(tt.in + "\nFOLLOWING DATA")
265		code, msg, err := r.ReadResponse(tt.inCode)
266		if err != nil {
267			t.Errorf("#%d: ReadResponse: %v", i, err)
268			continue
269		}
270		if code != tt.wantCode {
271			t.Errorf("#%d: code=%d, want %d", i, code, tt.wantCode)
272		}
273		if msg != tt.wantMsg {
274			t.Errorf("#%d: msg=%q, want %q", i, msg, tt.wantMsg)
275		}
276	}
277}
278
279// Test that multi-line errors are appropriately and fully read. Issue 10230.
280func TestReadMultiLineError(t *testing.T) {
281	r := reader("550-5.1.1 The email account that you tried to reach does not exist. Please try\n" +
282		"550-5.1.1 double-checking the recipient's email address for typos or\n" +
283		"550-5.1.1 unnecessary spaces. Learn more at\n" +
284		"Unexpected but legal text!\n" +
285		"550 5.1.1 https://support.google.com/mail/answer/6596 h20si25154304pfd.166 - gsmtp\n")
286
287	wantMsg := "5.1.1 The email account that you tried to reach does not exist. Please try\n" +
288		"5.1.1 double-checking the recipient's email address for typos or\n" +
289		"5.1.1 unnecessary spaces. Learn more at\n" +
290		"Unexpected but legal text!\n" +
291		"5.1.1 https://support.google.com/mail/answer/6596 h20si25154304pfd.166 - gsmtp"
292
293	code, msg, err := r.ReadResponse(250)
294	if err == nil {
295		t.Errorf("ReadResponse: no error, want error")
296	}
297	if code != 550 {
298		t.Errorf("ReadResponse: code=%d, want %d", code, 550)
299	}
300	if msg != wantMsg {
301		t.Errorf("ReadResponse: msg=%q, want %q", msg, wantMsg)
302	}
303	if err != nil && err.Error() != "550 "+wantMsg {
304		t.Errorf("ReadResponse: error=%q, want %q", err.Error(), "550 "+wantMsg)
305	}
306}
307
308func TestCommonHeaders(t *testing.T) {
309	commonHeaderOnce.Do(initCommonHeader)
310	for h := range commonHeader {
311		if h != CanonicalMIMEHeaderKey(h) {
312			t.Errorf("Non-canonical header %q in commonHeader", h)
313		}
314	}
315	t.Skip("gccgo escape analysis")
316	b := []byte("content-Length")
317	want := "Content-Length"
318	n := testing.AllocsPerRun(200, func() {
319		if x := canonicalMIMEHeaderKey(b); x != want {
320			t.Fatalf("canonicalMIMEHeaderKey(%q) = %q; want %q", b, x, want)
321		}
322	})
323	if n > 0 {
324		t.Errorf("canonicalMIMEHeaderKey allocs = %v; want 0", n)
325	}
326}
327
328var clientHeaders = strings.Replace(`Host: golang.org
329Connection: keep-alive
330Cache-Control: max-age=0
331Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
332User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3
333Accept-Encoding: gzip,deflate,sdch
334Accept-Language: en-US,en;q=0.8,fr-CH;q=0.6
335Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
336COOKIE: __utma=000000000.0000000000.0000000000.0000000000.0000000000.00; __utmb=000000000.0.00.0000000000; __utmc=000000000; __utmz=000000000.0000000000.00.0.utmcsr=code.google.com|utmccn=(referral)|utmcmd=referral|utmcct=/p/go/issues/detail
337Non-Interned: test
338
339`, "\n", "\r\n", -1)
340
341var serverHeaders = strings.Replace(`Content-Type: text/html; charset=utf-8
342Content-Encoding: gzip
343Date: Thu, 27 Sep 2012 09:03:33 GMT
344Server: Google Frontend
345Cache-Control: private
346Content-Length: 2298
347VIA: 1.1 proxy.example.com:80 (XXX/n.n.n-nnn)
348Connection: Close
349Non-Interned: test
350
351`, "\n", "\r\n", -1)
352
353func BenchmarkReadMIMEHeader(b *testing.B) {
354	b.ReportAllocs()
355	for _, set := range []struct {
356		name    string
357		headers string
358	}{
359		{"client_headers", clientHeaders},
360		{"server_headers", serverHeaders},
361	} {
362		b.Run(set.name, func(b *testing.B) {
363			var buf bytes.Buffer
364			br := bufio.NewReader(&buf)
365			r := NewReader(br)
366
367			for i := 0; i < b.N; i++ {
368				buf.WriteString(set.headers)
369				if _, err := r.ReadMIMEHeader(); err != nil {
370					b.Fatal(err)
371				}
372			}
373		})
374	}
375}
376
377func BenchmarkUncommon(b *testing.B) {
378	b.ReportAllocs()
379	var buf bytes.Buffer
380	br := bufio.NewReader(&buf)
381	r := NewReader(br)
382	for i := 0; i < b.N; i++ {
383		buf.WriteString("uncommon-header-for-benchmark: foo\r\n\r\n")
384		h, err := r.ReadMIMEHeader()
385		if err != nil {
386			b.Fatal(err)
387		}
388		if _, ok := h["Uncommon-Header-For-Benchmark"]; !ok {
389			b.Fatal("Missing result header.")
390		}
391	}
392}
393