@@ -10,15 +10,34 @@ import (
1010 algorithms "github.com/google/go-tpm-tools/keymanager/km_common/proto"
1111)
1212
13+ type mockKCC struct {
14+ generateFn func (algo * algorithms.HpkeAlgorithm , bindingPubKey []byte , lifespanSecs uint64 ) (uuid.UUID , []byte , error )
15+ enumerateFn func (limit , offset int ) ([]kpskcc.KEMKeyInfo , bool , error )
16+ }
17+
18+ func (m * mockKCC ) GenerateKEMKeypair (algo * algorithms.HpkeAlgorithm , bindingPubKey []byte , lifespanSecs uint64 ) (uuid.UUID , []byte , error ) {
19+ if m .generateFn != nil {
20+ return m .generateFn (algo , bindingPubKey , lifespanSecs )
21+ }
22+ return uuid .Nil , nil , nil
23+ }
24+
25+ func (m * mockKCC ) EnumerateKEMKeys (limit , offset int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
26+ if m .enumerateFn != nil {
27+ return m .enumerateFn (limit , offset )
28+ }
29+ return nil , false , nil
30+ }
31+
1332func TestServiceGenerateKEMKeypairSuccess (t * testing.T ) {
1433 expectedUUID := uuid .New ()
1534 expectedPubKey := make ([]byte , 32 )
1635 for i := range expectedPubKey {
1736 expectedPubKey [i ] = byte (i + 10 )
1837 }
1938
20- svc := NewService (
21- func (_ * algorithms.HpkeAlgorithm , bindingPubKey []byte , lifespanSecs uint64 ) (uuid.UUID , []byte , error ) {
39+ svc := NewService (& mockKCC {
40+ generateFn : func (_ * algorithms.HpkeAlgorithm , bindingPubKey []byte , lifespanSecs uint64 ) (uuid.UUID , []byte , error ) {
2241 if len (bindingPubKey ) != 32 {
2342 t .Fatalf ("expected 32-byte binding public key, got %d" , len (bindingPubKey ))
2443 }
@@ -27,10 +46,7 @@ func TestServiceGenerateKEMKeypairSuccess(t *testing.T) {
2746 }
2847 return expectedUUID , expectedPubKey , nil
2948 },
30- func (_ , _ int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
31- return nil , false , nil
32- },
33- )
49+ })
3450
3551 id , pubKey , err := svc .GenerateKEMKeypair (& algorithms.HpkeAlgorithm {}, make ([]byte , 32 ), 7200 )
3652 if err != nil {
@@ -45,14 +61,11 @@ func TestServiceGenerateKEMKeypairSuccess(t *testing.T) {
4561}
4662
4763func TestServiceGenerateKEMKeypairError (t * testing.T ) {
48- svc := NewService (
49- func (_ * algorithms.HpkeAlgorithm , _ []byte , _ uint64 ) (uuid.UUID , []byte , error ) {
64+ svc := NewService (& mockKCC {
65+ generateFn : func (_ * algorithms.HpkeAlgorithm , _ []byte , _ uint64 ) (uuid.UUID , []byte , error ) {
5066 return uuid .Nil , nil , fmt .Errorf ("FFI error" )
5167 },
52- func (_ , _ int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
53- return nil , false , nil
54- },
55- )
68+ })
5669
5770 _ , _ , err := svc .GenerateKEMKeypair (& algorithms.HpkeAlgorithm {}, make ([]byte , 32 ), 3600 )
5871 if err == nil {
@@ -74,17 +87,14 @@ func TestServiceEnumerateKEMKeysSuccess(t *testing.T) {
7487 },
7588 }
7689
77- svc := NewService (
78- func (_ * algorithms.HpkeAlgorithm , _ []byte , _ uint64 ) (uuid.UUID , []byte , error ) {
79- return uuid .Nil , nil , nil
80- },
81- func (limit , offset int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
90+ svc := NewService (& mockKCC {
91+ enumerateFn : func (limit , offset int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
8292 if limit != 100 || offset != 0 {
8393 return nil , false , fmt .Errorf ("unexpected limit/offset: %d/%d" , limit , offset )
8494 }
8595 return expectedKeys , false , nil
8696 },
87- )
97+ } )
8898
8999 keys , _ , err := svc .EnumerateKEMKeys (100 , 0 )
90100 if err != nil {
@@ -99,14 +109,11 @@ func TestServiceEnumerateKEMKeysSuccess(t *testing.T) {
99109}
100110
101111func TestServiceEnumerateKEMKeysError (t * testing.T ) {
102- svc := NewService (
103- func (_ * algorithms.HpkeAlgorithm , _ []byte , _ uint64 ) (uuid.UUID , []byte , error ) {
104- return uuid .Nil , nil , nil
105- },
106- func (_ , _ int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
112+ svc := NewService (& mockKCC {
113+ enumerateFn : func (_ , _ int ) ([]kpskcc.KEMKeyInfo , bool , error ) {
107114 return nil , false , fmt .Errorf ("enumerate error" )
108115 },
109- )
116+ } )
110117
111118 _ , _ , err := svc .EnumerateKEMKeys (100 , 0 )
112119 if err == nil {
0 commit comments