2019-05-22 14:57:12 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
package setup_test
|
2019-05-22 14:57:12 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-13 13:46:08 +01:00
|
|
|
|
2019-05-22 14:57:12 +01:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-05-22 14:57:12 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-27 18:36:51 +01:00
|
|
|
"storj.io/storj/uplink/setup"
|
2019-05-22 14:57:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaveEncryptionKey(t *testing.T) {
|
2019-06-07 17:14:40 +01:00
|
|
|
generateInputKey := func() string {
|
2019-06-26 11:38:51 +01:00
|
|
|
return string(testrand.BytesInt(testrand.Intn(storj.KeySize*3) + 1))
|
2019-05-22 14:57:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
inputKey := generateInputKey()
|
|
|
|
filename := ctx.File("storj-test-cmd-uplink", "encryption.key")
|
2019-06-27 18:36:51 +01:00
|
|
|
err := setup.SaveEncryptionKey(inputKey, filename)
|
2019-05-22 14:57:12 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
savedKey, err := ioutil.ReadFile(filename)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-07 17:14:40 +01:00
|
|
|
assert.Equal(t, inputKey, string(savedKey))
|
2019-05-22 14:57:12 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error: empty input key", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
filename := ctx.File("storj-test-cmd-uplink", "encryption.key")
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
err := setup.SaveEncryptionKey("", filename)
|
2019-05-22 14:57:12 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error: empty filepath", func(t *testing.T) {
|
|
|
|
inputKey := generateInputKey()
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
err := setup.SaveEncryptionKey(inputKey, "")
|
2019-05-22 14:57:12 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error: unexisting dir", func(t *testing.T) {
|
|
|
|
// Create a directory and remove it for making sure that the path doesn't
|
|
|
|
// exist
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
dir := ctx.Dir("storj-test-cmd-uplink")
|
|
|
|
ctx.Cleanup()
|
|
|
|
|
|
|
|
inputKey := generateInputKey()
|
|
|
|
filename := filepath.Join(dir, "enc.key")
|
2019-06-27 18:36:51 +01:00
|
|
|
err := setup.SaveEncryptionKey(inputKey, filename)
|
2019-05-22 14:57:12 +01:00
|
|
|
require.Errorf(t, err, "directory path doesn't exist")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error: file already exists", func(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
inputKey := generateInputKey()
|
|
|
|
filename := ctx.File("encryption.key")
|
|
|
|
require.NoError(t, ioutil.WriteFile(filename, nil, os.FileMode(0600)))
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
err := setup.SaveEncryptionKey(inputKey, filename)
|
2019-05-22 14:57:12 +01:00
|
|
|
require.Errorf(t, err, "file key already exists")
|
|
|
|
})
|
|
|
|
}
|