xref: /aosp_15_r20/external/tink/go/internal/monitoringutil/monitoring_util_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 monitoringutil_test
18
19import (
20	"testing"
21
22	"github.com/google/go-cmp/cmp"
23	"github.com/google/tink/go/core/primitiveset"
24	"github.com/google/tink/go/internal/monitoringutil"
25	"github.com/google/tink/go/monitoring"
26	tpb "github.com/google/tink/go/proto/tink_go_proto"
27)
28
29func TestKeysetInfoFromPrimitiveSetWithNilPrimitiveSetFails(t *testing.T) {
30	if _, err := monitoringutil.KeysetInfoFromPrimitiveSet(nil); err == nil {
31		t.Errorf("KeysetInfoFromPrimitiveSet(nil) err = nil, want error")
32	}
33}
34
35func validPrimitiveSet() *primitiveset.PrimitiveSet {
36	return &primitiveset.PrimitiveSet{
37		Primary: &primitiveset.Entry{},
38		Entries: map[string][]*primitiveset.Entry{
39			"one": []*primitiveset.Entry{
40				{
41					Status:  tpb.KeyStatusType_ENABLED,
42					TypeURL: "type.googleapis.com/google.crypto.tink.AesGcmKey",
43				},
44			},
45		},
46	}
47}
48
49func TestBaselinePrimitiveSet(t *testing.T) {
50	if _, err := monitoringutil.KeysetInfoFromPrimitiveSet(validPrimitiveSet()); err != nil {
51		t.Errorf("KeysetInfoFromPrimitiveSet() err = %v, want nil", err)
52	}
53}
54
55func TestKeysetInfoFromPrimitiveSetWithNoEntryFails(t *testing.T) {
56	ps := validPrimitiveSet()
57	ps.Entries = nil
58	if _, err := monitoringutil.KeysetInfoFromPrimitiveSet(ps); err == nil {
59		t.Errorf("KeysetInfoFromPrimitiveSet() err = nil, want error")
60	}
61}
62
63func TestKeysetInfoFromPrimitiveSetWithNoPrimaryFails(t *testing.T) {
64	ps := validPrimitiveSet()
65	ps.Primary = nil
66	if _, err := monitoringutil.KeysetInfoFromPrimitiveSet(ps); err == nil {
67		t.Errorf("KeysetInfoFromPrimitiveSet() err = nil, want error")
68	}
69}
70
71func TestKeysetInfoFromPrimitiveSetWithInvalidKeyStatusFails(t *testing.T) {
72	ps := validPrimitiveSet()
73	ps.Entries["invalid_key_status"] = []*primitiveset.Entry{
74		{
75			KeyID:  123,
76			Status: tpb.KeyStatusType_UNKNOWN_STATUS,
77		},
78	}
79	if _, err := monitoringutil.KeysetInfoFromPrimitiveSet(ps); err == nil {
80		t.Errorf("KeysetInfoFromPrimitiveSet() err = nil, want error")
81	}
82}
83
84func TestKeysetInfoFromPrimitiveSet(t *testing.T) {
85	ps := &primitiveset.PrimitiveSet{
86		Primary: &primitiveset.Entry{
87			KeyID: 1,
88		},
89		Annotations: map[string]string{
90			"foo": "bar",
91			"zoo": "far",
92		},
93		Entries: map[string][]*primitiveset.Entry{
94			// Adding all entries under the same prefix to get deterministic output.
95			"one": []*primitiveset.Entry{
96				&primitiveset.Entry{
97					KeyID:      1,
98					Status:     tpb.KeyStatusType_ENABLED,
99					TypeURL:    "type.googleapis.com/google.crypto.tink.AesSivKey",
100					PrefixType: tpb.OutputPrefixType_TINK,
101				},
102				&primitiveset.Entry{
103					KeyID:      2,
104					Status:     tpb.KeyStatusType_DISABLED,
105					TypeURL:    "type.googleapis.com/google.crypto.tink.AesGcmKey",
106					PrefixType: tpb.OutputPrefixType_TINK,
107				},
108				&primitiveset.Entry{
109					KeyID:      3,
110					Status:     tpb.KeyStatusType_DESTROYED,
111					TypeURL:    "type.googleapis.com/google.crypto.tink.AesCtrHmacKey",
112					PrefixType: tpb.OutputPrefixType_TINK,
113				},
114			},
115		},
116	}
117	want := &monitoring.KeysetInfo{
118		PrimaryKeyID: 1,
119		Annotations: map[string]string{
120			"foo": "bar",
121			"zoo": "far",
122		},
123		Entries: []*monitoring.Entry{
124			{
125				KeyID:     1,
126				Status:    monitoring.Enabled,
127				KeyType:   "tink.AesSivKey",
128				KeyPrefix: "TINK",
129			},
130			{
131				KeyID:     2,
132				Status:    monitoring.Disabled,
133				KeyType:   "tink.AesGcmKey",
134				KeyPrefix: "TINK",
135			},
136			{
137				KeyID:     3,
138				Status:    monitoring.Destroyed,
139				KeyType:   "tink.AesCtrHmacKey",
140				KeyPrefix: "TINK",
141			},
142		},
143	}
144	got, err := monitoringutil.KeysetInfoFromPrimitiveSet(ps)
145	if err != nil {
146		t.Fatalf("KeysetInfoFromPrimitiveSet() err = %v, want nil", err)
147	}
148	if !cmp.Equal(got, want) {
149		t.Errorf("got = %v, want = %v, with diff: %v", got, want, cmp.Diff(got, want))
150	}
151}
152