test/jdk/sun/security/krb5/auto/KdcPolicy.java
author weijun
Thu, 22 Mar 2018 08:09:51 +0800
changeset 49276 3acb379b8672
parent 48668 2da4a52715d8
permissions -rw-r--r--
8186228: sun/security/krb5/auto/KdcPolicy.java fails with "java.lang.Exception: Does not match. Output is c30000c30000c30000" Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     1
/*
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     4
 *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     8
 *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    13
 * accompanied this code).
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    14
 *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    18
 *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    21
 * questions.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    22
 */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    23
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    24
import java.io.*;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    25
import java.net.DatagramSocket;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    26
import java.net.ServerSocket;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    27
import java.nio.file.Files;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    28
import java.nio.file.Paths;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    29
import java.security.Security;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    30
import java.util.ArrayList;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    31
import java.util.List;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    32
import java.util.Random;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    33
import java.util.regex.Matcher;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    34
import java.util.regex.Pattern;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    35
import javax.security.auth.login.LoginException;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    36
import sun.security.krb5.Asn1Exception;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    37
import sun.security.krb5.Config;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    38
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    39
/*
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    40
 * @test
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    41
 * @bug 8164656 8181461 8194486
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    42
 * @summary krb5.kdc.bad.policy test
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    43
 * @library /test/lib
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    44
 * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    45
 * @run main/othervm -Djdk.net.hosts.file=TestHosts KdcPolicy udp
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    46
 * @run main/othervm -Djdk.net.hosts.file=TestHosts KdcPolicy tcp
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    47
 */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    48
public class KdcPolicy {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    49
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    50
    // Is this test on UDP?
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    51
    static boolean udp;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    52
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    53
    public static void main(String[] args) throws Exception {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    54
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    55
        udp = args[0].equals("udp");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    56
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    57
        try {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    58
            main0();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    59
        } catch (LoginException le) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    60
            Throwable cause = le.getCause();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    61
            if (cause instanceof Asn1Exception) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    62
                System.out.println("Another process sends a packet to " +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    63
                        "this server. Ignored.");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    64
                return;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    65
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    66
            throw le;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    67
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    68
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    69
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    70
    static DebugMatcher cm = new DebugMatcher();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    71
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    72
    static void main0() throws Exception {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    73
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    74
        System.setProperty("sun.security.krb5.debug", "true");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    75
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    76
        // One real KDC. Must be created before fake KDCs
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    77
        // to read the TestHosts file.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    78
        OneKDC kdc = new OneKDC(null);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    79
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    80
        // Two fake KDCs, d1 and d2 only listen but do not respond.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    81
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    82
        if (udp) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    83
            try (DatagramSocket d1 = new DatagramSocket();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    84
                 DatagramSocket d2 = new DatagramSocket()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    85
                run(d1.getLocalPort(), d2.getLocalPort(), kdc.getPort());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    86
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    87
        } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    88
            try (ServerSocket d1 = new ServerSocket(0);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    89
                 ServerSocket d2 = new ServerSocket(0)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    90
                run(d1.getLocalPort(), d2.getLocalPort(), kdc.getPort());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    91
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    92
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    93
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    94
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    95
    static void run(int p1, int p2, int p3) throws Exception {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    96
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    97
        // cm.kdc() will return a and b for fake KDCs, and c for real KDC.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    98
        cm.addPort(-1).addPort(p1).addPort(p2).addPort(p3);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
    99
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   100
        System.setProperty("java.security.krb5.conf", "alternative-krb5.conf");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   101
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   102
        // Check default timeout is 30s. Use real KDC only, otherwise too
49276
3acb379b8672 8186228: sun/security/krb5/auto/KdcPolicy.java fails with "java.lang.Exception: Does not match. Output is c30000c30000c30000"
weijun
parents: 48668
diff changeset
   103
        // slow to wait for timeout. Each request (without preauth and with
3acb379b8672 8186228: sun/security/krb5/auto/KdcPolicy.java fails with "java.lang.Exception: Does not match. Output is c30000c30000c30000"
weijun
parents: 48668
diff changeset
   104
        // preauth) might be retried 3 times, and could fail if one fails for
3acb379b8672 8186228: sun/security/krb5/auto/KdcPolicy.java fails with "java.lang.Exception: Does not match. Output is c30000c30000c30000"
weijun
parents: 48668
diff changeset
   105
        // all 3 times.
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   106
        writeConf(-1, -1, p3);
49276
3acb379b8672 8186228: sun/security/krb5/auto/KdcPolicy.java fails with "java.lang.Exception: Does not match. Output is c30000c30000c30000"
weijun
parents: 48668
diff changeset
   107
        test("(c30000){2,6}|(c30000){3,6}-");
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   108
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   109
        // 1. Default policy is tryLast
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   110
        //Security.setProperty("krb5.kdc.bad.policy", "tryLast");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   111
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   112
        // Need a real KDC, otherwise there is no last good.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   113
        // This test waste 3 seconds waiting for d1 to timeout.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   114
        // It is possible the real KDC cannot fulfil the request
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   115
        // in 3s, so it might fail (either 1st time or 2nd time).
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   116
        writeConf(1, 3000, p1, p3);
45519
9409e9e9ce09 8181461: sun/security/krb5/auto/KdcPolicy.java fails with java.lang.Exception: Does not match
weijun
parents: 40540
diff changeset
   117
        test("a3000c3000c3000|a3000c3000-|a3000c3000c3000a3000-");
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   118
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   119
        // If a test case won't use a real KDC, it can be sped up.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   120
        writeConf(3, 5, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   121
        test("a5a5a5b5b5b5-");  // default max_retries == 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   122
        test("a5a5a5b5b5b5-");  // all bad means no bad
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   123
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   124
        // 2. No policy.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   125
        Security.setProperty("krb5.kdc.bad.policy", "");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   126
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   127
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   128
        // This case needs a real KDC, otherwise, all bad means no
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   129
        // bad and we cannot tell the difference. This case waste 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   130
        // seconds on d1 to timeout twice. It is possible the real KDC
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   131
        // cannot fulfil the request within 3s, so it might fail
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   132
        // (either 1st time or 2nd time).
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   133
        writeConf(1, 3000, p1, p3);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   134
        test("a3000c3000a3000c3000|a3000c3000-|a3000c3000a3000c3000-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   135
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   136
        // 3. tryLess with no argument means tryLess:1,5000
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   137
        Security.setProperty("krb5.kdc.bad.policy", "tryLess");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   138
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   139
        // This case will waste 11s. We are checking that the default
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   140
        // value of 5000 in tryLess is only used if it's less than timeout
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   141
        // in krb5.conf
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   142
        writeConf(1, 6000, p1);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   143
        test("a6000-"); // timeout in krb5.conf is 6s
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   144
        test("a5000-"); // tryLess to 5s. This line can be made faster if
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   145
                        // d1 is a read KDC, but we have no existing method
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   146
                        // to start KDC on an existing ServerSocket (port).
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   147
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   148
        writeConf(-1, 4, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   149
        test("a4a4a4b4b4b4-");  // default max_retries == 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   150
        test("a4b4-");          // tryLess to 1. And since 4 < 5000, use 4.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   151
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   152
        test("a4a4a4b4b4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   153
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   154
        writeConf(5, 4, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   155
        test("a4a4a4a4a4b4b4b4b4b4-"); // user-provided max_retries == 5
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   156
        test("a4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   157
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   158
        test("a4a4a4a4a4b4b4b4b4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   159
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   160
        // 3. tryLess with arguments
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   161
        Security.setProperty("krb5.kdc.bad.policy",
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   162
                "tryLess:2,5");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   163
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   164
        writeConf(-1, 6, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   165
        test("a6a6a6b6b6b6-");  // default max_retries == 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   166
        test("a5a5b5b5-");      // tryLess to 2
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   167
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   168
        test("a6a6a6b6b6b6-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   169
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   170
        writeConf(5, 4, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   171
        test("a4a4a4a4a4b4b4b4b4b4-");  // user-provided max_retries == 5
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   172
        test("a4a4b4b4-");              // tryLess to 2
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   173
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   174
        test("a4a4a4a4a4b4b4b4b4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   175
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   176
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   177
    /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   178
     * Writes a krb5.conf file.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   179
     * @param max max_retries, -1 if not set
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   180
     * @param to kdc_timeout, -1 if not set
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   181
     * @param ports where KDCs listen on
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   182
     */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   183
    static void writeConf(int max, int to, int... ports) throws Exception {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   184
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   185
        // content of krb5.conf
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   186
        String conf = "";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   187
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   188
        // Extra settings in [libdefaults]
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   189
        String inDefaults = "";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   190
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   191
        // Extra settings in [realms]
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   192
        String inRealm = "";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   193
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   194
        // We will randomly put extra settings only in [libdefaults],
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   195
        // or in [realms] but with different values in [libdefaults],
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   196
        // to prove that settings in [realms] override those in [libdefaults].
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   197
        Random r = new Random();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   198
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   199
        if (max > 0) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   200
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   201
                inDefaults += "max_retries = " + max + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   202
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   203
                inRealm += "   max_retries = " + max + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   204
                inDefaults += "max_retries = " + (max + 1) + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   205
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   206
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   207
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   208
        if (to > 0) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   209
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   210
                inDefaults += "kdc_timeout = " + to + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   211
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   212
                inRealm += "   kdc_timeout = " + to + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   213
                inDefaults += "kdc_timeout = " + (to + 1) + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   214
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   215
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   216
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   217
        if (udp) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   218
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   219
                inDefaults += "udp_preference_limit = 10000\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   220
            } else if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   221
                inRealm += "   udp_preference_limit = 10000\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   222
                inDefaults += "udp_preference_limit = 1\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   223
            } // else no settings means UDP
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   224
        } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   225
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   226
                inDefaults += "udp_preference_limit = 1\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   227
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   228
                inRealm += "   udp_preference_limit = 1\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   229
                inDefaults += "udp_preference_limit = 10000\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   230
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   231
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   232
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   233
        conf = "[libdefaults]\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   234
                "default_realm = " + OneKDC.REALM + "\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   235
                inDefaults +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   236
                "\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   237
                "[realms]\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   238
                OneKDC.REALM + " = {\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   239
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   240
        for (int port : ports) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   241
            conf += "   kdc = " + OneKDC.KDCHOST + ":" + port + "\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   242
                    inRealm;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   243
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   244
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   245
        conf += "}\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   246
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   247
        Files.write(Paths.get("alternative-krb5.conf"), conf.getBytes());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   248
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   249
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   250
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   251
    /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   252
     * One call of krb5 login. As long as the result matches one of expected,
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   253
     * the test is considered as success. The grammar of expected is
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   254
     *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   255
     *    kdc#, timeout, kdc#, timeout, ..., optional "-" for failure
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   256
     */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   257
    static void test(String... expected) throws Exception {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   258
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   259
        System.out.println("------------------TEST----------------------");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   260
        PrintStream oldOut = System.out;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   261
        boolean failed = false;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   262
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   263
        System.setOut(new PrintStream(bo));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   264
        try {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   265
            Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   266
        } catch (Exception e) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   267
            failed = true;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   268
        } finally {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   269
            System.setOut(oldOut);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   270
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   271
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   272
        String[] lines = new String(bo.toByteArray()).split("\n");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   273
        StringBuilder sb = new StringBuilder();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   274
        for (String line: lines) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   275
            if (cm.match(line)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   276
                if (udp != cm.isUDP()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   277
                    sb.append("x");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   278
                }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   279
                sb.append(cm.kdc()).append(cm.timeout());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   280
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   281
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   282
        if (failed) sb.append('-');
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   283
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   284
        String output = sb.toString();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   285
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   286
        boolean found = false;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   287
        for (String ex : expected) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   288
            if (output.matches(ex)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   289
                System.out.println("Expected: " + ex + ", actual " + output);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   290
                found = true;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   291
                break;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   292
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   293
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   294
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   295
        if (!found) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   296
            System.out.println("--------------- ERROR START -------------");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   297
            System.out.println(new String(bo.toByteArray()));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   298
            System.out.println("--------------- ERROR END ---------------");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   299
            throw new Exception("Does not match. Output is " + output);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   300
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   301
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   302
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   303
    /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   304
     * A helper class to match the krb5 debug output:
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   305
     * >>> KDCCommunication: kdc=host UDP:11555, timeout=200,Attempt =1, #bytes=138
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   306
     *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   307
     * Example:
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   308
     *  DebugMatcher cm = new DebugMatcher();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   309
     *  cm.addPort(12345).addPort(11555);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   310
     *  for (String line : debugOutput) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   311
     *      if (cm.match(line)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   312
     *          System.out.printf("%c%d\n", cm.kdc(), cm.timeout());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   313
     *          // shows b200 for the example above
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   314
     *      }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   315
     *  }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   316
     */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   317
    static class DebugMatcher {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   318
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   319
        static final Pattern re = Pattern.compile(
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   320
                ">>> KDCCommunication: kdc=\\S+ (TCP|UDP):(\\d+), " +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   321
                        "timeout=(\\d+),Attempt\\s*=(\\d+)");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   322
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   323
        List<Integer> kdcPorts = new ArrayList<>();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   324
        Matcher matcher;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   325
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   326
        /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   327
         * Add KDC ports one by one. See {@link #kdc()}.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   328
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   329
        DebugMatcher addPort(int port) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   330
            if (port > 0) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   331
                kdcPorts.add(port);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   332
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   333
                kdcPorts.clear();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   334
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   335
            return this;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   336
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   337
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   338
        /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   339
         * When a line matches the ">>> KDCCommunication:" pattern. After a
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   340
         * match, the getters below can be called on this match.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   341
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   342
        boolean match(String line) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   343
            matcher = re.matcher(line);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   344
            return matcher.find();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   345
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   346
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   347
        /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   348
         * Protocol of this match, "UDP" or "TCP".
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   349
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   350
        boolean isUDP() {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   351
            return matcher.group(1).equals("UDP");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   352
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   353
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   354
        /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   355
         * KDC for this match, "a" for the one 1st added bt addPort(), "b"
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   356
         * for second, etc. Undefined for not added.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   357
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   358
        char kdc() {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   359
            int port = Integer.parseInt(matcher.group(2));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   360
            return (char) (kdcPorts.indexOf(port) + 'a');
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   361
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   362
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   363
        /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   364
         * Timeout value for this match.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   365
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   366
        int timeout() {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   367
            return Integer.parseInt(matcher.group(3));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   368
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   369
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   370
}