2019-07-20 10:26:52 +00:00
|
|
|
package sftpd_test
|
|
|
|
|
|
|
|
import (
|
2019-12-29 22:27:32 +00:00
|
|
|
"bufio"
|
2019-10-09 15:33:30 +00:00
|
|
|
"bytes"
|
2019-07-20 10:26:52 +00:00
|
|
|
"crypto/rand"
|
2019-10-09 15:33:30 +00:00
|
|
|
"crypto/sha256"
|
2019-11-18 22:30:37 +00:00
|
|
|
"crypto/sha512"
|
2020-01-06 20:42:41 +00:00
|
|
|
"encoding/json"
|
2019-07-20 10:26:52 +00:00
|
|
|
"fmt"
|
2019-11-18 22:30:37 +00:00
|
|
|
"hash"
|
2019-07-20 10:26:52 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-11-16 09:23:41 +00:00
|
|
|
"math"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-08-24 12:41:15 +00:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2019-07-20 10:26:52 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-11-15 11:15:07 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
"testing"
|
2019-07-26 09:34:44 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2019-11-13 10:36:21 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/pkg/sftp"
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-31 15:06:12 +00:00
|
|
|
logSender = "sftpdTesting"
|
|
|
|
sftpServerAddr = "127.0.0.1:2022"
|
|
|
|
defaultUsername = "test_user_sftp"
|
|
|
|
defaultPassword = "test_password"
|
|
|
|
testPubKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC03jj0D+djk7pxIf/0OhrxrchJTRZklofJ1NoIu4752Sq02mdXmarMVsqJ1cAjV5LBVy3D1F5U6XW4rppkXeVtd04Pxb09ehtH0pRRPaoHHlALiJt8CoMpbKYMA8b3KXPPriGxgGomvtU2T2RMURSwOZbMtpsugfjYSWenyYX+VORYhylWnSXL961LTyC21ehd6d6QnW9G7E5hYMITMY9TuQZz3bROYzXiTsgN0+g6Hn7exFQp50p45StUMfV/SftCMdCxlxuyGny2CrN/vfjO7xxOo2uv7q1qm10Q46KPWJQv+pgZ/OfL+EDjy07n5QVSKHlbx+2nT4Q0EgOSQaCTYwn3YjtABfIxWwgAFdyj6YlPulCL22qU4MYhDcA6PSBwDdf8hvxBfvsiHdM+JcSHvv8/VeJhk6CmnZxGY0fxBupov27z3yEO8nAg8k+6PaUiW1MSUfuGMF/ktB8LOstXsEPXSszuyXiOv4DaryOXUiSn7bmRqKcEFlJusO6aZP0= nicola@p1"
|
|
|
|
testPubKey1 = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCd60+/j+y8f0tLftihWV1YN9RSahMI9btQMDIMqts/jeNbD8jgoogM3nhF7KxfcaMKURuD47KC4Ey6iAJUJ0sWkSNNxOcIYuvA+5MlspfZDsa8Ag76Fe1vyz72WeHMHMeh/hwFo2TeIeIXg480T1VI6mzfDrVp2GzUx0SS0dMsQBjftXkuVR8YOiOwMCAH2a//M1OrvV7d/NBk6kBN0WnuIBb2jKm15PAA7+jQQG7tzwk2HedNH3jeL5GH31xkSRwlBczRK0xsCQXehAlx6cT/e/s44iJcJTHfpPKoSk6UAhPJYe7Z1QnuoawY9P9jQaxpyeImBZxxUEowhjpj2avBxKdRGBVK8R7EL8tSOeLbhdyWe5Mwc1+foEbq9Zz5j5Kd+hn3Wm1UnsGCrXUUUoZp1jnlNl0NakCto+5KmqnT9cHxaY+ix2RLUWAZyVFlRq71OYux1UHJnEJPiEI1/tr4jFBSL46qhQZv/TfpkfVW8FLz0lErfqu0gQEZnNHr3Fc= nicola@p1"
|
|
|
|
testPrivateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
|
2019-07-20 10:26:52 +00:00
|
|
|
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
|
|
|
|
NhAAAAAwEAAQAAAYEAtN449A/nY5O6cSH/9Doa8a3ISU0WZJaHydTaCLuO+dkqtNpnV5mq
|
|
|
|
zFbKidXAI1eSwVctw9ReVOl1uK6aZF3lbXdOD8W9PXobR9KUUT2qBx5QC4ibfAqDKWymDA
|
|
|
|
PG9ylzz64hsYBqJr7VNk9kTFEUsDmWzLabLoH42Elnp8mF/lTkWIcpVp0ly/etS08gttXo
|
|
|
|
XenekJ1vRuxOYWDCEzGPU7kGc920TmM14k7IDdPoOh5+3sRUKedKeOUrVDH1f0n7QjHQsZ
|
|
|
|
cbshp8tgqzf734zu8cTqNrr+6taptdEOOij1iUL/qYGfzny/hA48tO5+UFUih5W8ftp0+E
|
|
|
|
NBIDkkGgk2MJ92I7QAXyMVsIABXco+mJT7pQi9tqlODGIQ3AOj0gcA3X/Ib8QX77Ih3TPi
|
|
|
|
XEh77/P1XiYZOgpp2cRmNH8QbqaL9u898hDvJwIPJPuj2lIltTElH7hjBf5LQfCzrLV7BD
|
|
|
|
10rM7sl4jr+A2q8jl1Ikp+25kainBBZSbrDummT9AAAFgDU/VLk1P1S5AAAAB3NzaC1yc2
|
|
|
|
EAAAGBALTeOPQP52OTunEh//Q6GvGtyElNFmSWh8nU2gi7jvnZKrTaZ1eZqsxWyonVwCNX
|
|
|
|
ksFXLcPUXlTpdbiummRd5W13Tg/FvT16G0fSlFE9qgceUAuIm3wKgylspgwDxvcpc8+uIb
|
|
|
|
GAaia+1TZPZExRFLA5lsy2my6B+NhJZ6fJhf5U5FiHKVadJcv3rUtPILbV6F3p3pCdb0bs
|
|
|
|
TmFgwhMxj1O5BnPdtE5jNeJOyA3T6Doeft7EVCnnSnjlK1Qx9X9J+0Ix0LGXG7IafLYKs3
|
|
|
|
+9+M7vHE6ja6/urWqbXRDjoo9YlC/6mBn858v4QOPLTuflBVIoeVvH7adPhDQSA5JBoJNj
|
|
|
|
CfdiO0AF8jFbCAAV3KPpiU+6UIvbapTgxiENwDo9IHAN1/yG/EF++yId0z4lxIe+/z9V4m
|
|
|
|
GToKadnEZjR/EG6mi/bvPfIQ7ycCDyT7o9pSJbUxJR+4YwX+S0Hws6y1ewQ9dKzO7JeI6/
|
|
|
|
gNqvI5dSJKftuZGopwQWUm6w7ppk/QAAAAMBAAEAAAGAHKnC+Nq0XtGAkIFE4N18e6SAwy
|
|
|
|
0WSWaZqmCzFQM0S2AhJnweOIG/0ZZHjsRzKKauOTmppQk40dgVsejpytIek9R+aH172gxJ
|
|
|
|
2n4Cx0UwduRU5x8FFQlNc/kl722B0JWfJuB/snOZXv6LJ4o5aObIkozt2w9tVFeAqjYn2S
|
|
|
|
1UsNOfRHBXGsTYwpRDwFWP56nKo2d2wBBTHDhCy6fb2dLW1fvSi/YspueOGIlHpvlYKi2/
|
|
|
|
CWqvs9xVrwcScMtiDoQYq0khhO0efLCxvg/o+W9CLMVM2ms4G1zoSUQKN0oYWWQJyW4+VI
|
|
|
|
YneWO8UpN0J3ElXKi7bhgAat7dBaM1g9IrAzk153DiEFZNsPxGOgL/+YdQN7zUBx/z7EkI
|
|
|
|
jyv80RV7fpUXvcq2p+qNl6UVig3VSzRrnsaJkUWu/A0u59ha7ocv6NxDIXjxpIDJme16GF
|
|
|
|
quiGVBQNnYJymS/vFEbGf6bgf7iRmMCRUMG4nqLA6fPYP9uAtch+CmDfVLZC/fIdC5AAAA
|
|
|
|
wQCDissV4zH6bfqgxJSuYNk8Vbb+19cF3b7gH1rVlB3zxpCAgcRgMHC+dP1z2NRx7UW9MR
|
|
|
|
nye6kjpkzZZ0OigLqo7TtEq8uTglD9o6W7mRXqhy5A/ySOmqPL3ernHHQhGuoNODYAHkOU
|
|
|
|
u2Rh8HXi+VLwKZcLInPOYJvcuLG4DxN8WfeVvlMHwhAOaTNNOtL4XZDHQeIPc4qHmJymmv
|
|
|
|
sV7GuyQ6yW5C10uoGdxRPd90Bh4z4h2bKfZFjvEBbSBVkqrlAAAADBAN/zNtNayd/dX7Cr
|
|
|
|
Nb4sZuzCh+CW4BH8GOePZWNCATwBbNXBVb5cR+dmuTqYm+Ekz0VxVQRA1TvKncluJOQpoa
|
|
|
|
Xj8r0xdIgqkehnfDPMKtYVor06B9Fl1jrXtXU0Vrr6QcBWruSVyK1ZxqcmcNK/+KolVepe
|
|
|
|
A6vcl/iKaG4U7su166nxLST06M2EgcSVsFJHpKn5+WAXC+X0Gx8kNjWIIb3GpiChdc0xZD
|
|
|
|
mq02xZthVJrTCVw/e7gfDoB2QRsNV8HwAAAMEAzsCghZVp+0YsYg9oOrw4tEqcbEXEMhwY
|
|
|
|
0jW8JNL8Spr1Ibp5Dw6bRSk5azARjmJtnMJhJ3oeHfF0eoISqcNuQXGndGQbVM9YzzAzc1
|
|
|
|
NbbCNsVroqKlChT5wyPNGS+phi2bPARBno7WSDvshTZ7dAVEP2c9MJW0XwoSevwKlhgSdt
|
|
|
|
RLFFQ/5nclJSdzPBOmQouC0OBcMFSrYtMeknJ4VvueVvve5HcHFaEsaMc7ABAGaLYaBQOm
|
|
|
|
iixITGvaNZh/tjAAAACW5pY29sYUBwMQE=
|
|
|
|
-----END OPENSSH PRIVATE KEY-----`
|
2019-07-31 12:11:44 +00:00
|
|
|
configDir = ".."
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-24 12:41:15 +00:00
|
|
|
allPerms = []string{dataprovider.PermAny}
|
|
|
|
homeBasePath string
|
|
|
|
scpPath string
|
2019-11-26 21:26:42 +00:00
|
|
|
gitPath string
|
|
|
|
sshPath string
|
2019-08-24 12:41:15 +00:00
|
|
|
pubKeyPath string
|
|
|
|
privateKeyPath string
|
2019-11-26 21:26:42 +00:00
|
|
|
gitWrapPath string
|
2020-01-06 20:42:41 +00:00
|
|
|
extAuthPath string
|
2019-12-29 22:27:32 +00:00
|
|
|
logFilePath string
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2019-12-29 22:27:32 +00:00
|
|
|
logFilePath = filepath.Join(configDir, "sftpgo_sftpd_test.log")
|
2019-09-03 10:08:09 +00:00
|
|
|
loginBannerFileName := "login_banner"
|
|
|
|
loginBannerFile := filepath.Join(configDir, loginBannerFileName)
|
|
|
|
ioutil.WriteFile(loginBannerFile, []byte("simple login banner\n"), 0777)
|
2019-12-29 22:27:32 +00:00
|
|
|
logger.InitLogger(logFilePath, 5, 1, 28, false, zerolog.DebugLevel)
|
2019-08-07 20:46:13 +00:00
|
|
|
config.LoadConfig(configDir, "")
|
2019-07-26 09:34:44 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
|
|
|
|
err := dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "error initializing data provider: %v", err)
|
2019-07-26 09:34:44 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
dataProvider := dataprovider.GetProvider()
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
2019-08-08 08:01:33 +00:00
|
|
|
sftpdConf.BindPort = 2022
|
2019-09-03 10:08:09 +00:00
|
|
|
sftpdConf.KexAlgorithms = []string{"curve25519-sha256@libssh.org", "ecdh-sha2-nistp256",
|
|
|
|
"ecdh-sha2-nistp384"}
|
|
|
|
sftpdConf.Ciphers = []string{"chacha20-poly1305@openssh.com", "aes128-gcm@openssh.com",
|
|
|
|
"aes256-ctr"}
|
|
|
|
sftpdConf.MACs = []string{"hmac-sha2-256-etm@openssh.com", "hmac-sha2-256"}
|
|
|
|
sftpdConf.LoginBannerFile = loginBannerFileName
|
2019-11-18 22:30:37 +00:00
|
|
|
// we need to test all supported ssh commands
|
|
|
|
sftpdConf.EnabledSSHCommands = []string{"*"}
|
2019-10-09 15:33:30 +00:00
|
|
|
// we run the test cases with UploadMode atomic and resume support. The non atomic code path
|
2019-08-04 07:37:58 +00:00
|
|
|
// simply does not execute some code so if it works in atomic mode will
|
|
|
|
// work in non atomic mode too
|
2019-10-09 15:33:30 +00:00
|
|
|
sftpdConf.UploadMode = 2
|
2019-12-27 22:12:44 +00:00
|
|
|
homeBasePath = os.TempDir()
|
2019-11-26 21:26:42 +00:00
|
|
|
var scriptArgs string
|
2019-07-27 07:38:09 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2019-11-26 21:26:42 +00:00
|
|
|
scriptArgs = "%*"
|
2019-07-27 07:38:09 +00:00
|
|
|
} else {
|
2019-11-26 21:26:42 +00:00
|
|
|
sftpdConf.Actions.ExecuteOn = []string{"download", "upload", "rename", "delete", "ssh_cmd"}
|
2019-08-08 08:01:33 +00:00
|
|
|
sftpdConf.Actions.Command = "/usr/bin/true"
|
2019-07-27 07:38:09 +00:00
|
|
|
sftpdConf.Actions.HTTPNotificationURL = "http://127.0.0.1:8080/"
|
2019-11-26 21:26:42 +00:00
|
|
|
scriptArgs = "$@"
|
2019-07-27 07:38:09 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
|
|
|
|
scpPath, err = exec.LookPath("scp")
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to get scp command. SCP tests will be skipped, err: %v", err)
|
|
|
|
logger.WarnToConsole("unable to get scp command. SCP tests will be skipped, err: %v", err)
|
|
|
|
scpPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
gitPath, err = exec.LookPath("git")
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to get git command. GIT tests will be skipped, err: %v", err)
|
|
|
|
logger.WarnToConsole("unable to get git command. GIT tests will be skipped, err: %v", err)
|
|
|
|
gitPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
sshPath, err = exec.LookPath("ssh")
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to get ssh command. GIT tests will be skipped, err: %v", err)
|
|
|
|
logger.WarnToConsole("unable to get ssh command. GIT tests will be skipped, err: %v", err)
|
|
|
|
gitPath = ""
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
pubKeyPath = filepath.Join(homeBasePath, "ssh_key.pub")
|
|
|
|
privateKeyPath = filepath.Join(homeBasePath, "ssh_key")
|
2019-11-26 21:26:42 +00:00
|
|
|
gitWrapPath = filepath.Join(homeBasePath, "gitwrap.sh")
|
2020-01-06 20:42:41 +00:00
|
|
|
extAuthPath = filepath.Join(homeBasePath, "extauth.sh")
|
2019-08-24 12:41:15 +00:00
|
|
|
err = ioutil.WriteFile(pubKeyPath, []byte(testPubKey+"\n"), 0600)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("unable to save public key to file: %v", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(privateKeyPath, []byte(testPrivateKey+"\n"), 0600)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("unable to save private key to file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
err = ioutil.WriteFile(gitWrapPath, []byte(fmt.Sprintf("%v -i %v -oStrictHostKeyChecking=no %v\n",
|
|
|
|
sshPath, privateKeyPath, scriptArgs)), 0755)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("unable to save gitwrap shell script: %v", err)
|
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
sftpd.SetDataProvider(dataProvider)
|
2019-10-07 16:19:01 +00:00
|
|
|
httpd.SetDataProvider(dataProvider)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Debug(logSender, "", "initializing SFTP server with config %+v", sftpdConf)
|
2019-07-26 09:34:44 +00:00
|
|
|
if err := sftpdConf.Initialize(configDir); err != nil {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Error(logSender, "", "could not start SFTP server: %v", err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2019-10-07 16:19:01 +00:00
|
|
|
if err := httpdConf.Initialize(configDir); err != nil {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Error(logSender, "", "could not start HTTP server: %v", err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitTCPListening(fmt.Sprintf("%s:%d", sftpdConf.BindAddress, sftpdConf.BindPort))
|
|
|
|
waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort))
|
|
|
|
|
|
|
|
exitCode := m.Run()
|
2019-12-29 22:27:32 +00:00
|
|
|
os.Remove(logFilePath)
|
2019-09-03 10:08:09 +00:00
|
|
|
os.Remove(loginBannerFile)
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(pubKeyPath)
|
|
|
|
os.Remove(privateKeyPath)
|
|
|
|
os.Remove(gitWrapPath)
|
2020-01-06 20:42:41 +00:00
|
|
|
os.Remove(extAuthPath)
|
2019-07-26 09:34:44 +00:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:11:44 +00:00
|
|
|
func TestInitialization(t *testing.T) {
|
2019-08-07 20:46:13 +00:00
|
|
|
config.LoadConfig(configDir, "")
|
2019-07-31 12:11:44 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.Umask = "invalid umask"
|
2019-08-08 08:01:33 +00:00
|
|
|
sftpdConf.BindPort = 2022
|
2019-09-03 10:08:09 +00:00
|
|
|
sftpdConf.LoginBannerFile = "invalid_file"
|
2019-11-18 22:30:37 +00:00
|
|
|
sftpdConf.IsSCPEnabled = true
|
|
|
|
sftpdConf.EnabledSSHCommands = append(sftpdConf.EnabledSSHCommands, "ls")
|
2019-07-31 12:11:44 +00:00
|
|
|
err := sftpdConf.Initialize(configDir)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Inizialize must fail, a SFTP server should be already running")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestBasicSFTPHandling(t *testing.T) {
|
|
|
|
usePubKey := false
|
2019-08-01 07:24:35 +00:00
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.QuotaSize = 6553600
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-10-09 15:33:30 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
expectedQuotaSize := user.UsedQuotaSize + testFileSize
|
|
|
|
expectedQuotaFiles := user.UsedQuotaFiles + 1
|
2019-10-25 16:37:12 +00:00
|
|
|
createTestFile(testFilePath, testFileSize)
|
2019-10-16 05:48:22 +00:00
|
|
|
err = sftpUploadFile(testFilePath, path.Join("/missing_dir", testFileName), testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("upload a file to a missing dir must fail")
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file download error: %v", err)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if expectedQuotaFiles != user.UsedQuotaFiles {
|
|
|
|
t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
}
|
|
|
|
if expectedQuotaSize != user.UsedQuotaSize {
|
|
|
|
t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.Lstat(testFileName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("stat for deleted file must not succeed")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if (expectedQuotaFiles - 1) != user.UsedQuotaFiles {
|
|
|
|
t.Errorf("quota files does not match after delete, expected: %v, actual: %v", expectedQuotaFiles-1, user.UsedQuotaFiles)
|
|
|
|
}
|
|
|
|
if (expectedQuotaSize - testFileSize) != user.UsedQuotaSize {
|
|
|
|
t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize-testFileSize, user.UsedQuotaSize)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
|
|
|
os.Remove(localDownloadPath)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 22:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
func TestUploadResume(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
appendDataSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = appendToTestFile(testFilePath, appendDataSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to append to test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadResumeFile(testFilePath, testFileName, testFileSize+appendDataSize, false, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload resume error: %v", err)
|
|
|
|
}
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize+appendDataSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file download error: %v", err)
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
initialHash, err := computeHashForFile(sha256.New(), testFilePath)
|
2019-10-09 15:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error computing file hash: %v", err)
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
donwloadedFileHash, err := computeHashForFile(sha256.New(), localDownloadPath)
|
2019-10-09 15:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error computing downloaded file hash: %v", err)
|
|
|
|
}
|
|
|
|
if donwloadedFileHash != initialHash {
|
|
|
|
t.Errorf("resume failed: file hash does not match")
|
|
|
|
}
|
|
|
|
err = sftpUploadResumeFile(testFilePath, testFileName, testFileSize+appendDataSize, true, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("file upload resume with invalid offset must fail")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
|
|
|
os.Remove(localDownloadPath)
|
2019-10-09 15:33:30 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-07-20 22:19:17 +00:00
|
|
|
func TestDirCommands(t *testing.T) {
|
|
|
|
usePubKey := false
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 22:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-07-26 14:15:42 +00:00
|
|
|
// remove the home dir to test auto creation
|
2019-10-16 05:48:22 +00:00
|
|
|
os.RemoveAll(user.HomeDir)
|
2019-07-20 22:19:17 +00:00
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
2019-11-08 07:43:27 +00:00
|
|
|
err = client.Mkdir("test1")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error mkdir: %v", err)
|
|
|
|
}
|
2019-11-08 07:43:27 +00:00
|
|
|
err = client.Rename("test1", "test")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error rename: %v", err)
|
|
|
|
}
|
2019-11-08 07:43:27 +00:00
|
|
|
_, err = client.Lstat("/test1")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("stat for renamed dir must not succeed")
|
|
|
|
}
|
|
|
|
err = client.PosixRename("test", "test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error posix rename: %v", err)
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
err = client.Remove("test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error rmdir: %v", err)
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
err = client.Mkdir("/test/test1")
|
2019-10-16 05:48:22 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("recursive mkdir must fail")
|
|
|
|
}
|
|
|
|
client.Mkdir("/test")
|
|
|
|
err = client.Mkdir("/test/test1")
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-10-16 05:48:22 +00:00
|
|
|
t.Errorf("mkdir dir error: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-24 20:44:01 +00:00
|
|
|
_, err = client.ReadDir("/this/dir/does/not/exist")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("reading a missing dir must fail")
|
|
|
|
}
|
2019-10-16 05:48:22 +00:00
|
|
|
err = client.RemoveDirectory("/test/test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("remove dir error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.RemoveDirectory("/test")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("remove dir error: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.Lstat("/test")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("stat for deleted dir must not succeed")
|
|
|
|
}
|
|
|
|
err = client.RemoveDirectory("/test")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("remove missing path must fail")
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 07:43:27 +00:00
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
2019-10-16 05:48:22 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
err = client.Mkdir("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error mkdir: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Mkdir("/test/test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error mkdir subdir: %v", err)
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
testFileName := "/test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
2019-10-16 10:57:06 +00:00
|
|
|
err = sftpUploadFile(testFilePath, path.Join("/test", testFileName), testFileSize, client)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
err = client.Remove("/test")
|
2019-10-16 05:48:22 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("remove non empty dir must fail")
|
|
|
|
}
|
2019-10-16 10:57:06 +00:00
|
|
|
err = client.RemoveDirectory(path.Join("/test", testFileName))
|
2019-10-16 05:48:22 +00:00
|
|
|
if err == nil {
|
2019-11-15 11:15:07 +00:00
|
|
|
t.Errorf("remove a file with rmdir must fail")
|
2019-10-16 05:48:22 +00:00
|
|
|
}
|
2019-10-16 10:57:06 +00:00
|
|
|
err = client.Remove(path.Join("/test", testFileName))
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-10-16 05:48:22 +00:00
|
|
|
t.Errorf("remove file error: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-16 10:57:06 +00:00
|
|
|
err = client.Remove(path.Join("/test", testFileName))
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2019-10-16 05:48:22 +00:00
|
|
|
t.Errorf("remove missing file must fail")
|
|
|
|
}
|
|
|
|
err = client.Remove("/test/test1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("remove dir error: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
err = client.Remove("/test")
|
2019-10-16 05:48:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("remove dir error: %v", err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 06:30:22 +00:00
|
|
|
func TestLink(t *testing.T) {
|
2019-07-20 22:19:17 +00:00
|
|
|
usePubKey := false
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 22:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink(testFileName, testFileName+".link")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error creating symlink: %v", err)
|
|
|
|
}
|
2019-08-24 20:44:01 +00:00
|
|
|
_, err = client.ReadLink(testFileName + ".link")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("readlink is currently not implemented so must fail")
|
|
|
|
}
|
2019-08-08 19:42:07 +00:00
|
|
|
err = client.Symlink(testFileName, testFileName+".link")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("creating a symlink to an existing one must fail")
|
2019-09-13 06:30:22 +00:00
|
|
|
}
|
|
|
|
err = client.Link(testFileName, testFileName+".hlink")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("hard link is not supported and must fail")
|
2019-08-08 19:42:07 +00:00
|
|
|
}
|
2019-07-20 22:19:17 +00:00
|
|
|
err = client.Remove(testFileName + ".link")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing symlink: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 22:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
func TestStat(t *testing.T) {
|
2019-07-26 15:18:51 +00:00
|
|
|
usePubKey := false
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-11-15 11:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-07-26 15:18:51 +00:00
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
2019-11-15 11:26:52 +00:00
|
|
|
createTestFile(testFilePath, testFileSize)
|
2019-07-26 15:18:51 +00:00
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
_, err := client.Lstat(testFileName)
|
2019-07-26 15:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat error: %v", err)
|
|
|
|
}
|
2019-11-15 16:09:00 +00:00
|
|
|
// mode 0666 and 0444 works on Windows too
|
|
|
|
newPerm := os.FileMode(0666)
|
|
|
|
err = client.Chmod(testFileName, newPerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("chmod error: %v", err)
|
|
|
|
}
|
|
|
|
newFi, err := client.Lstat(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat error: %v", err)
|
|
|
|
}
|
|
|
|
if newPerm != newFi.Mode().Perm() {
|
|
|
|
t.Errorf("chmod failed expected: %v, actual: %v", newPerm, newFi.Mode().Perm())
|
|
|
|
}
|
|
|
|
newPerm = os.FileMode(0444)
|
|
|
|
err = client.Chmod(testFileName, newPerm)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("chmod error: %v", err)
|
|
|
|
}
|
|
|
|
newFi, err = client.Lstat(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat error: %v", err)
|
|
|
|
}
|
|
|
|
if newPerm != newFi.Mode().Perm() {
|
|
|
|
t.Errorf("chmod failed expected: %v, actual: %v", newPerm, newFi.Mode().Perm())
|
|
|
|
}
|
|
|
|
_, err = client.ReadLink(testFileName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("readlink is not supported and must fail")
|
|
|
|
}
|
2019-11-16 09:23:41 +00:00
|
|
|
err = client.Truncate(testFileName, 0)
|
2019-11-15 16:09:00 +00:00
|
|
|
if err != nil {
|
2019-11-16 09:23:41 +00:00
|
|
|
t.Errorf("truncate must be silently ignored: %v", err)
|
2019-11-15 16:09:00 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-11-15 16:09:00 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatChownChmod(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("chown is not supported on Windows, chmod is partially supported")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
createTestFile(testFilePath, testFileSize)
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
err = client.Chown(testFileName, os.Getuid(), os.Getgid())
|
2019-07-26 15:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("chown error: %v", err)
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
newPerm := os.FileMode(0600)
|
|
|
|
err = client.Chmod(testFileName, newPerm)
|
2019-07-26 15:18:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("chmod error: %v", err)
|
|
|
|
}
|
|
|
|
newFi, err := client.Lstat(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat error: %v", err)
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
if newPerm != newFi.Mode().Perm() {
|
|
|
|
t.Errorf("chown failed expected: %v, actual: %v", newPerm, newFi.Mode().Perm())
|
2019-07-26 15:18:51 +00:00
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
// l'errore viene riconvertito da sftp.ErrSSHFxNoSuchFile in os.ErrNotExist
|
|
|
|
err = client.Chmod(testFileName, newPerm)
|
|
|
|
if err != os.ErrNotExist {
|
|
|
|
t.Errorf("unexpected chmod error: %v expected: %v", err, os.ErrNotExist)
|
|
|
|
}
|
|
|
|
err = client.Chown(testFileName, os.Getuid(), os.Getgid())
|
|
|
|
if err != os.ErrNotExist {
|
|
|
|
t.Errorf("unexpected chown error: %v expected: %v", err, os.ErrNotExist)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-26 15:18:51 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-11-15 11:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-26 15:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 09:23:41 +00:00
|
|
|
func TestChtimes(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
testDir := "test"
|
|
|
|
createTestFile(testFilePath, testFileSize)
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
acmodTime := time.Now()
|
|
|
|
err = client.Chtimes(testFileName, acmodTime, acmodTime)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error changing file times")
|
|
|
|
}
|
|
|
|
newFi, err := client.Lstat(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file stat error: %v", err)
|
|
|
|
}
|
|
|
|
diff := math.Abs(newFi.ModTime().Sub(acmodTime).Seconds())
|
|
|
|
if diff > 1 {
|
|
|
|
t.Errorf("diff between wanted and real modification time too big: %v", diff)
|
|
|
|
}
|
|
|
|
err = client.Chtimes("invalidFile", acmodTime, acmodTime)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Mkdir(testDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create dir: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chtimes(testDir, acmodTime, acmodTime)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error changing dir times")
|
|
|
|
}
|
|
|
|
newFi, err = client.Lstat(testDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("dir stat error: %v", err)
|
|
|
|
}
|
|
|
|
diff = math.Abs(newFi.ModTime().Sub(acmodTime).Seconds())
|
|
|
|
if diff > 1 {
|
|
|
|
t.Errorf("diff between wanted and real modification time too big: %v", diff)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-11-16 09:23:41 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
// basic tests to verify virtual chroot, should be improved to cover more cases ...
|
|
|
|
func TestEscapeHomeDir(t *testing.T) {
|
|
|
|
usePubKey := true
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
testDir := "testDir"
|
|
|
|
linkPath := filepath.Join(homeBasePath, defaultUsername, testDir)
|
|
|
|
err = os.Symlink(homeBasePath, linkPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error making local symlink: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.ReadDir(testDir)
|
|
|
|
if err == nil {
|
2019-07-20 19:20:16 +00:00
|
|
|
t.Errorf("reading a symbolic link outside home dir should not succeeded")
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
os.Remove(linkPath)
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
2019-10-16 10:57:06 +00:00
|
|
|
remoteDestPath := path.Join("..", "..", testFileName)
|
2019-07-20 10:26:52 +00:00
|
|
|
err = sftpUploadFile(testFilePath, remoteDestPath, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.Lstat(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file stat error: %v the file was created outside the user dir!", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
linkPath = filepath.Join(homeBasePath, defaultUsername, testFileName)
|
|
|
|
err = os.Symlink(homeBasePath, linkPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error making local symlink: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpDownloadFile(testFileName, testFilePath, 0, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("download file outside home dir must fail")
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, remoteDestPath, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("overwrite a file outside home dir must fail")
|
|
|
|
}
|
|
|
|
err = client.Chmod(remoteDestPath, 0644)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("setstat on a file outside home dir must fail")
|
|
|
|
}
|
|
|
|
os.Remove(linkPath)
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHomeSpecialChars(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.HomeDir = filepath.Join(homeBasePath, "abc açà#&%lk")
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
files, err := client.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read remote dir: %v", err)
|
|
|
|
}
|
|
|
|
if len(files) < 1 {
|
|
|
|
t.Errorf("expected at least 1 file in this dir")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 12:11:44 +00:00
|
|
|
func TestLogin(t *testing.T) {
|
|
|
|
u := getTestUser(false)
|
2019-08-07 21:41:10 +00:00
|
|
|
u.PublicKeys = []string{testPubKey}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
client, err := getSftpClient(user, false)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
2019-07-31 12:11:44 +00:00
|
|
|
t.Errorf("sftp client with valid password must work")
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-11-13 10:36:21 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if user.LastLogin <= 0 {
|
|
|
|
t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-07-31 12:11:44 +00:00
|
|
|
t.Errorf("sftp client with valid public key must work")
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
user.Password = "invalid password"
|
|
|
|
client, err = getSftpClient(user, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with invalid password must fail")
|
|
|
|
defer client.Close()
|
|
|
|
}
|
2019-07-31 15:06:12 +00:00
|
|
|
// testPubKey1 is not authorized
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{testPubKey1}
|
2019-07-31 12:11:44 +00:00
|
|
|
user.Password = ""
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
2019-07-31 12:11:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, true)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with invalid public key must fail")
|
|
|
|
defer client.Close()
|
|
|
|
}
|
2019-07-31 15:06:12 +00:00
|
|
|
// login a user with multiple public keys, only the second one is valid
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{testPubKey1, testPubKey}
|
2019-07-31 15:06:12 +00:00
|
|
|
user.Password = ""
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
2019-07-31 15:06:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("sftp client with multiple public key must work if at least one public key is valid")
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
func TestLoginUserStatus(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("sftp client with valid credentials must work")
|
|
|
|
}
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if user.LastLogin <= 0 {
|
|
|
|
t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Status = 0
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login for a disabled user must fail")
|
|
|
|
defer client.Close()
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginUserExpiration(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("sftp client with valid credentials must work")
|
|
|
|
}
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if user.LastLogin <= 0 {
|
|
|
|
t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.ExpirationDate = utils.GetTimeAsMsSinceEpoch(time.Now()) - 120000
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login for an expired user must fail")
|
|
|
|
defer client.Close()
|
|
|
|
}
|
|
|
|
user.ExpirationDate = utils.GetTimeAsMsSinceEpoch(time.Now()) + 120000
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("login for a non expired user must succeed: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func TestLoginWithIPFilters(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Filters.DeniedIP = []string{"192.167.0.0/24", "172.18.0.0/16"}
|
|
|
|
u.Filters.AllowedIP = []string{}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("sftp client with valid credentials must work")
|
|
|
|
}
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if user.LastLogin <= 0 {
|
|
|
|
t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Filters.AllowedIP = []string{"127.0.0.0/8"}
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("login from an allowed IP must succeed: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
}
|
|
|
|
user.Filters.AllowedIP = []string{"172.19.0.0/16"}
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login from an not allowed IP must fail")
|
|
|
|
client.Close()
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestLoginAfterUserUpdateEmptyPwd(t *testing.T) {
|
|
|
|
usePubKey := false
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = ""
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{}
|
2019-07-20 10:26:52 +00:00
|
|
|
// password and public key should remain unchanged
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read remote dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginAfterUserUpdateEmptyPubKey(t *testing.T) {
|
|
|
|
usePubKey := true
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = ""
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{}
|
2019-07-20 10:26:52 +00:00
|
|
|
// password and public key should remain unchanged
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read remote dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 20:42:41 +00:00
|
|
|
func TestLoginExternalAuthPwdAndPubKey(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.QuotaFiles = 1000
|
|
|
|
dataProvider := dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
|
|
|
|
providerConf.ExternalAuthProgram = extAuthPath
|
|
|
|
providerConf.ExternalAuthScope = 0
|
|
|
|
err := dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider with users base dir")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
|
|
|
|
client, err := getSftpClient(u, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.Username = defaultUsername + "1"
|
|
|
|
client, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("external auth login with invalid user must fail")
|
|
|
|
}
|
|
|
|
usePubKey = false
|
|
|
|
u = getTestUser(usePubKey)
|
|
|
|
u.PublicKeys = []string{}
|
|
|
|
ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
|
|
|
|
client, err = getSftpClient(u, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get users: %v, out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
|
|
t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
|
|
|
|
}
|
|
|
|
user := users[0]
|
|
|
|
if len(user.PublicKeys) != 0 {
|
|
|
|
t.Errorf("number of public keys mismatch, expected: 0, actual: %v", len(user.PublicKeys))
|
|
|
|
}
|
|
|
|
if user.UsedQuotaSize == 0 {
|
|
|
|
t.Error("quota size must be > 0")
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
|
|
|
|
dataProvider = dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
os.Remove(extAuthPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginExternalAuthPwd(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
dataProvider := dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
|
|
|
|
providerConf.ExternalAuthProgram = extAuthPath
|
|
|
|
providerConf.ExternalAuthScope = 1
|
|
|
|
err := dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider with users base dir")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
|
|
|
|
client, err := getSftpClient(u, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.Username = defaultUsername + "1"
|
|
|
|
client, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("external auth login with invalid user must fail")
|
|
|
|
}
|
|
|
|
usePubKey = true
|
|
|
|
u = getTestUser(usePubKey)
|
|
|
|
client, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("external auth login with valid user but invalid auth scope must fail")
|
|
|
|
}
|
|
|
|
users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get users: %v, out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
|
|
t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
|
|
|
|
}
|
|
|
|
user := users[0]
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
|
|
|
|
dataProvider = dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
os.Remove(extAuthPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginExternalAuthPubKey(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
dataProvider := dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
|
|
|
|
providerConf.ExternalAuthProgram = extAuthPath
|
|
|
|
providerConf.ExternalAuthScope = 2
|
|
|
|
err := dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider with users base dir")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
|
|
|
|
client, err := getSftpClient(u, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.Username = defaultUsername + "1"
|
|
|
|
client, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("external auth login with invalid user must fail")
|
|
|
|
}
|
|
|
|
usePubKey = false
|
|
|
|
u = getTestUser(usePubKey)
|
|
|
|
client, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("external auth login with valid user but invalid auth scope must fail")
|
|
|
|
}
|
|
|
|
users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get users: %v, out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
if len(users) != 1 {
|
|
|
|
t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
|
|
|
|
}
|
|
|
|
user := users[0]
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
|
|
|
|
dataProvider = dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
os.Remove(extAuthPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginExternalAuthErrors(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
dataProvider := dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, true), 0755)
|
|
|
|
providerConf.ExternalAuthProgram = extAuthPath
|
|
|
|
providerConf.ExternalAuthScope = 0
|
|
|
|
err := dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider with users base dir")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
|
|
|
|
_, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("login must fail, external auth returns a non json response")
|
|
|
|
}
|
|
|
|
usePubKey = false
|
|
|
|
u = getTestUser(usePubKey)
|
|
|
|
_, err = getSftpClient(u, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("login must fail, external auth returns a non json response")
|
|
|
|
}
|
|
|
|
users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get users: %v, out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
if len(users) != 0 {
|
|
|
|
t.Errorf("number of users mismatch, expected: 0, actual: %v", len(users))
|
|
|
|
}
|
|
|
|
|
|
|
|
dataProvider = dataprovider.GetProvider()
|
|
|
|
dataprovider.Close(dataProvider)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error initializing data provider")
|
|
|
|
}
|
|
|
|
httpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
sftpd.SetDataProvider(dataprovider.GetProvider())
|
|
|
|
os.Remove(extAuthPath)
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestMaxSessions(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.MaxSessions = 1
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err := client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read remote dir: %v", err)
|
|
|
|
}
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("max sessions exceeded, new login should not succeed")
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 11:24:46 +00:00
|
|
|
func TestQuotaFileReplace(t *testing.T) {
|
|
|
|
usePubKey := false
|
2019-08-01 07:24:35 +00:00
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.QuotaFiles = 1000
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-28 11:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-31 12:11:44 +00:00
|
|
|
testFileSize := int64(65535)
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
2019-07-28 11:24:46 +00:00
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
expectedQuotaSize := user.UsedQuotaSize + testFileSize
|
|
|
|
expectedQuotaFiles := user.UsedQuotaFiles + 1
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-07-28 11:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
// now replace the same file, the quota must not change
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-07-28 11:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if expectedQuotaFiles != user.UsedQuotaFiles {
|
|
|
|
t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
}
|
|
|
|
if expectedQuotaSize != user.UsedQuotaSize {
|
|
|
|
t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
// now set a quota size restriction and upload the same file, upload should fail for space limit exceeded
|
2019-07-31 12:11:44 +00:00
|
|
|
user.QuotaSize = testFileSize - 1
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK)
|
2019-07-31 12:11:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error updating user: %v", err)
|
|
|
|
}
|
|
|
|
client, err = getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("quota size exceeded, file upload must fail")
|
|
|
|
}
|
2019-07-28 11:24:46 +00:00
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-28 11:24:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-28 11:24:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestQuotaScan(t *testing.T) {
|
|
|
|
usePubKey := false
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
expectedQuotaSize := user.UsedQuotaSize + testFileSize
|
|
|
|
expectedQuotaFiles := user.UsedQuotaFiles + 1
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
// create user with the same home dir, so there is at least an untracked file
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.StartQuotaScan(user, http.StatusCreated)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error starting quota scan: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
err = waitQuotaScans()
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-11-26 21:26:42 +00:00
|
|
|
t.Errorf("error waiting for active quota scans: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if expectedQuotaFiles != user.UsedQuotaFiles {
|
|
|
|
t.Errorf("quota files does not match after scan, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
}
|
|
|
|
if expectedQuotaSize != user.UsedQuotaSize {
|
|
|
|
t.Errorf("quota size does not match after scan, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func TestMultipleQuotaScans(t *testing.T) {
|
|
|
|
if !sftpd.AddQuotaScan(defaultUsername) {
|
|
|
|
t.Errorf("add quota failed")
|
|
|
|
}
|
|
|
|
if sftpd.AddQuotaScan(defaultUsername) {
|
|
|
|
t.Errorf("add quota must fail if another scan is already active")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
sftpd.RemoveQuotaScan(defaultUsername)
|
|
|
|
activeScans := sftpd.GetQuotaScans()
|
|
|
|
if len(activeScans) > 0 {
|
|
|
|
t.Errorf("no quota scan must be active: %v", len(activeScans))
|
|
|
|
}
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaSize(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.QuotaFiles = 1
|
|
|
|
u.QuotaSize = testFileSize - 1
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
2019-07-28 11:24:46 +00:00
|
|
|
err = sftpUploadFile(testFilePath, testFileName+".quota", testFileSize, client)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-07-28 11:24:46 +00:00
|
|
|
err = sftpUploadFile(testFilePath, testFileName+".quota.1", testFileSize, client)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("user is over quota file upload must fail")
|
|
|
|
}
|
2019-07-28 11:24:46 +00:00
|
|
|
err = client.Remove(testFileName + ".quota")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBandwidthAndConnections(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
testFileSize := int64(131072)
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.UploadBandwidth = 30
|
|
|
|
u.DownloadBandwidth = 25
|
|
|
|
wantedUploadElapsed := 1000 * (testFileSize / 1000) / u.UploadBandwidth
|
|
|
|
wantedDownloadElapsed := 1000 * (testFileSize / 1000) / u.DownloadBandwidth
|
|
|
|
// 100 ms tolerance
|
|
|
|
wantedUploadElapsed -= 100
|
|
|
|
wantedDownloadElapsed -= 100
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
startTime := time.Now()
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
|
|
|
if elapsed < (wantedUploadElapsed) {
|
|
|
|
t.Errorf("upload bandwidth throttling not respected, elapsed: %v, wanted: %v", elapsed, wantedUploadElapsed)
|
|
|
|
}
|
|
|
|
startTime = time.Now()
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
c := sftpDownloadNonBlocking(testFileName, localDownloadPath, testFileSize, client)
|
|
|
|
waitForActiveTransfer()
|
2019-07-27 07:38:09 +00:00
|
|
|
// wait some additional arbitrary time to wait for transfer activity to happen
|
|
|
|
// it is need to reach all the code in CheckIdleConnections
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
sftpd.CheckIdleConnections()
|
2019-07-26 13:08:08 +00:00
|
|
|
err = <-c
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file download error: %v", err)
|
|
|
|
}
|
|
|
|
elapsed = time.Since(startTime).Nanoseconds() / 1000000
|
|
|
|
if elapsed < (wantedDownloadElapsed) {
|
|
|
|
t.Errorf("download bandwidth throttling not respected, elapsed: %v, wanted: %v", elapsed, wantedDownloadElapsed)
|
|
|
|
}
|
|
|
|
// test disconnection
|
2019-07-27 19:19:30 +00:00
|
|
|
c = sftpUploadNonBlocking(testFilePath, testFileName+"_partial", testFileSize, client)
|
2019-07-26 13:08:08 +00:00
|
|
|
waitForActiveTransfer()
|
2019-07-27 07:38:09 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-07-26 13:08:08 +00:00
|
|
|
sftpd.CheckIdleConnections()
|
|
|
|
stats := sftpd.GetConnectionsStats()
|
|
|
|
for _, stat := range stats {
|
|
|
|
sftpd.CloseActiveConnection(stat.ConnectionID)
|
|
|
|
}
|
|
|
|
err = <-c
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("connection closed upload must fail")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
|
|
|
os.Remove(localDownloadPath)
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
func TestMissingFile(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
err = sftpDownloadFile("missing_file", localDownloadPath, 0, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("download missing file must fail")
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
os.Remove(localDownloadPath)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:18:43 +00:00
|
|
|
func TestOpenError(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
os.Chmod(user.GetHomeDir(), 0001)
|
|
|
|
_, err = client.ReadDir(".")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("read dir must fail if we have no filesystem read permissions")
|
|
|
|
}
|
|
|
|
os.Chmod(user.GetHomeDir(), 0755)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(user.GetHomeDir(), testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.Stat(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file stat error: %v", err)
|
|
|
|
}
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file download error: %v", err)
|
|
|
|
}
|
|
|
|
os.Chmod(testFilePath, 0001)
|
|
|
|
err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("file download must fail if we have no filesystem read permissions")
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(localDownloadPath, testFileName, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("upload must fail if we have no filesystem write permissions")
|
|
|
|
}
|
2019-11-15 11:15:07 +00:00
|
|
|
err = client.Mkdir("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error making dir: %v", err)
|
|
|
|
}
|
2019-11-14 13:18:43 +00:00
|
|
|
os.Chmod(user.GetHomeDir(), 0000)
|
|
|
|
_, err = client.Lstat(testFileName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("file stat must fail if we have no filesystem read permissions")
|
|
|
|
}
|
|
|
|
os.Chmod(user.GetHomeDir(), 0755)
|
2019-11-15 11:15:07 +00:00
|
|
|
os.Chmod(filepath.Join(user.GetHomeDir(), "test"), 0000)
|
|
|
|
err = client.Rename(testFileName, path.Join("test", testFileName))
|
|
|
|
if err == nil || !strings.Contains(err.Error(), sftp.ErrSSHFxPermissionDenied.Error()) {
|
|
|
|
t.Errorf("unexpected error: %v expected: %v", err, sftp.ErrSSHFxPermissionDenied)
|
|
|
|
}
|
|
|
|
os.Chmod(filepath.Join(user.GetHomeDir(), "test"), 0755)
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
|
|
|
os.Remove(localDownloadPath)
|
2019-11-14 13:18:43 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-08-04 07:37:58 +00:00
|
|
|
func TestOverwriteDirWithFile(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testDirName := "test_dir"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Mkdir(testDirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("mkdir error: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testDirName, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("copying a file over an existing dir must fail")
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Rename(testFileName, testDirName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("rename a file over an existing dir must fail")
|
|
|
|
}
|
|
|
|
err = client.RemoveDirectory(testDirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("dir remove error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 14:54:02 +00:00
|
|
|
func TestPasswordsHashPbkdf2Sha1(t *testing.T) {
|
|
|
|
pbkdf2Pwd := "$pbkdf2-sha1$150000$DveVjgYUD05R$X6ydQZdyMeOvpgND2nqGR/0GGic="
|
|
|
|
pbkdf2ClearPwd := "password"
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Password = pbkdf2Pwd
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = pbkdf2ClearPwd
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to login with pkkdf2 sha1 password: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir with pkkdf2 sha1 password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Password = pbkdf2Pwd
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with wrong password must fail")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-17 14:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordsHashPbkdf2Sha256(t *testing.T) {
|
|
|
|
pbkdf2Pwd := "$pbkdf2-sha256$150000$E86a9YMX3zC7$R5J62hsSq+pYw00hLLPKBbcGXmq7fj5+/M0IFoYtZbo="
|
|
|
|
pbkdf2ClearPwd := "password"
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Password = pbkdf2Pwd
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = pbkdf2ClearPwd
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to login with pkkdf2 sha1 password: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir with pkkdf2 sha1 password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Password = pbkdf2Pwd
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with wrong password must fail")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-17 14:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordsHashPbkdf2Sha512(t *testing.T) {
|
|
|
|
pbkdf2Pwd := "$pbkdf2-sha512$150000$dsu7T5R3IaVQ$1hFXPO1ntRBcoWkSLKw+s4sAP09Xtu4Ya7CyxFq64jM9zdUg8eRJVr3NcR2vQgb0W9HHvZaILHsL4Q/Vr6arCg=="
|
|
|
|
pbkdf2ClearPwd := "password"
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Password = pbkdf2Pwd
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = pbkdf2ClearPwd
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to login with pkkdf2 sha1 password: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir with pkkdf2 sha1 password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Password = pbkdf2Pwd
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with wrong password must fail")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-17 14:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordsHashBcrypt(t *testing.T) {
|
|
|
|
bcryptPwd := "$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK"
|
|
|
|
bcryptClearPwd := "secret"
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Password = bcryptPwd
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = bcryptClearPwd
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to login with bcrypt password: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir with bcrypt password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Password = bcryptPwd
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with wrong password must fail")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-17 14:54:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-17 14:54:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 10:23:27 +00:00
|
|
|
func TestPasswordsHashSHA512Crypt(t *testing.T) {
|
|
|
|
sha512CryptPwd := "$6$459ead56b72e44bc$uog86fUxscjt28BZxqFBE2pp2QD8P/1e98MNF75Z9xJfQvOckZnQ/1YJqiq1XeytPuDieHZvDAMoP7352ELkO1"
|
|
|
|
clearPwd := "secret"
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Password = sha512CryptPwd
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-09-15 10:23:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = clearPwd
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to login with sha512 crypt password: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir with sha512 crypt password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Password = sha512CryptPwd
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with wrong password must fail")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-09-15 10:23:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
2019-12-29 06:43:59 +00:00
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordsHashMD5Crypt(t *testing.T) {
|
|
|
|
md5CryptPwd := "$1$b5caebda$VODr/nyhGWgZaY8sJ4x05."
|
|
|
|
clearPwd := "password"
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Password = md5CryptPwd
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
user.Password = clearPwd
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to login with md5 crypt password: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get working dir with md5 crypt password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.Password = md5CryptPwd
|
|
|
|
_, err = getSftpClient(user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login with wrong password must fail")
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
2019-09-15 10:23:27 +00:00
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestPermList(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete, dataprovider.PermRename,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
_, err = client.ReadDir(".")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("read remote dir without permission should not succeed")
|
|
|
|
}
|
2019-07-26 14:15:42 +00:00
|
|
|
_, err = client.Stat("test_file")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("stat remote file without permission should not succeed")
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermDownload(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermUpload, dataprovider.PermDelete, dataprovider.PermRename,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("file download without permission should not succeed")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
|
|
|
os.Remove(localDownloadPath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermUpload(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermDelete, dataprovider.PermRename,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("file upload without permission should not succeed")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 06:53:45 +00:00
|
|
|
func TestPermOverwrite(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-09-17 06:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("file overwrite without permission should not succeed")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-09-17 06:53:45 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-09-17 06:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestPermDelete(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermRename,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("delete without permission should not succeed")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermRename(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Rename(testFileName, testFileName+".rename")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("rename without permission should not succeed")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermCreateDirs(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermRename, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
|
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
err = client.Mkdir("testdir")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("mkdir without permission should not succeed")
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermSymlink(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermOverwrite, dataprovider.PermChmod, dataprovider.PermChown,
|
|
|
|
dataprovider.PermChtimes}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink(testFilePath, testFilePath+".symlink")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("symlink without permission should not succeed")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-07-26 13:08:08 +00:00
|
|
|
|
2019-11-15 11:15:07 +00:00
|
|
|
func TestPermChmod(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-15 11:15:07 +00:00
|
|
|
dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermChown, dataprovider.PermChtimes}
|
2019-11-15 11:15:07 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chmod(testFileName, 0666)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("chmod without permission should not succeed")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-11-15 11:15:07 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermChown(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-15 11:15:07 +00:00
|
|
|
dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermChmod, dataprovider.PermChtimes}
|
2019-11-15 11:15:07 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
err = client.Chown(testFileName, os.Getuid(), os.Getgid())
|
2019-11-15 11:15:07 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("chown without permission should not succeed")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-11-15 11:15:07 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
2019-11-16 09:23:41 +00:00
|
|
|
|
|
|
|
func TestPermChtimes(t *testing.T) {
|
|
|
|
usePubKey := false
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
|
2019-11-16 09:23:41 +00:00
|
|
|
dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite,
|
|
|
|
dataprovider.PermChmod, dataprovider.PermChown}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chtimes(testFileName, time.Now(), time.Now())
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("chtimes without permission should not succeed")
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded file: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-11-16 09:23:41 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestSubDirsUploads(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermChtimes, dataprovider.PermDownload}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
err = client.Mkdir("subdir")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected mkdir error: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFileNameSub := "/subdir/test_file_dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileNameSub, testFileSize, client)
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink(testFileName, testFileNameSub+".link")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink(testFileName, testFileName+".link")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("symlink error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Rename(testFileName, testFileNameSub+".rename")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected rename error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Rename(testFileName, testFileName+".rename")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("rename error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileNameSub)
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName + ".rename")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("remove error: %v", err)
|
|
|
|
}
|
|
|
|
os.Remove(testFilePath)
|
|
|
|
}
|
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubDirsOverwrite(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermOverwrite, dataprovider.PermListItems}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "/subdir/test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, "test_file.dat")
|
|
|
|
testFileSFTPPath := filepath.Join(u.GetHomeDir(), "subdir", "test_file.dat")
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = createTestFile(testFileSFTPPath, 16384)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName+".new", testFileSize, client)
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected overwrite error: %v", err)
|
|
|
|
}
|
|
|
|
os.Remove(testFilePath)
|
|
|
|
}
|
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubDirsDownloads(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermChmod, dataprovider.PermUpload, dataprovider.PermListItems}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
err = client.Mkdir("subdir")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected mkdir error: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "/subdir/test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, "test_file.dat")
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
|
|
|
|
err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected overwrite error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chtimes(testFileName, time.Now(), time.Now())
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected chtimes error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Rename(testFileName, testFileName+".rename")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected rename error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink(testFileName, testFileName+".link")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected symlink error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Remove(testFileName)
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected remove error: %v", err)
|
|
|
|
}
|
|
|
|
os.Remove(localDownloadPath)
|
|
|
|
os.Remove(testFilePath)
|
|
|
|
}
|
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermsSubDirsSetstat(t *testing.T) {
|
|
|
|
// for setstat we check the parent dir permission if the requested path is a dir
|
|
|
|
// otherwise the path permission
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermCreateDirs}
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermAny}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
err = client.Mkdir("subdir")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected mkdir error: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "/subdir/test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, "test_file.dat")
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chtimes("/subdir/", time.Now(), time.Now())
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected chtimes error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chtimes("subdir/", time.Now(), time.Now())
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected chtimes error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Chtimes(testFileName, time.Now(), time.Now())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected chtimes error: %v", err)
|
|
|
|
}
|
|
|
|
os.Remove(testFilePath)
|
|
|
|
}
|
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermsSubDirsCommands(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
client.Mkdir("subdir")
|
|
|
|
acmodTime := time.Now()
|
|
|
|
err = client.Chtimes("/subdir", acmodTime, acmodTime)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected chtimes error: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.Stat("/subdir")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected stat error: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.ReadDir("/")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected readdir error: %v", err)
|
|
|
|
}
|
|
|
|
_, err = client.ReadDir("/subdir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.RemoveDirectory("/subdir/dir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Mkdir("/subdir/dir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
client.Mkdir("/otherdir")
|
|
|
|
err = client.Rename("/otherdir", "/subdir/otherdir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink("/otherdir", "/subdir/otherdir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink("/otherdir", "/otherdir_link")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected rename dir error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Rename("/otherdir", "/otherdir1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected rename dir error: %v", err)
|
|
|
|
}
|
|
|
|
err = client.RemoveDirectory("/subdir")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected remove dir error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-12-25 22:37:37 +00:00
|
|
|
func TestRootDirCommands(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
err = client.Rename("/", "rootdir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error renaming root dir: %v", err)
|
|
|
|
}
|
|
|
|
err = client.Symlink("/", "rootdir")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error symlinking root dir: %v", err)
|
|
|
|
}
|
|
|
|
err = client.RemoveDirectory("/")
|
|
|
|
if !strings.Contains(err.Error(), "Permission Denied") {
|
|
|
|
t.Errorf("unexpected error removing root dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestRelativePaths(t *testing.T) {
|
|
|
|
user := getTestUser(true)
|
|
|
|
path := filepath.Join(user.HomeDir, "/")
|
|
|
|
rel := user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "//")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "../..")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "../../../../../")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "/..")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "/../../../..")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, ".")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "somedir")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/somedir" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
path = filepath.Join(user.HomeDir, "/somedir/subdir")
|
|
|
|
rel = user.GetRelativePath(path)
|
|
|
|
if rel != "/somedir/subdir" {
|
|
|
|
t.Errorf("Unexpected relative path: %v", rel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserPerms(t *testing.T) {
|
|
|
|
user := getTestUser(true)
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermListItems}
|
|
|
|
user.Permissions["/p"] = []string{dataprovider.PermDelete}
|
|
|
|
user.Permissions["/p/1"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
|
|
|
|
user.Permissions["/p/2"] = []string{dataprovider.PermCreateDirs}
|
|
|
|
user.Permissions["/p/3"] = []string{dataprovider.PermChmod}
|
|
|
|
user.Permissions["/p/3/4"] = []string{dataprovider.PermChtimes}
|
|
|
|
user.Permissions["/tmp"] = []string{dataprovider.PermRename}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermListItems, "/") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermListItems, ".") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermListItems, "") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermListItems, "../") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
|
|
|
// path p and /p are the same
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermDelete, "/p") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermDownload, "/p/1") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermCreateDirs, "p/2") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermChmod, "/p/3") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermChtimes, "p/3/4/") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermChtimes, "p/3/4/../4") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
|
|
|
// undefined paths have permissions of the nearest path
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermListItems, "/p34") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermListItems, "/p34/p1/file.dat") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermChtimes, "/p/3/4/5/6") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
if !user.HasPerm(dataprovider.PermDownload, "/p/1/test/file.dat") {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Error("expected permission not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
func TestUserFiltersIPMaskConditions(t *testing.T) {
|
|
|
|
user := getTestUser(true)
|
|
|
|
// with no filter login must be allowed even if the remoteIP is invalid
|
|
|
|
if !user.IsLoginAllowed("192.168.1.5") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
if !user.IsLoginAllowed("invalid") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
user.Filters.DeniedIP = append(user.Filters.DeniedIP, "192.168.1.0/24")
|
|
|
|
if user.IsLoginAllowed("192.168.1.5") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
if !user.IsLoginAllowed("192.168.2.6") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
user.Filters.AllowedIP = append(user.Filters.AllowedIP, "192.168.1.5/32")
|
|
|
|
// if the same ip/mask is both denied and allowed then login must be denied
|
|
|
|
if user.IsLoginAllowed("192.168.1.5") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
if user.IsLoginAllowed("192.168.3.6") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
user.Filters.DeniedIP = []string{}
|
|
|
|
if !user.IsLoginAllowed("192.168.1.5") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
if user.IsLoginAllowed("192.168.1.6") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
user.Filters.DeniedIP = []string{"192.168.0.0/16", "172.16.0.0/16"}
|
|
|
|
user.Filters.AllowedIP = []string{}
|
|
|
|
if user.IsLoginAllowed("192.168.5.255") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
if user.IsLoginAllowed("172.16.1.2") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
if !user.IsLoginAllowed("172.18.2.1") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
user.Filters.AllowedIP = []string{"10.4.4.0/24"}
|
|
|
|
if user.IsLoginAllowed("10.5.4.2") {
|
|
|
|
t.Error("unexpected login allowed")
|
|
|
|
}
|
|
|
|
if !user.IsLoginAllowed("10.4.4.2") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
if !user.IsLoginAllowed("invalid") {
|
|
|
|
t.Error("unexpected login denied")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func TestSSHCommands(t *testing.T) {
|
2019-07-26 14:15:42 +00:00
|
|
|
usePubKey := false
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-07-26 14:15:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
_, err = runSSHCommand("ls", user, usePubKey)
|
2019-07-26 14:15:42 +00:00
|
|
|
if err == nil {
|
2019-11-18 22:30:37 +00:00
|
|
|
t.Errorf("unsupported ssh command must fail")
|
|
|
|
}
|
|
|
|
_, err = runSSHCommand("cd", user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error for ssh cd command: %v", err)
|
|
|
|
}
|
|
|
|
out, err := runSSHCommand("pwd", user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if string(out) != "/\n" {
|
|
|
|
t.Errorf("invalid response for ssh pwd command: %v", string(out))
|
|
|
|
}
|
|
|
|
out, err = runSSHCommand("md5sum", user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
// echo -n '' | md5sum
|
|
|
|
if !strings.Contains(string(out), "d41d8cd98f00b204e9800998ecf8427e") {
|
|
|
|
t.Errorf("invalid md5sum: %v", string(out))
|
|
|
|
}
|
|
|
|
out, err = runSSHCommand("sha1sum", user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(out), "da39a3ee5e6b4b0d3255bfef95601890afd80709") {
|
|
|
|
t.Errorf("invalid sha1sum: %v", string(out))
|
|
|
|
}
|
|
|
|
out, err = runSSHCommand("sha256sum", user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(out), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") {
|
|
|
|
t.Errorf("invalid sha256sum: %v", string(out))
|
|
|
|
}
|
|
|
|
out, err = runSSHCommand("sha384sum", user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(out), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b") {
|
|
|
|
t.Errorf("invalid sha384sum: %v", string(out))
|
2019-07-26 14:15:42 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-07-26 14:15:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func TestSSHFileHash(t *testing.T) {
|
|
|
|
usePubKey := true
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
client, err := getSftpClient(user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create sftp client: %v", err)
|
|
|
|
} else {
|
|
|
|
defer client.Close()
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file upload error: %v", err)
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermUpload}
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
_, err = runSSHCommand("sha512sum "+testFileName, user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("hash command with no list permission must fail")
|
|
|
|
}
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
initialHash, err := computeHashForFile(sha512.New(), testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error computing file hash: %v", err)
|
|
|
|
}
|
|
|
|
out, err := runSSHCommand("sha512sum "+testFileName, user, usePubKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(out), initialHash) {
|
|
|
|
t.Errorf("invalid sha512sum: %v", string(out))
|
|
|
|
}
|
|
|
|
_, err = runSSHCommand("sha512sum invalid_path", user, usePubKey)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("hash for an invalid path must fail")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicGitCommands(t *testing.T) {
|
|
|
|
if len(gitPath) == 0 || len(sshPath) == 0 {
|
|
|
|
t.Skip("git and/or ssh command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
repoName := "testrepo"
|
|
|
|
clonePath := filepath.Join(homeBasePath, repoName)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
os.RemoveAll(filepath.Join(homeBasePath, repoName))
|
|
|
|
out, err := initGitRepo(filepath.Join(user.HomeDir, repoName))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
out, err = cloneGitRepo(homeBasePath, "/"+repoName, user.Username)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
out, err = addFileToGitRepo(clonePath, 128)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v out: %v", err, string(out))
|
|
|
|
}
|
|
|
|
user.QuotaFiles = 100000
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
out, err = pushToGitRepo(clonePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v out: %v", err, string(out))
|
2019-12-29 22:27:32 +00:00
|
|
|
printLatestLogs(10)
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
err = waitQuotaScans()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error waiting for active quota scans: %v", err)
|
|
|
|
}
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to get user: %v", err)
|
|
|
|
}
|
|
|
|
user.QuotaSize = user.UsedQuotaSize - 1
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
|
|
|
out, err = pushToGitRepo(clonePath)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("git push must fail if quota is exceeded, out: %v", string(out))
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
os.RemoveAll(clonePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGitErrors(t *testing.T) {
|
|
|
|
if len(gitPath) == 0 || len(sshPath) == 0 {
|
|
|
|
t.Skip("git and/or ssh command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
repoName := "testrepo"
|
|
|
|
clonePath := filepath.Join(homeBasePath, repoName)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
os.RemoveAll(filepath.Join(homeBasePath, repoName))
|
|
|
|
out, err := cloneGitRepo(homeBasePath, "/"+repoName, user.Username)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("cloning a missing repo must fail, out: %v", string(out))
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-11-26 21:26:42 +00:00
|
|
|
os.RemoveAll(clonePath)
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// Start SCP tests
|
|
|
|
func TestSCPBasicHandling(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.QuotaSize = 6553600
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(131074)
|
|
|
|
expectedQuotaSize := user.UsedQuotaSize + testFileSize
|
|
|
|
expectedQuotaFiles := user.UsedQuotaFiles + 1
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/")
|
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
|
|
|
// test to download a missing file
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("downloading a missing file via scp must fail")
|
|
|
|
}
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading file via scp: %v", err)
|
|
|
|
}
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error downloading file via scp: %v", err)
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(localPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat for the downloaded file must succeed")
|
|
|
|
} else {
|
|
|
|
if fi.Size() != testFileSize {
|
|
|
|
t.Errorf("size of the file downloaded via SCP does not match the expected one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Remove(localPath)
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if expectedQuotaFiles != user.UsedQuotaFiles {
|
|
|
|
t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
}
|
|
|
|
if expectedQuotaSize != user.UsedQuotaSize {
|
|
|
|
t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPUploadFileOverwrite(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-09-02 21:12:41 +00:00
|
|
|
u.QuotaFiles = 1000
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
2019-09-02 21:12:41 +00:00
|
|
|
os.RemoveAll(user.GetHomeDir())
|
2019-08-24 12:41:15 +00:00
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(32760)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
2019-10-16 10:57:06 +00:00
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading file via scp: %v", err)
|
|
|
|
}
|
|
|
|
// test a new upload that must overwrite the existing file
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading existing file via scp: %v", err)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
2019-09-02 21:12:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting user: %v", err)
|
|
|
|
}
|
|
|
|
if user.UsedQuotaSize != testFileSize || user.UsedQuotaFiles != 1 {
|
|
|
|
t.Errorf("update quota error on file overwrite, actual size: %v, expected: %v actual files: %v, expected: 1",
|
|
|
|
user.UsedQuotaSize, testFileSize, user.UsedQuotaFiles)
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error downloading file via scp: %v", err)
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(localPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("stat for the downloaded file must succeed")
|
|
|
|
} else {
|
|
|
|
if fi.Size() != testFileSize {
|
|
|
|
t.Errorf("size of the file downloaded via SCP does not match the expected one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Remove(localPath)
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(testFilePath)
|
2019-08-24 12:41:15 +00:00
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPRecursive(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testBaseDirName := "test_dir"
|
|
|
|
testBaseDirPath := filepath.Join(homeBasePath, testBaseDirName)
|
|
|
|
testBaseDirDownName := "test_dir_down"
|
|
|
|
testBaseDirDownPath := filepath.Join(homeBasePath, testBaseDirDownName)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testBaseDirName, testFileName)
|
|
|
|
testFilePath1 := filepath.Join(homeBasePath, testBaseDirName, testBaseDirName, testFileName)
|
|
|
|
testFileSize := int64(131074)
|
|
|
|
createTestFile(testFilePath, testFileSize)
|
|
|
|
createTestFile(testFilePath1, testFileSize)
|
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testBaseDirName))
|
|
|
|
// test to download a missing dir
|
|
|
|
err = scpDownload(testBaseDirDownPath, remoteDownPath, true, true)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("downloading a missing dir via scp must fail")
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testBaseDirPath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading dir via scp: %v", err)
|
|
|
|
}
|
|
|
|
err = scpDownload(testBaseDirDownPath, remoteDownPath, true, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error downloading dir via scp: %v", err)
|
|
|
|
}
|
|
|
|
// test download without passing -r
|
|
|
|
err = scpDownload(testBaseDirDownPath, remoteDownPath, true, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("recursive download without -r must fail")
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(filepath.Join(testBaseDirDownPath, testFileName))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error downloading file using scp recursive: %v", err)
|
|
|
|
} else {
|
|
|
|
if fi.Size() != testFileSize {
|
|
|
|
t.Errorf("size for file downloaded using recursive scp does not match, actual: %v, expected: %v", fi.Size(), testFileSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fi, err = os.Stat(filepath.Join(testBaseDirDownPath, testBaseDirName, testFileName))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error downloading file using scp recursive: %v", err)
|
|
|
|
} else {
|
|
|
|
if fi.Size() != testFileSize {
|
|
|
|
t.Errorf("size for file downloaded using recursive scp does not match, actual: %v, expected: %v", fi.Size(), testFileSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// upload to a non existent dir
|
|
|
|
remoteUpPath = fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/non_existent_dir")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testBaseDirPath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uploading via scp to a non existent dir must fail")
|
|
|
|
}
|
|
|
|
os.RemoveAll(testBaseDirPath)
|
|
|
|
os.RemoveAll(testBaseDirDownPath)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestSCPPermsSubDirs(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/somedir"] = []string{dataprovider.PermListItems, dataprovider.PermUpload}
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
|
|
|
subPath := filepath.Join(user.GetHomeDir(), "somedir")
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
os.MkdirAll(subPath, 0777)
|
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/somedir")
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, true)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("download a dir with no permissions must fail")
|
|
|
|
}
|
|
|
|
os.Remove(subPath)
|
|
|
|
err = createTestFile(subPath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected download error: %v", err)
|
|
|
|
}
|
|
|
|
os.Chmod(subPath, 0001)
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("download a file with no system permissions must fail")
|
|
|
|
}
|
|
|
|
os.Chmod(subPath, 0755)
|
|
|
|
os.Remove(localPath)
|
|
|
|
os.RemoveAll(user.GetHomeDir())
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestSCPPermCreateDirs(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(32760)
|
|
|
|
testBaseDirName := "test_dir"
|
|
|
|
testBaseDirPath := filepath.Join(homeBasePath, testBaseDirName)
|
|
|
|
testFilePath1 := filepath.Join(homeBasePath, testBaseDirName, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
err = createTestFile(testFilePath1, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/tmp/")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
2019-12-25 17:20:19 +00:00
|
|
|
t.Errorf("scp upload must fail, the user cannot create files in a missing dir")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testBaseDirPath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp upload must fail, the user cannot create new dirs")
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
|
|
|
os.RemoveAll(testBaseDirPath)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPPermUpload(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermCreateDirs}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65536)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/tmp")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp upload must fail, the user cannot upload")
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 06:53:45 +00:00
|
|
|
func TestSCPPermOverwrite(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermUpload, dataprovider.PermCreateDirs}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-09-17 06:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65536)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/tmp")
|
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("scp upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp upload must fail, the user cannot ovewrite existing files")
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-09-17 06:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestSCPPermDownload(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
u := getTestUser(usePubKey)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermUpload, dataprovider.PermCreateDirs}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65537)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading existing file via scp: %v", err)
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
2019-08-24 12:41:15 +00:00
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp download must fail, the user cannot download")
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPQuotaSize(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.QuotaFiles = 1
|
|
|
|
u.QuotaSize = testFileSize - 1
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading existing file via scp: %v", err)
|
|
|
|
}
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath+".quota", true, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("user is over quota scp upload must fail")
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPEscapeHomeDir(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
os.MkdirAll(user.GetHomeDir(), 0777)
|
|
|
|
testDir := "testDir"
|
|
|
|
linkPath := filepath.Join(homeBasePath, defaultUsername, testDir)
|
|
|
|
err = os.Symlink(homeBasePath, linkPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error making local symlink: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join(testDir, testDir))
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("uploading to a dir with a symlink outside home dir must fail")
|
|
|
|
}
|
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testDir, testFileName))
|
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp download must fail, the requested file has a symlink outside user home")
|
|
|
|
}
|
|
|
|
remoteDownPath = fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testDir))
|
|
|
|
err = scpDownload(homeBasePath, remoteDownPath, false, true)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp download must fail, the requested dir is a symlink outside user home")
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPUploadPaths(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
testDirName := "testDir"
|
|
|
|
testDirPath := filepath.Join(user.GetHomeDir(), testDirName)
|
|
|
|
os.MkdirAll(testDirPath, 0777)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, testDirName)
|
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join(testDirName, testFileName))
|
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("scp upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = scpDownload(localPath, remoteDownPath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("scp download error: %v", err)
|
|
|
|
}
|
|
|
|
// upload a file to a missing dir
|
|
|
|
remoteUpPath = fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join(testDirName, testDirName, testFileName))
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("scp upload to a missing dir must fail")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(localPath)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPOverwriteDirWithFile(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
testDirPath := filepath.Join(user.GetHomeDir(), testFileName)
|
|
|
|
os.MkdirAll(testDirPath, 0777)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/")
|
2019-08-25 11:51:54 +00:00
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("copying a file over an existing dir must fail")
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-24 12:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 11:51:54 +00:00
|
|
|
func TestSCPRemoteToRemote(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
usePubKey := true
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
|
2019-08-25 11:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
u := getTestUser(usePubKey)
|
|
|
|
u.Username += "1"
|
|
|
|
u.HomeDir += "1"
|
2019-10-07 16:19:01 +00:00
|
|
|
user1, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-08-25 11:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
|
|
|
remote1UpPath := fmt.Sprintf("%v@127.0.0.1:%v", user1.Username, path.Join("/", testFileName))
|
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("scp upload error: %v", err)
|
|
|
|
}
|
|
|
|
err = scpUpload(remoteUpPath, remote1UpPath, false, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("scp upload remote to remote error: %v", err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-08-25 11:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(user1.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files for user1")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user1, http.StatusOK)
|
2019-08-25 11:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user1: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 21:10:20 +00:00
|
|
|
func TestSCPErrors(t *testing.T) {
|
|
|
|
if len(scpPath) == 0 {
|
|
|
|
t.Skip("scp command not found, unable to execute this test")
|
|
|
|
}
|
|
|
|
u := getTestUser(true)
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2019-09-07 21:10:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to add user: %v", err)
|
|
|
|
}
|
|
|
|
testFileSize := int64(524288)
|
|
|
|
testFileName := "test_file.dat"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to create test file: %v", err)
|
|
|
|
}
|
|
|
|
remoteUpPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, "/")
|
|
|
|
remoteDownPath := fmt.Sprintf("%v@127.0.0.1:%v", user.Username, path.Join("/", testFileName))
|
|
|
|
localPath := filepath.Join(homeBasePath, "scp_download.dat")
|
|
|
|
err = scpUpload(testFilePath, remoteUpPath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error uploading file via scp: %v", err)
|
|
|
|
}
|
2019-09-09 06:57:11 +00:00
|
|
|
user.UploadBandwidth = 512
|
|
|
|
user.DownloadBandwidth = 512
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK)
|
2019-09-09 06:57:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to update user: %v", err)
|
|
|
|
}
|
2019-09-07 21:10:20 +00:00
|
|
|
cmd := getScpDownloadCommand(localPath, remoteDownPath, false, false)
|
|
|
|
go func() {
|
|
|
|
if cmd.Run() == nil {
|
|
|
|
t.Errorf("SCP download must fail")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
waitForActiveTransfer()
|
|
|
|
// wait some additional arbitrary time to wait for transfer activity to happen
|
|
|
|
// it is need to reach all the code in CheckIdleConnections
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cmd.Process.Kill()
|
2019-09-10 16:47:21 +00:00
|
|
|
waitForNoActiveTransfer()
|
2019-09-07 21:10:20 +00:00
|
|
|
cmd = getScpUploadCommand(testFilePath, remoteUpPath, false, false)
|
|
|
|
go func() {
|
|
|
|
if cmd.Run() == nil {
|
|
|
|
t.Errorf("SCP upload must fail")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
waitForActiveTransfer()
|
|
|
|
// wait some additional arbitrary time to wait for transfer activity to happen
|
|
|
|
// it is need to reach all the code in CheckIdleConnections
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cmd.Process.Kill()
|
2019-09-10 16:47:21 +00:00
|
|
|
waitForNoActiveTransfer()
|
2019-09-07 21:10:20 +00:00
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing test file")
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
os.Remove(localPath)
|
2019-09-07 21:10:20 +00:00
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error removing uploaded files")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2019-09-07 21:10:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to remove user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// End SCP tests
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func waitTCPListening(address string) {
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2019-07-31 12:11:44 +00:00
|
|
|
logger.WarnToConsole("tcp server %v not listening: %v\n", address, err)
|
2019-07-26 13:08:08 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
logger.InfoToConsole("tcp server %v now listening\n", address)
|
2019-07-26 13:08:08 +00:00
|
|
|
defer conn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:15:42 +00:00
|
|
|
func getTestUser(usePubKey bool) dataprovider.User {
|
|
|
|
user := dataprovider.User{
|
2019-11-13 10:36:21 +00:00
|
|
|
Username: defaultUsername,
|
|
|
|
Password: defaultPassword,
|
|
|
|
HomeDir: filepath.Join(homeBasePath, defaultUsername),
|
|
|
|
Status: 1,
|
|
|
|
ExpirationDate: 0,
|
2019-07-26 14:15:42 +00:00
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = allPerms
|
2019-07-26 14:15:42 +00:00
|
|
|
if usePubKey {
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{testPubKey}
|
2019-07-26 14:15:42 +00:00
|
|
|
user.Password = ""
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func runSSHCommand(command string, user dataprovider.User, usePubKey bool) ([]byte, error) {
|
2019-07-26 14:15:42 +00:00
|
|
|
var sshSession *ssh.Session
|
2019-11-18 22:30:37 +00:00
|
|
|
var output []byte
|
2019-07-26 14:15:42 +00:00
|
|
|
config := &ssh.ClientConfig{
|
|
|
|
User: defaultUsername,
|
|
|
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if usePubKey {
|
|
|
|
key, err := ssh.ParsePrivateKey([]byte(testPrivateKey))
|
|
|
|
if err != nil {
|
2019-11-18 22:30:37 +00:00
|
|
|
return output, err
|
2019-07-26 14:15:42 +00:00
|
|
|
}
|
|
|
|
config.Auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
|
|
|
|
} else {
|
|
|
|
config.Auth = []ssh.AuthMethod{ssh.Password(defaultPassword)}
|
|
|
|
}
|
|
|
|
conn, err := ssh.Dial("tcp", sftpServerAddr, config)
|
|
|
|
if err != nil {
|
2019-11-18 22:30:37 +00:00
|
|
|
return output, err
|
2019-07-26 14:15:42 +00:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
sshSession, err = conn.NewSession()
|
|
|
|
if err != nil {
|
2019-11-18 22:30:37 +00:00
|
|
|
return output, err
|
2019-07-26 14:15:42 +00:00
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
sshSession.Stdout = &stdout
|
|
|
|
sshSession.Stderr = &stderr
|
|
|
|
err = sshSession.Run(command)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to run command %v: %v", command, stderr.Bytes())
|
|
|
|
}
|
|
|
|
return stdout.Bytes(), err
|
2019-07-26 14:15:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func getSftpClient(user dataprovider.User, usePubKey bool) (*sftp.Client, error) {
|
|
|
|
var sftpClient *sftp.Client
|
|
|
|
config := &ssh.ClientConfig{
|
2019-07-31 12:11:44 +00:00
|
|
|
User: user.Username,
|
2019-07-26 13:08:08 +00:00
|
|
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if usePubKey {
|
|
|
|
key, err := ssh.ParsePrivateKey([]byte(testPrivateKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
|
|
|
|
} else {
|
2019-07-31 12:11:44 +00:00
|
|
|
if len(user.Password) > 0 {
|
|
|
|
config.Auth = []ssh.AuthMethod{ssh.Password(user.Password)}
|
|
|
|
} else {
|
|
|
|
config.Auth = []ssh.AuthMethod{ssh.Password(defaultPassword)}
|
|
|
|
}
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
|
|
|
conn, err := ssh.Dial("tcp", sftpServerAddr, config)
|
|
|
|
if err != nil {
|
|
|
|
return sftpClient, err
|
|
|
|
}
|
|
|
|
sftpClient, err = sftp.NewClient(conn)
|
|
|
|
return sftpClient, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestFile(path string, size int64) error {
|
2019-08-24 12:41:15 +00:00
|
|
|
baseDir := filepath.Dir(path)
|
|
|
|
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(baseDir, 0777)
|
|
|
|
}
|
2019-07-26 13:08:08 +00:00
|
|
|
content := make([]byte, size)
|
|
|
|
_, err := rand.Read(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path, content, 0666)
|
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
func appendToTestFile(path string, size int64) error {
|
|
|
|
content := make([]byte, size)
|
|
|
|
_, err := rand.Read(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
written, err := io.Copy(f, bytes.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if int64(written) != size {
|
|
|
|
return fmt.Errorf("write error, written: %v/%v", written, size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func sftpUploadFile(localSourcePath string, remoteDestPath string, expectedSize int64, client *sftp.Client) error {
|
|
|
|
srcFile, err := os.Open(localSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
destFile, err := client.Create(remoteDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(destFile, srcFile)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
destFile.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// we need to close the file to trigger the close method on server
|
2019-12-25 17:20:19 +00:00
|
|
|
// we cannot defer closing or Lstat will fail for uploads in atomic mode
|
2019-08-04 07:37:58 +00:00
|
|
|
destFile.Close()
|
2019-07-26 13:08:08 +00:00
|
|
|
if expectedSize > 0 {
|
2019-12-25 17:20:19 +00:00
|
|
|
fi, err := client.Stat(remoteDestPath)
|
2019-07-26 13:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Size() != expectedSize {
|
|
|
|
return fmt.Errorf("uploaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
func sftpUploadResumeFile(localSourcePath string, remoteDestPath string, expectedSize int64, invalidOffset bool,
|
|
|
|
client *sftp.Client) error {
|
|
|
|
srcFile, err := os.Open(localSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
fi, err := client.Lstat(remoteDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !invalidOffset {
|
|
|
|
_, err = srcFile.Seek(fi.Size(), 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destFile, err := client.OpenFile(remoteDestPath, os.O_WRONLY|os.O_APPEND)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !invalidOffset {
|
|
|
|
_, err = destFile.Seek(fi.Size(), 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = io.Copy(destFile, srcFile)
|
|
|
|
if err != nil {
|
|
|
|
destFile.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// we need to close the file to trigger the close method on server
|
|
|
|
// we cannot defer closing or Lstat will fail for upload atomic mode
|
|
|
|
destFile.Close()
|
|
|
|
if expectedSize > 0 {
|
|
|
|
fi, err := client.Lstat(remoteDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Size() != expectedSize {
|
|
|
|
return fmt.Errorf("uploaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func sftpDownloadFile(remoteSourcePath string, localDestPath string, expectedSize int64, client *sftp.Client) error {
|
|
|
|
downloadDest, err := os.Create(localDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer downloadDest.Close()
|
|
|
|
sftpSrcFile, err := client.Open(remoteSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sftpSrcFile.Close()
|
|
|
|
_, err = io.Copy(downloadDest, sftpSrcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = downloadDest.Sync()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if expectedSize > 0 {
|
|
|
|
fi, err := downloadDest.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Size() != expectedSize {
|
|
|
|
return fmt.Errorf("downloaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func sftpUploadNonBlocking(localSourcePath string, remoteDestPath string, expectedSize int64, client *sftp.Client) <-chan error {
|
|
|
|
c := make(chan error)
|
|
|
|
go func() {
|
|
|
|
c <- sftpUploadFile(localSourcePath, remoteDestPath, expectedSize, client)
|
|
|
|
}()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func sftpDownloadNonBlocking(remoteSourcePath string, localDestPath string, expectedSize int64, client *sftp.Client) <-chan error {
|
|
|
|
c := make(chan error)
|
|
|
|
go func() {
|
|
|
|
c <- sftpDownloadFile(remoteSourcePath, localDestPath, expectedSize, client)
|
|
|
|
}()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-08-25 11:51:54 +00:00
|
|
|
func scpUpload(localPath, remotePath string, preserveTime, remoteToRemote bool) error {
|
2019-09-07 21:10:20 +00:00
|
|
|
cmd := getScpUploadCommand(localPath, remotePath, preserveTime, remoteToRemote)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func scpDownload(localPath, remotePath string, preserveTime, recursive bool) error {
|
|
|
|
cmd := getScpDownloadCommand(localPath, remotePath, preserveTime, recursive)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getScpDownloadCommand(localPath, remotePath string, preserveTime, recursive bool) *exec.Cmd {
|
2019-08-24 12:41:15 +00:00
|
|
|
var args []string
|
|
|
|
if preserveTime {
|
|
|
|
args = append(args, "-p")
|
|
|
|
}
|
2019-09-07 21:10:20 +00:00
|
|
|
if recursive {
|
|
|
|
args = append(args, "-r")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
args = append(args, "-P")
|
|
|
|
args = append(args, "2022")
|
|
|
|
args = append(args, "-o")
|
|
|
|
args = append(args, "StrictHostKeyChecking=no")
|
|
|
|
args = append(args, "-i")
|
|
|
|
args = append(args, privateKeyPath)
|
|
|
|
args = append(args, remotePath)
|
2019-09-07 21:10:20 +00:00
|
|
|
args = append(args, localPath)
|
|
|
|
return exec.Command(scpPath, args...)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 21:10:20 +00:00
|
|
|
func getScpUploadCommand(localPath, remotePath string, preserveTime, remoteToRemote bool) *exec.Cmd {
|
2019-08-24 12:41:15 +00:00
|
|
|
var args []string
|
2019-09-07 21:10:20 +00:00
|
|
|
if remoteToRemote {
|
|
|
|
args = append(args, "-3")
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
if preserveTime {
|
|
|
|
args = append(args, "-p")
|
|
|
|
}
|
2019-09-07 21:10:20 +00:00
|
|
|
fi, err := os.Stat(localPath)
|
|
|
|
if err == nil {
|
|
|
|
if fi.IsDir() {
|
|
|
|
args = append(args, "-r")
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
args = append(args, "-P")
|
|
|
|
args = append(args, "2022")
|
|
|
|
args = append(args, "-o")
|
|
|
|
args = append(args, "StrictHostKeyChecking=no")
|
|
|
|
args = append(args, "-i")
|
|
|
|
args = append(args, privateKeyPath)
|
|
|
|
args = append(args, localPath)
|
2019-09-07 21:10:20 +00:00
|
|
|
args = append(args, remotePath)
|
|
|
|
return exec.Command(scpPath, args...)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func computeHashForFile(hasher hash.Hash, path string) (string, error) {
|
2019-10-09 15:33:30 +00:00
|
|
|
hash := ""
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-11-18 22:30:37 +00:00
|
|
|
_, err = io.Copy(hasher, f)
|
|
|
|
if err == nil {
|
|
|
|
hash = fmt.Sprintf("%x", hasher.Sum(nil))
|
2019-10-09 15:33:30 +00:00
|
|
|
}
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:47:21 +00:00
|
|
|
func waitForNoActiveTransfer() {
|
|
|
|
for len(sftpd.GetConnectionsStats()) > 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func waitForActiveTransfer() {
|
|
|
|
stats := sftpd.GetConnectionsStats()
|
|
|
|
for len(stats) < 1 {
|
|
|
|
stats = sftpd.GetConnectionsStats()
|
|
|
|
}
|
|
|
|
activeTransferFound := false
|
|
|
|
for !activeTransferFound {
|
|
|
|
stats = sftpd.GetConnectionsStats()
|
|
|
|
if len(stats) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, stat := range stats {
|
|
|
|
if len(stat.Transfers) > 0 {
|
|
|
|
activeTransferFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
|
|
|
|
func waitQuotaScans() error {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
scans, _, err := httpd.GetQuotaScans(http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for len(scans) > 0 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
scans, _, err = httpd.GetQuotaScans(http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGitRepo(path string) ([]byte, error) {
|
|
|
|
os.MkdirAll(path, 0777)
|
|
|
|
args := []string{"init", "--bare"}
|
|
|
|
cmd := exec.Command(gitPath, args...)
|
|
|
|
cmd.Dir = path
|
|
|
|
return cmd.CombinedOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushToGitRepo(repoPath string) ([]byte, error) {
|
|
|
|
cmd := exec.Command(gitPath, "push")
|
|
|
|
cmd.Dir = repoPath
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
fmt.Sprintf("GIT_SSH=%v", gitWrapPath))
|
|
|
|
return cmd.CombinedOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneGitRepo(basePath, remotePath, username string) ([]byte, error) {
|
|
|
|
remoteUrl := fmt.Sprintf("ssh://%v@127.0.0.1:2022%v", username, remotePath)
|
|
|
|
args := []string{"clone", remoteUrl}
|
|
|
|
cmd := exec.Command(gitPath, args...)
|
|
|
|
cmd.Dir = basePath
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
fmt.Sprintf("GIT_SSH=%v", gitWrapPath))
|
|
|
|
return cmd.CombinedOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFileToGitRepo(repoPath string, fileSize int64) ([]byte, error) {
|
|
|
|
path := filepath.Join(repoPath, "test")
|
|
|
|
err := createTestFile(path, fileSize)
|
|
|
|
if err != nil {
|
|
|
|
return []byte(""), err
|
|
|
|
}
|
|
|
|
cmd := exec.Command(gitPath, "config", "user.email", "testuser@example.com")
|
|
|
|
cmd.Dir = repoPath
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
cmd = exec.Command(gitPath, "config", "user.name", "testuser")
|
|
|
|
cmd.Dir = repoPath
|
|
|
|
out, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
cmd = exec.Command(gitPath, "add", "test")
|
|
|
|
cmd.Dir = repoPath
|
|
|
|
out, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
cmd = exec.Command(gitPath, "commit", "-am", "test")
|
|
|
|
cmd.Dir = repoPath
|
|
|
|
return cmd.CombinedOutput()
|
|
|
|
}
|
2019-12-29 22:27:32 +00:00
|
|
|
|
2020-01-06 20:42:41 +00:00
|
|
|
func getExtAuthScriptContent(user dataprovider.User, sleepTime int, nonJsonResponse bool) []byte {
|
|
|
|
extAuthContent := []byte("#!/bin/sh\n\n")
|
|
|
|
u, _ := json.Marshal(user)
|
|
|
|
extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("if test \"$SFTPGO_AUTHD_USERNAME\" = \"%v\"; then\n", user.Username))...)
|
|
|
|
if nonJsonResponse {
|
|
|
|
extAuthContent = append(extAuthContent, []byte("echo 'text response'\n")...)
|
|
|
|
} else {
|
|
|
|
extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("echo '%v'\n", string(u)))...)
|
|
|
|
}
|
|
|
|
extAuthContent = append(extAuthContent, []byte("else\n")...)
|
|
|
|
if nonJsonResponse {
|
|
|
|
extAuthContent = append(extAuthContent, []byte("echo 'text response'\n")...)
|
|
|
|
} else {
|
|
|
|
extAuthContent = append(extAuthContent, []byte("echo '{\"username\":\"\"}'\n")...)
|
|
|
|
}
|
|
|
|
extAuthContent = append(extAuthContent, []byte("fi\n")...)
|
|
|
|
if sleepTime > 0 {
|
|
|
|
extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("sleep %v\n", sleepTime))...)
|
|
|
|
}
|
|
|
|
return extAuthContent
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:27:32 +00:00
|
|
|
func printLatestLogs(maxNumberOfLines int) {
|
|
|
|
var lines []string
|
|
|
|
f, err := os.Open(logFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lines = append(lines, scanner.Text()+"\r\n")
|
|
|
|
for len(lines) > maxNumberOfLines {
|
|
|
|
lines = lines[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if scanner.Err() != nil {
|
|
|
|
logger.WarnToConsole("Unable to print latest logs: %v", scanner.Err())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, line := range lines {
|
|
|
|
logger.DebugToConsole(line)
|
|
|
|
}
|
|
|
|
}
|