Skip to content

fix TLS tests on newer go versions and MacOS #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,7 @@ func TestSSLVerifyFull(t *testing.T) {
if err == nil {
t.Fatal("expected error")
}
{
var x509err x509.UnknownAuthorityError
if !errors.As(err, &x509err) {
var x509err x509.HostnameError
if !errors.As(err, &x509err) {
t.Fatalf("expected x509.UnknownAuthorityError or x509.HostnameError, got %#+v", err)
}
}
}
assertInvalidCertificate(t, err)

rootCertPath := filepath.Join(os.Getenv("PQSSLCERTTEST_PATH"), "root.crt")
rootCert := "sslrootcert=" + rootCertPath + " "
Expand Down Expand Up @@ -172,7 +164,7 @@ func TestSSLVerifyCA(t *testing.T) {
{
_, err := openSSLConn(t, "host=postgres sslmode=verify-ca user=pqgossltest")
var x509err x509.UnknownAuthorityError
if !errors.As(err, &x509err) {
if !errors.As(err, &x509err) && err.Error() != errMacOsCertificateNotCompliant {
t.Fatalf("expected %T, got %#+v", x509.UnknownAuthorityError{}, err)
}
}
Expand All @@ -181,7 +173,7 @@ func TestSSLVerifyCA(t *testing.T) {
{
_, err := openSSLConn(t, "host=postgres sslmode=verify-ca user=pqgossltest sslrootcert=''")
var x509err x509.UnknownAuthorityError
if !errors.As(err, &x509err) {
if !errors.As(err, &x509err) && err.Error() != errMacOsCertificateNotCompliant {
t.Fatalf("expected %T, got %#+v", x509.UnknownAuthorityError{}, err)
}
}
Expand Down
31 changes: 31 additions & 0 deletions ssl_test_go20minus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build !go1.20
// +build !go1.20

package pq

import (
"crypto/x509"
"testing"
)

const (
// Error specific to MacOS when certificate is missing SCT, see
// https://github.com/golang/go/issues/51991
// Validating such certificate always results with this error first
errMacOsCertificateNotCompliant = `x509: “postgres” certificate is not standards compliant`
)

func assertInvalidCertificate(t *testing.T, err error) {
if err.Error() == errMacOsCertificateNotCompliant {
return
}

switch x := err.(type) {
case x509.UnknownAuthorityError:
break
case x509.HostnameError:
break
default:
t.Fatalf("expected x509.UnknownAuthorityError, x509.HostnameError, got %#+v", x)
}
}
30 changes: 30 additions & 0 deletions ssl_test_go20plus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build go1.20
// +build go1.20

package pq

import (
"crypto/tls"
"crypto/x509"
"testing"
)

const (
// Error specific to MacOS when certificate is missing SCT, see
// https://github.com/golang/go/issues/51991
// Validating such certificate always results with this error first
errMacOsCertificateNotCompliant = `x509: “postgres” certificate is not standards compliant`
)

func assertInvalidCertificate(t *testing.T, err error) {
switch x := err.(type) {
case x509.UnknownAuthorityError:
break
case x509.HostnameError:
break
case *tls.CertificateVerificationError:
break
default:
t.Fatalf("expected x509.UnknownAuthorityError, x509.HostnameError or tls.CertificateVerificationError (go 1.20+), got %#+v", x)
}
}