1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 01:17:30 -05:00
v2fly/common/buf/reader_test.go
2019-02-10 15:02:28 +01:00

85 lines
1.8 KiB
Go

package buf_test
import (
"io"
"strings"
"testing"
"v2ray.com/core/common"
. "v2ray.com/core/common/buf"
"v2ray.com/core/transport/pipe"
)
func TestBytesReaderWriteTo(t *testing.T) {
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
reader := &BufferedReader{Reader: pReader}
b1 := New()
b1.WriteString("abc")
b2 := New()
b2.WriteString("efg")
common.Must(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}))
pWriter.Close()
pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024))
writer := NewBufferedWriter(pWriter2)
writer.SetBuffered(false)
nBytes, err := io.Copy(writer, reader)
common.Must(err)
if nBytes != 6 {
t.Error("copy: ", nBytes)
}
mb, err := pReader2.ReadMultiBuffer()
common.Must(err)
if s := mb.String(); s != "abcefg" {
t.Error("content: ", s)
}
}
func TestBytesReaderMultiBuffer(t *testing.T) {
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
reader := &BufferedReader{Reader: pReader}
b1 := New()
b1.WriteString("abc")
b2 := New()
b2.WriteString("efg")
common.Must(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}))
pWriter.Close()
mbReader := NewReader(reader)
mb, err := mbReader.ReadMultiBuffer()
common.Must(err)
if s := mb.String(); s != "abcefg" {
t.Error("content: ", s)
}
}
func TestReadByte(t *testing.T) {
sr := strings.NewReader("abcd")
reader := &BufferedReader{
Reader: NewReader(sr),
}
b, err := reader.ReadByte()
common.Must(err)
if b != 'a' {
t.Error("unexpected byte: ", b, " want a")
}
nBytes, err := reader.WriteTo(DiscardBytes)
common.Must(err)
if nBytes != 3 {
t.Error("unexpect bytes written: ", nBytes)
}
}
func TestReaderInterface(t *testing.T) {
_ = (io.Reader)(new(ReadVReader))
_ = (Reader)(new(ReadVReader))
_ = (Reader)(new(BufferedReader))
_ = (io.Reader)(new(BufferedReader))
_ = (io.ByteReader)(new(BufferedReader))
_ = (io.WriterTo)(new(BufferedReader))
}