test/jdk/sun/security/krb5/auto/KdcPolicy.java
author ssahoo
Thu, 25 Jan 2018 05:57:22 -0800
changeset 48668 2da4a52715d8
parent 47216 71c04702a3d5
child 49276 3acb379b8672
permissions -rw-r--r--
8194486: Several krb5 tests failed in Mac. Summary: Several tests failed due to improper host service Reviewed-by: weijun
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
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   103
        // slow to wait for timeout.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   104
        writeConf(-1, -1, p3);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   105
        test("c30000c30000");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   106
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   107
        // 1. Default policy is tryLast
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   108
        //Security.setProperty("krb5.kdc.bad.policy", "tryLast");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   109
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   110
        // Need a real KDC, otherwise there is no last good.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   111
        // This test waste 3 seconds waiting for d1 to timeout.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   112
        // It is possible the real KDC cannot fulfil the request
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   113
        // 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
   114
        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
   115
        test("a3000c3000c3000|a3000c3000-|a3000c3000c3000a3000-");
40540
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   116
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   117
        // 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
   118
        writeConf(3, 5, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   119
        test("a5a5a5b5b5b5-");  // default max_retries == 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   120
        test("a5a5a5b5b5b5-");  // all bad means no bad
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   121
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   122
        // 2. No policy.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   123
        Security.setProperty("krb5.kdc.bad.policy", "");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   124
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   125
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   126
        // 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
   127
        // 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
   128
        // 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
   129
        // cannot fulfil the request within 3s, so it might fail
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   130
        // (either 1st time or 2nd time).
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   131
        writeConf(1, 3000, p1, p3);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   132
        test("a3000c3000a3000c3000|a3000c3000-|a3000c3000a3000c3000-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   133
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   134
        // 3. tryLess with no argument means tryLess:1,5000
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   135
        Security.setProperty("krb5.kdc.bad.policy", "tryLess");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   136
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   137
        // 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
   138
        // 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
   139
        // in krb5.conf
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   140
        writeConf(1, 6000, p1);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   141
        test("a6000-"); // timeout in krb5.conf is 6s
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   142
        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
   143
                        // 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
   144
                        // to start KDC on an existing ServerSocket (port).
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   145
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   146
        writeConf(-1, 4, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   147
        test("a4a4a4b4b4b4-");  // default max_retries == 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   148
        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
   149
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   150
        test("a4a4a4b4b4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   151
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   152
        writeConf(5, 4, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   153
        test("a4a4a4a4a4b4b4b4b4b4-"); // user-provided max_retries == 5
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   154
        test("a4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   155
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   156
        test("a4a4a4a4a4b4b4b4b4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   157
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   158
        // 3. tryLess with arguments
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   159
        Security.setProperty("krb5.kdc.bad.policy",
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   160
                "tryLess:2,5");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   161
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   162
        writeConf(-1, 6, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   163
        test("a6a6a6b6b6b6-");  // default max_retries == 3
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   164
        test("a5a5b5b5-");      // tryLess to 2
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   165
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   166
        test("a6a6a6b6b6b6-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   167
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   168
        writeConf(5, 4, p1, p2);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   169
        test("a4a4a4a4a4b4b4b4b4b4-");  // user-provided max_retries == 5
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   170
        test("a4a4b4b4-");              // tryLess to 2
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   171
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   172
        test("a4a4a4a4a4b4b4b4b4b4-");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   173
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   174
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
     * Writes a krb5.conf file.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   177
     * @param max max_retries, -1 if not set
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   178
     * @param to kdc_timeout, -1 if not set
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   179
     * @param ports where KDCs listen on
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   180
     */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   181
    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
   182
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   183
        // content of krb5.conf
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   184
        String conf = "";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   185
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   186
        // Extra settings in [libdefaults]
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   187
        String inDefaults = "";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   188
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   189
        // Extra settings in [realms]
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   190
        String inRealm = "";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   191
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   192
        // We will randomly put extra settings only in [libdefaults],
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   193
        // or in [realms] but with different values in [libdefaults],
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   194
        // to prove that settings in [realms] override those in [libdefaults].
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   195
        Random r = new Random();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   196
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   197
        if (max > 0) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   198
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   199
                inDefaults += "max_retries = " + max + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   200
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   201
                inRealm += "   max_retries = " + max + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   202
                inDefaults += "max_retries = " + (max + 1) + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   203
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   204
        }
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
        if (to > 0) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   207
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   208
                inDefaults += "kdc_timeout = " + to + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   209
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   210
                inRealm += "   kdc_timeout = " + to + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   211
                inDefaults += "kdc_timeout = " + (to + 1) + "\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   212
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   213
        }
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
        if (udp) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   216
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   217
                inDefaults += "udp_preference_limit = 10000\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   218
            } else if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   219
                inRealm += "   udp_preference_limit = 10000\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   220
                inDefaults += "udp_preference_limit = 1\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   221
            } // else no settings means UDP
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   222
        } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   223
            if (r.nextBoolean()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   224
                inDefaults += "udp_preference_limit = 1\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   225
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   226
                inRealm += "   udp_preference_limit = 1\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   227
                inDefaults += "udp_preference_limit = 10000\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   228
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   229
        }
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
        conf = "[libdefaults]\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   232
                "default_realm = " + OneKDC.REALM + "\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   233
                inDefaults +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   234
                "\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   235
                "[realms]\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   236
                OneKDC.REALM + " = {\n";
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   237
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   238
        for (int port : ports) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   239
            conf += "   kdc = " + OneKDC.KDCHOST + ":" + port + "\n" +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   240
                    inRealm;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   241
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   242
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   243
        conf += "}\n";
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
        Files.write(Paths.get("alternative-krb5.conf"), conf.getBytes());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   246
        Config.refresh();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   247
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   248
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
     * 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
   251
     * 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
   252
     *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   253
     *    kdc#, timeout, kdc#, timeout, ..., optional "-" for failure
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
    static void test(String... expected) throws Exception {
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
        System.out.println("------------------TEST----------------------");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   258
        PrintStream oldOut = System.out;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   259
        boolean failed = false;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   260
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   261
        System.setOut(new PrintStream(bo));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   262
        try {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   263
            Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   264
        } catch (Exception e) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   265
            failed = true;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   266
        } finally {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   267
            System.setOut(oldOut);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   268
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   269
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   270
        String[] lines = new String(bo.toByteArray()).split("\n");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   271
        StringBuilder sb = new StringBuilder();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   272
        for (String line: lines) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   273
            if (cm.match(line)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   274
                if (udp != cm.isUDP()) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   275
                    sb.append("x");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   276
                }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   277
                sb.append(cm.kdc()).append(cm.timeout());
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
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   280
        if (failed) sb.append('-');
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
        String output = sb.toString();
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
        boolean found = false;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   285
        for (String ex : expected) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   286
            if (output.matches(ex)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   287
                System.out.println("Expected: " + ex + ", actual " + output);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   288
                found = true;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   289
                break;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   290
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   291
        }
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
        if (!found) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   294
            System.out.println("--------------- ERROR START -------------");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   295
            System.out.println(new String(bo.toByteArray()));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   296
            System.out.println("--------------- ERROR END ---------------");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   297
            throw new Exception("Does not match. Output is " + output);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   298
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   299
    }
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
     * A helper class to match the krb5 debug output:
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   303
     * >>> 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
   304
     *
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   305
     * Example:
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   306
     *  DebugMatcher cm = new DebugMatcher();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   307
     *  cm.addPort(12345).addPort(11555);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   308
     *  for (String line : debugOutput) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   309
     *      if (cm.match(line)) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   310
     *          System.out.printf("%c%d\n", cm.kdc(), cm.timeout());
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   311
     *          // shows b200 for the example above
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   312
     *      }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   313
     *  }
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
    static class DebugMatcher {
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 final Pattern re = Pattern.compile(
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   318
                ">>> KDCCommunication: kdc=\\S+ (TCP|UDP):(\\d+), " +
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   319
                        "timeout=(\\d+),Attempt\\s*=(\\d+)");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   320
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   321
        List<Integer> kdcPorts = new ArrayList<>();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   322
        Matcher matcher;
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   323
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   324
        /**
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   325
         * Add KDC ports one by one. See {@link #kdc()}.
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
        DebugMatcher addPort(int port) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   328
            if (port > 0) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   329
                kdcPorts.add(port);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   330
            } else {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   331
                kdcPorts.clear();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   332
            }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   333
            return this;
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
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
         * When a line matches the ">>> KDCCommunication:" pattern. After a
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   338
         * match, the getters below can be called on this match.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   339
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   340
        boolean match(String line) {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   341
            matcher = re.matcher(line);
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   342
            return matcher.find();
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   343
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   344
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
         * Protocol of this match, "UDP" or "TCP".
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
        boolean isUDP() {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   349
            return matcher.group(1).equals("UDP");
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   350
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   351
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
         * 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
   354
         * for second, etc. Undefined for not added.
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   355
         */
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   356
        char kdc() {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   357
            int port = Integer.parseInt(matcher.group(2));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   358
            return (char) (kdcPorts.indexOf(port) + 'a');
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   359
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   360
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
         * Timeout value for this match.
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
        int timeout() {
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   365
            return Integer.parseInt(matcher.group(3));
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   366
        }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   367
    }
49fd4217ac48 8164656: krb5 does not retry if TCP connection timeouts
weijun
parents:
diff changeset
   368
}