View Javadoc
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  
45  import static java.nio.charset.StandardCharsets.US_ASCII;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.assertSame;
49  import static org.junit.Assert.fail;
50  
51  import java.io.ByteArrayOutputStream;
52  import java.io.IOException;
53  import java.text.MessageFormat;
54  
55  import org.eclipse.jgit.internal.JGitText;
56  import org.junit.Test;
57  
58  public class CommitBuilderTest {
59  
60  	private void assertGpgSignatureStringOutcome(String signature,
61  			String expectedOutcome) throws IOException {
62  		ByteArrayOutputStream out = new ByteArrayOutputStream();
63  		CommitBuilder.writeGpgSignatureString(signature, out);
64  		String formatted_signature = new String(out.toByteArray(), US_ASCII);
65  		assertEquals(expectedOutcome, formatted_signature);
66  	}
67  
68  	@Test
69  	public void writeGpgSignatureString_1() throws Exception {
70  		// @formatter:off
71  		String signature = "-----BEGIN PGP SIGNATURE-----\n" +
72  				"Version: BCPG v1.60\n" +
73  				"\n" +
74  				"iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
75  				"opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
76  				"gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
77  				"uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
78  				"3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
79  				"IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
80  				"=b9OI\n" +
81  				"-----END PGP SIGNATURE-----";
82  		String expectedOutcome = "-----BEGIN PGP SIGNATURE-----\n" +
83  				" Version: BCPG v1.60\n" +
84  				" \n" +
85  				" iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
86  				" opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
87  				" gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
88  				" uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
89  				" 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
90  				" IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
91  				" =b9OI\n" +
92  				" -----END PGP SIGNATURE-----";
93  		// @formatter:on
94  		assertGpgSignatureStringOutcome(signature, expectedOutcome);
95  	}
96  
97  	@Test
98  	public void writeGpgSignatureString_failsForNonAscii() throws Exception {
99  		String signature = "Ü Ä";
100 		try {
101 			CommitBuilder.writeGpgSignatureString(signature,
102 					new ByteArrayOutputStream());
103 			fail("Exception expected");
104 		} catch (IllegalArgumentException e) {
105 			// good
106 			String message = MessageFormat.format(JGitText.get().notASCIIString,
107 					signature);
108 			assertEquals(message, e.getMessage());
109 		}
110 	}
111 
112 	@Test
113 	public void writeGpgSignatureString_oneLineNotModified() throws Exception {
114 		String signature = "    A string   ";
115 		String expectedOutcome = signature;
116 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
117 	}
118 
119 	@Test
120 	public void writeGpgSignatureString_preservesRandomWhitespace()
121 			throws Exception {
122 		// @formatter:off
123 		String signature = "    String with    \n"
124 				+ "Line 2\n"
125 				+ " Line 3\n"
126 				+ "Line 4   \n"
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 		// @formatter:on
134 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
135 	}
136 
137 	@Test
138 	public void writeGpgSignatureString_replaceCR() throws Exception {
139 		// @formatter:off
140 		String signature = "String with \r"
141 				+ "Line 2\r"
142 				+ "Line 3\r"
143 				+ "Line 4\r"
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 
154 	@Test
155 	public void writeGpgSignatureString_replaceCRLF() throws Exception {
156 		// @formatter:off
157 		String signature = "String with \r\n"
158 				+ "Line 2\r\n"
159 				+ "Line 3\r\n"
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 		// @formatter:on
168 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
169 	}
170 
171 	@Test
172 	public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
173 		// @formatter:off
174 		String signature = "String with \r"
175 				+ "Line 2\r\n"
176 				+ "Line 3\r"
177 				+ "Line 4\r\n"
178 				+ "Line 5";
179 		String expectedOutcome = "String with \n"
180 				+ " Line 2\n"
181 				+ " Line 3\n"
182 				+ " Line 4\n"
183 				+ " Line 5";
184 		// @formatter:on
185 		assertGpgSignatureStringOutcome(signature, expectedOutcome);
186 	}
187 
188 	@Test
189 	public void setGpgSignature() throws Exception {
190 		GpgSignature dummy = new GpgSignature(new byte[0]);
191 
192 		CommitBuilder builder = new CommitBuilder();
193 		assertNull(builder.getGpgSignature());
194 
195 		builder.setGpgSignature(dummy);
196 		assertSame(dummy, builder.getGpgSignature());
197 
198 		builder.setGpgSignature(null);
199 		assertNull(builder.getGpgSignature());
200 	}
201 }