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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2018, Salesforce. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lib;
  11. import static java.nio.charset.StandardCharsets.US_ASCII;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertSame;
  15. import static org.junit.Assert.assertThrows;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import org.eclipse.jgit.internal.JGitText;
  20. import org.junit.Test;
  21. public class CommitBuilderTest {
  22. // @formatter:off
  23. private static final String SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n" +
  24. "Version: BCPG v1.60\n" +
  25. "\n" +
  26. "iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  27. "opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  28. "gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  29. "uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  30. "3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  31. "IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  32. "=b9OI\n" +
  33. "-----END PGP SIGNATURE-----";
  34. private static final String EXPECTED = "-----BEGIN PGP SIGNATURE-----\n" +
  35. " Version: BCPG v1.60\n" +
  36. " \n" +
  37. " iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  38. " opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  39. " gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  40. " uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  41. " 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  42. " IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  43. " =b9OI\n" +
  44. " -----END PGP SIGNATURE-----";
  45. // @formatter:on
  46. private void assertGpgSignatureStringOutcome(String signature,
  47. String expectedOutcome) throws IOException {
  48. ByteArrayOutputStream out = new ByteArrayOutputStream();
  49. CommitBuilder.writeGpgSignatureString(signature, out);
  50. String formatted_signature = new String(out.toByteArray(), US_ASCII);
  51. assertEquals(expectedOutcome, formatted_signature);
  52. }
  53. @Test
  54. public void writeGpgSignatureString() throws Exception {
  55. assertGpgSignatureStringOutcome(SIGNATURE, EXPECTED);
  56. }
  57. @Test
  58. public void writeGpgSignatureStringTrailingLF() throws Exception {
  59. assertGpgSignatureStringOutcome(SIGNATURE + '\n', EXPECTED);
  60. }
  61. @Test
  62. public void writeGpgSignatureStringCRLF() throws Exception {
  63. assertGpgSignatureStringOutcome(SIGNATURE.replaceAll("\n", "\r\n"),
  64. EXPECTED);
  65. }
  66. @Test
  67. public void writeGpgSignatureStringTrailingCRLF() throws Exception {
  68. assertGpgSignatureStringOutcome(
  69. SIGNATURE.replaceAll("\n", "\r\n") + "\r\n", EXPECTED);
  70. }
  71. @Test
  72. public void writeGpgSignatureString_failsForNonAscii() throws Exception {
  73. String signature = "Ü Ä";
  74. IllegalArgumentException e = assertThrows(
  75. IllegalArgumentException.class,
  76. () -> CommitBuilder.writeGpgSignatureString(signature,
  77. new ByteArrayOutputStream()));
  78. String message = MessageFormat.format(JGitText.get().notASCIIString,
  79. signature);
  80. assertEquals(message, e.getMessage());
  81. }
  82. @Test
  83. public void writeGpgSignatureString_oneLineNotModified() throws Exception {
  84. String signature = " A string ";
  85. String expectedOutcome = signature;
  86. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  87. }
  88. @Test
  89. public void writeGpgSignatureString_preservesRandomWhitespace()
  90. throws Exception {
  91. // @formatter:off
  92. String signature = " String with \n"
  93. + "Line 2\n"
  94. + " Line 3\n"
  95. + "Line 4 \n"
  96. + " Line 5 ";
  97. String expectedOutcome = " String with \n"
  98. + " Line 2\n"
  99. + " Line 3\n"
  100. + " Line 4 \n"
  101. + " Line 5 ";
  102. // @formatter:on
  103. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  104. }
  105. @Test
  106. public void writeGpgSignatureString_replaceCR() throws Exception {
  107. // @formatter:off
  108. String signature = "String with \r"
  109. + "Line 2\r"
  110. + "Line 3\r"
  111. + "Line 4\r"
  112. + "Line 5";
  113. String expectedOutcome = "String with \n"
  114. + " Line 2\n"
  115. + " Line 3\n"
  116. + " Line 4\n"
  117. + " Line 5";
  118. // @formatter:on
  119. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  120. }
  121. @Test
  122. public void writeGpgSignatureString_replaceCRLF() throws Exception {
  123. // @formatter:off
  124. String signature = "String with \r\n"
  125. + "Line 2\r\n"
  126. + "Line 3\r\n"
  127. + "Line 4\r\n"
  128. + "Line 5";
  129. String expectedOutcome = "String with \n"
  130. + " Line 2\n"
  131. + " Line 3\n"
  132. + " Line 4\n"
  133. + " Line 5";
  134. // @formatter:on
  135. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  136. }
  137. @Test
  138. public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
  139. // @formatter:off
  140. String signature = "String with \r"
  141. + "Line 2\r\n"
  142. + "Line 3\r"
  143. + "Line 4\r\n"
  144. + "Line 5";
  145. String expectedOutcome = "String with \n"
  146. + " Line 2\n"
  147. + " Line 3\n"
  148. + " Line 4\n"
  149. + " Line 5";
  150. // @formatter:on
  151. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  152. }
  153. @Test
  154. public void setGpgSignature() throws Exception {
  155. GpgSignature dummy = new GpgSignature(new byte[0]);
  156. CommitBuilder builder = new CommitBuilder();
  157. assertNull(builder.getGpgSignature());
  158. builder.setGpgSignature(dummy);
  159. assertSame(dummy, builder.getGpgSignature());
  160. builder.setGpgSignature(null);
  161. assertNull(builder.getGpgSignature());
  162. }
  163. }