Home | History | Annotate | Download | only in internal
      1 // Copyright 2011 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 internal
      6 
      7 import (
      8 	"bufio"
      9 	"bytes"
     10 	"fmt"
     11 	"io"
     12 	"io/ioutil"
     13 	"strings"
     14 	"testing"
     15 )
     16 
     17 func TestChunk(t *testing.T) {
     18 	var b bytes.Buffer
     19 
     20 	w := NewChunkedWriter(&b)
     21 	const chunk1 = "hello, "
     22 	const chunk2 = "world! 0123456789abcdef"
     23 	w.Write([]byte(chunk1))
     24 	w.Write([]byte(chunk2))
     25 	w.Close()
     26 
     27 	if g, e := b.String(), "7\r\nhello, \r\n17\r\nworld! 0123456789abcdef\r\n0\r\n"; g != e {
     28 		t.Fatalf("chunk writer wrote %q; want %q", g, e)
     29 	}
     30 
     31 	r := NewChunkedReader(&b)
     32 	data, err := ioutil.ReadAll(r)
     33 	if err != nil {
     34 		t.Logf(`data: "%s"`, data)
     35 		t.Fatalf("ReadAll from reader: %v", err)
     36 	}
     37 	if g, e := string(data), chunk1+chunk2; g != e {
     38 		t.Errorf("chunk reader read %q; want %q", g, e)
     39 	}
     40 }
     41 
     42 func TestChunkReadMultiple(t *testing.T) {
     43 	// Bunch of small chunks, all read together.
     44 	{
     45 		var b bytes.Buffer
     46 		w := NewChunkedWriter(&b)
     47 		w.Write([]byte("foo"))
     48 		w.Write([]byte("bar"))
     49 		w.Close()
     50 
     51 		r := NewChunkedReader(&b)
     52 		buf := make([]byte, 10)
     53 		n, err := r.Read(buf)
     54 		if n != 6 || err != io.EOF {
     55 			t.Errorf("Read = %d, %v; want 6, EOF", n, err)
     56 		}
     57 		buf = buf[:n]
     58 		if string(buf) != "foobar" {
     59 			t.Errorf("Read = %q; want %q", buf, "foobar")
     60 		}
     61 	}
     62 
     63 	// One big chunk followed by a little chunk, but the small bufio.Reader size
     64 	// should prevent the second chunk header from being read.
     65 	{
     66 		var b bytes.Buffer
     67 		w := NewChunkedWriter(&b)
     68 		// fillBufChunk is 11 bytes + 3 bytes header + 2 bytes footer = 16 bytes,
     69 		// the same as the bufio ReaderSize below (the minimum), so even
     70 		// though we're going to try to Read with a buffer larger enough to also
     71 		// receive "foo", the second chunk header won't be read yet.
     72 		const fillBufChunk = "0123456789a"
     73 		const shortChunk = "foo"
     74 		w.Write([]byte(fillBufChunk))
     75 		w.Write([]byte(shortChunk))
     76 		w.Close()
     77 
     78 		r := NewChunkedReader(bufio.NewReaderSize(&b, 16))
     79 		buf := make([]byte, len(fillBufChunk)+len(shortChunk))
     80 		n, err := r.Read(buf)
     81 		if n != len(fillBufChunk) || err != nil {
     82 			t.Errorf("Read = %d, %v; want %d, nil", n, err, len(fillBufChunk))
     83 		}
     84 		buf = buf[:n]
     85 		if string(buf) != fillBufChunk {
     86 			t.Errorf("Read = %q; want %q", buf, fillBufChunk)
     87 		}
     88 
     89 		n, err = r.Read(buf)
     90 		if n != len(shortChunk) || err != io.EOF {
     91 			t.Errorf("Read = %d, %v; want %d, EOF", n, err, len(shortChunk))
     92 		}
     93 	}
     94 
     95 	// And test that we see an EOF chunk, even though our buffer is already full:
     96 	{
     97 		r := NewChunkedReader(bufio.NewReader(strings.NewReader("3\r\nfoo\r\n0\r\n")))
     98 		buf := make([]byte, 3)
     99 		n, err := r.Read(buf)
    100 		if n != 3 || err != io.EOF {
    101 			t.Errorf("Read = %d, %v; want 3, EOF", n, err)
    102 		}
    103 		if string(buf) != "foo" {
    104 			t.Errorf("buf = %q; want foo", buf)
    105 		}
    106 	}
    107 }
    108 
    109 func TestChunkReaderAllocs(t *testing.T) {
    110 	if testing.Short() {
    111 		t.Skip("skipping in short mode")
    112 	}
    113 	var buf bytes.Buffer
    114 	w := NewChunkedWriter(&buf)
    115 	a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
    116 	w.Write(a)
    117 	w.Write(b)
    118 	w.Write(c)
    119 	w.Close()
    120 
    121 	readBuf := make([]byte, len(a)+len(b)+len(c)+1)
    122 	byter := bytes.NewReader(buf.Bytes())
    123 	bufr := bufio.NewReader(byter)
    124 	mallocs := testing.AllocsPerRun(100, func() {
    125 		byter.Seek(0, 0)
    126 		bufr.Reset(byter)
    127 		r := NewChunkedReader(bufr)
    128 		n, err := io.ReadFull(r, readBuf)
    129 		if n != len(readBuf)-1 {
    130 			t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
    131 		}
    132 		if err != io.ErrUnexpectedEOF {
    133 			t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
    134 		}
    135 	})
    136 	if mallocs > 1.5 {
    137 		t.Errorf("mallocs = %v; want 1", mallocs)
    138 	}
    139 }
    140 
    141 func TestParseHexUint(t *testing.T) {
    142 	for i := uint64(0); i <= 1234; i++ {
    143 		line := []byte(fmt.Sprintf("%x", i))
    144 		got, err := parseHexUint(line)
    145 		if err != nil {
    146 			t.Fatalf("on %d: %v", i, err)
    147 		}
    148 		if got != i {
    149 			t.Errorf("for input %q = %d; want %d", line, got, i)
    150 		}
    151 	}
    152 	_, err := parseHexUint([]byte("bogus"))
    153 	if err == nil {
    154 		t.Error("expected error on bogus input")
    155 	}
    156 }
    157