Exemplo n.º 1
0
func TestCheckConfig(t *testing.T) {
	eConfig := config.EmptyConfig()
	eConfig.CreateMissingCerts = true
	eConfig.HostCertMap = make(map[string]config.CertPaths)
	//Testing empty HostCertMap:
	if CheckConfig(eConfig) != nil {
		t.Errorf("Empty HostCertMap should always pass CheckConfig.")
	}
	//Adding some stuff to HostCertMap:
	rand := util.RandString(10)
	cps := config.CertPaths{
		Certfile: "/tmp/" + rand + ".pem",
		Keyfile:  "/tmp/" + rand + ".key.pem"}
	eConfig.HostCertMap["test"] = cps
	//Testing creation of certs:
	err := CheckConfig(eConfig)
	if err != nil {
		t.Errorf("CheckConfig complains when it should have created some certs:\n\t%s\n", err)
	}
	if !fExists(cps.Certfile) {
		t.Errorf("CheckConfig didn't create expected file: %s\n", cps.Certfile)
	}
	if !fExists(cps.Keyfile) {
		t.Errorf("CheckConfig didn't create expected file: %s\n", cps.Keyfile)
	}
	fmt.Printf("Removing files:\n\t%s\n\t%s\n", cps.Certfile, cps.Keyfile)
	os.Remove(cps.Certfile)
	os.Remove(cps.Keyfile)
	//Testing missing HostCertFiles:
	eConfig.CreateMissingCerts = false
	if CheckConfig(eConfig) == nil {
		t.Errorf("CheckConfig didn't complain when it could not create missing certs.")
	}
	if fExists(cps.Certfile) {
		t.Errorf("CheckConfig created unexpected file: %s\n", cps.Certfile)
	}
	if fExists(cps.Keyfile) {
		t.Errorf("CheckConfig created unexpected file: %s\n", cps.Keyfile)
	}
}
Exemplo n.º 2
0
/*
  main does the following things:
  - If no parameters given, it complains with usage message.
  - If 'init' parameter is given, it prints a 'config.json' file.
  - If <configFile> parameter is given, it reads the file and tries to act accordingly.
  Exit codes:
  0: All fine
  1: Wrong usage
  2: Problems reading/parsing file
*/
func main() {
	//Flags to use:
	init := flag.Bool("init", false, "Set this flag to print the default config.")
	path := flag.String("config", "config.json", "Use this flag to specify a config file.")
	flag.Parse()
	//Checking if init case is wanted:
	if *init {
		c := config.EmptyConfig()
		fmt.Printf("%s\n", c.ToJson())
		os.Exit(0)
	}
	//Reading config file:
	config, err := config.ReadFile(*path)
	if err != nil {
		fmt.Printf("Error reading/parsing config file '%s':\n", *path)
		fmt.Println(err)
		os.Exit(2)
	}
	fmt.Printf("Successfully parsed config:\n\t%v\n", config)
	//Starting up:
	//FIXME IMPLEMENT .)
}