1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Complete Certs test

This commit is contained in:
JanHoefelmeyer 2023-09-19 16:11:23 +02:00
parent 3a7b411789
commit e4c636fe41
3 changed files with 111 additions and 8 deletions

View file

@ -3,24 +3,48 @@ package certs
import "testing"
func TestLoadCertificates(t *testing.T) {
TestCert := "data/testclient.crt"
Testkey := "data/testclientkey.pem"
Passphrase := "security123"
testCert := "data/testclient.crt"
testKey := "data/testclientkey.pem"
passphrase := "qwer"
missingCert := "data/testclientcert_missing.crt"
missingTestkey := "data/testclientkey_missing.pem"
privateKey := "data/privated.pem"
privateCert := "data/cert.crt"
if certificate, err := LoadCertificate(&TestCert, &Testkey, nil); certificate == nil || err != nil {
// Try to load certificate that is not protected, expect success.
if certificate, err := LoadCertificate(&testCert, &testKey, nil); certificate == nil || err != nil {
t.Errorf("Failure: Couldn't load supposedly valid certificate.")
}
if certificate, err := LoadCertificate(nil, &Testkey, nil); certificate != nil || err == nil {
// Try to load no certificate, expect error.
if certificate, err := LoadCertificate(nil, &testKey, nil); certificate != nil || err == nil {
t.Errorf("Failure: No error despite missing certificate")
}
if certificate, err := LoadCertificate(&TestCert, &missingTestkey, nil); certificate != nil || err == nil {
// Try to load certificate using a nonexistent key, expect error.
if certificate, err := LoadCertificate(&testCert, &missingTestkey, nil); certificate != nil || err == nil {
t.Errorf("Failure: No Failure while loading certificate using missing key.")
}
if certificate, err := LoadCertificate(&TestCert, &Testkey, &Passphrase); certificate != nil || err == nil {
// Try to decrypt not encrypted certificate, expect error
if certificate, err := LoadCertificate(&testCert, &testKey, &passphrase); certificate != nil || err == nil {
t.Errorf("Failure: Could load unprotected valid certificate with passphrase.")
}
if certificate, err := LoadCertificate(&TestCert, &missingTestkey, &Passphrase); certificate != nil || err == nil {
// Try to load encrypted certificate using a nonexistent key, but valid passphrase. Expect error.
if certificate, err := LoadCertificate(&testCert, &missingTestkey, &passphrase); certificate != nil || err == nil {
t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.")
}
// Try to load encrypted certificate, expecting success.
if certificate, err := LoadCertificate(&privateCert, &privateKey, &passphrase); certificate == nil || err != nil {
t.Errorf("Failure: Couldn't load supposedly valid encrypted certificate.")
}
// Try to load wrong encrypted certificate, expecting error.
if certificate, err := LoadCertificate(&testKey, &privateKey, &passphrase); certificate != nil || err == nil {
t.Errorf("Failure: No Failure while loading certificate using wrong encrypted key.")
}
// Try to load nonexistent encrypted certificate, expecting error.
if certificate, err := LoadCertificate(&missingCert, &privateKey, &passphrase); certificate != nil || err == nil {
t.Errorf("Failure: No Failure while loading nonexistens certificate.")
}
// Try to load nonexistent encrypted certificate, expecting error.
if certificate, err := LoadCertificate(nil, nil, nil); certificate != nil || err != nil {
t.Errorf("Failure: Expected nil return.")
}
}