1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 09:17:32 -05:00
v2fly/common/crypto/chunk_test.go

49 lines
1.1 KiB
Go
Raw Normal View History

2017-04-23 07:30:08 -04:00
package crypto_test
import (
"io"
"testing"
2018-07-13 08:36:09 -04:00
"v2ray.com/core/common"
2017-04-23 07:30:08 -04:00
"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
2018-03-11 18:29:17 -04:00
cache := buf.NewSize(8192)
2017-04-23 07:30:08 -04:00
writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
2018-07-30 16:45:06 -04:00
b.WriteBytes('a', 'b', 'c', 'd')
2018-07-13 08:36:09 -04:00
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
2017-04-23 07:30:08 -04:00
b = buf.New()
2018-07-30 16:45:06 -04:00
b.WriteBytes('e', 'f', 'g')
2018-07-13 08:36:09 -04:00
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
2017-04-23 07:30:08 -04:00
2018-07-13 08:36:09 -04:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
2017-04-23 07:30:08 -04:00
2018-07-13 08:36:09 -04:00
if cache.Len() != 13 {
t.Fatalf("Cache length is %d, want 13", cache.Len())
}
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
mb, err := reader.ReadMultiBuffer()
2018-07-13 08:36:09 -04:00
common.Must(err)
2018-04-02 14:00:50 -04:00
assert(mb.Len(), Equals, int32(4))
2017-10-24 10:15:35 -04:00
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)
2018-04-02 14:00:50 -04:00
assert(mb.Len(), Equals, int32(3))
2017-10-24 10:15:35 -04:00
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
}