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"
|
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")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test some error cases
|
|
|
|
func TestReveal(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{"YmJiYmJiYmJiYmJiYmJiYp*gcEWbAw", "base64 decode failed: illegal base64 data at input byte 22"},
|
|
|
|
{"aGVsbG8", "input too short"},
|
|
|
|
{"", "input too short"},
|
|
|
|
} {
|
|
|
|
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 with trimmed whitespace
|
|
|
|
hashedKeyCompare(t, " abcdef \t", "abcdef", true)
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|