prefix_test.go 3.63 KB
Newer Older
1 2 3 4 5 6 7 8
package encoding

import (
	"bytes"
	"io"
	"testing"
)

9 10 11 12 13 14 15 16 17 18
// badReader/Writer used to test error handling

type badReader struct{}

func (br *badReader) Read([]byte) (int, error) { return 0, io.EOF }

type badWriter struct{}

func (bw *badWriter) Write([]byte) (int, error) { return 0, nil }

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
func TestReadPrefix(t *testing.T) {
	b := new(bytes.Buffer)

	// standard
	b.Write(append(EncUint64(3), "foo"...))
	data, err := ReadPrefix(b, 3)
	if err != nil {
		t.Error(err)
	} else if string(data) != "foo" {
		t.Errorf("expected foo, got %s", data)
	}

	// 0-length
	b.Write(EncUint64(0))
	_, err = ReadPrefix(b, 0)
	if err != nil {
		t.Error(err)
	}

	// empty
	b.Write([]byte{})
	_, err = ReadPrefix(b, 3)
Luke Champine's avatar
Luke Champine committed
41 42
	if err != errNoData {
		t.Error("expected errNoData, got", err)
43 44 45 46 47
	}

	// less than 8 bytes
	b.Write([]byte{1, 2, 3})
	_, err = ReadPrefix(b, 3)
Luke Champine's avatar
Luke Champine committed
48 49
	if err != errBadPrefix {
		t.Error("expected errBadPrefix, got", err)
50 51 52 53 54 55
	}

	// exceed maxLen
	b.Write(EncUint64(4))
	_, err = ReadPrefix(b, 3)
	if err == nil {
Luke Champine's avatar
Luke Champine committed
56
		t.Error("expected errBadPrefix, got nil")
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	} else if err.Error() != "length 4 exceeds maxLen of 3" {
		t.Error("expected maxLen error, got", err)
	}

	// no data after length prefix
	b.Write(EncUint64(3))
	_, err = ReadPrefix(b, 3)
	if err != io.EOF {
		t.Error("expected EOF, got", err)
	}
}

func TestReadObject(t *testing.T) {
	b := new(bytes.Buffer)
	var obj string

	// standard
	b.Write(EncUint64(11))
	b.Write(append(EncUint64(3), "foo"...))
	err := ReadObject(b, &obj, 11)
	if err != nil {
		t.Error(err)
	} else if obj != "foo" {
		t.Errorf("expected foo, got %s", obj)
	}

	// empty
	b.Write([]byte{})
	err = ReadObject(b, &obj, 0)
Luke Champine's avatar
Luke Champine committed
86 87
	if err != errNoData {
		t.Error("expected errNoData, got", err)
88 89 90 91 92 93 94 95
	}

	// bad object
	b.Write(EncUint64(3))
	b.WriteString("foo") // strings need an additional length prefix
	err = ReadObject(b, &obj, 3)
	if err == nil {
		t.Error("expected err, got nil")
Luke Champine's avatar
Luke Champine committed
96 97
	} else if err.Error() != "could not decode type string: "+errBadPrefix.Error() {
		t.Error("expected errBadPrefix, got", err)
98 99 100 101 102 103 104 105 106 107 108 109
	}
}

func TestWritePrefix(t *testing.T) {
	b := new(bytes.Buffer)

	// standard
	err := WritePrefix(b, []byte("foo"))
	expected := append(EncUint64(3), "foo"...)
	if err != nil {
		t.Error(err)
	} else if bytes.Compare(b.Bytes(), expected) != 0 {
Luke Champine's avatar
Luke Champine committed
110
		t.Errorf("WritePrefix wrote wrong data: expected %v, got %v", b.Bytes(), expected)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	}

	// badWriter (returns nil error, but doesn't write anything)
	bw := new(badWriter)
	err = WritePrefix(bw, []byte("foo"))
	if err != io.ErrShortWrite {
		t.Error("expected ErrShortWrite, got", err)
	}
}

func TestWriteObject(t *testing.T) {
	b := new(bytes.Buffer)

	// standard
	err := WriteObject(b, "foo")
	expected := append(EncUint64(11), append(EncUint64(3), "foo"...)...)
	if err != nil {
		t.Error(err)
	} else if bytes.Compare(b.Bytes(), expected) != 0 {
Luke Champine's avatar
Luke Champine committed
130
		t.Errorf("WritePrefix wrote wrong data: expected %v, got %v", b.Bytes(), expected)
131 132 133 134 135 136 137 138 139 140
	}

	// badWriter
	bw := new(badWriter)
	err = WriteObject(bw, "foo")
	if err != io.ErrShortWrite {
		t.Error("expected ErrShortWrite, got", err)
	}
}

141
func TestReadWritePrefix(t *testing.T) {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	b := new(bytes.Buffer)

	// WritePrefix -> ReadPrefix
	data := []byte("foo")
	err := WritePrefix(b, data)
	if err != nil {
		t.Fatal(err)
	}
	rdata, err := ReadPrefix(b, 100)
	if err != nil {
		t.Error(err)
	} else if bytes.Compare(rdata, data) != 0 {
		t.Errorf("read/write mismatch: wrote %s, read %s", data, rdata)
	}

	// WriteObject -> ReadObject
	obj := "bar"
	err = WriteObject(b, obj)
	if err != nil {
		t.Fatal(err)
	}
	var robj string
	err = ReadObject(b, &robj, 100)
	if err != nil {
		t.Error(err)
	} else if robj != obj {
		t.Errorf("read/write mismatch: wrote %s, read %s", obj, robj)
	}
}