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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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_",
|
|
|
|
"contact_address": ""
|
|
|
|
}`)
|
|
|
|
|
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-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
|
2018-12-15 21:42:32 +00:00
|
|
|
conf, 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
|
|
|
}
|