github.com/eagleql/xray-core@v1.4.4/common/buf/multi_buffer_test.go (about)

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