Skip to content

Commit

Permalink
Complete Certs test
Browse files Browse the repository at this point in the history
  • Loading branch information
JanHoefelmeyer committed Sep 19, 2023
1 parent 3a7b411 commit e4c636f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
40 changes: 32 additions & 8 deletions internal/certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
37 changes: 37 additions & 0 deletions internal/certs/data/cert.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-----BEGIN CERTIFICATE-----
MIIGajCCBNKgAwIBAgIUGNi4GgCUssOOe3k0VuHf3R0+d54wDQYJKoZIhvcNAQEL
BQAwgY0xFDASBgNVBAMTC0NvbW1vbiBuYW1lMRMwEQYDVQQLEwppbnRldmF0aW9u
MRMwEQYDVQQKEwppbnRldmF0aW9uMRMwEQYDVQQHEwppbnRldmF0aW9uMRUwEwYD
VQQIEwxMb3dlciBTYXhvbnkxCzAJBgNVBAYTAkdFMRIwEAYKCZImiZPyLGQBGRYC
REMwHhcNMjMwOTE5MDcwMDA1WhcNMjYwNjE0MDcwMDA3WjCB8DEQMA4GA1UEAxMH
cmVxdWVzdDETMBEGA1UECxMKaW50ZXZhdGlvbjETMBEGA1UEChMKaW50ZXZhdGlv
bjETMBEGA1UEBxMKb3NuYWJydWVjazEVMBMGA1UECBMMbG93ZXIgc2F4b255MQsw
CQYDVQQGEwJHRTESMBAGCgmSJomT8ixkARkWAkRDMREwDwYKCZImiZPyLGQBGR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A8GA1UdDwEB/wQFAwMHsAAwHQYDVR0OBBYEFKrFhODjTKCopb+W
Qa29PsHR4HXgMB8GA1UdIwQYMBaAFCyZxCa1ZUHVy8LjikE8zumAiEgfMA0GCSqG
SIb3DQEBCwUAA4IBgQBTrAgh6d+qiLumEfmkNCmhewxKxDZp+Ni2nz9XRzNO2cQE
U0n8MdbnQInW3xJXng2sAcl1fQz0RN1hkyjDwi69mbbPgcTYmxJFvyt+zRYBe/Sq
4CGGkxEdPW94tMpQ6SrCn2mAMnvcq9A1pYBVYyPeUsaRHC5OUBFOMCevNy8JwNyY
MJ0H5HQCyCysbzA1d521pogGUs/tmbE+ym9zpV8vG0b6De1PexjVeGkTNYz6NCR2
VZTQ+OJ5iE5pHPEC1Qif44LrR9Kdn/wu3RjTYyHeBOJFjK+DKgleNF4QVTcZQIPE
snN4H+/VSgTZQ3kgWbtpd1m5oRBJovEc2Qe+l+iDFCk8OA4z/x+fkvOeD3NUAl7D
9Pt3cP3UtWUJp4NJn2dvUljmQhB02HSqdNBhqKSg4/cf7l8Zo1ejvBUosrlgw3C3
apDaC4/xk7woFKVYW25teH2ze+Gpz/YsLDtmL7Bri8CGVsqsN9yqO8SstwKBa3Rt
xQ2em6XnnanApT4iFX4=
-----END CERTIFICATE-----
42 changes: 42 additions & 0 deletions internal/certs/data/privated.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-CBC,054A583F6C90570F

tlGw8qlO25FaQdRLkai5L1JHWz/5fC4zd3qFISWssYH2FEnz8yfYsCoRLivVYhJB
fswOTj9h5b1RYRsWfIwCGfyNeOj8hkQrLwCW607vbhydGGJ4xc5RBF9MK0QCjSNT
r8myedNyfI4nm5enNVFDqYsqAc7cA3m1qw+QsAhPOrASDTp5svHR7g9+T6P5GDHm
B79nap02kfmodC7ytmWDBEclJ45Y19LOucN0+Nl6JgKkQEfWB/p2s2kGAGY1Of3X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 comments on commit e4c636f

Please sign in to comment.