Improve test coverage of package hsm
parent
5776723fa2
commit
c532ec436a
@ -0,0 +1,196 @@
|
||||
/*
|
||||
Copyright 2022 CAcert Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package hsm_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cacert.org/cacert-gosigner/pkg/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"git.cacert.org/cacert-gosigner/pkg/hsm"
|
||||
)
|
||||
|
||||
func TestEnsureCAKeysAndCertificates_not_in_setup_mode(t *testing.T) {
|
||||
testConfig := setupSignerConfig(t)
|
||||
setupSoftHsm(t)
|
||||
|
||||
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
|
||||
|
||||
ctx := hsm.SetupContext(
|
||||
hsm.CaConfigOption(testConfig),
|
||||
hsm.CADirectoryOption(t.TempDir()))
|
||||
|
||||
err := hsm.EnsureCAKeysAndCertificates(ctx)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := hsm.CloseP11Contexts(ctx)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
assert.ErrorContains(t, err, "not in setup mode")
|
||||
}
|
||||
|
||||
func prepareSoftHSM(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
|
||||
testConfig := setupSignerConfig(t)
|
||||
setupSoftHsm(t)
|
||||
|
||||
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
|
||||
|
||||
ctx := hsm.SetupContext(
|
||||
hsm.CaConfigOption(testConfig),
|
||||
hsm.SetupModeOption(),
|
||||
hsm.CADirectoryOption(t.TempDir()))
|
||||
|
||||
err := hsm.EnsureCAKeysAndCertificates(ctx)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := hsm.CloseP11Contexts(ctx)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestGetRootCACertificate(t *testing.T) {
|
||||
ctx := prepareSoftHSM(t)
|
||||
|
||||
testData := map[string]struct {
|
||||
label, errMsg string
|
||||
}{
|
||||
"known root": {
|
||||
label: "root",
|
||||
},
|
||||
"unknown root": {
|
||||
label: "unknown",
|
||||
errMsg: "could not get CA definition for label unknown",
|
||||
},
|
||||
"known intermediary": {
|
||||
label: "sub1",
|
||||
errMsg: "CA definition sub1 is not a root CA definition",
|
||||
},
|
||||
}
|
||||
|
||||
for name, item := range testData {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
root, err := hsm.GetRootCACertificate(ctx, item.label)
|
||||
|
||||
if item.errMsg != "" {
|
||||
assert.ErrorContains(t, err, item.errMsg)
|
||||
assert.Nil(t, root)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, root)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntermediaryCACertificate(t *testing.T) {
|
||||
ctx := prepareSoftHSM(t)
|
||||
|
||||
testData := map[string]struct {
|
||||
label, errMsg string
|
||||
}{
|
||||
"known intermediary": {
|
||||
label: "sub1",
|
||||
},
|
||||
"unknown intermediary": {
|
||||
label: "unknown",
|
||||
errMsg: "could not get CA definition for label unknown",
|
||||
},
|
||||
"known root": {
|
||||
label: "root",
|
||||
errMsg: "CA definition root is a root CA definition, intermediary expected",
|
||||
},
|
||||
}
|
||||
|
||||
for name, item := range testData {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
root, err := hsm.GetIntermediaryCACertificate(ctx, item.label)
|
||||
|
||||
if item.errMsg != "" {
|
||||
assert.ErrorContains(t, err, item.errMsg)
|
||||
assert.Nil(t, root)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, root)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSAKeyGeneration(t *testing.T) {
|
||||
const testRSASignerConfig = `---
|
||||
Settings:
|
||||
organization:
|
||||
organization: ["Acme CAs Ltd."]
|
||||
validity-years:
|
||||
root: 30
|
||||
intermediary: 10
|
||||
url-patterns:
|
||||
ocsp: http://ocsp.example.org/
|
||||
crl: http://crl.example.org/%s.crl
|
||||
issuer: http://%s.cas.example.org/
|
||||
CAs:
|
||||
root:
|
||||
common-name: "Acme CAs root"
|
||||
key-info:
|
||||
algorithm: RSA
|
||||
rsa-bits: 2048
|
||||
KeyStorage:
|
||||
default:
|
||||
type: softhsm
|
||||
label: acme-test-hsm
|
||||
`
|
||||
testConfig, err := config.LoadConfiguration(strings.NewReader(testRSASignerConfig))
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
setupSoftHsm(t)
|
||||
|
||||
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
|
||||
|
||||
ctx := hsm.SetupContext(
|
||||
hsm.CaConfigOption(testConfig),
|
||||
hsm.SetupModeOption(),
|
||||
hsm.CADirectoryOption(t.TempDir()))
|
||||
|
||||
err = hsm.EnsureCAKeysAndCertificates(ctx)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := hsm.CloseP11Contexts(ctx)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
root, err := hsm.GetRootCACertificate(ctx, "root")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, root)
|
||||
assert.Equal(t, x509.RSA, root.PublicKeyAlgorithm)
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
Copyright 2022 CAcert Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package hsm_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"git.cacert.org/cacert-gosigner/pkg/hsm"
|
||||
)
|
||||
|
||||
func TestEnsureCAKeysAndCertificates(t *testing.T) {
|
||||
testConfig := setupSignerConfig(t)
|
||||
setupSoftHsm(t)
|
||||
|
||||
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
log.SetOutput(buf)
|
||||
|
||||
ctx := hsm.SetupContext(
|
||||
hsm.CaConfigOption(testConfig),
|
||||
hsm.SetupModeOption(),
|
||||
hsm.CADirectoryOption(t.TempDir()))
|
||||
|
||||
err := hsm.EnsureCAKeysAndCertificates(ctx)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := hsm.CloseP11Contexts(ctx)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
output := buf.String()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, output, "found root CA certificate root: Acme CAs root")
|
||||
assert.Contains(t, output, "found intermediary CA certificate sub1: Acme CAs server sub CA")
|
||||
}
|
||||
|
||||
func TestEnsureCAKeysAndCertificates_verbose(t *testing.T) {
|
||||
testConfig := setupSignerConfig(t)
|
||||
setupSoftHsm(t)
|
||||
|
||||
t.Setenv("TOKEN_PIN_ACME_TEST_HSM", "123456")
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
log.SetOutput(buf)
|
||||
|
||||
ctx := hsm.SetupContext(
|
||||
hsm.CaConfigOption(testConfig),
|
||||
hsm.SetupModeOption(),
|
||||
hsm.VerboseLoggingOption(),
|
||||
hsm.CADirectoryOption(t.TempDir()))
|
||||
|
||||
err := hsm.EnsureCAKeysAndCertificates(ctx)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := hsm.CloseP11Contexts(ctx)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
output := buf.String()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, output, "found root CA certificate root:\n Subject")
|
||||
assert.Contains(t, output, "found intermediary CA certificate sub1:\n Subject")
|
||||
}
|
Loading…
Reference in New Issue