2015-12-28 14:51:24 +00:00
|
|
|
package sftp
|
|
|
|
|
2019-09-26 10:23:31 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
2023-04-20 21:02:56 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/backend/test"
|
2019-09-26 10:23:31 +00:00
|
|
|
)
|
2015-12-28 14:51:24 +00:00
|
|
|
|
2023-04-20 21:02:56 +00:00
|
|
|
var configTests = []test.ConfigTestData[Config]{
|
2015-12-28 14:51:24 +00:00
|
|
|
// first form, user specified sftp://user@host/dir
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://user@host/dir/subdir",
|
|
|
|
Cfg: Config{User: "user", Host: "host", Path: "dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://host/dir/subdir",
|
|
|
|
Cfg: Config{Host: "host", Path: "dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://host//dir/subdir",
|
|
|
|
Cfg: Config{Host: "host", Path: "/dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
2016-02-15 18:17:41 +00:00
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://host:10022//dir/subdir",
|
|
|
|
Cfg: Config{Host: "host", Port: "10022", Path: "/dir/subdir", Connections: 5},
|
2016-02-15 18:17:41 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://user@host:10022//dir/subdir",
|
|
|
|
Cfg: 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
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://user@host/dir/subdir/../other",
|
|
|
|
Cfg: Config{User: "user", Host: "host", Path: "dir/other", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://user@host/dir///subdir",
|
|
|
|
Cfg: 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.
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://user@[::1]/dir",
|
|
|
|
Cfg: Config{User: "user", Host: "::1", Path: "dir", Connections: 5},
|
2020-02-19 14:33:52 +00:00
|
|
|
},
|
|
|
|
// IPv6 address with port.
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp://user@[::1]:22/dir",
|
|
|
|
Cfg: 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
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp:user@host:/dir/subdir",
|
|
|
|
Cfg: 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
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp:user@domain@host:/dir/subdir",
|
|
|
|
Cfg: 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
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp:host:../dir/subdir",
|
|
|
|
Cfg: Config{Host: "host", Path: "../dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp:user@host:dir/subdir:suffix",
|
|
|
|
Cfg: Config{User: "user", Host: "host", Path: "dir/subdir:suffix", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp:user@host:dir/subdir/../other",
|
|
|
|
Cfg: Config{User: "user", Host: "host", Path: "dir/other", Connections: 5},
|
2016-02-15 15:58:13 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-21 19:11:33 +00:00
|
|
|
S: "sftp:user@host:dir///subdir",
|
|
|
|
Cfg: Config{User: "user", Host: "host", Path: "dir/subdir", Connections: 5},
|
2015-12-28 14:51:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfig(t *testing.T) {
|
2023-04-20 21:02:56 +00:00
|
|
|
test.ParseConfigTester(t, ParseConfig, configTests)
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|