jdk/test/javax/security/auth/kerberos/KerberosHashEqualsTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 23357 7225e28e91dd
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4641821
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary hashCode() and equals() for KerberosKey and KerberosTicket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @ignore Must set up KDC and setup Kerberos configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.kerberos.KerberosKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.kerberos.KerberosPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.kerberos.KerberosTicket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class KerberosHashEqualsTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        new KerberosHashEqualsTest().check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    void checkSame(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if(!o1.equals(o2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            throw new RuntimeException("equals() fails");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if(o1.hashCode() != o2.hashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            throw new RuntimeException("hashCode() not same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    void checkNotSame(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if(o1.equals(o2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new RuntimeException("equals() succeeds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    void check() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        KerberosKey k1, k2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        k1 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        k2 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        checkSame(k1, k1);  // me to me
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        checkSame(k1, k2);  // same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        k2.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        checkNotSame(k1, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // destroyed keys doesn't equal to each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        checkNotSame(k2, k1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        checkSame(k2, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // a little different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        k2 = new KerberosKey(new KerberosPrincipal("B"), "pass".getBytes(), 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        checkNotSame(k1, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        k2 = new KerberosKey(new KerberosPrincipal("A"), "ssap".getBytes(), 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        checkNotSame(k1, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        k2 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        checkNotSame(k1, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        k2 = new KerberosKey(new KerberosPrincipal("A"), "pass".getBytes(), 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        checkNotSame(k1, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        k1 = new KerberosKey(null, "pass".getBytes(), 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        checkNotSame(k1, k2); // null to non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        k2 = new KerberosKey(null, "pass".getBytes(), 1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        checkSame(k1, k2);    // null to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        checkNotSame(k1, "Another Object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        KerberosTicket t1, t2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        t1 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        checkSame(t1, t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        checkSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        t2 = new KerberosTicket("asn11".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client1"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server1"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass1".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 2, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {false, true}, new Date(0), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(1), new Date(0), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(1), new Date(0), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(1), new Date(0), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(0), new InetAddress[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(1), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        t1 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("server"), "pass".getBytes(), 1, new boolean[] {true, true}, new Date(0), new Date(0), new Date(0), new Date(2), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        checkSame(t1, t2);  // renewtill is useless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        t2.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        checkNotSame(t1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // destroyed tickets doesn't equal to each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        checkNotSame(t2, t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        checkSame(t2, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        t2 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        t1 = new KerberosTicket("asn1".getBytes(), new KerberosPrincipal("client"), new KerberosPrincipal("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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        checkNotSame(t1, t2);  // renewtill is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        checkNotSame(t1, "Another Object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println("Good!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}