jdk/test/sun/security/krb5/auto/BogusKDC.java
author stefank
Mon, 04 Apr 2016 09:15:01 +0200
changeset 37241 b9961c99c356
parent 31801 1297473ab237
child 36967 d041d2e80712
permissions -rw-r--r--
8152538: UL doesn't inline the LogTagSet::is_level check Reviewed-by: mlarsson, brutisso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     1
/*
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    36
 * @bug 4515853 8075297
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
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    39
 * @run main/othervm BogusKDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    40
 */
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    41
public class BogusKDC {
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
    static final String TEST_SRC = System.getProperty("test.src", ".");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    44
    static final String HOST = "localhost";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    45
    static final String NOT_EXISTING_HOST = "not.existing.host";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    46
    static final String REALM = "TEST.REALM";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    47
    static final String USER = "USER";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    48
    static final String USER_PRINCIPAL = USER + "@" + REALM;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    49
    static final String USER_PASSWORD = "password";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    50
    static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    51
    static final String KRB5_CONF = "krb5.conf";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    52
    static final int WRONG_KDC_PORT = 21;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    53
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    54
    static final String KRB5_CONF_TEMPLATE = ""
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    55
            + "[libdefaults]\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    56
            + "default_realm = TEST.REALM\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    57
            + "max_retries = 1\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    58
            + "\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    59
            + "[realms]\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    60
            + "TEST.REALM = {\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    61
            + "    kdc = %s\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    62
            + "    kdc = localhost:%d\n"
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    63
            + "}";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    64
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    65
    public static void main(String[] args) throws LoginException, IOException {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    66
        Map<String, String> principals = new HashMap<>();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    67
        principals.put(USER_PRINCIPAL, USER_PASSWORD);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    68
        principals.put(KRBTGT_PRINCIPAL, null);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    69
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    70
        System.setProperty("java.security.krb5.conf", KRB5_CONF);
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
        // start a local KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    73
        KDC kdc = KDC.startKDC(HOST, KRB5_CONF, REALM, principals, null, null);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    74
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    75
        System.setProperty("java.security.auth.login.config",
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    76
                TEST_SRC + File.separator + "refreshKrb5Config.jaas");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    77
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    78
        CallbackHandler handler = new Helper.UserPasswordHandler(
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    79
                USER, USER_PASSWORD);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    80
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    81
        // create a krb5 config with non-existing host for master KDC,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    82
        // and wrong port for slave KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    83
        try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    84
            w.write(String.format(KRB5_CONF_TEMPLATE,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    85
                    KDC.KDCNameService.NOT_EXISTING_HOST, WRONG_KDC_PORT));
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    86
            w.flush();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    87
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    88
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    89
        // login with not-refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    90
        try {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    91
            new LoginContext("NotRefreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    92
            throw new RuntimeException("Expected exception not thrown");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    93
        } catch (LoginException le) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    94
            System.out.println("Expected login failure: " + le);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    95
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    96
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    97
        // create a krb5 config with non-existing host for master KDC,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    98
        // but correct port for slave KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    99
        try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   100
            w.write(String.format(KRB5_CONF_TEMPLATE,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   101
                    KDC.KDCNameService.NOT_EXISTING_HOST, kdc.getPort()));
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   102
            w.flush();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   103
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   104
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   105
        // login with not-refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   106
        try {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   107
            new LoginContext("NotRefreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   108
            throw new RuntimeException("Expected exception not thrown");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   109
        } catch (LoginException le) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   110
            System.out.println("Expected login failure: " + le);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   111
        }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   112
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   113
        // login with refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   114
        new LoginContext("Refreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   115
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   116
        System.out.println("Test passed");
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
}