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
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
2018-04-02 14:00:50 -04:00
|
|
|
assert(cache.Len(), Equals, int32(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)
|
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
|
|
|
}
|