Add some extra coverage.
This commit is contained in:
parent
52a18e0c2d
commit
2026787ce4
2 changed files with 64 additions and 11 deletions
|
@ -38,9 +38,8 @@ var now = func() time.Time {
|
||||||
}
|
}
|
||||||
|
|
||||||
type keyType struct {
|
type keyType struct {
|
||||||
Kty keyvault.JSONWebKeyType
|
Kty keyvault.JSONWebKeyType
|
||||||
Curve keyvault.JSONWebKeyCurveName
|
Curve keyvault.JSONWebKeyCurveName
|
||||||
KeySize int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k keyType) KeyType(pl apiv1.ProtectionLevel) keyvault.JSONWebKeyType {
|
func (k keyType) KeyType(pl apiv1.ProtectionLevel) keyvault.JSONWebKeyType {
|
||||||
|
|
|
@ -42,17 +42,19 @@ func mockClient(t *testing.T) *mock.KeyVaultClient {
|
||||||
return mock.NewKeyVaultClient(ctrl)
|
return mock.NewKeyVaultClient(ctrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func mockCreateClient(t *testing.T, ctrl *gomock.Controller) {
|
func mockCreateClient(t *testing.T) *mock.KeyVaultClient {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
client := mock.NewKeyVaultClient(ctrl)
|
||||||
old := createClient
|
old := createClient
|
||||||
|
|
||||||
createClient = func(ctx context.Context, opts apiv1.Options) (KeyVaultClient, error) {
|
createClient = func(ctx context.Context, opts apiv1.Options) (KeyVaultClient, error) {
|
||||||
return mock.NewKeyVaultClient(ctrl), nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
createClient = old
|
createClient = old
|
||||||
|
ctrl.Finish()
|
||||||
})
|
})
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func createJWK(t *testing.T, pub crypto.PublicKey) *keyvault.JSONWebKey {
|
func createJWK(t *testing.T, pub crypto.PublicKey) *keyvault.JSONWebKey {
|
||||||
|
@ -78,8 +80,11 @@ func Test_now(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
client := mockClient(t)
|
||||||
mockCreateClient(t, ctrl)
|
old := createClient
|
||||||
|
t.Cleanup(func() {
|
||||||
|
createClient = old
|
||||||
|
})
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
@ -87,16 +92,27 @@ func TestNew(t *testing.T) {
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
setup func()
|
||||||
args args
|
args args
|
||||||
want *KeyVault
|
want *KeyVault
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"ok", args{context.Background(), apiv1.Options{}}, &KeyVault{
|
{"ok", func() {
|
||||||
baseClient: mock.NewKeyVaultClient(ctrl),
|
createClient = func(ctx context.Context, opts apiv1.Options) (KeyVaultClient, error) {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
}, args{context.Background(), apiv1.Options{}}, &KeyVault{
|
||||||
|
baseClient: client,
|
||||||
}, false},
|
}, false},
|
||||||
|
{"fail", func() {
|
||||||
|
createClient = func(ctx context.Context, opts apiv1.Options) (KeyVaultClient, error) {
|
||||||
|
return nil, errTest
|
||||||
|
}
|
||||||
|
}, args{context.Background(), apiv1.Options{}}, nil, true},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
tt.setup()
|
||||||
got, err := New(tt.args.ctx, tt.args.opts)
|
got, err := New(tt.args.ctx, tt.args.opts)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
@ -148,6 +164,9 @@ func TestKeyVault_GetPublicKey(t *testing.T) {
|
||||||
{"fail GetKey", fields{client}, args{&apiv1.GetPublicKeyRequest{
|
{"fail GetKey", fields{client}, args{&apiv1.GetPublicKeyRequest{
|
||||||
Name: "azurekms:vault=my-vault;name=not-found?version=my-version",
|
Name: "azurekms:vault=my-vault;name=not-found?version=my-version",
|
||||||
}}, nil, true},
|
}}, nil, true},
|
||||||
|
{"fail empty", fields{client}, args{&apiv1.GetPublicKeyRequest{
|
||||||
|
Name: "",
|
||||||
|
}}, nil, true},
|
||||||
{"fail vault", fields{client}, args{&apiv1.GetPublicKeyRequest{
|
{"fail vault", fields{client}, args{&apiv1.GetPublicKeyRequest{
|
||||||
Name: "azurekms:vault=;name=not-found?version=my-version",
|
Name: "azurekms:vault=;name=not-found?version=my-version",
|
||||||
}}, nil, true},
|
}}, nil, true},
|
||||||
|
@ -490,3 +509,38 @@ func TestKeyVault_Close(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_keyType_KeyType(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
Kty keyvault.JSONWebKeyType
|
||||||
|
Curve keyvault.JSONWebKeyCurveName
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
pl apiv1.ProtectionLevel
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
want keyvault.JSONWebKeyType
|
||||||
|
}{
|
||||||
|
{"ec", fields{keyvault.EC, keyvault.P256}, args{apiv1.UnspecifiedProtectionLevel}, keyvault.EC},
|
||||||
|
{"ec software", fields{keyvault.EC, keyvault.P384}, args{apiv1.Software}, keyvault.EC},
|
||||||
|
{"ec hsm", fields{keyvault.EC, keyvault.P521}, args{apiv1.HSM}, keyvault.ECHSM},
|
||||||
|
{"rsa", fields{keyvault.RSA, keyvault.P256}, args{apiv1.UnspecifiedProtectionLevel}, keyvault.RSA},
|
||||||
|
{"rsa software", fields{keyvault.RSA, ""}, args{apiv1.Software}, keyvault.RSA},
|
||||||
|
{"rsa hsm", fields{keyvault.RSA, ""}, args{apiv1.HSM}, keyvault.RSAHSM},
|
||||||
|
{"empty", fields{"FOO", ""}, args{apiv1.UnspecifiedProtectionLevel}, ""},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
k := keyType{
|
||||||
|
Kty: tt.fields.Kty,
|
||||||
|
Curve: tt.fields.Curve,
|
||||||
|
}
|
||||||
|
if got := k.KeyType(tt.args.pl); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("keyType.KeyType() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue