Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tolerance in a few tests for existing objects on token #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,30 @@ func TestFindKeysRequiresIdOrLabel(t *testing.T) {

func TestFindingKeysWithAttributes(t *testing.T) {
withContext(t, func(ctx *Context) {

var err error
// Compensate for unclean starting condition
preconditionAttrs_CkaValueLen16 := NewAttributeSet()
err = preconditionAttrs_CkaValueLen16.Set(CkaValueLen, 16)
require.NoError(t, err)

var preconditionKeys_CkaValueLen16 []*SecretKey
preconditionKeys_CkaValueLen16, err = ctx.FindKeysWithAttributes(preconditionAttrs_CkaValueLen16)
require.NoError(t, err)

preconditionAttrs_CkaValueLen32 := NewAttributeSet()
err = preconditionAttrs_CkaValueLen32.Set(CkaValueLen, 32)
require.NoError(t, err)

var preconditionKeys_CkaValueLen32 []*SecretKey
preconditionKeys_CkaValueLen32, err = ctx.FindKeysWithAttributes(preconditionAttrs_CkaValueLen32)
require.NoError(t, err)

label := randomBytes()
label2 := randomBytes()

key, err := ctx.GenerateSecretKeyWithLabel(randomBytes(), label, 128, CipherAES)
var key *SecretKey
key, err = ctx.GenerateSecretKeyWithLabel(randomBytes(), label, 128, CipherAES)
require.NoError(t, err)
defer func(k *SecretKey) { _ = k.Delete() }(key)

Expand Down Expand Up @@ -73,21 +93,29 @@ func TestFindingKeysWithAttributes(t *testing.T) {

keys, err = ctx.FindKeysWithAttributes(attrs)
require.NoError(t, err)
require.Len(t, keys, 2)
require.Len(t, keys, 2 + len(preconditionKeys_CkaValueLen16))

attrs = NewAttributeSet()
err = attrs.Set(CkaValueLen, 32)
require.NoError(t, err)

keys, err = ctx.FindKeysWithAttributes(attrs)
require.NoError(t, err)
require.Len(t, keys, 1)
require.Len(t, keys, 1 + len(preconditionKeys_CkaValueLen32))
})
}

func TestFindingKeyPairsWithAttributes(t *testing.T) {
withContext(t, func(ctx *Context) {

// Compensate for unclean starting stage
var err error
preconditionAttrs_CkaType_CKK_RSA := NewAttributeSet()
err = preconditionAttrs_CkaType_CKK_RSA.Set(CkaKeyType, pkcs11.CKK_RSA)
var preconditionKeysRsa []Signer
preconditionKeysRsa, err = ctx.FindKeyPairsWithAttributes(preconditionAttrs_CkaType_CKK_RSA)
require.NoError(t, err)

// Note: we use common labels, not IDs in this test code. AWS CloudHSM
// does not accept two keys with the same ID.

Expand Down Expand Up @@ -121,30 +149,40 @@ func TestFindingKeyPairsWithAttributes(t *testing.T) {
_ = attrs.Set(CkaKeyType, pkcs11.CKK_RSA)
keys, err = ctx.FindKeyPairsWithAttributes(attrs)
require.NoError(t, err)
require.Len(t, keys, 3)
require.Len(t, keys, 3 + len(preconditionKeysRsa))
})
}

func TestFindingAllKeys(t *testing.T) {
withContext(t, func(ctx *Context) {

var preconditionKeys []*SecretKey
var err error
preconditionKeys, err = ctx.FindAllKeys()

for i := 0; i < 10; i++ {
id := randomBytes()
key, err := ctx.GenerateSecretKey(id, 128, CipherAES)
require.NoError(t, err)

defer func(k *SecretKey) { _ = k.Delete() }(key)
}

keys, err := ctx.FindAllKeys()
var keys []*SecretKey
keys, err = ctx.FindAllKeys()
require.NoError(t, err)
require.NotNil(t, keys)

require.Len(t, keys, 10)
require.Len(t, keys, 10 + len(preconditionKeys))
})
}

func TestFindingAllKeyPairs(t *testing.T) {
withContext(t, func(ctx *Context) {

var preconditionRsaKeys []Signer
var err error
preconditionRsaKeys, err = ctx.FindAllKeyPairs()

for i := 1; i <= 5; i++ {
id := randomBytes()
key, err := ctx.GenerateRSAKeyPair(id, rsaSize)
Expand All @@ -153,11 +191,12 @@ func TestFindingAllKeyPairs(t *testing.T) {
defer func(k Signer) { _ = k.Delete() }(key)
}

keys, err := ctx.FindAllKeyPairs()
var keys []Signer
keys, err = ctx.FindAllKeyPairs()
require.NoError(t, err)
require.NotNil(t, keys)

require.Len(t, keys, 5)
require.Len(t, keys, 5 + len(preconditionRsaKeys))
})
}

Expand Down