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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* 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>");
}
@Test
public void timezoneRange_hours() {
int HOUR_TO_MS = 60 * 60 * 1000;
// java.util.TimeZone: Hours must be between 0 to 23
PushCertificateIdent hourLimit = PushCertificateIdent
.parse("A U. Thor <a_u_thor@example.com> 1218123387 +2300");
assertEquals(1380, hourLimit.getTimeZoneOffset());
assertEquals(23 * HOUR_TO_MS,
hourLimit.getTimeZone().getOffset(1218123387));
PushCertificateIdent hourDubious = PushCertificateIdent
.parse("A U. Thor <a_u_thor@example.com> 1218123387 +2400");
assertEquals(1440, hourDubious.getTimeZoneOffset());
assertEquals(0, hourDubious.getTimeZone().getOffset(1218123387));
}
@Test
public void timezoneRange_minutes() {
PushCertificateIdent hourLimit = PushCertificateIdent
.parse("A U. Thor <a_u_thor@example.com> 1218123387 +0059");
assertEquals(59, hourLimit.getTimeZoneOffset());
assertEquals(59 * 60 * 1000,
hourLimit.getTimeZone().getOffset(1218123387));
// This becomes one hour and one minute (!)
PushCertificateIdent hourDubious = PushCertificateIdent
.parse("A U. Thor <a_u_thor@example.com> 1218123387 +0061");
assertEquals(61, hourDubious.getTimeZoneOffset());
assertEquals(61 * 60 * 1000,
hourDubious.getTimeZone().getOffset(1218123387));
PushCertificateIdent weirdCase = PushCertificateIdent
.parse("A U. Thor <a_u_thor@example.com> 1218123387 +0099");
assertEquals(99, weirdCase.getTimeZoneOffset());
assertEquals(99 * 60 * 1000,
weirdCase.getTimeZone().getOffset(1218123387));
PushCertificateIdent weirdCase2 = PushCertificateIdent
.parse("A U. Thor <a_u_thor@example.com> 1218123387 +0199");
assertEquals(60 + 99, weirdCase2.getTimeZoneOffset());
assertEquals((60 + 99) * 60 * 1000,
weirdCase2.getTimeZone().getOffset(1218123387));
}
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());
}
}
|