2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/keyservice/keyserviceproto"
|
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) {
|
2024-01-16 11:45:35 -05:00
|
|
|
goleak.VerifyTestMain(m, goleak.IgnoreAnyFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"))
|
2022-06-30 09:24:36 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 03:48:47 -04:00
|
|
|
type stubKMSClient struct {
|
2022-03-22 11:03:15 -04:00
|
|
|
getDataKeyErr error
|
|
|
|
dataKey []byte
|
|
|
|
}
|
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
func (c *stubKMSClient) GetDataKey(context.Context, *keyserviceproto.GetDataKeyRequest, *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) {
|
|
|
|
return &keyserviceproto.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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|