github.com/danielpfeifer02/quic-go-prio-packs@v0.41.0-28/internal/qtls/cipher_suite_test.go (about)

     1  package qtls
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"net"
     7  
     8  	"github.com/danielpfeifer02/quic-go-prio-packs/internal/testdata"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Setting the Cipher Suite", func() {
    15  	for _, cs := range []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_CHACHA20_POLY1305_SHA256, tls.TLS_AES_256_GCM_SHA384} {
    16  		cs := cs
    17  
    18  		It(fmt.Sprintf("selects %s", tls.CipherSuiteName(cs)), func() {
    19  			reset := SetCipherSuite(cs)
    20  			defer reset()
    21  
    22  			ln, err := tls.Listen("tcp4", "localhost:0", testdata.GetTLSConfig())
    23  			Expect(err).ToNot(HaveOccurred())
    24  			defer ln.Close()
    25  
    26  			done := make(chan struct{})
    27  			go func() {
    28  				defer GinkgoRecover()
    29  				defer close(done)
    30  				conn, err := ln.Accept()
    31  				Expect(err).ToNot(HaveOccurred())
    32  				_, err = conn.Read(make([]byte, 10))
    33  				Expect(err).ToNot(HaveOccurred())
    34  				Expect(conn.(*tls.Conn).ConnectionState().CipherSuite).To(Equal(cs))
    35  			}()
    36  
    37  			conn, err := tls.Dial(
    38  				"tcp4",
    39  				fmt.Sprintf("localhost:%d", ln.Addr().(*net.TCPAddr).Port),
    40  				&tls.Config{RootCAs: testdata.GetRootCA()},
    41  			)
    42  			Expect(err).ToNot(HaveOccurred())
    43  			_, err = conn.Write([]byte("foobar"))
    44  			Expect(err).ToNot(HaveOccurred())
    45  			Expect(conn.ConnectionState().CipherSuite).To(Equal(cs))
    46  			Expect(conn.Close()).To(Succeed())
    47  			Eventually(done).Should(BeClosed())
    48  		})
    49  	}
    50  })