author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23357
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4641821 |
|
27 |
* @summary hashCode() and equals() for KerberosKey and KerberosTicket |
|
23357
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
28 |
*/ |
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
29 |
|
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
30 |
/* |
7225e28e91dd
8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents:
5506
diff
changeset
|
31 |
* Must setup KDC and Kerberos configuration file |
2 | 32 |
*/ |
33 |
||
34 |
import java.net.InetAddress; |
|
35 |
import java.util.Date; |
|
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
36 |
import javax.security.auth.kerberos.*; |
2 | 37 |
|
38 |
public class KerberosHashEqualsTest { |
|
39 |
public static void main(String[] args) throws Exception { |
|
40 |
new KerberosHashEqualsTest().check(); |
|
41 |
} |
|
42 |
||
43 |
void checkSame(Object o1, Object o2) { |
|
44 |
if(!o1.equals(o2)) { |
|
45 |
throw new RuntimeException("equals() fails"); |
|
46 |
} |
|
47 |
if(o1.hashCode() != o2.hashCode()) { |
|
48 |
throw new RuntimeException("hashCode() not same"); |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
void checkNotSame(Object o1, Object o2) { |
|
53 |
if(o1.equals(o2)) { |
|
54 |
throw new RuntimeException("equals() succeeds"); |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
void check() throws Exception { |
|
59 |
KerberosKey k1, k2; |
|
23735 | 60 |
k1 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1); |
61 |
k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1); |
|
2 | 62 |
checkSame(k1, k1); // me to me |
63 |
checkSame(k1, k2); // same |
|
64 |
||
65 |
k2.destroy(); |
|
66 |
checkNotSame(k1, k2); |
|
67 |
checkNotSame(k2, k1); |
|
68 |
checkSame(k2, k2); |
|
69 |
||
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
70 |
k1.destroy(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
71 |
checkNotSame(k1, k2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
72 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
73 |
// Destroyed key has string and hashCode |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
74 |
k1.toString(); k1.hashCode(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
75 |
|
2 | 76 |
// a little different |
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
77 |
k1 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1); |
23735 | 78 |
k2 = new KerberosKey(newKP("B"), "pass".getBytes(), 1, 1); |
2 | 79 |
checkNotSame(k1, k2); |
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
80 |
|
23735 | 81 |
k2 = new KerberosKey(newKP("A"), "ssap".getBytes(), 1, 1); |
2 | 82 |
checkNotSame(k1, k2); |
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
83 |
|
23735 | 84 |
k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 2, 1); |
2 | 85 |
checkNotSame(k1, k2); |
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
86 |
|
23735 | 87 |
k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 2); |
2 | 88 |
checkNotSame(k1, k2); |
89 |
||
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
90 |
// Null |
2 | 91 |
k1 = new KerberosKey(null, "pass".getBytes(), 1, 2); |
92 |
checkNotSame(k1, k2); // null to non-null |
|
93 |
k2 = new KerberosKey(null, "pass".getBytes(), 1, 2); |
|
94 |
checkSame(k1, k2); // null to null |
|
95 |
||
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
96 |
// Even key with null principal has a string and hashCode |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
97 |
k1.toString(); k1.hashCode(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
98 |
|
2 | 99 |
checkNotSame(k1, "Another Object"); |
100 |
||
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
101 |
EncryptionKey e1, e2; |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
102 |
e1 = new EncryptionKey("pass".getBytes(), 1); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
103 |
e2 = new EncryptionKey("pass".getBytes(), 1); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
104 |
checkSame(e1, e1); // me to me |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
105 |
checkSame(e1, e2); // same |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
106 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
107 |
e2.destroy(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
108 |
checkNotSame(e1, e2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
109 |
checkNotSame(e2, e1); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
110 |
checkSame(e2, e2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
111 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
112 |
e1.destroy(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
113 |
checkNotSame(e1, e2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
114 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
115 |
// Destroyed key has string and hashCode |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
116 |
e1.toString(); e1.hashCode(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
117 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
118 |
// a little different |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
119 |
e1 = new EncryptionKey("pass".getBytes(), 1); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
120 |
e2 = new EncryptionKey("ssap".getBytes(), 1); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
121 |
checkNotSame(e1, e2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
122 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
123 |
e2 = new EncryptionKey("pass".getBytes(), 2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
124 |
checkNotSame(e1, e2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
125 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
126 |
checkNotSame(e1, "Another Object"); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
127 |
|
2 | 128 |
KerberosTicket t1, t2; |
23735 | 129 |
t1 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
130 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
|
2 | 131 |
checkSame(t1, t1); |
132 |
checkSame(t1, t2); |
|
23735 | 133 |
t2 = new KerberosTicket("asn11".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
2 | 134 |
checkNotSame(t1, t2); |
23735 | 135 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client1"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
2 | 136 |
checkNotSame(t1, t2); |
23735 | 137 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server1"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
2 | 138 |
checkNotSame(t1, t2); |
23735 | 139 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass1".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
2 | 140 |
checkNotSame(t1, t2); |
23735 | 141 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 2, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
2 | 142 |
checkNotSame(t1, t2); |
23735 | 143 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {false, true}, new Date(0), new Date(0), new Date(0), new Date(0), null); |
2 | 144 |
checkNotSame(t1, t2); |
23735 | 145 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(1), new Date(0), new Date(0), new Date(0), null); |
2 | 146 |
checkNotSame(t1, t2); |
23735 | 147 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(1), new Date(0), new Date(0), null); |
2 | 148 |
checkNotSame(t1, t2); |
23735 | 149 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(1), new Date(0), null); |
2 | 150 |
checkNotSame(t1, t2); |
23735 | 151 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), new InetAddress[2]); |
2 | 152 |
checkNotSame(t1, t2); |
153 |
||
23735 | 154 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(1), null); |
155 |
t1 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(2), null); |
|
2 | 156 |
checkSame(t1, t2); // renewtill is useless |
157 |
||
158 |
t2.destroy(); |
|
159 |
checkNotSame(t1, t2); |
|
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
160 |
t2.hashCode(); t2.toString(); |
2 | 161 |
|
162 |
// destroyed tickets doesn't equal to each other |
|
163 |
checkNotSame(t2, t1); |
|
164 |
checkSame(t2, t2); |
|
165 |
||
23735 | 166 |
t2 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("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); |
167 |
t1 = new KerberosTicket("asn1".getBytes(), newKP("client"), newKP("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); |
|
2 | 168 |
checkNotSame(t1, t2); // renewtill is useful |
169 |
||
170 |
checkNotSame(t1, "Another Object"); |
|
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
171 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
172 |
KerberosCredMessage m1, m2; |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
173 |
m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
174 |
m2 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
175 |
checkSame(m1, m1); // me to me |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
176 |
checkSame(m1, m2); // same |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
177 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
178 |
m2.destroy(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
179 |
checkNotSame(m1, m2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
180 |
checkNotSame(m2, m1); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
181 |
checkSame(m2, m2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
182 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
183 |
m1.destroy(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
184 |
checkNotSame(m1, m2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
185 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
186 |
// Destroyed message has string and hashCode |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
187 |
m1.toString(); m1.hashCode(); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
188 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
189 |
// a little different |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
190 |
m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
191 |
m2 = new KerberosCredMessage(newKP("A"), newKP("S"), "message".getBytes()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
192 |
checkNotSame(m1, m2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
193 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
194 |
m2 = new KerberosCredMessage(newKP("C"), newKP("B"), "message".getBytes()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
195 |
checkNotSame(m1, m2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
196 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
197 |
m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "hello".getBytes()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
198 |
checkNotSame(m1, m2); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
199 |
|
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
200 |
checkNotSame(m1, "Another Object"); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23735
diff
changeset
|
201 |
|
2 | 202 |
System.out.println("Good!"); |
203 |
} |
|
23735 | 204 |
|
205 |
KerberosPrincipal newKP(String s) { |
|
206 |
return new KerberosPrincipal(s + "@JLABS.SFBAY.SUN.COM"); |
|
207 |
} |
|
2 | 208 |
} |