test/jdk/sun/security/krb5/auto/RefreshKrb5Config.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.IOException;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    26
import java.util.HashMap;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    27
import java.util.Map;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    28
import javax.security.auth.callback.CallbackHandler;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    29
import javax.security.auth.login.LoginContext;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    30
import javax.security.auth.login.LoginException;
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    31
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    32
/*
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    33
 * @test
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    34
 * @bug 4745056 8075297 8194486
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    35
 * @summary Checks if refreshKrb5Config is set to true for Krb5LoginModule,
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    36
 *          then configuration will be refreshed before login() method is called
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    37
 * @library /test/lib
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    38
 * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47216
diff changeset
    39
 * @run main/othervm -Djdk.net.hosts.file=TestHosts RefreshKrb5Config
31801
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 RefreshKrb5Config {
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_FILENAME = "krb5.conf";
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    52
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    53
    public static void main(String[] args) throws LoginException, IOException {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    54
        Map<String, String> principals = new HashMap<>();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    55
        principals.put(USER_PRINCIPAL, USER_PASSWORD);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    56
        principals.put(KRBTGT_PRINCIPAL, null);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    57
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    58
        System.setProperty("java.security.krb5.conf", KRB5_CONF_FILENAME);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    59
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    60
        // start a local KDC, and save krb5 config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    61
        KDC kdc = KDC.startKDC(HOST, null, REALM, principals, null, null);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    62
        KDC.saveConfig(KRB5_CONF_FILENAME, kdc, "max_retries = 1");
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
        System.setProperty("java.security.auth.login.config",
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    65
                TEST_SRC + File.separator + "refreshKrb5Config.jaas");
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
        CallbackHandler handler = new Helper.UserPasswordHandler(
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    68
                USER, USER_PASSWORD);
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
        // set incorrect KDC
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    71
        System.out.println("java.security.krb5.kdc = " + NOT_EXISTING_HOST);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    72
        System.setProperty("java.security.krb5.kdc", NOT_EXISTING_HOST);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    73
        System.out.println("java.security.krb5.realm = " + REALM);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    74
        System.setProperty("java.security.krb5.realm", REALM);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    75
        try {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    76
            new LoginContext("Refreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    77
            throw new RuntimeException("Expected exception not thrown");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    78
        } catch (LoginException le) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    79
            System.out.println("Expected login failure: " + le);
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
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    82
        // reset properties
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    83
        System.out.println("Reset java.security.krb5.kdc");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    84
        System.clearProperty("java.security.krb5.kdc");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    85
        System.out.println("Reset java.security.krb5.realm");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    86
        System.clearProperty("java.security.krb5.realm");
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
        // login with not-refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    89
        try {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    90
            new LoginContext("NotRefreshable", handler).login();
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    91
            throw new RuntimeException("Expected exception not thrown");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    92
        } catch (LoginException le) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    93
            System.out.println("Expected login failure: " + le);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    94
        }
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
        // login with refreshable config
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
    97
        new LoginContext("Refreshable", handler).login();
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
        System.out.println("Test passed");
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   100
    }
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   101
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents:
diff changeset
   102
}