You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushCertificateIdentTest.java 7.0KB

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