Commits

Tom Davies committed 3461519

add test

  • Participants
  • Parent commits 4f8e7ee
  • Branches fix-param-handling

Comments (0)

Files changed (2)

File src/main/java/com/atlassian/utils/process/ExternalProcessImpl.java

      * @param handler the process handler to manage the execution of this process
      */
     public ExternalProcessImpl(ExecutorService executorService, List<String> command, ProcessHandler handler) {
-        this.command = escapeParameters(command);
+        this.command = escapeArguments(command);
         setHandler(handler);
 
         this.executorService = executorService;
     }
 
 
-    public List<String> escapeParameters(List<String> commandArgs) {
-        for (int i = 0; i < commandArgs.size(); ++i) {
-            commandArgs.set(i, escapeArg(commandArgs.get(i)));
+    private List<String> escapeArguments(List<String> commandArgs) {
+        if (isWindows()) {
+            for (int i = 0; i < commandArgs.size(); ++i) {
+                commandArgs.set(i, escapeArg(commandArgs.get(i)));
+            }
         }
         return commandArgs;
     }
      * @param arg
      * @return
      */
-    private String escapeArg(String arg) {
+    protected String escapeArg(String arg) {
         String escapedArg = arg;
-        if (isWindows() && arg.contains("\"")) {
+        if (arg.contains("\"")) {
             boolean escapeNextChar = false;
             StringBuilder sb = new StringBuilder("\"");
             for (char c : arg.toCharArray()) {

File src/test/java/com/atlassian/utils/process/DoubleQuoteEscapingTest.java

+package com.atlassian.utils.process;
+
+import java.util.Collections;
+
+import org.junit.Test;
+
+import junit.framework.Assert;
+
+public class DoubleQuoteEscapingTest {
+    @Test
+    public void testEscaping() {
+        ExternalProcessImpl testProcess = new ExternalProcessImpl(null, Collections.<String>emptyList(), null);
+        testEscape(testProcess, "foo", "foo", false);
+        testEscape(testProcess, "f\"oo", "f\\\"oo", true); // unescaped quote is escaped
+        testEscape(testProcess, "f\\\"oo", "f\\\"oo", true); // already escaped " stays escaped
+        testEscape(testProcess, "\"foo\"", "\\\"foo\\\"", true); // leading and trailing quotes are escaped, and string is surrounded with quotes
+        testEscape(testProcess, "fo\"o\\", "fo\\\"o\\\\", true); // trailing \ is turned into an escaped \, so that it doesn't escape the added surrounding quote
+        testEscape(testProcess, "fo\\\"o\\\\", "fo\\\"o\\\\", true); // trailing \\ is left alone
+        testEscape(testProcess, "fo\"o\\\\o\\o\\x", "fo\\\"o\\\\o\\o\\x", true); // other escaped characters are left alone
+        testEscape(testProcess, "foo\\", "foo\\", false); // strings with no embedded quotes are not touched
+    }
+
+    private void testEscape(ExternalProcessImpl testProcess, String before, String expected, boolean shouldHaveSurroundingQuotes) {
+        String q = shouldHaveSurroundingQuotes ? "\"" : "";
+        Assert.assertEquals("The string '" + before + "' was not correctly escaped", q + expected + q, testProcess.escapeArg(before));
+    }
+}