2021-03-10 16:40:34 +01:00
|
|
|
package config_test
|
2015-02-19 20:26:00 +01:00
|
|
|
|
2016-02-16 16:25:27 +01:00
|
|
|
import (
|
2019-06-19 14:51:19 +02:00
|
|
|
"bytes"
|
2020-11-05 17:59:59 +01:00
|
|
|
"context"
|
2018-04-13 17:06:37 +02:00
|
|
|
"fmt"
|
2017-06-25 08:55:54 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-02-16 16:25:27 +01:00
|
|
|
"testing"
|
2016-06-29 18:59:31 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-03-10 16:40:34 +01:00
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2016-06-29 18:59:31 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-16 16:25:27 +01:00
|
|
|
)
|
2015-02-19 20:26:00 +01:00
|
|
|
|
2019-06-10 18:58:47 +02:00
|
|
|
func testConfigFile(t *testing.T, configFileName string) func() {
|
2020-11-05 17:59:59 +01:00
|
|
|
ctx := context.Background()
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ClearConfigPassword()
|
2019-06-22 18:25:12 +02:00
|
|
|
_ = os.Unsetenv("_RCLONE_CONFIG_KEY_FILE")
|
|
|
|
_ = os.Unsetenv("RCLONE_CONFIG_PASS")
|
2017-06-25 08:55:54 +02:00
|
|
|
// create temp config file
|
2019-06-10 18:58:47 +02:00
|
|
|
tempFile, err := ioutil.TempFile("", configFileName)
|
2017-06-25 08:55:54 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
path := tempFile.Name()
|
|
|
|
assert.NoError(t, tempFile.Close())
|
|
|
|
|
|
|
|
// temporarily adapt configuration
|
|
|
|
oldOsStdout := os.Stdout
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigPath := config.ConfigPath
|
2020-11-05 12:33:32 +01:00
|
|
|
oldConfig := *ci
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigFile := config.Data
|
|
|
|
oldReadLine := config.ReadLine
|
|
|
|
oldPassword := config.Password
|
2017-06-25 08:55:54 +02:00
|
|
|
os.Stdout = nil
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = path
|
2020-11-05 12:33:32 +01:00
|
|
|
ci = &fs.ConfigInfo{}
|
2019-06-10 18:58:47 +02:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
configfile.LoadConfig(ctx)
|
|
|
|
assert.Equal(t, []string{}, config.Data.GetSectionList())
|
2019-06-10 18:58:47 +02:00
|
|
|
|
|
|
|
// Fake a remote
|
|
|
|
fs.Register(&fs.RegInfo{
|
|
|
|
Name: "config_test_remote",
|
|
|
|
Options: fs.Options{
|
|
|
|
{
|
|
|
|
Name: "bool",
|
|
|
|
Default: false,
|
|
|
|
IsPassword: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pass",
|
|
|
|
Default: "",
|
|
|
|
IsPassword: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Undo the above
|
|
|
|
return func() {
|
|
|
|
err := os.Remove(path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-25 08:55:54 +02:00
|
|
|
os.Stdout = oldOsStdout
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ReadLine = oldReadLine
|
|
|
|
config.Password = oldPassword
|
2020-11-05 12:33:32 +01:00
|
|
|
*ci = oldConfig
|
2021-03-10 16:40:34 +01:00
|
|
|
config.Data = oldConfigFile
|
2019-06-22 18:25:12 +02:00
|
|
|
|
|
|
|
_ = os.Unsetenv("_RCLONE_CONFIG_KEY_FILE")
|
|
|
|
_ = os.Unsetenv("RCLONE_CONFIG_PASS")
|
2019-06-10 18:58:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-25 08:55:54 +02:00
|
|
|
|
2019-08-28 12:46:35 +02:00
|
|
|
// makeReadLine makes a simple readLine which returns a fixed list of
|
|
|
|
// strings
|
|
|
|
func makeReadLine(answers []string) func() string {
|
2017-06-25 08:55:54 +02:00
|
|
|
i := 0
|
2019-08-28 12:46:35 +02:00
|
|
|
return func() string {
|
2017-06-25 08:55:54 +02:00
|
|
|
i = i + 1
|
|
|
|
return answers[i-1]
|
|
|
|
}
|
2019-08-28 12:46:35 +02:00
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
|
2019-08-28 12:46:35 +02:00
|
|
|
func TestCRUD(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 12:33:32 +01:00
|
|
|
ctx := context.Background()
|
2019-08-28 12:46:35 +02:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 12:46:35 +02:00
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"y", // type my own password
|
|
|
|
"secret", // password
|
|
|
|
"secret", // repeat
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 16:40:34 +01:00
|
|
|
config.NewRemote(ctx, "test")
|
2017-06-25 08:55:54 +02:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, []string{"test"}, config.Data.GetSectionList())
|
|
|
|
assert.Equal(t, "config_test_remote", config.FileGet("test", "type"))
|
|
|
|
assert.Equal(t, "true", config.FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "secret", obscure.MustReveal(config.FileGet("test", "pass")))
|
2018-01-12 17:30:54 +01:00
|
|
|
|
2017-06-25 08:55:54 +02:00
|
|
|
// normal rename, test → asdf
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 12:46:35 +02:00
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
})
|
2021-03-10 16:40:34 +01:00
|
|
|
config.RenameRemote("test")
|
2019-06-10 18:58:47 +02:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, []string{"asdf"}, config.Data.GetSectionList())
|
|
|
|
assert.Equal(t, "config_test_remote", config.FileGet("asdf", "type"))
|
|
|
|
assert.Equal(t, "true", config.FileGet("asdf", "bool"))
|
|
|
|
assert.Equal(t, "secret", obscure.MustReveal(config.FileGet("asdf", "pass")))
|
2017-06-25 08:55:54 +02:00
|
|
|
|
|
|
|
// delete remote
|
2021-03-10 16:40:34 +01:00
|
|
|
config.DeleteRemote("asdf")
|
|
|
|
assert.Equal(t, []string{}, config.Data.GetSectionList())
|
2017-06-25 08:55:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 12:46:35 +02:00
|
|
|
func TestChooseOption(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 12:33:32 +01:00
|
|
|
ctx := context.Background()
|
2019-08-28 12:46:35 +02:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 12:46:35 +02:00
|
|
|
"config_test_remote", // type
|
|
|
|
"false", // bool value
|
|
|
|
"x", // bad choice
|
|
|
|
"g", // generate password
|
|
|
|
"1024", // very big
|
|
|
|
"y", // password OK
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 16:40:34 +01:00
|
|
|
config.Password = func(bits int) (string, error) {
|
2019-08-28 12:46:35 +02:00
|
|
|
assert.Equal(t, 1024, bits)
|
|
|
|
return "not very random password", nil
|
|
|
|
}
|
2021-03-10 16:40:34 +01:00
|
|
|
config.NewRemote(ctx, "test")
|
2019-08-28 12:46:35 +02:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, "false", config.FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "not very random password", obscure.MustReveal(config.FileGet("test", "pass")))
|
2019-08-28 12:46:35 +02:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 12:46:35 +02:00
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"n", // not required
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 16:40:34 +01:00
|
|
|
config.NewRemote(ctx, "test")
|
2019-08-28 12:46:35 +02:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, "true", config.FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "", config.FileGet("test", "pass"))
|
2019-08-28 12:46:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 13:39:33 +01:00
|
|
|
func TestNewRemoteName(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 12:33:32 +01:00
|
|
|
ctx := context.Background()
|
2019-11-05 13:39:33 +01:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-11-05 13:39:33 +01:00
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"n", // not required
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 16:40:34 +01:00
|
|
|
config.NewRemote(ctx, "test")
|
2019-11-05 13:39:33 +01:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-11-05 13:39:33 +01:00
|
|
|
"test", // already exists
|
|
|
|
"", // empty string not allowed
|
|
|
|
"bad@characters", // bad characters
|
|
|
|
"newname", // OK
|
|
|
|
})
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, "newname", config.NewRemoteName())
|
2019-11-05 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
func TestCreateUpdatePasswordRemote(t *testing.T) {
|
2020-11-05 19:02:26 +01:00
|
|
|
ctx := context.Background()
|
2019-06-10 18:58:47 +02:00
|
|
|
defer testConfigFile(t, "update.conf")()
|
|
|
|
|
2020-05-12 15:24:53 +02:00
|
|
|
for _, doObscure := range []bool{false, true} {
|
|
|
|
for _, noObscure := range []bool{false, true} {
|
|
|
|
if doObscure && noObscure {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Run(fmt.Sprintf("doObscure=%v,noObscure=%v", doObscure, noObscure), func(t *testing.T) {
|
2021-03-10 16:40:34 +01:00
|
|
|
require.NoError(t, config.CreateRemote(ctx, "test2", "config_test_remote", rc.Params{
|
2020-05-12 15:24:53 +02:00
|
|
|
"bool": true,
|
|
|
|
"pass": "potato",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, []string{"test2"}, config.Data.GetSectionList())
|
|
|
|
assert.Equal(t, "config_test_remote", config.FileGet("test2", "type"))
|
|
|
|
assert.Equal(t, "true", config.FileGet("test2", "bool"))
|
|
|
|
gotPw := config.FileGet("test2", "pass")
|
2020-05-12 15:24:53 +02:00
|
|
|
if !noObscure {
|
|
|
|
gotPw = obscure.MustReveal(gotPw)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "potato", gotPw)
|
|
|
|
|
|
|
|
wantPw := obscure.MustObscure("potato2")
|
2021-03-10 16:40:34 +01:00
|
|
|
require.NoError(t, config.UpdateRemote(ctx, "test2", rc.Params{
|
2020-05-12 15:24:53 +02:00
|
|
|
"bool": false,
|
|
|
|
"pass": wantPw,
|
|
|
|
"spare": "spare",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, []string{"test2"}, config.Data.GetSectionList())
|
|
|
|
assert.Equal(t, "config_test_remote", config.FileGet("test2", "type"))
|
|
|
|
assert.Equal(t, "false", config.FileGet("test2", "bool"))
|
|
|
|
gotPw = config.FileGet("test2", "pass")
|
2020-05-12 15:24:53 +02:00
|
|
|
if doObscure {
|
|
|
|
gotPw = obscure.MustReveal(gotPw)
|
|
|
|
}
|
|
|
|
assert.Equal(t, wantPw, gotPw)
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
require.NoError(t, config.PasswordRemote(ctx, "test2", rc.Params{
|
2020-05-12 15:24:53 +02:00
|
|
|
"pass": "potato3",
|
|
|
|
}))
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, []string{"test2"}, config.Data.GetSectionList())
|
|
|
|
assert.Equal(t, "config_test_remote", config.FileGet("test2", "type"))
|
|
|
|
assert.Equal(t, "false", config.FileGet("test2", "bool"))
|
|
|
|
assert.Equal(t, "potato3", obscure.MustReveal(config.FileGet("test2", "pass")))
|
2020-05-12 15:24:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:58:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 13:04:43 +02:00
|
|
|
// Test some error cases
|
|
|
|
func TestReveal(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
wantErr string
|
|
|
|
}{
|
2017-10-19 13:36:12 +02:00
|
|
|
{"YmJiYmJiYmJiYmJiYmJiYp*gcEWbAw", "base64 decode failed when revealing password - is it obscured?: illegal base64 data at input byte 22"},
|
|
|
|
{"aGVsbG8", "input too short when revealing password - is it obscured?"},
|
|
|
|
{"", "input too short when revealing password - is it obscured?"},
|
2016-08-14 13:04:43 +02:00
|
|
|
} {
|
2018-01-18 21:19:55 +01:00
|
|
|
gotString, gotErr := obscure.Reveal(test.in)
|
2016-08-14 13:04:43 +02:00
|
|
|
assert.Equal(t, "", gotString)
|
|
|
|
assert.Equal(t, test.wantErr, gotErr.Error())
|
2015-09-01 23:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
func TestConfigLoad(t *testing.T) {
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigPath := config.ConfigPath
|
|
|
|
config.ConfigPath = "./testdata/plain.conf"
|
2016-02-29 22:43:37 +01:00
|
|
|
defer func() {
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = oldConfigPath
|
2016-02-29 22:43:37 +01:00
|
|
|
}()
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
configfile.LoadConfig(context.Background())
|
|
|
|
sections := config.Data.GetSectionList()
|
2016-02-16 16:25:27 +01:00
|
|
|
var expect = []string{"RCLONE_ENCRYPT_V0", "nounc", "unc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
keys := config.Data.GetKeyList("nounc")
|
2016-02-16 16:25:27 +01:00
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncrypted(t *testing.T) {
|
|
|
|
var err error
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigPath := config.ConfigPath
|
|
|
|
config.ConfigPath = "./testdata/encrypted.conf"
|
2016-02-29 22:43:37 +01:00
|
|
|
defer func() {
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ClearConfigPassword()
|
2016-02-29 22:43:37 +01:00
|
|
|
}()
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// Set correct password
|
2021-03-10 16:40:34 +01:00
|
|
|
err = config.SetConfigPassword("asdf")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2021-03-10 16:40:34 +01:00
|
|
|
err = config.Data.Load()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2021-03-10 16:40:34 +01:00
|
|
|
sections := config.Data.GetSectionList()
|
2016-02-16 16:25:27 +01:00
|
|
|
var expect = []string{"nounc", "unc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
keys := config.Data.GetKeyList("nounc")
|
2016-02-16 16:25:27 +01:00
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
2019-11-18 10:55:27 +01:00
|
|
|
func TestConfigLoadEncryptedWithValidPassCommand(t *testing.T) {
|
2020-11-05 12:33:32 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigPath := config.ConfigPath
|
2020-11-05 12:33:32 +01:00
|
|
|
oldConfig := *ci
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = "./testdata/encrypted.conf"
|
2020-11-05 12:33:32 +01:00
|
|
|
// using ci.PasswordCommand, correct password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf"}
|
2019-11-18 10:55:27 +01:00
|
|
|
defer func() {
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ClearConfigPassword()
|
2020-11-05 12:33:32 +01:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 10:55:27 +01:00
|
|
|
}()
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ClearConfigPassword()
|
2019-11-18 10:55:27 +01:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
err := config.Data.Load()
|
2019-11-18 10:55:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
sections := config.Data.GetSectionList()
|
2020-01-23 14:54:18 +01:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
keys := config.Data.GetKeyList("nounc")
|
2020-01-23 14:54:18 +01:00
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
2019-11-18 10:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncryptedWithInvalidPassCommand(t *testing.T) {
|
2020-11-05 12:33:32 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigPath := config.ConfigPath
|
2020-11-05 12:33:32 +01:00
|
|
|
oldConfig := *ci
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = "./testdata/encrypted.conf"
|
2020-11-05 12:33:32 +01:00
|
|
|
// using ci.PasswordCommand, incorrect password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf-blurfl"}
|
2019-11-18 10:55:27 +01:00
|
|
|
defer func() {
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ClearConfigPassword()
|
2020-11-05 12:33:32 +01:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 10:55:27 +01:00
|
|
|
}()
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ClearConfigPassword()
|
2019-11-18 10:55:27 +01:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
err := config.Data.Load()
|
2020-01-23 14:54:18 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "using --password-command derived password")
|
2019-11-18 10:55:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-16 16:25:27 +01:00
|
|
|
func TestConfigLoadEncryptedFailures(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// This file should be too short to be decoded.
|
2021-03-10 16:40:34 +01:00
|
|
|
oldConfigPath := config.ConfigPath
|
|
|
|
config.ConfigPath = "./testdata/enc-short.conf"
|
|
|
|
defer func() { config.ConfigPath = oldConfigPath }()
|
|
|
|
err = config.Data.Load()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = "./testdata/enc-invalid.conf"
|
|
|
|
err = config.Data.Load()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = "./testdata/enc-too-new.conf"
|
|
|
|
err = config.Data.Load()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
2016-12-20 16:05:08 +01:00
|
|
|
// This file does not exist.
|
2021-03-10 16:40:34 +01:00
|
|
|
config.ConfigPath = "./testdata/filenotfound.conf"
|
|
|
|
err = config.Data.Load()
|
|
|
|
assert.Equal(t, config.ErrorConfigFileNotFound, err)
|
2018-04-13 17:06:37 +02:00
|
|
|
}
|
2019-06-19 14:51:19 +02:00
|
|
|
|
|
|
|
func TestFileRefresh(t *testing.T) {
|
2020-11-05 19:02:26 +01:00
|
|
|
ctx := context.Background()
|
2019-06-19 14:51:19 +02:00
|
|
|
defer testConfigFile(t, "refresh.conf")()
|
2021-03-10 16:40:34 +01:00
|
|
|
require.NoError(t, config.CreateRemote(ctx, "refresh_test", "config_test_remote", rc.Params{
|
2019-06-19 14:51:19 +02:00
|
|
|
"bool": true,
|
2020-05-12 15:24:53 +02:00
|
|
|
}, false, false))
|
2021-03-10 16:40:34 +01:00
|
|
|
b, err := ioutil.ReadFile(config.ConfigPath)
|
2019-06-19 14:51:19 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b = bytes.Replace(b, []byte("refresh_test"), []byte("refreshed_test"), 1)
|
2021-03-10 16:40:34 +01:00
|
|
|
err = ioutil.WriteFile(config.ConfigPath, b, 0644)
|
2019-06-19 14:51:19 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.NotEqual(t, []string{"refreshed_test"}, config.Data.GetSectionList())
|
|
|
|
err = config.FileRefresh()
|
2019-06-19 14:51:19 +02:00
|
|
|
assert.NoError(t, err)
|
2021-03-10 16:40:34 +01:00
|
|
|
assert.Equal(t, []string{"refreshed_test"}, config.Data.GetSectionList())
|
2019-06-19 14:51:19 +02:00
|
|
|
}
|