github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/test/fallback_test.go (about) 1 package jls 2 3 import ( 4 "context" 5 "io" 6 "log" 7 "net" 8 "net/http" 9 "testing" 10 11 "github.com/JimmyHuang454/JLS-go/tls" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestFallback(t *testing.T) { 16 serverName := "www.apple.com" 17 18 // server 19 cert, err := tls.X509KeyPair(certPem, keyPem) 20 assert.Nil(t, err) 21 serverConfig := &tls.Config{ 22 Certificates: []tls.Certificate{cert}, 23 ServerName: serverName, 24 UseJLS: true, JLSPWD: []byte("abc"), JLSIV: []byte("abc")} 25 port := "2000" 26 listener, err := tls.Listen("tcp", ":"+port, serverConfig) 27 assert.Nil(t, err) 28 29 go func() { 30 for true { 31 inClient, err := listener.Accept() 32 log.Println(err) 33 assert.NotNil(t, err) 34 inClient.Close() 35 } 36 }() 37 config := &tls.Config{ 38 ServerName: serverName, 39 } 40 tcpAddress := "127.0.0.1:" + port 41 42 // client1 43 for i := 0; i < 3; i++ { 44 tcp, err := net.Dial("tcp", tcpAddress) 45 assert.Nil(t, err) 46 tlsDial := tls.Client(tcp, config) 47 err = tlsDial.Handshake() 48 assert.Nil(t, err) 49 50 client := &http.Client{ 51 Transport: &http.Transport{ 52 DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { 53 return tlsDial, nil 54 }, 55 }, 56 } 57 request, _ := http.NewRequest("GET", "https://"+serverName, nil) 58 response, err := client.Do(request) 59 assert.Nil(t, err) 60 _, err = io.Copy(io.Discard, response.Body) 61 assert.Nil(t, err) 62 response.Body.Close() 63 client.CloseIdleConnections() 64 tcp.Close() 65 } 66 }