1 package org.apache.maven.shared.utils.cli.shell;
2
3 import java.util.List;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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().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().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().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 List<String> shellCommandLine = sh.getShellCommandLine("\"some arg with spaces\"");
82
83 String cli = StringUtils.join( shellCommandLine.iterator(), " " );
84 System.out.println( cli );
85 assertTrue( cli.endsWith( "'\"some arg with spaces\"'" ) );
86 }
87
88 public void testAddSingleQuotesOnArgumentWithSpaces()
89 {
90 Shell sh = newShell();
91
92 sh.setWorkingDirectory( "/usr/bin" );
93 sh.setExecutable( "chmod" );
94
95 List<String> shellCommandLine = sh.getShellCommandLine("some arg with spaces");
96
97 String cli = StringUtils.join( shellCommandLine.iterator(), " " );
98 System.out.println( cli );
99 assertTrue( cli.endsWith("'some arg with spaces'"));
100 }
101
102 public void testAddArgumentWithSingleQuote()
103 {
104 Shell sh = newShell();
105
106 sh.setWorkingDirectory( "/usr/bin" );
107 sh.setExecutable( "chmod" );
108
109 List<String> shellCommandLine = sh.getShellCommandLine("arg'withquote");
110
111 assertEquals("cd '/usr/bin' && 'chmod' 'arg'\"'\"'withquote'", shellCommandLine.get(shellCommandLine.size() - 1));
112 }
113
114 public void testArgumentsWithSemicolon()
115 {
116
117 System.out.println( "---- semi colon tests ----" );
118
119 Shell sh = newShell();
120
121 sh.setWorkingDirectory( "/usr/bin" );
122 sh.setExecutable( "chmod" );
123
124 List<String> shellCommandLine = sh.getShellCommandLine(";some&argwithunix$chars");
125
126 String cli = StringUtils.join( shellCommandLine.iterator(), " " );
127 System.out.println( cli );
128 assertTrue( cli.endsWith( "';some&argwithunix$chars'" ) );
129
130 Commandline commandline = new Commandline( newShell() );
131 commandline.setExecutable( "chmod" );
132 commandline.getShell().setQuotedArgumentsEnabled( true );
133 commandline.createArg().setValue( "--password" );
134 commandline.createArg().setValue( ";password" );
135
136 List<String> lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
137 System.out.println( lines );
138
139 assertEquals( "/bin/sh", lines.get( 0 ) );
140 assertEquals( "-c", lines.get( 1 ) );
141 assertEquals( "'chmod' '--password' ';password'", lines.get( 2 ) );
142
143 commandline = new Commandline( newShell() );
144 commandline.setExecutable( "chmod" );
145 commandline.getShell().setQuotedArgumentsEnabled( true );
146 commandline.createArg().setValue( "--password" );
147 commandline.createArg().setValue( ";password" );
148 lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
149 System.out.println( lines );
150
151 assertEquals( "/bin/sh", lines.get( 0) );
152 assertEquals( "-c", lines.get( 1 ) );
153 assertEquals( "'chmod' '--password' ';password'", lines.get( 2 ) );
154
155 commandline = new Commandline( new CmdShell() );
156 commandline.getShell().setQuotedArgumentsEnabled( true );
157 commandline.createArg().setValue( "--password" );
158 commandline.createArg().setValue( ";password" );
159 lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
160 System.out.println( lines );
161
162 assertEquals( "cmd.exe", lines.get( 0 ) );
163 assertEquals( "/X", lines.get( 1 ) );
164 assertEquals( "/C", lines.get( 2 ) );
165 assertEquals( "\"--password ;password\"", lines.get( 3 ) );
166 }
167
168 public void testBourneShellQuotingCharacters()
169 throws Exception
170 {
171
172
173 Commandline commandline = new Commandline( newShell() );
174 commandline.setExecutable( "chmod" );
175 commandline.getShell().setQuotedArgumentsEnabled( true );
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 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
197 List<String> lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
198 System.out.println( lines );
199
200 assertEquals( "/bin/sh", lines.get( 0 ) );
201 assertEquals( "-c", lines.get( 1 ) );
202 assertEquals( "'chmod' ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`' '#'",
203 lines.get( 2 ) );
204 }
205
206 }