Path: blob/master/test/jdk/javax/security/auth/kerberos/KerberosHashEqualsTest.java
41152 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 464182126* @summary hashCode() and equals() for KerberosKey and KerberosTicket27*/2829/*30* Must setup KDC and Kerberos configuration file31*/3233import java.net.InetAddress;34import java.util.Date;35import javax.security.auth.kerberos.*;3637public class KerberosHashEqualsTest {38public static void main(String[] args) throws Exception {39new KerberosHashEqualsTest().check();40}4142void checkSame(Object o1, Object o2) {43if(!o1.equals(o2)) {44throw new RuntimeException("equals() fails");45}46if(o1.hashCode() != o2.hashCode()) {47throw new RuntimeException("hashCode() not same");48}49}5051void checkNotSame(Object o1, Object o2) {52if(o1.equals(o2)) {53throw new RuntimeException("equals() succeeds");54}55}5657void check() throws Exception {58KerberosKey k1, k2;59k1 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1);60k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1);61checkSame(k1, k1); // me to me62checkSame(k1, k2); // same6364k2.destroy();65checkNotSame(k1, k2);66checkNotSame(k2, k1);67checkSame(k2, k2);6869k1.destroy();70checkNotSame(k1, k2);7172// Destroyed key has string and hashCode73k1.toString(); k1.hashCode();7475// a little different76k1 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 1);77k2 = new KerberosKey(newKP("B"), "pass".getBytes(), 1, 1);78checkNotSame(k1, k2);7980k2 = new KerberosKey(newKP("A"), "ssap".getBytes(), 1, 1);81checkNotSame(k1, k2);8283k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 2, 1);84checkNotSame(k1, k2);8586k2 = new KerberosKey(newKP("A"), "pass".getBytes(), 1, 2);87checkNotSame(k1, k2);8889// Null90k1 = new KerberosKey(null, "pass".getBytes(), 1, 2);91checkNotSame(k1, k2); // null to non-null92k2 = new KerberosKey(null, "pass".getBytes(), 1, 2);93checkSame(k1, k2); // null to null9495// Even key with null principal has a string and hashCode96k1.toString(); k1.hashCode();9798checkNotSame(k1, "Another Object");99100EncryptionKey e1, e2;101e1 = new EncryptionKey("pass".getBytes(), 1);102e2 = new EncryptionKey("pass".getBytes(), 1);103checkSame(e1, e1); // me to me104checkSame(e1, e2); // same105106e2.destroy();107checkNotSame(e1, e2);108checkNotSame(e2, e1);109checkSame(e2, e2);110111e1.destroy();112checkNotSame(e1, e2);113114// Destroyed key has string and hashCode115e1.toString(); e1.hashCode();116117// a little different118e1 = new EncryptionKey("pass".getBytes(), 1);119e2 = new EncryptionKey("ssap".getBytes(), 1);120checkNotSame(e1, e2);121122e2 = new EncryptionKey("pass".getBytes(), 2);123checkNotSame(e1, e2);124125checkNotSame(e1, "Another Object");126127KerberosTicket t1, t2;128t1 = 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);129t2 = 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);130checkSame(t1, t1);131checkSame(t1, t2);132t2 = 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);133checkNotSame(t1, t2);134t2 = 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);135checkNotSame(t1, t2);136t2 = 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);137checkNotSame(t1, t2);138t2 = 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);139checkNotSame(t1, t2);140t2 = 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);141checkNotSame(t1, t2);142t2 = 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);143checkNotSame(t1, t2);144t2 = 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);145checkNotSame(t1, t2);146t2 = 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);147checkNotSame(t1, t2);148t2 = 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);149checkNotSame(t1, t2);150t2 = 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]);151checkNotSame(t1, t2);152153t2 = 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);154t1 = 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);155checkSame(t1, t2); // renewtill is useless156157t2.destroy();158checkNotSame(t1, t2);159t2.hashCode(); t2.toString();160161// destroyed tickets doesn't equal to each other162checkNotSame(t2, t1);163checkSame(t2, t2);164165t2 = 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);166t1 = 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);167checkNotSame(t1, t2); // renewtill is useful168169checkNotSame(t1, "Another Object");170171KerberosCredMessage m1, m2;172m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes());173m2 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes());174checkSame(m1, m1); // me to me175checkSame(m1, m2); // same176177m2.destroy();178checkNotSame(m1, m2);179checkNotSame(m2, m1);180checkSame(m2, m2);181182m1.destroy();183checkNotSame(m1, m2);184185// Destroyed message has string and hashCode186m1.toString(); m1.hashCode();187188// a little different189m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "message".getBytes());190m2 = new KerberosCredMessage(newKP("A"), newKP("S"), "message".getBytes());191checkNotSame(m1, m2);192193m2 = new KerberosCredMessage(newKP("C"), newKP("B"), "message".getBytes());194checkNotSame(m1, m2);195196m1 = new KerberosCredMessage(newKP("C"), newKP("S"), "hello".getBytes());197checkNotSame(m1, m2);198199checkNotSame(m1, "Another Object");200201System.out.println("Good!");202}203204KerberosPrincipal newKP(String s) {205return new KerberosPrincipal(s + "@JLABS.SFBAY.SUN.COM");206}207}208209210