Skip to content

Commit 65e812d

Browse files
committed
Merge pull request caddyserver#270 from Makpoc/master
Add tests for command splitting and fix root tests on Windows
2 parents 5c3085f + 6af26e2 commit 65e812d

File tree

2 files changed

+151
-4
lines changed

2 files changed

+151
-4
lines changed

config/setup/root_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ func TestRoot(t *testing.T) {
2626
if err != nil {
2727
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
2828
}
29-
defer os.Remove(existingFile.Name())
29+
defer func() {
30+
existingFile.Close()
31+
os.Remove(existingFile.Name())
32+
}()
3033

31-
unaccessiblePath := filepath.Join(existingFile.Name(), "some_name")
34+
inaccessiblePath := getInaccessiblePath(existingFile.Name())
3235

3336
tests := []struct {
3437
input string
@@ -48,7 +51,7 @@ func TestRoot(t *testing.T) {
4851
`root `, true, "", parseErrContent,
4952
},
5053
{
51-
fmt.Sprintf(`root %s`, unaccessiblePath), true, "", unableToAccessErrContent,
54+
fmt.Sprintf(`root %s`, inaccessiblePath), true, "", unableToAccessErrContent,
5255
},
5356
{
5457
fmt.Sprintf(`root {
@@ -60,8 +63,9 @@ func TestRoot(t *testing.T) {
6063
for i, test := range tests {
6164
c := NewTestController(test.input)
6265
mid, err := Root(c)
66+
6367
if test.shouldErr && err == nil {
64-
t.Errorf("Test %d: Expected error but found nil for input %s", i, test.input)
68+
t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input)
6569
}
6670

6771
if err != nil {
@@ -97,3 +101,8 @@ func getTempDirPath() (string, error) {
97101

98102
return tempDir, nil
99103
}
104+
105+
func getInaccessiblePath(file string) string {
106+
// null byte in filename is not allowed on Windows AND unix
107+
return filepath.Join("C:", "file\x00name")
108+
}

middleware/commands_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package middleware
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestSplitCommandAndArgs(t *testing.T) {
10+
var parseErrorContent = "error parsing command:"
11+
var noCommandErrContent = "no command contained in"
12+
13+
tests := []struct {
14+
input string
15+
expectedCommand string
16+
expectedArgs []string
17+
expectedErrContent string
18+
}{
19+
// Test case 0 - emtpy command
20+
{
21+
input: ``,
22+
expectedCommand: ``,
23+
expectedArgs: nil,
24+
expectedErrContent: noCommandErrContent,
25+
},
26+
// Test case 1 - command without arguments
27+
{
28+
input: `command`,
29+
expectedCommand: `command`,
30+
expectedArgs: nil,
31+
expectedErrContent: ``,
32+
},
33+
// Test case 2 - command with single argument
34+
{
35+
input: `command arg1`,
36+
expectedCommand: `command`,
37+
expectedArgs: []string{`arg1`},
38+
expectedErrContent: ``,
39+
},
40+
// Test case 3 - command with multiple arguments
41+
{
42+
input: `command arg1 arg2`,
43+
expectedCommand: `command`,
44+
expectedArgs: []string{`arg1`, `arg2`},
45+
expectedErrContent: ``,
46+
},
47+
// Test case 4 - command with single argument with space character - in quotes
48+
{
49+
input: `command "arg1 arg1"`,
50+
expectedCommand: `command`,
51+
expectedArgs: []string{`arg1 arg1`},
52+
expectedErrContent: ``,
53+
},
54+
// Test case 4 - command with single argument with space character - escaped
55+
{
56+
input: `command arg1\ arg1`,
57+
expectedCommand: `command`,
58+
expectedArgs: []string{`arg1 arg1`},
59+
expectedErrContent: ``,
60+
},
61+
// Test case 6 - command with escaped quote character
62+
{
63+
input: `command "arg1 \" arg1"`,
64+
expectedCommand: `command`,
65+
expectedArgs: []string{`arg1 " arg1`},
66+
expectedErrContent: ``,
67+
},
68+
// Test case 7 - command with escaped backslash
69+
{
70+
input: `command '\arg1'`,
71+
expectedCommand: `command`,
72+
expectedArgs: []string{`\arg1`},
73+
expectedErrContent: ``,
74+
},
75+
// Test case 8 - command with comments
76+
{
77+
input: `command arg1 #comment1 comment2`,
78+
expectedCommand: `command`,
79+
expectedArgs: []string{`arg1`},
80+
expectedErrContent: "",
81+
},
82+
// Test case 9 - command with multiple spaces and tab character
83+
{
84+
input: "command arg1 arg2\targ3",
85+
expectedCommand: `command`,
86+
expectedArgs: []string{`arg1`, `arg2`, "arg3"},
87+
expectedErrContent: "",
88+
},
89+
// Test case 10 - command with unclosed quotes
90+
{
91+
input: `command "arg1 arg2`,
92+
expectedCommand: "",
93+
expectedArgs: nil,
94+
expectedErrContent: parseErrorContent,
95+
},
96+
// Test case 11 - command with unclosed quotes
97+
{
98+
input: `command 'arg1 arg2"`,
99+
expectedCommand: "",
100+
expectedArgs: nil,
101+
expectedErrContent: parseErrorContent,
102+
},
103+
}
104+
105+
for i, test := range tests {
106+
errorPrefix := fmt.Sprintf("Test [%d]: ", i)
107+
errorSuffix := fmt.Sprintf(" Command to parse: [%s]", test.input)
108+
actualCommand, actualArgs, actualErr := SplitCommandAndArgs(test.input)
109+
110+
// test if error matches expectation
111+
if test.expectedErrContent != "" {
112+
if actualErr == nil {
113+
t.Errorf(errorPrefix+"Expected error with content [%s], found no error."+errorSuffix, test.expectedErrContent)
114+
} else if !strings.Contains(actualErr.Error(), test.expectedErrContent) {
115+
t.Errorf(errorPrefix+"Expected error with content [%s], found [%v]."+errorSuffix, test.expectedErrContent, actualErr)
116+
}
117+
} else if actualErr != nil {
118+
t.Errorf(errorPrefix+"Expected no error, found [%v]."+errorSuffix, actualErr)
119+
}
120+
121+
// test if command matches
122+
if test.expectedCommand != actualCommand {
123+
t.Errorf("Expected command: [%s], actual: [%s]."+errorSuffix, test.expectedCommand, actualCommand)
124+
}
125+
126+
// test if arguments match
127+
if len(test.expectedArgs) != len(actualArgs) {
128+
t.Errorf("Wrong number of arguments! Expected [%v], actual [%v]."+errorSuffix, test.expectedArgs, actualArgs)
129+
}
130+
131+
for j, actualArg := range actualArgs {
132+
expectedArg := test.expectedArgs[j]
133+
if actualArg != expectedArg {
134+
t.Errorf(errorPrefix+"Argument at position [%d] differ! Expected [%s], actual [%s]"+errorSuffix, j, expectedArg, actualArg)
135+
}
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)