github.com/xraypb/xray-core@v1.6.6/common/buf/reader_test.go (about) 1 package buf_test 2 3 import ( 4 "bytes" 5 "io" 6 "strings" 7 "testing" 8 9 "github.com/xraypb/xray-core/common" 10 . "github.com/xraypb/xray-core/common/buf" 11 "github.com/xraypb/xray-core/transport/pipe" 12 ) 13 14 func TestBytesReaderWriteTo(t *testing.T) { 15 pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024)) 16 reader := &BufferedReader{Reader: pReader} 17 b1 := New() 18 b1.WriteString("abc") 19 b2 := New() 20 b2.WriteString("efg") 21 common.Must(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2})) 22 pWriter.Close() 23 24 pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024)) 25 writer := NewBufferedWriter(pWriter2) 26 writer.SetBuffered(false) 27 28 nBytes, err := io.Copy(writer, reader) 29 common.Must(err) 30 if nBytes != 6 { 31 t.Error("copy: ", nBytes) 32 } 33 34 mb, err := pReader2.ReadMultiBuffer() 35 common.Must(err) 36 if s := mb.String(); s != "abcefg" { 37 t.Error("content: ", s) 38 } 39 } 40 41 func TestBytesReaderMultiBuffer(t *testing.T) { 42 pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024)) 43 reader := &BufferedReader{Reader: pReader} 44 b1 := New() 45 b1.WriteString("abc") 46 b2 := New() 47 b2.WriteString("efg") 48 common.Must(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2})) 49 pWriter.Close() 50 51 mbReader := NewReader(reader) 52 mb, err := mbReader.ReadMultiBuffer() 53 common.Must(err) 54 if s := mb.String(); s != "abcefg" { 55 t.Error("content: ", s) 56 } 57 } 58 59 func TestReadByte(t *testing.T) { 60 sr := strings.NewReader("abcd") 61 reader := &BufferedReader{ 62 Reader: NewReader(sr), 63 } 64 b, err := reader.ReadByte() 65 common.Must(err) 66 if b != 'a' { 67 t.Error("unexpected byte: ", b, " want a") 68 } 69 if reader.BufferedBytes() != 3 { // 3 bytes left in buffer 70 t.Error("unexpected buffered Bytes: ", reader.BufferedBytes()) 71 } 72 73 nBytes, err := reader.WriteTo(DiscardBytes) 74 common.Must(err) 75 if nBytes != 3 { 76 t.Error("unexpect bytes written: ", nBytes) 77 } 78 } 79 80 func TestReadBuffer(t *testing.T) { 81 { 82 sr := strings.NewReader("abcd") 83 buf, err := ReadBuffer(sr) 84 common.Must(err) 85 86 if s := buf.String(); s != "abcd" { 87 t.Error("unexpected str: ", s, " want abcd") 88 } 89 buf.Release() 90 } 91 } 92 93 func TestReadAtMost(t *testing.T) { 94 sr := strings.NewReader("abcd") 95 reader := &BufferedReader{ 96 Reader: NewReader(sr), 97 } 98 99 mb, err := reader.ReadAtMost(3) 100 common.Must(err) 101 if s := mb.String(); s != "abc" { 102 t.Error("unexpected read result: ", s) 103 } 104 105 nBytes, err := reader.WriteTo(DiscardBytes) 106 common.Must(err) 107 if nBytes != 1 { 108 t.Error("unexpect bytes written: ", nBytes) 109 } 110 } 111 112 func TestPacketReader_ReadMultiBuffer(t *testing.T) { 113 const alpha = "abcefg" 114 buf := bytes.NewBufferString(alpha) 115 reader := &PacketReader{buf} 116 mb, err := reader.ReadMultiBuffer() 117 common.Must(err) 118 if s := mb.String(); s != alpha { 119 t.Error("content: ", s) 120 } 121 } 122 123 func TestReaderInterface(t *testing.T) { 124 _ = (io.Reader)(new(ReadVReader)) 125 _ = (Reader)(new(ReadVReader)) 126 127 _ = (Reader)(new(BufferedReader)) 128 _ = (io.Reader)(new(BufferedReader)) 129 _ = (io.ByteReader)(new(BufferedReader)) 130 _ = (io.WriterTo)(new(BufferedReader)) 131 }