1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
package quicvarint
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/require"
)
type nopReader struct{}
func (r *nopReader) Read(_ []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}
var _ io.Reader = &nopReader{}
type nopWriter struct{}
func (r *nopWriter) Write(_ []byte) (int, error) {
return 0, io.ErrShortBuffer
}
// eofReader is a reader that returns data and the io.EOF at the same time in the last Read call
type eofReader struct {
Data []byte
pos int
}
func (r *eofReader) Read(b []byte) (int, error) {
n := copy(b, r.Data[r.pos:])
r.pos += n
if r.pos >= len(r.Data) {
return n, io.EOF
}
return n, nil
}
var _ io.Writer = &nopWriter{}
func TestReaderPassesThroughUnchanged(t *testing.T) {
b := bytes.NewReader([]byte{0})
r := NewReader(b)
require.Equal(t, b, r)
}
func TestReaderWrapsIOReader(t *testing.T) {
n := &nopReader{}
r := NewReader(n)
require.NotEqual(t, n, r)
}
func TestReaderFailure(t *testing.T) {
r := NewReader(&nopReader{})
val, err := r.ReadByte()
require.Equal(t, io.ErrUnexpectedEOF, err)
require.Equal(t, byte(0), val)
}
func TestReaderHandlesEOF(t *testing.T) {
// test that the eofReader behaves as we expect
r := &eofReader{Data: []byte("foobar")}
b := make([]byte, 3)
n, err := r.Read(b)
require.Equal(t, 3, n)
require.NoError(t, err)
require.Equal(t, "foo", string(b))
n, err = r.Read(b)
require.Equal(t, 3, n)
require.Equal(t, io.EOF, err)
require.Equal(t, "bar", string(b))
n, err = r.Read(b)
require.Equal(t, io.EOF, err)
require.Zero(t, n)
// now test using it to read varints
reader := NewReader(&eofReader{Data: Append(nil, 1337)})
n2, err := Read(reader)
require.NoError(t, err)
require.EqualValues(t, 1337, n2)
}
func TestWriterPassesThroughUnchanged(t *testing.T) {
b := &bytes.Buffer{}
w := NewWriter(b)
require.Equal(t, b, w)
}
func TestWriterWrapsIOWriter(t *testing.T) {
n := &nopWriter{}
w := NewWriter(n)
require.NotEqual(t, n, w)
}
func TestWriterFailure(t *testing.T) {
w := NewWriter(&nopWriter{})
err := w.WriteByte(0)
require.Equal(t, io.ErrShortBuffer, err)
}
|