2016-12-09 12:17:34 +00:00
|
|
|
package buf_test
|
2016-05-11 17:54:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-18 10:32:24 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2016-05-11 17:54:20 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
. "v2ray.com/core/common/buf"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/testing/assert"
|
2017-04-18 10:32:24 +00:00
|
|
|
"v2ray.com/core/transport/ray"
|
2016-05-11 17:54:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdaptiveReader(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2016-05-11 17:54:20 +00:00
|
|
|
|
|
|
|
rawContent := make([]byte, 1024*1024)
|
2016-12-02 14:06:04 +00:00
|
|
|
buffer := bytes.NewBuffer(rawContent)
|
2016-05-11 17:54:20 +00:00
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
reader := NewReader(buffer)
|
2017-04-15 19:07:23 +00:00
|
|
|
b, err := reader.Read()
|
2016-05-11 17:54:20 +00:00
|
|
|
assert.Error(err).IsNil()
|
2017-04-15 19:07:23 +00:00
|
|
|
assert.Int(b.Len()).Equals(32 * 1024)
|
2016-05-11 17:54:20 +00:00
|
|
|
}
|
2017-04-18 10:32:24 +00:00
|
|
|
|
|
|
|
func TestBytesReaderWriteTo(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
stream := ray.NewStream(context.Background())
|
|
|
|
reader := ToBytesReader(stream)
|
|
|
|
b1 := New()
|
|
|
|
b1.AppendBytes('a', 'b', 'c')
|
|
|
|
b2 := New()
|
|
|
|
b2.AppendBytes('e', 'f', 'g')
|
|
|
|
assert.Error(stream.Write(NewMultiBufferValue(b1, b2))).IsNil()
|
|
|
|
stream.Close()
|
|
|
|
|
|
|
|
stream2 := ray.NewStream(context.Background())
|
|
|
|
writer := ToBytesWriter(stream2)
|
|
|
|
|
|
|
|
nBytes, err := io.Copy(writer, reader)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int64(nBytes).Equals(6)
|
|
|
|
|
|
|
|
mb, err := stream2.Read()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(len(mb)).Equals(2)
|
|
|
|
assert.String(mb[0].String()).Equals("abc")
|
|
|
|
assert.String(mb[1].String()).Equals("efg")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesReaderMultiBuffer(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
stream := ray.NewStream(context.Background())
|
|
|
|
reader := ToBytesReader(stream)
|
|
|
|
b1 := New()
|
|
|
|
b1.AppendBytes('a', 'b', 'c')
|
|
|
|
b2 := New()
|
|
|
|
b2.AppendBytes('e', 'f', 'g')
|
|
|
|
assert.Error(stream.Write(NewMultiBufferValue(b1, b2))).IsNil()
|
|
|
|
stream.Close()
|
|
|
|
|
|
|
|
mbReader := NewReader(reader)
|
|
|
|
mb, err := mbReader.Read()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(len(mb)).Equals(2)
|
|
|
|
assert.String(mb[0].String()).Equals("abc")
|
|
|
|
assert.String(mb[1].String()).Equals("efg")
|
|
|
|
}
|