|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 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 | +package org.parosproxy.paros; |
| 19 | + |
| 20 | +import static org.hamcrest.Matchers.equalTo; |
| 21 | +import static org.hamcrest.Matchers.hasItem; |
| 22 | +import static org.hamcrest.Matchers.hasSize; |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.junit.Assert.assertFalse; |
| 25 | +import static org.junit.Assert.assertThat; |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | +import static org.junit.Assert.fail; |
| 28 | +import static org.mockito.BDDMockito.given; |
| 29 | +import static org.mockito.Matchers.anyObject; |
| 30 | +import static org.mockito.Matchers.anyString; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Random; |
| 38 | +import java.util.Vector; |
| 39 | + |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Rule; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.rules.TemporaryFolder; |
| 44 | +import org.junit.runner.RunWith; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.MockitoAnnotations; |
| 47 | +import org.mockito.internal.util.reflection.Whitebox; |
| 48 | +import org.parosproxy.paros.extension.CommandLineArgument; |
| 49 | +import org.parosproxy.paros.extension.CommandLineListener; |
| 50 | +import org.powermock.api.mockito.PowerMockito; |
| 51 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 52 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 53 | +import org.zaproxy.zap.utils.I18N; |
| 54 | + |
| 55 | +@RunWith(PowerMockRunner.class) |
| 56 | +@PrepareForTest(Constant.class) |
| 57 | +public class CommandLineUnitTest { |
| 58 | + |
| 59 | + @Rule |
| 60 | + public TemporaryFolder folder = new TemporaryFolder(); |
| 61 | + |
| 62 | + private static final Vector<CommandLineArgument[]> NO_EXTENSIONS_CUSTOM_ARGUMENTS = new Vector<>(); |
| 63 | + private static final Map<String, CommandLineListener> NO_SUPPORTED_FILE_EXTENSIONS = Collections.emptyMap(); |
| 64 | + |
| 65 | + @Mock |
| 66 | + private Constant constant; |
| 67 | + @Mock |
| 68 | + private I18N i18n; |
| 69 | + |
| 70 | + private CommandLine cmdLine; |
| 71 | + |
| 72 | + @Before |
| 73 | + public void setUp() throws Exception { |
| 74 | + MockitoAnnotations.initMocks(this); |
| 75 | + mockConstantClass(); |
| 76 | + } |
| 77 | + |
| 78 | + private void mockConstantClass() { |
| 79 | + constant = PowerMockito.mock(Constant.class); |
| 80 | + i18n = PowerMockito.mock(I18N.class); |
| 81 | + Whitebox.setInternalState(constant, "messages", i18n); |
| 82 | + given(i18n.getString(anyString())).willReturn(""); |
| 83 | + given(i18n.getString(anyString(), anyObject())).willReturn(""); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void emptyCommandLine() throws Exception { |
| 88 | + cmdLine = new CommandLine(new String[] {}); |
| 89 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, NO_SUPPORTED_FILE_EXTENSIONS); |
| 90 | + assertTrue(cmdLine.isGUI()); |
| 91 | + assertFalse(cmdLine.isDaemon()); |
| 92 | + assertFalse(cmdLine.isReportVersion()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void daemonFlag() throws Exception { |
| 97 | + cmdLine = new CommandLine(new String[] { CommandLine.DAEMON }); |
| 98 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, NO_SUPPORTED_FILE_EXTENSIONS); |
| 99 | + assertFalse(cmdLine.isGUI()); |
| 100 | + assertTrue(cmdLine.isDaemon()); |
| 101 | + assertFalse(cmdLine.isReportVersion()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void shouldReportNonDaemonNorGuiIfSetCommandLineArgument() throws Exception { |
| 106 | + // Given / When |
| 107 | + cmdLine = new CommandLine(new String[] { CommandLine.CMD }); |
| 108 | + // Then |
| 109 | + assertThat(cmdLine.isDaemon(), is(equalTo(false))); |
| 110 | + assertThat(cmdLine.isGUI(), is(equalTo(false))); |
| 111 | + } |
| 112 | + |
| 113 | + @Test(expected = Exception.class) |
| 114 | + public void shouldFailIfSessionArgumentDoesNotHaveValue() throws Exception { |
| 115 | + // Given / When |
| 116 | + cmdLine = new CommandLine(new String[] { CommandLine.SESSION }); |
| 117 | + // Then = Exception.class |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void shouldAcceptSessionArgument() throws Exception { |
| 122 | + // Given |
| 123 | + String argumentValue = "/Dummy/Session/Path"; |
| 124 | + // When |
| 125 | + cmdLine = new CommandLine(new String[] { CommandLine.SESSION, argumentValue }); |
| 126 | + // Then |
| 127 | + assertThat(cmdLine.getArgument(CommandLine.SESSION), is(equalTo(argumentValue))); |
| 128 | + } |
| 129 | + |
| 130 | + @Test(expected = Exception.class) |
| 131 | + public void shouldFailIfNewSessionArgumentDoesNotHaveValue() throws Exception { |
| 132 | + // Given / When |
| 133 | + cmdLine = new CommandLine(new String[] { CommandLine.NEW_SESSION }); |
| 134 | + // Then = Exception.class |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void shouldAcceptNewSessionArgument() throws Exception { |
| 139 | + // Given |
| 140 | + String argumentValue = "/Dummy/Session/Path"; |
| 141 | + // When |
| 142 | + cmdLine = new CommandLine(new String[] { CommandLine.NEW_SESSION, argumentValue }); |
| 143 | + // Then |
| 144 | + assertThat(cmdLine.getArgument(CommandLine.NEW_SESSION), is(equalTo(argumentValue))); |
| 145 | + } |
| 146 | + |
| 147 | + @Test(expected = Exception.class) |
| 148 | + public void shouldFailIfPortArgumentDoesNotHaveValue() throws Exception { |
| 149 | + // Given / When |
| 150 | + cmdLine = new CommandLine(new String[] { CommandLine.PORT }); |
| 151 | + // Then = Exception.class |
| 152 | + } |
| 153 | + |
| 154 | + @Test(expected = Exception.class) |
| 155 | + public void shouldFailToParseInvalidPortArgument() throws Exception { |
| 156 | + // Given / When |
| 157 | + cmdLine = new CommandLine(new String[] { CommandLine.PORT, "InvalidPort" }); |
| 158 | + // Then = Exception.class |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void shouldParseValidPortArgument() throws Exception { |
| 163 | + // Given |
| 164 | + int port = 8080; |
| 165 | + // When |
| 166 | + cmdLine = new CommandLine(new String[] { CommandLine.PORT, Integer.toString(port) }); |
| 167 | + // Then |
| 168 | + assertThat(cmdLine.getPort(), is(equalTo(port))); |
| 169 | + assertThat(cmdLine.getArgument(CommandLine.PORT), is(equalTo("8080"))); |
| 170 | + } |
| 171 | + |
| 172 | + @Test(expected = Exception.class) |
| 173 | + public void shouldFailIfHostArgumentDoesNotHaveValue() throws Exception { |
| 174 | + // Given / When |
| 175 | + cmdLine = new CommandLine(new String[] { CommandLine.HOST }); |
| 176 | + // Then = Exception.class |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void shouldParseHostArgument() throws Exception { |
| 181 | + // Given |
| 182 | + String hostname = "127.0.0.1"; |
| 183 | + // When |
| 184 | + cmdLine = new CommandLine(new String[] { CommandLine.HOST, hostname }); |
| 185 | + // Then |
| 186 | + assertThat(cmdLine.getHost(), is(equalTo(hostname))); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void shouldGetNullFromNonGivenArgument() throws Exception { |
| 191 | + // Given |
| 192 | + cmdLine = new CommandLine(new String[] {}); |
| 193 | + // When |
| 194 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, NO_SUPPORTED_FILE_EXTENSIONS); |
| 195 | + // Then |
| 196 | + assertThat(cmdLine.getArgument("-NonGivenArgument"), is(equalTo(null))); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void shouldGetNullValueFromNonBuiltInArgument() throws Exception { |
| 201 | + // Given |
| 202 | + String argName = "-arg"; |
| 203 | + Vector<CommandLineArgument[]> supportedArguments = new Vector<>(); |
| 204 | + supportedArguments.add(new CommandLineArgument[] { new CommandLineArgument(argName, 1) }); |
| 205 | + cmdLine = new CommandLine(new String[] { argName, "value" }); |
| 206 | + // When |
| 207 | + cmdLine.parse(supportedArguments, NO_SUPPORTED_FILE_EXTENSIONS); |
| 208 | + // Then |
| 209 | + assertThat(cmdLine.getArgument(argName), is(equalTo(null))); |
| 210 | + } |
| 211 | + |
| 212 | + @Test(expected = Exception.class) |
| 213 | + public void shouldFailIfGivenUnsupportedArgument() throws Exception { |
| 214 | + // Given |
| 215 | + cmdLine = new CommandLine(new String[] { "-unsupported" }); |
| 216 | + // When |
| 217 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, NO_SUPPORTED_FILE_EXTENSIONS); |
| 218 | + // Then = Exception.class |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void claWithoutArgs() throws Exception { |
| 223 | + cmdLine = new CommandLine(new String[] { "-a", "-b" }); |
| 224 | + Vector<CommandLineArgument[]> customArguments = new Vector<>(); |
| 225 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-a", 0, null, null, null) }); |
| 226 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-b", 0, null, null, null) }); |
| 227 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-c", 0, null, null, null) }); |
| 228 | + cmdLine.parse(customArguments, NO_SUPPORTED_FILE_EXTENSIONS); |
| 229 | + |
| 230 | + assertTrue(customArguments.get(0)[0].isEnabled()); |
| 231 | + assertTrue(customArguments.get(1)[0].isEnabled()); |
| 232 | + assertFalse(customArguments.get(2)[0].isEnabled()); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + public void claWithArgs() throws Exception { |
| 237 | + cmdLine = new CommandLine(new String[] { "-a", "aaa", "-b", "bbb", "BBB" }); |
| 238 | + Vector<CommandLineArgument[]> customArguments = new Vector<>(); |
| 239 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-a", 1, null, null, null) }); |
| 240 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-b", 2, null, null, null) }); |
| 241 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-c", 3, null, null, null) }); |
| 242 | + cmdLine.parse(customArguments, NO_SUPPORTED_FILE_EXTENSIONS); |
| 243 | + |
| 244 | + assertTrue(customArguments.get(0)[0].isEnabled()); |
| 245 | + assertThat(customArguments.get(0)[0].getArguments(), hasSize(1)); |
| 246 | + assertThat(customArguments.get(0)[0].getArguments(), hasItem("aaa")); |
| 247 | + assertFalse(customArguments.get(0)[0].getArguments().contains("bbb")); |
| 248 | + |
| 249 | + assertTrue(customArguments.get(1)[0].isEnabled()); |
| 250 | + assertThat(customArguments.get(1)[0].getArguments(), hasSize(2)); |
| 251 | + assertFalse(customArguments.get(1)[0].getArguments().contains("aaa")); |
| 252 | + assertThat(customArguments.get(1)[0].getArguments(), hasItem("bbb")); |
| 253 | + assertThat(customArguments.get(1)[0].getArguments(), hasItem("BBB")); |
| 254 | + |
| 255 | + assertFalse(customArguments.get(2)[0].isEnabled()); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + public void claWithMissingArgs() throws Exception { |
| 260 | + cmdLine = new CommandLine(new String[] { "-a", "aaa", "-b", "bbb" }); |
| 261 | + Vector<CommandLineArgument[]> customArguments = new Vector<>(); |
| 262 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-a", 1, null, null, null) }); |
| 263 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-b", 2, null, null, null) }); |
| 264 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-c", 3, null, null, null) }); |
| 265 | + try { |
| 266 | + cmdLine.parse(customArguments, NO_SUPPORTED_FILE_EXTENSIONS); |
| 267 | + fail("Expected an exception"); |
| 268 | + } catch (Exception e) { |
| 269 | + // Expected |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + public void claWithPattern() throws Exception { |
| 275 | + cmdLine = new CommandLine(new String[] { "-script", "aaa", "bbb", "ccc" }); |
| 276 | + Vector<CommandLineArgument[]> customArguments = new Vector<>(); |
| 277 | + customArguments.add(new CommandLineArgument[] { new CommandLineArgument("-script", -1, ".*", null, null) }); |
| 278 | + cmdLine.parse(customArguments, NO_SUPPORTED_FILE_EXTENSIONS); |
| 279 | + assertTrue(customArguments.get(0)[0].isEnabled()); |
| 280 | + assertThat(customArguments.get(0)[0].getArguments().size(), is(equalTo(3))); |
| 281 | + } |
| 282 | + |
| 283 | + @Test(expected = Exception.class) |
| 284 | + public void shouldFailTheParseIfArgumentIsNotSupportedArgumentNorFile() throws Exception { |
| 285 | + // Given |
| 286 | + String notAFile = "NotAFile" + new Random().nextInt(); |
| 287 | + cmdLine = new CommandLine(new String[] { notAFile }); |
| 288 | + // When |
| 289 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, NO_SUPPORTED_FILE_EXTENSIONS); |
| 290 | + // Then = Exception.class |
| 291 | + } |
| 292 | + |
| 293 | + @Test(expected = Exception.class) |
| 294 | + public void shouldFailTheParseIfArgumentIsNotSupportedArgumentNorSupportedFileWithExtension() throws Exception { |
| 295 | + // Given |
| 296 | + cmdLine = new CommandLine(new String[] { "notsupported.test" }); |
| 297 | + // When |
| 298 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, NO_SUPPORTED_FILE_EXTENSIONS); |
| 299 | + // Then = Exception.class |
| 300 | + } |
| 301 | + |
| 302 | + @Test |
| 303 | + public void shouldAcceptFileArgumentIfHasSupportedFileExtension() throws Exception { |
| 304 | + // Given |
| 305 | + String fileExtension = "test"; |
| 306 | + File testFile = folder.newFile("aaa." + fileExtension); |
| 307 | + Map<String, CommandLineListener> supportedExtensions = new HashMap<>(); |
| 308 | + supportedExtensions.put(fileExtension, new AcceptAllFilesCommandLineListener()); |
| 309 | + cmdLine = new CommandLine(new String[] { testFile.toString() }); |
| 310 | + // When |
| 311 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, supportedExtensions); |
| 312 | + // Then = Accepted file argument |
| 313 | + } |
| 314 | + |
| 315 | + @Test(expected = Exception.class) |
| 316 | + public void shouldNotAcceptFileArgumentIfRejectedBySupportedFileExtension() throws Exception { |
| 317 | + // Given |
| 318 | + String fileExtension = "test"; |
| 319 | + File testFile = folder.newFile("aaa." + fileExtension); |
| 320 | + Map<String, CommandLineListener> supportedExtensions = new HashMap<>(); |
| 321 | + supportedExtensions.put(fileExtension, new RejectAllFilesCommandLineListener()); |
| 322 | + cmdLine = new CommandLine(new String[] { testFile.toString() }); |
| 323 | + // When |
| 324 | + cmdLine.parse(NO_EXTENSIONS_CUSTOM_ARGUMENTS, supportedExtensions); |
| 325 | + // Then = Exception.class |
| 326 | + } |
| 327 | + |
| 328 | + private static class AcceptAllFilesCommandLineListener implements CommandLineListener { |
| 329 | + |
| 330 | + @Override |
| 331 | + public boolean handleFile(File file) { |
| 332 | + return true; |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public List<String> getHandledExtensions() { |
| 337 | + return null; |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public void execute(CommandLineArgument[] args) { |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + private static class RejectAllFilesCommandLineListener implements CommandLineListener { |
| 346 | + |
| 347 | + @Override |
| 348 | + public boolean handleFile(File file) { |
| 349 | + return false; |
| 350 | + } |
| 351 | + |
| 352 | + @Override |
| 353 | + public List<String> getHandledExtensions() { |
| 354 | + return null; |
| 355 | + } |
| 356 | + |
| 357 | + @Override |
| 358 | + public void execute(CommandLineArgument[] args) { |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | +} |
0 commit comments