git-lfs/lfshttp/ssh_test.go
brian m. carlson e9ffd5dc5c
lfshttp: use a separate struct for SSH metadata
Right now, all of the SSH metadata for an endpoint is in the Endpoint
struct, but in the future we'd like to move the SSH code to its own
package.  At that point, we'll want to avoid a dependency on the
Endpoint struct, so let's move the SSH metadata out into its own struct,
which we'll include in Endpoint.

While we're at it, let's adjust most of the SSH code to use this new
struct instead so we can easily move it in the future.
2021-07-20 18:37:31 +00:00

660 lines
20 KiB
Go

package lfshttp
import (
"net/url"
"path/filepath"
"testing"
"time"
"github.com/git-lfs/git-lfs/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSSHCacheResolveFromCache(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{
Href: "cache",
createdAt: time.Now(),
}
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "cache", res.Href)
}
func TestSSHCacheResolveFromCacheWithFutureExpiresAt(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{
Href: "cache",
ExpiresAt: time.Now().Add(time.Duration(1) * time.Hour),
createdAt: time.Now(),
}
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "cache", res.Href)
}
func TestSSHCacheResolveFromCacheWithFutureExpiresIn(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{
Href: "cache",
ExpiresIn: 60 * 60,
createdAt: time.Now(),
}
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "cache", res.Href)
}
func TestSSHCacheResolveFromCacheWithPastExpiresAt(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{
Href: "cache",
ExpiresAt: time.Now().Add(time.Duration(-1) * time.Hour),
createdAt: time.Now(),
}
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "real", res.Href)
}
func TestSSHCacheResolveFromCacheWithPastExpiresIn(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{
Href: "cache",
ExpiresIn: -60 * 60,
createdAt: time.Now(),
}
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "real", res.Href)
}
func TestSSHCacheResolveFromCacheWithAmbiguousExpirationInfo(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{
Href: "cache",
ExpiresIn: 60 * 60,
ExpiresAt: time.Now().Add(-1 * time.Hour),
createdAt: time.Now(),
}
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "cache", res.Href)
}
func TestSSHCacheResolveWithoutError(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
assert.Equal(t, 0, len(cache.endpoints))
ssh.responses["userandhost"] = sshAuthResponse{Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "real", res.Href)
assert.Equal(t, 1, len(cache.endpoints))
cacheres, ok := cache.endpoints["userandhost//1//path//post"]
assert.True(t, ok)
assert.NotNil(t, cacheres)
assert.Equal(t, "real", cacheres.Href)
delete(ssh.responses, "userandhost")
res2, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "real", res2.Href)
}
func TestSSHCacheResolveWithError(t *testing.T) {
ssh := newFakeResolver()
cache := withSSHCache(ssh).(*sshCache)
assert.Equal(t, 0, len(cache.endpoints))
ssh.responses["userandhost"] = sshAuthResponse{Message: "resolve error", Href: "real"}
e := Endpoint{
SSHMetadata: SSHMetadata{
UserAndHost: "userandhost",
Port: "1",
Path: "path",
},
}
res, err := cache.Resolve(e, "post")
assert.NotNil(t, err)
assert.Equal(t, "real", res.Href)
assert.Equal(t, 0, len(cache.endpoints))
delete(ssh.responses, "userandhost")
res2, err := cache.Resolve(e, "post")
assert.Nil(t, err)
assert.Equal(t, "", res2.Href)
}
func newFakeResolver() *fakeResolver {
return &fakeResolver{responses: make(map[string]sshAuthResponse)}
}
type fakeResolver struct {
responses map[string]sshAuthResponse
}
func (r *fakeResolver) Resolve(e Endpoint, method string) (sshAuthResponse, error) {
res := r.responses[e.SSHMetadata.UserAndHost]
var err error
if len(res.Message) > 0 {
err = errors.New(res.Message)
}
res.createdAt = time.Now()
return res, err
}
func TestSSHGetLFSExeAndArgs(t *testing.T) {
cli, err := NewClient(nil)
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Path = "user/repo"
exe, args := sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata, endpoint.Operation, "GET")
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{
"--",
"user@foo.com",
"git-lfs-authenticate user/repo download",
}, args)
exe, args = sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata, endpoint.Operation, "HEAD")
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{
"--",
"user@foo.com",
"git-lfs-authenticate user/repo download",
}, args)
// this is going by endpoint.Operation, implicitly set by Endpoint() on L15.
exe, args = sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata, endpoint.Operation, "POST")
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{
"--",
"user@foo.com",
"git-lfs-authenticate user/repo download",
}, args)
endpoint.Operation = "upload"
exe, args = sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata, endpoint.Operation, "POST")
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{
"--",
"user@foo.com",
"git-lfs-authenticate user/repo upload",
}, args)
}
func TestSSHGetExeAndArgsSsh(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "",
"GIT_SSH": "",
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{"--", "user@foo.com"}, args)
}
func TestSSHGetExeAndArgsSshCustomPort(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "",
"GIT_SSH": "",
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Port = "8888"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{"-p", "8888", "--", "user@foo.com"}, args)
}
func TestSSHGetExeAndArgsPlink(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "plink.exe")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "",
"GIT_SSH": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, plink, exe)
assert.Equal(t, []string{"user@foo.com"}, args)
}
func TestSSHGetExeAndArgsPlinkCustomPort(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "plink")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "",
"GIT_SSH": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Port = "8888"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, plink, exe)
assert.Equal(t, []string{"-P", "8888", "user@foo.com"}, args)
}
func TestSSHGetExeAndArgsTortoisePlink(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "tortoiseplink.exe")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "",
"GIT_SSH": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, plink, exe)
assert.Equal(t, []string{"-batch", "user@foo.com"}, args)
}
func TestSSHGetExeAndArgsTortoisePlinkCustomPort(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "tortoiseplink")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "",
"GIT_SSH": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Port = "8888"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, plink, exe)
assert.Equal(t, []string{"-batch", "-P", "8888", "user@foo.com"}, args)
}
func TestSSHGetExeAndArgsSshCommandPrecedence(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "sshcmd",
"GIT_SSH": "bad",
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd user@foo.com"}, args)
}
func TestSSHGetExeAndArgsSshCommandArgs(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "sshcmd --args 1",
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd --args 1 user@foo.com"}, args)
}
func TestSSHGetExeAndArgsSshCommandArgsWithMixedQuotes(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "sshcmd foo 'bar \"baz\"'",
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd foo 'bar \"baz\"' user@foo.com"}, args)
}
func TestSSHGetExeAndArgsSshCommandCustomPort(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "sshcmd",
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Port = "8888"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd -p 8888 user@foo.com"}, args)
}
func TestSSHGetExeAndArgsCoreSshCommand(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": "sshcmd --args 2",
}, map[string]string{
"core.sshcommand": "sshcmd --args 1",
}))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd --args 2 user@foo.com"}, args)
}
func TestSSHGetExeAndArgsCoreSshCommandArgsWithMixedQuotes(t *testing.T) {
cli, err := NewClient(NewContext(nil, nil, map[string]string{
"core.sshcommand": "sshcmd foo 'bar \"baz\"'",
}))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd foo 'bar \"baz\"' user@foo.com"}, args)
}
func TestSSHGetExeAndArgsConfigVersusEnv(t *testing.T) {
cli, err := NewClient(NewContext(nil, nil, map[string]string{
"core.sshcommand": "sshcmd --args 1",
}))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", "sshcmd --args 1 user@foo.com"}, args)
}
func TestSSHGetLFSExeAndArgsWithCustomSSH(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH": "not-ssh",
}, nil))
require.Nil(t, err)
u, err := url.Parse("ssh://git@host.com:12345/repo")
require.Nil(t, err)
e := EndpointFromSshUrl(u)
t.Logf("ENDPOINT: %+v", e)
assert.Equal(t, "12345", e.SSHMetadata.Port)
assert.Equal(t, "git@host.com", e.SSHMetadata.UserAndHost)
assert.Equal(t, "repo", e.SSHMetadata.Path)
exe, args := sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, "download", "GET")
assert.Equal(t, "not-ssh", exe)
assert.Equal(t, []string{"-p", "12345", "git@host.com", "git-lfs-authenticate repo download"}, args)
}
func TestSSHGetLFSExeAndArgsInvalidOptionsAsHost(t *testing.T) {
cli, err := NewClient(nil)
require.Nil(t, err)
u, err := url.Parse("ssh://-oProxyCommand=gnome-calculator/repo")
require.Nil(t, err)
assert.Equal(t, "-oProxyCommand=gnome-calculator", u.Host)
e := EndpointFromSshUrl(u)
t.Logf("ENDPOINT: %+v", e)
assert.Equal(t, "-oProxyCommand=gnome-calculator", e.SSHMetadata.UserAndHost)
assert.Equal(t, "repo", e.SSHMetadata.Path)
exe, args := sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, "download", "GET")
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{"--", "-oProxyCommand=gnome-calculator", "git-lfs-authenticate repo download"}, args)
}
func TestSSHGetLFSExeAndArgsInvalidOptionsAsHostWithCustomSSH(t *testing.T) {
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH": "not-ssh",
}, nil))
require.Nil(t, err)
u, err := url.Parse("ssh://--oProxyCommand=gnome-calculator/repo")
require.Nil(t, err)
assert.Equal(t, "--oProxyCommand=gnome-calculator", u.Host)
e := EndpointFromSshUrl(u)
t.Logf("ENDPOINT: %+v", e)
assert.Equal(t, "--oProxyCommand=gnome-calculator", e.SSHMetadata.UserAndHost)
assert.Equal(t, "repo", e.SSHMetadata.Path)
exe, args := sshGetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, "download", "GET")
assert.Equal(t, "not-ssh", exe)
assert.Equal(t, []string{"oProxyCommand=gnome-calculator", "git-lfs-authenticate repo download"}, args)
}
func TestSSHGetExeAndArgsInvalidOptionsAsHost(t *testing.T) {
cli, err := NewClient(nil)
require.Nil(t, err)
u, err := url.Parse("ssh://-oProxyCommand=gnome-calculator")
require.Nil(t, err)
assert.Equal(t, "-oProxyCommand=gnome-calculator", u.Host)
e := EndpointFromSshUrl(u)
t.Logf("ENDPOINT: %+v", e)
assert.Equal(t, "-oProxyCommand=gnome-calculator", e.SSHMetadata.UserAndHost)
assert.Equal(t, "", e.SSHMetadata.Path)
exe, args, needShell := sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata)
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{"--", "-oProxyCommand=gnome-calculator"}, args)
assert.Equal(t, false, needShell)
}
func TestSSHGetExeAndArgsInvalidOptionsAsPath(t *testing.T) {
cli, err := NewClient(nil)
require.Nil(t, err)
u, err := url.Parse("ssh://git@git-host.com/-oProxyCommand=gnome-calculator")
require.Nil(t, err)
assert.Equal(t, "git-host.com", u.Host)
e := EndpointFromSshUrl(u)
t.Logf("ENDPOINT: %+v", e)
assert.Equal(t, "git@git-host.com", e.SSHMetadata.UserAndHost)
assert.Equal(t, "-oProxyCommand=gnome-calculator", e.SSHMetadata.Path)
exe, args, needShell := sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata)
assert.Equal(t, "ssh", exe)
assert.Equal(t, []string{"--", "git@git-host.com"}, args)
assert.Equal(t, false, needShell)
}
func TestParseBareSSHUrl(t *testing.T) {
e := EndpointFromBareSshUrl("git@git-host.com:repo.git")
t.Logf("endpoint: %+v", e)
assert.Equal(t, "git@git-host.com", e.SSHMetadata.UserAndHost)
assert.Equal(t, "repo.git", e.SSHMetadata.Path)
e = EndpointFromBareSshUrl("git@git-host.com/should-be-a-colon.git")
t.Logf("endpoint: %+v", e)
assert.Equal(t, "", e.SSHMetadata.UserAndHost)
assert.Equal(t, "", e.SSHMetadata.Path)
e = EndpointFromBareSshUrl("-oProxyCommand=gnome-calculator")
t.Logf("endpoint: %+v", e)
assert.Equal(t, "", e.SSHMetadata.UserAndHost)
assert.Equal(t, "", e.SSHMetadata.Path)
e = EndpointFromBareSshUrl("git@git-host.com:-oProxyCommand=gnome-calculator")
t.Logf("endpoint: %+v", e)
assert.Equal(t, "git@git-host.com", e.SSHMetadata.UserAndHost)
assert.Equal(t, "-oProxyCommand=gnome-calculator", e.SSHMetadata.Path)
}
func TestSSHGetExeAndArgsPlinkCommand(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "plink.exe")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", plink + " user@foo.com"}, args)
}
func TestSSHGetExeAndArgsPlinkCommandCustomPort(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "plink")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Port = "8888"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", plink + " -P 8888 user@foo.com"}, args)
}
func TestSSHGetExeAndArgsTortoisePlinkCommand(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "tortoiseplink.exe")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", plink + " -batch user@foo.com"}, args)
}
func TestSSHGetExeAndArgsTortoisePlinkCommandCustomPort(t *testing.T) {
plink := filepath.Join("Users", "joebloggs", "bin", "tortoiseplink")
cli, err := NewClient(NewContext(nil, map[string]string{
"GIT_SSH_COMMAND": plink,
}, nil))
require.Nil(t, err)
endpoint := Endpoint{Operation: "download"}
endpoint.SSHMetadata.UserAndHost = "user@foo.com"
endpoint.SSHMetadata.Port = "8888"
exe, args := sshFormatArgs(sshGetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &endpoint.SSHMetadata))
assert.Equal(t, "sh", exe)
assert.Equal(t, []string{"-c", plink + " -batch -P 8888 user@foo.com"}, args)
}