Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/ConfigConstructor.java
38861 views
/*1* Copyright (c) 2003, 2004, 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*/2223/**24* @test25* @bug 470336126* @summary can not specify Configuration to LoginContext constructor27*28* @run main/othervm/policy=ConfigConstructor.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructor29*30*/3132/**33* This test shares the login config with ConfigConstructorNoPerm.34* This test has all necessary permissions configured in the policy35* (ConfigConstructorNoPerm has no perms and checks for SecurityExceptions).36*/3738import java.util.Map;39import javax.security.auth.*;40import javax.security.auth.login.*;41import javax.security.auth.spi.*;42import javax.security.auth.callback.*;4344public class ConfigConstructor {4546private static Subject s = new Subject();47private static CallbackHandler ch =48new com.sun.security.auth.callback.TextCallbackHandler();49private static Configuration c = new MyConfig();5051public static void main(String[] args) throws Exception {5253// test non-null behavior with provided config54LoginContext lc = new LoginContext55("module1",56s,57ch,58c);59lc.login();60System.out.println("Test 1 Passed");6162// test null behavior with provided config63LoginContext lc2 = new LoginContext64("module2",65null,66null,67c);68lc2.login();69System.out.println("Test 2 Passed");7071// test null config72LoginContext lc3 = new LoginContext73("module3",74s,75ch,76null);77lc3.login();78System.out.println("Test 3 Passed");7980// test null config81LoginContext lc4 = new LoginContext82("module4",83null,84null,85null);86lc4.login();87System.out.println("Test 4 Passed");8889// test security (without permission)90try {91LoginContext lc5 = new LoginContext92("module5",93null,94null,95c);96lc5.login();97throw new SecurityException("test failed - security check failed");98} catch (LoginException le) {99if (le.getCause() instanceof SecurityException) {100// test passed101} else {102le.printStackTrace();103throw new SecurityException("test failed: " +104"LoginException did not have chained SecurityException");105}106}107System.out.println("Test 5 Passed");108109// test security (with permission)110LoginContext lc6 = new LoginContext111("module6",112null,113null,114c);115lc6.login();116System.out.println("Test 6 Passed");117118// test other119LoginContext lc7 = new LoginContext120("goToOther",121null,122null,123c);124lc7.login();125System.out.println("Test 7 Passed");126127// test other old constructor128LoginContext lc8 = new LoginContext129("goToOther");130lc8.login();131System.out.println("Test 8 Passed");132}133134private static class MyConfig extends Configuration {135public MyConfig() { }136public AppConfigurationEntry[] getAppConfigurationEntry(String name) {137java.util.HashMap map = new java.util.HashMap();138AppConfigurationEntry[] entries = new AppConfigurationEntry[1];139140if (name.equals("module1")) {141AppConfigurationEntry entry = new AppConfigurationEntry142("ConfigConstructor$MyModule1",143AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,144map);145entries[0] = entry;146} else if (name.equals("module2")) {147AppConfigurationEntry entry = new AppConfigurationEntry148("ConfigConstructor$MyModule2",149AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,150map);151entries[0] = entry;152} else if (name.equals("module3")) {153AppConfigurationEntry entry = new AppConfigurationEntry154("ConfigConstructor$MyModule3",155AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,156map);157entries[0] = entry;158} else if (name.equals("module4")) {159AppConfigurationEntry entry = new AppConfigurationEntry160("ConfigConstructor$MyModule4",161AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,162map);163entries[0] = entry;164} else if (name.equals("module5")) {165AppConfigurationEntry entry = new AppConfigurationEntry166("ConfigConstructor$MyModule5",167AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,168map);169entries[0] = entry;170} else if (name.equals("module6")) {171AppConfigurationEntry entry = new AppConfigurationEntry172("ConfigConstructor$MyModule6",173AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,174map);175entries[0] = entry;176} else if (name.equalsIgnoreCase("other")) {177AppConfigurationEntry entry = new AppConfigurationEntry178("ConfigConstructor$MyModule2",179AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,180map);181entries[0] = entry;182} else {183entries = null;184}185return entries;186}187public void refresh() { }188}189190public static class MyModule1 implements LoginModule {191192public MyModule1() { }193194public void initialize(Subject s, CallbackHandler ch,195Map<String,?> state, Map<String,?> options) {196if (s != ConfigConstructor.s ||197ch != ConfigConstructor.ch) {198throw new SecurityException("Module 1 failed");199}200}201202public boolean login() throws LoginException { return true; }203public boolean commit() throws LoginException { return true; }204public boolean abort() throws LoginException { return true; }205public boolean logout() throws LoginException { return true; }206}207208public static class MyModule2 implements LoginModule {209210public MyModule2() { }211212public void initialize(Subject s, CallbackHandler ch,213Map<String,?> state, Map<String,?> options) {214if (s == ConfigConstructor.s ||215ch != null) {216throw new SecurityException("Module 2 failed");217}218}219220public boolean login() throws LoginException { return true; }221public boolean commit() throws LoginException { return true; }222public boolean abort() throws LoginException { return true; }223public boolean logout() throws LoginException { return true; }224}225226public static class MyModule3 implements LoginModule {227228public MyModule3() { }229230public void initialize(Subject s, CallbackHandler ch,231Map<String,?> state, Map<String,?> options) {232if (s != ConfigConstructor.s ||233ch == null ||234ch == ConfigConstructor.ch) {235throw new SecurityException("Module 3 failed");236}237}238239public boolean login() throws LoginException { return true; }240public boolean commit() throws LoginException { return true; }241public boolean abort() throws LoginException { return true; }242public boolean logout() throws LoginException { return true; }243}244245public static class MyModule4 implements LoginModule {246247public MyModule4() { }248249public void initialize(Subject s, CallbackHandler ch,250Map<String,?> state, Map<String,?> options) {251if (s == ConfigConstructor.s ||252ch != null) {253throw new SecurityException("Module 4 failed");254}255}256257public boolean login() throws LoginException { return true; }258public boolean commit() throws LoginException { return true; }259public boolean abort() throws LoginException { return true; }260public boolean logout() throws LoginException { return true; }261}262263public static class MyModule5 implements LoginModule {264265public MyModule5() { }266267public void initialize(Subject s, CallbackHandler ch,268Map<String,?> state, Map<String,?> options) { }269270public boolean login() throws LoginException {271// do something security-sensitive272System.out.println(System.getProperty("user.name"));273return true;274}275public boolean commit() throws LoginException { return true; }276public boolean abort() throws LoginException { return true; }277public boolean logout() throws LoginException { return true; }278}279280public static class MyModule6 implements LoginModule {281282public MyModule6() { }283284public void initialize(Subject s, CallbackHandler ch,285Map<String,?> state, Map<String,?> options) { }286287public boolean login() throws LoginException {288// do something security-sensitive289System.out.println(System.getProperty("user.home"));290return true;291}292public boolean commit() throws LoginException { return true; }293public boolean abort() throws LoginException { return true; }294public boolean logout() throws LoginException { return true; }295}296}297298299