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

     1  package buf_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"strings"
     7  	"testing"
     8  
     9  	"v2ray.com/core/common"
    10  	. "v2ray.com/core/common/buf"
    11  	"v2ray.com/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  
    94  func TestReadAtMost(t *testing.T) {
    95  	sr := strings.NewReader("abcd")
    96  	reader := &BufferedReader{
    97  		Reader: NewReader(sr),
    98  	}
    99  
   100  	mb, err := reader.ReadAtMost(3)
   101  	common.Must(err)
   102  	if s := mb.String(); s != "abc" {
   103  		t.Error("unexpected read result: ", s)
   104  	}
   105  
   106  	nBytes, err := reader.WriteTo(DiscardBytes)
   107  	common.Must(err)
   108  	if nBytes != 1 {
   109  		t.Error("unexpect bytes written: ", nBytes)
   110  	}
   111  }
   112  
   113  func TestPacketReader_ReadMultiBuffer(t *testing.T) {
   114  	const alpha = "abcefg"
   115  	buf := bytes.NewBufferString(alpha)
   116  	reader := &PacketReader{buf}
   117  	mb, err := reader.ReadMultiBuffer()
   118  	common.Must(err)
   119  	if s := mb.String(); s != alpha {
   120  		t.Error("content: ", s)
   121  	}
   122  }
   123  
   124  func TestReaderInterface(t *testing.T) {
   125  	_ = (io.Reader)(new(ReadVReader))
   126  	_ = (Reader)(new(ReadVReader))
   127  
   128  	_ = (Reader)(new(BufferedReader))
   129  	_ = (io.Reader)(new(BufferedReader))
   130  	_ = (io.ByteReader)(new(BufferedReader))
   131  	_ = (io.WriterTo)(new(BufferedReader))
   132  }