1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-03 16:57:38 -05:00
v2fly/common/crypto/chunk_test.go

52 lines
1.0 KiB
Go
Raw Normal View History

2017-04-23 07:30:08 -04:00
package crypto_test
import (
2018-08-16 06:05:33 -04:00
"bytes"
2017-04-23 07:30:08 -04:00
"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"
)
func TestChunkStreamIO(t *testing.T) {
2018-08-16 06:05:33 -04:00
cache := bytes.NewBuffer(make([]byte, 0, 8192))
2017-04-23 07:30:08 -04:00
writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
2018-11-14 16:55:20 -05:00
b.WriteString("abcd")
2018-11-16 05:08:12 -05:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
2017-04-23 07:30:08 -04:00
b = buf.New()
2018-11-14 16:55:20 -05:00
b.WriteString("efg")
2018-11-16 05:08:12 -05:00
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{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)
2019-02-10 09:02:28 -05:00
if s := mb.String(); s != "abcd" {
t.Error("content: ", s)
}
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
mb, err = reader.ReadMultiBuffer()
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-10 09:02:28 -05:00
if s := mb.String(); s != "efg" {
t.Error("content: ", s)
}
2017-04-23 07:30:08 -04:00
2017-11-09 16:33:15 -05:00
_, err = reader.ReadMultiBuffer()
2019-02-10 09:02:28 -05:00
if err != io.EOF {
t.Error("error: ", err)
}
2017-04-23 07:30:08 -04:00
}