Skip to content

Commit

Permalink
util: add a unit test to test min tls version (#50450)
Browse files Browse the repository at this point in the history
ref #36036
  • Loading branch information
tiancaiamao authored Jan 16, 2024
1 parent a257521 commit 1d4220d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/util/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,53 @@ func TestVerifyCommonNameAndRotate(t *testing.T) {
require.NoError(t, resp.Body.Close())
}

func TestTLSVersion(t *testing.T) {
caData, certs, keys := generateCerts(t, []string{"server", "client"})
serverCert, serverKey := certs[0], keys[0]
clientCert, clientKey := certs[1], keys[1]

serverTLS, err := util.NewTLSConfig(
util.WithCAContent(caData),
util.WithCertAndKeyContent(serverCert, serverKey),
)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
server, port := runServer(ctx, serverTLS, t)
defer func() {
cancel()
server.Close()
}()
url := fmt.Sprintf("https://127.0.0.1:%d", port)

clientTLS1, err := util.NewTLSConfig(
util.WithCAContent(caData),
util.WithCertAndKeyContent(clientCert, clientKey),
)
require.NoError(t, err)

type testCase struct {
version uint16
succ bool
}
testCases := []testCase{
{tls.VersionTLS10, false},
{tls.VersionTLS11, false},
{tls.VersionTLS12, true},
{tls.VersionTLS13, true},
}
for _, c := range testCases {
clientTLS1.MinVersion = c.version
clientTLS1.MaxVersion = c.version
resp, err := util.ClientWithTLS(clientTLS1).Get(url)
if c.succ {
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
} else {
require.Error(t, err)
}
}
}

func TestCA(t *testing.T) {
caData, certs, keys := generateCerts(t, []string{"server", "client"})
serverCert, serverKey := certs[0], keys[0]
Expand Down

0 comments on commit 1d4220d

Please sign in to comment.