fspath: Implement a connection string parser #4996

This is implemented as a state machine parser so it can emit sensible
error messages.

It does not use the connection strings elsewhere in rclone yet - see
subsequent commits.

An optional fuzzer is implemented for the Parse function.
This commit is contained in:
Nick Craig-Wood 2021-02-09 09:30:40 +00:00
parent 3b21857097
commit 8a46dd1b57
8 changed files with 613 additions and 70 deletions

View file

@ -1,13 +1,23 @@
package fspath
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/driveletter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
makeCorpus = flag.Bool("make-corpus", false, "Set to make the fuzzing corpus")
)
func TestCheckConfigName(t *testing.T) {
@ -39,6 +49,7 @@ func TestCheckRemoteName(t *testing.T) {
want error
}{
{":remote:", nil},
{":s3:", nil},
{"remote:", nil},
{"", errInvalidCharacters},
{"rem:ote", errInvalidCharacters},
@ -49,44 +60,267 @@ func TestCheckRemoteName(t *testing.T) {
{"[remote:", errInvalidCharacters},
{"*:", errInvalidCharacters},
} {
got := CheckRemoteName(test.in)
got := checkRemoteName(test.in)
assert.Equal(t, test.want, got, test.in)
}
}
func TestParse(t *testing.T) {
for _, test := range []struct {
in, wantConfigName, wantFsPath string
wantErr error
isDriveLetter = func(name string) bool {
return name == "C"
}
defer func() {
isDriveLetter = driveletter.IsDriveLetter
}()
for testNumber, test := range []struct {
in string
wantParsed Parsed
wantErr error
}{
{"", "", "", errCantBeEmpty},
{":", "", "", errInvalidCharacters},
{"::", ":", "", errInvalidCharacters},
{":/:", "", "/:", errInvalidCharacters},
{"/:", "", "/:", nil},
{"\\backslash:", "", "\\backslash:", nil},
{"/slash:", "", "/slash:", nil},
{"with\\backslash:", "", "with\\backslash:", nil},
{"with/slash:", "", "with/slash:", nil},
{"/path/to/file", "", "/path/to/file", nil},
{"/path:/to/file", "", "/path:/to/file", nil},
{"./path:/to/file", "", "./path:/to/file", nil},
{"./:colon.txt", "", "./:colon.txt", nil},
{"path/to/file", "", "path/to/file", nil},
{"remote:path/to/file", "remote", "path/to/file", nil},
{"rem*ote:path/to/file", "rem*ote", "path/to/file", errInvalidCharacters},
{"remote:/path/to/file", "remote", "/path/to/file", nil},
{"rem.ote:/path/to/file", "rem.ote", "/path/to/file", errInvalidCharacters},
{":backend:/path/to/file", ":backend", "/path/to/file", nil},
{":bac*kend:/path/to/file", ":bac*kend", "/path/to/file", errInvalidCharacters},
{
in: "",
wantErr: errCantBeEmpty,
}, {
in: ":",
wantErr: errConfigName,
}, {
in: "::",
wantErr: errConfigNameEmpty,
}, {
in: ":/:",
wantErr: errInvalidCharacters,
}, {
in: "/:",
wantParsed: Parsed{
ConfigString: "",
Path: "/:",
},
}, {
in: "\\backslash:",
wantParsed: Parsed{
ConfigString: "",
Path: "\\backslash:",
},
}, {
in: "/slash:",
wantParsed: Parsed{
ConfigString: "",
Path: "/slash:",
},
}, {
in: "with\\backslash:",
wantParsed: Parsed{
ConfigString: "",
Path: "with\\backslash:",
},
}, {
in: "with/slash:",
wantParsed: Parsed{
ConfigString: "",
Path: "with/slash:",
},
}, {
in: "/path/to/file",
wantParsed: Parsed{
ConfigString: "",
Path: "/path/to/file",
},
}, {
in: "/path:/to/file",
wantParsed: Parsed{
ConfigString: "",
Path: "/path:/to/file",
},
}, {
in: "./path:/to/file",
wantParsed: Parsed{
ConfigString: "",
Path: "./path:/to/file",
},
}, {
in: "./:colon.txt",
wantParsed: Parsed{
ConfigString: "",
Path: "./:colon.txt",
},
}, {
in: "path/to/file",
wantParsed: Parsed{
ConfigString: "",
Path: "path/to/file",
},
}, {
in: "remote:path/to/file",
wantParsed: Parsed{
ConfigString: "remote",
Name: "remote",
Path: "path/to/file",
},
}, {
in: "rem*ote:path/to/file",
wantErr: errInvalidCharacters,
}, {
in: "remote:/path/to/file",
wantParsed: Parsed{
ConfigString: "remote",
Name: "remote",
Path: "/path/to/file",
},
}, {
in: "rem.ote:/path/to/file",
wantErr: errInvalidCharacters,
}, {
in: ":backend:/path/to/file",
wantParsed: Parsed{
ConfigString: ":backend",
Name: ":backend",
Path: "/path/to/file",
},
}, {
in: ":bac*kend:/path/to/file",
wantErr: errInvalidCharacters,
}, {
in: `C:\path\to\file`,
wantParsed: Parsed{
Name: "",
Path: `C:\path\to\file`,
},
}, {
in: `remote:\path\to\file`,
wantParsed: Parsed{
Name: "remote",
ConfigString: "remote",
Path: `\path\to\file`,
},
}, {
in: `D:/path/to/file`,
wantParsed: Parsed{
Name: "D",
ConfigString: "D",
Path: `/path/to/file`,
},
}, {
in: `:backend,param1:/path/to/file`,
wantParsed: Parsed{
ConfigString: `:backend,param1`,
Name: ":backend",
Path: "/path/to/file",
Config: configmap.Simple{
"param1": "true",
},
},
}, {
in: `:backend,param1=value:/path/to/file`,
wantParsed: Parsed{
ConfigString: `:backend,param1=value`,
Name: ":backend",
Path: "/path/to/file",
Config: configmap.Simple{
"param1": "value",
},
},
}, {
in: `:backend,param1=value1,param2,param3=value3:/path/to/file`,
wantParsed: Parsed{
ConfigString: `:backend,param1=value1,param2,param3=value3`,
Name: ":backend",
Path: "/path/to/file",
Config: configmap.Simple{
"param1": "value1",
"param2": "true",
"param3": "value3",
},
},
}, {
in: `:backend,param1=value1,param2="value2",param3='value3':/path/to/file`,
wantParsed: Parsed{
ConfigString: `:backend,param1=value1,param2="value2",param3='value3'`,
Name: ":backend",
Path: "/path/to/file",
Config: configmap.Simple{
"param1": "value1",
"param2": "value2",
"param3": "value3",
},
},
}, {
in: `:backend,param-1=value:/path/to/file`,
wantErr: errBadConfigParam,
}, {
in: `:backend,param1="value"x:/path/to/file`,
wantErr: errAfterQuote,
}, {
in: `:backend,`,
wantErr: errParam,
}, {
in: `:backend,param=value`,
wantErr: errValue,
}, {
in: `:backend,param="value'`,
wantErr: errQuotedValue,
}, {
in: `:backend,param1="value"`,
wantErr: errAfterQuote,
}, {
in: `:backend,=value:`,
wantErr: errEmptyConfigParam,
}, {
in: `:backend,:`,
wantErr: errEmptyConfigParam,
}, {
in: `:backend,,:`,
wantErr: errEmptyConfigParam,
}, {
in: `:backend,param=:path`,
wantParsed: Parsed{
ConfigString: `:backend,param=`,
Name: ":backend",
Path: "path",
Config: configmap.Simple{
"param": "",
},
},
}, {
in: `:backend,param="with""quote":path`,
wantParsed: Parsed{
ConfigString: `:backend,param="with""quote"`,
Name: ":backend",
Path: "path",
Config: configmap.Simple{
"param": `with"quote`,
},
},
}, {
in: `:backend,param='''''':`,
wantParsed: Parsed{
ConfigString: `:backend,param=''''''`,
Name: ":backend",
Path: "",
Config: configmap.Simple{
"param": `''`,
},
},
}, {
in: `:backend,param=''bad'':`,
wantErr: errAfterQuote,
},
} {
gotConfigName, gotFsPath, gotErr := Parse(test.in)
if runtime.GOOS == "windows" {
test.wantFsPath = strings.Replace(test.wantFsPath, `\`, `/`, -1)
gotParsed, gotErr := Parse(test.in)
// For non-local paths we convert \ into / on Windows
if runtime.GOOS == "windows" && test.wantParsed.Name != "" {
test.wantParsed.Path = strings.Replace(test.wantParsed.Path, `\`, `/`, -1)
}
assert.Equal(t, test.wantErr, gotErr)
assert.Equal(t, test.wantConfigName, gotConfigName)
assert.Equal(t, test.wantFsPath, gotFsPath)
assert.Equal(t, test.wantErr, gotErr, test.in)
if test.wantErr == nil {
assert.Equal(t, test.wantParsed, gotParsed, test.in)
}
if *makeCorpus {
// write the test corpus for fuzzing
require.NoError(t, os.MkdirAll("corpus", 0777))
require.NoError(t, ioutil.WriteFile(fmt.Sprintf("corpus/%02d", testNumber), []byte(test.in), 0666))
}
}
}