2015-02-19 19:26:00 +00:00
|
|
|
package fs
|
|
|
|
|
2016-02-16 15:25:27 +00:00
|
|
|
import (
|
2016-08-14 11:04:43 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
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
|
|
|
|
|
|
|
"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
|
|
|
|
2016-08-14 11:04:43 +00:00
|
|
|
func TestObscure(t *testing.T) {
|
2015-09-01 21:33:34 +00:00
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want string
|
2016-08-14 11:04:43 +00:00
|
|
|
iv string
|
2015-09-01 21:33:34 +00:00
|
|
|
}{
|
2016-08-14 11:04:43 +00:00
|
|
|
{"", "YWFhYWFhYWFhYWFhYWFhYQ", "aaaaaaaaaaaaaaaa"},
|
|
|
|
{"potato", "YWFhYWFhYWFhYWFhYWFhYXMaGgIlEQ", "aaaaaaaaaaaaaaaa"},
|
|
|
|
{"potato", "YmJiYmJiYmJiYmJiYmJiYp3gcEWbAw", "bbbbbbbbbbbbbbbb"},
|
2015-09-01 21:33:34 +00:00
|
|
|
} {
|
2016-08-14 11:04:43 +00:00
|
|
|
cryptRand = bytes.NewBufferString(test.iv)
|
|
|
|
got, err := Obscure(test.in)
|
|
|
|
cryptRand = rand.Reader
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.want, got)
|
|
|
|
recoveredIn, err := Reveal(got)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.in, recoveredIn, "not bidirectional")
|
|
|
|
// Now the Must variants
|
|
|
|
cryptRand = bytes.NewBufferString(test.iv)
|
|
|
|
got = MustObscure(test.in)
|
|
|
|
cryptRand = rand.Reader
|
2016-06-29 16:59:31 +00:00
|
|
|
assert.Equal(t, test.want, got)
|
2016-08-14 11:04:43 +00:00
|
|
|
recoveredIn = MustReveal(got)
|
|
|
|
assert.Equal(t, test.in, recoveredIn, "not bidirectional")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 06:55:54 +00:00
|
|
|
func TestCRUD(t *testing.T) {
|
|
|
|
configKey = nil // reset password
|
|
|
|
// create temp config file
|
|
|
|
tempFile, err := ioutil.TempFile("", "crud.conf")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
path := tempFile.Name()
|
|
|
|
defer func() {
|
|
|
|
err := os.Remove(path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
assert.NoError(t, tempFile.Close())
|
|
|
|
|
|
|
|
// temporarily adapt configuration
|
|
|
|
oldOsStdout := os.Stdout
|
|
|
|
oldConfigFile := configFile
|
|
|
|
oldConfig := Config
|
2017-06-28 19:50:47 +00:00
|
|
|
oldConfigData := configData
|
2017-06-25 06:55:54 +00:00
|
|
|
oldReadLine := ReadLine
|
|
|
|
os.Stdout = nil
|
|
|
|
configFile = &path
|
|
|
|
Config = &ConfigInfo{}
|
2017-06-28 19:50:47 +00:00
|
|
|
configData = nil
|
2017-06-25 06:55:54 +00:00
|
|
|
defer func() {
|
|
|
|
os.Stdout = oldOsStdout
|
|
|
|
configFile = oldConfigFile
|
|
|
|
ReadLine = oldReadLine
|
|
|
|
Config = oldConfig
|
2017-06-28 19:50:47 +00:00
|
|
|
configData = oldConfigData
|
2017-06-25 06:55:54 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
LoadConfig()
|
|
|
|
assert.Equal(t, []string{}, configData.GetSectionList())
|
|
|
|
|
|
|
|
// add new remote
|
|
|
|
i := 0
|
|
|
|
ReadLine = func() string {
|
|
|
|
answers := []string{
|
|
|
|
"local", // type is local
|
|
|
|
"1", // yes, disable long filenames
|
|
|
|
"y", // looks good, save
|
|
|
|
}
|
|
|
|
i = i + 1
|
|
|
|
return answers[i-1]
|
|
|
|
}
|
|
|
|
NewRemote("test")
|
|
|
|
assert.Equal(t, []string{"test"}, configData.GetSectionList())
|
|
|
|
|
|
|
|
// normal rename, test → asdf
|
|
|
|
ReadLine = func() string { return "asdf" }
|
|
|
|
RenameRemote("test")
|
|
|
|
assert.Equal(t, []string{"asdf"}, configData.GetSectionList())
|
|
|
|
|
|
|
|
// no-op rename, asdf → asdf
|
|
|
|
RenameRemote("asdf")
|
|
|
|
assert.Equal(t, []string{"asdf"}, configData.GetSectionList())
|
|
|
|
|
|
|
|
// delete remote
|
|
|
|
DeleteRemote("asdf")
|
|
|
|
assert.Equal(t, []string{}, configData.GetSectionList())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
} {
|
|
|
|
gotString, gotErr := Reveal(test.in)
|
|
|
|
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
|
2016-02-16 15:25:27 +00:00
|
|
|
c, err := loadConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sections := c.GetSectionList()
|
|
|
|
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
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
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)
|
2016-02-16 15:25:27 +00:00
|
|
|
c, err := loadConfigFile()
|
2016-06-29 16:59:31 +00:00
|
|
|
require.NoError(t, err)
|
2016-02-16 15:25:27 +00:00
|
|
|
sections := c.GetSectionList()
|
|
|
|
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
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
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 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 }()
|
2016-02-16 15:25:27 +00:00
|
|
|
_, err = loadConfigFile()
|
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"
|
|
|
|
_, err = loadConfigFile()
|
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"
|
|
|
|
_, err = loadConfigFile()
|
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"
|
|
|
|
c, err := loadConfigFile()
|
2016-12-20 15:05:08 +00:00
|
|
|
assert.Equal(t, errorConfigFileNotFound, err)
|
|
|
|
assert.Nil(t, c)
|
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
|
|
|
}
|
|
|
|
}
|