From 469088629e4149e420e665000253dc7be1646f2e Mon Sep 17 00:00:00 2001 From: Liza Tretyakova Date: Wed, 27 Nov 2024 05:22:34 -0800 Subject: [PATCH] Extract the StubConfig test class into a testutil. First with only RegisterKeyManagerFunctionality. PiperOrigin-RevId: 700663844 Change-Id: I43a30e44e2bc22808c704009b902dba17d43e497 --- aead/aes_gcm_siv_key_manager_test.go | 22 +++----- aead/aesctrhmac/key_manager_test.go | 22 +++----- aead/chacha20poly1305/key_manager_test.go | 22 +++----- aead/xchacha20poly1305/key_manager_test.go | 22 +++----- internal/testing/stubconfig/stubconfig.go | 41 ++++++++++++++ .../testing/stubconfig/stubconfig_test.go | 55 +++++++++++++++++++ 6 files changed, 124 insertions(+), 60 deletions(-) create mode 100644 internal/testing/stubconfig/stubconfig.go create mode 100644 internal/testing/stubconfig/stubconfig_test.go diff --git a/aead/aes_gcm_siv_key_manager_test.go b/aead/aes_gcm_siv_key_manager_test.go index 228dca3..3bb74de 100644 --- a/aead/aes_gcm_siv_key_manager_test.go +++ b/aead/aes_gcm_siv_key_manager_test.go @@ -24,6 +24,7 @@ import ( "github.com/tink-crypto/tink-go/v2/aead/subtle" "github.com/tink-crypto/tink-go/v2/core/registry" "github.com/tink-crypto/tink-go/v2/internal/internalapi" + "github.com/tink-crypto/tink-go/v2/internal/testing/stubconfig" "github.com/tink-crypto/tink-go/v2/subtle/random" "github.com/tink-crypto/tink-go/v2/testutil" gcmsivpb "github.com/tink-crypto/tink-go/v2/proto/aes_gcm_siv_go_proto" @@ -316,19 +317,10 @@ func validateAESGCMSIVPrimitive(p any, key *gcmsivpb.AesGcmSivKey) error { return nil } -type stubAESGCMSIVConfig struct { - keyManagers map[string]registry.KeyManager -} - -func (sc *stubAESGCMSIVConfig) RegisterKeyManager(keyTypeURL string, km registry.KeyManager, _ internalapi.Token) error { - sc.keyManagers[keyTypeURL] = km - return nil -} - func TestRegisterKeyManager(t *testing.T) { - sc := &stubAESGCMSIVConfig{make(map[string]registry.KeyManager)} - if len(sc.keyManagers) != 0 { - t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.keyManagers)) + sc := stubconfig.NewStubConfig() + if len(sc.KeyManagers) != 0 { + t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.KeyManagers)) } err := aead.RegisterKeyManager(sc, internalapi.Token{}) @@ -336,10 +328,10 @@ func TestRegisterKeyManager(t *testing.T) { t.Fatalf("RegisterKeyManager() err = %v, want nil", err) } - if len(sc.keyManagers) != 1 { - t.Errorf("Number of registered key types = %d, want 1", len(sc.keyManagers)) + if len(sc.KeyManagers) != 1 { + t.Errorf("Number of registered key types = %d, want 1", len(sc.KeyManagers)) } - if _, ok := sc.keyManagers[testutil.AESGCMSIVTypeURL]; !ok { + if _, ok := sc.KeyManagers[testutil.AESGCMSIVTypeURL]; !ok { t.Errorf("RegisterKeyManager() registered wrong type URL, want %q", testutil.AESGCMSIVTypeURL) } } diff --git a/aead/aesctrhmac/key_manager_test.go b/aead/aesctrhmac/key_manager_test.go index b0efc52..5d126c6 100644 --- a/aead/aesctrhmac/key_manager_test.go +++ b/aead/aesctrhmac/key_manager_test.go @@ -22,6 +22,7 @@ import ( "github.com/tink-crypto/tink-go/v2/aead/aesctrhmac" "github.com/tink-crypto/tink-go/v2/core/registry" "github.com/tink-crypto/tink-go/v2/internal/internalapi" + "github.com/tink-crypto/tink-go/v2/internal/testing/stubconfig" "github.com/tink-crypto/tink-go/v2/testutil" ctrpb "github.com/tink-crypto/tink-go/v2/proto/aes_ctr_go_proto" achpb "github.com/tink-crypto/tink-go/v2/proto/aes_ctr_hmac_aead_go_proto" @@ -245,19 +246,10 @@ func TestKeyManagerPrimitiveWithInvalidKey(t *testing.T) { } } -type stubConfig struct { - keyManagers map[string]registry.KeyManager -} - -func (sc *stubConfig) RegisterKeyManager(keyTypeURL string, km registry.KeyManager, _ internalapi.Token) error { - sc.keyManagers[keyTypeURL] = km - return nil -} - func TestRegisterKeyManager(t *testing.T) { - sc := &stubConfig{make(map[string]registry.KeyManager)} - if len(sc.keyManagers) != 0 { - t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.keyManagers)) + sc := stubconfig.NewStubConfig() + if len(sc.KeyManagers) != 0 { + t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.KeyManagers)) } err := aesctrhmac.RegisterKeyManager(sc, internalapi.Token{}) @@ -265,10 +257,10 @@ func TestRegisterKeyManager(t *testing.T) { t.Fatalf("RegisterKeyManager() err = %v, want nil", err) } - if len(sc.keyManagers) != 1 { - t.Errorf("Number of registered key types = %d, want 1", len(sc.keyManagers)) + if len(sc.KeyManagers) != 1 { + t.Errorf("Number of registered key types = %d, want 1", len(sc.KeyManagers)) } - if _, ok := sc.keyManagers[testutil.AESCTRHMACAEADTypeURL]; !ok { + if _, ok := sc.KeyManagers[testutil.AESCTRHMACAEADTypeURL]; !ok { t.Errorf("RegisterKeyManager() registered wrong type URL, want \"%v\"", testutil.AESCTRHMACAEADTypeURL) } } diff --git a/aead/chacha20poly1305/key_manager_test.go b/aead/chacha20poly1305/key_manager_test.go index 3ff47e7..13ac139 100644 --- a/aead/chacha20poly1305/key_manager_test.go +++ b/aead/chacha20poly1305/key_manager_test.go @@ -23,6 +23,7 @@ import ( "google.golang.org/protobuf/proto" "github.com/tink-crypto/tink-go/v2/core/registry" "github.com/tink-crypto/tink-go/v2/internal/internalapi" + "github.com/tink-crypto/tink-go/v2/internal/testing/stubconfig" "github.com/tink-crypto/tink-go/v2/subtle/random" "github.com/tink-crypto/tink-go/v2/testutil" @@ -208,19 +209,10 @@ func validateChaCha20Poly1305Key(key *cppb.ChaCha20Poly1305Key) error { return validateChaCha20Poly1305Primitive(p, key) } -type stubConfig struct { - keyManagers map[string]registry.KeyManager -} - -func (sc *stubConfig) RegisterKeyManager(keyTypeURL string, km registry.KeyManager, _ internalapi.Token) error { - sc.keyManagers[keyTypeURL] = km - return nil -} - func TestRegisterKeyManager(t *testing.T) { - sc := &stubConfig{make(map[string]registry.KeyManager)} - if len(sc.keyManagers) != 0 { - t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.keyManagers)) + sc := stubconfig.NewStubConfig() + if len(sc.KeyManagers) != 0 { + t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.KeyManagers)) } err := tinkchacha20poly1305.RegisterKeyManager(sc, internalapi.Token{}) @@ -228,10 +220,10 @@ func TestRegisterKeyManager(t *testing.T) { t.Fatalf("RegisterKeyManager() err = %v, want nil", err) } - if len(sc.keyManagers) != 1 { - t.Errorf("Number of registered key types = %d, want 1", len(sc.keyManagers)) + if len(sc.KeyManagers) != 1 { + t.Errorf("Number of registered key types = %d, want 1", len(sc.KeyManagers)) } - if _, ok := sc.keyManagers[testutil.ChaCha20Poly1305TypeURL]; !ok { + if _, ok := sc.KeyManagers[testutil.ChaCha20Poly1305TypeURL]; !ok { t.Errorf("RegisterKeyManager() registered wrong type URL, want %q", testutil.ChaCha20Poly1305TypeURL) } } diff --git a/aead/xchacha20poly1305/key_manager_test.go b/aead/xchacha20poly1305/key_manager_test.go index a076a93..e0acfe0 100644 --- a/aead/xchacha20poly1305/key_manager_test.go +++ b/aead/xchacha20poly1305/key_manager_test.go @@ -26,6 +26,7 @@ import ( "github.com/tink-crypto/tink-go/v2/core/registry" "github.com/tink-crypto/tink-go/v2/internal/internalapi" "github.com/tink-crypto/tink-go/v2/internal/internalregistry" + "github.com/tink-crypto/tink-go/v2/internal/testing/stubconfig" "github.com/tink-crypto/tink-go/v2/subtle/random" "github.com/tink-crypto/tink-go/v2/testutil" @@ -339,19 +340,10 @@ func validateXChaCha20Poly1305Key(key *xpb.XChaCha20Poly1305Key) error { return validateXChaCha20Poly1305Primitive(p, key) } -type stubConfig struct { - keyManagers map[string]registry.KeyManager -} - -func (sc *stubConfig) RegisterKeyManager(keyTypeURL string, km registry.KeyManager, _ internalapi.Token) error { - sc.keyManagers[keyTypeURL] = km - return nil -} - func TestRegisterKeyManager(t *testing.T) { - sc := &stubConfig{make(map[string]registry.KeyManager)} - if len(sc.keyManagers) != 0 { - t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.keyManagers)) + sc := stubconfig.NewStubConfig() + if len(sc.KeyManagers) != 0 { + t.Fatalf("Initial number of registered key types = %d, want 0", len(sc.KeyManagers)) } err := xchacha20poly1305.RegisterKeyManager(sc, internalapi.Token{}) @@ -359,10 +351,10 @@ func TestRegisterKeyManager(t *testing.T) { t.Fatalf("RegisterKeyManager() err = %v, want nil", err) } - if len(sc.keyManagers) != 1 { - t.Errorf("Number of registered key types = %d, want 1", len(sc.keyManagers)) + if len(sc.KeyManagers) != 1 { + t.Errorf("Number of registered key types = %d, want 1", len(sc.KeyManagers)) } - if _, ok := sc.keyManagers[testutil.XChaCha20Poly1305TypeURL]; !ok { + if _, ok := sc.KeyManagers[testutil.XChaCha20Poly1305TypeURL]; !ok { t.Errorf("RegisterKeyManager() registered wrong type URL, want %q", testutil.XChaCha20Poly1305TypeURL) } } diff --git a/internal/testing/stubconfig/stubconfig.go b/internal/testing/stubconfig/stubconfig.go new file mode 100644 index 0000000..e14b8d2 --- /dev/null +++ b/internal/testing/stubconfig/stubconfig.go @@ -0,0 +1,41 @@ +// Copyright 2024 Google LLC +// +// 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 stubconfig provides test utilities that are *NOT* meant for public use. +package stubconfig + +import ( + "github.com/tink-crypto/tink-go/v2/core/registry" + "github.com/tink-crypto/tink-go/v2/internal/internalapi" +) + +// StubConfig simulates the KeyManager registration behaviour of the real Config +// in order to test that the corresponding registration functions in the +// KeyManagers (which cannot directly depend on the Config to avoid circular +// dependency). +type StubConfig struct { + KeyManagers map[string]registry.KeyManager +} + +// RegisterKeyManager is the method responsible for the KeyManager registration +// in the Config interface. +func (sc *StubConfig) RegisterKeyManager(keyTypeURL string, km registry.KeyManager, _ internalapi.Token) error { + sc.KeyManagers[keyTypeURL] = km + return nil +} + +// NewStubConfig returns an empty instance of a StubConfig. +func NewStubConfig() *StubConfig { + return &StubConfig{make(map[string]registry.KeyManager)} +} diff --git a/internal/testing/stubconfig/stubconfig_test.go b/internal/testing/stubconfig/stubconfig_test.go new file mode 100644 index 0000000..1bc725c --- /dev/null +++ b/internal/testing/stubconfig/stubconfig_test.go @@ -0,0 +1,55 @@ +// Copyright 2024 Google LLC +// +// 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 stubconfig_test + +import ( + "testing" + + "google.golang.org/protobuf/proto" + + "github.com/tink-crypto/tink-go/v2/internal/internalapi" + "github.com/tink-crypto/tink-go/v2/internal/testing/stubconfig" + tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto" +) + +type stubKeyManager struct{} + +func (s stubKeyManager) Primitive(_ []byte) (any, error) { panic("not needed in test") } +func (s stubKeyManager) NewKey(_ []byte) (proto.Message, error) { panic("not needed in test") } +func (s stubKeyManager) DoesSupport(_ string) bool { panic("not needed in test") } +func (s stubKeyManager) TypeURL() string { panic("not needed in test") } +func (s stubKeyManager) NewKeyData(_ []byte) (*tinkpb.KeyData, error) { panic("not needed in test") } + +func TestStubConfig(t *testing.T) { + c := stubconfig.NewStubConfig() + if c == nil { + t.Fatalf("stubconfig.NewStubConfig() = nil, want not nil") + } + + l := len(c.KeyManagers) + if l != 0 { + t.Fatalf("Initial number of registered key types = %d, want 0", l) + } + + err := c.RegisterKeyManager("", stubKeyManager{}, internalapi.Token{}) + if err != nil { + t.Fatalf("StubConfig.RegisterKeyManager(): err = %v, want nil", err) + } + + l = len(c.KeyManagers) + if l != 1 { + t.Fatalf("Number of registered key types = %d, want 1", l) + } +}