xref: /aosp_15_r20/external/tink/go/internal/testing/stubkeymanager/stubkeymanager_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17package stubkeymanager_test
18
19import (
20	"bytes"
21	"errors"
22	"testing"
23
24	"github.com/google/go-cmp/cmp"
25	"google.golang.org/protobuf/testing/protocmp"
26	"github.com/google/tink/go/internal/testing/stubkeymanager"
27	agpb "github.com/google/tink/go/proto/aes_gcm_go_proto"
28	tpb "github.com/google/tink/go/proto/tink_go_proto"
29)
30
31type fakePrimitive struct {
32	Name string
33}
34
35func TestStubKeyManager(t *testing.T) {
36	keyType := "some.key.type"
37	km := stubkeymanager.StubKeyManager{
38		URL:  keyType,
39		Key:  &agpb.AesGcmKey{Version: 1, KeyValue: []byte("key_value")},
40		Prim: &fakePrimitive{Name: "fake-primitive-name"},
41		KeyData: &tpb.KeyData{
42			TypeUrl:         keyType,
43			Value:           []byte("key_value"),
44			KeyMaterialType: tpb.KeyData_SYMMETRIC,
45		},
46	}
47	if !km.DoesSupport(keyType) {
48		t.Errorf("DoesSupport(%q) = false , want true", keyType)
49	}
50	if km.DoesSupport("some.other.key.type") {
51		t.Errorf("DoesSupport(%q) = true , want false", keyType)
52	}
53	if km.TypeURL() != km.URL {
54		t.Errorf("TypeURL() = %q, want %q", km.TypeURL(), keyType)
55	}
56	key, err := km.NewKey(nil)
57	if err != nil {
58		t.Errorf("NewKey() err = %v, want nil", err)
59	}
60	if !cmp.Equal(key, km.Key, protocmp.Transform()) {
61		t.Errorf("NewKey() = %v, want %v", key, km.Key)
62	}
63	keyData, err := km.NewKeyData(nil)
64	if err != nil {
65		t.Errorf("NewKeyData() err = %v, want nil", err)
66	}
67	if !cmp.Equal(keyData, km.KeyData, protocmp.Transform()) {
68		t.Errorf("NewKeyData() = %v, want %v", keyData, km.KeyData)
69	}
70	p, err := km.Primitive(nil)
71	if err != nil {
72		t.Errorf("Primitive() err = %v, want nil", err)
73	}
74	if !cmp.Equal(p, km.Prim) {
75		t.Errorf("Primitive() = %v, want %v", p, km.Prim)
76	}
77}
78
79func TestStubPrivateKeyManager(t *testing.T) {
80	km := stubkeymanager.StubPrivateKeyManager{
81		PubKeyData: &tpb.KeyData{
82			TypeUrl:         "some.key.type",
83			Value:           []byte("key_value"),
84			KeyMaterialType: tpb.KeyData_ASYMMETRIC_PUBLIC,
85		},
86	}
87	pubKeyData, err := km.PublicKeyData(nil)
88	if err != nil {
89		t.Errorf("PublicKeyData() err = %v, want nil", err)
90	}
91	if !cmp.Equal(pubKeyData, km.PubKeyData, protocmp.Transform()) {
92		t.Errorf("PublicKeyData() = %v, want %v", pubKeyData, km.PubKeyData)
93	}
94}
95
96func TestStubDerivableKeyManager(t *testing.T) {
97	km := stubkeymanager.StubDerivableKeyManager{
98		KeyMatType: tpb.KeyData_SYMMETRIC,
99		DerKey:     &agpb.AesGcmKey{Version: 0, KeyValue: []byte("derived_key_value")},
100		DerErr:     errors.New("hiya"),
101	}
102	if km.KeyMaterialType() != km.KeyMatType {
103		t.Errorf("KeyMaterialType() = %d, want %d", km.KeyMaterialType(), tpb.KeyData_SYMMETRIC)
104	}
105	derivedKey, err := km.DeriveKey([]byte{}, &bytes.Buffer{})
106	if !cmp.Equal(derivedKey, km.DerKey, protocmp.Transform()) {
107		t.Errorf("DeriveKey() = %v, want %v", derivedKey, km.DerKey)
108	}
109	if err != km.DerErr {
110		t.Errorf("DeriveKey() err = %v, want %v", err, km.DerErr)
111	}
112}
113