1 /* 2 * Copyright (c) 1999, 2001, 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 4170635 28 * @summary Verify equals()/hashCode() contract honored 29 * @run main/othervm/policy=Allow.policy AVAEqualsHashCode 30 */ 31 32 import java.io.*; 33 import sun.security.x509.*; 34 import sun.security.util.*; 35 import java.lang.reflect.*; 36 37 public class AVAEqualsHashCode { 38 39 public static void main(String[] args) throws Exception { 40 41 int data[] = { 1, 2, 840, 113549, 2, 5 }; 42 43 // encode 44 String name = "CN=eve s. dropper"; 45 X500Name dn = new X500Name(name); 46 DerOutputStream deros = new DerOutputStream(); 47 ObjectIdentifier oid = new ObjectIdentifier(data); 48 49 dn.encode(deros); 50 byte[] ba = deros.toByteArray(); 51 DerValue dv = new DerValue(ba); 52 53 GetAVAConstructor a = new GetAVAConstructor(); 54 java.security.AccessController.doPrivileged(a); 55 Constructor c = a.getCons(); 56 57 Object[] objs = new Object[2]; 58 objs[0] = oid; 59 objs[1] = dv; 60 Object ava1 = null, ava2 = null; 61 try { 62 ava1 = c.newInstance(objs); 63 ava2 = c.newInstance(objs); 64 } catch (Exception e) { 65 System.out.println("Caught unexpected exception " + e); 66 throw e; 67 } 68 69 // System.out.println(ava1.equals(ava2)); 70 // System.out.println(ava1.hashCode() == ava2.hashCode()); 71 if ( (ava1.equals(ava2)) == (ava1.hashCode()==ava2.hashCode()) ) 72 System.out.println("PASSED"); 73 else 74 throw new Exception("FAILED equals()/hashCode() contract"); 75 } 76 } 77 78 class GetAVAConstructor implements java.security.PrivilegedExceptionAction { 79 80 private Class avaClass = null; 81 private Constructor avaCons = null; 82 83 public Object run() throws Exception { 84 try { 85 avaClass = Class.forName("sun.security.x509.AVA"); 86 Constructor[] cons = avaClass.getDeclaredConstructors(); 87 88 int i; 89 for (i = 0; i < cons.length; i++) { 90 Class [] parms = cons[i].getParameterTypes(); 91 if (parms.length == 2) { 92 if (parms[0].getName().equalsIgnoreCase("sun.security.util.ObjectIdentifier") && 93 parms[1].getName().equalsIgnoreCase("sun.security.util.DerValue")) { 94 avaCons = cons[i]; 95 avaCons.setAccessible(true); 96 break; 97 } 98 } 99 } 100 101 } catch (Exception e) { 102 System.out.println("Caught unexpected exception " + e); 103 throw e; 104 } 105 return avaCons; 106 } 107 108 public Constructor getCons(){ 109 return avaCons; 110 } 111 112 }