2018-10-06 20:47:31 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-02-02 03:44:50 +00:00
|
|
|
"reflect"
|
2018-10-06 20:47:31 +00:00
|
|
|
"testing"
|
2020-07-18 03:23:44 +00:00
|
|
|
|
|
|
|
log "github.com/gophish/gophish/logger"
|
2018-10-06 20:47:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var validConfig = []byte(`{
|
|
|
|
"admin_server": {
|
|
|
|
"listen_url": "127.0.0.1:3333",
|
|
|
|
"use_tls": true,
|
|
|
|
"cert_path": "gophish_admin.crt",
|
|
|
|
"key_path": "gophish_admin.key"
|
|
|
|
},
|
|
|
|
"phish_server": {
|
|
|
|
"listen_url": "0.0.0.0:8080",
|
|
|
|
"use_tls": false,
|
|
|
|
"cert_path": "example.crt",
|
|
|
|
"key_path": "example.key"
|
|
|
|
},
|
|
|
|
"db_name": "sqlite3",
|
|
|
|
"db_path": "gophish.db",
|
|
|
|
"migrations_prefix": "db/db_",
|
2022-09-29 11:25:35 +00:00
|
|
|
"contact_address": "",
|
|
|
|
"attachments": {
|
|
|
|
"plain_text_file_list": [
|
|
|
|
".txt",
|
|
|
|
".html",
|
|
|
|
".ics",
|
|
|
|
".ps1",
|
|
|
|
".bat",
|
|
|
|
".vbs",
|
|
|
|
".sh",
|
|
|
|
".py"
|
|
|
|
]
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
}`)
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func createTemporaryConfig(t *testing.T) *os.File {
|
2018-10-06 20:47:31 +00:00
|
|
|
f, err := ioutil.TempFile("", "gophish-config")
|
2020-02-02 03:44:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temporary config: %v", err)
|
|
|
|
}
|
|
|
|
return f
|
2018-10-06 20:47:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func removeTemporaryConfig(t *testing.T, f *os.File) {
|
|
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove temporary config: %v", err)
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
|
|
f := createTemporaryConfig(t)
|
|
|
|
defer removeTemporaryConfig(t, f)
|
|
|
|
_, err := f.Write(validConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing config to temporary file: %v", err)
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
// Load the valid config
|
2020-02-02 03:44:50 +00:00
|
|
|
conf, err := LoadConfig(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error loading config from temporary file: %v", err)
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
|
2018-12-15 21:42:32 +00:00
|
|
|
expectedConfig := &Config{}
|
2018-10-06 20:47:31 +00:00
|
|
|
err = json.Unmarshal(validConfig, &expectedConfig)
|
2020-02-02 03:44:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error unmarshaling config: %v", err)
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
expectedConfig.MigrationsPath = expectedConfig.MigrationsPath + expectedConfig.DBName
|
|
|
|
expectedConfig.TestFlag = false
|
2020-04-24 04:16:44 +00:00
|
|
|
expectedConfig.AdminConf.CSRFKey = ""
|
2020-07-18 03:23:44 +00:00
|
|
|
expectedConfig.Logging = &log.Config{}
|
2020-02-02 03:44:50 +00:00
|
|
|
if !reflect.DeepEqual(expectedConfig, conf) {
|
|
|
|
t.Fatalf("invalid config received. expected %#v got %#v", expectedConfig, conf)
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
|
|
|
|
// Load an invalid config
|
2020-05-26 02:46:36 +00:00
|
|
|
_, err = LoadConfig("bogusfile")
|
2020-02-02 03:44:50 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error when loading invalid config, but got %v", err)
|
|
|
|
}
|
2018-10-06 20:47:31 +00:00
|
|
|
}
|