The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/registry/apis/secret/encryption/cipher/service/service_test.go

50 lines
1.3 KiB

package service
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace/noop"
"github.com/grafana/grafana/pkg/infra/usagestats"
"github.com/grafana/grafana/pkg/registry/apis/secret/encryption/cipher"
)
func newGcmService(t *testing.T) cipher.Cipher {
t.Helper()
usageStats := &usagestats.UsageStatsMock{}
svc, err := ProvideAESGCMCipherService(noop.NewTracerProvider().Tracer("test"), usageStats)
require.NoError(t, err, "failed to set up encryption service")
return svc
}
func TestService(t *testing.T) {
t.Parallel()
t.Run("decrypt empty payload should return error", func(t *testing.T) {
t.Parallel()
svc := newGcmService(t)
_, err := svc.Decrypt(t.Context(), []byte(""), "1234")
require.Error(t, err)
assert.Equal(t, "unable to derive encryption algorithm", err.Error())
})
t.Run("encrypt and decrypt with GCM should work", func(t *testing.T) {
t.Parallel()
svc := newGcmService(t)
encrypted, err := svc.Encrypt(t.Context(), []byte("grafana"), "1234")
require.NoError(t, err)
decrypted, err := svc.Decrypt(t.Context(), encrypted, "1234")
require.NoError(t, err)
assert.Equal(t, []byte("grafana"), decrypted)
// We'll let the provider deal with testing details.
})
}