1454
|
1 |
/*
|
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4641821
|
|
27 |
* @summary hashCode() and equals() for KerberosKey and KerberosTicket
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.InetAddress;
|
|
31 |
import java.util.Date;
|
|
32 |
import javax.security.auth.kerberos.KerberosKey;
|
|
33 |
import javax.security.auth.kerberos.KerberosPrincipal;
|
|
34 |
import javax.security.auth.kerberos.KerberosTicket;
|
|
35 |
|
|
36 |
public class KerberosHashEqualsTest {
|
|
37 |
public static void main(String[] args) throws Exception {
|
|
38 |
new OneKDC(null);
|
|
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 |
|
|
59 |
// The key part:
|
|
60 |
// new KerberosKey(principal, bytes, keyType, version)
|
|
61 |
|
|
62 |
KerberosKey k1, k2;
|
|
63 |
KerberosPrincipal CLIENT = new KerberosPrincipal("client");
|
|
64 |
KerberosPrincipal SERVER = new KerberosPrincipal("server");
|
|
65 |
byte[] PASS = "pass".getBytes();
|
|
66 |
|
|
67 |
k1 = new KerberosKey(CLIENT, PASS, 1, 1);
|
|
68 |
k2 = new KerberosKey(CLIENT, PASS, 1, 1);
|
|
69 |
checkSame(k1, k1); // me is me
|
|
70 |
checkSame(k1, k2); // same
|
|
71 |
|
|
72 |
// A destroyed key doesn't equal to any key
|
|
73 |
k2.destroy();
|
|
74 |
checkNotSame(k1, k2);
|
|
75 |
checkNotSame(k2, k1);
|
|
76 |
k1.destroy();
|
|
77 |
checkNotSame(k1, k2); // even if they are both destroyed
|
|
78 |
checkNotSame(k2, k1);
|
|
79 |
checkSame(k2, k2);
|
|
80 |
|
|
81 |
// a little difference means not equal
|
|
82 |
k1 = new KerberosKey(CLIENT, PASS, 1, 1);
|
|
83 |
k2 = new KerberosKey(SERVER, PASS, 1, 1);
|
|
84 |
checkNotSame(k1, k2); // Different principal name
|
|
85 |
|
|
86 |
k2 = new KerberosKey(CLIENT, "ssap".getBytes(), 1, 1);
|
|
87 |
checkNotSame(k1, k2); // Different password
|
|
88 |
|
|
89 |
k2 = new KerberosKey(CLIENT, PASS, 2, 1);
|
|
90 |
checkNotSame(k1, k2); // Different keytype
|
|
91 |
|
|
92 |
k2 = new KerberosKey(CLIENT, PASS, 1, 2);
|
|
93 |
checkNotSame(k1, k2); // Different version
|
|
94 |
|
|
95 |
k2 = new KerberosKey(null, PASS, 1, 2);
|
|
96 |
checkNotSame(k1, k2); // null is not non-null
|
|
97 |
|
|
98 |
k1 = new KerberosKey(null, PASS, 1, 2);
|
|
99 |
checkSame(k1, k2); // null is null
|
|
100 |
|
|
101 |
checkNotSame(k1, "Another Object");
|
|
102 |
|
|
103 |
// The ticket part:
|
|
104 |
// new KerberosTicket(asn1 bytes, client, server, session key, type, flags,
|
|
105 |
// auth, start, end, renewUntil times, address)
|
|
106 |
|
|
107 |
KerberosTicket t1, t2;
|
|
108 |
|
|
109 |
byte[] ASN1 = "asn1".getBytes();
|
|
110 |
boolean[] FORWARDABLE = new boolean[] {true, true};
|
|
111 |
boolean[] ALLTRUE = new boolean[] {true, true, true, true, true, true, true, true, true, true};
|
|
112 |
Date D0 = new Date(0);
|
|
113 |
|
|
114 |
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
115 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
116 |
checkSame(t1, t1);
|
|
117 |
checkSame(t1, t2);
|
|
118 |
|
|
119 |
// destroyed tickets doesn't equal to each other
|
|
120 |
t1.destroy();
|
|
121 |
checkNotSame(t1, t2);
|
|
122 |
checkNotSame(t2, t1);
|
|
123 |
|
|
124 |
t2.destroy();
|
|
125 |
checkNotSame(t1, t2); // even if they are both destroyed
|
|
126 |
checkNotSame(t2, t1);
|
|
127 |
|
|
128 |
checkSame(t2, t2); // unless they are the same object
|
|
129 |
|
|
130 |
// a little difference means not equal
|
|
131 |
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
132 |
t2 = new KerberosTicket("asn11".getBytes(), CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
133 |
checkNotSame(t1, t2); // Different ASN1 encoding
|
|
134 |
|
|
135 |
t2 = new KerberosTicket(ASN1, new KerberosPrincipal("client1"), SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
136 |
checkNotSame(t1, t2); // Different client
|
|
137 |
|
|
138 |
t2 = new KerberosTicket(ASN1, CLIENT, new KerberosPrincipal("server1"), PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
139 |
checkNotSame(t1, t2); // Different server
|
|
140 |
|
|
141 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, "pass1".getBytes(), 1, FORWARDABLE, D0, D0, D0, D0, null);
|
|
142 |
checkNotSame(t1, t2); // Different session key
|
|
143 |
|
|
144 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 2, FORWARDABLE, D0, D0, D0, D0, null);
|
|
145 |
checkNotSame(t1, t2); // Different key type
|
|
146 |
|
|
147 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, new boolean[] {true, false}, D0, D0, D0, D0, null);
|
|
148 |
checkNotSame(t1, t2); // Different flags, not FORWARDABLE
|
|
149 |
|
|
150 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, new Date(1), D0, D0, D0, null);
|
|
151 |
checkNotSame(t1, t2); // Different authtime
|
|
152 |
|
|
153 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, new Date(1), D0, D0, null);
|
|
154 |
checkNotSame(t1, t2); // Different starttime
|
|
155 |
|
|
156 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, new Date(1), D0, null);
|
|
157 |
checkNotSame(t1, t2); // Different endtime
|
|
158 |
|
|
159 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, new InetAddress[2]);
|
|
160 |
checkNotSame(t1, t2); // Different client addresses
|
|
161 |
|
|
162 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, new Date(1), null);
|
|
163 |
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, new Date(2), null);
|
|
164 |
checkSame(t1, t2); // renewtill is ignored when RENEWABLE ticket flag is not set.
|
|
165 |
|
|
166 |
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, ALLTRUE, D0, D0, D0, new Date(1), null);
|
|
167 |
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, ALLTRUE, D0, D0, D0, new Date(2), null);
|
|
168 |
checkNotSame(t1, t2); // renewtill is used when RENEWABLE is set.
|
|
169 |
|
|
170 |
checkNotSame(t1, "Another Object");
|
|
171 |
System.out.println("Good!");
|
|
172 |
}
|
|
173 |
}
|