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