Home | History | Annotate | Download | only in textproto
      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 
      5 package textproto
      6 
      7 import (
      8 	"bufio"
      9 	"bytes"
     10 	"io"
     11 	"reflect"
     12 	"strings"
     13 	"testing"
     14 )
     15 
     16 type canonicalHeaderKeyTest struct {
     17 	in, out string
     18 }
     19 
     20 var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{
     21 	{"a-b-c", "A-B-C"},
     22 	{"a-1-c", "A-1-C"},
     23 	{"User-Agent", "User-Agent"},
     24 	{"uSER-aGENT", "User-Agent"},
     25 	{"user-agent", "User-Agent"},
     26 	{"USER-AGENT", "User-Agent"},
     27 
     28 	// Non-ASCII or anything with spaces or non-token chars is unchanged:
     29 	{"ser-agenT", "ser-agenT"},
     30 	{"a B", "a B"},
     31 
     32 	// This caused a panic due to mishandling of a space:
     33 	{"C Ontent-Transfer-Encoding", "C Ontent-Transfer-Encoding"},
     34 	{"foo bar", "foo bar"},
     35 }
     36 
     37 func TestCanonicalMIMEHeaderKey(t *testing.T) {
     38 	for _, tt := range canonicalHeaderKeyTests {
     39 		if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out {
     40 			t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out)
     41 		}
     42 	}
     43 }
     44 
     45 func reader(s string) *Reader {
     46 	return NewReader(bufio.NewReader(strings.NewReader(s)))
     47 }
     48 
     49 func TestReadLine(t *testing.T) {
     50 	r := reader("line1\nline2\n")
     51 	s, err := r.ReadLine()
     52 	if s != "line1" || err != nil {
     53 		t.Fatalf("Line 1: %s, %v", s, err)
     54 	}
     55 	s, err = r.ReadLine()
     56 	if s != "line2" || err != nil {
     57 		t.Fatalf("Line 2: %s, %v", s, err)
     58 	}
     59 	s, err = r.ReadLine()
     60 	if s != "" || err != io.EOF {
     61 		t.Fatalf("EOF: %s, %v", s, err)
     62 	}
     63 }
     64 
     65 func TestReadContinuedLine(t *testing.T) {
     66 	r := reader("line1\nline\n 2\nline3\n")
     67 	s, err := r.ReadContinuedLine()
     68 	if s != "line1" || err != nil {
     69 		t.Fatalf("Line 1: %s, %v", s, err)
     70 	}
     71 	s, err = r.ReadContinuedLine()
     72 	if s != "line 2" || err != nil {
     73 		t.Fatalf("Line 2: %s, %v", s, err)
     74 	}
     75 	s, err = r.ReadContinuedLine()
     76 	if s != "line3" || err != nil {
     77 		t.Fatalf("Line 3: %s, %v", s, err)
     78 	}
     79 	s, err = r.ReadContinuedLine()
     80 	if s != "" || err != io.EOF {
     81 		t.Fatalf("EOF: %s, %v", s, err)
     82 	}
     83 }
     84 
     85 func TestReadCodeLine(t *testing.T) {
     86 	r := reader("123 hi\n234 bye\n345 no way\n")
     87 	code, msg, err := r.ReadCodeLine(0)
     88 	if code != 123 || msg != "hi" || err != nil {
     89 		t.Fatalf("Line 1: %d, %s, %v", code, msg, err)
     90 	}
     91 	code, msg, err = r.ReadCodeLine(23)
     92 	if code != 234 || msg != "bye" || err != nil {
     93 		t.Fatalf("Line 2: %d, %s, %v", code, msg, err)
     94 	}
     95 	code, msg, err = r.ReadCodeLine(346)
     96 	if code != 345 || msg != "no way" || err == nil {
     97 		t.Fatalf("Line 3: %d, %s, %v", code, msg, err)
     98 	}
     99 	if e, ok := err.(*Error); !ok || e.Code != code || e.Msg != msg {
    100 		t.Fatalf("Line 3: wrong error %v\n", err)
    101 	}
    102 	code, msg, err = r.ReadCodeLine(1)
    103 	if code != 0 || msg != "" || err != io.EOF {
    104 		t.Fatalf("EOF: %d, %s, %v", code, msg, err)
    105 	}
    106 }
    107 
    108 func TestReadDotLines(t *testing.T) {
    109 	r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanother\n")
    110 	s, err := r.ReadDotLines()
    111 	want := []string{"dotlines", "foo", ".bar", "..baz", "quux", ""}
    112 	if !reflect.DeepEqual(s, want) || err != nil {
    113 		t.Fatalf("ReadDotLines: %v, %v", s, err)
    114 	}
    115 
    116 	s, err = r.ReadDotLines()
    117 	want = []string{"another"}
    118 	if !reflect.DeepEqual(s, want) || err != io.ErrUnexpectedEOF {
    119 		t.Fatalf("ReadDotLines2: %v, %v", s, err)
    120 	}
    121 }
    122 
    123 func TestReadDotBytes(t *testing.T) {
    124 	r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n")
    125 	b, err := r.ReadDotBytes()
    126 	want := []byte("dotlines\nfoo\n.bar\n..baz\nquux\n\n")
    127 	if !reflect.DeepEqual(b, want) || err != nil {
    128 		t.Fatalf("ReadDotBytes: %q, %v", b, err)
    129 	}
    130 
    131 	b, err = r.ReadDotBytes()
    132 	want = []byte("anot.her\n")
    133 	if !reflect.DeepEqual(b, want) || err != io.ErrUnexpectedEOF {
    134 		t.Fatalf("ReadDotBytes2: %q, %v", b, err)
    135 	}
    136 }
    137 
    138 func TestReadMIMEHeader(t *testing.T) {
    139 	r := reader("my-key: Value 1  \r\nLong-key: Even \n Longer Value\r\nmy-Key: Value 2\r\n\n")
    140 	m, err := r.ReadMIMEHeader()
    141 	want := MIMEHeader{
    142 		"My-Key":   {"Value 1", "Value 2"},
    143 		"Long-Key": {"Even Longer Value"},
    144 	}
    145 	if !reflect.DeepEqual(m, want) || err != nil {
    146 		t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
    147 	}
    148 }
    149 
    150 func TestReadMIMEHeaderSingle(t *testing.T) {
    151 	r := reader("Foo: bar\n\n")
    152 	m, err := r.ReadMIMEHeader()
    153 	want := MIMEHeader{"Foo": {"bar"}}
    154 	if !reflect.DeepEqual(m, want) || err != nil {
    155 		t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
    156 	}
    157 }
    158 
    159 func TestReadMIMEHeaderNoKey(t *testing.T) {
    160 	r := reader(": bar\ntest-1: 1\n\n")
    161 	m, err := r.ReadMIMEHeader()
    162 	want := MIMEHeader{"Test-1": {"1"}}
    163 	if !reflect.DeepEqual(m, want) || err != nil {
    164 		t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
    165 	}
    166 }
    167 
    168 func TestLargeReadMIMEHeader(t *testing.T) {
    169 	data := make([]byte, 16*1024)
    170 	for i := 0; i < len(data); i++ {
    171 		data[i] = 'x'
    172 	}
    173 	sdata := string(data)
    174 	r := reader("Cookie: " + sdata + "\r\n\n")
    175 	m, err := r.ReadMIMEHeader()
    176 	if err != nil {
    177 		t.Fatalf("ReadMIMEHeader: %v", err)
    178 	}
    179 	cookie := m.Get("Cookie")
    180 	if cookie != sdata {
    181 		t.Fatalf("ReadMIMEHeader: %v bytes, want %v bytes", len(cookie), len(sdata))
    182 	}
    183 }
    184 
    185 // Test that we read slightly-bogus MIME headers seen in the wild,
    186 // with spaces before colons, and spaces in keys.
    187 func TestReadMIMEHeaderNonCompliant(t *testing.T) {
    188 	// Invalid HTTP response header as sent by an Axis security
    189 	// camera: (this is handled by IE, Firefox, Chrome, curl, etc.)
    190 	r := reader("Foo: bar\r\n" +
    191 		"Content-Language: en\r\n" +
    192 		"SID : 0\r\n" +
    193 		"Audio Mode : None\r\n" +
    194 		"Privilege : 127\r\n\r\n")
    195 	m, err := r.ReadMIMEHeader()
    196 	want := MIMEHeader{
    197 		"Foo":              {"bar"},
    198 		"Content-Language": {"en"},
    199 		"Sid":              {"0"},
    200 		"Audio Mode":       {"None"},
    201 		"Privilege":        {"127"},
    202 	}
    203 	if !reflect.DeepEqual(m, want) || err != nil {
    204 		t.Fatalf("ReadMIMEHeader =\n%v, %v; want:\n%v", m, err, want)
    205 	}
    206 }
    207 
    208 type readResponseTest struct {
    209 	in       string
    210 	inCode   int
    211 	wantCode int
    212 	wantMsg  string
    213 }
    214 
    215 var readResponseTests = []readResponseTest{
    216 	{"230-Anonymous access granted, restrictions apply\n" +
    217 		"Read the file README.txt,\n" +
    218 		"230  please",
    219 		23,
    220 		230,
    221 		"Anonymous access granted, restrictions apply\nRead the file README.txt,\n please",
    222 	},
    223 
    224 	{"230 Anonymous access granted, restrictions apply\n",
    225 		23,
    226 		230,
    227 		"Anonymous access granted, restrictions apply",
    228 	},
    229 
    230 	{"400-A\n400-B\n400 C",
    231 		4,
    232 		400,
    233 		"A\nB\nC",
    234 	},
    235 
    236 	{"400-A\r\n400-B\r\n400 C\r\n",
    237 		4,
    238 		400,
    239 		"A\nB\nC",
    240 	},
    241 }
    242 
    243 // See http://www.ietf.org/rfc/rfc959.txt page 36.
    244 func TestRFC959Lines(t *testing.T) {
    245 	for i, tt := range readResponseTests {
    246 		r := reader(tt.in + "\nFOLLOWING DATA")
    247 		code, msg, err := r.ReadResponse(tt.inCode)
    248 		if err != nil {
    249 			t.Errorf("#%d: ReadResponse: %v", i, err)
    250 			continue
    251 		}
    252 		if code != tt.wantCode {
    253 			t.Errorf("#%d: code=%d, want %d", i, code, tt.wantCode)
    254 		}
    255 		if msg != tt.wantMsg {
    256 			t.Errorf("#%d: msg=%q, want %q", i, msg, tt.wantMsg)
    257 		}
    258 	}
    259 }
    260 
    261 func TestCommonHeaders(t *testing.T) {
    262 	for h := range commonHeader {
    263 		if h != CanonicalMIMEHeaderKey(h) {
    264 			t.Errorf("Non-canonical header %q in commonHeader", h)
    265 		}
    266 	}
    267 	b := []byte("content-Length")
    268 	want := "Content-Length"
    269 	n := testing.AllocsPerRun(200, func() {
    270 		if x := canonicalMIMEHeaderKey(b); x != want {
    271 			t.Fatalf("canonicalMIMEHeaderKey(%q) = %q; want %q", b, x, want)
    272 		}
    273 	})
    274 	if n > 0 {
    275 		t.Errorf("canonicalMIMEHeaderKey allocs = %v; want 0", n)
    276 	}
    277 }
    278 
    279 var clientHeaders = strings.Replace(`Host: golang.org
    280 Connection: keep-alive
    281 Cache-Control: max-age=0
    282 Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    283 User-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
    284 Accept-Encoding: gzip,deflate,sdch
    285 Accept-Language: en-US,en;q=0.8,fr-CH;q=0.6
    286 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
    287 COOKIE: __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
    288 Non-Interned: test
    289 
    290 `, "\n", "\r\n", -1)
    291 
    292 var serverHeaders = strings.Replace(`Content-Type: text/html; charset=utf-8
    293 Content-Encoding: gzip
    294 Date: Thu, 27 Sep 2012 09:03:33 GMT
    295 Server: Google Frontend
    296 Cache-Control: private
    297 Content-Length: 2298
    298 VIA: 1.1 proxy.example.com:80 (XXX/n.n.n-nnn)
    299 Connection: Close
    300 Non-Interned: test
    301 
    302 `, "\n", "\r\n", -1)
    303 
    304 func BenchmarkReadMIMEHeader(b *testing.B) {
    305 	b.ReportAllocs()
    306 	var buf bytes.Buffer
    307 	br := bufio.NewReader(&buf)
    308 	r := NewReader(br)
    309 	for i := 0; i < b.N; i++ {
    310 		var want int
    311 		var find string
    312 		if (i & 1) == 1 {
    313 			buf.WriteString(clientHeaders)
    314 			want = 10
    315 			find = "Cookie"
    316 		} else {
    317 			buf.WriteString(serverHeaders)
    318 			want = 9
    319 			find = "Via"
    320 		}
    321 		h, err := r.ReadMIMEHeader()
    322 		if err != nil {
    323 			b.Fatal(err)
    324 		}
    325 		if len(h) != want {
    326 			b.Fatalf("wrong number of headers: got %d, want %d", len(h), want)
    327 		}
    328 		if _, ok := h[find]; !ok {
    329 			b.Fatalf("did not find key %s", find)
    330 		}
    331 	}
    332 }
    333 
    334 func BenchmarkUncommon(b *testing.B) {
    335 	b.ReportAllocs()
    336 	var buf bytes.Buffer
    337 	br := bufio.NewReader(&buf)
    338 	r := NewReader(br)
    339 	for i := 0; i < b.N; i++ {
    340 		buf.WriteString("uncommon-header-for-benchmark: foo\r\n\r\n")
    341 		h, err := r.ReadMIMEHeader()
    342 		if err != nil {
    343 			b.Fatal(err)
    344 		}
    345 		if _, ok := h["Uncommon-Header-For-Benchmark"]; !ok {
    346 			b.Fatal("Missing result header.")
    347 		}
    348 	}
    349 }
    350