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 package org.eclipse.jgit.transport;
45
46 import static org.eclipse.jgit.transport.PushCertificateIdent.parse;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertNotNull;
49
50 import java.util.Date;
51 import java.util.TimeZone;
52
53 import org.eclipse.jgit.lib.PersonIdent;
54 import org.junit.Test;
55
56 public class PushCertificateIdentTest {
57 @Test
58 public void parseValid() throws Exception {
59 String raw = "A U. Thor <a_u_thor@example.com> 1218123387 +0700";
60 PushCertificateIdent ident = parse(raw);
61 assertEquals(raw, ident.getRaw());
62 assertEquals("A U. Thor <a_u_thor@example.com>", ident.getUserId());
63 assertEquals("A U. Thor", ident.getName());
64 assertEquals("a_u_thor@example.com", ident.getEmailAddress());
65 assertEquals(1218123387000L, ident.getWhen().getTime());
66 assertEquals(TimeZone.getTimeZone("GMT+0700"), ident.getTimeZone());
67 assertEquals(7 * 60, ident.getTimeZoneOffset());
68 }
69
70 @Test
71 public void trimName() throws Exception {
72 String name = "A U. Thor";
73 String email = "a_u_thor@example.com";
74 String rest = "<a_u_thor@example.com> 1218123387 +0700";
75
76 checkNameEmail(name, email, name + rest);
77 checkNameEmail(name, email, " " + name + rest);
78 checkNameEmail(name, email, " " + name + rest);
79 checkNameEmail(name, email, name + " " + rest);
80 checkNameEmail(name, email, name + " " + rest);
81 checkNameEmail(name, email, " " + name + " " + rest);
82 }
83
84 @Test
85 public void noEmail() throws Exception {
86 String name = "A U. Thor";
87 String rest = " 1218123387 +0700";
88
89 checkNameEmail(name, null, name + rest);
90 checkNameEmail(name, null, " " + name + rest);
91 checkNameEmail(name, null, " " + name + rest);
92 checkNameEmail(name, null, name + " " + rest);
93 checkNameEmail(name, null, name + " " + rest);
94 checkNameEmail(name, null, " " + name + " " + rest);
95 }
96
97 @Test
98 public void exoticUserId() throws Exception {
99 String rest = " 218123387 +0700";
100 assertEquals("", parse(rest).getUserId());
101
102 String id = "foo\n\0bar\uabcd\n ";
103 assertEquals(id, parse(id + rest).getUserId());
104 }
105
106 @Test
107 public void fuzzyCasesMatchPersonIdent() throws Exception {
108
109 Date when = new Date(1234567890000l);
110 TimeZone tz = TimeZone.getTimeZone("GMT-7");
111
112 assertMatchesPersonIdent(
113 "A U Thor <author@example.com>, C O. Miter <comiter@example.com> 1234567890 -0700",
114 new PersonIdent("A U Thor", "author@example.com", when, tz),
115 "A U Thor <author@example.com>, C O. Miter <comiter@example.com>");
116 assertMatchesPersonIdent(
117 "A U Thor <author@example.com> and others 1234567890 -0700",
118 new PersonIdent("A U Thor", "author@example.com", when, tz),
119 "A U Thor <author@example.com> and others");
120 }
121
122 @Test
123 public void incompleteCasesMatchPersonIdent() throws Exception {
124
125 Date when = new Date(1234567890000l);
126 TimeZone tz = TimeZone.getTimeZone("GMT-7");
127
128 assertMatchesPersonIdent(
129 "Me <> 1234567890 -0700",
130 new PersonIdent("Me", "", when, tz),
131 "Me <>");
132 assertMatchesPersonIdent(
133 " <me@example.com> 1234567890 -0700",
134 new PersonIdent("", "me@example.com", when, tz),
135 " <me@example.com>");
136 assertMatchesPersonIdent(
137 " <> 1234567890 -0700",
138 new PersonIdent("", "", when, tz),
139 " <>");
140 assertMatchesPersonIdent(
141 "<>",
142 new PersonIdent("", "", 0, 0),
143 "<>");
144 assertMatchesPersonIdent(
145 " <>",
146 new PersonIdent("", "", 0, 0),
147 " <>");
148 assertMatchesPersonIdent(
149 "<me@example.com>",
150 new PersonIdent("", "me@example.com", 0, 0),
151 "<me@example.com>");
152 assertMatchesPersonIdent(
153 " <me@example.com>",
154 new PersonIdent("", "me@example.com", 0, 0),
155 " <me@example.com>");
156 assertMatchesPersonIdent(
157 "Me <>",
158 new PersonIdent("Me", "", 0, 0),
159 "Me <>");
160 assertMatchesPersonIdent(
161 "Me <me@example.com>",
162 new PersonIdent("Me", "me@example.com", 0, 0),
163 "Me <me@example.com>");
164 assertMatchesPersonIdent(
165 "Me <me@example.com> 1234567890",
166 new PersonIdent("Me", "me@example.com", 0, 0),
167 "Me <me@example.com>");
168 assertMatchesPersonIdent(
169 "Me <me@example.com> 1234567890 ",
170 new PersonIdent("Me", "me@example.com", 0, 0),
171 "Me <me@example.com>");
172 }
173
174 private static void assertMatchesPersonIdent(String raw,
175 PersonIdent expectedPersonIdent, String expectedUserId) {
176 PushCertificateIdent certIdent = PushCertificateIdent.parse(raw);
177 assertNotNull(raw);
178 assertEquals(raw, certIdent.getRaw());
179 assertEquals(expectedPersonIdent.getName(), certIdent.getName());
180 assertEquals(expectedPersonIdent.getEmailAddress(),
181 certIdent.getEmailAddress());
182 assertEquals(expectedPersonIdent.getWhen(), certIdent.getWhen());
183 assertEquals(expectedPersonIdent.getTimeZoneOffset(),
184 certIdent.getTimeZoneOffset());
185 assertEquals(expectedUserId, certIdent.getUserId());
186 }
187
188 private static void checkNameEmail(String expectedName, String expectedEmail,
189 String raw) {
190 PushCertificateIdent ident = parse(raw);
191 assertNotNull(ident);
192 assertEquals(raw, ident.getRaw());
193 assertEquals(expectedName, ident.getName());
194 assertEquals(expectedEmail, ident.getEmailAddress());
195 }
196 }