Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/KerberosHashEqualsTest.java
38853 views
/*1* Copyright (c) 2005, 2011, 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* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock KerberosHashEqualsTest27* @summary hashCode() and equals() for KerberosKey and KerberosTicket28*/2930import java.net.InetAddress;31import java.util.Date;32import javax.security.auth.kerberos.KerberosKey;33import javax.security.auth.kerberos.KerberosPrincipal;34import javax.security.auth.kerberos.KerberosTicket;3536public class KerberosHashEqualsTest {37public static void main(String[] args) throws Exception {38new OneKDC(null);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 {5859// The key part:60// new KerberosKey(principal, bytes, keyType, version)6162KerberosKey k1, k2;63KerberosPrincipal CLIENT = new KerberosPrincipal("client");64KerberosPrincipal SERVER = new KerberosPrincipal("server");65byte[] PASS = "pass".getBytes();6667k1 = new KerberosKey(CLIENT, PASS, 1, 1);68k2 = new KerberosKey(CLIENT, PASS, 1, 1);69checkSame(k1, k1); // me is me70checkSame(k1, k2); // same7172// A destroyed key doesn't equal to any key73k2.destroy();74checkNotSame(k1, k2);75checkNotSame(k2, k1);76k1.destroy();77checkNotSame(k1, k2); // even if they are both destroyed78checkNotSame(k2, k1);79checkSame(k2, k2);8081// a little difference means not equal82k1 = new KerberosKey(CLIENT, PASS, 1, 1);83k2 = new KerberosKey(SERVER, PASS, 1, 1);84checkNotSame(k1, k2); // Different principal name8586k2 = new KerberosKey(CLIENT, "ssap".getBytes(), 1, 1);87checkNotSame(k1, k2); // Different password8889k2 = new KerberosKey(CLIENT, PASS, 2, 1);90checkNotSame(k1, k2); // Different keytype9192k2 = new KerberosKey(CLIENT, PASS, 1, 2);93checkNotSame(k1, k2); // Different version9495k2 = new KerberosKey(null, PASS, 1, 2);96checkNotSame(k1, k2); // null is not non-null9798k1 = new KerberosKey(null, PASS, 1, 2);99checkSame(k1, k2); // null is null100101checkNotSame(k1, "Another Object");102103// The ticket part:104// new KerberosTicket(asn1 bytes, client, server, session key, type, flags,105// auth, start, end, renewUntil times, address)106107KerberosTicket t1, t2;108109byte[] ASN1 = "asn1".getBytes();110boolean[] FORWARDABLE = new boolean[] {true, true};111boolean[] ALLTRUE = new boolean[] {true, true, true, true, true, true, true, true, true, true};112Date D0 = new Date(0);113114t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);115t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);116checkSame(t1, t1);117checkSame(t1, t2);118119// destroyed tickets doesn't equal to each other120t1.destroy();121checkNotSame(t1, t2);122checkNotSame(t2, t1);123124t2.destroy();125checkNotSame(t1, t2); // even if they are both destroyed126checkNotSame(t2, t1);127128checkSame(t2, t2); // unless they are the same object129130// a little difference means not equal131t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);132t2 = new KerberosTicket("asn11".getBytes(), CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);133checkNotSame(t1, t2); // Different ASN1 encoding134135t2 = new KerberosTicket(ASN1, new KerberosPrincipal("client1"), SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);136checkNotSame(t1, t2); // Different client137138t2 = new KerberosTicket(ASN1, CLIENT, new KerberosPrincipal("server1"), PASS, 1, FORWARDABLE, D0, D0, D0, D0, null);139checkNotSame(t1, t2); // Different server140141t2 = new KerberosTicket(ASN1, CLIENT, SERVER, "pass1".getBytes(), 1, FORWARDABLE, D0, D0, D0, D0, null);142checkNotSame(t1, t2); // Different session key143144t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 2, FORWARDABLE, D0, D0, D0, D0, null);145checkNotSame(t1, t2); // Different key type146147t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, new boolean[] {true, false}, D0, D0, D0, D0, null);148checkNotSame(t1, t2); // Different flags, not FORWARDABLE149150t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, new Date(1), D0, D0, D0, null);151checkNotSame(t1, t2); // Different authtime152153t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, new Date(1), D0, D0, null);154checkNotSame(t1, t2); // Different starttime155156t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, new Date(1), D0, null);157checkNotSame(t1, t2); // Different endtime158159t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, D0, new InetAddress[2]);160checkNotSame(t1, t2); // Different client addresses161162t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, new Date(1), null);163t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, FORWARDABLE, D0, D0, D0, new Date(2), null);164checkSame(t1, t2); // renewtill is ignored when RENEWABLE ticket flag is not set.165166t2 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, ALLTRUE, D0, D0, D0, new Date(1), null);167t1 = new KerberosTicket(ASN1, CLIENT, SERVER, PASS, 1, ALLTRUE, D0, D0, D0, new Date(2), null);168checkNotSame(t1, t2); // renewtill is used when RENEWABLE is set.169170checkNotSame(t1, "Another Object");171System.out.println("Good!");172}173}174175176