jdk/test/sun/security/krb5/auto/BadKdc.java
author weijun
Wed, 23 May 2012 15:51:10 +0800
changeset 12842 f6f9cb8f6b97
parent 9522 38e96027251b
child 13362 ff59dd695fc0
permissions -rw-r--r--
7162687: enhance KDC server availability detection 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
/*
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2009, 2011, 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
     */
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
    70
    public static void go(int[]... expected)
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
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
    86
    public static void go0(int[]... expected)
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
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   151
    private static void test(int... expected) throws Exception {
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();
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   153
        try {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   154
            test0(bo, expected);
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   155
        } catch (Exception e) {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   156
            System.out.println("----------------- ERROR -----------------");
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   157
            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
   158
            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
   159
            throw e;
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   160
        }
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
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   163
    /**
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   164
     * 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
   165
     * @param expected the expected kdc# timeout kdc# timeout...
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   166
     */
9522
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   167
    private static void test0(ByteArrayOutputStream bo, int... expected)
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   168
            throws Exception {
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   169
        PrintStream oldout = System.out;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   170
        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
   171
        try {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   172
            Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   173
        } finally {
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   174
            System.setOut(oldout);
38e96027251b 6950929: Failures on Solaris sparc 64bit sun/security/krb5/auto/BadKdc4.java (and linux?)
weijun
parents: 5506
diff changeset
   175
        }
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   176
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   177
        String[] lines = new String(bo.toByteArray()).split("\n");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   178
        System.out.println("----------------- TEST -----------------");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   179
        int count = 0;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   180
        for (String line: lines) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   181
            Matcher m = re.matcher(line);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   182
            if (m.find()) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   183
                System.out.println(line);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   184
                if (Integer.parseInt(m.group(1)) != expected[count++] ||
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   185
                        Integer.parseInt(m.group(2)) != expected[count++]) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   186
                    throw new Exception("Fail here");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   187
                }
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
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   190
        if (count != expected.length) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   191
            throw new Exception("Less rounds");
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   192
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   193
    }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
diff changeset
   194
}