changeset 1711:276b0f97412a

Fix quoted arguments in thermostat shell Fix a bug where arguments parsing in thermostat shell did not respect quoted arguments and instead split on any and all space characters, so it was not possible to provide commands with arguments containing any space characters. Introduce new parser rather than relying on String#split. PR2490 http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2490 Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-July/014380.html
author Andrew Azores <aazores@redhat.com>
date Thu, 02 Jul 2015 10:10:40 -0400
parents 763dc2378c4e
children b2a64131e8a6
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellArgsParser.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellArgsParserTest.java
diffstat 3 files changed, 305 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellArgsParser.java	Thu Jul 02 10:10:40 2015 -0400
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2012-2015 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Parser for thermostat shell command line input. Splits lines on whitespaces to chunk commands and arguments,
+ * respecting quotation marks, so that ex:
+ *
+ * some-command --flag "quoted arg"
+ *
+ * is split into three parts: "some-command", "--flag", "quoted arg"
+ */
+class ShellArgsParser {
+
+    private final String input;
+    private int pos = 0;
+    private char c;
+
+    ShellArgsParser(String input) {
+        this.input = input;
+        if (input.length() > 0) {
+            c = input.charAt(pos);
+        }
+    }
+
+    String[] parse() {
+        if (input.isEmpty()) {
+            return new String[]{};
+        }
+        List<String> result = new ArrayList<>();
+        while (ready()) {
+            if (isWhitespace()) {
+                whitespace();
+            } else if (isQuote()) {
+                result.add(quote());
+            } else {
+                result.add(word());
+            }
+        }
+        return result.toArray(new String[result.size()]);
+    }
+
+    private void whitespace() {
+        while (isWhitespace() && ready()) {
+            readChar();
+        }
+    }
+
+    private String quote() {
+        StringBuilder sb = new StringBuilder();
+        while (ready()) {
+            readChar();
+            if (isEscapedQuote()) {
+                readChar();
+                sb.append(c);
+                continue;
+            }
+            if (isQuote()) {
+                break;
+            }
+            sb.append(c);
+        }
+        return sb.toString();
+    }
+
+    private String word() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(c);
+        while (!isWhitespace() && ready()) {
+            readChar();
+            if (isEscapedQuote()) {
+                readChar();
+                sb.append(c);
+                continue;
+            }
+            if (!isWhitespace()) {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+    private boolean ready() {
+        return pos < input.length() - 1;
+    }
+
+    private char readChar() {
+        c = input.charAt(++pos);
+        return c;
+    }
+
+    private char lookahead() {
+        return input.charAt(pos + 1);
+    }
+
+    private boolean isEscapedQuote() {
+        return c == '\\' && lookahead() == '"';
+    }
+
+    private boolean isQuote() {
+        return c == '"';
+    }
+
+    private boolean isWhitespace() {
+        return Character.isWhitespace(c);
+    }
+
+}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java	Tue Jun 23 12:32:59 2015 +0200
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java	Thu Jul 02 10:10:40 2015 -0400
@@ -196,7 +196,7 @@
     }
 
     private void launchCommand(String line) throws CommandException {
-        String[] parsed = line.split(" +");
+        String[] parsed = new ShellArgsParser(line).parse();
         ServiceReference launcherRef = bundleContext.getServiceReference(Launcher.class.getName());
         if (launcherRef != null) {
             Launcher launcher = (Launcher) bundleContext.getService(launcherRef);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellArgsParserTest.java	Thu Jul 02 10:10:40 2015 -0400
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2012-2015 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+
+public class ShellArgsParserTest {
+
+    @Test
+    public void testEmptyString() {
+        assertEmpty("");
+    }
+
+    @Test
+    public void testSingleArg() {
+        assertResult("foo", "foo");
+    }
+
+    @Test
+    public void testTwoArgs() {
+        assertResult("foo bar", "foo", "bar");
+    }
+
+    @Test
+    public void testThreeArgs() {
+        assertResult("foo bar baz", "foo", "bar", "baz");
+    }
+
+    @Test
+    public void testLeadingSpaces() {
+        assertResult("    foo", "foo");
+    }
+
+    @Test
+    public void testTrailingSpaces() {
+        assertResult("foo    ", "foo");
+    }
+
+    @Test
+    public void testInnerSpaces() {
+        assertResult("foo    bar", "foo", "bar");
+    }
+
+    @Test
+    public void testLotsOfSpaces() {
+        assertResult("    foo    bar    ", "foo", "bar");
+    }
+
+    @Test
+    public void testOnlySpaces() {
+        assertEmpty("    ");
+    }
+
+    @Test
+    public void testTabCharacter() {
+        assertResult("foo\tbar", "foo", "bar");
+    }
+
+    @Test
+    public void testQuotedArg() {
+        assertResult("\"foo\"", "foo");
+    }
+
+    @Test
+    public void testQuotedString() {
+        assertResult("\"foo bar\"", "foo bar");
+    }
+
+    @Test
+    public void testSingleStartingQuote() {
+        // malformed argument
+        assertResult("\"foo", "foo");
+    }
+
+    @Test
+    public void testSingleEndingQuote() {
+        // malformed argument
+        assertResult("foo\"", "foo\"");
+    }
+
+    @Test
+    public void testSingleMiddleQuote() {
+        // malformed argument
+        assertResult("foo \" bar", "foo", " bar");
+    }
+
+    @Test
+    public void testSingleEscapedQuote() {
+        assertResult("foo\\\"", "foo\"");
+    }
+
+    @Test
+    public void testQuoteContainingEscapedQuoteLiteral() {
+        assertResult("\"foo \\\" bar\"", "foo \" bar");
+    }
+
+    @Test
+    public void testQuoteContainingEscapedQuoteLiteral2() {
+        assertResult("\"foo \\\"\"", "foo \"");
+    }
+
+    @Test
+    public void testQuotedEmptyString() {
+        assertResult("\"\"", "");
+    }
+
+    @Test
+    public void testQuotedSpacesString() {
+        assertResult("\" \"", " ");
+    }
+
+    private void assertEmpty(String input) {
+        ShellArgsParser sap = new ShellArgsParser(input);
+        String[] result = sap.parse();
+        assertArrayEquals(new String[]{}, result);
+    }
+
+    private void assertResult(String input, String ... expecteds) {
+        ShellArgsParser sap = new ShellArgsParser(input);
+        String[] result = sap.parse();
+        assertArrayEquals(expecteds, result);
+    }
+}