1 package org.codehaus.plexus.util.cli.shell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.codehaus.plexus.util.StringUtils;
23 import org.codehaus.plexus.util.cli.Commandline;
24 import org.junit.jupiter.api.Test;
25
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29
30
31
32
33
34
35
36 public class BourneShellTest {
37
38
39
40
41
42
43 protected Shell newShell() {
44 return new BourneShell();
45 }
46
47
48
49
50 @org.junit.jupiter.api.Test
51 public void testQuoteWorkingDirectoryAndExecutable() {
52 Shell sh = newShell();
53
54 sh.setWorkingDirectory("/usr/local/bin");
55 sh.setExecutable("chmod");
56
57 String executable =
58 StringUtils.join(sh.getShellCommandLine(new String[] {}).iterator(), " ");
59
60 assertEquals("/bin/sh -c cd '/usr/local/bin' && 'chmod'", executable);
61 }
62
63
64
65
66 @Test
67 public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes() {
68 Shell sh = newShell();
69
70 sh.setWorkingDirectory("/usr/local/'something else'");
71 sh.setExecutable("chmod");
72
73 String executable =
74 StringUtils.join(sh.getShellCommandLine(new String[] {}).iterator(), " ");
75
76 assertEquals("/bin/sh -c cd '/usr/local/'\"'\"'something else'\"'\"'' && 'chmod'", executable);
77 }
78
79
80
81
82 @org.junit.jupiter.api.Test
83 public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep() {
84 Shell sh = newShell();
85
86 sh.setWorkingDirectory("\\usr\\local\\'something else'");
87 sh.setExecutable("chmod");
88
89 String executable =
90 StringUtils.join(sh.getShellCommandLine(new String[] {}).iterator(), " ");
91
92 assertEquals("/bin/sh -c cd '\\usr\\local\\\'\"'\"'something else'\"'\"'' && 'chmod'", executable);
93 }
94
95
96
97
98 @Test
99 public void testPreserveSingleQuotesOnArgument() {
100 Shell sh = newShell();
101
102 sh.setWorkingDirectory("/usr/bin");
103 sh.setExecutable("chmod");
104
105 String[] args = {"\'some arg with spaces\'"};
106
107 List<String> shellCommandLine = sh.getShellCommandLine(args);
108
109 String cli = StringUtils.join(shellCommandLine.iterator(), " ");
110 System.out.println(cli);
111 assertTrue(cli.endsWith("''\"'\"'some arg with spaces'\"'\"''"));
112 }
113
114
115
116
117 @Test
118 public void testAddSingleQuotesOnArgumentWithSpaces() {
119 Shell sh = newShell();
120
121 sh.setWorkingDirectory("/usr/bin");
122 sh.setExecutable("chmod");
123
124 String[] args = {"some arg with spaces"};
125
126 List<String> shellCommandLine = sh.getShellCommandLine(args);
127
128 String cli = StringUtils.join(shellCommandLine.iterator(), " ");
129 System.out.println(cli);
130 assertTrue(cli.endsWith("\'" + args[0] + "\'"));
131 }
132
133
134
135
136 @Test
137 public void testEscapeSingleQuotesOnArgument() {
138 Shell sh = newShell();
139
140 sh.setWorkingDirectory("/usr/bin");
141 sh.setExecutable("chmod");
142
143 String[] args = {"arg'withquote"};
144
145 List<String> shellCommandLine = sh.getShellCommandLine(args);
146
147 String cli = StringUtils.join(shellCommandLine.iterator(), " ");
148 System.out.println(cli);
149 assertEquals(
150 "cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'", shellCommandLine.get(shellCommandLine.size() - 1));
151 }
152
153
154
155
156 @Test
157 public void testArgumentsWithsemicolon() {
158
159 System.out.println("---- semi colon tests ----");
160
161 Shell sh = newShell();
162
163 sh.setWorkingDirectory("/usr/bin");
164 sh.setExecutable("chmod");
165
166 String[] args = {";some&argwithunix$chars"};
167
168 List<String> shellCommandLine = sh.getShellCommandLine(args);
169
170 String cli = StringUtils.join(shellCommandLine.iterator(), " ");
171 System.out.println(cli);
172 assertTrue(cli.endsWith("\'" + args[0] + "\'"));
173
174 Commandline commandline = new Commandline(newShell());
175 commandline.setExecutable("chmod");
176 commandline.getShell().setQuotedArgumentsEnabled(true);
177 commandline.createArg().setValue("--password");
178 commandline.createArg().setValue(";password");
179
180 String[] lines = commandline.getShellCommandline();
181 System.out.println(Arrays.asList(lines));
182
183 assertEquals("/bin/sh", lines[0]);
184 assertEquals("-c", lines[1]);
185 assertEquals("'chmod' '--password' ';password'", lines[2]);
186
187 commandline = new Commandline(newShell());
188 commandline.setExecutable("chmod");
189 commandline.getShell().setQuotedArgumentsEnabled(true);
190 commandline.createArg().setValue("--password");
191 commandline.createArg().setValue(";password");
192 lines = commandline.getShellCommandline();
193 System.out.println(Arrays.asList(lines));
194
195 assertEquals("/bin/sh", lines[0]);
196 assertEquals("-c", lines[1]);
197 assertEquals("'chmod' '--password' ';password'", lines[2]);
198
199 commandline = new Commandline(new CmdShell());
200 commandline.getShell().setQuotedArgumentsEnabled(true);
201 commandline.createArg().setValue("--password");
202 commandline.createArg().setValue(";password");
203 lines = commandline.getShellCommandline();
204 System.out.println(Arrays.asList(lines));
205
206 assertEquals("cmd.exe", lines[0]);
207 assertEquals("/X", lines[1]);
208 assertEquals("/C", lines[2]);
209 assertEquals("\"--password ;password\"", lines[3]);
210 }
211
212
213
214
215
216
217 @Test
218 public void testBourneShellQuotingCharacters() throws Exception {
219
220
221 Commandline commandline = new Commandline(newShell());
222 commandline.setExecutable("chmod");
223 commandline.getShell().setQuotedArgumentsEnabled(true);
224 commandline.createArg().setValue(" ");
225 commandline.createArg().setValue("|");
226 commandline.createArg().setValue("&&");
227 commandline.createArg().setValue("||");
228 commandline.createArg().setValue(";");
229 commandline.createArg().setValue(";;");
230 commandline.createArg().setValue("&");
231 commandline.createArg().setValue("()");
232 commandline.createArg().setValue("<");
233 commandline.createArg().setValue("<<");
234 commandline.createArg().setValue(">");
235 commandline.createArg().setValue(">>");
236 commandline.createArg().setValue("*");
237 commandline.createArg().setValue("?");
238 commandline.createArg().setValue("[");
239 commandline.createArg().setValue("]");
240 commandline.createArg().setValue("{");
241 commandline.createArg().setValue("}");
242 commandline.createArg().setValue("`");
243
244 String[] lines = commandline.getShellCommandline();
245 System.out.println(Arrays.asList(lines));
246
247 assertEquals("/bin/sh", lines[0]);
248 assertEquals("-c", lines[1]);
249 assertEquals(
250 "'chmod' ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`'", lines[2]);
251 }
252 }