sftpgo-mirror/internal/ftpd/internal_test.go
Nicola Murino 2710207779
update jquery, go deps, actions/setup-go to v4
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
2023-03-15 18:44:08 +01:00

1137 lines
39 KiB
Go

// Copyright (C) 2019-2023 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ftpd
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/eikenb/pipeat"
ftpserver "github.com/fclairamb/ftpserverlib"
"github.com/pires/go-proxyproto"
"github.com/sftpgo/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/drakkan/sftpgo/v2/internal/common"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/util"
"github.com/drakkan/sftpgo/v2/internal/vfs"
)
const (
ftpsCert = `-----BEGIN CERTIFICATE-----
MIICHTCCAaKgAwIBAgIUHnqw7QnB1Bj9oUsNpdb+ZkFPOxMwCgYIKoZIzj0EAwIw
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDAyMDQwOTUzMDRaFw0zMDAyMDEw
OTUzMDRaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYD
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwdjAQBgcqhkjOPQIBBgUrgQQA
IgNiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVqWvrJ51t5OxV0v25NsOgR82CA
NXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIVCzgWkxiz7XE4lgUwX44FCXZM
3+JeUbKjUzBRMB0GA1UdDgQWBBRhLw+/o3+Z02MI/d4tmaMui9W16jAfBgNVHSME
GDAWgBRhLw+/o3+Z02MI/d4tmaMui9W16jAPBgNVHRMBAf8EBTADAQH/MAoGCCqG
SM49BAMCA2kAMGYCMQDqLt2lm8mE+tGgtjDmtFgdOcI72HSbRQ74D5rYTzgST1rY
/8wTi5xl8TiFUyLMUsICMQC5ViVxdXbhuG7gX6yEqSkMKZICHpO8hqFwOD/uaFVI
dV4vKmHUzwK/eIx+8Ay3neE=
-----END CERTIFICATE-----`
ftpsKey = `-----BEGIN EC PARAMETERS-----
BgUrgQQAIg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDCfMNsN6miEE3rVyUPwElfiJSWaR5huPCzUenZOfJT04GAcQdWvEju3
UM2lmBLIXpGgBwYFK4EEACKhZANiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVq
WvrJ51t5OxV0v25NsOgR82CANXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIV
CzgWkxiz7XE4lgUwX44FCXZM3+JeUbI=
-----END EC PRIVATE KEY-----`
caCRT = `-----BEGIN CERTIFICATE-----
MIIE5jCCAs6gAwIBAgIBATANBgkqhkiG9w0BAQsFADATMREwDwYDVQQDEwhDZXJ0
QXV0aDAeFw0yMzAxMDMxMDIwNDdaFw0zMzAxMDMxMDMwNDZaMBMxETAPBgNVBAMT
CENlcnRBdXRoMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxq6Wl1Ih
hgvGdM8M2IVI7dwnv3yShJygZsnREQSEW0xeWJL5DtNeHCME5WByFUAlZKpePtW8
TNwln9DYDtgNSMiWwvO/wR0mXsyU8Ma4ZBMlX0oOkWo1Ff/M/u8YY9X78Vvwd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nH
uaPG3o9EBDf5eFKi76o+pVtqxrwhY88M/Yw0ykEA6Nf7RCo2ucdemg==
-----END RSA PRIVATE KEY-----`
)
var (
configDir = filepath.Join(".", "..", "..")
)
type mockFTPClientContext struct {
lastDataChannel ftpserver.DataChannel
remoteIP string
localIP string
}
func (cc mockFTPClientContext) Path() string {
return ""
}
func (cc mockFTPClientContext) SetPath(name string) {}
func (cc mockFTPClientContext) SetListPath(name string) {}
func (cc mockFTPClientContext) SetDebug(debug bool) {}
func (cc mockFTPClientContext) Debug() bool {
return false
}
func (cc mockFTPClientContext) ID() uint32 {
return 1
}
func (cc mockFTPClientContext) RemoteAddr() net.Addr {
ip := "127.0.0.1"
if cc.remoteIP != "" {
ip = cc.remoteIP
}
return &net.IPAddr{IP: net.ParseIP(ip)}
}
func (cc mockFTPClientContext) LocalAddr() net.Addr {
ip := "127.0.0.1"
if cc.localIP != "" {
ip = cc.localIP
}
return &net.IPAddr{IP: net.ParseIP(ip)}
}
func (cc mockFTPClientContext) GetClientVersion() string {
return "mock version"
}
func (cc mockFTPClientContext) Close() error {
return nil
}
func (cc mockFTPClientContext) HasTLSForControl() bool {
return false
}
func (cc mockFTPClientContext) HasTLSForTransfers() bool {
return false
}
func (cc mockFTPClientContext) SetTLSRequirement(requirement ftpserver.TLSRequirement) error {
return nil
}
func (cc mockFTPClientContext) GetLastCommand() string {
return ""
}
func (cc mockFTPClientContext) GetLastDataChannel() ftpserver.DataChannel {
return cc.lastDataChannel
}
// MockOsFs mockable OsFs
type MockOsFs struct {
vfs.Fs
err error
statErr error
isAtomicUploadSupported bool
}
// Name returns the name for the Fs implementation
func (fs MockOsFs) Name() string {
return "mockOsFs"
}
// IsUploadResumeSupported returns true if resuming uploads is supported
func (MockOsFs) IsUploadResumeSupported() bool {
return false
}
// IsAtomicUploadSupported returns true if atomic upload is supported
func (fs MockOsFs) IsAtomicUploadSupported() bool {
return fs.isAtomicUploadSupported
}
// Stat returns a FileInfo describing the named file
func (fs MockOsFs) Stat(name string) (os.FileInfo, error) {
if fs.statErr != nil {
return nil, fs.statErr
}
return os.Stat(name)
}
// Lstat returns a FileInfo describing the named file
func (fs MockOsFs) Lstat(name string) (os.FileInfo, error) {
if fs.statErr != nil {
return nil, fs.statErr
}
return os.Lstat(name)
}
// Remove removes the named file or (empty) directory.
func (fs MockOsFs) Remove(name string, isDir bool) error {
if fs.err != nil {
return fs.err
}
return os.Remove(name)
}
// Rename renames (moves) source to target
func (fs MockOsFs) Rename(source, target string) (int, int64, error) {
if fs.err != nil {
return -1, -1, fs.err
}
err := os.Rename(source, target)
return -1, -1, err
}
func newMockOsFs(err, statErr error, atomicUpload bool, connectionID, rootDir string) vfs.Fs {
return &MockOsFs{
Fs: vfs.NewOsFs(connectionID, rootDir, ""),
err: err,
statErr: statErr,
isAtomicUploadSupported: atomicUpload,
}
}
func TestInitialization(t *testing.T) {
oldMgr := certMgr
certMgr = nil
binding := Binding{
Port: 2121,
}
c := &Configuration{
Bindings: []Binding{binding},
CertificateFile: "acert",
CertificateKeyFile: "akey",
}
assert.False(t, binding.HasProxy())
assert.Equal(t, "Disabled", binding.GetTLSDescription())
err := c.Initialize(configDir)
assert.Error(t, err)
c.CertificateFile = ""
c.CertificateKeyFile = ""
c.BannerFile = "afile"
server := NewServer(c, configDir, binding, 0)
assert.Equal(t, "", server.initialMsg)
_, err = server.GetTLSConfig()
assert.Error(t, err)
binding.TLSMode = 1
server = NewServer(c, configDir, binding, 0)
_, err = server.GetSettings()
assert.Error(t, err)
binding.PassiveConnectionsSecurity = 100
binding.ActiveConnectionsSecurity = 100
server = NewServer(c, configDir, binding, 0)
_, err = server.GetSettings()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid passive_connections_security")
}
binding.PassiveConnectionsSecurity = 1
server = NewServer(c, configDir, binding, 0)
_, err = server.GetSettings()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid active_connections_security")
}
binding = Binding{
Port: 2121,
ForcePassiveIP: "192.168.1",
}
server = NewServer(c, configDir, binding, 0)
_, err = server.GetSettings()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "is not valid")
}
binding.ForcePassiveIP = "::ffff:192.168.89.9"
err = binding.checkPassiveIP()
assert.NoError(t, err)
assert.Equal(t, "192.168.89.9", binding.ForcePassiveIP)
binding.ForcePassiveIP = "::1"
err = binding.checkPassiveIP()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "is not a valid IPv4 address")
}
err = ReloadCertificateMgr()
assert.NoError(t, err)
certMgr = oldMgr
binding = Binding{
Port: 2121,
ClientAuthType: 1,
}
server = NewServer(c, configDir, binding, 0)
cfg, err := server.GetTLSConfig()
assert.NoError(t, err)
assert.Equal(t, tls.RequireAndVerifyClientCert, cfg.ClientAuth)
}
func TestServerGetSettings(t *testing.T) {
oldConfig := common.Config
binding := Binding{
Port: 2121,
ApplyProxyConfig: true,
}
c := &Configuration{
Bindings: []Binding{binding},
PassivePortRange: PortRange{
Start: 10000,
End: 11000,
},
}
assert.False(t, binding.HasProxy())
server := NewServer(c, configDir, binding, 0)
settings, err := server.GetSettings()
assert.NoError(t, err)
assert.Equal(t, 10000, settings.PassiveTransferPortRange.Start)
assert.Equal(t, 11000, settings.PassiveTransferPortRange.End)
common.Config.ProxyProtocol = 1
_, err = server.GetSettings()
assert.Error(t, err)
server.binding.Port = 8021
assert.Equal(t, "Plain and explicit", binding.GetTLSDescription())
binding.TLSMode = 1
assert.Equal(t, "Explicit required", binding.GetTLSDescription())
binding.TLSMode = 2
assert.Equal(t, "Implicit", binding.GetTLSDescription())
certPath := filepath.Join(os.TempDir(), "test.crt")
keyPath := filepath.Join(os.TempDir(), "test.key")
err = os.WriteFile(certPath, []byte(ftpsCert), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(keyPath, []byte(ftpsKey), os.ModePerm)
assert.NoError(t, err)
common.Config.ProxyAllowed = nil
c.CertificateFile = certPath
c.CertificateKeyFile = keyPath
server = NewServer(c, configDir, binding, 0)
server.binding.Port = 9021
settings, err = server.GetSettings()
assert.NoError(t, err)
assert.NotNil(t, settings.Listener)
listener, err := net.Listen("tcp", ":0")
assert.NoError(t, err)
listener, err = server.WrapPassiveListener(listener)
assert.NoError(t, err)
_, ok := listener.(*proxyproto.Listener)
assert.True(t, ok)
err = os.Remove(certPath)
assert.NoError(t, err)
err = os.Remove(keyPath)
assert.NoError(t, err)
common.Config = oldConfig
}
func TestUserInvalidParams(t *testing.T) {
u := dataprovider.User{
BaseUser: sdk.BaseUser{
HomeDir: "invalid",
},
}
binding := Binding{
Port: 2121,
}
c := &Configuration{
Bindings: []Binding{binding},
PassivePortRange: PortRange{
Start: 10000,
End: 11000,
},
}
server := NewServer(c, configDir, binding, 3)
_, err := server.validateUser(u, mockFTPClientContext{}, dataprovider.LoginMethodPassword)
assert.Error(t, err)
u.Username = "a"
u.HomeDir = filepath.Clean(os.TempDir())
subDir := "subdir"
mappedPath1 := filepath.Join(os.TempDir(), "vdir1")
vdirPath1 := "/vdir1"
mappedPath2 := filepath.Join(os.TempDir(), "vdir1", subDir)
vdirPath2 := "/vdir2"
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
BaseVirtualFolder: vfs.BaseVirtualFolder{
MappedPath: mappedPath1,
},
VirtualPath: vdirPath1,
})
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
BaseVirtualFolder: vfs.BaseVirtualFolder{
MappedPath: mappedPath2,
},
VirtualPath: vdirPath2,
})
_, err = server.validateUser(u, mockFTPClientContext{}, dataprovider.LoginMethodPassword)
assert.Error(t, err)
u.VirtualFolders = nil
_, err = server.validateUser(u, mockFTPClientContext{}, dataprovider.LoginMethodPassword)
assert.Error(t, err)
}
func TestFTPMode(t *testing.T) {
connection := &Connection{
BaseConnection: common.NewBaseConnection("", common.ProtocolFTP, "", "", dataprovider.User{}),
}
assert.Empty(t, connection.getFTPMode())
connection.clientContext = mockFTPClientContext{lastDataChannel: ftpserver.DataChannelActive}
assert.Equal(t, "active", connection.getFTPMode())
connection.clientContext = mockFTPClientContext{lastDataChannel: ftpserver.DataChannelPassive}
assert.Equal(t, "passive", connection.getFTPMode())
connection.clientContext = mockFTPClientContext{lastDataChannel: 0}
assert.Empty(t, connection.getFTPMode())
}
func TestClientVersion(t *testing.T) {
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("2_%v", mockCC.ID())
user := dataprovider.User{}
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
err := common.Connections.Add(connection)
assert.NoError(t, err)
stats := common.Connections.GetStats("")
if assert.Len(t, stats, 1) {
assert.Equal(t, "mock version", stats[0].ClientVersion)
common.Connections.Remove(connection.GetID())
}
assert.Len(t, common.Connections.GetStats(""), 0)
}
func TestDriverMethodsNotImplemented(t *testing.T) {
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("2_%v", mockCC.ID())
user := dataprovider.User{}
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
_, err := connection.Create("")
assert.EqualError(t, err, errNotImplemented.Error())
err = connection.MkdirAll("", os.ModePerm)
assert.EqualError(t, err, errNotImplemented.Error())
_, err = connection.Open("")
assert.EqualError(t, err, errNotImplemented.Error())
_, err = connection.OpenFile("", 0, os.ModePerm)
assert.EqualError(t, err, errNotImplemented.Error())
err = connection.RemoveAll("")
assert.EqualError(t, err, errNotImplemented.Error())
assert.Equal(t, connection.GetID(), connection.Name())
}
func TestResolvePathErrors(t *testing.T) {
user := dataprovider.User{
BaseUser: sdk.BaseUser{
HomeDir: "invalid",
},
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("%v", mockCC.ID())
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
err := connection.Mkdir("", os.ModePerm)
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.Remove("")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.RemoveDir("")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.Rename("", "")
assert.ErrorIs(t, err, common.ErrOpUnsupported)
err = connection.Symlink("", "")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
_, err = connection.Stat("")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.Chmod("", os.ModePerm)
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.Chtimes("", time.Now(), time.Now())
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
_, err = connection.ReadDir("")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
_, err = connection.GetHandle("", 0, 0)
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
_, err = connection.GetAvailableSpace("")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
}
func TestUploadFileStatError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("this test is not available on Windows")
}
user := dataprovider.User{
BaseUser: sdk.BaseUser{
Username: "user",
HomeDir: filepath.Clean(os.TempDir()),
},
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("%v", mockCC.ID())
fs := vfs.NewOsFs(connID, user.HomeDir, "")
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
testFile := filepath.Join(user.HomeDir, "test", "testfile")
err := os.MkdirAll(filepath.Dir(testFile), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(testFile, []byte("data"), os.ModePerm)
assert.NoError(t, err)
err = os.Chmod(filepath.Dir(testFile), 0001)
assert.NoError(t, err)
_, err = connection.uploadFile(fs, testFile, "test", 0)
assert.Error(t, err)
err = os.Chmod(filepath.Dir(testFile), os.ModePerm)
assert.NoError(t, err)
err = os.RemoveAll(filepath.Dir(testFile))
assert.NoError(t, err)
}
func TestAVBLErrors(t *testing.T) {
user := dataprovider.User{
BaseUser: sdk.BaseUser{
Username: "user",
HomeDir: filepath.Clean(os.TempDir()),
},
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("%v", mockCC.ID())
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
_, err := connection.GetAvailableSpace("/")
assert.NoError(t, err)
_, err = connection.GetAvailableSpace("/missing-path")
assert.Error(t, err)
assert.True(t, errors.Is(err, fs.ErrNotExist))
}
func TestUploadOverwriteErrors(t *testing.T) {
user := dataprovider.User{
BaseUser: sdk.BaseUser{
Username: "user",
HomeDir: filepath.Clean(os.TempDir()),
},
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("%v", mockCC.ID())
fs := newMockOsFs(nil, nil, false, connID, user.GetHomeDir())
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
flags := 0
flags |= os.O_APPEND
_, err := connection.handleFTPUploadToExistingFile(fs, flags, "", "", 0, "")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrOpUnsupported.Error())
}
f, err := os.CreateTemp("", "temp")
assert.NoError(t, err)
err = f.Close()
assert.NoError(t, err)
flags = 0
flags |= os.O_CREATE
flags |= os.O_TRUNC
tr, err := connection.handleFTPUploadToExistingFile(fs, flags, f.Name(), f.Name(), 123, f.Name())
if assert.NoError(t, err) {
transfer := tr.(*transfer)
transfers := connection.GetTransfers()
if assert.Equal(t, 1, len(transfers)) {
assert.Equal(t, transfers[0].ID, transfer.GetID())
assert.Equal(t, int64(123), transfer.InitialSize)
err = transfer.Close()
assert.NoError(t, err)
assert.Equal(t, 0, len(connection.GetTransfers()))
}
}
err = os.Remove(f.Name())
assert.NoError(t, err)
_, err = connection.handleFTPUploadToExistingFile(fs, os.O_TRUNC, filepath.Join(os.TempDir(), "sub", "file"),
filepath.Join(os.TempDir(), "sub", "file1"), 0, "/sub/file1")
assert.Error(t, err)
fs = vfs.NewOsFs(connID, user.GetHomeDir(), "")
_, err = connection.handleFTPUploadToExistingFile(fs, 0, "missing1", "missing2", 0, "missing")
assert.Error(t, err)
}
func TestTransferErrors(t *testing.T) {
testfile := "testfile"
file, err := os.Create(testfile)
assert.NoError(t, err)
user := dataprovider.User{
BaseUser: sdk.BaseUser{
Username: "user",
HomeDir: filepath.Clean(os.TempDir()),
},
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
mockCC := mockFTPClientContext{}
connID := fmt.Sprintf("%v", mockCC.ID())
fs := newMockOsFs(nil, nil, false, connID, user.GetHomeDir())
connection := &Connection{
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, "", "", user),
clientContext: mockCC,
}
baseTransfer := common.NewBaseTransfer(file, connection.BaseConnection, nil, file.Name(), file.Name(), testfile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
tr := newTransfer(baseTransfer, nil, nil, 0)
err = tr.Close()
assert.NoError(t, err)
_, err = tr.Seek(10, 0)
assert.Error(t, err)
buf := make([]byte, 64)
_, err = tr.Read(buf)
assert.Error(t, err)
err = tr.Close()
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrTransferClosed.Error())
}
assert.Len(t, connection.GetTransfers(), 0)
r, _, err := pipeat.Pipe()
assert.NoError(t, err)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testfile, testfile, testfile,
common.TransferUpload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
tr = newTransfer(baseTransfer, nil, r, 10)
pos, err := tr.Seek(10, 0)
assert.NoError(t, err)
assert.Equal(t, pos, tr.expectedOffset)
err = tr.closeIO()
assert.NoError(t, err)
r, w, err := pipeat.Pipe()
assert.NoError(t, err)
pipeWriter := vfs.NewPipeWriter(w)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testfile, testfile, testfile,
common.TransferUpload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
tr = newTransfer(baseTransfer, pipeWriter, nil, 0)
err = r.Close()
assert.NoError(t, err)
errFake := fmt.Errorf("fake upload error")
go func() {
time.Sleep(100 * time.Millisecond)
pipeWriter.Done(errFake)
}()
err = tr.closeIO()
assert.EqualError(t, err, errFake.Error())
_, err = tr.Seek(1, 0)
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrOpUnsupported.Error())
}
err = os.Remove(testfile)
assert.NoError(t, err)
}
func TestVerifyTLSConnection(t *testing.T) {
oldCertMgr := certMgr
caCrlPath := filepath.Join(os.TempDir(), "testcrl.crt")
certPath := filepath.Join(os.TempDir(), "test.crt")
keyPath := filepath.Join(os.TempDir(), "test.key")
err := os.WriteFile(caCrlPath, []byte(caCRL), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(certPath, []byte(ftpsCert), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(keyPath, []byte(ftpsKey), os.ModePerm)
assert.NoError(t, err)
keyPairs := []common.TLSKeyPair{
{
Cert: certPath,
Key: keyPath,
ID: common.DefaultTLSKeyPaidID,
},
}
certMgr, err = common.NewCertManager(keyPairs, "", "ftp_test")
assert.NoError(t, err)
certMgr.SetCARevocationLists([]string{caCrlPath})
err = certMgr.LoadCRLs()
assert.NoError(t, err)
crt, err := tls.X509KeyPair([]byte(client1Crt), []byte(client1Key))
assert.NoError(t, err)
x509crt, err := x509.ParseCertificate(crt.Certificate[0])
assert.NoError(t, err)
server := Server{}
state := tls.ConnectionState{
PeerCertificates: []*x509.Certificate{x509crt},
}
err = server.verifyTLSConnection(state)
assert.Error(t, err) // no verified certification chain
crt, err = tls.X509KeyPair([]byte(caCRT), []byte(caKey))
assert.NoError(t, err)
x509CAcrt, err := x509.ParseCertificate(crt.Certificate[0])
assert.NoError(t, err)
state.VerifiedChains = append(state.VerifiedChains, []*x509.Certificate{x509crt, x509CAcrt})
err = server.verifyTLSConnection(state)
assert.NoError(t, err)
crt, err = tls.X509KeyPair([]byte(client2Crt), []byte(client2Key))
assert.NoError(t, err)
x509crtRevoked, err := x509.ParseCertificate(crt.Certificate[0])
assert.NoError(t, err)
state.VerifiedChains = append(state.VerifiedChains, []*x509.Certificate{x509crtRevoked, x509CAcrt})
state.PeerCertificates = []*x509.Certificate{x509crtRevoked}
err = server.verifyTLSConnection(state)
assert.EqualError(t, err, common.ErrCrtRevoked.Error())
err = os.Remove(caCrlPath)
assert.NoError(t, err)
err = os.Remove(certPath)
assert.NoError(t, err)
err = os.Remove(keyPath)
assert.NoError(t, err)
certMgr = oldCertMgr
}
func TestCiphers(t *testing.T) {
b := Binding{
TLSCipherSuites: []string{},
}
b.setCiphers()
require.Nil(t, b.ciphers)
b.TLSCipherSuites = []string{"TLS_AES_128_GCM_SHA256", "TLS_AES_256_GCM_SHA384"}
b.setCiphers()
require.Len(t, b.ciphers, 2)
require.Equal(t, []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384}, b.ciphers)
}
func TestPassiveIPResolver(t *testing.T) {
b := Binding{
PassiveIPOverrides: []PassiveIPOverride{
{},
},
}
err := b.checkPassiveIP()
assert.Error(t, err)
assert.Contains(t, err.Error(), "passive IP networks override cannot be empty")
b = Binding{
PassiveIPOverrides: []PassiveIPOverride{
{
IP: "invalid ip",
},
},
}
err = b.checkPassiveIP()
assert.Error(t, err)
assert.Contains(t, err.Error(), "is not valid")
b = Binding{
PassiveIPOverrides: []PassiveIPOverride{
{
IP: "192.168.1.1",
Networks: []string{"192.168.1.0/24", "invalid cidr"},
},
},
}
err = b.checkPassiveIP()
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid passive IP networks override")
b = Binding{
ForcePassiveIP: "192.168.2.1",
PassiveIPOverrides: []PassiveIPOverride{
{
IP: "::ffff:192.168.1.1",
Networks: []string{"192.168.1.0/24"},
},
},
}
err = b.checkPassiveIP()
assert.NoError(t, err)
assert.NotEmpty(t, b.PassiveIPOverrides[0].GetNetworksAsString())
assert.Equal(t, "192.168.1.1", b.PassiveIPOverrides[0].IP)
require.Len(t, b.PassiveIPOverrides[0].parsedNetworks, 1)
ip := net.ParseIP("192.168.1.2")
assert.True(t, b.PassiveIPOverrides[0].parsedNetworks[0](ip))
ip = net.ParseIP("192.168.0.2")
assert.False(t, b.PassiveIPOverrides[0].parsedNetworks[0](ip))
mockCC := mockFTPClientContext{
remoteIP: "192.168.1.10",
localIP: "192.168.1.3",
}
passiveIP, err := b.passiveIPResolver(mockCC)
assert.NoError(t, err)
assert.Equal(t, "192.168.1.1", passiveIP)
b.PassiveIPOverrides[0].IP = ""
passiveIP, err = b.passiveIPResolver(mockCC)
assert.NoError(t, err)
assert.Equal(t, "192.168.1.3", passiveIP)
mockCC.remoteIP = "172.16.2.3"
passiveIP, err = b.passiveIPResolver(mockCC)
assert.NoError(t, err)
assert.Equal(t, b.ForcePassiveIP, passiveIP)
}
func TestRelativePath(t *testing.T) {
rel := getPathRelativeTo("/testpath", "/testpath")
assert.Empty(t, rel)
rel = getPathRelativeTo("/", "/")
assert.Empty(t, rel)
rel = getPathRelativeTo("/", "/dir/sub")
assert.Equal(t, "dir/sub", rel)
rel = getPathRelativeTo("./", "/dir/sub")
assert.Equal(t, "/dir/sub", rel)
rel = getPathRelativeTo("/sub", "/dir/sub")
assert.Equal(t, "../dir/sub", rel)
rel = getPathRelativeTo("/dir", "/dir/sub")
assert.Equal(t, "sub", rel)
rel = getPathRelativeTo("/dir/sub", "/dir")
assert.Equal(t, "../", rel)
rel = getPathRelativeTo("dir", "/dir1")
assert.Equal(t, "/dir1", rel)
rel = getPathRelativeTo("", "/dir2")
assert.Equal(t, "dir2", rel)
rel = getPathRelativeTo(".", "/dir2")
assert.Equal(t, "/dir2", rel)
rel = getPathRelativeTo("/dir3", "dir3")
assert.Equal(t, "dir3", rel)
}
func TestConfigsFromProvider(t *testing.T) {
err := dataprovider.UpdateConfigs(nil, "", "", "")
assert.NoError(t, err)
c := Configuration{}
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
configs := dataprovider.Configs{
ACME: &dataprovider.ACMEConfigs{
Domain: "domain.com",
Email: "info@domain.com",
HTTP01Challenge: dataprovider.ACMEHTTP01Challenge{Port: 80},
Protocols: 2,
},
}
err = dataprovider.UpdateConfigs(&configs, "", "", "")
assert.NoError(t, err)
util.CertsBasePath = ""
// crt and key empty
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
util.CertsBasePath = filepath.Clean(os.TempDir())
// crt not found
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
keyPairs := c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 0)
crtPath := filepath.Join(util.CertsBasePath, util.SanitizeDomain(configs.ACME.Domain)+".crt")
err = os.WriteFile(crtPath, nil, 0666)
assert.NoError(t, err)
// key not found
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
keyPairs = c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 0)
keyPath := filepath.Join(util.CertsBasePath, util.SanitizeDomain(configs.ACME.Domain)+".key")
err = os.WriteFile(keyPath, nil, 0666)
assert.NoError(t, err)
// acme cert used
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Equal(t, configs.ACME.Domain, c.acmeDomain)
keyPairs = c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 1)
// protocols does not match
configs.ACME.Protocols = 5
err = dataprovider.UpdateConfigs(&configs, "", "", "")
assert.NoError(t, err)
c.acmeDomain = ""
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
keyPairs = c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 0)
err = os.Remove(crtPath)
assert.NoError(t, err)
err = os.Remove(keyPath)
assert.NoError(t, err)
util.CertsBasePath = ""
err = dataprovider.UpdateConfigs(nil, "", "", "")
assert.NoError(t, err)
}
func TestPassiveHost(t *testing.T) {
b := Binding{
PassiveHost: "invalid hostname",
}
_, err := b.getPassiveIP(nil)
assert.Error(t, err)
b.PassiveHost = "localhost"
ip, err := b.getPassiveIP(nil)
assert.NoError(t, err, ip)
assert.Equal(t, "127.0.0.1", ip)
}