2019-06-27 18:36:51 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package setup_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testrand"
|
|
|
|
libuplink "storj.io/storj/lib/uplink"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/uplink"
|
|
|
|
"storj.io/storj/uplink/setup"
|
|
|
|
)
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
func TestLoadEncryptionAccess(t *testing.T) {
|
|
|
|
saveRawCtx := func(access *libuplink.EncryptionAccess) (filepath string, clenaup func()) {
|
2019-06-27 18:36:51 +01:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
filename := ctx.File("encryption.ctx")
|
2019-06-28 06:18:24 +01:00
|
|
|
data, err := access.Serialize()
|
2019-06-27 18:36:51 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(filename, []byte(data), os.FileMode(0400))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return filename, ctx.Cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("ok: reading from file", func(t *testing.T) {
|
|
|
|
passphrase := testrand.BytesInt(1 + testrand.Intn(100))
|
|
|
|
|
|
|
|
key, err := storj.NewKey(passphrase)
|
|
|
|
require.NoError(t, err)
|
2019-06-28 06:18:24 +01:00
|
|
|
access := libuplink.NewEncryptionAccessWithDefaultKey(*key)
|
|
|
|
filename, cleanup := saveRawCtx(access)
|
2019-06-27 18:36:51 +01:00
|
|
|
defer cleanup()
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
gotCtx, err := setup.LoadEncryptionAccess(context.Background(), uplink.EncryptionConfig{
|
|
|
|
EncAccessFilepath: filename,
|
2019-06-27 18:36:51 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-06-28 06:18:24 +01:00
|
|
|
require.Equal(t, access, gotCtx)
|
2019-06-27 18:36:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ok: empty filepath", func(t *testing.T) {
|
2019-06-28 06:18:24 +01:00
|
|
|
gotCtx, err := setup.LoadEncryptionAccess(context.Background(), uplink.EncryptionConfig{
|
|
|
|
EncAccessFilepath: "",
|
2019-06-27 18:36:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, gotCtx)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error: file not found", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
filename := ctx.File("encryption.ctx")
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
_, err := setup.LoadEncryptionAccess(context.Background(), uplink.EncryptionConfig{
|
|
|
|
EncAccessFilepath: filename,
|
2019-06-27 18:36:51 +01:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|