github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/common/buf/io_test.go (about)

     1  package buf_test
     2  
     3  import (
     4  	"crypto/tls"
     5  	"io"
     6  	"testing"
     7  
     8  	. "github.com/xtls/xray-core/common/buf"
     9  	"github.com/xtls/xray-core/common/net"
    10  	"github.com/xtls/xray-core/testing/servers/tcp"
    11  )
    12  
    13  func TestWriterCreation(t *testing.T) {
    14  	tcpServer := tcp.Server{}
    15  	dest, err := tcpServer.Start()
    16  	if err != nil {
    17  		t.Fatal("failed to start tcp server: ", err)
    18  	}
    19  	defer tcpServer.Close()
    20  
    21  	conn, err := net.Dial("tcp", dest.NetAddr())
    22  	if err != nil {
    23  		t.Fatal("failed to dial a TCP connection: ", err)
    24  	}
    25  	defer conn.Close()
    26  
    27  	{
    28  		writer := NewWriter(conn)
    29  		if _, ok := writer.(*BufferToBytesWriter); !ok {
    30  			t.Fatal("writer is not a BufferToBytesWriter")
    31  		}
    32  
    33  		writer2 := NewWriter(writer.(io.Writer))
    34  		if writer2 != writer {
    35  			t.Fatal("writer is not reused")
    36  		}
    37  	}
    38  
    39  	tlsConn := tls.Client(conn, &tls.Config{
    40  		InsecureSkipVerify: true,
    41  	})
    42  	defer tlsConn.Close()
    43  
    44  	{
    45  		writer := NewWriter(tlsConn)
    46  		if _, ok := writer.(*SequentialWriter); !ok {
    47  			t.Fatal("writer is not a SequentialWriter")
    48  		}
    49  	}
    50  }