2016-02-15 18:17:41 +00:00
|
|
|
package sftp
|
|
|
|
|
2017-04-10 20:17:50 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
2016-02-15 18:17:41 +00:00
|
|
|
|
|
|
|
var sshcmdTests = []struct {
|
2017-04-10 20:17:50 +00:00
|
|
|
cfg Config
|
|
|
|
cmd string
|
|
|
|
args []string
|
2016-02-15 18:17:41 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-04-10 20:17:50 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/subdir"},
|
|
|
|
"ssh",
|
2020-02-19 14:53:20 +00:00
|
|
|
[]string{"-l", "user", "-s", "sftp", "--", "host"},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-10 20:17:50 +00:00
|
|
|
Config{Host: "host", Path: "dir/subdir"},
|
|
|
|
"ssh",
|
2020-02-19 14:53:20 +00:00
|
|
|
[]string{"-s", "sftp", "--", "host"},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-19 14:33:52 +00:00
|
|
|
Config{Host: "host", Port: "10022", Path: "/dir/subdir"},
|
2017-04-10 20:17:50 +00:00
|
|
|
"ssh",
|
2020-02-19 14:53:20 +00:00
|
|
|
[]string{"-p", "10022", "-s", "sftp", "--", "host"},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-19 14:33:52 +00:00
|
|
|
Config{User: "user", Host: "host", Port: "10022", Path: "/dir/subdir"},
|
2017-04-10 20:17:50 +00:00
|
|
|
"ssh",
|
2020-02-19 14:53:20 +00:00
|
|
|
[]string{"-p", "10022", "-l", "user", "-s", "sftp", "--", "host"},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
2020-02-19 14:33:52 +00:00
|
|
|
{
|
|
|
|
// IPv6 address.
|
|
|
|
Config{User: "user", Host: "::1", Path: "dir"},
|
|
|
|
"ssh",
|
2020-02-19 14:53:20 +00:00
|
|
|
[]string{"-l", "user", "-s", "sftp", "--", "::1"},
|
2020-02-19 14:33:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// IPv6 address with zone and port.
|
|
|
|
Config{User: "user", Host: "::1%lo0", Port: "22", Path: "dir"},
|
|
|
|
"ssh",
|
2020-02-19 14:53:20 +00:00
|
|
|
[]string{"-p", "22", "-l", "user", "-s", "sftp", "--", "::1%lo0"},
|
2020-02-19 14:33:52 +00:00
|
|
|
},
|
2016-02-15 18:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildSSHCommand(t *testing.T) {
|
2020-02-19 14:33:52 +00:00
|
|
|
for i, test := range sshcmdTests {
|
2017-04-10 20:17:50 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
cmd, args, err := buildSSHCommand(test.cfg)
|
|
|
|
if err != nil {
|
2020-02-19 14:33:52 +00:00
|
|
|
t.Fatalf("%v in test %d", err, i)
|
2016-02-15 18:17:41 +00:00
|
|
|
}
|
2017-04-10 20:17:50 +00:00
|
|
|
|
|
|
|
if cmd != test.cmd {
|
|
|
|
t.Fatalf("cmd: want %v, got %v", test.cmd, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(test.args, args) {
|
2020-02-19 14:33:52 +00:00
|
|
|
t.Fatalf("wrong args in test %d, want:\n %v\ngot:\n %v",
|
|
|
|
i, test.args, args)
|
2017-04-10 20:17:50 +00:00
|
|
|
}
|
|
|
|
})
|
2016-02-15 18:17:41 +00:00
|
|
|
}
|
|
|
|
}
|