1/* 2 * Copyright (c) 2005, 2014, 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 */ 29 30/* 31 * Must setup KDC and Kerberos configuration file 32 */ 33 34import java.net.InetAddress; 35import java.util.Date; 36import javax.security.auth.kerberos.*; 37 38public 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; 60 k1 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1); 61 k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1); 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 70 k1.destroy(); 71 checkNotSame(k1, k2); 72 73 // Destroyed key has string and hashCode 74 k1.toString(); k1.hashCode(); 75 76 // a little different 77 k1 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1); 78 k2 = new KerberosKey(newKP("B"), "pass".getBytes(), 1, 1); 79 checkNotSame(k1, k2); 80 81 k2 = new KerberosKey(newKP("A"), "ssap".getBytes(), 1, 1); 82 checkNotSame(k1, k2); 83 84 k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 2, 1); 85 checkNotSame(k1, k2); 86 87 k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 2); 88 checkNotSame(k1, k2); 89 90 // Null 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 96 // Even key with null principal has a string and hashCode 97 k1.toString(); k1.hashCode(); 98 99 checkNotSame(k1, "Another Object"); 100 101 EncryptionKey e1, e2; 102 e1 = new EncryptionKey("pass".getBytes(), 1); 103 e2 = new EncryptionKey("pass".getBytes(), 1); 104 checkSame(e1, e1); // me to me 105 checkSame(e1, e2); // same 106 107 e2.destroy(); 108 checkNotSame(e1, e2); 109 checkNotSame(e2, e1); 110 checkSame(e2, e2); 111 112 e1.destroy(); 113 checkNotSame(e1, e2); 114 115 // Destroyed key has string and hashCode 116 e1.toString(); e1.hashCode(); 117 118 // a little different 119 e1 = new EncryptionKey("pass".getBytes(), 1); 120 e2 = new EncryptionKey("ssap".getBytes(), 1); 121 checkNotSame(e1, e2); 122 123 e2 = new EncryptionKey("pass".getBytes(), 2); 124 checkNotSame(e1, e2); 125 126 checkNotSame(e1, "Another Object"); 127 128 KerberosTicket t1, t2; 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); 131 checkSame(t1, t1); 132 checkSame(t1, t2); 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); 134 checkNotSame(t1, t2); 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); 136 checkNotSame(t1, t2); 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); 138 checkNotSame(t1, t2); 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); 140 checkNotSame(t1, t2); 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); 142 checkNotSame(t1, t2); 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); 144 checkNotSame(t1, t2); 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); 146 checkNotSame(t1, t2); 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); 148 checkNotSame(t1, t2); 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); 150 checkNotSame(t1, t2); 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]); 152 checkNotSame(t1, t2); 153 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); 156 checkSame(t1, t2); // renewtill is useless 157 158 t2.destroy(); 159 checkNotSame(t1, t2); 160 t2.hashCode(); t2.toString(); 161 162 // destroyed tickets doesn't equal to each other 163 checkNotSame(t2, t1); 164 checkSame(t2, t2); 165 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); 168 checkNotSame(t1, t2); // renewtill is useful 169 170 checkNotSame(t1, "Another Object"); 171 172 KerberosCredMessage m1, m2; 173 m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes()); 174 m2 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes()); 175 checkSame(m1, m1); // me to me 176 checkSame(m1, m2); // same 177 178 m2.destroy(); 179 checkNotSame(m1, m2); 180 checkNotSame(m2, m1); 181 checkSame(m2, m2); 182 183 m1.destroy(); 184 checkNotSame(m1, m2); 185 186 // Destroyed message has string and hashCode 187 m1.toString(); m1.hashCode(); 188 189 // a little different 190 m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes()); 191 m2 = new KerberosCredMessage(newKP("A"), newKP("S"), "message".getBytes()); 192 checkNotSame(m1, m2); 193 194 m2 = new KerberosCredMessage(newKP("C"), newKP("B"), "message".getBytes()); 195 checkNotSame(m1, m2); 196 197 m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "hello".getBytes()); 198 checkNotSame(m1, m2); 199 200 checkNotSame(m1, "Another Object"); 201 202 System.out.println("Good!"); 203 } 204 205 KerberosPrincipal newKP(String s) { 206 return new KerberosPrincipal(s + "@JLABS.SFBAY.SUN.COM"); 207 } 208} 209