Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2020 Thomas Wolf <thomas.wolf@paranor.ch> 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 java.nio.charset.StandardCharsets.UTF_8;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertThrows;
  15. import org.eclipse.jgit.internal.JGitText;
  16. import org.eclipse.jgit.util.RawParseUtils;
  17. import org.junit.Test;
  18. public class TagBuilderTest {
  19. // @formatter:off
  20. private static final String SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n" +
  21. "Version: BCPG v1.60\n" +
  22. "\n" +
  23. "iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  24. "opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  25. "gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  26. "uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  27. "3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  28. "IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  29. "=b9OI\n" +
  30. "-----END PGP SIGNATURE-----";
  31. // @formatter:on
  32. private static final String TAGGER_LINE = "A U. Thor <a_u_thor@example.com> 1218123387 +0700";
  33. private static final PersonIdent TAGGER = RawParseUtils
  34. .parsePersonIdent(TAGGER_LINE);
  35. @Test
  36. public void testTagSimple() throws Exception {
  37. TagBuilder t = new TagBuilder();
  38. t.setTag("sometag");
  39. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  40. t.setEncoding(US_ASCII);
  41. t.setMessage("Short message only");
  42. t.setTagger(TAGGER);
  43. String tag = new String(t.build(), UTF_8);
  44. String expected = "object 0000000000000000000000000000000000000000\n"
  45. + "type commit\n" //
  46. + "tag sometag\n" //
  47. + "tagger " + TAGGER_LINE + '\n' //
  48. + "encoding US-ASCII\n" //
  49. + '\n' //
  50. + "Short message only";
  51. assertEquals(expected, tag);
  52. }
  53. @Test
  54. public void testTagWithSignatureShortMessageEndsInLF() throws Exception {
  55. TagBuilder t = new TagBuilder();
  56. t.setTag("sometag");
  57. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  58. t.setEncoding(US_ASCII);
  59. t.setMessage("Short message only\n");
  60. t.setTagger(TAGGER);
  61. t.setGpgSignature(new GpgSignature(SIGNATURE.getBytes(US_ASCII)));
  62. String tag = new String(t.build(), UTF_8);
  63. String expected = "object 0000000000000000000000000000000000000000\n"
  64. + "type commit\n" //
  65. + "tag sometag\n" //
  66. + "tagger " + TAGGER_LINE + '\n' //
  67. + "encoding US-ASCII\n" //
  68. + '\n' //
  69. + "Short message only\n" //
  70. + SIGNATURE + '\n';
  71. assertEquals(expected, tag);
  72. }
  73. @Test
  74. public void testTagWithSignatureMessageNoLF() {
  75. TagBuilder t = new TagBuilder();
  76. t.setTag("sometag");
  77. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  78. t.setEncoding(US_ASCII);
  79. t.setMessage("A message\n\nthat does not end in LF");
  80. t.setTagger(TAGGER);
  81. t.setGpgSignature(new GpgSignature(SIGNATURE.getBytes(US_ASCII)));
  82. Throwable ex = assertThrows(Throwable.class, t::build);
  83. assertEquals(JGitText.get().signedTagMessageNoLf, ex.getMessage());
  84. }
  85. @Test
  86. public void testTagWithSignatureNoParagraphsMessage() throws Exception {
  87. TagBuilder t = new TagBuilder();
  88. t.setTag("sometag");
  89. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  90. t.setEncoding(US_ASCII);
  91. t.setMessage("A strange\ntag message\n");
  92. t.setTagger(TAGGER);
  93. t.setGpgSignature(new GpgSignature(SIGNATURE.getBytes(US_ASCII)));
  94. String tag = new String(t.build(), UTF_8);
  95. String expected = "object 0000000000000000000000000000000000000000\n"
  96. + "type commit\n" //
  97. + "tag sometag\n" //
  98. + "tagger " + TAGGER_LINE + '\n' //
  99. + "encoding US-ASCII\n" //
  100. + '\n' //
  101. + "A strange\ntag message\n" //
  102. + SIGNATURE + '\n';
  103. assertEquals(expected, tag);
  104. }
  105. @Test
  106. public void testTagWithSignatureLongMessage() throws Exception {
  107. TagBuilder t = new TagBuilder();
  108. t.setTag("sometag");
  109. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  110. t.setMessage("Short message\n\nFollowed by explanations.\n");
  111. t.setTagger(TAGGER);
  112. t.setGpgSignature(new GpgSignature(SIGNATURE.getBytes(US_ASCII)));
  113. String tag = new String(t.build(), UTF_8);
  114. String expected = "object 0000000000000000000000000000000000000000\n"
  115. + "type commit\n" //
  116. + "tag sometag\n" //
  117. + "tagger " + TAGGER_LINE + '\n' //
  118. + '\n' //
  119. + "Short message\n\nFollowed by explanations.\n" //
  120. + SIGNATURE + '\n';
  121. assertEquals(expected, tag);
  122. }
  123. @Test
  124. public void testTagWithSignatureEmptyMessage() throws Exception {
  125. TagBuilder t = new TagBuilder();
  126. t.setTag("sometag");
  127. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  128. t.setTagger(TAGGER);
  129. t.setMessage("");
  130. String emptyMsg = new String(t.build(), UTF_8);
  131. t.setGpgSignature(new GpgSignature(SIGNATURE.getBytes(US_ASCII)));
  132. String tag = new String(t.build(), UTF_8);
  133. String expected = "object 0000000000000000000000000000000000000000\n"
  134. + "type commit\n" //
  135. + "tag sometag\n" //
  136. + "tagger " + TAGGER_LINE + '\n' //
  137. + '\n';
  138. assertEquals(expected, emptyMsg);
  139. assertEquals(expected + SIGNATURE + '\n', tag);
  140. }
  141. @Test
  142. public void testTagWithSignatureOnly() throws Exception {
  143. TagBuilder t = new TagBuilder();
  144. t.setTag("sometag");
  145. t.setObjectId(ObjectId.zeroId(), Constants.OBJ_COMMIT);
  146. t.setTagger(TAGGER);
  147. String emptyMsg = new String(t.build(), UTF_8);
  148. t.setGpgSignature(new GpgSignature(SIGNATURE.getBytes(US_ASCII)));
  149. String tag = new String(t.build(), UTF_8);
  150. String expected = "object 0000000000000000000000000000000000000000\n"
  151. + "type commit\n" //
  152. + "tag sometag\n" //
  153. + "tagger " + TAGGER_LINE + '\n' //
  154. + '\n';
  155. assertEquals(expected, emptyMsg);
  156. assertEquals(expected + SIGNATURE + '\n', tag);
  157. }
  158. }