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