github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/vision/vision_test.go (about)

     1  package vision
     2  
     3  import (
     4  	"crypto/tls"
     5  	"io"
     6  	"net"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    11  )
    12  
    13  func TestVision(t *testing.T) {
    14  	s, err := net.Listen("tcp", "127.0.0.1:32123")
    15  	assert.NoError(t, err)
    16  
    17  	go func() {
    18  		for {
    19  			conn, err := s.Accept()
    20  			if err != nil {
    21  				t.Error(err)
    22  				break
    23  			}
    24  
    25  			tlsConn := tls.Server(conn, &tls.Config{})
    26  
    27  			go func() {
    28  				visionConn, err := NewVisionConn(conn, tlsConn, [16]byte{})
    29  				if err != nil {
    30  					t.Error(err)
    31  				}
    32  				defer visionConn.Close()
    33  
    34  				buf := make([]byte, 1024)
    35  
    36  				n, err := visionConn.Read(buf)
    37  				if err != nil {
    38  					t.Error(err)
    39  				}
    40  
    41  				t.Log(n, string(buf[:n]), "end")
    42  
    43  				n, err = visionConn.Write(buf[:n])
    44  				if err != nil {
    45  					t.Error(err)
    46  				}
    47  
    48  				t.Log(n)
    49  			}()
    50  		}
    51  	}()
    52  
    53  	conn, err := net.Dial("tcp", "127.0.0.1:32123")
    54  	assert.NoError(t, err)
    55  
    56  	tlsConn := tls.Client(conn, &tls.Config{})
    57  
    58  	vconn, err := NewVisionConn(conn, tlsConn, [16]byte{})
    59  	assert.NoError(t, err)
    60  
    61  	_, err = vconn.Write([]byte("Hello World!"))
    62  	assert.NoError(t, err)
    63  
    64  	n, err := io.Copy(os.Stdout, vconn)
    65  	assert.NoError(t, err)
    66  
    67  	t.Log(n)
    68  }