PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/src/test/java/hudson/util/ArgumentListBuilder2Test.java

https://gitlab.com/CORP-RESELLER/jenkins
Java | 112 lines | 72 code | 11 blank | 29 comment | 0 complexity | 5b1989877d7972260af8010b08bbc320 MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010, Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import static org.hamcrest.MatcherAssert.assertThat;
  26. import static org.hamcrest.Matchers.containsString;
  27. import static org.hamcrest.Matchers.equalTo;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertTrue;
  30. import static org.junit.Assume.assumeTrue;
  31. import hudson.Functions;
  32. import hudson.Launcher.LocalLauncher;
  33. import hudson.Launcher.RemoteLauncher;
  34. import hudson.Proc;
  35. import hudson.model.Slave;
  36. import org.apache.tools.ant.util.JavaEnvUtils;
  37. import org.junit.Rule;
  38. import org.junit.Test;
  39. import org.jvnet.hudson.test.Email;
  40. import org.jvnet.hudson.test.JenkinsRule;
  41. import com.google.common.base.Joiner;
  42. import java.io.ByteArrayOutputStream;
  43. import java.io.StringWriter;
  44. /**
  45. * @author Kohsuke Kawaguchi
  46. */
  47. public class ArgumentListBuilder2Test {
  48. @Rule
  49. public JenkinsRule j = new JenkinsRule();
  50. /**
  51. * Makes sure {@link RemoteLauncher} properly masks arguments.
  52. */
  53. @Test
  54. @Email("http://n4.nabble.com/Password-masking-when-running-commands-on-a-slave-tp1753033p1753033.html")
  55. public void slaveMask() throws Exception {
  56. ArgumentListBuilder args = new ArgumentListBuilder();
  57. args.add("java");
  58. args.addMasked("-version");
  59. Slave s = j.createSlave();
  60. s.toComputer().connect(false).get();
  61. StringWriter out = new StringWriter();
  62. assertEquals(0,s.createLauncher(new StreamTaskListener(out)).launch().cmds(args).join());
  63. System.out.println(out);
  64. assertTrue(out.toString().contains("$ java ********"));
  65. }
  66. @Test
  67. public void ensureArgumentsArePassedViaCmdExeUnmodified() throws Exception {
  68. assumeTrue(Functions.isWindows());
  69. String[] specials = new String[] {
  70. "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")",
  71. "_", "+", "{", "}", "[", "]", ":", ";", "\"", "'", "\\", "|",
  72. "<", ">", ",", ".", "/", "?", " "
  73. };
  74. String out = echoArgs(specials);
  75. String expected = String.format("%n%s", Joiner.on(" ").join(specials));
  76. assertThat(out, containsString(expected));
  77. }
  78. public String echoArgs(String... arguments) throws Exception {
  79. ArgumentListBuilder args = new ArgumentListBuilder(JavaEnvUtils.getJreExecutable("java"), "-cp", "target/test-classes/", "hudson.util.EchoCommand");
  80. args.add(arguments);
  81. args = args.toWindowsCommand();
  82. ByteArrayOutputStream out = new ByteArrayOutputStream();
  83. final StreamTaskListener listener = new StreamTaskListener(out);
  84. Proc p = new LocalLauncher(listener)
  85. .launch()
  86. .stderr(System.err)
  87. .stdout(out)
  88. .cmds(args)
  89. .start()
  90. ;
  91. int code = p.join();
  92. listener.close();
  93. assertThat(code, equalTo(0));
  94. return out.toString();
  95. }
  96. }