2015-02-19 20:26:00 +01:00
|
|
|
package fs
|
|
|
|
|
2016-02-16 16:25:27 +01:00
|
|
|
import (
|
2016-08-14 13:04:43 +02:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2016-02-16 16:25:27 +01:00
|
|
|
"testing"
|
2016-06-29 18:59:31 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-16 16:25:27 +01:00
|
|
|
)
|
2015-02-19 20:26:00 +01:00
|
|
|
|
|
|
|
func TestSizeSuffixString(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in float64
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{0, "0"},
|
2016-06-03 23:49:14 +02:00
|
|
|
{102, "102"},
|
2015-03-14 18:53:53 +01:00
|
|
|
{1024, "1k"},
|
|
|
|
{1024 * 1024, "1M"},
|
|
|
|
{1024 * 1024 * 1024, "1G"},
|
|
|
|
{10 * 1024 * 1024 * 1024, "10G"},
|
|
|
|
{10.1 * 1024 * 1024 * 1024, "10.100G"},
|
2016-06-03 22:51:39 +02:00
|
|
|
{-1, "off"},
|
|
|
|
{-100, "off"},
|
2015-02-19 20:26:00 +01:00
|
|
|
} {
|
|
|
|
ss := SizeSuffix(test.in)
|
|
|
|
got := ss.String()
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, test.want, got)
|
2015-02-19 20:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 14:04:30 +02:00
|
|
|
func TestSizeSuffixUnit(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in float64
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{0, "0 Bytes"},
|
|
|
|
{102, "102 Bytes"},
|
|
|
|
{1024, "1 kBytes"},
|
|
|
|
{1024 * 1024, "1 MBytes"},
|
|
|
|
{1024 * 1024 * 1024, "1 GBytes"},
|
|
|
|
{10 * 1024 * 1024 * 1024, "10 GBytes"},
|
|
|
|
{10.1 * 1024 * 1024 * 1024, "10.100 GBytes"},
|
|
|
|
{-1, "off"},
|
|
|
|
{-100, "off"},
|
|
|
|
} {
|
|
|
|
ss := SizeSuffix(test.in)
|
|
|
|
got := ss.Unit("Bytes")
|
|
|
|
assert.Equal(t, test.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:26:00 +01:00
|
|
|
func TestSizeSuffixSet(t *testing.T) {
|
2016-06-29 18:59:31 +02:00
|
|
|
for _, test := range []struct {
|
2015-02-19 20:26:00 +01:00
|
|
|
in string
|
|
|
|
want int64
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"0", 0, false},
|
2016-06-03 22:16:48 +02:00
|
|
|
{"1b", 1, false},
|
|
|
|
{"102B", 102, false},
|
2015-02-19 20:26:00 +01: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-03 22:51:39 +02:00
|
|
|
{"off", -1, false},
|
|
|
|
{"OFF", -1, false},
|
2015-02-19 20:26:00 +01:00
|
|
|
{"", 0, true},
|
|
|
|
{"1p", 0, true},
|
|
|
|
{"1.p", 0, true},
|
|
|
|
{"1p", 0, true},
|
2015-03-14 18:53:53 +01:00
|
|
|
{"-1K", 0, true},
|
2015-02-19 20:26:00 +01:00
|
|
|
} {
|
|
|
|
ss := SizeSuffix(0)
|
|
|
|
err := ss.Set(test.in)
|
2016-06-29 18:59:31 +02:00
|
|
|
if test.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2015-02-19 20:26:00 +01:00
|
|
|
}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, test.want, int64(ss))
|
2015-02-19 20:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 23:33:34 +02:00
|
|
|
|
2016-08-14 13:04:43 +02:00
|
|
|
func TestObscure(t *testing.T) {
|
2015-09-01 23:33:34 +02:00
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want string
|
2016-08-14 13:04:43 +02:00
|
|
|
iv string
|
2015-09-01 23:33:34 +02:00
|
|
|
}{
|
2016-08-14 13:04:43 +02:00
|
|
|
{"", "YWFhYWFhYWFhYWFhYWFhYQ", "aaaaaaaaaaaaaaaa"},
|
|
|
|
{"potato", "YWFhYWFhYWFhYWFhYWFhYXMaGgIlEQ", "aaaaaaaaaaaaaaaa"},
|
|
|
|
{"potato", "YmJiYmJiYmJiYmJiYmJiYp3gcEWbAw", "bbbbbbbbbbbbbbbb"},
|
2015-09-01 23:33:34 +02:00
|
|
|
} {
|
2016-08-14 13:04:43 +02: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 18:59:31 +02:00
|
|
|
assert.Equal(t, test.want, got)
|
2016-08-14 13:04:43 +02: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 23:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
func TestConfigLoad(t *testing.T) {
|
2016-02-29 22:43:37 +01:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 16:25:27 +01:00
|
|
|
ConfigPath = "./testdata/plain.conf"
|
2016-02-29 22:43:37 +01:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
}()
|
|
|
|
configKey = nil // reset password
|
2016-02-16 16:25:27 +01:00
|
|
|
c, err := loadConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sections := c.GetSectionList()
|
|
|
|
var expect = []string{"RCLONE_ENCRYPT_V0", "nounc", "unc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncrypted(t *testing.T) {
|
|
|
|
var err error
|
2016-02-29 22:43:37 +01:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 16:25:27 +01:00
|
|
|
ConfigPath = "./testdata/encrypted.conf"
|
2016-02-29 22:43:37 +01:00
|
|
|
defer func() {
|
|
|
|
ConfigPath = oldConfigPath
|
|
|
|
configKey = nil // reset password
|
|
|
|
}()
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// Set correct password
|
2016-08-14 18:16:06 +02:00
|
|
|
err = setConfigPassword("asdf")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
c, err := loadConfigFile()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
sections := c.GetSectionList()
|
|
|
|
var expect = []string{"nounc", "unc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, sections)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
keys := c.GetKeyList("nounc")
|
|
|
|
expect = []string{"type", "nounc"}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, expect, keys)
|
2016-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigLoadEncryptedFailures(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// This file should be too short to be decoded.
|
2016-02-29 22:43:37 +01:00
|
|
|
oldConfigPath := ConfigPath
|
2016-02-16 16:25:27 +01:00
|
|
|
ConfigPath = "./testdata/enc-short.conf"
|
2016-02-29 22:43:37 +01:00
|
|
|
defer func() { ConfigPath = oldConfigPath }()
|
2016-02-16 16:25:27 +01:00
|
|
|
_, err = loadConfigFile()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
|
|
|
ConfigPath = "./testdata/enc-invalid.conf"
|
|
|
|
_, err = loadConfigFile()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
|
|
|
ConfigPath = "./testdata/enc-too-new.conf"
|
|
|
|
_, err = loadConfigFile()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// This file contains invalid base64 characters.
|
|
|
|
ConfigPath = "./testdata/filenotfound.conf"
|
|
|
|
c, err := loadConfigFile()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, c.GetSectionList(), 0, "Expected 0-length section")
|
2016-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassword(t *testing.T) {
|
2016-02-29 22:43:37 +01:00
|
|
|
defer func() {
|
|
|
|
configKey = nil // reset password
|
|
|
|
}()
|
2016-02-16 16:25:27 +01:00
|
|
|
var err error
|
|
|
|
// Empty password should give error
|
2016-08-14 18:16:06 +02:00
|
|
|
err = setConfigPassword(" \t ")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
|
|
|
|
// Test invalid utf8 sequence
|
2016-08-14 18:16:06 +02:00
|
|
|
err = setConfigPassword(string([]byte{0xff, 0xfe, 0xfd}) + "abc")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err)
|
2016-02-16 16:25:27 +01: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 18:16:06 +02:00
|
|
|
err := setConfigPassword(a)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
k1 := configKey
|
|
|
|
|
2016-08-14 18:16:06 +02:00
|
|
|
err = setConfigPassword(b)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-02-16 16:25:27 +01:00
|
|
|
k2 := configKey
|
2016-06-29 18:59:31 +02:00
|
|
|
|
|
|
|
if shouldMatch {
|
|
|
|
assert.Equal(t, k1, k2)
|
|
|
|
} else {
|
|
|
|
assert.NotEqual(t, k1, k2)
|
2016-02-16 16:25:27 +01:00
|
|
|
}
|
|
|
|
}
|