mirror of
https://github.com/nwtgck/handy-sshd.git
synced 2025-06-08 07:03:04 +00:00
test SFTP
This commit is contained in:
parent
3b0b650318
commit
2295abacdd
2 changed files with 48 additions and 5 deletions
|
@ -61,7 +61,7 @@ func TestAllPermissionsAllowed(t *testing.T) {
|
||||||
assertLocalPortForwarding(t, client)
|
assertLocalPortForwarding(t, client)
|
||||||
assertExec(t, client)
|
assertExec(t, client)
|
||||||
assertPtyTerminal(t, client)
|
assertPtyTerminal(t, client)
|
||||||
// TODO: sftp
|
assertSftp(t, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyPassword(t *testing.T) {
|
func TestEmptyPassword(t *testing.T) {
|
||||||
|
@ -173,7 +173,7 @@ func TestAllowExecute(t *testing.T) {
|
||||||
assertNoLocalPortForwarding(t, client)
|
assertNoLocalPortForwarding(t, client)
|
||||||
assertExec(t, client)
|
assertExec(t, client)
|
||||||
assertPtyTerminal(t, client)
|
assertPtyTerminal(t, client)
|
||||||
// TODO: no sftp
|
assertNoSftp(t, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllowTcpipForward(t *testing.T) {
|
func TestAllowTcpipForward(t *testing.T) {
|
||||||
|
@ -204,7 +204,7 @@ func TestAllowTcpipForward(t *testing.T) {
|
||||||
assertNoLocalPortForwarding(t, client)
|
assertNoLocalPortForwarding(t, client)
|
||||||
assertNoExec(t, client)
|
assertNoExec(t, client)
|
||||||
assertNoPtyTerminal(t, client)
|
assertNoPtyTerminal(t, client)
|
||||||
// TODO: no sftp
|
assertNoSftp(t, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllowDirectTcpip(t *testing.T) {
|
func TestAllowDirectTcpip(t *testing.T) {
|
||||||
|
@ -235,7 +235,36 @@ func TestAllowDirectTcpip(t *testing.T) {
|
||||||
assertLocalPortForwarding(t, client)
|
assertLocalPortForwarding(t, client)
|
||||||
assertNoExec(t, client)
|
assertNoExec(t, client)
|
||||||
assertNoPtyTerminal(t, client)
|
assertNoPtyTerminal(t, client)
|
||||||
// TODO: no sftp
|
assertNoSftp(t, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: TestAllowSftp
|
func TestAllowSftp(t *testing.T) {
|
||||||
|
rootCmd := RootCmd()
|
||||||
|
port := getAvailableTcpPort()
|
||||||
|
rootCmd.SetArgs([]string{"--port", strconv.Itoa(port), "--user", "john:mypassword", "--allow-sftp"})
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
go func() {
|
||||||
|
var stderrBuf bytes.Buffer
|
||||||
|
rootCmd.SetErr(&stderrBuf)
|
||||||
|
rootCmd.ExecuteContext(ctx)
|
||||||
|
}()
|
||||||
|
waitTCPServer(port)
|
||||||
|
sshClientConfig := &ssh.ClientConfig{
|
||||||
|
User: "john",
|
||||||
|
Auth: []ssh.AuthMethod{ssh.Password("mypassword")},
|
||||||
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
address := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
|
||||||
|
client, err := ssh.Dial("tcp", address, sshClientConfig)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer client.Close()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assertNoRemotePortForwarding(t, client)
|
||||||
|
assertNoLocalPortForwarding(t, client)
|
||||||
|
assertNoExec(t, client)
|
||||||
|
assertNoPtyTerminal(t, client)
|
||||||
|
assertSftp(t, client)
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"github.com/pkg/sftp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"io"
|
"io"
|
||||||
|
@ -162,3 +163,16 @@ func assertNoRemotePortForwarding(t *testing.T, client *ssh.Client) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, "ssh: tcpip-forward request denied by peer", err.Error())
|
assert.Equal(t, "ssh: tcpip-forward request denied by peer", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertSftp(t *testing.T, client *ssh.Client) {
|
||||||
|
sftpClient, err := sftp.NewClient(client)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
_, err = sftpClient.Getwd()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertNoSftp(t *testing.T, client *ssh.Client) {
|
||||||
|
_, err := sftp.NewClient(client)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, "ssh: subsystem request failed", err.Error())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue