2018-08-08 04:33:40 -04:00
|
|
|
package buf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v4/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
|
2018-08-08 04:33:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriterCreation(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{}
|
|
|
|
dest, err := tcpServer.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to start tcp server: ", err)
|
|
|
|
}
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", dest.NetAddr())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to dial a TCP connection: ", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
{
|
|
|
|
writer := NewWriter(conn)
|
|
|
|
if _, ok := writer.(*BufferToBytesWriter); !ok {
|
|
|
|
t.Fatal("writer is not a BufferToBytesWriter")
|
|
|
|
}
|
|
|
|
|
|
|
|
writer2 := NewWriter(writer.(io.Writer))
|
|
|
|
if writer2 != writer {
|
|
|
|
t.Fatal("writer is not reused")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConn := tls.Client(conn, &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
})
|
|
|
|
defer tlsConn.Close()
|
|
|
|
|
|
|
|
{
|
|
|
|
writer := NewWriter(tlsConn)
|
|
|
|
if _, ok := writer.(*SequentialWriter); !ok {
|
|
|
|
t.Fatal("writer is not a SequentialWriter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|