You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommitBuilderTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2018, Salesforce.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import static java.nio.charset.StandardCharsets.US_ASCII;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertSame;
  48. import static org.junit.Assert.fail;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.IOException;
  51. import java.text.MessageFormat;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.junit.Test;
  54. public class CommitBuilderTest {
  55. private void assertGpgSignatureStringOutcome(String signature,
  56. String expectedOutcome) throws IOException {
  57. ByteArrayOutputStream out = new ByteArrayOutputStream();
  58. CommitBuilder.writeGpgSignatureString(signature, out);
  59. String formatted_signature = new String(out.toByteArray(), US_ASCII);
  60. assertEquals(expectedOutcome, formatted_signature);
  61. }
  62. @Test
  63. public void writeGpgSignatureString_1() throws Exception {
  64. // @formatter:off
  65. String signature = "-----BEGIN PGP SIGNATURE-----\n" +
  66. "Version: BCPG v1.60\n" +
  67. "\n" +
  68. "iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  69. "opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  70. "gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  71. "uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  72. "3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  73. "IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  74. "=b9OI\n" +
  75. "-----END PGP SIGNATURE-----";
  76. String expectedOutcome = "-----BEGIN PGP SIGNATURE-----\n" +
  77. " Version: BCPG v1.60\n" +
  78. " \n" +
  79. " iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  80. " opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  81. " gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  82. " uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  83. " 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  84. " IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  85. " =b9OI\n" +
  86. " -----END PGP SIGNATURE-----";
  87. // @formatter:on
  88. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  89. }
  90. @Test
  91. public void writeGpgSignatureString_failsForNonAscii() throws Exception {
  92. String signature = "Ü Ä";
  93. try {
  94. CommitBuilder.writeGpgSignatureString(signature,
  95. new ByteArrayOutputStream());
  96. fail("Exception expected");
  97. } catch (IllegalArgumentException e) {
  98. // good
  99. String message = MessageFormat.format(JGitText.get().notASCIIString,
  100. signature);
  101. assertEquals(message, e.getMessage());
  102. }
  103. }
  104. @Test
  105. public void writeGpgSignatureString_oneLineNotModified() throws Exception {
  106. String signature = " A string ";
  107. String expectedOutcome = signature;
  108. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  109. }
  110. @Test
  111. public void writeGpgSignatureString_preservesRandomWhitespace()
  112. throws Exception {
  113. // @formatter:off
  114. String signature = " String with \n"
  115. + "Line 2\n"
  116. + " Line 3\n"
  117. + "Line 4 \n"
  118. + " Line 5 ";
  119. String expectedOutcome = " String with \n"
  120. + " Line 2\n"
  121. + " Line 3\n"
  122. + " Line 4 \n"
  123. + " Line 5 ";
  124. // @formatter:on
  125. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  126. }
  127. @Test
  128. public void writeGpgSignatureString_replaceCR() throws Exception {
  129. // @formatter:off
  130. String signature = "String with \r"
  131. + "Line 2\r"
  132. + "Line 3\r"
  133. + "Line 4\r"
  134. + "Line 5";
  135. String expectedOutcome = "String with \n"
  136. + " Line 2\n"
  137. + " Line 3\n"
  138. + " Line 4\n"
  139. + " Line 5";
  140. // @formatter:on
  141. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  142. }
  143. @Test
  144. public void writeGpgSignatureString_replaceCRLF() throws Exception {
  145. // @formatter:off
  146. String signature = "String with \r\n"
  147. + "Line 2\r\n"
  148. + "Line 3\r\n"
  149. + "Line 4\r\n"
  150. + "Line 5";
  151. String expectedOutcome = "String with \n"
  152. + " Line 2\n"
  153. + " Line 3\n"
  154. + " Line 4\n"
  155. + " Line 5";
  156. // @formatter:on
  157. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  158. }
  159. @Test
  160. public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
  161. // @formatter:off
  162. String signature = "String with \r"
  163. + "Line 2\r\n"
  164. + "Line 3\r"
  165. + "Line 4\r\n"
  166. + "Line 5";
  167. String expectedOutcome = "String with \n"
  168. + " Line 2\n"
  169. + " Line 3\n"
  170. + " Line 4\n"
  171. + " Line 5";
  172. // @formatter:on
  173. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  174. }
  175. @Test
  176. public void setGpgSignature() throws Exception {
  177. GpgSignature dummy = new GpgSignature(new byte[0]);
  178. CommitBuilder builder = new CommitBuilder();
  179. assertNull(builder.getGpgSignature());
  180. builder.setGpgSignature(dummy);
  181. assertSame(dummy, builder.getGpgSignature());
  182. builder.setGpgSignature(null);
  183. assertNull(builder.getGpgSignature());
  184. }
  185. }