jdk/test/sun/security/krb5/auto/BadKdc.java
author weijun
Wed, 07 Nov 2012 14:13:01 +0800
changeset 14413 e954df027393
parent 14342 8435a30053c1
child 23339 d66b18f52418
permissions -rw-r--r--
6355584: Introduce constrained Kerberos delegation Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13362
diff changeset
     2
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     4
 *
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     8
 *
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    13
 * accompanied this code).
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    14
 *
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4531
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4531
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4531
diff changeset
    21
 * questions.
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    22
 */
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    23
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    24
import java.io.*;
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    25
import java.net.BindException;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    26
import java.net.DatagramPacket;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    27
import java.net.DatagramSocket;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    28
import java.net.InetAddress;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    29
import java.util.regex.Matcher;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    30
import java.util.regex.Pattern;
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    31
import javax.security.auth.login.LoginException;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    32
import sun.security.krb5.Asn1Exception;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    33
import sun.security.krb5.Config;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    34
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    35
public class BadKdc {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    36
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    37
    // Matches the krb5 debug output:
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    38
    // >>> KDCCommunication: kdc=kdc.rabbit.hole UDP:14319, timeout=2000,...
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    39
    //                                               ^ kdc#         ^ timeout
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    40
    static final Pattern re = Pattern.compile(
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    41
            ">>> KDCCommunication: kdc=kdc.rabbit.hole UDP:(\\d)...., " +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    42
            "timeout=(\\d)000,");
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    43
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    44
    /*
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    45
     * There are several cases this test fails:
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    46
     *
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    47
     * 1. The random selected port is used by another process. No good way to
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    48
     * prevent this happening, coz krb5.conf must be written before KDC starts.
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    49
     * There are two different outcomes:
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    50
     *
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    51
     *  a. Cannot start the KDC. A BindException thrown.
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    52
     *  b. When trying to access a non-existing KDC, a response is received!
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    53
     *     Most likely a Asn1Exception thrown
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    54
     *
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    55
     * 2. Even if a KDC is started, and more than 20 seconds pass by, a timeout
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    56
     * can still happens for the first UDP request. In fact, the KDC did not
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    57
     * received it at all. This happens on almost all platforms, especially
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    58
     * solaris-i586 and solaris-x64.
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    59
     *
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    60
     * To avoid them:
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    61
     *
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    62
     * 1. Catch those exceptions and ignore
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    63
     *
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    64
     * 2. a. Make the timeout longer? useless
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    65
     *    b. Read the output carefully, if there is a timeout, it's OK.
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    66
     *       Just make sure the retries times and KDCs are correct.
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    67
     *       This is tough.
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    68
     *    c. Feed the KDC a UDP packet first. The current "solution".
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    69
     */
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
    70
    public static void go(String... expected)
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    71
            throws Exception {
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    72
        try {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    73
            go0(expected);
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    74
        } catch (BindException be) {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    75
            System.out.println("The random port is used by another process");
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    76
        } catch (LoginException le) {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    77
            Throwable cause = le.getCause();
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    78
            if (cause instanceof Asn1Exception) {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    79
                System.out.println("Bad packet possibly from another process");
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    80
                return;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    81
            }
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    82
            throw le;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    83
        }
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    84
    }
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    85
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
    86
    public static void go0(String... expected)
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    87
            throws Exception {
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    88
        System.setProperty("sun.security.krb5.debug", "true");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    89
12842
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
    90
        // Idle UDP sockets will trigger a SocketTimeoutException, without it,
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
    91
        // a PortUnreachableException will be thrown.
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
    92
        DatagramSocket d1 = null, d2 = null, d3 = null;
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
    93
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    94
        // Make sure KDCs' ports starts with 1 and 2 and 3,
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    95
        // useful for checking debug output.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    96
        int p1 = 10000 + new java.util.Random().nextInt(10000);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    97
        int p2 = 20000 + new java.util.Random().nextInt(10000);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    98
        int p3 = 30000 + new java.util.Random().nextInt(10000);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    99
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   100
        FileWriter fw = new FileWriter("alternative-krb5.conf");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   101
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   102
        fw.write("[libdefaults]\n" +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   103
                "default_realm = " + OneKDC.REALM + "\n" +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   104
                "kdc_timeout = 2000\n");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   105
        fw.write("[realms]\n" + OneKDC.REALM + " = {\n" +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   106
                "kdc = " + OneKDC.KDCHOST + ":" + p1 + "\n" +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   107
                "kdc = " + OneKDC.KDCHOST + ":" + p2 + "\n" +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   108
                "kdc = " + OneKDC.KDCHOST + ":" + p3 + "\n" +
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   109
                "}\n");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   110
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   111
        fw.close();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   112
        System.setProperty("java.security.krb5.conf", "alternative-krb5.conf");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   113
        Config.refresh();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   114
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   115
        // Turn on k3 only
12842
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   116
        d1 = new DatagramSocket(p1);
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   117
        d2 = new DatagramSocket(p2);
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   118
        KDC k3 = on(p3);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   119
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   120
        test(expected[0]);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   121
        test(expected[1]);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   122
        Config.refresh();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   123
        test(expected[2]);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   124
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   125
        k3.terminate(); // shutdown k3
12842
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   126
        d3 = new DatagramSocket(p3);
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   127
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   128
        d2.close();
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   129
        on(p2);         // k2 is on
12842
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   130
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   131
        test(expected[3]);
12842
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   132
        d1.close();
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   133
        on(p1);         // k1 and k2 is on
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   134
        test(expected[4]);
12842
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   135
f6f9cb8f6b97 7162687: enhance KDC server availability detection
weijun
parents: 9522
diff changeset
   136
        d3.close();
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   137
    }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   138
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   139
    private static KDC on(int p) throws Exception {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   140
        KDC k = new KDC(OneKDC.REALM, OneKDC.KDCHOST, p, true);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   141
        k.addPrincipal(OneKDC.USER, OneKDC.PASS);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   142
        k.addPrincipalRandKey("krbtgt/" + OneKDC.REALM);
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   143
        // Feed a packet to newly started KDC to warm it up
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   144
        System.err.println("-------- IGNORE THIS ERROR MESSAGE --------");
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   145
        new DatagramSocket().send(
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   146
                new DatagramPacket("Hello".getBytes(), 5,
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   147
                        InetAddress.getByName(OneKDC.KDCHOST), p));
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   148
        return k;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   149
    }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   150
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   151
    private static void test(String expected) throws Exception {
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   152
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   153
        System.out.println("----------------- TEST -----------------");
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   154
        try {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   155
            test0(bo, expected);
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   156
        } catch (Exception e) {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   157
            System.out.println("----------------- ERROR -----------------");
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   158
            System.out.println(new String(bo.toByteArray()));
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   159
            System.out.println("--------------- ERROR END ---------------");
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   160
            throw e;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   161
        }
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   162
    }
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   163
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   164
    /**
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   165
     * One round of test for max_retries and timeout.
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   166
     * @param expected the expected kdc# timeout kdc# timeout...
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   167
     */
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   168
    private static void test0(ByteArrayOutputStream bo, String expected)
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   169
            throws Exception {
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   170
        PrintStream oldout = System.out;
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   171
        boolean failed = false;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   172
        System.setOut(new PrintStream(bo));
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   173
        try {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   174
            Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   175
        } catch (Exception e) {
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   176
            failed = true;
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   177
        } finally {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   178
            System.setOut(oldout);
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   179
        }
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   180
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   181
        String[] lines = new String(bo.toByteArray()).split("\n");
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   182
        StringBuilder sb = new StringBuilder();
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   183
        for (String line: lines) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   184
            Matcher m = re.matcher(line);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   185
            if (m.find()) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   186
                System.out.println(line);
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   187
                sb.append(m.group(1)).append(m.group(2));
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   188
            }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   189
        }
13362
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   190
        if (failed) sb.append('-');
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   191
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   192
        String output = sb.toString();
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   193
        System.out.println("Expected: " + expected + ", actual " + output);
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   194
        if (!output.matches(expected)) {
ff59dd695fc0 7178649: TEST BUG: BadKdc3.java needs improvement to ignore the unlikely but possible timeout
weijun
parents: 12842
diff changeset
   195
            throw new Exception("Does not match");
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   196
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   197
    }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   198
}