test/jdk/sun/security/krb5/auto/BogusKDC.java
author ssahoo
Thu, 25 Jan 2018 05:57:22 -0800
changeset 48668 2da4a52715d8
parent 47216 71c04702a3d5
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:
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     1
/*
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     4
 *
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     8
 *
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    13
 * accompanied this code).
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    14
 *
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    18
 *
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    21
 * questions.
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    22
 */
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    23
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    24
import java.io.File;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    25
import java.io.FileWriter;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    26
import java.io.IOException;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    27
import java.io.PrintWriter;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    28
import java.util.HashMap;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    29
import java.util.Map;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    30
import javax.security.auth.callback.CallbackHandler;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    31
import javax.security.auth.login.LoginContext;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    32
import javax.security.auth.login.LoginException;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    33
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    34
/*
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    35
 * @test
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    36
 * @bug 4515853 8075297 8194486
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    37
 * @summary Checks that Kerberos client tries slave KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    38
 *          if master KDC is not responding
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    39
 * @library /test/lib
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    40
 * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    41
 * @run main/othervm -Djdk.net.hosts.file=TestHosts BogusKDC
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    42
 */
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    43
public class BogusKDC {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    44
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    45
    static final String TEST_SRC = System.getProperty("test.src", ".");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    46
    static final String HOST = "localhost";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    47
    static final String NOT_EXISTING_HOST = "not.existing.host";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    48
    static final String REALM = "TEST.REALM";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    49
    static final String USER = "USER";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    50
    static final String USER_PRINCIPAL = USER + "@" + REALM;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    51
    static final String USER_PASSWORD = "password";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    52
    static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    53
    static final String KRB5_CONF = "krb5.conf";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    54
    static final int WRONG_KDC_PORT = 21;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    55
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    56
    static final String KRB5_CONF_TEMPLATE = ""
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    57
            + "[libdefaults]\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    58
            + "default_realm = TEST.REALM\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    59
            + "max_retries = 1\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    60
            + "\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    61
            + "[realms]\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    62
            + "TEST.REALM = {\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    63
            + "    kdc = %s\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    64
            + "    kdc = localhost:%d\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    65
            + "}";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    66
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    67
    public static void main(String[] args) throws LoginException, IOException {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    68
        Map<String, String> principals = new HashMap<>();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    69
        principals.put(USER_PRINCIPAL, USER_PASSWORD);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    70
        principals.put(KRBTGT_PRINCIPAL, null);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    71
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    72
        System.setProperty("java.security.krb5.conf", KRB5_CONF);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    73
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    74
        // start a local KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    75
        KDC kdc = KDC.startKDC(HOST, KRB5_CONF, REALM, principals, null, null);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    76
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    77
        System.setProperty("java.security.auth.login.config",
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    78
                TEST_SRC + File.separator + "refreshKrb5Config.jaas");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    79
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    80
        CallbackHandler handler = new Helper.UserPasswordHandler(
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    81
                USER, USER_PASSWORD);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    82
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    83
        // create a krb5 config with non-existing host for master KDC,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    84
        // and wrong port for slave KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    85
        try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    86
            w.write(String.format(KRB5_CONF_TEMPLATE,
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 31801
diff changeset
    87
                    KDC.NOT_EXISTING_HOST, WRONG_KDC_PORT));
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    88
            w.flush();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    89
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    90
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    91
        // login with not-refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    92
        try {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    93
            new LoginContext("NotRefreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    94
            throw new RuntimeException("Expected exception not thrown");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    95
        } catch (LoginException le) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    96
            System.out.println("Expected login failure: " + le);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    97
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    98
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    99
        // create a krb5 config with non-existing host for master KDC,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   100
        // but correct port for slave KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   101
        try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   102
            w.write(String.format(KRB5_CONF_TEMPLATE,
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 31801
diff changeset
   103
                    KDC.NOT_EXISTING_HOST, kdc.getPort()));
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   104
            w.flush();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   105
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   106
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   107
        // login with not-refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   108
        try {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   109
            new LoginContext("NotRefreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   110
            throw new RuntimeException("Expected exception not thrown");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   111
        } catch (LoginException le) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   112
            System.out.println("Expected login failure: " + le);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   113
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   114
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   115
        // login with refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   116
        new LoginContext("Refreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   117
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   118
        System.out.println("Test passed");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   119
    }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   120
}