Path: blob/master/test/jdk/sun/security/x509/AVA/AVAEqualsHashCode.java
41153 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @author Gary Ellison26* @bug 4170635 824215127* @summary Verify equals()/hashCode() contract honored28* @modules java.base/sun.security.util29* java.base/sun.security.x50930* @run main/othervm/policy=Allow.policy AVAEqualsHashCode31*/3233import java.io.*;34import sun.security.x509.*;35import sun.security.util.*;36import java.lang.reflect.*;3738public class AVAEqualsHashCode {3940public static void main(String[] args) throws Exception {4142// encode43String name = "CN=eve s. dropper";44X500Name dn = new X500Name(name);45DerOutputStream deros = new DerOutputStream();46ObjectIdentifier oid = ObjectIdentifier.of("1.2.840.113549.2.5");4748dn.encode(deros);49byte[] ba = deros.toByteArray();50DerValue dv = new DerValue(ba);5152GetAVAConstructor a = new GetAVAConstructor();53java.security.AccessController.doPrivileged(a);54Constructor c = a.getCons();5556Object[] objs = new Object[2];57objs[0] = oid;58objs[1] = dv;59Object ava1 = null, ava2 = null;60try {61ava1 = c.newInstance(objs);62ava2 = c.newInstance(objs);63} catch (Exception e) {64System.out.println("Caught unexpected exception " + e);65throw e;66}6768// System.out.println(ava1.equals(ava2));69// System.out.println(ava1.hashCode() == ava2.hashCode());70if ( (ava1.equals(ava2)) == (ava1.hashCode()==ava2.hashCode()) )71System.out.println("PASSED");72else73throw new Exception("FAILED equals()/hashCode() contract");74}75}7677class GetAVAConstructor implements java.security.PrivilegedExceptionAction {7879private Class avaClass = null;80private Constructor avaCons = null;8182public Object run() throws Exception {83try {84avaClass = Class.forName("sun.security.x509.AVA");85Constructor[] cons = avaClass.getDeclaredConstructors();8687int i;88for (i = 0; i < cons.length; i++) {89Class [] parms = cons[i].getParameterTypes();90if (parms.length == 2) {91if (parms[0].getName().equalsIgnoreCase("sun.security.util.ObjectIdentifier") &&92parms[1].getName().equalsIgnoreCase("sun.security.util.DerValue")) {93avaCons = cons[i];94avaCons.setAccessible(true);95break;96}97}98}99100} catch (Exception e) {101System.out.println("Caught unexpected exception " + e);102throw e;103}104return avaCons;105}106107public Constructor getCons(){108return avaCons;109}110111}112113114