2019-07-26 09:34:44 +00:00
|
|
|
package config_test
|
|
|
|
|
|
|
|
import (
|
2019-07-29 06:53:22 +00:00
|
|
|
"encoding/json"
|
2019-07-26 09:34:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-07-29 06:53:22 +00:00
|
|
|
"strings"
|
2019-07-26 09:34:44 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"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/sftpd"
|
|
|
|
)
|
|
|
|
|
2019-08-07 20:46:13 +00:00
|
|
|
const (
|
|
|
|
tempConfigName = "temp"
|
|
|
|
)
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestLoadConfigTest(t *testing.T) {
|
|
|
|
configDir := ".."
|
2019-08-07 20:46:13 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
2019-07-26 09:34:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error loading config")
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
emptyHTTPDConf := httpd.Conf{}
|
2019-07-26 09:34:44 +00:00
|
|
|
if config.GetHTTPDConfig() == emptyHTTPDConf {
|
|
|
|
t.Errorf("error loading httpd conf")
|
|
|
|
}
|
|
|
|
emptyProviderConf := dataprovider.Config{}
|
2019-11-14 10:06:03 +00:00
|
|
|
if config.GetProviderConf().Driver == emptyProviderConf.Driver {
|
2019-07-26 09:34:44 +00:00
|
|
|
t.Errorf("error loading provider conf")
|
|
|
|
}
|
|
|
|
emptySFTPDConf := sftpd.Configuration{}
|
2019-07-27 07:38:09 +00:00
|
|
|
if config.GetSFTPDConfig().BindPort == emptySFTPDConf.BindPort {
|
2019-07-26 09:34:44 +00:00
|
|
|
t.Errorf("error loading SFTPD conf")
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2019-07-26 09:34:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("loading a non existent config file must fail")
|
|
|
|
}
|
|
|
|
ioutil.WriteFile(configFilePath, []byte("{invalid json}"), 0666)
|
2019-08-07 20:46:13 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2019-07-26 09:34:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("loading an invalid config file must fail")
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
ioutil.WriteFile(configFilePath, []byte("{\"sftpd\": {\"bind_port\": \"a\"}}"), 0666)
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("loading a config with an invalid bond_port must fail")
|
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
2019-07-29 06:53:22 +00:00
|
|
|
|
|
|
|
func TestEmptyBanner(t *testing.T) {
|
|
|
|
configDir := ".."
|
2019-08-07 20:46:13 +00:00
|
|
|
confName := tempConfigName + ".json"
|
2019-07-29 06:53:22 +00:00
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2019-08-07 20:46:13 +00:00
|
|
|
config.LoadConfig(configDir, "")
|
2019-07-29 06:53:22 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.Banner = " "
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
config.LoadConfig(configDir, tempConfigName)
|
2019-07-29 06:53:22 +00:00
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
|
|
|
if strings.TrimSpace(sftpdConf.Banner) == "" {
|
|
|
|
t.Errorf("SFTPD banner cannot be empty")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
|
|
|
func TestInvalidUploadMode(t *testing.T) {
|
|
|
|
configDir := ".."
|
2019-08-07 20:46:13 +00:00
|
|
|
confName := tempConfigName + ".json"
|
2019-08-04 07:37:58 +00:00
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
2019-08-07 20:46:13 +00:00
|
|
|
config.LoadConfig(configDir, "")
|
2019-08-04 07:37:58 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.UploadMode = 10
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
2019-08-07 20:46:13 +00:00
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
2019-08-04 07:37:58 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Loading configuration with invalid upload_mode must fail")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
|
2020-01-06 20:42:41 +00:00
|
|
|
func TestInvalidExternalAuthScope(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.ExternalAuthScope = 10
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Loading configuration with invalid external_auth_scope must fail")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func TestInvalidCredentialsPath(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.CredentialsPath = ""
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Loading configuration with credentials path must fail")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:02:06 +00:00
|
|
|
func TestInvalidProxyProtocol(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.ProxyProtocol = 10
|
|
|
|
c := make(map[string]sftpd.Configuration)
|
|
|
|
c["sftpd"] = sftpdConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Loading configuration with invalid proxy_protocol must fail")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
|
|
|
|
2020-03-03 08:09:58 +00:00
|
|
|
func TestInvalidUsersBaseDir(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.UsersBaseDir = "."
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
|
|
|
err = config.LoadConfig(configDir, tempConfigName)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Loading configuration with invalid users base dir must fail")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
|
|
|
|
2020-04-01 21:25:23 +00:00
|
|
|
func TestHookCompatibity(t *testing.T) {
|
|
|
|
configDir := ".."
|
|
|
|
confName := tempConfigName + ".json"
|
|
|
|
configFilePath := filepath.Join(configDir, confName)
|
|
|
|
config.LoadConfig(configDir, "")
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.ExternalAuthProgram = "ext_auth_program"
|
|
|
|
providerConf.PreLoginProgram = "pre_login_program"
|
|
|
|
c := make(map[string]dataprovider.Config)
|
|
|
|
c["data_provider"] = providerConf
|
|
|
|
jsonConf, _ := json.Marshal(c)
|
|
|
|
err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
|
|
|
config.LoadConfig(configDir, tempConfigName)
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
if providerConf.ExternalAuthHook != "ext_auth_program" {
|
|
|
|
t.Error("unexpected external auth hook")
|
|
|
|
}
|
|
|
|
if providerConf.PreLoginHook != "pre_login_program" {
|
|
|
|
t.Error("unexpected pre-login hook")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.KeyboardInteractiveProgram = "key_int_program"
|
|
|
|
cnf := make(map[string]sftpd.Configuration)
|
|
|
|
cnf["sftpd"] = sftpdConf
|
|
|
|
jsonConf, _ = json.Marshal(cnf)
|
|
|
|
err = ioutil.WriteFile(configFilePath, jsonConf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error saving temporary configuration")
|
|
|
|
}
|
|
|
|
config.LoadConfig(configDir, tempConfigName)
|
|
|
|
sftpdConf = config.GetSFTPDConfig()
|
|
|
|
if sftpdConf.KeyboardInteractiveHook != "key_int_program" {
|
|
|
|
t.Error("unexpected keyboard interactive hook")
|
|
|
|
}
|
|
|
|
os.Remove(configFilePath)
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
func TestSetGetConfig(t *testing.T) {
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.IdleTimeout = 3
|
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
|
|
|
if config.GetSFTPDConfig().IdleTimeout != sftpdConf.IdleTimeout {
|
|
|
|
t.Errorf("set sftpd conf failed")
|
|
|
|
}
|
|
|
|
dataProviderConf := config.GetProviderConf()
|
|
|
|
dataProviderConf.Host = "test host"
|
|
|
|
config.SetProviderConf(dataProviderConf)
|
|
|
|
if config.GetProviderConf().Host != dataProviderConf.Host {
|
|
|
|
t.Errorf("set data provider conf failed")
|
|
|
|
}
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
httpdConf.BindAddress = "0.0.0.0"
|
|
|
|
config.SetHTTPDConfig(httpdConf)
|
|
|
|
if config.GetHTTPDConfig().BindAddress != httpdConf.BindAddress {
|
|
|
|
t.Errorf("set httpd conf failed")
|
|
|
|
}
|
|
|
|
}
|