1 /* 2 * Copyright �, Aegeus Technology Limited. 3 * All rights reserved. 4 */ 5 package jsdsi; 6 7 import java.net.URI; 8 import java.net.URISyntaxException; 9 import java.security.KeyPair; 10 import java.security.NoSuchAlgorithmException; 11 12 import jsdsi.util.DigestAlgoEnum; 13 import jsdsi.util.KeyEnum; 14 import jsdsi.util.KeyPairFactory; 15 import junit.framework.TestCase; 16 17 18 /*** 19 * Some simple tests. 20 * 21 * @author Sean Radford 22 * @version $Revision: 1.2 $ $Date: 2004/11/08 12:08:08 $ 23 */ 24 public class PublicKeyHashTest extends TestCase { 25 26 private KeyPair keyPair; 27 28 29 /*** 30 * @see junit.framework.TestCase#setUp() 31 */ 32 protected void setUp() throws Exception { 33 super.setUp(); 34 Provider.install(); 35 keyPair = KeyPairFactory.create(KeyEnum.RSA, 512); 36 } 37 38 /*** 39 * Test of a Hash with no URI[]. 40 * @throws Exception 41 */ 42 public void testHashOnly() throws Exception { 43 Hash hashExpected = new Hash(DigestAlgoEnum.MD5, 44 (PublicKey)keyPair.getPublic(), null); 45 PublicKeyHash pubKeyHash = new PublicKeyHash(hashExpected); 46 Hash hashActual = pubKeyHash.getHash(); 47 assertEquals("Hash, ", hashExpected, hashActual); 48 } 49 50 /*** 51 * Test of a Hash with a URI[]. 52 * @throws URISyntaxException 53 */ 54 public void testHashAndURIs() throws URISyntaxException { 55 URI[] urisExpected = new URI[] { new URI("test:/test") }; 56 Hash hashExpected = new Hash(DigestAlgoEnum.MD5, 57 (PublicKey)keyPair.getPublic(), 58 urisExpected); 59 PublicKeyHash pubKeyHash = new PublicKeyHash(hashExpected); 60 Hash hashActual = pubKeyHash.getHash(); 61 assertEquals("Hash, ", hashExpected, hashActual); 62 URI[] urisActual = pubKeyHash.getURIs(); 63 assertNotNull("URIs is NULL", urisActual); 64 assertEquals("URI.length, ", urisExpected.length, urisActual.length); 65 } 66 67 /*** 68 * Test of samePrincipalAs with matching PublicKeyHash. 69 */ 70 public void testSamePrincipalAsWithPublicKeyHash() { 71 Hash hash = new Hash(DigestAlgoEnum.MD5, 72 (PublicKey)keyPair.getPublic(), null); 73 PublicKeyHash pubKeyHash = new PublicKeyHash(hash); 74 assertTrue(pubKeyHash.samePrincipalAs(pubKeyHash)); 75 } 76 77 /*** 78 * Test of samePrincipalAs with matching PublicKey. 79 */ 80 public void testSamePrincipalAsWithPublicKey() { 81 Hash hash = new Hash(DigestAlgoEnum.MD5, 82 (PublicKey)keyPair.getPublic(), null); 83 PublicKeyHash pubKeyHash = new PublicKeyHash(hash); 84 assertTrue(pubKeyHash.samePrincipalAs( (Principal) keyPair.getPublic() )); 85 } 86 87 /*** 88 * Test of samePrincipalAs with non-matching PublicKeyHash. 89 * @throws NoSuchAlgorithmException 90 */ 91 public void testNotSamePrincipalAsWithPublicKeyHash() throws NoSuchAlgorithmException { 92 Hash hash = new Hash(DigestAlgoEnum.MD5, 93 (PublicKey)keyPair.getPublic(), null); 94 PublicKeyHash pubKeyHash = new PublicKeyHash(hash); 95 KeyPair otherKeyPair = KeyPairFactory.create(KeyEnum.RSA, 512); 96 Hash otherHash = new Hash(DigestAlgoEnum.MD5, 97 (PublicKey)otherKeyPair.getPublic(), null); 98 PublicKeyHash otherPubKeyHash = new PublicKeyHash(otherHash); 99 assertFalse(pubKeyHash.samePrincipalAs(otherPubKeyHash)); 100 } 101 102 /*** 103 * Test of samePrincipalAs with non-matching PublicKey. 104 * @throws NoSuchAlgorithmException 105 */ 106 public void testNotSamePrincipalAsWithPublicKey() throws NoSuchAlgorithmException { 107 Hash hash = new Hash(DigestAlgoEnum.MD5, 108 (PublicKey)keyPair.getPublic(), null); 109 PublicKeyHash pubKeyHash = new PublicKeyHash(hash); 110 KeyPair otherKeyPair = KeyPairFactory.create(KeyEnum.RSA, 512); 111 assertFalse(pubKeyHash.samePrincipalAs((Principal) otherKeyPair.getPublic())); 112 } 113 114 }

This page was automatically generated by Maven