2022-03-22 11:03:15 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
"github.com/edgelesssys/constellation/kms/kmsproto"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-03-22 11:03:15 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
type stubKMSClient struct {
|
2022-03-22 11:03:15 -04:00
|
|
|
getDataKeyErr error
|
|
|
|
dataKey []byte
|
|
|
|
}
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
func (c *stubKMSClient) GetDataKey(context.Context, *kmsproto.GetDataKeyRequest, *grpc.ClientConn) (*kmsproto.GetDataKeyResponse, error) {
|
|
|
|
return &kmsproto.GetDataKeyResponse{DataKey: c.dataKey}, c.getDataKeyErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstellationKMS(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-07-05 03:48:47 -04:00
|
|
|
kms *stubKMSClient
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"GetDataKey success": {
|
2022-07-05 03:48:47 -04:00
|
|
|
kms: &stubKMSClient{dataKey: []byte{0x1, 0x2, 0x3}},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"GetDataKey error": {
|
2022-07-05 03:48:47 -04:00
|
|
|
kms: &stubKMSClient{getDataKeyErr: errors.New("error")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2022-07-05 03:48:47 -04:00
|
|
|
kms: tc.kms,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
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
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
assert.Nil(res)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|