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.

BouncyCastleGpgKeyLocatorTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2019, 2020 Thomas Wolf <thomas.wolf@paranor.ch> 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.gpg.bc.internal;
  11. import static org.junit.Assert.assertFalse;
  12. import static org.junit.Assert.assertTrue;
  13. import java.util.Locale;
  14. import org.junit.Test;
  15. public class BouncyCastleGpgKeyLocatorTest {
  16. private static final String USER_ID = "Heinrich Heine <heinrichh@uni-duesseldorf.de>";
  17. private static boolean match(String userId, String pattern) {
  18. return BouncyCastleGpgKeyLocator.containsSigningKey(userId, pattern);
  19. }
  20. @Test
  21. public void testFullMatch() throws Exception {
  22. assertTrue(match(USER_ID,
  23. "=Heinrich Heine <heinrichh@uni-duesseldorf.de>"));
  24. assertFalse(match(USER_ID, "=Heinrich Heine"));
  25. assertFalse(match(USER_ID, "= "));
  26. assertFalse(match(USER_ID, "=heinrichh@uni-duesseldorf.de"));
  27. }
  28. @Test
  29. public void testEmpty() throws Exception {
  30. assertFalse(match(USER_ID, ""));
  31. assertFalse(match(USER_ID, null));
  32. assertFalse(match("", ""));
  33. assertFalse(match(null, ""));
  34. assertFalse(match(null, null));
  35. assertFalse(match("", "something"));
  36. assertFalse(match(null, "something"));
  37. }
  38. @Test
  39. public void testFullEmail() throws Exception {
  40. assertTrue(match(USER_ID, "<heinrichh@uni-duesseldorf.de>"));
  41. assertTrue(match(USER_ID + " ", "<heinrichh@uni-duesseldorf.de>"));
  42. assertFalse(match(USER_ID, "<>"));
  43. assertFalse(match(USER_ID, "<h>"));
  44. assertFalse(match(USER_ID, "<heinrichh>"));
  45. assertFalse(match(USER_ID, "<uni-duesseldorf>"));
  46. assertFalse(match(USER_ID, "<h@u>"));
  47. assertTrue(match(USER_ID, "<HeinrichH@uni-duesseldorf.de>"));
  48. assertFalse(match(USER_ID.substring(0, USER_ID.length() - 1),
  49. "<heinrichh@uni-duesseldorf.de>"));
  50. assertFalse(match("", "<>"));
  51. assertFalse(match("", "<heinrichh@uni-duesseldorf.de>"));
  52. }
  53. @Test
  54. public void testPartialEmail() throws Exception {
  55. assertTrue(match(USER_ID, "@heinrichh@uni-duesseldorf.de"));
  56. assertTrue(match(USER_ID, "@heinrichh"));
  57. assertTrue(match(USER_ID, "@duesseldorf"));
  58. assertTrue(match(USER_ID, "@uni-d"));
  59. assertTrue(match(USER_ID, "@h"));
  60. assertTrue(match(USER_ID, "@."));
  61. assertTrue(match(USER_ID, "@h@u"));
  62. assertFalse(match(USER_ID, "@ "));
  63. assertFalse(match(USER_ID, "@"));
  64. assertFalse(match(USER_ID, "@Heine"));
  65. assertTrue(match(USER_ID, "@HeinrichH"));
  66. assertTrue(match(USER_ID, "@Heinrich"));
  67. assertFalse(match("", "@"));
  68. assertFalse(match("", "@h"));
  69. }
  70. private void substringTests(String prefix) throws Exception {
  71. assertTrue(match(USER_ID, prefix + "heinrichh@uni-duesseldorf.de"));
  72. assertTrue(match(USER_ID, prefix + "heinrich"));
  73. assertTrue(match(USER_ID, prefix + "HEIN"));
  74. assertTrue(match(USER_ID, prefix + "Heine <"));
  75. assertTrue(match(USER_ID, prefix + "UNI"));
  76. assertTrue(match(USER_ID, prefix + "uni"));
  77. assertTrue(match(USER_ID, prefix + "rich He"));
  78. assertTrue(match(USER_ID, prefix + "h@u"));
  79. assertTrue(match(USER_ID, prefix + USER_ID));
  80. assertTrue(match(USER_ID, prefix + USER_ID.toUpperCase(Locale.ROOT)));
  81. assertFalse(match(USER_ID, prefix + ""));
  82. assertFalse(match(USER_ID, prefix + " "));
  83. assertFalse(match(USER_ID, prefix + "yy"));
  84. assertFalse(match("", prefix + ""));
  85. assertFalse(match("", prefix + "uni"));
  86. }
  87. @Test
  88. public void testSubstringPlain() throws Exception {
  89. substringTests("");
  90. }
  91. @Test
  92. public void testSubstringAsterisk() throws Exception {
  93. substringTests("*");
  94. }
  95. @Test
  96. public void testExplicitFingerprint() throws Exception {
  97. assertFalse(match("John Fade <j.fade@example.com>", "0xfade"));
  98. assertFalse(match("John Fade <0xfade@example.com>", "0xfade"));
  99. assertFalse(match("John Fade <0xfade@example.com>", "0xFADE"));
  100. assertFalse(match("", "0xfade"));
  101. }
  102. @Test
  103. public void testImplicitFingerprint() throws Exception {
  104. assertTrue(match("John Fade <j.fade@example.com>", "fade"));
  105. assertTrue(match("John Fade <0xfade@example.com>", "fade"));
  106. assertTrue(match("John Fade <j.fade@example.com>", "FADE"));
  107. assertTrue(match("John Fade <0xfade@example.com>", "FADE"));
  108. }
  109. @Test
  110. public void testZeroX() throws Exception {
  111. assertTrue(match("John Fade <0xfade@example.com>", "0x"));
  112. assertTrue(match("John Fade <0xfade@example.com>", "*0x"));
  113. assertTrue(match("John Fade <0xfade@example.com>", "*0xfade"));
  114. assertTrue(match("John Fade <0xfade@example.com>", "*0xFADE"));
  115. assertTrue(match("John Fade <0xfade@example.com>", "@0xfade"));
  116. assertTrue(match("John Fade <0xfade@example.com>", "@0xFADE"));
  117. assertFalse(match("", "0x"));
  118. }
  119. }