Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/BogusKDC.java
38853 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.FileWriter;25import java.io.IOException;26import java.io.PrintWriter;27import java.util.HashMap;28import java.util.Map;29import javax.security.auth.callback.CallbackHandler;30import javax.security.auth.login.LoginContext;31import javax.security.auth.login.LoginException;3233/*34* @test35* @bug 4515853 807529736* @summary Checks that Kerberos client tries slave KDC37* if master KDC is not responding38* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock BogusKDC39*/40public class BogusKDC {4142static final String TEST_SRC = System.getProperty("test.src", ".");43static final String HOST = "localhost";44static final String NOT_EXISTING_HOST = "not.existing.host";45static final String REALM = "TEST.REALM";46static final String USER = "USER";47static final String USER_PRINCIPAL = USER + "@" + REALM;48static final String USER_PASSWORD = "password";49static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;50static final String KRB5_CONF = "krb5.conf";51static final int WRONG_KDC_PORT = 21;5253static final String KRB5_CONF_TEMPLATE = ""54+ "[libdefaults]\n"55+ "default_realm = TEST.REALM\n"56+ "max_retries = 1\n"57+ "\n"58+ "[realms]\n"59+ "TEST.REALM = {\n"60+ " kdc = %s\n"61+ " kdc = localhost:%d\n"62+ "}";6364public static void main(String[] args) throws LoginException, IOException {65Map<String, String> principals = new HashMap<>();66principals.put(USER_PRINCIPAL, USER_PASSWORD);67principals.put(KRBTGT_PRINCIPAL, null);6869System.setProperty("java.security.krb5.conf", KRB5_CONF);7071// start a local KDC72KDC kdc = KDC.startKDC(HOST, KRB5_CONF, REALM, principals, null, null);7374System.setProperty("java.security.auth.login.config",75TEST_SRC + File.separator + "refreshKrb5Config.jaas");7677CallbackHandler handler = new Helper.UserPasswordHandler(78USER, USER_PASSWORD);7980// create a krb5 config with non-existing host for master KDC,81// and wrong port for slave KDC82try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {83w.write(String.format(KRB5_CONF_TEMPLATE,84KDC.KDCNameService.NOT_EXISTING_HOST, WRONG_KDC_PORT));85w.flush();86}8788// login with not-refreshable config89try {90new LoginContext("NotRefreshable", handler).login();91throw new RuntimeException("Expected exception not thrown");92} catch (LoginException le) {93System.out.println("Expected login failure: " + le);94}9596// create a krb5 config with non-existing host for master KDC,97// but correct port for slave KDC98try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {99w.write(String.format(KRB5_CONF_TEMPLATE,100KDC.KDCNameService.NOT_EXISTING_HOST, kdc.getPort()));101w.flush();102}103104// login with not-refreshable config105try {106new LoginContext("NotRefreshable", handler).login();107throw new RuntimeException("Expected exception not thrown");108} catch (LoginException le) {109System.out.println("Expected login failure: " + le);110}111112// login with refreshable config113new LoginContext("Refreshable", handler).login();114115System.out.println("Test passed");116}117}118119120