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

Cosmetics

This commit is contained in:
Sascha L. Teichmann 2023-09-25 13:05:53 +02:00
parent 1f7d5ada14
commit 21fbd401b7
5 changed files with 36 additions and 45 deletions

View file

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

View file

@ -22,7 +22,7 @@ func TestNewPatternMatcher(t *testing.T) {
} }
regex = append(regex, "++") regex = append(regex, "++")
if pm, err := NewPatternMatcher(regex); pm != nil || err == nil { 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")
} }
} }

View file

@ -1,10 +0,0 @@
// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2023 Intevation GmbH <https://intevation.de>
// Package misc implements miscellaneous helper functions.
package misc

View file

@ -9,7 +9,7 @@
package misc package misc
import ( import (
"bytes" "io"
"mime/multipart" "mime/multipart"
"testing" "testing"
) )
@ -17,8 +17,7 @@ import (
// TestCreateFormFile tests if CreateFormFile throws an error when creating // TestCreateFormFile tests if CreateFormFile throws an error when creating
// a FormFile // a FormFile
func TestCreateFormFile(t *testing.T) { func TestCreateFormFile(t *testing.T) {
body := new(bytes.Buffer) writer := multipart.NewWriter(io.Discard)
writer := multipart.NewWriter(body)
if _, err := CreateFormFile(writer, "csaf", "data", "application/json"); err != nil { if _, err := CreateFormFile(writer, "csaf", "data", "application/json"); err != nil {
t.Errorf("Failure: failed to create an io.Writer via CreateFormFile") t.Errorf("Failure: failed to create an io.Writer via CreateFormFile")

View file

@ -92,8 +92,8 @@ func TestLoadToml(t *testing.T) {
t.Errorf("Failure: Didn't throw the correct " + t.Errorf("Failure: Didn't throw the correct " +
"error on trying to load nonexistant file") "error on trying to load nonexistant file")
} }
if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != "could not parse [\"surplus\"] "+ const errMsg = `could not parse ["surplus"] from "data/config_plus.toml"`
"from \"data/config_plus.toml\"" { if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != errMsg {
t.Errorf("Failure: Succeeded in parsing nonexistant parameter") t.Errorf("Failure: Succeeded in parsing nonexistant parameter")
} }
if err := loadTOML(&cfg, "data/config.toml"); err != nil { if err := loadTOML(&cfg, "data/config.toml"); err != nil {