2018-01-12 16:30:54 +00:00
|
|
|
package config
|
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"
|
|
|
|
"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)
|
2017-06-25 06:55:54 +00:00
|
|
|
configKey = nil // reset password
|
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
|
2018-01-12 16:30:54 +00:00
|
|
|
oldConfigPath := ConfigPath
|
2020-11-05 11:33:32 +00:00
|
|
|
oldConfig := *ci
|
2020-11-07 21:05:58 +00:00
|
|
|
oldConfigFile := Data
|
2017-06-25 06:55:54 +00:00
|
|
|
oldReadLine := ReadLine
|
2019-08-28 10:46:35 +00:00
|
|
|
oldPassword := Password
|
2017-06-25 06:55:54 +00:00
|
|
|
os.Stdout = nil
|
2018-01-12 16:30:54 +00:00
|
|
|
ConfigPath = path
|
2020-11-05 11:33:32 +00:00
|
|
|
ci = &fs.ConfigInfo{}
|
2020-11-07 21:05:58 +00:00
|
|
|
Data = nil
|
2019-06-10 16:58:47 +00:00
|
|
|
|
2020-11-05 16:59:59 +00:00
|
|
|
LoadConfig(ctx)
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{}, 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
|
2018-01-12 16:30:54 +00:00
|
|
|
ConfigPath = oldConfigPath
|
2017-06-25 06:55:54 +00:00
|
|
|
ReadLine = oldReadLine
|
2019-08-28 10:46:35 +00:00
|
|
|
Password = oldPassword
|
2020-11-05 11:33:32 +00:00
|
|
|
*ci = oldConfig
|
2020-11-07 21:05:58 +00:00
|
|
|
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
|
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"y", // type my own password
|
|
|
|
"secret", // password
|
|
|
|
"secret", // repeat
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2020-11-05 11:33:32 +00:00
|
|
|
NewRemote(ctx, "test")
|
2017-06-25 06:55:54 +00:00
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{"test"}, Data.GetSectionList())
|
2019-06-10 16:58:47 +00:00
|
|
|
assert.Equal(t, "config_test_remote", FileGet("test", "type"))
|
|
|
|
assert.Equal(t, "true", FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "secret", obscure.MustReveal(FileGet("test", "pass")))
|
2018-01-12 16:30:54 +00:00
|
|
|
|
2017-06-25 06:55:54 +00:00
|
|
|
// normal rename, test → asdf
|
2019-08-28 10:46:35 +00:00
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
})
|
2017-06-25 06:55:54 +00:00
|
|
|
RenameRemote("test")
|
2019-06-10 16:58:47 +00:00
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{"asdf"}, Data.GetSectionList())
|
2019-06-10 16:58:47 +00:00
|
|
|
assert.Equal(t, "config_test_remote", FileGet("asdf", "type"))
|
|
|
|
assert.Equal(t, "true", FileGet("asdf", "bool"))
|
|
|
|
assert.Equal(t, "secret", obscure.MustReveal(FileGet("asdf", "pass")))
|
2017-06-25 06:55:54 +00:00
|
|
|
|
|
|
|
// delete remote
|
|
|
|
DeleteRemote("asdf")
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{}, 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
|
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"config_test_remote", // type
|
|
|
|
"false", // bool value
|
|
|
|
"x", // bad choice
|
|
|
|
"g", // generate password
|
|
|
|
"1024", // very big
|
|
|
|
"y", // password OK
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
|
|
|
Password = func(bits int) (string, error) {
|
|
|
|
assert.Equal(t, 1024, bits)
|
|
|
|
return "not very random password", nil
|
|
|
|
}
|
2020-11-05 11:33:32 +00:00
|
|
|
NewRemote(ctx, "test")
|
2019-08-28 10:46:35 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "false", FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "not very random password", obscure.MustReveal(FileGet("test", "pass")))
|
|
|
|
|
|
|
|
// script for creating remote
|
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"n", // not required
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2020-11-05 11:33:32 +00:00
|
|
|
NewRemote(ctx, "test")
|
2019-08-28 10:46:35 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "true", FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "", FileGet("test", "pass"))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"config_test_remote", // type
|
|
|
|
"true", // bool value
|
|
|
|
"n", // not required
|
|
|
|
"y", // looks good, save
|
|
|
|
})
|
2020-11-05 11:33:32 +00:00
|
|
|
NewRemote(ctx, "test")
|
2019-11-05 12:39:33 +00:00
|
|
|
|
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"test", // already exists
|
|
|
|
"", // empty string not allowed
|
|
|
|
"bad@characters", // bad characters
|
|
|
|
"newname", // OK
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(t, "newname", NewRemoteName())
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-11-05 18:02:26 +00:00
|
|
|
require.NoError(t, CreateRemote(ctx, "test2", "config_test_remote", rc.Params{
|
2020-05-12 13:24:53 +00:00
|
|
|
"bool": true,
|
|
|
|
"pass": "potato",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{"test2"}, Data.GetSectionList())
|
2020-05-12 13:24:53 +00:00
|
|
|
assert.Equal(t, "config_test_remote", FileGet("test2", "type"))
|
|
|
|
assert.Equal(t, "true", FileGet("test2", "bool"))
|
|
|
|
gotPw := FileGet("test2", "pass")
|
|
|
|
if !noObscure {
|
|
|
|
gotPw = obscure.MustReveal(gotPw)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "potato", gotPw)
|
|
|
|
|
|
|
|
wantPw := obscure.MustObscure("potato2")
|
2020-11-05 18:02:26 +00:00
|
|
|
require.NoError(t, UpdateRemote(ctx, "test2", rc.Params{
|
2020-05-12 13:24:53 +00:00
|
|
|
"bool": false,
|
|
|
|
"pass": wantPw,
|
|
|
|
"spare": "spare",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{"test2"}, Data.GetSectionList())
|
2020-05-12 13:24:53 +00:00
|
|
|
assert.Equal(t, "config_test_remote", FileGet("test2", "type"))
|
|
|
|
assert.Equal(t, "false", FileGet("test2", "bool"))
|
|
|
|
gotPw = FileGet("test2", "pass")
|
|
|
|
if doObscure {
|
|
|
|
gotPw = obscure.MustReveal(gotPw)
|
|
|
|
}
|
|
|
|
assert.Equal(t, wantPw, gotPw)
|
|
|
|
|
2020-11-05 18:02:26 +00:00
|
|
|
require.NoError(t, PasswordRemote(ctx, "test2", rc.Params{
|
2020-05-12 13:24:53 +00:00
|
|
|
"pass": "potato3",
|
|
|
|
}))
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{"test2"}, Data.GetSectionList())
|
2020-05-12 13:24:53 +00:00
|
|
|
assert.Equal(t, "config_test_remote", FileGet("test2", "type"))
|
|
|
|
assert.Equal(t, "false", FileGet("test2", "bool"))
|
|
|
|
assert.Equal(t, "potato3", obscure.MustReveal(FileGet("test2", "pass")))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-02-29 21:43:37 +00:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 15:25:27 +00:00
|
|
|
ConfigPath = "./testdata/plain.conf"
|
2016-02-29 21:43:37 +00:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
}()
|
|
|
|
configKey = nil // reset password
|
2020-11-07 21:05:58 +00:00
|
|
|
err := Data.Load()
|
2016-02-16 15:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-07 21:05:58 +00:00
|
|
|
sections := 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
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
keys := 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
|
2016-02-29 21:43:37 +00:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 15:25:27 +00:00
|
|
|
ConfigPath = "./testdata/encrypted.conf"
|
2016-02-29 21:43:37 +00:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
|
|
|
}()
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// Set correct password
|
2016-08-14 16:16:06 +00:00
|
|
|
err = setConfigPassword("asdf")
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-07 21:05:58 +00:00
|
|
|
err = Data.Load()
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-07 21:05:58 +00:00
|
|
|
sections := 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
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
keys := 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)
|
2019-11-18 09:55:27 +00:00
|
|
|
oldConfigPath := ConfigPath
|
2020-11-05 11:33:32 +00:00
|
|
|
oldConfig := *ci
|
2019-11-18 09:55:27 +00:00
|
|
|
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() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
2020-11-05 11:33:32 +00:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 09:55:27 +00:00
|
|
|
}()
|
|
|
|
|
2020-01-23 13:54:18 +00:00
|
|
|
configKey = nil // reset password
|
2019-11-18 09:55:27 +00:00
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
err := Data.Load()
|
2019-11-18 09:55:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
sections := Data.GetSectionList()
|
2020-01-23 13:54:18 +00:00
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
keys := 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)
|
2019-11-18 09:55:27 +00:00
|
|
|
oldConfigPath := ConfigPath
|
2020-11-05 11:33:32 +00:00
|
|
|
oldConfig := *ci
|
2019-11-18 09:55:27 +00:00
|
|
|
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() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
2020-11-05 11:33:32 +00:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 09:55:27 +00:00
|
|
|
}()
|
|
|
|
|
2020-01-23 13:54:18 +00:00
|
|
|
configKey = nil // reset password
|
2019-11-18 09:55:27 +00:00
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
err := 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.
|
2016-02-29 21:43:37 +00:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 15:25:27 +00:00
|
|
|
ConfigPath = "./testdata/enc-short.conf"
|
2016-02-29 21:43:37 +00:00
|
|
|
defer func() { ConfigPath = oldConfigPath }()
|
2020-11-07 21:05:58 +00:00
|
|
|
err = 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.
|
|
|
|
ConfigPath = "./testdata/enc-invalid.conf"
|
2020-11-07 21:05:58 +00:00
|
|
|
err = 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.
|
|
|
|
ConfigPath = "./testdata/enc-too-new.conf"
|
2020-11-07 21:05:58 +00:00
|
|
|
err = 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.
|
2016-02-16 15:25:27 +00:00
|
|
|
ConfigPath = "./testdata/filenotfound.conf"
|
2020-11-07 21:05:58 +00:00
|
|
|
err = Data.Load()
|
|
|
|
assert.Equal(t, ErrorConfigFileNotFound, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword(t *testing.T) {
|
2016-02-29 21:43:37 +00:00
|
|
|
defer func() {
|
|
|
|
configKey = nil // reset password
|
|
|
|
}()
|
2016-02-16 15:25:27 +00:00
|
|
|
var err error
|
|
|
|
// Empty password should give error
|
2016-08-14 16:16:06 +00:00
|
|
|
err = setConfigPassword(" \t ")
|
2016-06-29 16:59:31 +00:00
|
|
|
require.Error(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// Test invalid utf8 sequence
|
2016-08-14 16:16:06 +00:00
|
|
|
err = setConfigPassword(string([]byte{0xff, 0xfe, 0xfd}) + "abc")
|
2016-06-29 16:59:31 +00:00
|
|
|
require.Error(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
|
|
|
|
// Simple check of wrong passwords
|
|
|
|
hashedKeyCompare(t, "mis", "match", false)
|
|
|
|
|
|
|
|
// Check that passwords match after unicode normalization
|
|
|
|
hashedKeyCompare(t, "ff\u0041\u030A", "ffÅ", true)
|
|
|
|
|
|
|
|
// Check that passwords preserves case
|
|
|
|
hashedKeyCompare(t, "abcdef", "ABCDEF", false)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashedKeyCompare(t *testing.T, a, b string, shouldMatch bool) {
|
2016-08-14 16:16:06 +00:00
|
|
|
err := setConfigPassword(a)
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
k1 := configKey
|
|
|
|
|
2016-08-14 16:16:06 +00:00
|
|
|
err = setConfigPassword(b)
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
k2 := configKey
|
2016-06-29 16:59:31 +00:00
|
|
|
|
|
|
|
if shouldMatch {
|
|
|
|
assert.Equal(t, k1, k2)
|
|
|
|
} else {
|
|
|
|
assert.NotEqual(t, k1, k2)
|
2016-02-16 15:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 15:06:37 +00:00
|
|
|
|
|
|
|
func TestMatchProvider(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
config string
|
|
|
|
provider string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"", "", true},
|
|
|
|
{"one", "one", true},
|
|
|
|
{"one,two", "two", true},
|
|
|
|
{"one,two,three", "two", true},
|
|
|
|
{"one", "on", false},
|
|
|
|
{"one,two,three", "tw", false},
|
|
|
|
{"!one,two,three", "two", false},
|
|
|
|
{"!one,two,three", "four", true},
|
|
|
|
} {
|
|
|
|
what := fmt.Sprintf("%q,%q", test.config, test.provider)
|
|
|
|
got := matchProvider(test.config, test.provider)
|
|
|
|
assert.Equal(t, test.want, got, what)
|
|
|
|
}
|
|
|
|
}
|
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")()
|
2020-11-05 18:02:26 +00:00
|
|
|
require.NoError(t, 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))
|
2019-06-19 12:51:19 +00:00
|
|
|
b, err := ioutil.ReadFile(ConfigPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b = bytes.Replace(b, []byte("refresh_test"), []byte("refreshed_test"), 1)
|
|
|
|
err = ioutil.WriteFile(ConfigPath, b, 0644)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.NotEqual(t, []string{"refreshed_test"}, Data.GetSectionList())
|
2019-06-19 12:51:19 +00:00
|
|
|
err = FileRefresh()
|
|
|
|
assert.NoError(t, err)
|
2020-11-07 21:05:58 +00:00
|
|
|
assert.Equal(t, []string{"refreshed_test"}, Data.GetSectionList())
|
2019-06-19 12:51:19 +00:00
|
|
|
}
|