2021-03-12 00:20:31 +08: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 23:40:34 +08:00
|
|
|
|
|
|
|
import (
|
2021-03-12 00:20:31 +08:00
|
|
|
"context"
|
2021-03-10 23:40:34 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2021-03-10 23:40:34 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
func TestConfigLoadEncrypted(t *testing.T) {
|
|
|
|
var err error
|
2021-04-08 23:49:47 +08:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/encrypted.conf"))
|
2021-03-10 23:40:34 +08:00
|
|
|
defer func() {
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2021-03-12 00:20:31 +08:00
|
|
|
config.ClearConfigPassword()
|
2021-03-10 23:40:34 +08:00
|
|
|
}()
|
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
// Set correct password
|
|
|
|
err = config.SetConfigPassword("asdf")
|
|
|
|
require.NoError(t, err)
|
2021-04-27 05:37:49 +08:00
|
|
|
err = config.Data().Load()
|
2021-03-12 00:20:31 +08:00
|
|
|
require.NoError(t, err)
|
2021-04-27 05:37:49 +08:00
|
|
|
sections := config.Data().GetSectionList()
|
2021-03-12 00:20:31 +08:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
2021-03-10 23:40:34 +08:00
|
|
|
|
2021-04-27 05:37:49 +08:00
|
|
|
keys := config.Data().GetKeyList("nounc")
|
2021-03-12 00:20:31 +08:00
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
|
|
|
}
|
2021-03-10 23:40:34 +08:00
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
func TestConfigLoadEncryptedWithValidPassCommand(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-04-08 23:49:47 +08:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
2021-03-12 00:20:31 +08:00
|
|
|
oldConfig := *ci
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/encrypted.conf"))
|
2021-03-12 00:20:31 +08:00
|
|
|
// using ci.PasswordCommand, correct password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf"}
|
|
|
|
defer func() {
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2021-03-12 00:20:31 +08:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
|
|
|
}()
|
2021-03-10 23:40:34 +08:00
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
config.ClearConfigPassword()
|
2021-03-10 23:40:34 +08:00
|
|
|
|
2021-04-27 05:37:49 +08:00
|
|
|
err := config.Data().Load()
|
2021-03-12 00:20:31 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-27 05:37:49 +08:00
|
|
|
sections := config.Data().GetSectionList()
|
2021-03-12 00:20:31 +08:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
|
|
|
|
2021-04-27 05:37:49 +08:00
|
|
|
keys := config.Data().GetKeyList("nounc")
|
2021-03-12 00:20:31 +08:00
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
2021-03-10 23:40:34 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
func TestConfigLoadEncryptedWithInvalidPassCommand(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-04-08 23:49:47 +08:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
2021-03-12 00:20:31 +08:00
|
|
|
oldConfig := *ci
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/encrypted.conf"))
|
2021-03-12 00:20:31 +08:00
|
|
|
// using ci.PasswordCommand, incorrect password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf-blurfl"}
|
|
|
|
defer func() {
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2021-03-12 00:20:31 +08:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
|
|
|
}()
|
2021-03-10 23:40:34 +08:00
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
|
2021-04-27 05:37:49 +08:00
|
|
|
err := config.Data().Load()
|
2021-03-12 00:20:31 +08: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 23:49:47 +08:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/enc-short.conf"))
|
|
|
|
defer func() { assert.NoError(t, config.SetConfigPath(oldConfigPath)) }()
|
2021-04-27 05:37:49 +08:00
|
|
|
err = config.Data().Load()
|
2021-03-12 00:20:31 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/enc-invalid.conf"))
|
2021-04-27 05:37:49 +08:00
|
|
|
err = config.Data().Load()
|
2021-03-12 00:20:31 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/enc-too-new.conf"))
|
2021-04-27 05:37:49 +08:00
|
|
|
err = config.Data().Load()
|
2021-03-12 00:20:31 +08:00
|
|
|
require.Error(t, err)
|
2021-03-10 23:40:34 +08:00
|
|
|
|
2021-03-12 00:20:31 +08:00
|
|
|
// This file does not exist.
|
2021-04-08 23:49:47 +08:00
|
|
|
assert.NoError(t, config.SetConfigPath("./testdata/filenotfound.conf"))
|
2021-04-27 05:37:49 +08:00
|
|
|
err = config.Data().Load()
|
2021-03-12 00:20:31 +08:00
|
|
|
assert.Equal(t, config.ErrorConfigFileNotFound, err)
|
2021-03-10 23:40:34 +08:00
|
|
|
}
|