2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-01-11 04:08:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/keyservice/keyserviceproto"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-05-23 05:36:54 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubClient struct {
|
|
|
|
getDataKeyErr error
|
|
|
|
dataKey []byte
|
|
|
|
}
|
|
|
|
|
2023-01-11 04:08:57 -05:00
|
|
|
func (c *stubClient) GetDataKey(context.Context, *keyserviceproto.GetDataKeyRequest, *grpc.ClientConn) (*keyserviceproto.GetDataKeyResponse, error) {
|
|
|
|
return &keyserviceproto.GetDataKeyResponse{DataKey: c.dataKey}, c.getDataKeyErr
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
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-05-23 05:36:54 -04:00
|
|
|
func TestGetDataKey(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
client *stubClient
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"GetDataKey success": {
|
|
|
|
client: &stubClient{dataKey: []byte{0x1, 0x2, 0x3}},
|
|
|
|
},
|
|
|
|
"GetDataKey error": {
|
|
|
|
client: &stubClient{getDataKeyErr: errors.New("error")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
listener := bufconn.Listen(1)
|
|
|
|
defer listener.Close()
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
client := New(
|
|
|
|
logger.NewTest(t),
|
|
|
|
listener.Addr().String(),
|
|
|
|
)
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
client.grpc = tc.client
|
|
|
|
|
|
|
|
res, err := client.GetDataKey(context.Background(), "disk-uuid", 32)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.client.dataKey, res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|