2018-02-19 04:31:29 -05:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-02-19 04:31:29 -05:00
|
|
|
)
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
func TestParseURL(t *testing.T) {
|
2018-02-19 04:31:29 -05:00
|
|
|
testCases := []struct {
|
|
|
|
url string
|
|
|
|
expectedArgs []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
url: "ssh://foo",
|
|
|
|
expectedArgs: []string{
|
2020-05-18 14:12:52 -04:00
|
|
|
"--", "foo",
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://me@foo:10022",
|
|
|
|
expectedArgs: []string{
|
|
|
|
"-l", "me",
|
|
|
|
"-p", "10022",
|
2020-05-18 14:12:52 -04:00
|
|
|
"--", "foo",
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://me:passw0rd@foo",
|
2018-11-09 02:35:34 -05:00
|
|
|
expectedError: "plain-text password is not supported",
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
{
|
2023-03-04 07:53:25 -05:00
|
|
|
url: "ssh://foo/bar",
|
|
|
|
expectedArgs: []string{
|
|
|
|
"--", "foo",
|
|
|
|
},
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://foo?bar",
|
2018-11-09 02:35:34 -05:00
|
|
|
expectedError: `extra query after the host: "bar"`,
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://foo#bar",
|
2018-11-09 02:35:34 -05:00
|
|
|
expectedError: `extra fragment after the host: "bar"`,
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://",
|
2018-11-09 02:35:34 -05:00
|
|
|
expectedError: "no host specified",
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "foo://bar",
|
2018-11-09 02:35:34 -05:00
|
|
|
expectedError: `expected scheme ssh, got "foo"`,
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2020-05-18 14:12:52 -04:00
|
|
|
t.Run(tc.url, func(t *testing.T) {
|
|
|
|
sp, err := ParseURL(tc.url)
|
|
|
|
if tc.expectedError == "" {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(tc.expectedArgs, sp.Args()))
|
|
|
|
} else {
|
|
|
|
assert.ErrorContains(t, err, tc.expectedError)
|
|
|
|
}
|
|
|
|
})
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
}
|