Skip to content

Commit

Permalink
Refactor tests for BuildCertificateChain
Browse files Browse the repository at this point in the history
Uses a test fixture for chain instead of creating them during test.
  • Loading branch information
stevenvegt committed Dec 3, 2024
1 parent 5764d72 commit 0de134e
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions uzi_vc_issuer/ura_issuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,20 @@ func TestBuildUraVerifiableCredential(t *testing.T) {
}

func TestBuildCertificateChain(t *testing.T) {
certs, _, _, _, _, err := x509_cert.BuildSelfSignedCertChain("2.16.528.1.1007.99.2110-1-900030787-S-90000380-00.000-11223344", "90000380")
failError(t, err)
chainBytes, err := os.ReadFile("testdata/valid_chain.pem")
require.NoError(t, err, "failed to read chain")

certs, err := parsePEMCertificates(t, chainBytes)
require.NoError(t, err, "failed to parse chain")

tests := []struct {
name string
errorText string
in func(certs []*x509.Certificate) []*x509.Certificate
out func(certs []*x509.Certificate) []*x509.Certificate
}{
{
name: "happy flow",
name: "ok - valid cert input",
in: func(certs []*x509.Certificate) []*x509.Certificate {
return certs
},
Expand All @@ -174,31 +178,31 @@ func TestBuildCertificateChain(t *testing.T) {
errorText: "",
},
{
name: "no signing certificate",
name: "ok - it handles out of order certificates",
in: func(certs []*x509.Certificate) []*x509.Certificate {
certs = certs[1:]
certs = []*x509.Certificate{certs[2], certs[0], certs[3], certs[1]}
return certs
},
out: func(certs []*x509.Certificate) []*x509.Certificate {
return nil
return certs
},
errorText: "failed to find signing certificate",
errorText: "",
},
{
name: "no root CA certificate",
name: "nok - missing signing certificate",
in: func(certs []*x509.Certificate) []*x509.Certificate {
certs = certs[:3]
certs = certs[1:]
return certs
},
out: func(certs []*x509.Certificate) []*x509.Certificate {
return nil
},
errorText: "failed to find path from signingCert to root",
errorText: "failed to find signing certificate",
},
{
name: "no intermediate CA certificate type 1",
name: "nok - missing root CA certificate",
in: func(certs []*x509.Certificate) []*x509.Certificate {
certs = []*x509.Certificate{certs[0], certs[2], certs[3]}
certs = certs[:3]
return certs
},
out: func(certs []*x509.Certificate) []*x509.Certificate {
Expand All @@ -207,9 +211,9 @@ func TestBuildCertificateChain(t *testing.T) {
errorText: "failed to find path from signingCert to root",
},
{
name: "no intermediate CA certificate type 2",
name: "nok - missing first intermediate CA certificate",
in: func(certs []*x509.Certificate) []*x509.Certificate {
certs = []*x509.Certificate{certs[0], certs[1], certs[3]}
certs = []*x509.Certificate{certs[0], certs[2], certs[3]}
return certs
},
out: func(certs []*x509.Certificate) []*x509.Certificate {
Expand All @@ -218,30 +222,16 @@ func TestBuildCertificateChain(t *testing.T) {
errorText: "failed to find path from signingCert to root",
},
{
name: "no intermediate CA certificate type 3",
name: "nok - missing second intermediate CA certificate",
in: func(certs []*x509.Certificate) []*x509.Certificate {
certs = []*x509.Certificate{certs[0], nil, certs[2], certs[3]}
certs = []*x509.Certificate{certs[0], certs[1], certs[3]}
return certs
},
out: func(certs []*x509.Certificate) []*x509.Certificate {
return nil
},
errorText: "failed to find path from signingCert to root",
},
{
name: "reverse certificate order",
in: func(certs []*x509.Certificate) []*x509.Certificate {
rv := make([]*x509.Certificate, 0)
for i := len(certs) - 1; i >= 0; i-- {
rv = append(rv, certs[i])
}
return rv
},
out: func(certs []*x509.Certificate) []*x509.Certificate {
return certs
},
errorText: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 0de134e

Please sign in to comment.