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 5.4KB

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