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