View Javadoc
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  
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.fail;
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  	private void assertGpgSignatureStringOutcome(String signature,
28  			String expectedOutcome) throws IOException {
29  		ByteArrayOutputStream out = new ByteArrayOutputStream();
30  		CommitBuilder.writeGpgSignatureString(signature, out);
31  		String formatted_signature = new String(out.toByteArray(), US_ASCII);
32  		assertEquals(expectedOutcome, formatted_signature);
33  	}
34  
35  	@Test
36  	public void writeGpgSignatureString_1() throws Exception {
37  		// @formatter:off
38  		String signature = "-----BEGIN PGP SIGNATURE-----\n" +
39  				"Version: BCPG v1.60\n" +
40  				"\n" +
41  				"iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
42  				"opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
43  				"gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
44  				"uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
45  				"3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
46  				"IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
47  				"=b9OI\n" +
48  				"-----END PGP SIGNATURE-----";
49  		String expectedOutcome = "-----BEGIN PGP SIGNATURE-----\n" +
50  				" Version: BCPG v1.60\n" +
51  				" \n" +
52  				" iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
53  				" opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
54  				" gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
55  				" uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
56  				" 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
57  				" IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
58  				" =b9OI\n" +
59  				" -----END PGP SIGNATURE-----";
60  		// @formatter:on
61  		assertGpgSignatureStringOutcome(signature, expectedOutcome);
62  	}
63  
64  	@Test
65  	public void writeGpgSignatureString_failsForNonAscii() throws Exception {
66  		String signature = "Ü Ä";
67  		try {
68  			CommitBuilder.writeGpgSignatureString(signature,
69  					new ByteArrayOutputStream());
70  			fail("Exception expected");
71  		} catch (IllegalArgumentException e) {
72  			// good
73  			String message = MessageFormat.format(JGitText.get().notASCIIString,
74  					signature);
75  			assertEquals(message, e.getMessage());
76  		}
77  	}
78  
79  	@Test
80  	public void writeGpgSignatureString_oneLineNotModified() throws Exception {
81  		String signature = "    A string   ";
82  		String expectedOutcome = signature;
83  		assertGpgSignatureStringOutcome(signature, expectedOutcome);
84  	}
85  
86  	@Test
87  	public void writeGpgSignatureString_preservesRandomWhitespace()
88  			throws Exception {
89  		// @formatter:off
90  		String signature = "    String with    \n"
91  				+ "Line 2\n"
92  				+ " Line 3\n"
93  				+ "Line 4   \n"
94  				+ "  Line 5  ";
95  		String expectedOutcome = "    String with    \n"
96  				+ " Line 2\n"
97  				+ "  Line 3\n"
98  				+ " Line 4   \n"
99  				+ "   Line 5  ";
100 		// @formatter:on
101 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
102 	}
103 
104 	@Test
105 	public void writeGpgSignatureString_replaceCR() throws Exception {
106 		// @formatter:off
107 		String signature = "String with \r"
108 				+ "Line 2\r"
109 				+ "Line 3\r"
110 				+ "Line 4\r"
111 				+ "Line 5";
112 		String expectedOutcome = "String with \n"
113 				+ " Line 2\n"
114 				+ " Line 3\n"
115 				+ " Line 4\n"
116 				+ " Line 5";
117 		// @formatter:on
118 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
119 	}
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 
138 	@Test
139 	public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
140 		// @formatter:off
141 		String signature = "String with \r"
142 				+ "Line 2\r\n"
143 				+ "Line 3\r"
144 				+ "Line 4\r\n"
145 				+ "Line 5";
146 		String expectedOutcome = "String with \n"
147 				+ " Line 2\n"
148 				+ " Line 3\n"
149 				+ " Line 4\n"
150 				+ " Line 5";
151 		// @formatter:on
152 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
153 	}
154 
155 	@Test
156 	public void setGpgSignature() throws Exception {
157 		GpgSignature dummy = new GpgSignature(new byte[0]);
158 
159 		CommitBuilder builder = new CommitBuilder();
160 		assertNull(builder.getGpgSignature());
161 
162 		builder.setGpgSignature(dummy);
163 		assertSame(dummy, builder.getGpgSignature());
164 
165 		builder.setGpgSignature(null);
166 		assertNull(builder.getGpgSignature());
167 	}
168 }