View Javadoc
1   /*
2    * Copyright (C) 2011, Leonard Broman <leonard.broman@gmail.com>
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.util;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.fail;
48  
49  import java.nio.charset.Charset;
50  import java.nio.charset.UnsupportedCharsetException;
51  
52  import org.eclipse.jgit.lib.Constants;
53  import org.junit.Test;
54  
55  import static java.nio.charset.StandardCharsets.UTF_8;
56  
57  public class RawParseUtilsTest {
58  	String commit = "tree e3a1035abd2b319bb01e57d69b0ba6cab289297e\n" +
59  		"parent 54e895b87c0768d2317a2b17062e3ad9f76a8105\n" +
60  		"committer A U Thor <author@xample.com 1528968566 +0200\n" +
61  		"gpgsig -----BEGIN PGP SIGNATURE-----\n" +
62  		" \n" +
63  		" wsBcBAABCAAQBQJbGB4pCRBK7hj4Ov3rIwAAdHIIAENrvz23867ZgqrmyPemBEZP\n" +
64  		" U24B1Tlq/DWvce2buaxmbNQngKZ0pv2s8VMc11916WfTIC9EKvioatmpjduWvhqj\n" +
65  		" znQTFyiMor30pyYsfrqFuQZvqBW01o8GEWqLg8zjf9Rf0R3LlOEw86aT8CdHRlm6\n" +
66  		" wlb22xb8qoX4RB+LYfz7MhK5F+yLOPXZdJnAVbuyoMGRnDpwdzjL5Hj671+XJxN5\n" +
67  		" SasRdhxkkfw/ZnHxaKEc4juMz8Nziz27elRwhOQqlTYoXNJnsV//wy5Losd7aKi1\n" +
68  		" xXXyUpndEOmT0CIcKHrN/kbYoVL28OJaxoBuva3WYQaRrzEe3X02NMxZe9gkSqA=\n" +
69  		" =TClh\n" +
70  		" -----END PGP SIGNATURE-----\n" +
71  		"some other header\n\n" +
72  		"commit message";
73  
74  	@Test
75  	public void testParseEncoding_ISO8859_1_encoding() {
76  		Charset result = RawParseUtils.parseEncoding(Constants
77  				.encodeASCII("encoding ISO-8859-1\n"));
78  		assertNotNull(result);
79  	}
80  
81  	@Test
82  	public void testParseEncoding_Accept_Latin_One_AsISO8859_1() {
83  		Charset result = RawParseUtils.parseEncoding(Constants
84  				.encodeASCII("encoding latin-1\n"));
85  		assertNotNull(result);
86  		assertEquals("ISO-8859-1", result.name());
87  	}
88  
89  	@Test
90  	public void testParseEncoding_badEncoding() {
91  		try {
92  			RawParseUtils.parseEncoding(Constants.encodeASCII("encoding xyz\n"));
93  			fail("should throw an UnsupportedCharsetException: xyz");
94  		} catch (UnsupportedCharsetException e) {
95  			assertEquals("xyz", e.getMessage());
96  		}
97  	}
98  
99  	@Test
100 	public void testHeaderStart() {
101 		byte[] headerName = "some".getBytes(UTF_8);
102 		byte[] commitBytes = commit.getBytes(UTF_8);
103 		assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 0));
104 		assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 4));
105 
106 		byte[] missingHeaderName = "missing".getBytes(UTF_8);
107 		assertEquals(-1, RawParseUtils.headerStart(missingHeaderName,
108 							   commitBytes, 0));
109 
110 		byte[] fauxHeaderName = "other".getBytes(UTF_8);
111 		assertEquals(-1, RawParseUtils.headerStart(fauxHeaderName, commitBytes, 625 + 4));
112 	}
113 
114 	@Test
115 	public void testHeaderEnd() {
116 		byte[] commitBytes = commit.getBytes(UTF_8);
117 		int[] expected = new int[] {45, 93, 148, 619, 637};
118 		int start = 0;
119 		for (int i = 0; i < expected.length; i++) {
120 			start = RawParseUtils.headerEnd(commitBytes, start);
121 			assertEquals(expected[i], start);
122 			start += 1;
123 		}
124 	}
125 }