1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/common/crypto/chunk_test.go

45 lines
1016 B
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"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2017-04-23 07:30:08 -04:00
)
func TestChunkStreamIO(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-23 07:30:08 -04:00
cache := buf.NewLocal(8192)
writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
b.AppendBytes('a', 'b', 'c', 'd')
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
2017-04-23 07:30:08 -04:00
b = buf.New()
b.AppendBytes('e', 'f', 'g')
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
2017-04-23 07:30:08 -04:00
2017-10-24 10:15:35 -04:00
assert(cache.Len(), Equals, 13)
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
mb, err := reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(mb.Len(), Equals, 4)
assert(mb[0].Bytes(), Equals, []byte("abcd"))
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
mb, err = reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(mb.Len(), Equals, 3)
assert(mb[0].Bytes(), Equals, []byte("efg"))
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
_, err = reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, Equals, io.EOF)
2017-04-23 07:30:08 -04:00
}