1 package org.apache.maven.shared.utils.cli.shell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.utils.StringUtils;
23 import org.apache.maven.shared.utils.cli.Commandline;
24
25 import java.util.Arrays;
26 import java.util.List;
27
28 import junit.framework.TestCase;
29
30 public class BourneShellTest
31 extends TestCase
32 {
33
34 Shell newShell()
35 {
36 return new BourneShell();
37 }
38
39 public void testQuoteWorkingDirectoryAndExecutable()
40 {
41 Shell sh = newShell();
42
43 sh.setWorkingDirectory( "/usr/local/bin" );
44 sh.setExecutable( "chmod" );
45
46 String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
47
48 assertEquals( "/bin/sh -c cd /usr/local/bin && chmod", executable );
49 }
50
51 public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes()
52 {
53 Shell sh = newShell();
54
55 sh.setWorkingDirectory( "/usr/local/'something else'" );
56 sh.setExecutable( "chmod" );
57
58 String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
59
60 assertEquals( "/bin/sh -c cd \"/usr/local/\'something else\'\" && chmod", executable );
61 }
62
63 public void testQuoteWorkingDirectoryAndExecutable_WDPathWithSingleQuotes_BackslashFileSep()
64 {
65 Shell sh = newShell();
66
67 sh.setWorkingDirectory( "\\usr\\local\\'something else'" );
68 sh.setExecutable( "chmod" );
69
70 String executable = StringUtils.join( sh.getShellCommandLine( new String[]{} ).iterator(), " " );
71
72 assertEquals( "/bin/sh -c cd \"\\usr\\local\\\'something else\'\" && chmod", executable );
73 }
74
75 public void testPreserveSingleQuotesOnArgument()
76 {
77 Shell sh = newShell();
78
79 sh.setWorkingDirectory( "/usr/bin" );
80 sh.setExecutable( "chmod" );
81
82 String[] args = { "\'some arg with spaces\'" };
83
84 List<String> shellCommandLine = sh.getShellCommandLine( args );
85
86 String cli = StringUtils.join( shellCommandLine.iterator(), " " );
87 System.out.println( cli );
88 assertTrue( cli.endsWith( args[0] ) );
89 }
90
91 public void testAddSingleQuotesOnArgumentWithSpaces()
92 {
93 Shell sh = newShell();
94
95 sh.setWorkingDirectory( "/usr/bin" );
96 sh.setExecutable( "chmod" );
97
98 String[] args = { "some arg with spaces" };
99
100 List<String> shellCommandLine = sh.getShellCommandLine( args );
101
102 String cli = StringUtils.join( shellCommandLine.iterator(), " " );
103 System.out.println( cli );
104 assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
105 }
106
107 public void testArgumentsWithsemicolon()
108 {
109
110 System.out.println( "---- semi colon tests ----" );
111
112 Shell sh = newShell();
113
114 sh.setWorkingDirectory( "/usr/bin" );
115 sh.setExecutable( "chmod" );
116
117 String[] args = { ";some&argwithunix$chars" };
118
119 List<String> shellCommandLine = sh.getShellCommandLine( args );
120
121 String cli = StringUtils.join( shellCommandLine.iterator(), " " );
122 System.out.println( cli );
123 assertTrue( cli.endsWith( "\'" + args[0] + "\'" ) );
124
125 Commandline commandline = new Commandline( newShell() );
126 commandline.setExecutable( "chmod" );
127 commandline.getShell().setQuotedArgumentsEnabled( true );
128 commandline.createArg().setValue( "--password" );
129 commandline.createArg().setValue( ";password" );
130
131 List<String> lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
132 System.out.println( lines );
133
134 assertEquals( "/bin/sh", lines.get( 0 ) );
135 assertEquals( "-c", lines.get( 1 ) );
136 assertEquals( "chmod --password ';password'", lines.get( 2 ) );
137
138 commandline = new Commandline( newShell() );
139 commandline.setExecutable( "chmod" );
140 commandline.getShell().setQuotedArgumentsEnabled( true );
141 commandline.createArg().setValue( "--password" );
142 commandline.createArg().setValue( ";password" );
143 lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
144 System.out.println( Arrays.asList( lines ) );
145
146 assertEquals( "/bin/sh", lines.get( 0) );
147 assertEquals( "-c", lines.get( 1 ) );
148 assertEquals( "chmod --password ';password'", lines.get( 2 ) );
149
150 commandline = new Commandline( new CmdShell() );
151 commandline.getShell().setQuotedArgumentsEnabled( true );
152 commandline.createArg().setValue( "--password" );
153 commandline.createArg().setValue( ";password" );
154 lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
155 System.out.println( Arrays.asList( lines ) );
156
157 assertEquals( "cmd.exe", lines.get( 0 ) );
158 assertEquals( "/X", lines.get( 1 ) );
159 assertEquals( "/C", lines.get( 2 ) );
160 assertEquals( "\"--password ;password\"", lines.get( 3 ) );
161 }
162
163 public void testBourneShellQuotingCharacters()
164 throws Exception
165 {
166
167
168 Commandline commandline = new Commandline( newShell() );
169 commandline.setExecutable( "chmod" );
170 commandline.getShell().setQuotedArgumentsEnabled( true );
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 commandline.createArg().setValue( "`" );
190
191 List<String> lines = commandline.getShell().getShellCommandLine( commandline.getArguments() );
192 System.out.println( lines );
193
194 assertEquals( "/bin/sh", lines.get( 0 ) );
195 assertEquals( "-c", lines.get( 1 ) );
196 assertEquals( "chmod ' ' '|' '&&' '||' ';' ';;' '&' '()' '<' '<<' '>' '>>' '*' '?' '[' ']' '{' '}' '`'",
197 lines.get( 2 ) );
198 }
199
200 }