Permalink
Browse files

Golang: move registry initialization to init.

Ref: #93.
PiperOrigin-RevId: 215967965
GitOrigin-RevId: 4eeba51b1ae8d4b71e0512c385d57f2e317479ba
  • Loading branch information...
thaidn authored and Tink Team committed Oct 5, 2018
1 parent 0e5faf5 commit e941aa57e5ecb0d8c13fcf7c9150b8609391ab25
View
@@ -7,11 +7,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"aead_config.go",
"aead_factory.go",
"aead_key_templates.go",
"aes_gcm_key_manager.go",
"chacha20poly1305_key_manager.go",
"init.go",
"proto_util.go",
"xchacha20poly1305_key_manager.go",
],
@@ -33,7 +33,6 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"aead_config_test.go",
"aead_factory_test.go",
"aead_key_templates_test.go",
"aes_gcm_key_manager_test.go",
@@ -29,15 +29,7 @@ import (
tinkpb "github.com/google/tink/proto/tink_go_proto"
)
func setupFactoryTest() {
if err := aead.Register(); err != nil {
panic(fmt.Sprintf("cannot register AEAD key types: %s", err))
}
}
func TestFactoryMultipleKeys(t *testing.T) {
setupFactoryTest()
// encrypt with non-raw key
keyset := testutil.NewTestAESGCMKeyset(tinkpb.OutputPrefixType_TINK)
primaryKey := keyset.Key[0]
@@ -85,7 +77,6 @@ func TestFactoryMultipleKeys(t *testing.T) {
}
func TestFactoryRawKeyAsPrimary(t *testing.T) {
setupFactoryTest()
keyset := testutil.NewTestAESGCMKeyset(tinkpb.OutputPrefixType_RAW)
if keyset.Key[0].OutputPrefixType != tinkpb.OutputPrefixType_RAW {
t.Errorf("primary key is not a raw key")
@@ -16,18 +16,21 @@
package aead
import (
"fmt"
"github.com/google/tink/go/tink"
)
// Register registers latest AEAD key types and their managers with the Registry.
func Register() error {
func init() {
if err := tink.RegisterKeyManager(NewAESGCMKeyManager()); err != nil {
return err
panic(fmt.Sprintf("aead.init() failed: %v", err))
}
if err := tink.RegisterKeyManager(NewChaCha20Poly1305KeyManager()); err != nil {
return err
panic(fmt.Sprintf("aead.init() failed: %v", err))
}
return tink.RegisterKeyManager(NewXChaCha20Poly1305KeyManager())
if err := tink.RegisterKeyManager(NewXChaCha20Poly1305KeyManager()); err != nil {
panic(fmt.Sprintf("aead.init() failed: %v", err))
}
}
@@ -21,11 +21,7 @@ import (
"github.com/google/tink/go/tink"
)
func TestConfigRegistration(t *testing.T) {
err := aead.Register()
if err != nil {
t.Errorf("cannot register standard key types")
}
func TestAeadInit(t *testing.T) {
// Check for AES-GCM key manager.
keyManager, err := tink.GetKeyManager(aead.AESGCMTypeURL)
if err != nil {
@@ -15,24 +15,15 @@
package insecure_test
import (
"fmt"
"testing"
"github.com/golang/protobuf/proto"
"github.com/google/tink/go/insecure"
"github.com/google/tink/go/mac"
"github.com/google/tink/go/testutil"
"github.com/google/tink/go/tink"
tinkpb "github.com/google/tink/proto/tink_go_proto"
)
func setUp() {
if err := mac.Register(); err != nil {
panic(fmt.Sprintf("cannot register MAC key types: %s", err))
}
}
func TestFromKeyset(t *testing.T) {
keyData := tink.CreateKeyData("some type url", []byte{0}, tinkpb.KeyData_SYMMETRIC)
key := tink.CreateKey(keyData, tinkpb.KeyStatusType_ENABLED, 1, tinkpb.OutputPrefixType_TINK)
@@ -59,8 +50,6 @@ func TestFromKeysetWithInvalidInput(t *testing.T) {
}
func TestKeysetHandleFromSerializedProto(t *testing.T) {
setUp()
// Create a keyset that contains a single HmacKey.
manager := testutil.NewHMACKeysetManager()
handle, err := manager.KeysetHandle()
@@ -90,8 +79,6 @@ func TestKeysetHandleFromSerializedProto(t *testing.T) {
}
func TestKeysetHandleFromSerializedProtoWithInvalidInput(t *testing.T) {
setUp()
manager := testutil.NewHMACKeysetManager()
handle, err := manager.KeysetHandle()
if handle == nil || err != nil {
View
@@ -8,7 +8,7 @@ go_library(
name = "go_default_library",
srcs = [
"hmac_key_manager.go",
"mac_config.go",
"init.go",
"mac_factory.go",
"mac_key_templates.go",
"proto_util.go",
@@ -30,7 +30,7 @@ go_test(
name = "go_default_test",
srcs = [
"hmac_key_manager_test.go",
"mac_config_test.go",
"init_test.go",
"mac_factory_test.go",
"mac_key_templates_test.go",
],
@@ -16,10 +16,13 @@
package mac
import (
"fmt"
"github.com/google/tink/go/tink"
)
// Register registers latest MAC key types and their managers with the Registry.
func Register() error {
return tink.RegisterKeyManager(NewHMACKeyManager())
func init() {
if err := tink.RegisterKeyManager(NewHMACKeyManager()); err != nil {
panic(fmt.Sprintf("mac.init() failed: %v", err))
}
}
@@ -21,11 +21,7 @@ import (
"github.com/google/tink/go/tink"
)
func TestRegistration(t *testing.T) {
err := mac.Register()
if err != nil {
t.Errorf("cannot register standard key types")
}
func TestMacInit(t *testing.T) {
keyManager, err := tink.GetKeyManager(mac.HMACTypeURL)
if err != nil {
t.Errorf("unexpected error: %s", err)
View
@@ -9,10 +9,10 @@ go_library(
srcs = [
"ecdsa_signer_key_manager.go",
"ecdsa_verifier_key_manager.go",
"init.go",
"proto_util.go",
"signer_factory.go",
"verifier_factory.go",
"signature_config.go",
"signature_key_templates.go",
],
importpath = "github.com/google/tink/go/signature",
@@ -33,7 +33,7 @@ go_test(
srcs = [
"ecdsa_signer_key_manager_test.go",
"ecdsa_verifier_key_manager_test.go",
"signature_config_test.go",
"init_test.go",
"signature_factory_test.go",
"signature_key_templates_test.go",
],
@@ -16,14 +16,16 @@
package signature
import (
"fmt"
"github.com/google/tink/go/tink"
)
// Register registers latest signature key types and their managers with the Registry.
func Register() error {
err := tink.RegisterKeyManager(NewECDSASignerKeyManager())
if err != nil {
return err
func init() {
if err := tink.RegisterKeyManager(NewECDSASignerKeyManager()); err != nil {
panic(fmt.Sprintf("signature.init() failed: %v", err))
}
return tink.RegisterKeyManager(NewECDSAVerifierKeyManager())
if err := tink.RegisterKeyManager(NewECDSAVerifierKeyManager()); err != nil {
panic(fmt.Sprintf("signature.init() failed: %v", err))
}
}
@@ -21,10 +21,7 @@ import (
"github.com/google/tink/go/tink"
)
func TestSignatureConfigRegistration(t *testing.T) {
if err := signature.Register(); err != nil {
t.Errorf("cannot register standard key types")
}
func TestSignatureInit(t *testing.T) {
// check for ECDSASignerKeyManager
keyManager, err := tink.GetKeyManager(signature.ECDSASignerTypeURL)
if err != nil {
@@ -15,7 +15,6 @@
package signature_test
import (
"fmt"
"testing"
"github.com/golang/protobuf/proto"
@@ -29,10 +28,6 @@ import (
)
func TestSignerVerifyFactory(t *testing.T) {
if err := signature.Register(); err != nil {
panic(fmt.Sprintf("cannot register signature key types: %s", err))
}
tinkPriv, tinkPub := newECDSAKeysetKeypair(commonpb.HashType_SHA512,
commonpb.EllipticCurveType_NIST_P521,
tinkpb.OutputPrefixType_TINK,
@@ -15,7 +15,6 @@
package tink_test
import (
"fmt"
"testing"
"github.com/google/tink/go/mac"
@@ -24,15 +23,7 @@ import (
tinkpb "github.com/google/tink/proto/tink_go_proto"
)
func setUpKeysetHandleTest() {
if err := mac.Register(); err != nil {
panic(fmt.Sprintf("cannot register MAC key types: %v", err))
}
}
func TestNewKeysetHandle(t *testing.T) {
setUpKeysetHandleTest()
kt := mac.HMACSHA256Tag128KeyTemplate()
kh, err := tink.NewKeysetHandle(kt)
if err != nil {
@@ -55,8 +46,6 @@ func TestNewKeysetHandle(t *testing.T) {
}
func TestNewKeysetHandleWithInvalidInput(t *testing.T) {
setUpKeysetHandleTest()
// template unregistered TypeUrl
template := mac.HMACSHA256Tag128KeyTemplate()
template.TypeUrl = "some unknown TypeUrl"
@@ -15,28 +15,14 @@
package tink_test
import (
"fmt"
"testing"
"github.com/google/tink/go/aead"
"github.com/google/tink/go/mac"
"github.com/google/tink/go/tink"
tinkpb "github.com/google/tink/proto/tink_go_proto"
)
func setUpKeysetManagerTest() {
if err := mac.Register(); err != nil {
panic(fmt.Sprintf("cannot register MAC key types: %v", err))
}
if err := aead.Register(); err != nil {
panic(fmt.Sprintf("cannot register AEAD key types: %v", err))
}
}
func TestKeysetManagerBasic(t *testing.T) {
setUpKeysetManagerTest()
// Create a keyset that contains a single HmacKey.
ksm := tink.NewKeysetManager()
kt := mac.HMACSHA256Tag128KeyTemplate()
View
@@ -15,7 +15,6 @@
package tink_test
import (
"fmt"
"testing"
"github.com/golang/protobuf/proto"
@@ -32,16 +31,6 @@ import (
tinkpb "github.com/google/tink/proto/tink_go_proto"
)
func setupRegistryTests() {
if err := mac.Register(); err != nil {
panic(fmt.Sprintf("cannot register MAC key types: %v", err))
}
if err := aead.Register(); err != nil {
panic(fmt.Sprintf("cannot register AEAD key types: %v", err))
}
}
func TestRegistryBasic(t *testing.T) {
// try to put a HMACKeyManager
hmacManager := mac.NewHMACKeyManager()
@@ -62,7 +51,6 @@ func TestRegisterKeyManager(t *testing.T) {
var km tink.KeyManager
var err error
// register mac and aead types.
setupRegistryTests()
// get HMACKeyManager
km, err = tink.GetKeyManager(mac.HMACTypeURL)
if err != nil {
@@ -83,7 +71,6 @@ func TestRegisterKeyManager(t *testing.T) {
func TestRegisterKeyManagerWithCollision(t *testing.T) {
// register mac and aead types.
setupRegistryTests()
// dummyKeyManager's typeURL is equal to that of AESGCM
var dummyKeyManager tink.KeyManager = new(testutil.DummyAEADKeyManager)
// this should not overwrite the existing manager.
@@ -99,7 +86,6 @@ func TestRegisterKeyManagerWithCollision(t *testing.T) {
}
func TestNewKeyData(t *testing.T) {
setupRegistryTests()
// new Keydata from a Hmac KeyTemplate
keyData, err := tink.NewKeyData(mac.HMACSHA256Tag128KeyTemplate())
if err != nil {
@@ -124,7 +110,6 @@ func TestNewKeyData(t *testing.T) {
}
func TestNewKey(t *testing.T) {
setupRegistryTests()
// aead template
aesGcmTemplate := aead.AES128GCMKeyTemplate()
key, err := tink.NewKey(aesGcmTemplate)
@@ -151,7 +136,6 @@ func TestNewKey(t *testing.T) {
}
func TestPrimitiveFromKeyData(t *testing.T) {
setupRegistryTests()
// hmac keydata
keyData := testutil.NewHMACKeyData(commonpb.HashType_SHA256, 16)
p, err := tink.PrimitiveFromKeyData(keyData)
@@ -176,7 +160,6 @@ func TestPrimitiveFromKeyData(t *testing.T) {
}
func TestPrimitive(t *testing.T) {
setupRegistryTests()
// hmac key
key := testutil.NewHMACKey(commonpb.HashType_SHA256, 16)
serializedKey, _ := proto.Marshal(key)
@@ -206,7 +189,6 @@ func TestPrimitive(t *testing.T) {
}
func TestPrimitives(t *testing.T) {
setupRegistryTests()
// valid input
template1 := aead.AES128GCMKeyTemplate()
template2 := aead.AES256GCMKeyTemplate()

0 comments on commit e941aa5

Please sign in to comment.