View Javadoc
1   /*
2    * Copyright (C) 2011, Leonard Broman <leonard.broman@gmail.com> 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.util;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.fail;
15  
16  import java.nio.charset.Charset;
17  import java.nio.charset.UnsupportedCharsetException;
18  
19  import org.eclipse.jgit.lib.Constants;
20  import org.junit.Test;
21  
22  import static java.nio.charset.StandardCharsets.UTF_8;
23  
24  public class RawParseUtilsTest {
25  	String commit = "tree e3a1035abd2b319bb01e57d69b0ba6cab289297e\n" +
26  		"parent 54e895b87c0768d2317a2b17062e3ad9f76a8105\n" +
27  		"committer A U Thor <author@xample.com 1528968566 +0200\n" +
28  		"gpgsig -----BEGIN PGP SIGNATURE-----\n" +
29  		" \n" +
30  		" wsBcBAABCAAQBQJbGB4pCRBK7hj4Ov3rIwAAdHIIAENrvz23867ZgqrmyPemBEZP\n" +
31  		" U24B1Tlq/DWvce2buaxmbNQngKZ0pv2s8VMc11916WfTIC9EKvioatmpjduWvhqj\n" +
32  		" znQTFyiMor30pyYsfrqFuQZvqBW01o8GEWqLg8zjf9Rf0R3LlOEw86aT8CdHRlm6\n" +
33  		" wlb22xb8qoX4RB+LYfz7MhK5F+yLOPXZdJnAVbuyoMGRnDpwdzjL5Hj671+XJxN5\n" +
34  		" SasRdhxkkfw/ZnHxaKEc4juMz8Nziz27elRwhOQqlTYoXNJnsV//wy5Losd7aKi1\n" +
35  		" xXXyUpndEOmT0CIcKHrN/kbYoVL28OJaxoBuva3WYQaRrzEe3X02NMxZe9gkSqA=\n" +
36  		" =TClh\n" +
37  		" -----END PGP SIGNATURE-----\n" +
38  		"some other header\n\n" +
39  		"commit message";
40  
41  	@Test
42  	public void testParseEncoding_ISO8859_1_encoding() {
43  		Charset result = RawParseUtils.parseEncoding(Constants
44  				.encodeASCII("encoding ISO-8859-1\n"));
45  		assertNotNull(result);
46  	}
47  
48  	@Test
49  	public void testParseEncoding_Accept_Latin_One_AsISO8859_1() {
50  		Charset result = RawParseUtils.parseEncoding(Constants
51  				.encodeASCII("encoding latin-1\n"));
52  		assertNotNull(result);
53  		assertEquals("ISO-8859-1", result.name());
54  	}
55  
56  	@Test
57  	public void testParseEncoding_badEncoding() {
58  		try {
59  			RawParseUtils.parseEncoding(Constants.encodeASCII("encoding xyz\n"));
60  			fail("should throw an UnsupportedCharsetException: xyz");
61  		} catch (UnsupportedCharsetException e) {
62  			assertEquals("xyz", e.getMessage());
63  		}
64  	}
65  
66  	@Test
67  	public void testHeaderStart() {
68  		byte[] headerName = "some".getBytes(UTF_8);
69  		byte[] commitBytes = commit.getBytes(UTF_8);
70  		assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 0));
71  		assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 4));
72  
73  		byte[] missingHeaderName = "missing".getBytes(UTF_8);
74  		assertEquals(-1, RawParseUtils.headerStart(missingHeaderName,
75  							   commitBytes, 0));
76  
77  		byte[] fauxHeaderName = "other".getBytes(UTF_8);
78  		assertEquals(-1, RawParseUtils.headerStart(fauxHeaderName, commitBytes, 625 + 4));
79  	}
80  
81  	@Test
82  	public void testHeaderEnd() {
83  		byte[] commitBytes = commit.getBytes(UTF_8);
84  		int[] expected = new int[] {45, 93, 148, 619, 637};
85  		int start = 0;
86  		for (int i = 0; i < expected.length; i++) {
87  			start = RawParseUtils.headerEnd(commitBytes, start);
88  			assertEquals(expected[i], start);
89  			start += 1;
90  		}
91  	}
92  }