github.com/FabianToSpace/GoRecon@v0.0.5/config/config_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "testing" 6 ) 7 8 func TestGetConfig(t *testing.T) { 9 // Config not existing 10 t.Run("Config not existing", func(t *testing.T) { 11 curdir, _ := os.Getwd() 12 tmp, _ := os.MkdirTemp("", "configtest") 13 os.Chdir(tmp) 14 15 _, err := GetConfig() 16 if err != nil { 17 t.Error("Expected no error when config file is not existing") 18 } 19 20 os.Chdir(curdir) 21 os.RemoveAll(tmp) 22 }) 23 24 // Test for successful reading of config file 25 t.Run("Successful reading of config file", func(t *testing.T) { 26 curdir, _ := os.Getwd() 27 tmp, _ := os.MkdirTemp("", "configtest") 28 os.Chdir(tmp) 29 30 os.WriteFile("config.yaml", []byte(`portrange: 1 31 outputformat: test 32 outputfile: test.json 33 debug: false 34 threads: 1 35 plugins: 36 portscans: 37 - nmap-tcp-all 38 servicescans: 39 - whatweb`), 0400) 40 41 Config, err := GetConfig() 42 if err != nil { 43 t.Errorf("Error reading config file: %v", err) 44 } 45 if Config.PortRange != "1" || 46 Config.OutputFormat != "test" || 47 Config.OutputFile != "test.json" || 48 Config.Debug != false || 49 Config.Threads != 1 || 50 len(Config.Plugins.PortScans) != 1 || 51 len(Config.Plugins.ServiceScans) != 1 { 52 t.Errorf("Invalid config values") 53 } 54 55 os.Chdir(curdir) 56 os.RemoveAll(tmp) 57 }) 58 59 // Test for handling error when decoding config file 60 t.Run("Error decoding config file", func(t *testing.T) { 61 curdir, _ := os.Getwd() 62 tmp, _ := os.MkdirTemp("", "configtest") 63 os.Chdir(tmp) 64 65 os.WriteFile("config.yaml", []byte(`portrange: 1 66 outputformat: test 67 outputfile: test.json 68 debug: false 69 threads: 1`), 0400) 70 71 _, err := GetConfig() 72 if err == nil { 73 t.Errorf("Expected error when decoding config file: %v", err) 74 } 75 76 os.Chdir(curdir) 77 os.RemoveAll(tmp) 78 }) 79 80 // Test for handling error when processing config with environment variables 81 t.Run("Error processing config with environment variables", func(t *testing.T) { 82 curdir, _ := os.Getwd() 83 tmp, _ := os.MkdirTemp("", "configtest") 84 os.Chdir(tmp) 85 86 os.WriteFile("config.yaml", []byte(`portrange: 1 87 outputformat: test 88 outputfile: test.json 89 debug: false 90 threads: 1`), 0400) 91 92 os.Setenv("PORT_RANGE", "5") 93 os.Setenv("OUTPUT_FORMAT", "env") 94 os.Setenv("OUTPUT_FILE", "env.json") 95 os.Setenv("DEBUG", "true") 96 os.Setenv("THREADS", "5") 97 os.Setenv("PORT_SCANS", "nmap-tcp-all;nmap-tcp-top") 98 os.Setenv("SERVICE_SCANS", "whatweb;nikto") 99 100 Config, err := GetConfig() 101 if err != nil { 102 t.Errorf("Error reading config file: %v", err) 103 } 104 105 if Config.PortRange != "5" || 106 Config.OutputFormat != "env" || 107 Config.OutputFile != "env.json" || 108 Config.Debug != true || 109 Config.Threads != 5 || 110 len(Config.Plugins.PortScans) != 2 || 111 len(Config.Plugins.ServiceScans) != 2 { 112 t.Errorf("Invalid config values") 113 } 114 115 os.Chdir(curdir) 116 os.RemoveAll(tmp) 117 }) 118 }