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