github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/test/psk_test.go (about)

     1  package jls
     2  
     3  import (
     4  	"log"
     5  	"testing"
     6  
     7  	"github.com/JimmyHuang454/JLS-go/tls"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPSK(t *testing.T) {
    12  	cert, err := tls.X509KeyPair(certPem, keyPem)
    13  	assert.Nil(t, err)
    14  
    15  	serverConfig := &tls.Config{Certificates: []tls.Certificate{cert},
    16  		UseJLS: true, JLSPWD: []byte("abc"), JLSIV: []byte("abc"), SessionTicketsDisabled: false}
    17  	port := "2021"
    18  	listener, err := tls.Listen("tcp", ":"+port, serverConfig)
    19  	assert.Nil(t, err)
    20  
    21  	go func() {
    22  		for {
    23  			inClient, err := listener.Accept()
    24  			log.Println(err)
    25  			assert.Nil(t, err)
    26  			buf := make([]byte, 200)
    27  			n, err := inClient.Read(buf)
    28  			assert.Equal(t, n, 1)
    29  			inClient.Close()
    30  		}
    31  	}()
    32  
    33  	for i := 0; i < 3; i++ {
    34  		conn, err := tls.Dial("tcp", "127.0.0.1:"+port,
    35  			&tls.Config{InsecureSkipVerify: false,
    36  				ServerName: "abc.com",
    37  				UseJLS:     true, JLSPWD: []byte("abc"), JLSIV: []byte("abc"), SessionTicketsDisabled: false})
    38  		assert.Nil(t, err)
    39  		conn.Write([]byte{1})
    40  		err = conn.Close()
    41  		assert.Nil(t, err)
    42  	}
    43  }