View Javadoc
1   /*
2    * Copyright (C) 2015, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 		// See RawParseUtils_ParsePersonIdentTest#testParsePersonIdent_fuzzyCases()
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 		// See RawParseUtils_ParsePersonIdentTest#testParsePersonIdent_incompleteCases()
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 }