Skip to content

Commit

Permalink
Add a Len() method to Handle
Browse files Browse the repository at this point in the history
#tinkApiChange

PiperOrigin-RevId: 635926914
Change-Id: I9aec188f14101880e8f36e603f9c00484932818b
  • Loading branch information
morambro authored and copybara-github committed May 21, 2024
1 parent f3be051 commit 2030aeb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions keyset/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (h *Handle) String() string {
return string(c)
}

// Len returns the number of keys in the keyset.
func (h *Handle) Len() int {
return len(h.ks.GetKey())
}

// KeysetInfo returns KeysetInfo representation of the managed keyset.
// The result does not contain any sensitive key material.
func (h *Handle) KeysetInfo() *tinkpb.KeysetInfo {
Expand Down
30 changes: 30 additions & 0 deletions keyset/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,33 @@ func TestPrimitivesWithKeyManager(t *testing.T) {
t.Errorf("handle.PrimitivesWithKeyManager().Primary = %v, want instance of `testPrimitive`", primitives.Primary.Primitive)
}
}

func TestLenWithOneKey(t *testing.T) {
template := mac.HMACSHA256Tag128KeyTemplate()
handle, err := keyset.NewHandle(template)
if err != nil {
t.Fatalf("keyset.NewHandle(%v) err = %v, want nil", template, err)
}
if handle.Len() != 1 {
t.Errorf("handle.Len() = %d, want 1", handle.Len())
}
}

func TestLenWithMultipleKeys(t *testing.T) {
ks := &tinkpb.Keyset{
Key: []*tinkpb.Keyset_Key{
testutil.NewDummyKey(1, tinkpb.KeyStatusType_ENABLED, tinkpb.OutputPrefixType_TINK),
testutil.NewDummyKey(2, tinkpb.KeyStatusType_ENABLED, tinkpb.OutputPrefixType_TINK),
testutil.NewDummyKey(3, tinkpb.KeyStatusType_ENABLED, tinkpb.OutputPrefixType_TINK),
testutil.NewDummyKey(4, tinkpb.KeyStatusType_ENABLED, tinkpb.OutputPrefixType_TINK),
},
PrimaryKeyId: 1,
}
handle, err := testkeyset.NewHandle(ks)
if err != nil {
t.Fatalf("testkeyset.NewHandle(%v) err = %v, want nil", ks, err)
}
if handle.Len() != len(ks.Key) {
t.Errorf("handle.Len() = %d, want %d", handle.Len(), len(ks.Key))
}
}

0 comments on commit 2030aeb

Please sign in to comment.