2019-05-10 20:00:18 +02:00
|
|
|
// Serve sftp tests set up a server and run the integration tests
|
|
|
|
// for the sftp remote against it.
|
|
|
|
//
|
|
|
|
// We skip tests on platforms with troublesome character mappings
|
|
|
|
|
|
|
|
//+build !windows,!darwin,!plan9
|
|
|
|
|
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2019-05-10 20:00:18 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
2019-07-28 19:47:38 +02:00
|
|
|
_ "github.com/rclone/rclone/backend/local"
|
2019-08-02 11:56:27 +02:00
|
|
|
"github.com/rclone/rclone/cmd/serve/servetest"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
2019-12-24 18:34:35 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-10 20:00:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testBindAddress = "localhost:0"
|
|
|
|
testUser = "testuser"
|
|
|
|
testPass = "testpass"
|
|
|
|
)
|
|
|
|
|
|
|
|
// check interfaces
|
|
|
|
var (
|
|
|
|
_ sftp.FileReader = vfsHandler{}
|
|
|
|
_ sftp.FileWriter = vfsHandler{}
|
|
|
|
_ sftp.FileCmder = vfsHandler{}
|
|
|
|
_ sftp.FileLister = vfsHandler{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestSftp runs the sftp server then runs the unit tests for the
|
|
|
|
// sftp remote against it.
|
|
|
|
func TestSftp(t *testing.T) {
|
2019-08-02 11:56:27 +02:00
|
|
|
// Configure and start the server
|
|
|
|
start := func(f fs.Fs) (configmap.Simple, func()) {
|
|
|
|
opt := DefaultOpt
|
|
|
|
opt.ListenAddr = testBindAddress
|
|
|
|
opt.User = testUser
|
|
|
|
opt.Pass = testPass
|
2019-05-10 20:00:18 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
w := newServer(context.Background(), f, &opt)
|
2019-12-24 18:34:35 +01:00
|
|
|
require.NoError(t, w.serve())
|
2019-05-10 20:00:18 +02:00
|
|
|
|
2019-08-02 11:56:27 +02:00
|
|
|
// Read the host and port we started on
|
|
|
|
addr := w.Addr()
|
|
|
|
colon := strings.LastIndex(addr, ":")
|
2019-05-10 20:00:18 +02:00
|
|
|
|
2019-08-02 11:56:27 +02:00
|
|
|
// Config for the backend we'll use to connect to the server
|
|
|
|
config := configmap.Simple{
|
|
|
|
"type": "sftp",
|
|
|
|
"user": testUser,
|
|
|
|
"pass": obscure.MustObscure(testPass),
|
|
|
|
"host": addr[:colon],
|
|
|
|
"port": addr[colon+1:],
|
|
|
|
}
|
2019-05-10 20:00:18 +02:00
|
|
|
|
2019-08-02 11:56:27 +02:00
|
|
|
// return a stop function
|
|
|
|
return config, func() {
|
|
|
|
w.Close()
|
|
|
|
w.Wait()
|
|
|
|
}
|
2019-05-10 20:00:18 +02:00
|
|
|
}
|
2019-08-02 11:56:27 +02:00
|
|
|
|
|
|
|
servetest.Run(t, "sftp", start)
|
2019-05-10 20:00:18 +02:00
|
|
|
}
|