1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import static org.eclipse.jgit.transport.PushCertificateIdent.parse;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.util.Date;
18 import java.util.TimeZone;
19
20 import org.eclipse.jgit.lib.PersonIdent;
21 import org.junit.Test;
22
23 public class PushCertificateIdentTest {
24 @Test
25 public void parseValid() throws Exception {
26 String raw = "A U. Thor <a_u_thor@example.com> 1218123387 +0700";
27 PushCertificateIdent ident = parse(raw);
28 assertEquals(raw, ident.getRaw());
29 assertEquals("A U. Thor <a_u_thor@example.com>", ident.getUserId());
30 assertEquals("A U. Thor", ident.getName());
31 assertEquals("a_u_thor@example.com", ident.getEmailAddress());
32 assertEquals(1218123387000L, ident.getWhen().getTime());
33 assertEquals(TimeZone.getTimeZone("GMT+0700"), ident.getTimeZone());
34 assertEquals(7 * 60, ident.getTimeZoneOffset());
35 }
36
37 @Test
38 public void trimName() throws Exception {
39 String name = "A U. Thor";
40 String email = "a_u_thor@example.com";
41 String rest = "<a_u_thor@example.com> 1218123387 +0700";
42
43 checkNameEmail(name, email, name + rest);
44 checkNameEmail(name, email, " " + name + rest);
45 checkNameEmail(name, email, " " + name + rest);
46 checkNameEmail(name, email, name + " " + rest);
47 checkNameEmail(name, email, name + " " + rest);
48 checkNameEmail(name, email, " " + name + " " + rest);
49 }
50
51 @Test
52 public void noEmail() throws Exception {
53 String name = "A U. Thor";
54 String rest = " 1218123387 +0700";
55
56 checkNameEmail(name, null, name + rest);
57 checkNameEmail(name, null, " " + name + rest);
58 checkNameEmail(name, null, " " + name + rest);
59 checkNameEmail(name, null, name + " " + rest);
60 checkNameEmail(name, null, name + " " + rest);
61 checkNameEmail(name, null, " " + name + " " + rest);
62 }
63
64 @Test
65 public void exoticUserId() throws Exception {
66 String rest = " 218123387 +0700";
67 assertEquals("", parse(rest).getUserId());
68
69 String id = "foo\n\0bar\uabcd\n ";
70 assertEquals(id, parse(id + rest).getUserId());
71 }
72
73 @Test
74 public void fuzzyCasesMatchPersonIdent() throws Exception {
75
76 Date when = new Date(1234567890000l);
77 TimeZone tz = TimeZone.getTimeZone("GMT-7");
78
79 assertMatchesPersonIdent(
80 "A U Thor <author@example.com>, C O. Miter <comiter@example.com> 1234567890 -0700",
81 new PersonIdent("A U Thor", "author@example.com", when, tz),
82 "A U Thor <author@example.com>, C O. Miter <comiter@example.com>");
83 assertMatchesPersonIdent(
84 "A U Thor <author@example.com> and others 1234567890 -0700",
85 new PersonIdent("A U Thor", "author@example.com", when, tz),
86 "A U Thor <author@example.com> and others");
87 }
88
89 @Test
90 public void incompleteCasesMatchPersonIdent() throws Exception {
91
92 Date when = new Date(1234567890000l);
93 TimeZone tz = TimeZone.getTimeZone("GMT-7");
94
95 assertMatchesPersonIdent(
96 "Me <> 1234567890 -0700",
97 new PersonIdent("Me", "", when, tz),
98 "Me <>");
99 assertMatchesPersonIdent(
100 " <me@example.com> 1234567890 -0700",
101 new PersonIdent("", "me@example.com", when, tz),
102 " <me@example.com>");
103 assertMatchesPersonIdent(
104 " <> 1234567890 -0700",
105 new PersonIdent("", "", when, tz),
106 " <>");
107 assertMatchesPersonIdent(
108 "<>",
109 new PersonIdent("", "", 0, 0),
110 "<>");
111 assertMatchesPersonIdent(
112 " <>",
113 new PersonIdent("", "", 0, 0),
114 " <>");
115 assertMatchesPersonIdent(
116 "<me@example.com>",
117 new PersonIdent("", "me@example.com", 0, 0),
118 "<me@example.com>");
119 assertMatchesPersonIdent(
120 " <me@example.com>",
121 new PersonIdent("", "me@example.com", 0, 0),
122 " <me@example.com>");
123 assertMatchesPersonIdent(
124 "Me <>",
125 new PersonIdent("Me", "", 0, 0),
126 "Me <>");
127 assertMatchesPersonIdent(
128 "Me <me@example.com>",
129 new PersonIdent("Me", "me@example.com", 0, 0),
130 "Me <me@example.com>");
131 assertMatchesPersonIdent(
132 "Me <me@example.com> 1234567890",
133 new PersonIdent("Me", "me@example.com", 0, 0),
134 "Me <me@example.com>");
135 assertMatchesPersonIdent(
136 "Me <me@example.com> 1234567890 ",
137 new PersonIdent("Me", "me@example.com", 0, 0),
138 "Me <me@example.com>");
139 }
140
141 private static void assertMatchesPersonIdent(String raw,
142 PersonIdent expectedPersonIdent, String expectedUserId) {
143 PushCertificateIdent certIdent = PushCertificateIdent.parse(raw);
144 assertNotNull(raw);
145 assertEquals(raw, certIdent.getRaw());
146 assertEquals(expectedPersonIdent.getName(), certIdent.getName());
147 assertEquals(expectedPersonIdent.getEmailAddress(),
148 certIdent.getEmailAddress());
149 assertEquals(expectedPersonIdent.getWhen(), certIdent.getWhen());
150 assertEquals(expectedPersonIdent.getTimeZoneOffset(),
151 certIdent.getTimeZoneOffset());
152 assertEquals(expectedUserId, certIdent.getUserId());
153 }
154
155 private static void checkNameEmail(String expectedName, String expectedEmail,
156 String raw) {
157 PushCertificateIdent ident = parse(raw);
158 assertNotNull(ident);
159 assertEquals(raw, ident.getRaw());
160 assertEquals(expectedName, ident.getName());
161 assertEquals(expectedEmail, ident.getEmailAddress());
162 }
163 }