Skip to content

Commit

Permalink
Extract the StubConfig test class into a testutil. First with only Re…
Browse files Browse the repository at this point in the history
…gisterKeyManagerFunctionality.

PiperOrigin-RevId: 700663844
Change-Id: I43a30e44e2bc22808c704009b902dba17d43e497
  • Loading branch information
LizaTretyakova authored and copybara-github committed Nov 27, 2024
1 parent e1c134b commit 4690886
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 60 deletions.
22 changes: 7 additions & 15 deletions aead/aes_gcm_siv_key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -316,30 +317,21 @@ 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{})
if err != nil {
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)
}
}
22 changes: 7 additions & 15 deletions aead/aesctrhmac/key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -245,30 +246,21 @@ 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{})
if err != nil {
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)
}
}
22 changes: 7 additions & 15 deletions aead/chacha20poly1305/key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -208,30 +209,21 @@ 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{})
if err != nil {
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)
}
}
22 changes: 7 additions & 15 deletions aead/xchacha20poly1305/key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -339,30 +340,21 @@ 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{})
if err != nil {
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)
}
}
41 changes: 41 additions & 0 deletions internal/testing/stubconfig/stubconfig.go
Original file line number Diff line number Diff line change
@@ -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)}
}
55 changes: 55 additions & 0 deletions internal/testing/stubconfig/stubconfig_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 4690886

Please sign in to comment.