2015-12-28 14:51:24 +00:00
|
|
|
package sftp
|
|
|
|
|
2019-09-26 10:23:31 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2015-12-28 14:57:20 +00:00
|
|
|
var configTests = []struct {
|
2016-02-15 15:58:13 +00:00
|
|
|
in string
|
2015-12-28 14:51:24 +00:00
|
|
|
cfg Config
|
|
|
|
}{
|
|
|
|
// first form, user specified sftp://user@host/dir
|
|
|
|
{
|
|
|
|
"sftp://user@host/dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp://host/dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{Host: "host", Path: "dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp://host//dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{Host: "host", Path: "/dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
2016-02-15 18:17:41 +00:00
|
|
|
{
|
|
|
|
"sftp://host:10022//dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{Host: "host", Port: "10022", Path: "/dir/subdir", Connections: 5},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp://user@host:10022//dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Port: "10022", Path: "/dir/subdir", Connections: 5},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
2016-02-15 15:58:13 +00:00
|
|
|
{
|
|
|
|
"sftp://user@host/dir/subdir/../other",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/other", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp://user@host/dir///subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/subdir", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2020-02-19 14:33:52 +00:00
|
|
|
// IPv6 address.
|
|
|
|
{
|
|
|
|
"sftp://user@[::1]/dir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "::1", Path: "dir", Connections: 5},
|
2020-02-19 14:33:52 +00:00
|
|
|
},
|
|
|
|
// IPv6 address with port.
|
|
|
|
{
|
|
|
|
"sftp://user@[::1]:22/dir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "::1", Port: "22", Path: "dir", Connections: 5},
|
2020-02-19 14:33:52 +00:00
|
|
|
},
|
|
|
|
|
2015-12-28 14:51:24 +00:00
|
|
|
// second form, user specified sftp:user@host:/dir
|
|
|
|
{
|
2016-02-15 15:58:13 +00:00
|
|
|
"sftp:user@host:/dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "/dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
2019-09-26 10:23:31 +00:00
|
|
|
{
|
|
|
|
"sftp:user@domain@host:/dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user@domain", Host: "host", Path: "/dir/subdir", Connections: 5},
|
2019-09-26 10:23:31 +00:00
|
|
|
},
|
2015-12-28 14:51:24 +00:00
|
|
|
{
|
2016-02-15 15:58:13 +00:00
|
|
|
"sftp:host:../dir/subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{Host: "host", Path: "../dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
{
|
2016-02-15 15:58:13 +00:00
|
|
|
"sftp:user@host:dir/subdir:suffix",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/subdir:suffix", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp:user@host:dir/subdir/../other",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/other", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sftp:user@host:dir///subdir",
|
2021-08-07 17:56:59 +00:00
|
|
|
Config{User: "user", Host: "host", Path: "dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfig(t *testing.T) {
|
2015-12-28 14:57:20 +00:00
|
|
|
for i, test := range configTests {
|
2016-02-15 15:58:13 +00:00
|
|
|
cfg, err := ParseConfig(test.in)
|
2015-12-28 14:51:24 +00:00
|
|
|
if err != nil {
|
2016-02-15 15:58:13 +00:00
|
|
|
t.Errorf("test %d:%s failed: %v", i, test.in, err)
|
2015-12-28 14:51:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg != test.cfg {
|
2016-02-15 15:58:13 +00:00
|
|
|
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v",
|
|
|
|
i, test.in, test.cfg, cfg)
|
2015-12-28 14:51:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 10:02:07 +00:00
|
|
|
|
|
|
|
var configTestsInvalid = []string{
|
|
|
|
"sftp://host:dir",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfigInvalid(t *testing.T) {
|
|
|
|
for i, test := range configTestsInvalid {
|
|
|
|
_, err := ParseConfig(test)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("test %d: invalid config %s did not return an error", i, test)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|