View Javadoc
1   package org.codehaus.plexus.util.cli.shell;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * <p>BourneShellTest class.</p>
31   *
32   * @author herve
33   * @version $Id: $Id
34   * @since 3.4.0
35   */
36  public class BourneShellTest {
37  
38      /**
39       * <p>newShell.</p>
40       *
41       * @return a {@link org.codehaus.plexus.util.cli.shell.Shell} object.
42       */
43      protected Shell newShell() {
44          return new BourneShell();
45      }
46  
47      /**
48       * <p>testQuoteWorkingDirectoryAndExecutable.</p>
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       * <p>testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes.</p>
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       * <p>testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep.</p>
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       * <p>testPreserveSingleQuotesOnArgument.</p>
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      * <p>testAddSingleQuotesOnArgumentWithSpaces.</p>
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      * <p>testEscapeSingleQuotesOnArgument.</p>
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      * <p>testArgumentsWithsemicolon.</p>
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      * <p>testBourneShellQuotingCharacters.</p>
214      *
215      * @throws java.lang.Exception if any.
216      */
217     @Test
218     public void testBourneShellQuotingCharacters() throws Exception {
219         // { ' ', '$', ';', '&', '|', '<', '>', '*', '?', '(', ')' };
220         // test with values http://steve-parker.org/sh/bourne.shtml Appendix B - Meta-characters and Reserved Words
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 }