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"
|
2024-09-05 12:52:15 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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
|
2024-09-05 12:52:15 +02:00
|
|
|
assert.False(t, config.IsEncrypted())
|
2021-03-11 17:20:31 +01:00
|
|
|
err = config.SetConfigPassword("asdf")
|
|
|
|
require.NoError(t, err)
|
2024-09-05 12:52:15 +02:00
|
|
|
assert.True(t, config.IsEncrypted())
|
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
|
|
|
}
|
2024-07-23 13:36:04 +02:00
|
|
|
|
|
|
|
func TestGetPasswordCommand(t *testing.T) {
|
|
|
|
ctx, ci := fs.AddConfig(context.Background())
|
|
|
|
|
|
|
|
// Not configured
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{}
|
|
|
|
pass, err := config.GetPasswordCommand(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "", pass)
|
|
|
|
|
|
|
|
// With password - happy path
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf"}
|
|
|
|
pass, err = config.GetPasswordCommand(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "asdf", pass)
|
|
|
|
|
|
|
|
// Empty password returned
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", ""}
|
|
|
|
_, err = config.GetPasswordCommand(ctx)
|
|
|
|
assert.ErrorContains(t, err, "returned empty string")
|
|
|
|
|
|
|
|
// Error when running command
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"XXX non-existent command XXX", ""}
|
|
|
|
_, err = config.GetPasswordCommand(ctx)
|
|
|
|
assert.ErrorContains(t, err, "not found")
|
2024-09-05 12:52:15 +02:00
|
|
|
|
|
|
|
// Check the state of the environment variable in --password-command
|
|
|
|
checkCode := `
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if _, found := os.LookupEnv("RCLONE_PASSWORD_CHANGE"); found {
|
|
|
|
fmt.Println("Env var set")
|
|
|
|
} else {
|
|
|
|
fmt.Println("OK")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
dir := t.TempDir()
|
|
|
|
code := filepath.Join(dir, "file.go")
|
|
|
|
require.NoError(t, os.WriteFile(code, []byte(checkCode), 0777))
|
|
|
|
|
|
|
|
// Check the environment variable unset when called directly
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"go", "run", code}
|
|
|
|
pass, err = config.GetPasswordCommand(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "OK", pass)
|
2024-07-23 13:36:04 +02:00
|
|
|
}
|