github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/common/buf/multi_buffer_test.go (about)

     1  package buf_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"io/ioutil"
    11  	"os"
    12  
    13  	"v2ray.com/core/common"
    14  	. "v2ray.com/core/common/buf"
    15  )
    16  
    17  func TestMultiBufferRead(t *testing.T) {
    18  	b1 := New()
    19  	common.Must2(b1.WriteString("ab"))
    20  
    21  	b2 := New()
    22  	common.Must2(b2.WriteString("cd"))
    23  	mb := MultiBuffer{b1, b2}
    24  
    25  	bs := make([]byte, 32)
    26  	_, nBytes := SplitBytes(mb, bs)
    27  	if nBytes != 4 {
    28  		t.Error("expect 4 bytes split, but got ", nBytes)
    29  	}
    30  	if r := cmp.Diff(bs[:nBytes], []byte("abcd")); r != "" {
    31  		t.Error(r)
    32  	}
    33  }
    34  
    35  func TestMultiBufferAppend(t *testing.T) {
    36  	var mb MultiBuffer
    37  	b := New()
    38  	common.Must2(b.WriteString("ab"))
    39  	mb = append(mb, b)
    40  	if mb.Len() != 2 {
    41  		t.Error("expected length 2, but got ", mb.Len())
    42  	}
    43  }
    44  
    45  func TestMultiBufferSliceBySizeLarge(t *testing.T) {
    46  	lb := make([]byte, 8*1024)
    47  	common.Must2(io.ReadFull(rand.Reader, lb))
    48  
    49  	mb := MergeBytes(nil, lb)
    50  
    51  	mb, mb2 := SplitSize(mb, 1024)
    52  	if mb2.Len() != 1024 {
    53  		t.Error("expect length 1024, but got ", mb2.Len())
    54  	}
    55  	if mb.Len() != 7*1024 {
    56  		t.Error("expect length 7*1024, but got ", mb.Len())
    57  	}
    58  
    59  	mb, mb3 := SplitSize(mb, 7*1024)
    60  	if mb3.Len() != 7*1024 {
    61  		t.Error("expect length 7*1024, but got", mb.Len())
    62  	}
    63  
    64  	if !mb.IsEmpty() {
    65  		t.Error("expect empty buffer, but got ", mb.Len())
    66  	}
    67  }
    68  
    69  func TestMultiBufferSplitFirst(t *testing.T) {
    70  	b1 := New()
    71  	b1.WriteString("b1")
    72  
    73  	b2 := New()
    74  	b2.WriteString("b2")
    75  
    76  	b3 := New()
    77  	b3.WriteString("b3")
    78  
    79  	var mb MultiBuffer
    80  	mb = append(mb, b1, b2, b3)
    81  
    82  	mb, c1 := SplitFirst(mb)
    83  	if diff := cmp.Diff(b1.String(), c1.String()); diff != "" {
    84  		t.Error(diff)
    85  	}
    86  
    87  	mb, c2 := SplitFirst(mb)
    88  	if diff := cmp.Diff(b2.String(), c2.String()); diff != "" {
    89  		t.Error(diff)
    90  	}
    91  
    92  	mb, c3 := SplitFirst(mb)
    93  	if diff := cmp.Diff(b3.String(), c3.String()); diff != "" {
    94  		t.Error(diff)
    95  	}
    96  
    97  	if !mb.IsEmpty() {
    98  		t.Error("expect empty buffer, but got ", mb.String())
    99  	}
   100  }
   101  
   102  func TestMultiBufferReadAllToByte(t *testing.T) {
   103  	{
   104  		lb := make([]byte, 8*1024)
   105  		common.Must2(io.ReadFull(rand.Reader, lb))
   106  		rd := bytes.NewBuffer(lb)
   107  		b, err := ReadAllToBytes(rd)
   108  		common.Must(err)
   109  
   110  		if l := len(b); l != 8*1024 {
   111  			t.Error("unexpceted length from ReadAllToBytes", l)
   112  		}
   113  
   114  	}
   115  	{
   116  		const dat = "data/test_MultiBufferReadAllToByte.dat"
   117  		f, err := os.Open(dat)
   118  		common.Must(err)
   119  
   120  		buf2, err := ReadAllToBytes(f)
   121  		common.Must(err)
   122  		f.Close()
   123  
   124  		cnt, err := ioutil.ReadFile(dat)
   125  		common.Must(err)
   126  
   127  		if d := cmp.Diff(buf2, cnt); d != "" {
   128  			t.Error("fail to read from file: ", d)
   129  		}
   130  	}
   131  }
   132  
   133  func TestMultiBufferCopy(t *testing.T) {
   134  	lb := make([]byte, 8*1024)
   135  	common.Must2(io.ReadFull(rand.Reader, lb))
   136  	reader := bytes.NewBuffer(lb)
   137  
   138  	mb, err := ReadFrom(reader)
   139  	common.Must(err)
   140  
   141  	lbdst := make([]byte, 8*1024)
   142  	mb.Copy(lbdst)
   143  
   144  	if d := cmp.Diff(lb, lbdst); d != "" {
   145  		t.Error("unexpceted different from MultiBufferCopy ", d)
   146  	}
   147  }
   148  
   149  func TestSplitFirstBytes(t *testing.T) {
   150  	a := New()
   151  	common.Must2(a.WriteString("ab"))
   152  	b := New()
   153  	common.Must2(b.WriteString("bc"))
   154  
   155  	mb := MultiBuffer{a, b}
   156  
   157  	o := make([]byte, 2)
   158  	_, cnt := SplitFirstBytes(mb, o)
   159  	if cnt != 2 {
   160  		t.Error("unexpected cnt from SplitFirstBytes ", cnt)
   161  	}
   162  	if d := cmp.Diff(string(o), "ab"); d != "" {
   163  		t.Error("unexpected splited result from SplitFirstBytes ", d)
   164  	}
   165  }
   166  
   167  func TestCompact(t *testing.T) {
   168  	a := New()
   169  	common.Must2(a.WriteString("ab"))
   170  	b := New()
   171  	common.Must2(b.WriteString("bc"))
   172  
   173  	mb := MultiBuffer{a, b}
   174  	cmb := Compact(mb)
   175  
   176  	if w := cmb.String(); w != "abbc" {
   177  		t.Error("unexpected Compact result ", w)
   178  	}
   179  }
   180  
   181  func BenchmarkSplitBytes(b *testing.B) {
   182  	var mb MultiBuffer
   183  	raw := make([]byte, Size)
   184  
   185  	b.ResetTimer()
   186  	for i := 0; i < b.N; i++ {
   187  		buffer := StackNew()
   188  		buffer.Extend(Size)
   189  		mb = append(mb, &buffer)
   190  		mb, _ = SplitBytes(mb, raw)
   191  	}
   192  }