KerberosHashEqualsTest.java revision 2362:00cd9dc3c2b5
1/*
2 * Copyright (c) 2005, 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 * @bug 4641821
27 * @summary hashCode() and equals() for KerberosKey and KerberosTicket
28 * @ignore Must set up KDC and setup Kerberos configuration file
29 */
30
31import java.net.InetAddress;
32import java.util.Date;
33import javax.security.auth.kerberos.KerberosKey;
34import javax.security.auth.kerberos.KerberosPrincipal;
35import javax.security.auth.kerberos.KerberosTicket;
36
37public class KerberosHashEqualsTest {
38    public static void main(String[] args) throws Exception {
39        new KerberosHashEqualsTest().check();
40    }
41
42    void checkSame(Object o1, Object o2) {
43        if(!o1.equals(o2)) {
44            throw new RuntimeException("equals() fails");
45        }
46        if(o1.hashCode() != o2.hashCode()) {
47            throw new RuntimeException("hashCode() not same");
48        }
49    }
50
51    void checkNotSame(Object o1, Object o2) {
52        if(o1.equals(o2)) {
53            throw new RuntimeException("equals() succeeds");
54        }
55    }
56
57    void check() throws Exception {
58        KerberosKey k1, k2;
59        k1 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 1, 1);
60        k2 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 1, 1);
61        checkSame(k1, k1);  // me to me
62        checkSame(k1, k2);  // same
63
64        k2.destroy();
65        checkNotSame(k1, k2);
66
67        // destroyed keys doesn't equal to each other
68        checkNotSame(k2, k1);
69        checkSame(k2, k2);
70
71        // a little different
72        k2 = new KerberosKey(new KerberosPrincipal("B"), "pass".getBytes(), 1, 1);
73        checkNotSame(k1, k2);
74        k2 = new KerberosKey(new KerberosPrincipal("A"), "ssap".getBytes(), 1, 1);
75        checkNotSame(k1, k2);
76        k2 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 2, 1);
77        checkNotSame(k1, k2);
78        k2 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 1, 2);
79        checkNotSame(k1, k2);
80
81        k1 = new KerberosKey(null, "pass".getBytes(), 1, 2);
82        checkNotSame(k1, k2); // null to non-null
83        k2 = new KerberosKey(null, "pass".getBytes(), 1, 2);
84        checkSame(k1, k2);    // null to null
85
86        checkNotSame(k1, "Another Object");
87
88        KerberosTicket t1, t2;
89        t1 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
91        checkSame(t1, t1);
92        checkSame(t1, t2);
93        t2 = new KerberosTicket("asn11".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
94        checkNotSame(t1, t2);
95        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client1"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
96        checkNotSame(t1, t2);
97        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server1"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
98        checkNotSame(t1, t2);
99        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass1".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
100        checkNotSame(t1, t2);
101        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 2, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
102        checkNotSame(t1, t2);
103        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {false, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
104        checkNotSame(t1, t2);
105        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(1), new Date(0), new Date(0), new Date(0), null);
106        checkNotSame(t1, t2);
107        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(1), new Date(0), new Date(0), null);
108        checkNotSame(t1, t2);
109        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(1), new Date(0), null);
110        checkNotSame(t1, t2);
111        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), new InetAddress[2]);
112        checkNotSame(t1, t2);
113
114        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(1), null);
115        t1 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(2), null);
116        checkSame(t1, t2);  // renewtill is useless
117
118        t2.destroy();
119        checkNotSame(t1, t2);
120
121        // destroyed tickets doesn't equal to each other
122        checkNotSame(t2, t1);
123        checkSame(t2, t2);
124
125        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true, true, true, true, true, true, true, true, true}, new Date(0), new Date(0), new Date(0), new Date(1), null);
126        t1 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true, true, true, true, true, true, true, true, true}, new Date(0), new Date(0), new Date(0), new Date(2), null);
127        checkNotSame(t1, t2);  // renewtill is useful
128
129        checkNotSame(t1, "Another Object");
130        System.out.println("Good!");
131    }
132}
133