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