view j2se/test/sun/security/x509/AVA/AVAEqualsHashCode.java @ 2:16f2b6c91171 trunk

[svn] Load openjdk/jdk7/b14 into jdk/trunk.
author xiomara
date Fri, 22 Jun 2007 00:46:43 +0000
parents 193df1943809
children 64ed597c0ad3
line wrap: on
line source

/*
 * Copyright 1999-2001 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test 1.5 07/06/21
 * @author Gary Ellison
 * @bug 4170635
 * @summary Verify equals()/hashCode() contract honored
 * @run main/othervm/policy=Allow.policy AVAEqualsHashCode
 */

import java.io.*;
import sun.security.x509.*;
import sun.security.util.*;
import java.lang.reflect.*;

public class AVAEqualsHashCode {

   public static void main(String[] args) throws Exception {

	int data[] = { 1, 2, 840, 113549, 2, 5 };

	// encode
	String name = "CN=eve s. dropper";
	X500Name dn = new X500Name(name);
	DerOutputStream deros = new DerOutputStream();
	ObjectIdentifier oid = new ObjectIdentifier(data);

	dn.encode(deros);
	byte[] ba = deros.toByteArray();
	DerValue dv = new DerValue(ba);

	GetAVAConstructor a = new GetAVAConstructor();
	java.security.AccessController.doPrivileged(a);
	Constructor c = a.getCons();

	Object[] objs = new Object[2];
	objs[0] = oid;
	objs[1] = dv;
	Object ava1 = null, ava2 = null;
	try {
	    ava1 = c.newInstance(objs);
	    ava2 = c.newInstance(objs);
	} catch (Exception e) {
	    System.out.println("Caught unexpected exception " + e);
	    throw e;
	}
	
	// System.out.println(ava1.equals(ava2));
	// System.out.println(ava1.hashCode() == ava2.hashCode());
        if ( (ava1.equals(ava2)) == (ava1.hashCode()==ava2.hashCode()) )
            System.out.println("PASSED");
        else
	    throw new Exception("FAILED equals()/hashCode() contract");
    }
}

class GetAVAConstructor implements java.security.PrivilegedExceptionAction {

    private Class avaClass = null;
    private Constructor avaCons = null;

    public Object run() throws Exception {
	try {
	    avaClass = Class.forName("sun.security.x509.AVA");
	    Constructor[] cons = avaClass.getDeclaredConstructors();

	    int i;
	    for (i = 0; i < cons.length; i++) {
		Class [] parms = cons[i].getParameterTypes();
		if (parms.length == 2) {
		    if (parms[0].getName().equalsIgnoreCase("sun.security.util.ObjectIdentifier") &&
			    parms[1].getName().equalsIgnoreCase("sun.security.util.DerValue")) {
	    		avaCons = cons[i];
			avaCons.setAccessible(true);
			break;
		    }
		}	
	    }
				
	} catch (Exception e) {
	    System.out.println("Caught unexpected exception " + e);
	    throw e;
	}
	return avaCons;
    }

    public Constructor getCons(){
	return avaCons;
    }

}