v2ray-core/common/crypto/chunk_test.go

50 lines
1.1 KiB
Go
Raw Normal View History

2017-04-23 11:30:08 +00:00
package crypto_test
import (
2018-08-16 10:05:33 +00:00
"bytes"
2017-04-23 11:30:08 +00:00
"io"
"testing"
2018-07-13 12:36:09 +00:00
"v2ray.com/core/common"
2017-04-23 11:30:08 +00:00
"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
2018-08-16 10:05:33 +00:00
cache := bytes.NewBuffer(make([]byte, 0, 8192))
2017-04-23 11:30:08 +00:00
writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
2018-11-14 21:55:20 +00:00
b.WriteString("abcd")
2018-11-16 10:08:12 +00:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
2017-04-23 11:30:08 +00:00
b = buf.New()
2018-11-14 21:55:20 +00:00
b.WriteString("efg")
2018-11-16 10:08:12 +00:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
2017-04-23 11:30:08 +00:00
2018-07-13 12:36:09 +00:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
2017-04-23 11:30:08 +00:00
2018-07-13 12:36:09 +00:00
if cache.Len() != 13 {
t.Fatalf("Cache length is %d, want 13", cache.Len())
}
2017-04-23 11:30:08 +00:00
2017-11-09 21:33:15 +00:00
mb, err := reader.ReadMultiBuffer()
2018-07-13 12:36:09 +00:00
common.Must(err)
2018-04-02 18:00:50 +00:00
assert(mb.Len(), Equals, int32(4))
2017-10-24 14:15:35 +00:00
assert(mb[0].Bytes(), Equals, []byte("abcd"))
2017-04-23 11:30:08 +00:00
2017-11-09 21:33:15 +00:00
mb, err = reader.ReadMultiBuffer()
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2018-04-02 18:00:50 +00:00
assert(mb.Len(), Equals, int32(3))
2017-10-24 14:15:35 +00:00
assert(mb[0].Bytes(), Equals, []byte("efg"))
2017-04-23 11:30:08 +00:00
2017-11-09 21:33:15 +00:00
_, err = reader.ReadMultiBuffer()
2017-10-24 14:15:35 +00:00
assert(err, Equals, io.EOF)
2017-04-23 11:30:08 +00:00
}