| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package execplugin |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func TestShlexSplit(t *testing.T) { |
| 13 |
testCases := []struct { |
| 14 |
name string |
| 15 |
input string |
| 16 |
expected []string |
| 17 |
wantErr bool |
| 18 |
}{ |
| 19 |
{ |
| 20 |
name: "basic space separation", |
| 21 |
input: `hello world`, |
| 22 |
expected: []string{"hello", "world"}, |
| 23 |
wantErr: false, |
| 24 |
}, |
| 25 |
{ |
| 26 |
name: "double quoted string", |
| 27 |
input: `"hello world"`, |
| 28 |
expected: []string{"hello world"}, |
| 29 |
wantErr: false, |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "single quoted string", |
| 33 |
input: `'hello world'`, |
| 34 |
expected: []string{"hello world"}, |
| 35 |
wantErr: false, |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "mixed quotes and words", |
| 39 |
input: `hello "world test"`, |
| 40 |
expected: []string{"hello", "world test"}, |
| 41 |
wantErr: false, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "single quotes with spaces", |
| 45 |
input: `hello 'world test'`, |
| 46 |
expected: []string{"hello", "world test"}, |
| 47 |
wantErr: false, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "nested quotes - single in double", |
| 51 |
input: `"hello 'nested' world"`, |
| 52 |
expected: []string{"hello 'nested' world"}, |
| 53 |
wantErr: false, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "nested quotes - double in single", |
| 57 |
input: `'hello "nested" world'`, |
| 58 |
expected: []string{"hello \"nested\" world"}, |
| 59 |
wantErr: false, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "escaped space", |
| 63 |
input: `hello\ world`, |
| 64 |
expected: []string{"hello world"}, |
| 65 |
wantErr: false, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "escaped quotes in double quotes", |
| 69 |
input: `"hello \"world\""`, |
| 70 |
expected: []string{"hello \"world\""}, |
| 71 |
wantErr: false, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "single quote in single quotes", |
| 75 |
input: `'can'\''t'`, |
| 76 |
expected: []string{"can't"}, |
| 77 |
wantErr: false, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "complex argument list", |
| 81 |
input: `arg1 "arg 2" 'arg 3' arg4`, |
| 82 |
expected: []string{"arg1", "arg 2", "arg 3", "arg4"}, |
| 83 |
wantErr: false, |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "echo command with escaped quotes", |
| 87 |
input: `echo "Hello, \"World!\""`, |
| 88 |
expected: []string{"echo", "Hello, \"World!\""}, |
| 89 |
wantErr: false, |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "grep command with quoted search term", |
| 93 |
input: `grep -r "search term" /path/to/dir`, |
| 94 |
expected: []string{"grep", "-r", "search term", "/path/to/dir"}, |
| 95 |
wantErr: false, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "ls command with quoted filename", |
| 99 |
input: `ls -la "file with spaces.txt"`, |
| 100 |
expected: []string{"ls", "-la", "file with spaces.txt"}, |
| 101 |
wantErr: false, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "empty string", |
| 105 |
input: ``, |
| 106 |
expected: []string{}, |
| 107 |
wantErr: false, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "multiple spaces", |
| 111 |
input: ` multiple spaces `, |
| 112 |
expected: []string{"multiple", "spaces"}, |
| 113 |
wantErr: false, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "with comment string", |
| 117 |
input: `echo "Hello, W#orld!" ${USER} # This is a comment`, |
| 118 |
expected: []string{"echo", "Hello, W#orld!", "${USER}"}, |
| 119 |
wantErr: false, |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "comment only", |
| 123 |
input: `# this line is just a comment`, |
| 124 |
expected: []string{}, |
| 125 |
wantErr: false, |
| 126 |
}, |
| 127 |
// may cause an error in shlex at python3 |
| 128 |
{ |
| 129 |
name: "unclosed double quote", |
| 130 |
input: `"unclosed quote`, |
| 131 |
expected: nil, |
| 132 |
wantErr: true, |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "unclosed single quote", |
| 136 |
input: `'unclosed quote`, |
| 137 |
expected: nil, |
| 138 |
wantErr: true, |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "mixed unclosed quotes", |
| 142 |
input: `"mixed 'quotes`, |
| 143 |
expected: nil, |
| 144 |
wantErr: true, |
| 145 |
}, |
| 146 |
{ |
| 147 |
name: "single quote closed with double quote", |
| 148 |
input: `"hello world'`, |
| 149 |
expected: nil, |
| 150 |
wantErr: true, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "double quote closed with single quote", |
| 154 |
input: `'hello world"`, |
| 155 |
expected: nil, |
| 156 |
wantErr: true, |
| 157 |
}, |
| 158 |
} |
| 159 |
|
| 160 |
// execute each test case |
| 161 |
for _, tc := range testCases { |
| 162 |
t.Run(tc.name, func(t *testing.T) { |
| 163 |
// call the ShlexSplit function |
| 164 |
result, err := ShlexSplit(tc.input) |
| 165 |
|
| 166 |
// check for expected error |
| 167 |
if tc.wantErr { |
| 168 |
if err == nil { |
| 169 |
t.Errorf("FAIL: Expected error but got none, Expected: %q\n", tc.expected) |
| 170 |
} |
| 171 |
return |
| 172 |
} |
| 173 |
|
| 174 |
if assert.NoError(t, err, "FAIL: Unexpected error %q for input %q", err, tc.input) { |
| 175 |
// check if the result matches the expected output |
| 176 |
assert.Equal(t, tc.expected, result, |
| 177 |
"FAIL: Result mismatch,Input %q, Expected %q, Got: %q\n", |
| 178 |
tc.input, tc.expected, result, |
| 179 |
) |
| 180 |
} |
| 181 |
}) |
| 182 |
} |
| 183 |
} |