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
func TestReadPrefixedBytes(t *testing.T) {
20 21 22 23
	b := new(bytes.Buffer)

	// standard
	b.Write(append(EncUint64(3), "foo"...))
24
	data, err := ReadPrefixedBytes(b, 3)
25 26 27 28 29 30 31 32
	if err != nil {
		t.Error(err)
	} else if string(data) != "foo" {
		t.Errorf("expected foo, got %s", data)
	}

	// 0-length
	b.Write(EncUint64(0))
33
	_, err = ReadPrefixedBytes(b, 0)
34 35 36 37 38 39
	if err != nil {
		t.Error(err)
	}

	// empty
	b.Write([]byte{})
40
	_, err = ReadPrefixedBytes(b, 3)
41 42
	if err != io.EOF {
		t.Error("expected EOF, got", err)
43 44 45 46
	}

	// less than 8 bytes
	b.Write([]byte{1, 2, 3})
47
	_, err = ReadPrefixedBytes(b, 3)
48 49
	if err != io.ErrUnexpectedEOF {
		t.Error("expected unexpected EOF, got", err)
50 51 52 53
	}

	// exceed maxLen
	b.Write(EncUint64(4))
54
	_, err = ReadPrefixedBytes(b, 3)
55
	if err == nil || err.Error() != "length 4 exceeds maxLen of 3" {
56 57 58 59 60
		t.Error("expected maxLen error, got", err)
	}

	// no data after length prefix
	b.Write(EncUint64(3))
61
	_, err = ReadPrefixedBytes(b, 3)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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)
84 85
	if err != io.EOF {
		t.Error("expected EOF, got", err)
86 87 88 89 90 91
	}

	// bad object
	b.Write(EncUint64(3))
	b.WriteString("foo") // strings need an additional length prefix
	err = ReadObject(b, &obj, 3)
92 93
	if err == nil || err.Error() != "could not decode type string: "+io.ErrUnexpectedEOF.Error() {
		t.Error("expected unexpected EOF, got", err)
94 95 96
	}
}

97
func TestWritePrefixedBytes(t *testing.T) {
98 99 100
	b := new(bytes.Buffer)

	// standard
101
	err := WritePrefixedBytes(b, []byte("foo"))
102 103 104
	expected := append(EncUint64(3), "foo"...)
	if err != nil {
		t.Error(err)
105
	} else if !bytes.Equal(b.Bytes(), expected) {
106
		t.Errorf("WritePrefixedBytes wrote wrong data: expected %v, got %v", b.Bytes(), expected)
107 108 109 110
	}

	// badWriter (returns nil error, but doesn't write anything)
	bw := new(badWriter)
111
	err = WritePrefixedBytes(bw, []byte("foo"))
112 113 114 115 116 117 118 119 120 121 122 123 124
	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)
125
	} else if !bytes.Equal(b.Bytes(), expected) {
126
		t.Errorf("WritePrefixedBytes wrote wrong data: expected %v, got %v", b.Bytes(), expected)
127 128 129 130 131 132 133 134 135 136
	}

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

137
func TestReadWritePrefixedBytes(t *testing.T) {
138 139
	b := new(bytes.Buffer)

140
	// WritePrefixedBytes -> ReadPrefixedBytes
141
	data := []byte("foo")
142
	err := WritePrefixedBytes(b, data)
143 144 145
	if err != nil {
		t.Fatal(err)
	}
146
	rdata, err := ReadPrefixedBytes(b, 100)
147 148
	if err != nil {
		t.Error(err)
149
	} else if !bytes.Equal(rdata, data) {
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		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)
	}
}