2022-03-22 11:03:15 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/vpnapi/vpnproto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubVPNClient struct {
|
|
|
|
getDataKeyErr error
|
|
|
|
dataKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubVPNClient) GetDataKey(context.Context, *vpnproto.GetDataKeyRequest, *grpc.ClientConn) (*vpnproto.GetDataKeyResponse, error) {
|
|
|
|
return &vpnproto.GetDataKeyResponse{DataKey: c.dataKey}, c.getDataKeyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstellationKMS(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
vpn *stubVPNClient
|
|
|
|
errExpected bool
|
|
|
|
}{
|
|
|
|
"GetDataKey success": {
|
|
|
|
vpn: &stubVPNClient{dataKey: []byte{0x1, 0x2, 0x3}},
|
|
|
|
errExpected: false,
|
|
|
|
},
|
|
|
|
"GetDataKey error": {
|
|
|
|
vpn: &stubVPNClient{getDataKeyErr: errors.New("error")},
|
|
|
|
errExpected: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
listener := bufconn.Listen(1)
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
kms := &ConstellationKMS{
|
|
|
|
endpoint: listener.Addr().String(),
|
|
|
|
vpn: tc.vpn,
|
|
|
|
}
|
2022-03-24 10:21:19 -04:00
|
|
|
res, err := kms.GetDEK(context.Background(), "data-key", 64)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
if tc.errExpected {
|
|
|
|
assert.Error(err)
|
|
|
|
assert.Nil(res)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|