You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/crypto/chunk_test.go

49 lines
1.1 KiB

package crypto_test
import (
"io"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/crypto"
. "v2ray.com/ext/assert"
)
func TestChunkStreamIO(t *testing.T) {
assert := With(t)
cache := buf.NewSize(8192)
writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
b.WriteBytes('a', 'b', 'c', 'd')
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
b = buf.New()
b.WriteBytes('e', 'f', 'g')
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
if cache.Len() != 13 {
t.Fatalf("Cache length is %d, want 13", cache.Len())
}
mb, err := reader.ReadMultiBuffer()
common.Must(err)
assert(mb.Len(), Equals, int32(4))
assert(mb[0].Bytes(), Equals, []byte("abcd"))
mb, err = reader.ReadMultiBuffer()
assert(err, IsNil)
assert(mb.Len(), Equals, int32(3))
assert(mb[0].Bytes(), Equals, []byte("efg"))
_, err = reader.ReadMultiBuffer()
assert(err, Equals, io.EOF)
}