KerberosHashEqualsTest.java revision 2362
0N/A/*
0N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4641821
0N/A * @summary hashCode() and equals() for KerberosKey and KerberosTicket
0N/A */
0N/A
0N/Aimport java.net.InetAddress;
0N/Aimport java.util.Date;
0N/Aimport javax.security.auth.kerberos.KerberosKey;
0N/Aimport javax.security.auth.kerberos.KerberosPrincipal;
0N/Aimport javax.security.auth.kerberos.KerberosTicket;
0N/A
0N/Apublic class KerberosHashEqualsTest {
0N/A public static void main(String[] args) throws Exception {
0N/A new OneKDC(null);
0N/A new KerberosHashEqualsTest().check();
0N/A }
0N/A
0N/A void checkSame(Object o1, Object o2) {
0N/A if(!o1.equals(o2)) {
0N/A throw new RuntimeException("equals() fails");
0N/A }
0N/A if(o1.hashCode() != o2.hashCode()) {
0N/A throw new RuntimeException("hashCode() not same");
0N/A }
0N/A }
0N/A
0N/A void checkNotSame(Object o1, Object o2) {
0N/A if(o1.equals(o2)) {
0N/A throw new RuntimeException("equals() succeeds");
0N/A }
0N/A }
0N/A
0N/A void check() throws Exception {
0N/A
0N/A // The key part:
0N/A // new KerberosKey(principal, bytes, keyType, version)
0N/A
0N/A KerberosKey k1, k2;
0N/A KerberosPrincipal CLIENT = new KerberosPrincipal("client");
0N/A KerberosPrincipal SERVER = new KerberosPrincipal("server");
0N/A byte[] PASS = "pass".getBytes();
0N/A
0N/A k1 = new KerberosKey(CLIENT, PASS, 1, 1);
0N/A k2 = new KerberosKey(CLIENT, PASS, 1, 1);
0N/A checkSame(k1, k1); // me is me
0N/A checkSame(k1, k2); // same
0N/A
0N/A // A destroyed key doesn't equal to any key
0N/A k2.destroy();
0N/A checkNotSame(k1, k2);
0N/A checkNotSame(k2, k1);
0N/A k1.destroy();
0N/A checkNotSame(k1, k2); // even if they are both destroyed
0N/A checkNotSame(k2, k1);
0N/A checkSame(k2, k2);
0N/A
0N/A // a little difference means not equal
0N/A k1 = new KerberosKey(CLIENT, PASS, 1, 1);
k2 = new KerberosKey(SERVER, PASS, 1, 1);
checkNotSame(k1, k2); // Different principal name
k2 = new KerberosKey(CLIENT, "ssap".getBytes(), 1, 1);
checkNotSame(k1, k2); // Different password
k2 = new KerberosKey(CLIENT, PASS, 2, 1);
checkNotSame(k1, k2); // Different keytype
k2 = new KerberosKey(CLIENT, PASS, 1, 2);
checkNotSame(k1, k2); // Different version
k2 = new KerberosKey(null, PASS, 1, 2);
checkNotSame(k1, k2); // null is not non-null
k1 = new KerberosKey(null, PASS, 1, 2);
checkSame(k1, k2); // null is null
checkNotSame(k1, "Another Object");
// The ticket part:
// new KerberosTicket(asn1 bytes, client, server, session key, type, flags,
// auth, start, end, renewUntil times, address)
KerberosTicket t1, t2;
byte[] ASN1 = "asn1".getBytes();
boolean[] FORWARDABLE = new boolean[] {true, true};
boolean[] ALLTRUE = new boolean[] {true, true, true, true, true, true, true, true, true, true};
Date D0 = new Date(0);
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
checkSame(t1, t1);
checkSame(t1, t2);
// destroyed tickets doesn't equal to each other
t1.destroy();
checkNotSame(t1, t2);
checkNotSame(t2, t1);
t2.destroy();
checkNotSame(t1, t2); // even if they are both destroyed
checkNotSame(t2, t1);
checkSame(t2, t2); // unless they are the same object
// a little difference means not equal
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
t2 = new KerberosTicket("asn11".getBytes(), CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
checkNotSame(t1, t2); // Different ASN1 encoding
t2 = new KerberosTicket(ASN1, new KerberosPrincipal("client1"), SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
checkNotSame(t1, t2); // Different client
t2 = new KerberosTicket(ASN1, CLIENT, new KerberosPrincipal("server1"), PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);
checkNotSame(t1, t2); // Different server
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, "pass1".getBytes(), 1, FORWARDABLE, D0, D0, D0, D0, null);
checkNotSame(t1, t2); // Different session key
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 2, FORWARDABLE, D0, D0, D0, D0, null);
checkNotSame(t1, t2); // Different key type
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, new boolean[] {true, false}, D0, D0, D0, D0, null);
checkNotSame(t1, t2); // Different flags, not FORWARDABLE
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, new Date(1), D0, D0, D0, null);
checkNotSame(t1, t2); // Different authtime
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, new Date(1), D0, D0, null);
checkNotSame(t1, t2); // Different starttime
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, new Date(1), D0, null);
checkNotSame(t1, t2); // Different endtime
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, new InetAddress[2]);
checkNotSame(t1, t2); // Different client addresses
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, new Date(1), null);
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, new Date(2), null);
checkSame(t1, t2); // renewtill is ignored when RENEWABLE ticket flag is not set.
t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, ALLTRUE, D0, D0, D0, new Date(1), null);
t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, ALLTRUE, D0, D0, D0, new Date(2), null);
checkNotSame(t1, t2); // renewtill is used when RENEWABLE is set.
checkNotSame(t1, "Another Object");
System.out.println("Good!");
}
}