Incomplete coverage of test scenarios and bad code

This commit is contained in:
10284789刁浩 2022-06-14 07:48:43 +00:00
parent 537941765f
commit 696d537239
2 changed files with 17 additions and 12 deletions

View File

@ -247,13 +247,8 @@ func CertOrKeyExist(pkiPath, name string) bool {
_, certErr := os.Stat(certificatePath)
_, keyErr := os.Stat(privateKeyPath)
if os.IsNotExist(certErr) && os.IsNotExist(keyErr) {
// The cert and the key do not exist
return false
}
// Both files exist or one of them
return true
return !(os.IsNotExist(certErr) && os.IsNotExist(keyErr))
}
// CSROrKeyExist returns true if one of the CSR or key exists

View File

@ -262,12 +262,16 @@ func TestCertOrKeyExist(t *testing.T) {
}
defer os.RemoveAll(tmpdir)
caCert := &x509.Certificate{}
actual := WriteCertAndKey(tmpdir, "foo", caCert, rootCAKey)
if actual != nil {
if err = WriteCertAndKey(tmpdir, "foo-0", rootCACert, rootCAKey); err != nil {
t.Errorf(
"failed WriteCertAndKey with an error: %v",
actual,
err,
)
}
if err = WriteCert(tmpdir, "foo-1", rootCACert); err != nil {
t.Errorf(
"failed WriteCert with an error: %v",
err,
)
}
@ -284,9 +288,15 @@ func TestCertOrKeyExist(t *testing.T) {
expected: false,
},
{
desc: "valid path and name",
desc: "valid path and name, both cert and key exist",
path: tmpdir,
name: "foo",
name: "foo-0",
expected: true,
},
{
desc: "valid path and name, only cert exist",
path: tmpdir,
name: "foo-1",
expected: true,
},
}