1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/buf/writer_test.go

99 lines
2.3 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 (
2017-04-17 16:35:20 -04:00
"bufio"
2016-05-11 13:54:20 -04:00
"bytes"
"crypto/rand"
2017-04-17 16:35:20 -04:00
"io"
2018-04-16 18:40:27 -04:00
"testing"
2017-04-17 16:35:20 -04:00
2019-02-09 09:46:48 -05:00
"github.com/google/go-cmp/cmp"
2017-11-03 20:33:35 -04:00
"v2ray.com/core/common"
2016-12-09 07:17:34 -05:00
. "v2ray.com/core/common/buf"
2018-04-16 18:40:27 -04:00
"v2ray.com/core/transport/pipe"
2016-05-11 13:54:20 -04:00
)
2016-12-09 07:17:34 -05:00
func TestWriter(t *testing.T) {
lb := New()
2018-11-02 10:01:33 -04:00
common.Must2(lb.ReadFrom(rand.Reader))
2016-05-11 13:54:20 -04:00
2016-12-06 05:03:42 -05:00
expectedBytes := append([]byte(nil), lb.Bytes()...)
2016-05-11 13:54:20 -04:00
2016-12-06 05:03:42 -05:00
writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024))
2017-11-09 16:33:15 -05:00
writer := NewBufferedWriter(NewWriter(writeBuffer))
writer.SetBuffered(false)
2019-02-09 09:46:48 -05:00
common.Must(writer.WriteMultiBuffer(MultiBuffer{lb}))
common.Must(writer.Flush())
if r := cmp.Diff(expectedBytes, writeBuffer.Bytes()); r != "" {
t.Error(r)
}
2016-05-11 13:54:20 -04:00
}
2017-04-17 16:35:20 -04:00
func TestBytesWriterReadFrom(t *testing.T) {
const size = 50000
2018-05-25 06:50:33 -04:00
pReader, pWriter := pipe.New(pipe.WithSizeLimit(size))
reader := bufio.NewReader(io.LimitReader(rand.Reader, size))
2018-04-16 18:40:27 -04:00
writer := NewBufferedWriter(pWriter)
2017-11-09 16:33:15 -05:00
writer.SetBuffered(false)
nBytes, err := reader.WriteTo(writer)
2019-02-09 09:46:48 -05:00
if nBytes != size {
t.Fatal("unexpected size of bytes written: ", nBytes)
}
2018-10-24 06:02:02 -04:00
if err != nil {
t.Fatal("expect success, but actually error: ", err.Error())
}
2017-04-17 16:35:20 -04:00
2018-04-16 18:40:27 -04:00
mb, err := pReader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05:00
if mb.Len() != size {
t.Fatal("unexpected size read: ", mb.Len())
}
2017-04-17 16:35:20 -04:00
}
2017-11-03 20:33:35 -04:00
func TestDiscardBytes(t *testing.T) {
b := New()
2018-11-02 10:01:33 -04:00
common.Must2(b.ReadFullFrom(rand.Reader, Size))
2017-11-03 20:33:35 -04:00
nBytes, err := io.Copy(DiscardBytes, b)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05:00
if nBytes != Size {
t.Error("copy size: ", nBytes)
}
2017-11-03 20:33:35 -04:00
}
func TestDiscardBytesMultiBuffer(t *testing.T) {
const size = 10240*1024 + 1
buffer := bytes.NewBuffer(make([]byte, 0, size))
common.Must2(buffer.ReadFrom(io.LimitReader(rand.Reader, size)))
r := NewReader(buffer)
2018-04-20 18:54:53 -04:00
nBytes, err := io.Copy(DiscardBytes, &BufferedReader{Reader: r})
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-09 09:46:48 -05:00
if nBytes != size {
t.Error("copy size: ", nBytes)
}
2017-11-03 20:33:35 -04:00
}
2017-12-03 17:11:29 -05:00
func TestWriterInterface(t *testing.T) {
2018-07-31 07:43:27 -04:00
{
var writer interface{} = (*BufferToBytesWriter)(nil)
switch writer.(type) {
case Writer, io.Writer, io.ReaderFrom:
default:
t.Error("BufferToBytesWriter is not Writer, io.Writer or io.ReaderFrom")
}
}
{
var writer interface{} = (*BufferedWriter)(nil)
switch writer.(type) {
case Writer, io.Writer, io.ReaderFrom, io.ByteWriter:
default:
t.Error("BufferedWriter is not Writer, io.Writer, io.ReaderFrom or io.ByteWriter")
}
}
2017-12-03 17:11:29 -05:00
}