File: primitiveregistry_test.go

package info (click to toggle)
golang-github-tink-crypto-tink-go 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 14,996 kB
  • sloc: sh: 876; makefile: 6
file content (174 lines) | stat: -rw-r--r-- 5,129 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2025 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 primitiveregistry_test

import (
	"fmt"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/tink-crypto/tink-go/v2/internal/primitiveregistry"
	"github.com/tink-crypto/tink-go/v2/key"
)

type testParameters struct {
	key.Parameters
	hasIDRequirement bool
}

func (p *testParameters) HasIDRequirement() bool {
	return p.hasIDRequirement
}

func (p *testParameters) Equal(other key.Parameters) bool {
	otherP, ok := other.(*testParameters)
	if !ok {
		return false
	}
	return p.hasIDRequirement == otherP.hasIDRequirement
}

type testKey struct {
	key.Key
	params *testParameters
	id     uint32
}

func (k *testKey) Parameters() key.Parameters {
	return k.params
}

func (k *testKey) IDRequirement() (uint32, bool) {
	if k.params.HasIDRequirement() {
		return k.id, true
	}
	return 0, false
}

func (k *testKey) Equal(other key.Key) bool {
	otherK, ok := other.(*testKey)
	if !ok {
		return false
	}
	return k.id == otherK.id && k.params.Equal(otherK.params)
}

type testPrimitive struct {
	ID uint32
}

type anotherTestKey struct {
	key.Key
}

func (k *anotherTestKey) Parameters() key.Parameters {
	return nil
}

func (k *anotherTestKey) IDRequirement() (uint32, bool) {
	return 0, false
}

func (k *anotherTestKey) Equal(other key.Key) bool {
	_, ok := other.(*anotherTestKey)
	return ok
}

func testPrimitiveConstructor(k key.Key) (any, error) {
	testK, ok := k.(*testKey)
	if !ok {
		return nil, fmt.Errorf("unexpected key type: %T", k)
	}
	id, _ := testK.IDRequirement()
	return &testPrimitive{ID: id}, nil
}

func TestRegisterPrimitiveConstructorAndPrimitive_Success(t *testing.T) {
	if err := primitiveregistry.RegisterPrimitiveConstructor[*testKey](testPrimitiveConstructor); err != nil {
		t.Fatalf("primitiveregistry.RegisterPrimitiveConstructor() err = %v, want nil", err)
	}
	defer primitiveregistry.UnregisterPrimitiveConstructor[*testKey]()
	k := &testKey{params: &testParameters{hasIDRequirement: true}, id: 123}
	p, err := primitiveregistry.Primitive(k)
	if err != nil {
		t.Fatalf("primitiveregistry.Primitive() err = %v, want nil", err)
	}
	testP, ok := p.(*testPrimitive)
	if !ok {
		t.Fatalf("primitive is not of type *testPrimitive")
	}
	if !cmp.Equal(testP, &testPrimitive{ID: 123}) {
		t.Errorf("primitiveregistry.Primitive() = %v, want %v", testP, &testPrimitive{ID: 123})
	}
}

func TestRegisterPrimitiveConstructor_SameConstructorTwiceIsNoOp(t *testing.T) {
	if err := primitiveregistry.RegisterPrimitiveConstructor[*testKey](testPrimitiveConstructor); err != nil {
		t.Fatalf("primitiveregistry.RegisterPrimitiveConstructor() err = %v, want nil", err)
	}
	defer primitiveregistry.UnregisterPrimitiveConstructor[*testKey]()
	if err := primitiveregistry.RegisterPrimitiveConstructor[*testKey](testPrimitiveConstructor); err != nil {
		t.Fatalf("primitiveregistry.RegisterPrimitiveConstructor() err = %v, want nil", err)
	}
}

func TestRegisterPrimitiveConstructor_FailsIfDifferentConstructorRegistered(t *testing.T) {
	if err := primitiveregistry.RegisterPrimitiveConstructor[*testKey](testPrimitiveConstructor); err != nil {
		t.Fatalf("primitiveregistry.RegisterPrimitiveConstructor() err = %v, want nil", err)
	}
	defer primitiveregistry.UnregisterPrimitiveConstructor[*testKey]()
	anotherConstructor := func(k key.Key) (any, error) {
		return nil, nil
	}
	if err := primitiveregistry.RegisterPrimitiveConstructor[*testKey](anotherConstructor); err == nil {
		t.Fatalf("primitiveregistry.RegisterPrimitiveConstructor() err = nil, want error")
	}
}

func TestPrimitive_Fails(t *testing.T) {
	for _, tc := range []struct {
		name string
		key  key.Key
	}{
		{
			name: "anotherTestKey",
			key:  &anotherTestKey{},
		},
		{
			name: "nilKey",
			key:  nil,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			if _, err := primitiveregistry.Primitive(tc.key); err == nil {
				t.Fatalf("primitiveregistry.Primitive() err = nil, want error")
			}
		})
	}
}

func TestUnregisterRemovesConstructor(t *testing.T) {
	if err := primitiveregistry.RegisterPrimitiveConstructor[*testKey](testPrimitiveConstructor); err != nil {
		t.Fatalf("primitiveregistry.RegisterPrimitiveConstructor() err = %v, want nil", err)
	}
	k := &testKey{params: &testParameters{hasIDRequirement: true}, id: 123}
	if _, err := primitiveregistry.Primitive(k); err != nil {
		t.Fatalf("primitiveregistry.Primitive() err = %v, want nil", err)
	}
	primitiveregistry.UnregisterPrimitiveConstructor[*testKey]()
	if _, err := primitiveregistry.Primitive(k); err == nil {
		t.Fatalf("primitiveregistry.Primitive() err = nil, want error")
	}
}