(t *testing.T)
| 757 | } |
| 758 | |
| 759 | func TestTLSConfig(t *testing.T) { |
| 760 | configTLSConfig := TLSConfig{ |
| 761 | CAFile: TLSCAChainPath, |
| 762 | CertFile: ClientCertificatePath, |
| 763 | KeyFile: ClientKeyNoPassPath, |
| 764 | ServerName: "localhost", |
| 765 | InsecureSkipVerify: false, |
| 766 | } |
| 767 | |
| 768 | tlsCAChain, err := os.ReadFile(TLSCAChainPath) |
| 769 | require.NoErrorf(t, err, "Can't read the CA certificate chain (%s)", |
| 770 | TLSCAChainPath) |
| 771 | rootCAs := x509.NewCertPool() |
| 772 | rootCAs.AppendCertsFromPEM(tlsCAChain) |
| 773 | |
| 774 | expectedTLSConfig := &tls.Config{ |
| 775 | RootCAs: rootCAs, |
| 776 | ServerName: configTLSConfig.ServerName, |
| 777 | InsecureSkipVerify: configTLSConfig.InsecureSkipVerify, |
| 778 | } |
| 779 | |
| 780 | tlsConfig, err := NewTLSConfig(&configTLSConfig) |
| 781 | require.NoErrorf(t, err, "Can't create a new TLS Config from a configuration (%s).", err) |
| 782 | |
| 783 | clientCertificate, err := tls.LoadX509KeyPair(ClientCertificatePath, ClientKeyNoPassPath) |
| 784 | require.NoErrorf(t, err, "Can't load the client key pair ('%s' and '%s'). Reason: %s", |
| 785 | ClientCertificatePath, ClientKeyNoPassPath, err) |
| 786 | cert, err := tlsConfig.GetClientCertificate(nil) |
| 787 | require.NoErrorf(t, err, "unexpected error returned by tlsConfig.GetClientCertificate(): %s", err) |
| 788 | require.Truef(t, reflect.DeepEqual(cert, &clientCertificate), "Unexpected client certificate result: \n\n%+v\n expected\n\n%+v", cert, clientCertificate) |
| 789 | |
| 790 | // tlsConfig.rootCAs.LazyCerts contains functions getCert() in go 1.16, which are |
| 791 | // never equal. Compare the Subjects instead. |
| 792 | //nolint:staticcheck // Ignore SA1019. (*CertPool).Subjects is deprecated because it may not include the system certs but it isn't the case here. |
| 793 | require.Truef(t, reflect.DeepEqual(tlsConfig.RootCAs.Subjects(), expectedTLSConfig.RootCAs.Subjects()), "Unexpected RootCAs result: \n\n%+v\n expected\n\n%+v", tlsConfig.RootCAs.Subjects(), expectedTLSConfig.RootCAs.Subjects()) |
| 794 | tlsConfig.RootCAs = nil |
| 795 | expectedTLSConfig.RootCAs = nil |
| 796 | |
| 797 | // Non-nil functions are never equal. |
| 798 | tlsConfig.GetClientCertificate = nil |
| 799 | |
| 800 | require.Truef(t, reflect.DeepEqual(tlsConfig, expectedTLSConfig), "Unexpected TLS Config result: \n\n%+v\n expected\n\n%+v", tlsConfig, expectedTLSConfig) |
| 801 | } |
| 802 | |
| 803 | func TestTLSConfigEmpty(t *testing.T) { |
| 804 | configTLSConfig := TLSConfig{ |
nothing calls this directly
no test coverage detected
searching dependent graphs…