2018-01-13 00:30:54 +08:00
|
|
|
package config
|
2015-02-20 03:26:00 +08:00
|
|
|
|
2016-02-16 23:25:27 +08:00
|
|
|
import (
|
2019-06-19 20:51:19 +08:00
|
|
|
"bytes"
|
2020-11-06 00:59:59 +08:00
|
|
|
"context"
|
2018-04-13 23:06:37 +08:00
|
|
|
"fmt"
|
2017-06-25 14:55:54 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-02-16 23:25:27 +08:00
|
|
|
"testing"
|
2016-06-30 00:59:31 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2016-06-30 00:59:31 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-16 23:25:27 +08:00
|
|
|
)
|
2015-02-20 03:26:00 +08:00
|
|
|
|
2019-06-11 00:58:47 +08:00
|
|
|
func testConfigFile(t *testing.T, configFileName string) func() {
|
2020-11-06 00:59:59 +08:00
|
|
|
ctx := context.Background()
|
2020-11-05 19:33:32 +08:00
|
|
|
ci := fs.GetConfig(ctx)
|
2017-06-25 14:55:54 +08:00
|
|
|
configKey = nil // reset password
|
2019-06-23 00:25:12 +08:00
|
|
|
_ = os.Unsetenv("_RCLONE_CONFIG_KEY_FILE")
|
|
|
|
_ = os.Unsetenv("RCLONE_CONFIG_PASS")
|
2017-06-25 14:55:54 +08:00
|
|
|
// create temp config file
|
2019-06-11 00:58:47 +08:00
|
|
|
tempFile, err := ioutil.TempFile("", configFileName)
|
2017-06-25 14:55:54 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
path := tempFile.Name()
|
|
|
|
assert.NoError(t, tempFile.Close())
|
|
|
|
|
|
|
|
// temporarily adapt configuration
|
|
|
|
oldOsStdout := os.Stdout
|
2018-01-13 00:30:54 +08:00
|
|
|
oldConfigPath := ConfigPath
|
2020-11-05 19:33:32 +08:00
|
|
|
oldConfig := *ci
|
2018-03-17 19:36:30 +08:00
|
|
|
oldConfigFile := configFile
|
2017-06-25 14:55:54 +08:00
|
|
|
oldReadLine := ReadLine
|
2019-08-28 18:46:35 +08:00
|
|
|
oldPassword := Password
|
2017-06-25 14:55:54 +08:00
|
|
|
os.Stdout = nil
|
2018-01-13 00:30:54 +08:00
|
|
|
ConfigPath = path
|
2020-11-05 19:33:32 +08:00
|
|
|
ci = &fs.ConfigInfo{}
|
2018-03-17 19:36:30 +08:00
|
|
|
configFile = nil
|
2019-06-11 00:58:47 +08:00
|
|
|
|
2020-11-06 00:59:59 +08:00
|
|
|
LoadConfig(ctx)
|
2019-06-11 00:58:47 +08:00
|
|
|
assert.Equal(t, []string{}, getConfigData().GetSectionList())
|
|
|
|
|
|
|
|
// 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 14:55:54 +08:00
|
|
|
os.Stdout = oldOsStdout
|
2018-01-13 00:30:54 +08:00
|
|
|
ConfigPath = oldConfigPath
|
2017-06-25 14:55:54 +08:00
|
|
|
ReadLine = oldReadLine
|
2019-08-28 18:46:35 +08:00
|
|
|
Password = oldPassword
|
2020-11-05 19:33:32 +08:00
|
|
|
*ci = oldConfig
|
2018-03-17 19:36:30 +08:00
|
|
|
configFile = oldConfigFile
|
2019-06-23 00:25:12 +08:00
|
|
|
|
|
|
|
_ = os.Unsetenv("_RCLONE_CONFIG_KEY_FILE")
|
|
|
|
_ = os.Unsetenv("RCLONE_CONFIG_PASS")
|
2019-06-11 00:58:47 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-25 14:55:54 +08:00
|
|
|
|
2019-08-28 18:46:35 +08:00
|
|
|
// makeReadLine makes a simple readLine which returns a fixed list of
|
|
|
|
// strings
|
|
|
|
func makeReadLine(answers []string) func() string {
|
2017-06-25 14:55:54 +08:00
|
|
|
i := 0
|
2019-08-28 18:46:35 +08:00
|
|
|
return func() string {
|
2017-06-25 14:55:54 +08:00
|
|
|
i = i + 1
|
|
|
|
return answers[i-1]
|
|
|
|
}
|
2019-08-28 18:46:35 +08:00
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
|
2019-08-28 18:46:35 +08:00
|
|
|
func TestCRUD(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 19:33:32 +08:00
|
|
|
ctx := context.Background()
|
2019-08-28 18:46:35 +08: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 19:33:32 +08:00
|
|
|
NewRemote(ctx, "test")
|
2017-06-25 14:55:54 +08:00
|
|
|
|
2019-06-11 00:58:47 +08:00
|
|
|
assert.Equal(t, []string{"test"}, configFile.GetSectionList())
|
|
|
|
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-13 00:30:54 +08:00
|
|
|
|
2017-06-25 14:55:54 +08:00
|
|
|
// normal rename, test → asdf
|
2019-08-28 18:46:35 +08:00
|
|
|
ReadLine = makeReadLine([]string{
|
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
"asdf",
|
|
|
|
})
|
2017-06-25 14:55:54 +08:00
|
|
|
RenameRemote("test")
|
2019-06-11 00:58:47 +08:00
|
|
|
|
2018-03-17 19:36:30 +08:00
|
|
|
assert.Equal(t, []string{"asdf"}, configFile.GetSectionList())
|
2019-06-11 00:58:47 +08: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 14:55:54 +08:00
|
|
|
|
|
|
|
// delete remote
|
|
|
|
DeleteRemote("asdf")
|
2018-03-17 19:36:30 +08:00
|
|
|
assert.Equal(t, []string{}, configFile.GetSectionList())
|
2017-06-25 14:55:54 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 18:46:35 +08:00
|
|
|
func TestChooseOption(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 19:33:32 +08:00
|
|
|
ctx := context.Background()
|
2019-08-28 18:46:35 +08: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 19:33:32 +08:00
|
|
|
NewRemote(ctx, "test")
|
2019-08-28 18:46:35 +08: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 19:33:32 +08:00
|
|
|
NewRemote(ctx, "test")
|
2019-08-28 18:46:35 +08:00
|
|
|
|
|
|
|
assert.Equal(t, "true", FileGet("test", "bool"))
|
|
|
|
assert.Equal(t, "", FileGet("test", "pass"))
|
|
|
|
}
|
|
|
|
|
2019-11-05 20:39:33 +08:00
|
|
|
func TestNewRemoteName(t *testing.T) {
|
|
|
|
defer testConfigFile(t, "crud.conf")()
|
2020-11-05 19:33:32 +08:00
|
|
|
ctx := context.Background()
|
2019-11-05 20:39:33 +08: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 19:33:32 +08:00
|
|
|
NewRemote(ctx, "test")
|
2019-11-05 20:39:33 +08: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 08:17:24 +08:00
|
|
|
func TestCreateUpdatePasswordRemote(t *testing.T) {
|
2020-11-06 02:02:26 +08:00
|
|
|
ctx := context.Background()
|
2019-06-11 00:58:47 +08:00
|
|
|
defer testConfigFile(t, "update.conf")()
|
|
|
|
|
2020-05-12 21:24:53 +08: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-06 02:02:26 +08:00
|
|
|
require.NoError(t, CreateRemote(ctx, "test2", "config_test_remote", rc.Params{
|
2020-05-12 21:24:53 +08:00
|
|
|
"bool": true,
|
|
|
|
"pass": "potato",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"test2"}, configFile.GetSectionList())
|
|
|
|
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-06 02:02:26 +08:00
|
|
|
require.NoError(t, UpdateRemote(ctx, "test2", rc.Params{
|
2020-05-12 21:24:53 +08:00
|
|
|
"bool": false,
|
|
|
|
"pass": wantPw,
|
|
|
|
"spare": "spare",
|
|
|
|
}, doObscure, noObscure))
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"test2"}, configFile.GetSectionList())
|
|
|
|
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-06 02:02:26 +08:00
|
|
|
require.NoError(t, PasswordRemote(ctx, "test2", rc.Params{
|
2020-05-12 21:24:53 +08:00
|
|
|
"pass": "potato3",
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"test2"}, configFile.GetSectionList())
|
|
|
|
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-11 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:04:43 +08:00
|
|
|
// Test some error cases
|
|
|
|
func TestReveal(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
wantErr string
|
|
|
|
}{
|
2017-10-19 19:36:12 +08: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 19:04:43 +08:00
|
|
|
} {
|
2018-01-19 04:19:55 +08:00
|
|
|
gotString, gotErr := obscure.Reveal(test.in)
|
2016-08-14 19:04:43 +08:00
|
|
|
assert.Equal(t, "", gotString)
|
|
|
|
assert.Equal(t, test.wantErr, gotErr.Error())
|
2015-09-02 05:33:34 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
func TestConfigLoad(t *testing.T) {
|
2016-03-01 05:43:37 +08:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 23:25:27 +08:00
|
|
|
ConfigPath = "./testdata/plain.conf"
|
2016-03-01 05:43:37 +08:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
}()
|
|
|
|
configKey = nil // reset password
|
2016-02-16 23:25:27 +08:00
|
|
|
c, err := loadConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sections := c.GetSectionList()
|
|
|
|
var expect = []string{"RCLONE_ENCRYPT_V0", "nounc", "unc"}
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 23:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncrypted(t *testing.T) {
|
|
|
|
var err error
|
2016-03-01 05:43:37 +08:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 23:25:27 +08:00
|
|
|
ConfigPath = "./testdata/encrypted.conf"
|
2016-03-01 05:43:37 +08:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
|
|
|
}()
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
// Set correct password
|
2016-08-15 00:16:06 +08:00
|
|
|
err = setConfigPassword("asdf")
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
c, err := loadConfigFile()
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
sections := c.GetSectionList()
|
|
|
|
var expect = []string{"nounc", "unc"}
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 23:25:27 +08:00
|
|
|
}
|
|
|
|
|
2019-11-18 17:55:27 +08:00
|
|
|
func TestConfigLoadEncryptedWithValidPassCommand(t *testing.T) {
|
2020-11-05 19:33:32 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2019-11-18 17:55:27 +08:00
|
|
|
oldConfigPath := ConfigPath
|
2020-11-05 19:33:32 +08:00
|
|
|
oldConfig := *ci
|
2019-11-18 17:55:27 +08:00
|
|
|
ConfigPath = "./testdata/encrypted.conf"
|
2020-11-05 19:33:32 +08:00
|
|
|
// using ci.PasswordCommand, correct password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf"}
|
2019-11-18 17:55:27 +08:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
2020-11-05 19:33:32 +08:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 17:55:27 +08:00
|
|
|
}()
|
|
|
|
|
2020-01-23 21:54:18 +08:00
|
|
|
configKey = nil // reset password
|
2019-11-18 17:55:27 +08:00
|
|
|
|
2020-01-23 21:54:18 +08:00
|
|
|
c, err := loadConfigFile()
|
2019-11-18 17:55:27 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-23 21:54:18 +08:00
|
|
|
sections := c.GetSectionList()
|
|
|
|
var expect = []string{"nounc", "unc"}
|
|
|
|
assert.Equal(t, expect, sections)
|
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
expect = []string{"type", "nounc"}
|
|
|
|
assert.Equal(t, expect, keys)
|
2019-11-18 17:55:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncryptedWithInvalidPassCommand(t *testing.T) {
|
2020-11-05 19:33:32 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2019-11-18 17:55:27 +08:00
|
|
|
oldConfigPath := ConfigPath
|
2020-11-05 19:33:32 +08:00
|
|
|
oldConfig := *ci
|
2019-11-18 17:55:27 +08:00
|
|
|
ConfigPath = "./testdata/encrypted.conf"
|
2020-11-05 19:33:32 +08:00
|
|
|
// using ci.PasswordCommand, incorrect password
|
|
|
|
ci.PasswordCommand = fs.SpaceSepList{"echo", "asdf-blurfl"}
|
2019-11-18 17:55:27 +08:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
2020-11-05 19:33:32 +08:00
|
|
|
*ci = oldConfig
|
|
|
|
ci.PasswordCommand = nil
|
2019-11-18 17:55:27 +08:00
|
|
|
}()
|
|
|
|
|
2020-01-23 21:54:18 +08:00
|
|
|
configKey = nil // reset password
|
2019-11-18 17:55:27 +08:00
|
|
|
|
2020-01-23 21:54:18 +08:00
|
|
|
_, err := loadConfigFile()
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "using --password-command derived password")
|
2019-11-18 17:55:27 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 23:25:27 +08:00
|
|
|
func TestConfigLoadEncryptedFailures(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// This file should be too short to be decoded.
|
2016-03-01 05:43:37 +08:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 23:25:27 +08:00
|
|
|
ConfigPath = "./testdata/enc-short.conf"
|
2016-03-01 05:43:37 +08:00
|
|
|
defer func() { ConfigPath = oldConfigPath }()
|
2016-02-16 23:25:27 +08:00
|
|
|
_, err = loadConfigFile()
|
2016-06-30 00:59:31 +08:00
|
|
|
require.Error(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
|
|
|
ConfigPath = "./testdata/enc-invalid.conf"
|
|
|
|
_, err = loadConfigFile()
|
2016-06-30 00:59:31 +08:00
|
|
|
require.Error(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
|
|
|
ConfigPath = "./testdata/enc-too-new.conf"
|
|
|
|
_, err = loadConfigFile()
|
2016-06-30 00:59:31 +08:00
|
|
|
require.Error(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
|
2016-12-20 23:05:08 +08:00
|
|
|
// This file does not exist.
|
2016-02-16 23:25:27 +08:00
|
|
|
ConfigPath = "./testdata/filenotfound.conf"
|
|
|
|
c, err := loadConfigFile()
|
2016-12-20 23:05:08 +08:00
|
|
|
assert.Equal(t, errorConfigFileNotFound, err)
|
|
|
|
assert.Nil(t, c)
|
2016-02-16 23:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword(t *testing.T) {
|
2016-03-01 05:43:37 +08:00
|
|
|
defer func() {
|
|
|
|
configKey = nil // reset password
|
|
|
|
}()
|
2016-02-16 23:25:27 +08:00
|
|
|
var err error
|
|
|
|
// Empty password should give error
|
2016-08-15 00:16:06 +08:00
|
|
|
err = setConfigPassword(" \t ")
|
2016-06-30 00:59:31 +08:00
|
|
|
require.Error(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
|
|
|
|
// Test invalid utf8 sequence
|
2016-08-15 00:16:06 +08:00
|
|
|
err = setConfigPassword(string([]byte{0xff, 0xfe, 0xfd}) + "abc")
|
2016-06-30 00:59:31 +08:00
|
|
|
require.Error(t, err)
|
2016-02-16 23:25:27 +08: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-15 00:16:06 +08:00
|
|
|
err := setConfigPassword(a)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
k1 := configKey
|
|
|
|
|
2016-08-15 00:16:06 +08:00
|
|
|
err = setConfigPassword(b)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
k2 := configKey
|
2016-06-30 00:59:31 +08:00
|
|
|
|
|
|
|
if shouldMatch {
|
|
|
|
assert.Equal(t, k1, k2)
|
|
|
|
} else {
|
|
|
|
assert.NotEqual(t, k1, k2)
|
2016-02-16 23:25:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 23:06:37 +08: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 20:51:19 +08:00
|
|
|
|
|
|
|
func TestFileRefresh(t *testing.T) {
|
2020-11-06 02:02:26 +08:00
|
|
|
ctx := context.Background()
|
2019-06-19 20:51:19 +08:00
|
|
|
defer testConfigFile(t, "refresh.conf")()
|
2020-11-06 02:02:26 +08:00
|
|
|
require.NoError(t, CreateRemote(ctx, "refresh_test", "config_test_remote", rc.Params{
|
2019-06-19 20:51:19 +08:00
|
|
|
"bool": true,
|
2020-05-12 21:24:53 +08:00
|
|
|
}, false, false))
|
2019-06-19 20:51:19 +08: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)
|
|
|
|
|
|
|
|
assert.NotEqual(t, []string{"refreshed_test"}, configFile.GetSectionList())
|
|
|
|
err = FileRefresh()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"refreshed_test"}, configFile.GetSectionList())
|
|
|
|
}
|