github.com/xraypb/xray-core@v1.6.6/common/crypto/chunk_test.go (about)

     1  package crypto_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/xraypb/xray-core/common"
     9  	"github.com/xraypb/xray-core/common/buf"
    10  	. "github.com/xraypb/xray-core/common/crypto"
    11  )
    12  
    13  func TestChunkStreamIO(t *testing.T) {
    14  	cache := bytes.NewBuffer(make([]byte, 0, 8192))
    15  
    16  	writer := NewChunkStreamWriter(PlainChunkSizeParser{}, cache)
    17  	reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
    18  
    19  	b := buf.New()
    20  	b.WriteString("abcd")
    21  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
    22  
    23  	b = buf.New()
    24  	b.WriteString("efg")
    25  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
    26  
    27  	common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
    28  
    29  	if cache.Len() != 13 {
    30  		t.Fatalf("Cache length is %d, want 13", cache.Len())
    31  	}
    32  
    33  	mb, err := reader.ReadMultiBuffer()
    34  	common.Must(err)
    35  
    36  	if s := mb.String(); s != "abcd" {
    37  		t.Error("content: ", s)
    38  	}
    39  
    40  	mb, err = reader.ReadMultiBuffer()
    41  	common.Must(err)
    42  
    43  	if s := mb.String(); s != "efg" {
    44  		t.Error("content: ", s)
    45  	}
    46  
    47  	_, err = reader.ReadMultiBuffer()
    48  	if err != io.EOF {
    49  		t.Error("error: ", err)
    50  	}
    51  }