Skip to content

Commit

Permalink
remove loops in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Dec 20, 2023
1 parent 6f38f8a commit 47420d1
Show file tree
Hide file tree
Showing 13 changed files with 1,220 additions and 1,084 deletions.
94 changes: 32 additions & 62 deletions internal/certificates/jks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,46 @@ import (
)

func TestExtractJKSCertificatesInfo(t *testing.T) {
testCases := []testCase{
{
Name: "Test JKS certificate - broken",
Cert: Certificate{
Name: "TestCert",
Path: "../../tests/certs/jks/broken.jks",
Password: "password",
},
t.Run("Test JKS certificate - broken", func(t *testing.T) {
tc := testCase{
Name: "Test JKS certificate - broken",
Cert: Certificate{Name: "TestCert", Path: "../../tests/certs/jks/broken.jks", Password: "password"},
ExpectedResults: []CertificateInfo{},
ExpectedError: "Failed to load JKS file 'TestCert': got invalid magic",
},
{
}
if err := runExtractCertificateUnitTest(tc, t, ExtractJKSCertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test JKS certificate - valid", func(t *testing.T) {
tc := testCase{
Name: "Test JKS certificate - valid",
Cert: Certificate{
Name: "TestCert",
Password: "password",
Path: "../../tests/certs/jks/regular.jks",
},
Cert: Certificate{Name: "TestCert", Password: "password", Path: "../../tests/certs/jks/regular.jks"},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Type: "jks",
Epoch: 1723973250,
Subject: "CN=regular,OU=MyOrganization,O=MyCompany,L=MyCity,ST=MyState,C=MyCountry",
},
{Name: "TestCert", Type: "jks", Epoch: 1723973250, Subject: "CN=regular,OU=MyOrganization,O=MyCompany,L=MyCity,ST=MyState,C=MyCountry"},
},
ExpectedError: "",
},
{
}
if err := runExtractCertificateUnitTest(tc, t, ExtractJKSCertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test JKS certificate - valid chain", func(t *testing.T) {
tc := testCase{
Name: "Test JKS certificate - valid chain",
Cert: Certificate{
Name: "TestCert",
Password: "password",
Path: "../../tests/certs/jks/chain.jks",
},
Cert: Certificate{Name: "TestCert", Password: "password", Path: "../../tests/certs/jks/chain.jks"},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Type: "jks",
Epoch: 1723973251,
Subject: "CN=root",
},
{
Name: "TestCert",
Type: "jks",
Epoch: 1723973256,
Subject: "CN=intermediate",
},
{
Name: "TestCert",
Type: "jks",
Epoch: 1723973256,
Subject: "CN=leaf",
},
{
Name: "TestCert",
Type: "jks",
Epoch: 1723973251,
Subject: "CN=chain,OU=MyOrganization,O=MyCompany,L=MyCity,ST=MyState,C=MyCountry",
},
{Name: "TestCert", Type: "jks", Epoch: 1723973251, Subject: "CN=root"},
{Name: "TestCert", Type: "jks", Epoch: 1723973256, Subject: "CN=intermediate"},
{Name: "TestCert", Type: "jks", Epoch: 1723973256, Subject: "CN=leaf"},
{Name: "TestCert", Type: "jks", Epoch: 1723973251, Subject: "CN=chain,OU=MyOrganization,O=MyCompany,L=MyCity,ST=MyState,C=MyCountry"},
},
ExpectedError: "",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
if err := runExtractCertificateUnitTest(tc, t, ExtractJKSCertificatesInfo); err != nil {
t.Error(err)
}
})
}
}
if err := runExtractCertificateUnitTest(tc, t, ExtractJKSCertificatesInfo); err != nil {
t.Error(err)
}
})
}
209 changes: 81 additions & 128 deletions internal/certificates/p12_test.go
Original file line number Diff line number Diff line change
@@ -1,141 +1,94 @@
package certificates

import (
"testing"
)
import "testing"

func TestExtractP12CertificatesInfo(t *testing.T) {
testCases := []testCase{
{
Name: "Test JKS certificate - JKS with pkcs12",
Cert: Certificate{
Name: "TestCert",
Password: "password",
Path: "../../tests/certs/jks/pkcs12.jks",
},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Epoch: 1724097113,
Subject: "CN=pkcs12",
Type: "p12",
},
},
ExpectedError: "",
},
{
Name: "Test P12 certificate with no password",
Cert: Certificate{
Name: "TestCert",
Password: "",
Path: "../../tests/certs/p12/without_password.p12",
},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Subject: "CN=without_password",
Epoch: 1722925468,
Type: "p12",
},
},
ExpectedError: "",
},
{
Name: "Test P12 certificate with password",
Cert: Certificate{
Name: "TestCert",
Path: "../../tests/certs/p12/with_password.p12",
Password: "password",
},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Subject: "CN=with_password",
Epoch: 1722925469,
Type: "p12",
},
},
ExpectedError: "",
},
{
Name: "Test P12 certificate with wrong password",
Cert: Certificate{
Name: "TestCert",
Password: "wrong",
Path: "../../tests/certs/p12/with_password.p12",
},
t.Run("Test JKS certificate - JKS with pkcs12", func(t *testing.T) {
tc := testCase{
Name: "Test JKS certificate - JKS with pkcs12",
Cert: Certificate{Name: "TestCert", Password: "password", Path: "../../tests/certs/jks/pkcs12.jks"},
ExpectedResults: []CertificateInfo{{Name: "TestCert", Epoch: 1724097113, Subject: "CN=pkcs12", Type: "p12"}},
ExpectedError: "",
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test P12 certificate with no password", func(t *testing.T) {
tc := testCase{
Name: "Test P12 certificate with no password",
Cert: Certificate{Name: "TestCert", Password: "", Path: "../../tests/certs/p12/without_password.p12"},
ExpectedResults: []CertificateInfo{{Name: "TestCert", Subject: "CN=without_password", Epoch: 1722925468, Type: "p12"}},
ExpectedError: "",
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test P12 certificate with password", func(t *testing.T) {
tc := testCase{
Name: "Test P12 certificate with password",
Cert: Certificate{Name: "TestCert", Path: "../../tests/certs/p12/with_password.p12", Password: "password"},
ExpectedResults: []CertificateInfo{{Name: "TestCert", Subject: "CN=with_password", Epoch: 1722925469, Type: "p12"}},
ExpectedError: "",
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test P12 certificate with wrong password", func(t *testing.T) {
tc := testCase{
Name: "Test P12 certificate with wrong password",
Cert: Certificate{Name: "TestCert", Password: "wrong", Path: "../../tests/certs/p12/with_password.p12"},
ExpectedResults: []CertificateInfo{{}},
ExpectedError: "Failed to decode P12 file 'TestCert': pkcs12: decryption password incorrect",
},
{
Name: "Test P12 certificate with is broken",
Cert: Certificate{
Name: "TestCert",
Password: "",
Path: "../../tests/certs/p12/broken.p12",
},
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test P12 certificate which is broken", func(t *testing.T) {
tc := testCase{
Name: "Test P12 certificate which is broken",
Cert: Certificate{Name: "TestCert", Password: "", Path: "../../tests/certs/p12/broken.p12"},
ExpectedResults: []CertificateInfo{{}},
ExpectedError: "Failed to decode P12 file 'TestCert': pkcs12: error reading P12 data: asn1: structure error: tags don't match (16 vs {class:1 tag:2 length:114 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} pfxPdu @2",
},
{
Name: "Test P12 certificate without subject",
Cert: Certificate{
Name: "TestCert",
Path: "../../tests/certs/p12/empty_subject.p12",
Password: "password",
},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Subject: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
Epoch: 1722925468,
Type: "p12",
},
},
ExpectedError: "",
},
{
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test P12 certificate without subject", func(t *testing.T) {
tc := testCase{
Name: "Test P12 certificate without subject",
Cert: Certificate{Name: "TestCert", Path: "../../tests/certs/p12/empty_subject.p12", Password: "password"},
ExpectedResults: []CertificateInfo{{Name: "TestCert", Subject: "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU", Epoch: 1722925468, Type: "p12"}},
ExpectedError: "",
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})

t.Run("Test P12 certificate with chain", func(t *testing.T) {
tc := testCase{
Name: "Test P12 certificate with chain",
Cert: Certificate{
Name: "TestCert",
Path: "../../tests/certs/p12/chain.p12",
Password: "password",
},
Cert: Certificate{Name: "TestCert", Path: "../../tests/certs/p12/chain.p12", Password: "password"},
ExpectedResults: []CertificateInfo{
{
Name: "TestCert",
Subject: "CN=final",
Epoch: 1722925468,
Type: "p12",
},
{
Name: "TestCert",
Subject: "CN=intermediate",
Epoch: 1722925468,
Type: "p12",
},
{
Name: "TestCert",
Subject: "CN=root",
Epoch: 1722925468,
Type: "p12",
},
{
Name: "TestCert",
Subject: "CN=final",
Epoch: 1722925468,
Type: "p12",
},
{Name: "TestCert", Subject: "CN=final", Epoch: 1722925468, Type: "p12"},
{Name: "TestCert", Subject: "CN=intermediate", Epoch: 1722925468, Type: "p12"},
{Name: "TestCert", Subject: "CN=root", Epoch: 1722925468, Type: "p12"},
{Name: "TestCert", Subject: "CN=final", Epoch: 1722925468, Type: "p12"},
},
ExpectedError: "",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})
}
}
if err := runExtractCertificateUnitTest(tc, t, ExtractP12CertificatesInfo); err != nil {
t.Error(err)
}
})
}
Loading

0 comments on commit 47420d1

Please sign in to comment.