1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lib;
11
12 import static java.nio.charset.StandardCharsets.US_ASCII;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertThrows;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.text.MessageFormat;
21
22 import org.eclipse.jgit.internal.JGitText;
23 import org.junit.Test;
24
25 public class CommitBuilderTest {
26
27
28 private static final String SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n" +
29 "Version: BCPG v1.60\n" +
30 "\n" +
31 "iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
32 "opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
33 "gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
34 "uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
35 "3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
36 "IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
37 "=b9OI\n" +
38 "-----END PGP SIGNATURE-----";
39
40 private static final String EXPECTED = "-----BEGIN PGP SIGNATURE-----\n" +
41 " Version: BCPG v1.60\n" +
42 " \n" +
43 " iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
44 " opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
45 " gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
46 " uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
47 " 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
48 " IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
49 " =b9OI\n" +
50 " -----END PGP SIGNATURE-----";
51
52
53 private void assertGpgSignatureStringOutcome(String signature,
54 String expectedOutcome) throws IOException {
55 ByteArrayOutputStream out = new ByteArrayOutputStream();
56 ObjectBuilder.writeMultiLineHeader(signature, out, true);
57 String formatted_signature = new String(out.toByteArray(), US_ASCII);
58 assertEquals(expectedOutcome, formatted_signature);
59 }
60
61 @Test
62 public void writeGpgSignatureString() throws Exception {
63 assertGpgSignatureStringOutcome(SIGNATURE, EXPECTED);
64 }
65
66 @Test
67 public void writeGpgSignatureStringTrailingLF() throws Exception {
68 assertGpgSignatureStringOutcome(SIGNATURE + '\n', EXPECTED);
69 }
70
71 @Test
72 public void writeGpgSignatureStringCRLF() throws Exception {
73 assertGpgSignatureStringOutcome(SIGNATURE.replaceAll("\n", "\r\n"),
74 EXPECTED);
75 }
76
77 @Test
78 public void writeGpgSignatureStringTrailingCRLF() throws Exception {
79 assertGpgSignatureStringOutcome(
80 SIGNATURE.replaceAll("\n", "\r\n") + "\r\n", EXPECTED);
81 }
82
83 @Test
84 public void writeGpgSignatureString_failsForNonAscii() throws Exception {
85 String signature = "Ü Ä";
86 IllegalArgumentException e = assertThrows(
87 IllegalArgumentException.class,
88 () -> ObjectBuilder.writeMultiLineHeader(signature,
89 new ByteArrayOutputStream(), true));
90 String message = MessageFormat.format(JGitText.get().notASCIIString,
91 signature);
92 assertEquals(message, e.getMessage());
93 }
94
95 @Test
96 public void writeGpgSignatureString_oneLineNotModified() throws Exception {
97 String signature = " A string ";
98 String expectedOutcome = signature;
99 assertGpgSignatureStringOutcome(signature, expectedOutcome);
100 }
101
102 @Test
103 public void writeGpgSignatureString_preservesRandomWhitespace()
104 throws Exception {
105
106 String signature = " String with \n"
107 + "Line 2\n"
108 + " Line 3\n"
109 + "Line 4 \n"
110 + " Line 5 ";
111 String expectedOutcome = " String with \n"
112 + " Line 2\n"
113 + " Line 3\n"
114 + " Line 4 \n"
115 + " Line 5 ";
116
117 assertGpgSignatureStringOutcome(signature, expectedOutcome);
118 }
119
120 @Test
121 public void writeGpgSignatureString_replaceCR() throws Exception {
122
123 String signature = "String with \r"
124 + "Line 2\r"
125 + "Line 3\r"
126 + "Line 4\r"
127 + "Line 5";
128 String expectedOutcome = "String with \n"
129 + " Line 2\n"
130 + " Line 3\n"
131 + " Line 4\n"
132 + " Line 5";
133
134 assertGpgSignatureStringOutcome(signature, expectedOutcome);
135 }
136
137 @Test
138 public void writeGpgSignatureString_replaceCRLF() throws Exception {
139
140 String signature = "String with \r\n"
141 + "Line 2\r\n"
142 + "Line 3\r\n"
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
151 assertGpgSignatureStringOutcome(signature, expectedOutcome);
152 }
153
154 @Test
155 public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
156
157 String signature = "String with \r"
158 + "Line 2\r\n"
159 + "Line 3\r"
160 + "Line 4\r\n"
161 + "Line 5";
162 String expectedOutcome = "String with \n"
163 + " Line 2\n"
164 + " Line 3\n"
165 + " Line 4\n"
166 + " Line 5";
167
168 assertGpgSignatureStringOutcome(signature, expectedOutcome);
169 }
170
171 @Test
172 public void setGpgSignature() throws Exception {
173 GpgSignature dummy = new GpgSignature(new byte[0]);
174
175 CommitBuilder builder = new CommitBuilder();
176 assertNull(builder.getGpgSignature());
177
178 builder.setGpgSignature(dummy);
179 assertSame(dummy, builder.getGpgSignature());
180
181 builder.setGpgSignature(null);
182 assertNull(builder.getGpgSignature());
183 }
184 }