@@ -8,16 +8,88 @@ import (
88 "testing"
99
1010 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
1112)
1213
14+ // Generates a config file, where snclient can call a script.
15+ // scriptName does not have an extension
16+ // scriptFilename does have (most likely an OS specific) script extension.
17+ // It registers four commands for script
18+ // scriptName_arg1 : ./${SCRIPT_FILENAME} "$ARG1$"
19+ // scriptName_arg_numbered : ./${SCRIPT_FILENAME} "$ARG1$" "$ARG2$" "$ARG3$" "$ARG4$" "$ARG5$" "$ARG6$" "$ARG7$" "$ARG8$" "$ARG9$" "$ARG10$"
20+ // scriptName_args : ./${SCRIPT_FILENAME} "$ARGS$"
21+ // scriptName_args_quouted : ./${SCRIPT_FILENAME} "$ARGS"$"
22+ //
23+ //nolint:unparam // scriptName is so far always "powershell_detail" , no other test script uses this function. Keep it as a parameter for future use.
24+ func snclientConfigFileWithScript (t * testing.T , scriptsDir , scriptName , scriptFilename string ) string {
25+ t .Helper ()
26+
27+ configTemplate := `
28+ [/modules]
29+ CheckExternalScripts = enabled
30+
31+ [/paths]
32+ scripts = ${SCRIPTS_DIR}
33+ shared-path = %(scripts)
34+
35+ [/settings/external scripts]
36+ timeout = 1111111
37+ allow arguments = true
38+
39+ [/settings/external scripts/scripts]
40+ ${SCRIPT_NAME}_arg1 = ./${SCRIPT_FILENAME} $ARG1$
41+
42+ [/settings/external scripts/scripts/${SCRIPT_NAME}_arg1]
43+ allow arguments = true
44+ allow nasty characters = true
45+
46+ [/settings/external scripts/scripts]
47+ ${SCRIPT_NAME}_arg_numbered = ./${SCRIPT_FILENAME} $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$
48+
49+ [/settings/external scripts/scripts/${SCRIPT_NAME}_arg_numbered]
50+ allow arguments = true
51+ allow nasty characters = true
52+
53+ [/settings/external scripts/scripts]
54+ ${SCRIPT_NAME}_args = ./${SCRIPT_FILENAME} $ARGS$
55+
56+ [/settings/external scripts/scripts/${SCRIPT_NAME}_args]
57+ allow arguments = true
58+ allow nasty characters = true
59+
60+ [/settings/external scripts/scripts]
61+ ${SCRIPT_NAME}_args_quouted = ./${SCRIPT_FILENAME} $ARGS"$
62+
63+ [/settings/external scripts/scripts/${SCRIPT_NAME}_args_quouted]
64+ allow arguments = true
65+ allow nasty characters = true
66+ `
67+
68+ mapper := func (placeholderName string ) string {
69+ switch placeholderName {
70+ case "SCRIPTS_DIR" :
71+ return scriptsDir
72+ case "SCRIPT_NAME" :
73+ return scriptName
74+ case "SCRIPT_FILENAME" :
75+ return scriptFilename
76+ default :
77+ // if its not some value we know, leave it as is
78+ return "$" + placeholderName
79+ }
80+ }
81+
82+ return os .Expand (configTemplate , mapper )
83+ }
84+
1385func TestMakeCmd (t * testing.T ) {
1486 config := ""
1587 snc := StartTestAgent (t , config )
1688
1789 commandString := `.\t\scripts\powershell_detail.ps1 -option1 option1 -option2 'option2' -option3 "option3" -option4 'option4.option4,option4:option4;option4|option4$option4' `
1890 cmd , err := snc .makeCmd (context .TODO (), commandString )
1991
20- assert . Nilf (t , err , "there should not be any errors when converting command: %s into an exec.Cmd of os/exec" , commandString )
92+ require . NoErrorf (t , err , "there should not be any errors when converting command: %s into an exec.Cmd of os/exec" , commandString )
2193
2294 assert .NotEmptyf (t , cmd .SysProcAttr .CmdLine , "exec.Cmd from command: %s should not have an empty SysProcAttr.CmdLine" , commandString )
2395
@@ -42,7 +114,10 @@ func TestMakeCmd(t *testing.T) {
42114 // users have to use it like this --optionX 'foo,bar' to have it accepted as a string
43115
44116 cmdLineExpectedContains := `-option1 option1 -option2 'option2' -option3 "option3" -option4 'option4.option4,option4:option4;option4|option4$option4'`
45- assert .Containsf (t , cmd .SysProcAttr .CmdLine , cmdLineExpectedContains , "exec.Cmd from command: %s\n should contain this substring: %s\n but it looks like this: %s" , commandString , cmdLineExpectedContains , cmd .SysProcAttr .CmdLine )
117+ assert .Containsf (t , cmd .SysProcAttr .CmdLine ,
118+ cmdLineExpectedContains ,
119+ "exec.Cmd from command: %s\n should contain this substring: %s\n but it looks like this: %s" ,
120+ commandString , cmdLineExpectedContains , cmd .SysProcAttr .CmdLine )
46121
47122 var pathEnv string
48123 for _ , envVar := range cmd .Env {
@@ -51,13 +126,13 @@ func TestMakeCmd(t *testing.T) {
51126 }
52127 }
53128
54- assert .NotEmpty (t , pathEnv , cmd . Env , "converted exec.Cmd from command: %s should contain PATH environment variable" , commandString )
129+ assert .NotEmpty (t , pathEnv , "converted exec.Cmd from command: %s should contain PATH environment variable" , commandString )
55130
56131 // script is found under C:\Users\sorus\repositories\snclient\pkg\snclient\t
57132 // scriptsPath, _ := snc.config.Section("/paths").GetString("scripts")
58133 // assert.Containsf(t, pathEnv, scriptsPath+":", "converted exec.Cmd from command: %s should have its PATH variable: %s include the config ScriptsPath: %s", commandString, pathEnv, scriptsPath)
59134
60- assert .Equal ( t , true , cmd .SysProcAttr .HideWindow , "converted exec.Cmd from command: %s should hide its spawned window" , commandString )
135+ assert .Truef ( t , cmd .SysProcAttr .HideWindow , "converted exec.Cmd from command: %s should hide its spawned window" , commandString )
61136
62137 StopTestAgent (t , snc )
63138}
@@ -75,7 +150,6 @@ func TestPowershell1(t *testing.T) {
75150 res := snc .RunCheck ("powershell_detail_arg1" , []string {})
76151
77152 outputString := string (res .BuildPluginOutput ())
78- // t.Logf("\n%s\n", outputString)
79153
80154 assert .Equalf (t , CheckExitOK , res .State , "check should return state ok" )
81155
@@ -88,7 +162,6 @@ func TestPowershell1(t *testing.T) {
88162 for _ , rawCommandLineExpectedItem := range rawCommandlineExpected {
89163 assert .Containsf (t , outputString , rawCommandLineExpectedItem , "raw commandline should contain: %s" , rawCommandLineExpectedItem )
90164 }
91-
92165}
93166
94167func TestPowershellScriptArg1 (t * testing.T ) {
@@ -111,7 +184,6 @@ func TestPowershellScriptArg1(t *testing.T) {
111184 assert .NotNilf (t , checkData , "check should return a checkData" )
112185
113186 outputString := string (checkResult .BuildPluginOutput ())
114- // t.Logf("\n%s\n", outputString)
115187
116188 assert .Equalf (t , CheckExitOK , checkResult .State , "check should return state OK" )
117189
@@ -138,6 +210,7 @@ func TestPowershellScriptArg1(t *testing.T) {
138210 }
139211}
140212
213+ //nolint:dupl // the functions are largely the same, but scriptMacroType is different. Redefining expected strings for each macro type is easier to understand.
141214func TestPowershellScriptArgNumbered (t * testing.T ) {
142215 testDir , _ := os .Getwd ()
143216 scriptsDir := filepath .Join (testDir , "t" , "scripts" )
@@ -167,7 +240,6 @@ func TestPowershellScriptArgNumbered(t *testing.T) {
167240 assert .NotNilf (t , checkData , "check should return a checkData" )
168241
169242 outputString := string (checkResult .BuildPluginOutput ())
170- //t.Logf("\n%s\n", outputString)
171243
172244 assert .Equalf (t , CheckExitOK , checkResult .State , "check should return state OK" )
173245
@@ -191,6 +263,7 @@ func TestPowershellScriptArgNumbered(t *testing.T) {
191263 }
192264}
193265
266+ //nolint:dupl // the functions are largely the same, but scriptMacroType is different. Redefining expected strings for each macro type is easier to understand.
194267func TestPowershellScriptArgs (t * testing.T ) {
195268 testDir , _ := os .Getwd ()
196269 scriptsDir := filepath .Join (testDir , "t" , "scripts" )
@@ -220,7 +293,6 @@ func TestPowershellScriptArgs(t *testing.T) {
220293 assert .NotNilf (t , checkData , "check should return a checkData" )
221294
222295 outputString := string (checkResult .BuildPluginOutput ())
223- //t.Logf("\n%s\n", outputString)
224296
225297 assert .Equalf (t , CheckExitOK , checkResult .State , "check should return state OK" )
226298
0 commit comments