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