2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 19:38:57 +00:00
restic/internal/backend/sftp/split_test.go

116 lines
1.9 KiB
Go
Raw Normal View History

2017-04-03 06:57:33 +00:00
package sftp
import (
"reflect"
"testing"
)
func TestShellSplitter(t *testing.T) {
var tests = []struct {
data string
2017-04-03 19:05:42 +00:00
cmd string
args []string
2017-04-03 06:57:33 +00:00
}{
{
`foo`,
2017-04-03 19:05:42 +00:00
"foo", []string{},
2017-04-03 06:57:33 +00:00
},
{
`'foo'`,
2017-04-03 19:05:42 +00:00
"foo", []string{},
2017-04-03 06:57:33 +00:00
},
{
`foo bar baz`,
2017-04-03 19:05:42 +00:00
"foo", []string{"bar", "baz"},
2017-04-03 06:57:33 +00:00
},
{
`foo 'bar' baz`,
2017-04-03 19:05:42 +00:00
"foo", []string{"bar", "baz"},
2017-04-03 06:57:33 +00:00
},
{
2017-04-03 19:05:42 +00:00
`'bar box' baz`,
"bar box", []string{"baz"},
2017-04-03 06:57:33 +00:00
},
{
`"bar 'box'" baz`,
2017-04-03 19:05:42 +00:00
"bar 'box'", []string{"baz"},
2017-04-03 06:57:33 +00:00
},
{
`'bar "box"' baz`,
2017-04-03 19:05:42 +00:00
`bar "box"`, []string{"baz"},
2017-04-03 06:57:33 +00:00
},
{
`\"bar box baz`,
2017-04-03 19:05:42 +00:00
`"bar`, []string{"box", "baz"},
2017-04-03 06:57:33 +00:00
},
{
`"bar/foo/x" "box baz"`,
2017-04-03 19:05:42 +00:00
"bar/foo/x", []string{"box baz"},
2017-04-03 06:57:33 +00:00
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
2017-04-03 19:05:42 +00:00
cmd, args, err := SplitShellArgs(test.data)
2017-04-03 06:57:33 +00:00
if err != nil {
t.Fatal(err)
}
2017-04-03 19:05:42 +00:00
if cmd != test.cmd {
t.Fatalf("wrong cmd returned, want:\n %#v\ngot:\n %#v",
test.cmd, cmd)
}
if !reflect.DeepEqual(args, test.args) {
t.Fatalf("wrong args returned, want:\n %#v\ngot:\n %#v",
test.args, args)
2017-04-03 06:57:33 +00:00
}
})
}
}
func TestShellSplitterInvalid(t *testing.T) {
var tests = []struct {
data string
err string
}{
{
"foo'",
"single-quoted string not terminated",
},
{
`foo"`,
"double-quoted string not terminated",
},
{
"foo 'bar",
"single-quoted string not terminated",
},
{
`foo "bar`,
"double-quoted string not terminated",
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
2017-04-03 19:05:42 +00:00
cmd, args, err := SplitShellArgs(test.data)
2017-04-03 06:57:33 +00:00
if err == nil {
t.Fatalf("expected error not found: %v", test.err)
}
if err.Error() != test.err {
t.Fatalf("expected error not found, want:\n %q\ngot:\n %q", test.err, err.Error())
}
2017-04-03 19:05:42 +00:00
if cmd != "" {
t.Fatalf("splitter returned cmd from invalid data: %v", cmd)
}
if len(args) > 0 {
t.Fatalf("splitter returned fields from invalid data: %v", args)
2017-04-03 06:57:33 +00:00
}
})
}
}