1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/common/crypto/chunk_test.go

45 lines
1.0 KiB
Go
Raw Normal View History

2017-04-23 07:30:08 -04:00
package crypto_test
import (
"io"
"testing"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/crypto"
"v2ray.com/core/testing/assert"
)
func TestChunkStreamIO(t *testing.T) {
assert := assert.On(t)
cache := buf.NewLocal(8192)
writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
b.AppendBytes('a', 'b', 'c', 'd')
assert.Error(writer.Write(buf.NewMultiBufferValue(b))).IsNil()
b = buf.New()
b.AppendBytes('e', 'f', 'g')
assert.Error(writer.Write(buf.NewMultiBufferValue(b))).IsNil()
assert.Error(writer.Write(buf.NewMultiBuffer())).IsNil()
assert.Int(cache.Len()).Equals(13)
mb, err := reader.Read()
assert.Error(err).IsNil()
assert.Int(mb.Len()).Equals(4)
assert.Bytes(mb[0].Bytes()).Equals([]byte("abcd"))
mb, err = reader.Read()
assert.Error(err).IsNil()
assert.Int(mb.Len()).Equals(3)
assert.Bytes(mb[0].Bytes()).Equals([]byte("efg"))
_, err = reader.Read()
assert.Error(err).Equals(io.EOF)
}