1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 }