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{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://me@foo:10022",
|
|
|
|
expectedArgs: []string{
|
|
|
|
"-l", "me",
|
|
|
|
"-p", "10022",
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
url: "ssh://foo/bar",
|
2018-11-09 02:35:34 -05:00
|
|
|
expectedError: `extra path 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 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 {
|
2019-03-01 09:34:36 -05:00
|
|
|
sp, err := ParseURL(tc.url)
|
2018-02-19 04:31:29 -05:00
|
|
|
if tc.expectedError == "" {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(tc.expectedArgs, sp.Args()))
|
|
|
|
} else {
|
|
|
|
assert.ErrorContains(t, err, tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|