2015-02-20 03:26:00 +08:00
|
|
|
package fs
|
|
|
|
|
2016-02-16 23:25:27 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
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
|
|
|
|
|
|
|
func TestSizeSuffixString(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in float64
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{0, "0"},
|
2016-06-04 05:49:14 +08:00
|
|
|
{102, "102"},
|
2015-03-15 01:53:53 +08:00
|
|
|
{1024, "1k"},
|
|
|
|
{1024 * 1024, "1M"},
|
|
|
|
{1024 * 1024 * 1024, "1G"},
|
|
|
|
{10 * 1024 * 1024 * 1024, "10G"},
|
|
|
|
{10.1 * 1024 * 1024 * 1024, "10.100G"},
|
2016-06-04 04:51:39 +08:00
|
|
|
{-1, "off"},
|
|
|
|
{-100, "off"},
|
2015-02-20 03:26:00 +08:00
|
|
|
} {
|
|
|
|
ss := SizeSuffix(test.in)
|
|
|
|
got := ss.String()
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, test.want, got)
|
2015-02-20 03:26:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSizeSuffixSet(t *testing.T) {
|
2016-06-30 00:59:31 +08:00
|
|
|
for _, test := range []struct {
|
2015-02-20 03:26:00 +08:00
|
|
|
in string
|
|
|
|
want int64
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"0", 0, false},
|
2016-06-04 04:16:48 +08:00
|
|
|
{"1b", 1, false},
|
|
|
|
{"102B", 102, false},
|
2015-02-20 03:26:00 +08:00
|
|
|
{"0.1k", 102, false},
|
|
|
|
{"0.1", 102, false},
|
|
|
|
{"1K", 1024, false},
|
|
|
|
{"1", 1024, false},
|
|
|
|
{"2.5", 1024 * 2.5, false},
|
|
|
|
{"1M", 1024 * 1024, false},
|
|
|
|
{"1.g", 1024 * 1024 * 1024, false},
|
|
|
|
{"10G", 10 * 1024 * 1024 * 1024, false},
|
2016-06-04 04:51:39 +08:00
|
|
|
{"off", -1, false},
|
|
|
|
{"OFF", -1, false},
|
2015-02-20 03:26:00 +08:00
|
|
|
{"", 0, true},
|
|
|
|
{"1p", 0, true},
|
|
|
|
{"1.p", 0, true},
|
|
|
|
{"1p", 0, true},
|
2015-03-15 01:53:53 +08:00
|
|
|
{"-1K", 0, true},
|
2015-02-20 03:26:00 +08:00
|
|
|
} {
|
|
|
|
ss := SizeSuffix(0)
|
|
|
|
err := ss.Set(test.in)
|
2016-06-30 00:59:31 +08:00
|
|
|
if test.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2015-02-20 03:26:00 +08:00
|
|
|
}
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, test.want, int64(ss))
|
2015-02-20 03:26:00 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 05:33:34 +08:00
|
|
|
|
|
|
|
func TestReveal(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
|
|
|
{"2sTcyNrA", "potato"},
|
|
|
|
} {
|
|
|
|
got := Reveal(test.in)
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, test.want, got)
|
|
|
|
assert.Equal(t, test.in, Obscure(got), "not bidirectional")
|
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
|
|
|
|
err = setPassword("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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
|
|
|
ConfigPath = "./testdata/filenotfound.conf"
|
|
|
|
c, err := loadConfigFile()
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, c.GetSectionList(), 0, "Expected 0-length section")
|
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
|
|
|
|
err = setPassword(" \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
|
|
|
|
err = setPassword(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 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) {
|
|
|
|
err := setPassword(a)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-02-16 23:25:27 +08:00
|
|
|
k1 := configKey
|
|
|
|
|
|
|
|
err = setPassword(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
|
|
|
}
|
|
|
|
}
|