2021-03-10 15:40:34 +00:00
|
|
|
package config_test
|
2015-02-19 19:26:00 +00:00
|
|
|
|
2016-02-16 15:25:27 +00:00
|
|
|
import (
|
2019-06-19 12:51:19 +00:00
|
|
|
"bytes"
|
2020-11-05 16:59:59 +00:00
|
|
|
"context"
|
2018-04-13 15:06:37 +00:00
|
|
|
"fmt"
|
2017-06-25 06:55:54 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-02-16 15:25:27 +00:00
|
|
|
"testing"
|
2016-06-29 16:59:31 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-03-10 15:40:34 +00:00
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2016-06-29 16:59:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-16 15:25:27 +00:00
|
|
|
)
|
2015-02-19 19:26:00 +00:00
|
|
|
|
2019-06-10 16:58:47 +00:00
|
|
|
func testConfigFile(t *testing.T, configFileName string) func() {
|
2020-11-05 16:59:59 +00:00
|
|
|
ctx := context.Background()
|
2020-11-05 11:33:32 +00:00
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ClearConfigPassword()
|
2019-06-22 16:25:12 +00:00
|
|
|
_ = os.Unsetenv("_RCLONE_CONFIG_KEY_FILE")
|
|
|
|
_ = os.Unsetenv("RCLONE_CONFIG_PASS")
|
2017-06-25 06:55:54 +00:00
|
|
|
// create temp config file
|
2019-06-10 16:58:47 +00:00
|
|
|
tempFile, err := ioutil.TempFile("", configFileName)
|
2017-06-25 06:55:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
path := tempFile.Name()
|
|
|
|
assert.NoError(t, tempFile.Close())
|
|
|
|
|
|
|
|
// temporarily adapt configuration
|
|
|
|
oldOsStdout := os.Stdout
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigPath := config.ConfigPath
|
2020-11-05 11:33:32 +00:00
|
|
|
oldConfig := *ci
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigFile := config.Data
|
|
|
|
oldReadLine := config.ReadLine
|
|
|
|
oldPassword := config.Password
|
2017-06-25 06:55:54 +00:00
|
|
|
os.Stdout = nil
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = path
|
2020-11-05 11:33:32 +00:00
|
|
|
ci = &fs.ConfigInfo{}
|
2019-06-10 16:58:47 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
configfile.LoadConfig(ctx)
|
|
|
|
assert.Equal(t, []string{}, config.Data.GetSectionList())
|
2019-06-10 16:58:47 +00: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 06:55:54 +00:00
|
|
|
os.Stdout = oldOsStdout
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ReadLine = oldReadLine
|
|
|
|
config.Password = oldPassword
|
2020-11-05 11:33:32 +00:00
|
|
|
*ci = oldConfig
|
2021-03-10 15:40:34 +00:00
|
|
|
config.Data = oldConfigFile
|
2019-06-22 16:25:12 +00:00
|
|
|
|
|
|
|
_ = os.Unsetenv("_RCLONE_CONFIG_KEY_FILE")
|
|
|
|
_ = os.Unsetenv("RCLONE_CONFIG_PASS")
|
2019-06-10 16:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-25 06:55:54 +00:00
|
|
|
|
2019-08-28 10:46:35 +00:00
|
|
|
// makeReadLine makes a simple readLine which returns a fixed list of
|
|
|
|
// strings
|
|
|
|
func makeReadLine(answers []string) func() string {
|
2017-06-25 06:55:54 +00:00
|
|
|
i := 0
|
2019-08-28 10:46:35 +00:00
|
|
|
return func() string {
|
2017-06-25 06:55:54 +00:00
|
|
|
i = i + 1
|
|
|
|
return answers[i-1]
|
|
|
|
}
|
2019-08-28 10:46:35 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
|
2019-08-28 10:46:35 +00:00
|
|
|
func TestCRUD(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx := context.Background()
|
2019-08-28 10:46:35 +00:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 10:46:35 +00:00
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"y", // type my own password
|
|
|
|
"secret", // password
|
|
|
|
"secret", // repeat
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 15:40:34 +00:00
|
|
|
config.NewRemote(ctx, "test")
|
2017-06-25 06:55:54 +00:00
|
|
|
|
2021-03-10 15:40:34 +00: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 16:30:54 +00:00
|
|
|
|
2017-06-25 06:55:54 +00:00
|
|
|
// normal rename, test → asdf
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 10:46:35 +00:00
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
})
|
2021-03-10 15:40:34 +00:00
|
|
|
config.RenameRemote("test")
|
2019-06-10 16:58:47 +00:00
|
|
|
|
2021-03-10 15:40:34 +00: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 06:55:54 +00:00
|
|
|
|
|
|
|
// delete remote
|
2021-03-10 15:40:34 +00:00
|
|
|
config.DeleteRemote("asdf")
|
|
|
|
assert.Equal(t, []string{}, config.Data.GetSectionList())
|
2017-06-25 06:55:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 10:46:35 +00:00
|
|
|
func TestChooseOption(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx := context.Background()
|
2019-08-28 10:46:35 +00:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 10:46:35 +00: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 15:40:34 +00:00
|
|
|
config.Password = func(bits int) (string, error) {
|
2019-08-28 10:46:35 +00:00
|
|
|
assert.Equal(t, 1024, bits)
|
|
|
|
return "not very random password", nil
|
|
|
|
}
|
2021-03-10 15:40:34 +00:00
|
|
|
config.NewRemote(ctx, "test")
|
2019-08-28 10:46:35 +00:00
|
|
|
|
2021-03-10 15:40:34 +00: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 10:46:35 +00:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-08-28 10:46:35 +00:00
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"n", // not required
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 15:40:34 +00:00
|
|
|
config.NewRemote(ctx, "test")
|
2019-08-28 10:46:35 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
assert.Equal(t, "true", config.FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "", config.FileGet("test", "pass"))
|
2019-08-28 10:46:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:39:33 +00:00
|
|
|
func TestNewRemoteName(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx := context.Background()
|
2019-11-05 12:39:33 +00:00
|
|
|
|
|
|
|
// script for creating remote
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-11-05 12:39:33 +00:00
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"n", // not required
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2021-03-10 15:40:34 +00:00
|
|
|
config.NewRemote(ctx, "test")
|
2019-11-05 12:39:33 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ReadLine = makeReadLine([]string{
|
2019-11-05 12:39:33 +00:00
|
|
|
"test", // already exists
|
|
|
|
"", // empty string not allowed
|
|
|
|
"bad@characters", // bad characters
|
|
|
|
"newname", // OK
|
|
|
|
})
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
assert.Equal(t, "newname", config.NewRemoteName())
|
2019-11-05 12:39:33 +00: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 00:17:24 +00:00
|
|
|
func TestCreateUpdatePasswordRemote(t *testing.T) {
|
2020-11-05 18:02:26 +00:00
|
|
|
ctx := context.Background()
|
2019-06-10 16:58:47 +00:00
|
|
|
defer testConfigFile(t, "update.conf")()
|
|
|
|
|
2020-05-12 13:24:53 +00: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 15:40:34 +00:00
|
|
|
require.NoError(t, config.CreateRemote(ctx, "test2", "config_test_remote", rc.Params{
|
2020-05-12 13:24:53 +00:00
|
|
|
"bool": true,
|
|
|
|
"pass": "potato",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
2021-03-10 15:40:34 +00: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 13:24:53 +00:00
|
|
|
if !noObscure {
|
|
|
|
gotPw = obscure.MustReveal(gotPw)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "potato", gotPw)
|
|
|
|
|
|
|
|
wantPw := obscure.MustObscure("potato2")
|
2021-03-10 15:40:34 +00:00
|
|
|
require.NoError(t, config.UpdateRemote(ctx, "test2", rc.Params{
|
2020-05-12 13:24:53 +00:00
|
|
|
"bool": false,
|
|
|
|
"pass": wantPw,
|
|
|
|
"spare": "spare",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
2021-03-10 15:40:34 +00: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 13:24:53 +00:00
|
|
|
if doObscure {
|
|
|
|
gotPw = obscure.MustReveal(gotPw)
|
|
|
|
}
|
|
|
|
assert.Equal(t, wantPw, gotPw)
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
require.NoError(t, config.PasswordRemote(ctx, "test2", rc.Params{
|
2020-05-12 13:24:53 +00:00
|
|
|
"pass": "potato3",
|
|
|
|
}))
|
|
|
|
|
2021-03-10 15:40:34 +00: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 13:24:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 16:58:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 11:04:43 +00:00
|
|
|
// Test some error cases
|
|
|
|
func TestReveal(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
wantErr string
|
|
|
|
}{
|
2017-10-19 11:36:12 +00: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 11:04:43 +00:00
|
|
|
} {
|
2018-01-18 20:19:55 +00:00
|
|
|
gotString, gotErr := obscure.Reveal(test.in)
|
2016-08-14 11:04:43 +00:00
|
|
|
assert.Equal(t, "", gotString)
|
|
|
|
assert.Equal(t, test.wantErr, gotErr.Error())
|
2015-09-01 21:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
func TestConfigLoad(t *testing.T) {
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigPath := config.ConfigPath
|
|
|
|
config.ConfigPath = "./testdata/plain.conf"
|
2016-02-29 21:43:37 +00:00
|
|
|
defer func() {
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = oldConfigPath
|
2016-02-29 21:43:37 +00:00
|
|
|
}()
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ClearConfigPassword()
|
|
|
|
configfile.LoadConfig(context.Background())
|
|
|
|
sections := config.Data.GetSectionList()
|
2016-02-16 15:25:27 +00:00
|
|
|
var expect = []string{"RCLONE_ENCRYPT_V0", "nounc", "unc"}
|
2016-06-29 16:59:31 +00:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
keys := config.Data.GetKeyList("nounc")
|
2016-02-16 15:25:27 +00:00
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-29 16:59:31 +00:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncrypted(t *testing.T) {
|
|
|
|
var err error
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigPath := config.ConfigPath
|
|
|
|
config.ConfigPath = "./testdata/encrypted.conf"
|
2016-02-29 21:43:37 +00:00
|
|
|
defer func() {
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ClearConfigPassword()
|
2016-02-29 21:43:37 +00:00
|
|
|
}()
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// Set correct password
|
2021-03-10 15:40:34 +00:00
|
|
|
err = config.SetConfigPassword("asdf")
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-10 15:40:34 +00:00
|
|
|
err = config.Data.Load()
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-10 15:40:34 +00:00
|
|
|
sections := config.Data.GetSectionList()
|
2016-02-16 15:25:27 +00:00
|
|
|
var expect = []string{"nounc", "unc"}
|
2016-06-29 16:59:31 +00:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
keys := config.Data.GetKeyList("nounc")
|
2016-02-16 15:25:27 +00:00
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-29 16:59:31 +00:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 09:55:27 +00:00
|
|
|
func TestConfigLoadEncryptedWithValidPassCommand(t *testing.T) {
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigPath := config.ConfigPath
|
2020-11-05 11:33:32 +00:00
|
|
|
oldConfig := *ci
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = "./testdata/encrypted.conf"
|
2020-11-05 11:33:32 +00:00
|
|
|
// using ci.PasswordCommand, correct password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf"}
|
2019-11-18 09:55:27 +00:00
|
|
|
defer func() {
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ClearConfigPassword()
|
2020-11-05 11:33:32 +00:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 09:55:27 +00:00
|
|
|
}()
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ClearConfigPassword()
|
2019-11-18 09:55:27 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
err := config.Data.Load()
|
2019-11-18 09:55:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
sections := config.Data.GetSectionList()
|
2020-01-23 13:54:18 +00:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
keys := config.Data.GetKeyList("nounc")
|
2020-01-23 13:54:18 +00:00
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
2019-11-18 09:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncryptedWithInvalidPassCommand(t *testing.T) {
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigPath := config.ConfigPath
|
2020-11-05 11:33:32 +00:00
|
|
|
oldConfig := *ci
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = "./testdata/encrypted.conf"
|
2020-11-05 11:33:32 +00:00
|
|
|
// using ci.PasswordCommand, incorrect password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf-blurfl"}
|
2019-11-18 09:55:27 +00:00
|
|
|
defer func() {
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = oldConfigPath
|
|
|
|
config.ClearConfigPassword()
|
2020-11-05 11:33:32 +00:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 09:55:27 +00:00
|
|
|
}()
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ClearConfigPassword()
|
2019-11-18 09:55:27 +00:00
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
err := config.Data.Load()
|
2020-01-23 13:54:18 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "using --password-command derived password")
|
2019-11-18 09:55:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 15:25:27 +00:00
|
|
|
func TestConfigLoadEncryptedFailures(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// This file should be too short to be decoded.
|
2021-03-10 15:40:34 +00:00
|
|
|
oldConfigPath := config.ConfigPath
|
|
|
|
config.ConfigPath = "./testdata/enc-short.conf"
|
|
|
|
defer func() { config.ConfigPath = oldConfigPath }()
|
|
|
|
err = config.Data.Load()
|
2016-06-29 16:59:31 +00:00
|
|
|
require.Error(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = "./testdata/enc-invalid.conf"
|
|
|
|
err = config.Data.Load()
|
2016-06-29 16:59:31 +00:00
|
|
|
require.Error(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = "./testdata/enc-too-new.conf"
|
|
|
|
err = config.Data.Load()
|
2016-06-29 16:59:31 +00:00
|
|
|
require.Error(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
2016-12-20 15:05:08 +00:00
|
|
|
// This file does not exist.
|
2021-03-10 15:40:34 +00:00
|
|
|
config.ConfigPath = "./testdata/filenotfound.conf"
|
|
|
|
err = config.Data.Load()
|
|
|
|
assert.Equal(t, config.ErrorConfigFileNotFound, err)
|
2018-04-13 15:06:37 +00:00
|
|
|
}
|
2019-06-19 12:51:19 +00:00
|
|
|
|
|
|
|
func TestFileRefresh(t *testing.T) {
|
2020-11-05 18:02:26 +00:00
|
|
|
ctx := context.Background()
|
2019-06-19 12:51:19 +00:00
|
|
|
defer testConfigFile(t, "refresh.conf")()
|
2021-03-10 15:40:34 +00:00
|
|
|
require.NoError(t, config.CreateRemote(ctx, "refresh_test", "config_test_remote", rc.Params{
|
2019-06-19 12:51:19 +00:00
|
|
|
"bool": true,
|
2020-05-12 13:24:53 +00:00
|
|
|
}, false, false))
|
2021-03-10 15:40:34 +00:00
|
|
|
b, err := ioutil.ReadFile(config.ConfigPath)
|
2019-06-19 12:51:19 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b = bytes.Replace(b, []byte("refresh_test"), []byte("refreshed_test"), 1)
|
2021-03-10 15:40:34 +00:00
|
|
|
err = ioutil.WriteFile(config.ConfigPath, b, 0644)
|
2019-06-19 12:51:19 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
assert.NotEqual(t, []string{"refreshed_test"}, config.Data.GetSectionList())
|
|
|
|
err = config.FileRefresh()
|
2019-06-19 12:51:19 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-10 15:40:34 +00:00
|
|
|
assert.Equal(t, []string{"refreshed_test"}, config.Data.GetSectionList())
|
2019-06-19 12:51:19 +00:00
|
|
|
}
|