diff --git a/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java b/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java index e37eb609ca0d8cdc52d2b72658310f514a043104..557a76af01ddf0bd0a96b2e11c04b008cd999e26 100644 --- a/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java +++ b/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java @@ -23,16 +23,25 @@ */ package hudson.util; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; - +import hudson.Functions; +import hudson.Launcher.LocalLauncher; import hudson.Launcher.RemoteLauncher; +import hudson.Proc; import hudson.model.Slave; + +import org.apache.tools.ant.util.JavaEnvUtils; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.Email; import org.jvnet.hudson.test.JenkinsRule; +import com.google.common.base.Joiner; + +import java.io.ByteArrayOutputStream; import java.io.StringWriter; /** @@ -61,4 +70,33 @@ public class ArgumentListBuilder2Test { System.out.println(out); assertTrue(out.toString().contains("$ java ********")); } + + @Test + public void ensureArgumentsArePassedToCommandUnmodified() throws Exception { + String[] specials = new String[] { + "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", + "_", "+", "{", "}", "[", "]", ":", ";", "\"", "'", "\\", "|", + "<", ">", ",", ".", "/", "?", " " + }; + + ArgumentListBuilder args = new ArgumentListBuilder(JavaEnvUtils.getJreExecutable("java"), "-cp", "target/test-classes/", "hudson.util.EchoCommand"); + args.add(specials); + if (Functions.isWindows()) { + args = args.toWindowsCommand(); + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + final StreamTaskListener listener = new StreamTaskListener(out); + Proc p = new LocalLauncher(listener) + .launch() + .stderr(System.err) + .stdout(out) + .cmds(args) + .start() + ; + int code = p.join(); + + String expected = String.format("%n%s", Joiner.on(" ").join(specials)); + assertThat("Error code " + code, out.toString(), containsString(expected)); + } } diff --git a/test/src/test/java/hudson/util/EchoCommand.java b/test/src/test/java/hudson/util/EchoCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..66fd690dfc4ac37d948edc9a74192dfcaee018b0 --- /dev/null +++ b/test/src/test/java/hudson/util/EchoCommand.java @@ -0,0 +1,40 @@ +/* + * The MIT License + * + * Copyright (c) 2015 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson.util; + +/** + * Command line utility to print all arguments as provided. + * + * @author ogondza + */ +public final class EchoCommand { + public static void main(String[] args) { + StringBuilder sb = new StringBuilder(); + for (String a: args) { + if (sb.length() != 0) sb.append(' '); + sb.append(a); + } + System.out.println(sb.toString()); + } +}