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