mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-13 22:51:08 -05:00
Merge pull request #270 from Makpoc/master
Add tests for command splitting and fix root tests on Windows
This commit is contained in:
commit
65e812d3a9
2 changed files with 151 additions and 4 deletions
|
@ -26,9 +26,12 @@ func TestRoot(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
|
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
|
||||||
}
|
}
|
||||||
defer os.Remove(existingFile.Name())
|
defer func() {
|
||||||
|
existingFile.Close()
|
||||||
|
os.Remove(existingFile.Name())
|
||||||
|
}()
|
||||||
|
|
||||||
unaccessiblePath := filepath.Join(existingFile.Name(), "some_name")
|
inaccessiblePath := getInaccessiblePath(existingFile.Name())
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
|
@ -48,7 +51,7 @@ func TestRoot(t *testing.T) {
|
||||||
`root `, true, "", parseErrContent,
|
`root `, true, "", parseErrContent,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fmt.Sprintf(`root %s`, unaccessiblePath), true, "", unableToAccessErrContent,
|
fmt.Sprintf(`root %s`, inaccessiblePath), true, "", unableToAccessErrContent,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fmt.Sprintf(`root {
|
fmt.Sprintf(`root {
|
||||||
|
@ -60,8 +63,9 @@ func TestRoot(t *testing.T) {
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
c := NewTestController(test.input)
|
c := NewTestController(test.input)
|
||||||
mid, err := Root(c)
|
mid, err := Root(c)
|
||||||
|
|
||||||
if test.shouldErr && err == nil {
|
if test.shouldErr && err == nil {
|
||||||
t.Errorf("Test %d: Expected error but found nil for input %s", i, test.input)
|
t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -97,3 +101,8 @@ func getTempDirPath() (string, error) {
|
||||||
|
|
||||||
return tempDir, nil
|
return tempDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getInaccessiblePath(file string) string {
|
||||||
|
// null byte in filename is not allowed on Windows AND unix
|
||||||
|
return filepath.Join("C:", "file\x00name")
|
||||||
|
}
|
||||||
|
|
138
middleware/commands_test.go
Normal file
138
middleware/commands_test.go
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSplitCommandAndArgs(t *testing.T) {
|
||||||
|
var parseErrorContent = "error parsing command:"
|
||||||
|
var noCommandErrContent = "no command contained in"
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expectedCommand string
|
||||||
|
expectedArgs []string
|
||||||
|
expectedErrContent string
|
||||||
|
}{
|
||||||
|
// Test case 0 - emtpy command
|
||||||
|
{
|
||||||
|
input: ``,
|
||||||
|
expectedCommand: ``,
|
||||||
|
expectedArgs: nil,
|
||||||
|
expectedErrContent: noCommandErrContent,
|
||||||
|
},
|
||||||
|
// Test case 1 - command without arguments
|
||||||
|
{
|
||||||
|
input: `command`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: nil,
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 2 - command with single argument
|
||||||
|
{
|
||||||
|
input: `command arg1`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1`},
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 3 - command with multiple arguments
|
||||||
|
{
|
||||||
|
input: `command arg1 arg2`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1`, `arg2`},
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 4 - command with single argument with space character - in quotes
|
||||||
|
{
|
||||||
|
input: `command "arg1 arg1"`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1 arg1`},
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 4 - command with single argument with space character - escaped
|
||||||
|
{
|
||||||
|
input: `command arg1\ arg1`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1 arg1`},
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 6 - command with escaped quote character
|
||||||
|
{
|
||||||
|
input: `command "arg1 \" arg1"`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1 " arg1`},
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 7 - command with escaped backslash
|
||||||
|
{
|
||||||
|
input: `command '\arg1'`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`\arg1`},
|
||||||
|
expectedErrContent: ``,
|
||||||
|
},
|
||||||
|
// Test case 8 - command with comments
|
||||||
|
{
|
||||||
|
input: `command arg1 #comment1 comment2`,
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1`},
|
||||||
|
expectedErrContent: "",
|
||||||
|
},
|
||||||
|
// Test case 9 - command with multiple spaces and tab character
|
||||||
|
{
|
||||||
|
input: "command arg1 arg2\targ3",
|
||||||
|
expectedCommand: `command`,
|
||||||
|
expectedArgs: []string{`arg1`, `arg2`, "arg3"},
|
||||||
|
expectedErrContent: "",
|
||||||
|
},
|
||||||
|
// Test case 10 - command with unclosed quotes
|
||||||
|
{
|
||||||
|
input: `command "arg1 arg2`,
|
||||||
|
expectedCommand: "",
|
||||||
|
expectedArgs: nil,
|
||||||
|
expectedErrContent: parseErrorContent,
|
||||||
|
},
|
||||||
|
// Test case 11 - command with unclosed quotes
|
||||||
|
{
|
||||||
|
input: `command 'arg1 arg2"`,
|
||||||
|
expectedCommand: "",
|
||||||
|
expectedArgs: nil,
|
||||||
|
expectedErrContent: parseErrorContent,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
errorPrefix := fmt.Sprintf("Test [%d]: ", i)
|
||||||
|
errorSuffix := fmt.Sprintf(" Command to parse: [%s]", test.input)
|
||||||
|
actualCommand, actualArgs, actualErr := SplitCommandAndArgs(test.input)
|
||||||
|
|
||||||
|
// test if error matches expectation
|
||||||
|
if test.expectedErrContent != "" {
|
||||||
|
if actualErr == nil {
|
||||||
|
t.Errorf(errorPrefix+"Expected error with content [%s], found no error."+errorSuffix, test.expectedErrContent)
|
||||||
|
} else if !strings.Contains(actualErr.Error(), test.expectedErrContent) {
|
||||||
|
t.Errorf(errorPrefix+"Expected error with content [%s], found [%v]."+errorSuffix, test.expectedErrContent, actualErr)
|
||||||
|
}
|
||||||
|
} else if actualErr != nil {
|
||||||
|
t.Errorf(errorPrefix+"Expected no error, found [%v]."+errorSuffix, actualErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// test if command matches
|
||||||
|
if test.expectedCommand != actualCommand {
|
||||||
|
t.Errorf("Expected command: [%s], actual: [%s]."+errorSuffix, test.expectedCommand, actualCommand)
|
||||||
|
}
|
||||||
|
|
||||||
|
// test if arguments match
|
||||||
|
if len(test.expectedArgs) != len(actualArgs) {
|
||||||
|
t.Errorf("Wrong number of arguments! Expected [%v], actual [%v]."+errorSuffix, test.expectedArgs, actualArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
for j, actualArg := range actualArgs {
|
||||||
|
expectedArg := test.expectedArgs[j]
|
||||||
|
if actualArg != expectedArg {
|
||||||
|
t.Errorf(errorPrefix+"Argument at position [%d] differ! Expected [%s], actual [%s]"+errorSuffix, j, expectedArg, actualArg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue