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

71 lines
1.5 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"
"testing"
2017-04-17 16:35:20 -04:00
"context"
"io"
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"
2017-04-17 16:35:20 -04:00
"v2ray.com/core/transport/ray"
2017-10-26 15:44:22 -04:00
. "v2ray.com/ext/assert"
2016-05-11 13:54:20 -04:00
)
2016-12-09 07:17:34 -05:00
func TestWriter(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-05-11 13:54:20 -04:00
2016-12-09 07:17:34 -05:00
lb := New()
2017-10-24 10:15:35 -04:00
assert(lb.AppendSupplier(ReadFrom(rand.Reader)), IsNil)
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-02-15 16:51:01 -05:00
writer := NewWriter(NewBufferedWriter(writeBuffer))
2017-04-15 15:07:23 -04:00
err := writer.Write(NewMultiBufferValue(lb))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(expectedBytes, Equals, writeBuffer.Bytes())
2016-05-11 13:54:20 -04:00
}
2017-04-17 16:35:20 -04:00
func TestBytesWriterReadFrom(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-17 16:35:20 -04:00
cache := ray.NewStream(context.Background())
reader := bufio.NewReader(io.LimitReader(rand.Reader, 8192))
_, err := reader.WriteTo(ToBytesWriter(cache))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-04-17 16:35:20 -04:00
mb, err := cache.Read()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(mb.Len(), Equals, 8192)
assert(len(mb), Equals, 4)
2017-04-17 16:35:20 -04:00
}
2017-11-03 20:33:35 -04:00
func TestDiscardBytes(t *testing.T) {
assert := With(t)
b := New()
common.Must(b.Reset(ReadFrom(rand.Reader)))
nBytes, err := io.Copy(DiscardBytes, b)
assert(nBytes, Equals, int64(Size))
assert(err, IsNil)
}
func TestDiscardBytesMultiBuffer(t *testing.T) {
assert := With(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)
nBytes, err := io.Copy(DiscardBytes, ToBytesReader(r))
assert(nBytes, Equals, int64(size))
assert(err, IsNil)
}