🎣 Open-Source Phishing Toolkit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

  1. package config
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "reflect"
  7. "testing"
  8. log "github.com/gophish/gophish/logger"
  9. )
  10. var validConfig = []byte(`{
  11. "admin_server": {
  12. "listen_url": "127.0.0.1:3333",
  13. "use_tls": true,
  14. "cert_path": "gophish_admin.crt",
  15. "key_path": "gophish_admin.key"
  16. },
  17. "phish_server": {
  18. "listen_url": "0.0.0.0:8080",
  19. "use_tls": false,
  20. "cert_path": "example.crt",
  21. "key_path": "example.key"
  22. },
  23. "db_name": "sqlite3",
  24. "db_path": "gophish.db",
  25. "migrations_prefix": "db/db_",
  26. "contact_address": ""
  27. }`)
  28. func createTemporaryConfig(t *testing.T) *os.File {
  29. f, err := ioutil.TempFile("", "gophish-config")
  30. if err != nil {
  31. t.Fatalf("unable to create temporary config: %v", err)
  32. }
  33. return f
  34. }
  35. func removeTemporaryConfig(t *testing.T, f *os.File) {
  36. err := f.Close()
  37. if err != nil {
  38. t.Fatalf("unable to remove temporary config: %v", err)
  39. }
  40. }
  41. func TestLoadConfig(t *testing.T) {
  42. f := createTemporaryConfig(t)
  43. defer removeTemporaryConfig(t, f)
  44. _, err := f.Write(validConfig)
  45. if err != nil {
  46. t.Fatalf("error writing config to temporary file: %v", err)
  47. }
  48. // Load the valid config
  49. conf, err := LoadConfig(f.Name())
  50. if err != nil {
  51. t.Fatalf("error loading config from temporary file: %v", err)
  52. }
  53. expectedConfig := &Config{}
  54. err = json.Unmarshal(validConfig, &expectedConfig)
  55. if err != nil {
  56. t.Fatalf("error unmarshaling config: %v", err)
  57. }
  58. expectedConfig.MigrationsPath = expectedConfig.MigrationsPath + expectedConfig.DBName
  59. expectedConfig.TestFlag = false
  60. expectedConfig.AdminConf.CSRFKey = ""
  61. expectedConfig.Logging = &log.Config{}
  62. if !reflect.DeepEqual(expectedConfig, conf) {
  63. t.Fatalf("invalid config received. expected %#v got %#v", expectedConfig, conf)
  64. }
  65. // Load an invalid config
  66. _, err = LoadConfig("bogusfile")
  67. if err == nil {
  68. t.Fatalf("expected error when loading invalid config, but got %v", err)
  69. }
  70. }