Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/DynamicConfigurationTest.java
38860 views
/*1* Copyright (c) 2004, 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.IOException;24import javax.security.auth.Subject;25import javax.security.auth.callback.Callback;26import javax.security.auth.callback.CallbackHandler;27import javax.security.auth.callback.NameCallback;28import javax.security.auth.callback.PasswordCallback;29import javax.security.auth.callback.UnsupportedCallbackException;30import javax.security.auth.login.Configuration;31import javax.security.auth.login.LoginContext;32import javax.security.auth.login.LoginException;3334/**35* @test36* @bug 8050427 470336137* @summary Test case for RFE: 4703361. Tests the Dynamic Configuration of38* Authentication Modules with different methods39* @compile SmartLoginModule.java DummyLoginModule.java MyConfiguration.java40* @run main/othervm DynamicConfigurationTest41*/42public class DynamicConfigurationTest {4344public static void main(String... args) {45String rightConfigName = "PT";46String wrongConfigName = "NT";47char[] rightPwd = new char[]{'t', 'e', 's', 't', 'P', 'a', 's', 's',48'w', 'o', 'r', 'd', '1'};49char[] wrongPwd = new char[]{'w', 'r', 'o', 'n', 'g', 'P', 'a', 's',50's','w', 'o', 'r', 'd'};5152// Test with wrong configuration name53// Expect LoginException when initiate a new LoginContext object54testConfigName(wrongConfigName, true);55System.out.println("Wrong Config Name Test passed ");5657// Spedify two loginModules: SmartLoginModule and DummyLoginModule58// Flags: required-required59// Test with right password for SmartLoginModule60// No exception is expected61Configuration cf = new MyConfiguration();62testLogin(rightConfigName, rightPwd, cf, false);63System.out.println("Positive test passed");6465// Spedify two loginModules: SmartLoginModule and DummyLoginModule66// Flags: required-required67// Test with wrong password for SmartLoginModule68// Expect LoginException by calling LoginContext.login() method69testLogin(rightConfigName, wrongPwd, cf, true);70System.out.println("Should fail test passed");7172// Spedify two loginModules: SmartLoginModule and DummyLoginModule73// Change the flags from required-required to optional-sufficient74// Test with wrong password for SmartLoginModule, while DummyLoginModule75// always passes76// No Exception is expected77cf = new MyConfiguration(true);78testLogin(rightConfigName, wrongPwd, cf, false);79System.out.println("One module fails where are other module succeeeds "80+ "Test passed with optional-sufficient flags");81}8283public static void testConfigName(String confName,84boolean expectException) {85String expectedMsg = "No LoginModules configured for " + confName;86try {87LoginContext lc = new LoginContext(confName, new Subject(),88new MyCallbackHandler(), new MyConfiguration());8990if (expectException) {91throw new RuntimeException("Wrong Config Name Test failed: "92+ "expected LoginException not thrown.");93}94} catch (LoginException le) {95if (!expectException || !le.getMessage().equals(expectedMsg)) {96System.out.println("Wrong Config Name Test failed: "97+ "received Unexpected exception.");98throw new RuntimeException(le);99}100}101}102103public static void testLogin(String confName, char[] passwd,104Configuration cf, boolean expectException) {105try {106CallbackHandler ch = new MyCallbackHandler("testUser", passwd);107LoginContext lc = new LoginContext(confName, new Subject(),108ch, cf);109lc.login();110if (expectException) {111throw new RuntimeException("Login Test failed: "112+ "expected LoginException not thrown");113}114} catch (LoginException le) {115if (!expectException) {116System.out.println("Login Test failed: "117+ "received Unexpected exception.");118throw new RuntimeException(le);119}120}121}122}123124/**125* The application simulates the CallbackHandler. It simulates! which means all126* process to get username and password is ignored. We have to take this127* approach for automation purpose. So, this is not a real world example at all.128*/129class MyCallbackHandler implements CallbackHandler {130131String userName;132char[] password;133134/**135* This is simply a workaround approach for IO approach to get username and136* password. For automation purpose only.137*/138public MyCallbackHandler() {139super();140}141142public MyCallbackHandler(String username, char[] password) {143super();144userName = username;145this.password = password;146}147148@Override149public void handle(Callback[] callbacks) throws IOException,150UnsupportedCallbackException {151for (Callback callback : callbacks) {152if (callback instanceof NameCallback) {153NameCallback nc = (NameCallback) callback;154nc.setName(userName);155} else if (callback instanceof PasswordCallback) {156PasswordCallback pc = (PasswordCallback) callback;157pc.setPassword(password);158} else {159throw new UnsupportedCallbackException(callback,160"Unrecognized Callback");161}162}163}164}165166167