2017-04-15 15:07:23 -04:00
|
|
|
package buf_test
|
|
|
|
|
|
|
|
import (
|
2019-11-22 03:26:59 -05:00
|
|
|
"bytes"
|
2018-03-28 08:33:34 -04:00
|
|
|
"crypto/rand"
|
2018-04-13 07:54:36 -04:00
|
|
|
"io"
|
2019-11-26 20:12:02 -05:00
|
|
|
"os"
|
2020-10-11 07:22:46 -04:00
|
|
|
"testing"
|
2018-11-18 16:12:31 -05:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
. "github.com/v2fly/v2ray-core/v5/common/buf"
|
2017-04-15 15:07:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMultiBufferRead(t *testing.T) {
|
|
|
|
b1 := New()
|
2019-01-18 09:59:39 -05:00
|
|
|
common.Must2(b1.WriteString("ab"))
|
2017-04-15 15:07:23 -04:00
|
|
|
|
|
|
|
b2 := New()
|
2019-01-18 09:59:39 -05:00
|
|
|
common.Must2(b2.WriteString("cd"))
|
2018-11-16 05:08:12 -05:00
|
|
|
mb := MultiBuffer{b1, b2}
|
2017-04-15 15:07:23 -04:00
|
|
|
|
|
|
|
bs := make([]byte, 32)
|
2018-11-18 13:44:32 -05:00
|
|
|
_, nBytes := SplitBytes(mb, bs)
|
2019-01-18 09:59:39 -05:00
|
|
|
if nBytes != 4 {
|
|
|
|
t.Error("expect 4 bytes split, but got ", nBytes)
|
|
|
|
}
|
|
|
|
if r := cmp.Diff(bs[:nBytes], []byte("abcd")); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
2017-04-15 15:07:23 -04:00
|
|
|
}
|
2017-04-20 09:02:35 -04:00
|
|
|
|
|
|
|
func TestMultiBufferAppend(t *testing.T) {
|
|
|
|
var mb MultiBuffer
|
|
|
|
b := New()
|
2019-01-18 09:59:39 -05:00
|
|
|
common.Must2(b.WriteString("ab"))
|
2018-11-17 03:12:20 -05:00
|
|
|
mb = append(mb, b)
|
2019-01-18 09:59:39 -05:00
|
|
|
if mb.Len() != 2 {
|
|
|
|
t.Error("expected length 2, but got ", mb.Len())
|
|
|
|
}
|
2017-04-20 09:02:35 -04:00
|
|
|
}
|
2018-03-28 08:33:34 -04:00
|
|
|
|
|
|
|
func TestMultiBufferSliceBySizeLarge(t *testing.T) {
|
2018-08-16 06:05:33 -04:00
|
|
|
lb := make([]byte, 8*1024)
|
|
|
|
common.Must2(io.ReadFull(rand.Reader, lb))
|
2018-03-28 08:33:34 -04:00
|
|
|
|
2018-11-18 13:36:36 -05:00
|
|
|
mb := MergeBytes(nil, lb)
|
2018-03-28 08:33:34 -04:00
|
|
|
|
2018-11-18 14:16:14 -05:00
|
|
|
mb, mb2 := SplitSize(mb, 1024)
|
2018-11-18 16:12:31 -05:00
|
|
|
if mb2.Len() != 1024 {
|
|
|
|
t.Error("expect length 1024, but got ", mb2.Len())
|
|
|
|
}
|
|
|
|
if mb.Len() != 7*1024 {
|
|
|
|
t.Error("expect length 7*1024, but got ", mb.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
mb, mb3 := SplitSize(mb, 7*1024)
|
|
|
|
if mb3.Len() != 7*1024 {
|
|
|
|
t.Error("expect length 7*1024, but got", mb.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mb.IsEmpty() {
|
|
|
|
t.Error("expect empty buffer, but got ", mb.Len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiBufferSplitFirst(t *testing.T) {
|
|
|
|
b1 := New()
|
|
|
|
b1.WriteString("b1")
|
|
|
|
|
|
|
|
b2 := New()
|
|
|
|
b2.WriteString("b2")
|
|
|
|
|
|
|
|
b3 := New()
|
|
|
|
b3.WriteString("b3")
|
|
|
|
|
|
|
|
var mb MultiBuffer
|
|
|
|
mb = append(mb, b1, b2, b3)
|
|
|
|
|
|
|
|
mb, c1 := SplitFirst(mb)
|
|
|
|
if diff := cmp.Diff(b1.String(), c1.String()); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
mb, c2 := SplitFirst(mb)
|
|
|
|
if diff := cmp.Diff(b2.String(), c2.String()); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
mb, c3 := SplitFirst(mb)
|
|
|
|
if diff := cmp.Diff(b3.String(), c3.String()); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mb.IsEmpty() {
|
|
|
|
t.Error("expect empty buffer, but got ", mb.String())
|
|
|
|
}
|
2018-03-28 08:33:34 -04:00
|
|
|
}
|
2018-11-18 18:33:00 -05:00
|
|
|
|
2019-11-22 03:26:59 -05:00
|
|
|
func TestMultiBufferReadAllToByte(t *testing.T) {
|
2019-11-26 20:12:02 -05:00
|
|
|
{
|
|
|
|
lb := make([]byte, 8*1024)
|
|
|
|
common.Must2(io.ReadFull(rand.Reader, lb))
|
|
|
|
rd := bytes.NewBuffer(lb)
|
|
|
|
b, err := ReadAllToBytes(rd)
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if l := len(b); l != 8*1024 {
|
2022-12-10 04:28:10 -05:00
|
|
|
t.Error("unexpected length from ReadAllToBytes", l)
|
2019-11-26 20:12:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const dat = "data/test_MultiBufferReadAllToByte.dat"
|
|
|
|
f, err := os.Open(dat)
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
buf2, err := ReadAllToBytes(f)
|
|
|
|
common.Must(err)
|
|
|
|
f.Close()
|
|
|
|
|
2023-10-15 22:23:50 -04:00
|
|
|
cnt, err := os.ReadFile(dat)
|
2019-11-26 20:12:02 -05:00
|
|
|
common.Must(err)
|
2019-11-22 03:26:59 -05:00
|
|
|
|
2019-11-26 20:12:02 -05:00
|
|
|
if d := cmp.Diff(buf2, cnt); d != "" {
|
|
|
|
t.Error("fail to read from file: ", d)
|
|
|
|
}
|
2019-11-22 03:26:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiBufferCopy(t *testing.T) {
|
|
|
|
lb := make([]byte, 8*1024)
|
|
|
|
common.Must2(io.ReadFull(rand.Reader, lb))
|
|
|
|
reader := bytes.NewBuffer(lb)
|
|
|
|
|
|
|
|
mb, err := ReadFrom(reader)
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
lbdst := make([]byte, 8*1024)
|
|
|
|
mb.Copy(lbdst)
|
|
|
|
|
|
|
|
if d := cmp.Diff(lb, lbdst); d != "" {
|
2022-12-10 04:28:10 -05:00
|
|
|
t.Error("unexpected different from MultiBufferCopy ", d)
|
2019-11-22 03:26:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitFirstBytes(t *testing.T) {
|
|
|
|
a := New()
|
|
|
|
common.Must2(a.WriteString("ab"))
|
|
|
|
b := New()
|
|
|
|
common.Must2(b.WriteString("bc"))
|
|
|
|
|
|
|
|
mb := MultiBuffer{a, b}
|
|
|
|
|
|
|
|
o := make([]byte, 2)
|
|
|
|
_, cnt := SplitFirstBytes(mb, o)
|
|
|
|
if cnt != 2 {
|
|
|
|
t.Error("unexpected cnt from SplitFirstBytes ", cnt)
|
|
|
|
}
|
|
|
|
if d := cmp.Diff(string(o), "ab"); d != "" {
|
|
|
|
t.Error("unexpected splited result from SplitFirstBytes ", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompact(t *testing.T) {
|
|
|
|
a := New()
|
|
|
|
common.Must2(a.WriteString("ab"))
|
|
|
|
b := New()
|
|
|
|
common.Must2(b.WriteString("bc"))
|
|
|
|
|
|
|
|
mb := MultiBuffer{a, b}
|
|
|
|
cmb := Compact(mb)
|
|
|
|
|
|
|
|
if w := cmb.String(); w != "abbc" {
|
|
|
|
t.Error("unexpected Compact result ", w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:33:00 -05:00
|
|
|
func BenchmarkSplitBytes(b *testing.B) {
|
|
|
|
var mb MultiBuffer
|
|
|
|
raw := make([]byte, Size)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buffer := StackNew()
|
|
|
|
buffer.Extend(Size)
|
|
|
|
mb = append(mb, &buffer)
|
|
|
|
mb, _ = SplitBytes(mb, raw)
|
|
|
|
}
|
|
|
|
}
|