aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java
blob: cee023d5a95efe45b7ddc1de1f889ca3f9e9a2b9 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2015, Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.transport;

import static org.eclipse.jgit.transport.PushCertificateIdent.parse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Date;
import java.util.TimeZone;

import org.eclipse.jgit.lib.PersonIdent;
import org.junit.Test;

public class PushCertificateIdentTest {
	@Test
	public void parseValid() throws Exception {
		String raw = "A U. Thor <a_u_thor@example.com> 1218123387 +0700";
		PushCertificateIdent ident = parse(raw);
		assertEquals(raw, ident.getRaw());
		assertEquals("A U. Thor <a_u_thor@example.com>", ident.getUserId());
		assertEquals("A U. Thor", ident.getName());
		assertEquals("a_u_thor@example.com", ident.getEmailAddress());
		assertEquals(1218123387000L, ident.getWhen().getTime());
		assertEquals(TimeZone.getTimeZone("GMT+0700"), ident.getTimeZone());
		assertEquals(7 * 60, ident.getTimeZoneOffset());
	}

	@Test
	public void trimName() throws Exception {
		String name = "A U. Thor";
		String email = "a_u_thor@example.com";
		String rest = "<a_u_thor@example.com> 1218123387 +0700";

		checkNameEmail(name, email, name + rest);
		checkNameEmail(name, email, " " + name + rest);
		checkNameEmail(name, email, "  " + name + rest);
		checkNameEmail(name, email, name + " " + rest);
		checkNameEmail(name, email, name + "  " + rest);
		checkNameEmail(name, email, " " + name + " " + rest);
	}

	@Test
	public void noEmail() throws Exception {
		String name = "A U. Thor";
		String rest = " 1218123387 +0700";

		checkNameEmail(name, null, name + rest);
		checkNameEmail(name, null, " " + name + rest);
		checkNameEmail(name, null, "  " + name + rest);
		checkNameEmail(name, null, name + " " + rest);
		checkNameEmail(name, null, name + "  " + rest);
		checkNameEmail(name, null, " " + name + " " + rest);
	}

	@Test
	public void exoticUserId() throws Exception {
		String rest = " 218123387 +0700";
		assertEquals("", parse(rest).getUserId());

		String id = "foo\n\0bar\uabcd\n ";
		assertEquals(id, parse(id + rest).getUserId());
	}

	@Test
	public void fuzzyCasesMatchPersonIdent() throws Exception {
		// See RawParseUtils_ParsePersonIdentTest#testParsePersonIdent_fuzzyCases()
		Date when = new Date(1234567890000L);
		TimeZone tz = TimeZone.getTimeZone("GMT-7");

		assertMatchesPersonIdent(
				"A U Thor <author@example.com>,  C O. Miter <comiter@example.com> 1234567890 -0700",
				new PersonIdent("A U Thor", "author@example.com", when, tz),
				"A U Thor <author@example.com>,  C O. Miter <comiter@example.com>");
		assertMatchesPersonIdent(
				"A U Thor <author@example.com> and others 1234567890 -0700",
				new PersonIdent("A U Thor", "author@example.com", when, tz),
				"A U Thor <author@example.com> and others");
	}

	@Test
	public void incompleteCasesMatchPersonIdent() throws Exception {
		// See RawParseUtils_ParsePersonIdentTest#testParsePersonIdent_incompleteCases()
		Date when = new Date(1234567890000L);
		TimeZone tz = TimeZone.getTimeZone("GMT-7");

		assertMatchesPersonIdent(
				"Me <> 1234567890 -0700",
				new PersonIdent("Me", "", when, tz),
				"Me <>");
		assertMatchesPersonIdent(
				" <me@example.com> 1234567890 -0700",
				new PersonIdent("", "me@example.com", when, tz),
				" <me@example.com>");
		assertMatchesPersonIdent(
				" <> 1234567890 -0700",
				new PersonIdent("", "", when, tz),
				" <>");
		assertMatchesPersonIdent(
				"<>",
				new PersonIdent("", "", 0, 0),
				"<>");
		assertMatchesPersonIdent(
				" <>",
				new PersonIdent("", "", 0, 0),
				" <>");
		assertMatchesPersonIdent(
				"<me@example.com>",
				new PersonIdent("", "me@example.com", 0, 0),
				"<me@example.com>");
		assertMatchesPersonIdent(
				" <me@example.com>",
				new PersonIdent("", "me@example.com", 0, 0),
				" <me@example.com>");
		assertMatchesPersonIdent(
				"Me <>",
				new PersonIdent("Me", "", 0, 0),
				"Me <>");
		assertMatchesPersonIdent(
				"Me <me@example.com>",
				new PersonIdent("Me", "me@example.com", 0, 0),
				"Me <me@example.com>");
		assertMatchesPersonIdent(
				"Me <me@example.com> 1234567890",
				new PersonIdent("Me", "me@example.com", 0, 0),
				"Me <me@example.com>");
		assertMatchesPersonIdent(
				"Me <me@example.com> 1234567890 ",
				new PersonIdent("Me", "me@example.com", 0, 0),
				"Me <me@example.com>");
	}

	private static void assertMatchesPersonIdent(String raw,
			PersonIdent expectedPersonIdent, String expectedUserId) {
		PushCertificateIdent certIdent = PushCertificateIdent.parse(raw);
		assertNotNull(raw);
		assertEquals(raw, certIdent.getRaw());
		assertEquals(expectedPersonIdent.getName(), certIdent.getName());
		assertEquals(expectedPersonIdent.getEmailAddress(),
				certIdent.getEmailAddress());
		assertEquals(expectedPersonIdent.getWhen(), certIdent.getWhen());
		assertEquals(expectedPersonIdent.getTimeZoneOffset(),
				certIdent.getTimeZoneOffset());
		assertEquals(expectedUserId, certIdent.getUserId());
	}

	private static void checkNameEmail(String expectedName, String expectedEmail,
			String raw) {
		PushCertificateIdent ident = parse(raw);
		assertNotNull(ident);
		assertEquals(raw, ident.getRaw());
		assertEquals(expectedName, ident.getName());
		assertEquals(expectedEmail, ident.getEmailAddress());
	}
}