PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/hudson/util/ArgumentListBuilderTest.java

https://gitlab.com/kalyanjk29/jenkins
Java | 186 lines | 136 code | 25 blank | 25 comment | 2 complexity | bdbfa1e92b850084861d29bda0a99392 MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc.
  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 java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import org.junit.Assert;
  30. import org.junit.Test;
  31. public class ArgumentListBuilderTest extends Assert {
  32. public static void assertArrayEquals(String msg, boolean[] expected, boolean[] actual) {
  33. assertArrayEquals(msg,box(expected),box(actual));
  34. }
  35. private static Boolean[] box(boolean[] a) {
  36. if(a==null) return null;
  37. Boolean[] r = new Boolean[a.length];
  38. for (int i = 0; i < a.length; i++)
  39. r[i] = a[i];
  40. return r;
  41. }
  42. @Test
  43. public void assertEmptyMask() {
  44. ArgumentListBuilder builder = new ArgumentListBuilder();
  45. builder.add("arg");
  46. builder.add("other", "arguments");
  47. assertFalse("There shouldnt be any masked arguments", builder.hasMaskedArguments());
  48. boolean[] array = builder.toMaskArray();
  49. assertNotNull("The mask array should not be null", array);
  50. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,false}, array);
  51. }
  52. @Test
  53. public void assertLastArgumentIsMasked() {
  54. ArgumentListBuilder builder = new ArgumentListBuilder();
  55. builder.add("arg");
  56. builder.addMasked("ismasked");
  57. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  58. boolean[] array = builder.toMaskArray();
  59. assertNotNull("The mask array should not be null", array);
  60. assertArrayEquals("The mask array was incorrect", new boolean[]{false,true}, array);
  61. }
  62. @Test
  63. public void assertSeveralMaskedArguments() {
  64. ArgumentListBuilder builder = new ArgumentListBuilder();
  65. builder.add("arg");
  66. builder.addMasked("ismasked");
  67. builder.add("non masked arg");
  68. builder.addMasked("ismasked2");
  69. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  70. boolean[] array = builder.toMaskArray();
  71. assertNotNull("The mask array should not be null", array);
  72. assertArrayEquals("The mask array was incorrect", new boolean[]{false,true, false, true}, array);
  73. }
  74. @Test
  75. public void assertPrependAfterAddingMasked() {
  76. ArgumentListBuilder builder = new ArgumentListBuilder();
  77. builder.addMasked("ismasked");
  78. builder.add("arg");
  79. builder.prepend("first", "second");
  80. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  81. boolean[] array = builder.toMaskArray();
  82. assertNotNull("The mask array should not be null", array);
  83. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,true,false}, array);
  84. }
  85. @Test
  86. public void assertPrependBeforeAddingMasked() {
  87. ArgumentListBuilder builder = new ArgumentListBuilder();
  88. builder.prepend("first", "second");
  89. builder.addMasked("ismasked");
  90. builder.add("arg");
  91. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  92. boolean[] array = builder.toMaskArray();
  93. assertNotNull("The mask array should not be null", array);
  94. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,true,false}, array);
  95. }
  96. @Test
  97. public void testToWindowsCommand() {
  98. ArgumentListBuilder builder = new ArgumentListBuilder(
  99. "ant.bat", "-Dfoo1=abc", // nothing special, no quotes
  100. "-Dfoo2=foo bar", "-Dfoo3=/u*r", "-Dfoo4=/us?", // add quotes
  101. "-Dfoo10=bar,baz",
  102. "-Dfoo5=foo;bar^baz", "-Dfoo6=<xml>&here;</xml>", // add quotes
  103. "-Dfoo7=foo|bar\"baz", // add quotes and "" for "
  104. "-Dfoo8=% %QED% %comspec% %-%(%.%", // add quotes, and extra quotes for %Q and %c
  105. "-Dfoo9=%'''%%@%"); // no quotes as none of the % are followed by a letter
  106. // By default, does not escape %VAR%
  107. assertArrayEquals(new String[] { "cmd.exe", "/C",
  108. "\"ant.bat -Dfoo1=abc \"-Dfoo2=foo bar\""
  109. + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo10=bar,baz\" \"-Dfoo5=foo;bar^baz\""
  110. + " \"-Dfoo6=<xml>&here;</xml>\" \"-Dfoo7=foo|bar\"\"baz\""
  111. + " \"-Dfoo8=% %QED% %comspec% %-%(%.%\""
  112. + " -Dfoo9=%'''%%@% && exit %%ERRORLEVEL%%\"" },
  113. builder.toWindowsCommand().toCommandArray());
  114. // Pass flag to escape %VAR%
  115. assertArrayEquals(new String[] { "cmd.exe", "/C",
  116. "\"ant.bat -Dfoo1=abc \"-Dfoo2=foo bar\""
  117. + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo10=bar,baz\" \"-Dfoo5=foo;bar^baz\""
  118. + " \"-Dfoo6=<xml>&here;</xml>\" \"-Dfoo7=foo|bar\"\"baz\""
  119. + " \"-Dfoo8=% %\"Q\"ED% %\"c\"omspec% %-%(%.%\""
  120. + " -Dfoo9=%'''%%@% && exit %%ERRORLEVEL%%\"" },
  121. builder.toWindowsCommand(true).toCommandArray());
  122. }
  123. @Test
  124. public void assertMaskOnClone() {
  125. ArgumentListBuilder builder = new ArgumentListBuilder();
  126. builder.add("arg1");
  127. builder.addMasked("masked1");
  128. builder.add("arg2");
  129. ArgumentListBuilder clone = builder.clone();
  130. assertTrue("There should be masked arguments", clone.hasMaskedArguments());
  131. boolean[] array = clone.toMaskArray();
  132. assertNotNull("The mask array should not be null", array);
  133. assertArrayEquals("The mask array was incorrect", builder.toMaskArray(), array);
  134. }
  135. private static final Map<String, String> KEY_VALUES = new HashMap<String, String>() {{
  136. put("key1", "value1");
  137. put("key2", "value2");
  138. put("key3", "value3");
  139. }};
  140. private static final Set<String> MASKS = new HashSet<String>() {{
  141. add("key2");
  142. }};
  143. @Test
  144. public void assertKeyValuePairsWithMask() {
  145. ArgumentListBuilder builder = new ArgumentListBuilder();
  146. builder.addKeyValuePairs(null, KEY_VALUES, MASKS);
  147. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  148. boolean[] array = builder.toMaskArray();
  149. assertNotNull("The mask array should not be null", array);
  150. assertArrayEquals("The mask array was incorrect", new boolean[]{false,true,false}, array);
  151. }
  152. @Test
  153. public void assertKeyValuePairs() {
  154. ArgumentListBuilder builder = new ArgumentListBuilder();
  155. builder.addKeyValuePairs(null, KEY_VALUES);
  156. assertFalse("There shouldnt be any masked arguments", builder.hasMaskedArguments());
  157. boolean[] array = builder.toMaskArray();
  158. assertNotNull("The mask array should not be null", array);
  159. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,false}, array);
  160. }
  161. }