config_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package config_test
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/drakkan/sftpgo/config"
  10. "github.com/drakkan/sftpgo/dataprovider"
  11. "github.com/drakkan/sftpgo/httpd"
  12. "github.com/drakkan/sftpgo/sftpd"
  13. )
  14. const (
  15. tempConfigName = "temp"
  16. )
  17. func TestLoadConfigTest(t *testing.T) {
  18. configDir := ".."
  19. err := config.LoadConfig(configDir, "")
  20. if err != nil {
  21. t.Errorf("error loading config")
  22. }
  23. emptyHTTPDConf := httpd.Conf{}
  24. if config.GetHTTPDConfig() == emptyHTTPDConf {
  25. t.Errorf("error loading httpd conf")
  26. }
  27. emptyProviderConf := dataprovider.Config{}
  28. if config.GetProviderConf().Driver == emptyProviderConf.Driver {
  29. t.Errorf("error loading provider conf")
  30. }
  31. emptySFTPDConf := sftpd.Configuration{}
  32. if config.GetSFTPDConfig().BindPort == emptySFTPDConf.BindPort {
  33. t.Errorf("error loading SFTPD conf")
  34. }
  35. confName := tempConfigName + ".json"
  36. configFilePath := filepath.Join(configDir, confName)
  37. err = config.LoadConfig(configDir, tempConfigName)
  38. if err == nil {
  39. t.Errorf("loading a non existent config file must fail")
  40. }
  41. ioutil.WriteFile(configFilePath, []byte("{invalid json}"), 0666)
  42. err = config.LoadConfig(configDir, tempConfigName)
  43. if err == nil {
  44. t.Errorf("loading an invalid config file must fail")
  45. }
  46. ioutil.WriteFile(configFilePath, []byte("{\"sftpd\": {\"bind_port\": \"a\"}}"), 0666)
  47. err = config.LoadConfig(configDir, tempConfigName)
  48. if err == nil {
  49. t.Errorf("loading a config with an invalid bond_port must fail")
  50. }
  51. os.Remove(configFilePath)
  52. }
  53. func TestEmptyBanner(t *testing.T) {
  54. configDir := ".."
  55. confName := tempConfigName + ".json"
  56. configFilePath := filepath.Join(configDir, confName)
  57. config.LoadConfig(configDir, "")
  58. sftpdConf := config.GetSFTPDConfig()
  59. sftpdConf.Banner = " "
  60. c := make(map[string]sftpd.Configuration)
  61. c["sftpd"] = sftpdConf
  62. jsonConf, _ := json.Marshal(c)
  63. err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
  64. if err != nil {
  65. t.Errorf("error saving temporary configuration")
  66. }
  67. config.LoadConfig(configDir, tempConfigName)
  68. sftpdConf = config.GetSFTPDConfig()
  69. if strings.TrimSpace(sftpdConf.Banner) == "" {
  70. t.Errorf("SFTPD banner cannot be empty")
  71. }
  72. os.Remove(configFilePath)
  73. }
  74. func TestInvalidUploadMode(t *testing.T) {
  75. configDir := ".."
  76. confName := tempConfigName + ".json"
  77. configFilePath := filepath.Join(configDir, confName)
  78. config.LoadConfig(configDir, "")
  79. sftpdConf := config.GetSFTPDConfig()
  80. sftpdConf.UploadMode = 10
  81. c := make(map[string]sftpd.Configuration)
  82. c["sftpd"] = sftpdConf
  83. jsonConf, _ := json.Marshal(c)
  84. err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
  85. if err != nil {
  86. t.Errorf("error saving temporary configuration")
  87. }
  88. err = config.LoadConfig(configDir, tempConfigName)
  89. if err == nil {
  90. t.Errorf("Loading configuration with invalid upload_mode must fail")
  91. }
  92. os.Remove(configFilePath)
  93. }
  94. func TestInvalidExternalAuthScope(t *testing.T) {
  95. configDir := ".."
  96. confName := tempConfigName + ".json"
  97. configFilePath := filepath.Join(configDir, confName)
  98. config.LoadConfig(configDir, "")
  99. providerConf := config.GetProviderConf()
  100. providerConf.ExternalAuthScope = 10
  101. c := make(map[string]dataprovider.Config)
  102. c["data_provider"] = providerConf
  103. jsonConf, _ := json.Marshal(c)
  104. err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
  105. if err != nil {
  106. t.Errorf("error saving temporary configuration")
  107. }
  108. err = config.LoadConfig(configDir, tempConfigName)
  109. if err == nil {
  110. t.Errorf("Loading configuration with invalid external_auth_scope must fail")
  111. }
  112. os.Remove(configFilePath)
  113. }
  114. func TestInvalidCredentialsPath(t *testing.T) {
  115. configDir := ".."
  116. confName := tempConfigName + ".json"
  117. configFilePath := filepath.Join(configDir, confName)
  118. config.LoadConfig(configDir, "")
  119. providerConf := config.GetProviderConf()
  120. providerConf.CredentialsPath = ""
  121. c := make(map[string]dataprovider.Config)
  122. c["data_provider"] = providerConf
  123. jsonConf, _ := json.Marshal(c)
  124. err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
  125. if err != nil {
  126. t.Errorf("error saving temporary configuration")
  127. }
  128. err = config.LoadConfig(configDir, tempConfigName)
  129. if err == nil {
  130. t.Errorf("Loading configuration with credentials path must fail")
  131. }
  132. os.Remove(configFilePath)
  133. }
  134. func TestInvalidProxyProtocol(t *testing.T) {
  135. configDir := ".."
  136. confName := tempConfigName + ".json"
  137. configFilePath := filepath.Join(configDir, confName)
  138. config.LoadConfig(configDir, "")
  139. sftpdConf := config.GetSFTPDConfig()
  140. sftpdConf.ProxyProtocol = 10
  141. c := make(map[string]sftpd.Configuration)
  142. c["sftpd"] = sftpdConf
  143. jsonConf, _ := json.Marshal(c)
  144. err := ioutil.WriteFile(configFilePath, jsonConf, 0666)
  145. if err != nil {
  146. t.Errorf("error saving temporary configuration")
  147. }
  148. err = config.LoadConfig(configDir, tempConfigName)
  149. if err == nil {
  150. t.Errorf("Loading configuration with invalid proxy_protocol must fail")
  151. }
  152. os.Remove(configFilePath)
  153. }
  154. func TestSetGetConfig(t *testing.T) {
  155. sftpdConf := config.GetSFTPDConfig()
  156. sftpdConf.IdleTimeout = 3
  157. config.SetSFTPDConfig(sftpdConf)
  158. if config.GetSFTPDConfig().IdleTimeout != sftpdConf.IdleTimeout {
  159. t.Errorf("set sftpd conf failed")
  160. }
  161. dataProviderConf := config.GetProviderConf()
  162. dataProviderConf.Host = "test host"
  163. config.SetProviderConf(dataProviderConf)
  164. if config.GetProviderConf().Host != dataProviderConf.Host {
  165. t.Errorf("set data provider conf failed")
  166. }
  167. httpdConf := config.GetHTTPDConfig()
  168. httpdConf.BindAddress = "0.0.0.0"
  169. config.SetHTTPDConfig(httpdConf)
  170. if config.GetHTTPDConfig().BindAddress != httpdConf.BindAddress {
  171. t.Errorf("set httpd conf failed")
  172. }
  173. }