2021-03-11 17:20:31 +01:00
|
|
|
// These are in an external package because we need to import configfile
|
|
|
|
//
|
|
|
|
// Internal tests are in crypt_internal_test.go
|
|
|
|
|
|
|
|
package config_test
|
2021-03-10 16:40:34 +01:00
|
|
|
|
|
|
|
import (
|
2021-03-11 17:20:31 +01:00
|
|
|
"context"
|
2021-03-10 16:40:34 +01:00
|
|
|
"testing"
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2021-03-10 16:40:34 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
func TestConfigLoadEncrypted(t *testing.T) {
|
|
|
|
var err error
|
2021-04-08 17:49:47 +02:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/encrypted.conf"))
|
2021-03-10 16:40:34 +01:00
|
|
|
defer func() {
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2021-03-11 17:20:31 +01:00
|
|
|
config.ClearConfigPassword()
|
2021-03-10 16:40:34 +01:00
|
|
|
}()
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
// Set correct password
|
|
|
|
err = config.SetConfigPassword("asdf")
|
|
|
|
require.NoError(t, err)
|
2021-04-26 23:37:49 +02:00
|
|
|
err = config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
require.NoError(t, err)
|
2021-04-26 23:37:49 +02:00
|
|
|
sections := config.Data().GetSectionList()
|
2021-03-11 17:20:31 +01:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-04-26 23:37:49 +02:00
|
|
|
keys := config.Data().GetKeyList("nounc")
|
2021-03-11 17:20:31 +01:00
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
|
|
|
}
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
func TestConfigLoadEncryptedWithValidPassCommand(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-04-08 17:49:47 +02:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
2021-03-11 17:20:31 +01:00
|
|
|
oldConfig := *ci
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/encrypted.conf"))
|
2021-03-11 17:20:31 +01:00
|
|
|
// using ci.PasswordCommand, correct password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf"}
|
|
|
|
defer func() {
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2021-03-11 17:20:31 +01:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
|
|
|
}()
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
config.ClearConfigPassword()
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-04-26 23:37:49 +02:00
|
|
|
err := config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-26 23:37:49 +02:00
|
|
|
sections := config.Data().GetSectionList()
|
2021-03-11 17:20:31 +01:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
|
|
|
|
2021-04-26 23:37:49 +02:00
|
|
|
keys := config.Data().GetKeyList("nounc")
|
2021-03-11 17:20:31 +01:00
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
2021-03-10 16:40:34 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
func TestConfigLoadEncryptedWithInvalidPassCommand(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-04-08 17:49:47 +02:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
2021-03-11 17:20:31 +01:00
|
|
|
oldConfig := *ci
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/encrypted.conf"))
|
2021-03-11 17:20:31 +01:00
|
|
|
// using ci.PasswordCommand, incorrect password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf-blurfl"}
|
|
|
|
defer func() {
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2021-03-11 17:20:31 +01:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
|
|
|
}()
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
|
2021-04-26 23:37:49 +02:00
|
|
|
err := config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "using --password-command derived password")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncryptedFailures(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// This file should be too short to be decoded.
|
2021-04-08 17:49:47 +02:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/enc-short.conf"))
|
|
|
|
defer func() { assert.NoError(t, config.SetConfigPath(oldConfigPath)) }()
|
2021-04-26 23:37:49 +02:00
|
|
|
err = config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/enc-invalid.conf"))
|
2021-04-26 23:37:49 +02:00
|
|
|
err = config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/enc-too-new.conf"))
|
2021-04-26 23:37:49 +02:00
|
|
|
err = config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
require.Error(t, err)
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
// This file does not exist.
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/filenotfound.conf"))
|
2021-04-26 23:37:49 +02:00
|
|
|
err = config.Data().Load()
|
2021-03-11 17:20:31 +01:00
|
|
|
assert.Equal(t, config.ErrorConfigFileNotFound, err)
|
2021-03-10 16:40:34 +01:00
|
|
|
}
|