View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils.cli.shell;
20  
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  import org.apache.maven.shared.utils.StringUtils;
25  import org.apache.maven.shared.utils.cli.Commandline;
26  
27  public class BourneShellTest extends TestCase {
28  
29      Shell newShell() {
30          return new BourneShell();
31      }
32  
33      public void testQuoteWorkingDirectoryAndExecutable() {
34          Shell sh = newShell();
35  
36          sh.setWorkingDirectory("/usr/local/bin");
37          sh.setExecutable("chmod");
38  
39          String executable = StringUtils.join(sh.getShellCommandLine().iterator(), " ");
40  
41          assertEquals("/bin/sh -c cd '/usr/local/bin' && 'chmod'", executable);
42      }
43  
44      public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes() {
45          Shell sh = newShell();
46  
47          sh.setWorkingDirectory("/usr/local/'something else'");
48          sh.setExecutable("chmod");
49  
50          String executable = StringUtils.join(sh.getShellCommandLine().iterator(), " ");
51  
52          assertEquals("/bin/sh -c cd '/usr/local/'\"'\"'something else'\"'\"'' && 'chmod'", executable);
53      }
54  
55      public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep() {
56          Shell sh = newShell();
57  
58          sh.setWorkingDirectory("\\usr\\local\\'something else'");
59          sh.setExecutable("chmod");
60  
61          String executable = StringUtils.join(sh.getShellCommandLine().iterator(), " ");
62  
63          assertEquals("/bin/sh -c cd '\\usr\\local\\'\"'\"'something else'\"'\"'' && 'chmod'", executable);
64      }
65  
66      public void testPreserveSingleQuotesOnArgument() {
67          Shell sh = newShell();
68  
69          sh.setWorkingDirectory("/usr/bin");
70          sh.setExecutable("chmod");
71  
72          List<String> shellCommandLine = sh.getShellCommandLine("\"some arg with spaces\"");
73  
74          String cli = StringUtils.join(shellCommandLine.iterator(), " ");
75          assertTrue(cli.endsWith("'\"some arg with spaces\"'"));
76      }
77  
78      public void testAddSingleQuotesOnArgumentWithSpaces() {
79          Shell sh = newShell();
80  
81          sh.setWorkingDirectory("/usr/bin");
82          sh.setExecutable("chmod");
83  
84          List<String> shellCommandLine = sh.getShellCommandLine("some arg with spaces");
85  
86          String cli = StringUtils.join(shellCommandLine.iterator(), " ");
87          assertTrue(cli.endsWith("'some arg with spaces'"));
88      }
89  
90      public void testAddArgumentWithSingleQuote() {
91          Shell sh = newShell();
92  
93          sh.setWorkingDirectory("/usr/bin");
94          sh.setExecutable("chmod");
95  
96          List<String> shellCommandLine = sh.getShellCommandLine("arg'withquote");
97  
98          assertEquals(
99                  "cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'", shellCommandLine.get(shellCommandLine.size() - 1));
100     }
101 
102     public void testArgumentsWithSemicolon() {
103         Shell sh = newShell();
104 
105         sh.setWorkingDirectory("/usr/bin");
106         sh.setExecutable("chmod");
107 
108         List<String> shellCommandLine = sh.getShellCommandLine(";some&argwithunix$chars");
109 
110         String cli = StringUtils.join(shellCommandLine.iterator(), " ");
111         assertTrue(cli.endsWith("';some&argwithunix$chars'"));
112 
113         Commandline commandline = new Commandline(newShell());
114         commandline.setExecutable("chmod");
115         commandline.getShell().setQuotedArgumentsEnabled(true);
116         commandline.createArg().setValue("--password");
117         commandline.createArg().setValue(";password");
118 
119         List<String> lines = commandline.getShell().getShellCommandLine(commandline.getArguments());
120 
121         assertEquals("/bin/sh", lines.get(0));
122         assertEquals("-c", lines.get(1));
123         assertEquals("'chmod' '--password' ';password'", lines.get(2));
124 
125         commandline = new Commandline(newShell());
126         commandline.setExecutable("chmod");
127         commandline.getShell().setQuotedArgumentsEnabled(true);
128         commandline.createArg().setValue("--password");
129         commandline.createArg().setValue(";password");
130         lines = commandline.getShell().getShellCommandLine(commandline.getArguments());
131 
132         assertEquals("/bin/sh", lines.get(0));
133         assertEquals("-c", lines.get(1));
134         assertEquals("'chmod' '--password' ';password'", lines.get(2));
135 
136         commandline = new Commandline(new CmdShell());
137         commandline.getShell().setQuotedArgumentsEnabled(true);
138         commandline.createArg().setValue("--password");
139         commandline.createArg().setValue(";password");
140         lines = commandline.getShell().getShellCommandLine(commandline.getArguments());
141 
142         assertEquals("cmd.exe", lines.get(0));
143         assertEquals("/X", lines.get(1));
144         assertEquals("/C", lines.get(2));
145         assertEquals("\"--password ;password\"", lines.get(3));
146     }
147 
148     public void testBourneShellQuotingCharacters() throws Exception {
149         // { ' ', '$', ';', '&', '|', '<', '>', '*', '?', '(', ')' };
150         // test with values https://steve-parker.org/sh/bourne.shtml Appendix B - Meta-characters and Reserved Words
151         Commandline commandline = new Commandline(newShell());
152         commandline.setExecutable("chmod");
153         commandline.getShell().setQuotedArgumentsEnabled(true);
154         commandline.createArg().setValue(" ");
155         commandline.createArg().setValue("|");
156         commandline.createArg().setValue("&&");
157         commandline.createArg().setValue("||");
158         commandline.createArg().setValue(";");
159         commandline.createArg().setValue(";;");
160         commandline.createArg().setValue("&");
161         commandline.createArg().setValue("()");
162         commandline.createArg().setValue("<");
163         commandline.createArg().setValue("<<");
164         commandline.createArg().setValue(">");
165         commandline.createArg().setValue(">>");
166         commandline.createArg().setValue("*");
167         commandline.createArg().setValue("?");
168         commandline.createArg().setValue("[");
169         commandline.createArg().setValue("]");
170         commandline.createArg().setValue("{");
171         commandline.createArg().setValue("}");
172         commandline.createArg().setValue("`");
173         commandline.createArg().setValue("#");
174 
175         List<String> lines = commandline.getShell().getShellCommandLine(commandline.getArguments());
176 
177         assertEquals("/bin/sh", lines.get(0));
178         assertEquals("-c", lines.get(1));
179         assertEquals(
180                 "'chmod' ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`' '#'",
181                 lines.get(2));
182     }
183 }