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 junit.framework.TestCase;
20  import org.codehaus.plexus.util.StringUtils;
21  import org.codehaus.plexus.util.cli.Commandline;
22  
23  import java.util.Arrays;
24  import java.util.List;
25  
26  public class BourneShellTest
27      extends TestCase
28  {
29  
30      protected Shell newShell()
31      {
32          return new BourneShell();
33      }
34  
35      public void testQuoteWorkingDirectoryAndExecutable()
36      {
37          Shell sh = newShell();
38  
39          sh.setWorkingDirectory( "/usr/local/bin" );
40          sh.setExecutable( "chmod" );
41  
42          String executable = StringUtils.join( sh.getShellCommandLine( new String[] {} ).iterator(), " " );
43  
44          assertEquals( "/bin/sh -c cd '/usr/local/bin' && 'chmod'", executable );
45      }
46  
47      public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes()
48      {
49          Shell sh = newShell();
50  
51          sh.setWorkingDirectory( "/usr/local/'something else'" );
52          sh.setExecutable( "chmod" );
53  
54          String executable = StringUtils.join( sh.getShellCommandLine( new String[] {} ).iterator(), " " );
55  
56          assertEquals( "/bin/sh -c cd '/usr/local/'\"'\"'something else'\"'\"'' && 'chmod'", executable );
57      }
58  
59      public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep()
60      {
61          Shell sh = newShell();
62  
63          sh.setWorkingDirectory( "\\usr\\local\\'something else'" );
64          sh.setExecutable( "chmod" );
65  
66          String executable = StringUtils.join( sh.getShellCommandLine( new String[] {} ).iterator(), " " );
67  
68          assertEquals( "/bin/sh -c cd '\\usr\\local\\\'\"'\"'something else'\"'\"'' && 'chmod'", executable );
69      }
70  
71      public void testPreserveSingleQuotesOnArgument()
72      {
73          Shell sh = newShell();
74  
75          sh.setWorkingDirectory( "/usr/bin" );
76          sh.setExecutable( "chmod" );
77  
78          String[] args = { "\'some arg with spaces\'" };
79  
80          List<String> shellCommandLine = sh.getShellCommandLine( args );
81  
82          String cli = StringUtils.join( shellCommandLine.iterator(), " " );
83          System.out.println( cli );
84          assertTrue( cli.endsWith( "''\"'\"'some arg with spaces'\"'\"''" ) );
85      }
86  
87      public void testAddSingleQuotesOnArgumentWithSpaces()
88      {
89          Shell sh = newShell();
90  
91          sh.setWorkingDirectory( "/usr/bin" );
92          sh.setExecutable( "chmod" );
93  
94          String[] args = { "some arg with spaces" };
95  
96          List<String> shellCommandLine = sh.getShellCommandLine( args );
97  
98          String cli = StringUtils.join( shellCommandLine.iterator(), " " );
99          System.out.println( cli );
100         assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
101     }
102 
103     public void testEscapeSingleQuotesOnArgument()
104     {
105         Shell sh = newShell();
106 
107         sh.setWorkingDirectory( "/usr/bin" );
108         sh.setExecutable( "chmod" );
109 
110         String[] args = { "arg'withquote" };
111 
112         List<String> shellCommandLine = sh.getShellCommandLine( args );
113 
114         String cli = StringUtils.join( shellCommandLine.iterator(), " " );
115         System.out.println( cli );
116         assertEquals( "cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'",
117                       shellCommandLine.get( shellCommandLine.size() - 1 ) );
118     }
119 
120     public void testArgumentsWithsemicolon()
121     {
122 
123         System.out.println( "---- semi colon tests ----" );
124 
125         Shell sh = newShell();
126 
127         sh.setWorkingDirectory( "/usr/bin" );
128         sh.setExecutable( "chmod" );
129 
130         String[] args = { ";some&argwithunix$chars" };
131 
132         List<String> shellCommandLine = sh.getShellCommandLine( args );
133 
134         String cli = StringUtils.join( shellCommandLine.iterator(), " " );
135         System.out.println( cli );
136         assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
137 
138         Commandline commandline = new Commandline( newShell() );
139         commandline.setExecutable( "chmod" );
140         commandline.getShell().setQuotedArgumentsEnabled( true );
141         commandline.createArg().setValue( "--password" );
142         commandline.createArg().setValue( ";password" );
143 
144         String[] lines = commandline.getShellCommandline();
145         System.out.println( Arrays.asList( lines ) );
146 
147         assertEquals( "/bin/sh", lines[0] );
148         assertEquals( "-c", lines[1] );
149         assertEquals( "'chmod' '--password' ';password'", lines[2] );
150 
151         commandline = new Commandline( newShell() );
152         commandline.setExecutable( "chmod" );
153         commandline.getShell().setQuotedArgumentsEnabled( true );
154         commandline.createArg().setValue( "--password" );
155         commandline.createArg().setValue( ";password" );
156         lines = commandline.getShellCommandline();
157         System.out.println( Arrays.asList( lines ) );
158 
159         assertEquals( "/bin/sh", lines[0] );
160         assertEquals( "-c", lines[1] );
161         assertEquals( "'chmod' '--password' ';password'", lines[2] );
162 
163         commandline = new Commandline( new CmdShell() );
164         commandline.getShell().setQuotedArgumentsEnabled( true );
165         commandline.createArg().setValue( "--password" );
166         commandline.createArg().setValue( ";password" );
167         lines = commandline.getShellCommandline();
168         System.out.println( Arrays.asList( lines ) );
169 
170         assertEquals( "cmd.exe", lines[0] );
171         assertEquals( "/X", lines[1] );
172         assertEquals( "/C", lines[2] );
173         assertEquals( "\"--password ;password\"", lines[3] );
174     }
175 
176     public void testBourneShellQuotingCharacters()
177         throws Exception
178     {
179         // { ' ', '$', ';', '&', '|', '<', '>', '*', '?', '(', ')' };
180         // test with values http://steve-parker.org/sh/bourne.shtml Appendix B - Meta-characters and Reserved Words
181         Commandline commandline = new Commandline( newShell() );
182         commandline.setExecutable( "chmod" );
183         commandline.getShell().setQuotedArgumentsEnabled( true );
184         commandline.createArg().setValue( " " );
185         commandline.createArg().setValue( "|" );
186         commandline.createArg().setValue( "&&" );
187         commandline.createArg().setValue( "||" );
188         commandline.createArg().setValue( ";" );
189         commandline.createArg().setValue( ";;" );
190         commandline.createArg().setValue( "&" );
191         commandline.createArg().setValue( "()" );
192         commandline.createArg().setValue( "<" );
193         commandline.createArg().setValue( "<<" );
194         commandline.createArg().setValue( ">" );
195         commandline.createArg().setValue( ">>" );
196         commandline.createArg().setValue( "*" );
197         commandline.createArg().setValue( "?" );
198         commandline.createArg().setValue( "[" );
199         commandline.createArg().setValue( "]" );
200         commandline.createArg().setValue( "{" );
201         commandline.createArg().setValue( "}" );
202         commandline.createArg().setValue( "`" );
203 
204         String[] lines = commandline.getShellCommandline();
205         System.out.println( Arrays.asList( lines ) );
206 
207         assertEquals( "/bin/sh", lines[0] );
208         assertEquals( "-c", lines[1] );
209         assertEquals( "'chmod' ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`'",
210                       lines[2] );
211 
212     }
213 
214 }