1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/common/buf/reader_test.go

132 lines
2.9 KiB
Go
Raw Normal View History

2016-12-09 07:17:34 -05:00
package buf_test
2016-05-11 13:54:20 -04:00
import (
2019-11-22 03:26:59 -05:00
"bytes"
2017-04-18 06:32:24 -04:00
"io"
2018-10-24 07:51:04 -04:00
"strings"
2016-05-11 13:54:20 -04:00
"testing"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
. "github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/transport/pipe"
2016-05-11 13:54:20 -04:00
)
2017-04-18 06:32:24 -04:00
func TestBytesReaderWriteTo(t *testing.T) {
2018-05-25 06:50:33 -04:00
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
2018-04-20 18:54:53 -04:00
reader := &BufferedReader{Reader: pReader}
2017-04-18 06:32:24 -04:00
b1 := New()
2018-11-14 16:55:20 -05:00
b1.WriteString("abc")
2017-04-18 06:32:24 -04:00
b2 := New()
2018-11-14 16:55:20 -05:00
b2.WriteString("efg")
2019-02-10 09:02:28 -05:00
common.Must(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}))
2018-04-16 18:40:27 -04:00
pWriter.Close()
2017-04-18 06:32:24 -04:00
2018-05-25 06:50:33 -04:00
pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024))
2018-04-16 18:40:27 -04:00
writer := NewBufferedWriter(pWriter2)
2017-11-09 16:33:15 -05:00
writer.SetBuffered(false)
2017-04-18 06:32:24 -04:00
nBytes, err := io.Copy(writer, reader)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-10 09:02:28 -05:00
if nBytes != 6 {
t.Error("copy: ", nBytes)
}
2017-04-18 06:32:24 -04:00
2018-04-16 18:40:27 -04:00
mb, err := pReader2.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-10 09:02:28 -05:00
if s := mb.String(); s != "abcefg" {
t.Error("content: ", s)
}
2017-04-18 06:32:24 -04:00
}
func TestBytesReaderMultiBuffer(t *testing.T) {
2018-05-25 06:50:33 -04:00
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
2018-04-20 18:54:53 -04:00
reader := &BufferedReader{Reader: pReader}
2017-04-18 06:32:24 -04:00
b1 := New()
2018-11-14 16:55:20 -05:00
b1.WriteString("abc")
2017-04-18 06:32:24 -04:00
b2 := New()
2018-11-14 16:55:20 -05:00
b2.WriteString("efg")
2019-02-10 09:02:28 -05:00
common.Must(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}))
2018-04-16 18:40:27 -04:00
pWriter.Close()
2017-04-18 06:32:24 -04:00
mbReader := NewReader(reader)
2017-11-09 16:33:15 -05:00
mb, err := mbReader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-10 09:02:28 -05:00
if s := mb.String(); s != "abcefg" {
t.Error("content: ", s)
}
2017-04-18 06:32:24 -04:00
}
2017-12-03 17:11:29 -05:00
2018-10-24 07:51:04 -04:00
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")
}
2019-11-22 03:26:59 -05:00
if reader.BufferedBytes() != 3 { // 3 bytes left in buffer
t.Error("unexpected buffered Bytes: ", reader.BufferedBytes())
}
2018-10-24 07:51:04 -04:00
2018-11-18 13:36:36 -05:00
nBytes, err := reader.WriteTo(DiscardBytes)
2018-10-24 07:51:04 -04:00
common.Must(err)
if nBytes != 3 {
t.Error("unexpect bytes written: ", nBytes)
}
}
2019-11-22 03:26:59 -05:00
func TestReadBuffer(t *testing.T) {
{
sr := strings.NewReader("abcd")
buf, err := ReadBuffer(sr)
common.Must(err)
if s := buf.String(); s != "abcd" {
t.Error("unexpected str: ", s, " want abcd")
}
buf.Release()
}
}
func TestReadAtMost(t *testing.T) {
sr := strings.NewReader("abcd")
reader := &BufferedReader{
Reader: NewReader(sr),
}
mb, err := reader.ReadAtMost(3)
common.Must(err)
if s := mb.String(); s != "abc" {
t.Error("unexpected read result: ", s)
}
nBytes, err := reader.WriteTo(DiscardBytes)
common.Must(err)
if nBytes != 1 {
t.Error("unexpect bytes written: ", nBytes)
}
}
func TestPacketReader_ReadMultiBuffer(t *testing.T) {
const alpha = "abcefg"
buf := bytes.NewBufferString(alpha)
reader := &PacketReader{buf}
mb, err := reader.ReadMultiBuffer()
common.Must(err)
if s := mb.String(); s != alpha {
t.Error("content: ", s)
}
}
2017-12-03 17:11:29 -05:00
func TestReaderInterface(t *testing.T) {
2018-10-24 07:09:07 -04:00
_ = (io.Reader)(new(ReadVReader))
_ = (Reader)(new(ReadVReader))
2017-12-03 17:11:29 -05:00
2018-10-24 07:09:07 -04:00
_ = (Reader)(new(BufferedReader))
_ = (io.Reader)(new(BufferedReader))
_ = (io.ByteReader)(new(BufferedReader))
_ = (io.WriterTo)(new(BufferedReader))
2017-12-03 17:11:29 -05:00
}