github.com/imannamdari/v2ray-core/v5@v5.0.5/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/google/go-cmp/cmp" 12 13 "github.com/imannamdari/v2ray-core/v5/common" 14 . "github.com/imannamdari/v2ray-core/v5/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("unexpected length from ReadAllToBytes", l) 112 } 113 } 114 { 115 const dat = "data/test_MultiBufferReadAllToByte.dat" 116 f, err := os.Open(dat) 117 common.Must(err) 118 119 buf2, err := ReadAllToBytes(f) 120 common.Must(err) 121 f.Close() 122 123 cnt, err := ioutil.ReadFile(dat) 124 common.Must(err) 125 126 if d := cmp.Diff(buf2, cnt); d != "" { 127 t.Error("fail to read from file: ", d) 128 } 129 } 130 } 131 132 func TestMultiBufferCopy(t *testing.T) { 133 lb := make([]byte, 8*1024) 134 common.Must2(io.ReadFull(rand.Reader, lb)) 135 reader := bytes.NewBuffer(lb) 136 137 mb, err := ReadFrom(reader) 138 common.Must(err) 139 140 lbdst := make([]byte, 8*1024) 141 mb.Copy(lbdst) 142 143 if d := cmp.Diff(lb, lbdst); d != "" { 144 t.Error("unexpected different from MultiBufferCopy ", d) 145 } 146 } 147 148 func TestSplitFirstBytes(t *testing.T) { 149 a := New() 150 common.Must2(a.WriteString("ab")) 151 b := New() 152 common.Must2(b.WriteString("bc")) 153 154 mb := MultiBuffer{a, b} 155 156 o := make([]byte, 2) 157 _, cnt := SplitFirstBytes(mb, o) 158 if cnt != 2 { 159 t.Error("unexpected cnt from SplitFirstBytes ", cnt) 160 } 161 if d := cmp.Diff(string(o), "ab"); d != "" { 162 t.Error("unexpected splited result from SplitFirstBytes ", d) 163 } 164 } 165 166 func TestCompact(t *testing.T) { 167 a := New() 168 common.Must2(a.WriteString("ab")) 169 b := New() 170 common.Must2(b.WriteString("bc")) 171 172 mb := MultiBuffer{a, b} 173 cmb := Compact(mb) 174 175 if w := cmb.String(); w != "abbc" { 176 t.Error("unexpected Compact result ", w) 177 } 178 } 179 180 func BenchmarkSplitBytes(b *testing.B) { 181 var mb MultiBuffer 182 raw := make([]byte, Size) 183 184 b.ResetTimer() 185 for i := 0; i < b.N; i++ { 186 buffer := StackNew() 187 buffer.Extend(Size) 188 mb = append(mb, &buffer) 189 mb, _ = SplitBytes(mb, raw) 190 } 191 }