Skip to content

Commit

Permalink
Cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
s-l-teichmann committed Sep 25, 2023
1 parent 1f7d5ad commit 21fbd40
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
60 changes: 31 additions & 29 deletions internal/certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,50 @@ import "testing"
// valid certificates and throws an error at invalid certificates,
// keys or passphrases
func TestLoadCertificates(t *testing.T) {
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"
var (
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"
)

// Try to load certificate that is not protected, expect success.
if certificate, err := LoadCertificate(&testCert, &testKey, nil); certificate == nil || err != nil {
// Try to load cert that is not protected, expect success.
if cert, err := LoadCertificate(&testCert, &testKey, nil); cert == nil || err != nil {
t.Errorf("Failure: Couldn't load supposedly valid certificate.")
}
// Try to load no certificate, expect error.
if certificate, err := LoadCertificate(nil, &testKey, nil); certificate != nil || err == nil {
// Try to load no cert, expect error.
if cert, err := LoadCertificate(nil, &testKey, nil); cert != nil || err == nil {
t.Errorf("Failure: No error despite missing certificate")
}
// Try to load certificate using a nonexistent key, expect error.
if certificate, err := LoadCertificate(&testCert, &missingTestkey, nil); certificate != nil || err == nil {
// Try to load cert using a nonexistent key, expect error.
if cert, err := LoadCertificate(&testCert, &missingTestkey, nil); cert != nil || err == nil {
t.Errorf("Failure: No Failure while loading certificate using missing key.")
}
// Try to decrypt not encrypted certificate, expect error
if certificate, err := LoadCertificate(&testCert, &testKey, &passphrase); certificate != nil || err == nil {
// Try to decrypt not encrypted cert, expect error
if cert, err := LoadCertificate(&testCert, &testKey, &passphrase); cert != nil || err == nil {
t.Errorf("Failure: Could load unprotected valid certificate with passphrase.")
}
// 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 cert using a nonexistent key, but valid passphrase. Expect error.
if cert, err := LoadCertificate(&testCert, &missingTestkey, &passphrase); cert != 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 {
// Try to load encrypted cert, expecting success.
if cert, err := LoadCertificate(&privateCert, &privateKey, &passphrase); cert == 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 wrong encrypted cert, expecting error.
if cert, err := LoadCertificate(&testKey, &privateKey, &passphrase); cert != 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 cert, expecting error.
if cert, err := LoadCertificate(&missingCert, &privateKey, &passphrase); cert != 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.")
// Try to load nonexistent encrypted cert, expecting error.
if cert, err := LoadCertificate(nil, nil, nil); cert != nil || err != nil {
t.Errorf("Failure: Expected nil return.")
}
}
2 changes: 1 addition & 1 deletion internal/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewPatternMatcher(t *testing.T) {
}
regex = append(regex, "++")
if pm, err := NewPatternMatcher(regex); pm != nil || err == nil {
t.Errorf("Failure: No error thrown at invalid compile pattern")
t.Errorf("Failure: No error returned at invalid compile pattern")
}
}

Expand Down
10 changes: 0 additions & 10 deletions internal/misc/doc_test.go

This file was deleted.

5 changes: 2 additions & 3 deletions internal/misc/mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
package misc

import (
"bytes"
"io"
"mime/multipart"
"testing"
)

// TestCreateFormFile tests if CreateFormFile throws an error when creating
// a FormFile
func TestCreateFormFile(t *testing.T) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
writer := multipart.NewWriter(io.Discard)

if _, err := CreateFormFile(writer, "csaf", "data", "application/json"); err != nil {
t.Errorf("Failure: failed to create an io.Writer via CreateFormFile")
Expand Down
4 changes: 2 additions & 2 deletions internal/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func TestLoadToml(t *testing.T) {
t.Errorf("Failure: Didn't throw the correct " +
"error on trying to load nonexistant file")
}
if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != "could not parse [\"surplus\"] "+
"from \"data/config_plus.toml\"" {
const errMsg = `could not parse ["surplus"] from "data/config_plus.toml"`
if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != errMsg {
t.Errorf("Failure: Succeeded in parsing nonexistant parameter")
}
if err := loadTOML(&cfg, "data/config.toml"); err != nil {
Expand Down

0 comments on commit 21fbd40

Please sign in to comment.