1/* 2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @author Gary Ellison 27 * @bug 4183136 28 * @summary java.security.Identity violates equals/hashCode contract 29 */ 30import java.security.*; 31import java.io.*; 32 33public class EqualsHashCodeContract 34{ 35 public static void main(String args[]) throws Exception { 36 37 Identity i1=new MyIdentity("identity", 38 new MyIdentityScope("IdentityScope")); 39 Identity i2=new MyIdentity("identity", 40 new MyIdentityScope("IdentityScope")); 41 Identity i3=new MyIdentity("identity", 42 new MyIdentityScope("")); 43 44 PublicKey pk1=new MyPublicKey(); 45 PublicKey pk2=new MyPublicKey(); 46 47 if ( !(i1.equals(i2)) == (i1.hashCode()==i2.hashCode()) ) { 48 System.err.println("FAILED"); 49 Exception up = new 50 Exception("Contract violated -- same name and same scope"); 51 throw up; 52 } 53 System.out.println("Test same name, same scope........... PASSED"); 54 55 i1.setPublicKey(pk1); 56 i3.setPublicKey(pk1); 57 if ( !((i1.equals(i3)) && (i1.hashCode()==i3.hashCode()))) { 58 System.err.println("FAILED"); 59 Exception up = new Exception("Contract violated -- PublicKeys do not differ"); 60 throw up; 61 } 62 System.out.println("Test same name, same PublicKeys ..... PASSED"); 63 64 System.out.println("TEST PASSED"); 65 66 } 67} 68 69class MyIdentity extends Identity { 70 public MyIdentity(String name, IdentityScope is) throws KeyManagementException { 71 super(name, is); 72 } 73} 74 75class MyPublicKey implements PublicKey, Certificate { 76 private byte e[] = null; 77 public String getAlgorithm() { 78 return null; 79 } 80 public String getFormat() { 81 return new String("PKCS15"); 82 } 83 84 public byte[] getEncoded() { 85 if (e == null) { 86 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 87 DataOutputStream ds = new DataOutputStream(bs); 88 try { 89 ds.writeLong(System.currentTimeMillis()); 90 } catch (IOException ioe) { 91 ioe.printStackTrace(); 92 } 93 e = bs.toByteArray(); 94 } 95 96 return e; 97 } 98 99 public void decode(InputStream stream) { 100 } 101 public void encode(OutputStream stream) { 102 } 103 public Principal getGuarantor() { 104 return null; 105 } 106 public Principal getPrincipal() { 107 return null; 108 } 109 public PublicKey getPublicKey() { 110 return this; 111 } 112 public String toString(boolean detailed) { 113 return null; 114 } 115} 116 117 118class MyIdentityScope extends IdentityScope { 119 public MyIdentityScope(String name) { 120 super(name); 121 } 122 123 public int size() { 124 return 0; 125 } 126 127 public Identity getIdentity(String name) { 128 return null; 129 } 130 131 public Identity getIdentity(PublicKey key) { 132 return null; 133 } 134 135 public void addIdentity(Identity identity) { 136 } 137 138 public void removeIdentity(Identity identity) { 139 } 140 141 public java.util.Enumeration identities() { 142 return null; 143 } 144 145 146} 147