Path: blob/master/test/jdk/javax/security/auth/login/LoginContext/ConfigConstructor.java
51706 views
/*1* Copyright (c) 2003, 2017, 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* @modules jdk.security.auth27* @summary can not specify Configuration to LoginContext constructor28*29* @run main/othervm/policy=ConfigConstructor.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructor30*31*/3233/**34* This test shares the login config with ConfigConstructorNoPerm.35* This test has all necessary permissions configured in the policy36* (ConfigConstructorNoPerm has no perms and checks for SecurityExceptions).37*/3839import java.util.Map;40import javax.security.auth.Subject;41import javax.security.auth.login.AppConfigurationEntry;42import javax.security.auth.login.Configuration;43import javax.security.auth.login.LoginContext;44import javax.security.auth.login.LoginException;45import javax.security.auth.spi.LoginModule;46import javax.security.auth.callback.CallbackHandler;4748public class ConfigConstructor {4950private static Subject s = new Subject();51private static CallbackHandler ch =52new com.sun.security.auth.callback.TextCallbackHandler();53private static Configuration c = new MyConfig();5455public static void main(String[] args) throws Exception {5657// test non-null behavior with provided config58LoginContext lc = new LoginContext59("module1",60s,61ch,62c);63lc.login();64System.out.println("Test 1 Passed");6566// test null behavior with provided config67LoginContext lc2 = new LoginContext68("module2",69null,70null,71c);72lc2.login();73System.out.println("Test 2 Passed");7475// test null config76LoginContext lc3 = new LoginContext77("module3",78s,79ch,80null);81lc3.login();82System.out.println("Test 3 Passed");8384// test null config85LoginContext lc4 = new LoginContext86("module4",87null,88null,89null);90lc4.login();91System.out.println("Test 4 Passed");9293// test security (without permission)94try {95LoginContext lc5 = new LoginContext96("module5",97null,98null,99c);100lc5.login();101throw new SecurityException("test failed - security check failed");102} catch (LoginException le) {103if (le.getCause() instanceof SecurityException) {104// test passed105} else {106le.printStackTrace();107throw new SecurityException("test failed: " +108"LoginException did not have chained SecurityException");109}110}111System.out.println("Test 5 Passed");112113// test security (with permission)114LoginContext lc6 = new LoginContext115("module6",116null,117null,118c);119lc6.login();120System.out.println("Test 6 Passed");121122// test other123LoginContext lc7 = new LoginContext124("goToOther",125null,126null,127c);128lc7.login();129System.out.println("Test 7 Passed");130131// test other old constructor132LoginContext lc8 = new LoginContext133("goToOther");134lc8.login();135System.out.println("Test 8 Passed");136}137138private static class MyConfig extends Configuration {139public MyConfig() { }140public AppConfigurationEntry[] getAppConfigurationEntry(String name) {141java.util.HashMap map = new java.util.HashMap();142AppConfigurationEntry[] entries = new AppConfigurationEntry[1];143144if (name.equals("module1")) {145AppConfigurationEntry entry = new AppConfigurationEntry146("ConfigConstructor$MyModule1",147AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,148map);149entries[0] = entry;150} else if (name.equals("module2")) {151AppConfigurationEntry entry = new AppConfigurationEntry152("ConfigConstructor$MyModule2",153AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,154map);155entries[0] = entry;156} else if (name.equals("module3")) {157AppConfigurationEntry entry = new AppConfigurationEntry158("ConfigConstructor$MyModule3",159AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,160map);161entries[0] = entry;162} else if (name.equals("module4")) {163AppConfigurationEntry entry = new AppConfigurationEntry164("ConfigConstructor$MyModule4",165AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,166map);167entries[0] = entry;168} else if (name.equals("module5")) {169AppConfigurationEntry entry = new AppConfigurationEntry170("ConfigConstructor$MyModule5",171AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,172map);173entries[0] = entry;174} else if (name.equals("module6")) {175AppConfigurationEntry entry = new AppConfigurationEntry176("ConfigConstructor$MyModule6",177AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,178map);179entries[0] = entry;180} else if (name.equalsIgnoreCase("other")) {181AppConfigurationEntry entry = new AppConfigurationEntry182("ConfigConstructor$MyModule2",183AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,184map);185entries[0] = entry;186} else {187entries = null;188}189return entries;190}191public void refresh() { }192}193194public static class MyModule1 implements LoginModule {195196public MyModule1() { }197198public void initialize(Subject s, CallbackHandler ch,199Map<String,?> state, Map<String,?> options) {200if (s != ConfigConstructor.s ||201ch != ConfigConstructor.ch) {202throw new SecurityException("Module 1 failed");203}204}205206public boolean login() throws LoginException { return true; }207public boolean commit() throws LoginException { return true; }208public boolean abort() throws LoginException { return true; }209public boolean logout() throws LoginException { return true; }210}211212public static class MyModule2 implements LoginModule {213214public MyModule2() { }215216public void initialize(Subject s, CallbackHandler ch,217Map<String,?> state, Map<String,?> options) {218if (s == ConfigConstructor.s ||219ch != null) {220throw new SecurityException("Module 2 failed");221}222}223224public boolean login() throws LoginException { return true; }225public boolean commit() throws LoginException { return true; }226public boolean abort() throws LoginException { return true; }227public boolean logout() throws LoginException { return true; }228}229230public static class MyModule3 implements LoginModule {231232public MyModule3() { }233234public void initialize(Subject s, CallbackHandler ch,235Map<String,?> state, Map<String,?> options) {236if (s != ConfigConstructor.s ||237ch == null ||238ch == ConfigConstructor.ch) {239throw new SecurityException("Module 3 failed");240}241}242243public boolean login() throws LoginException { return true; }244public boolean commit() throws LoginException { return true; }245public boolean abort() throws LoginException { return true; }246public boolean logout() throws LoginException { return true; }247}248249public static class MyModule4 implements LoginModule {250251public MyModule4() { }252253public void initialize(Subject s, CallbackHandler ch,254Map<String,?> state, Map<String,?> options) {255if (s == ConfigConstructor.s ||256ch != null) {257throw new SecurityException("Module 4 failed");258}259}260261public boolean login() throws LoginException { return true; }262public boolean commit() throws LoginException { return true; }263public boolean abort() throws LoginException { return true; }264public boolean logout() throws LoginException { return true; }265}266267public static class MyModule5 implements LoginModule {268269public MyModule5() { }270271public void initialize(Subject s, CallbackHandler ch,272Map<String,?> state, Map<String,?> options) { }273274public boolean login() throws LoginException {275// do something security-sensitive276System.out.println(System.getProperty("user.name"));277return true;278}279public boolean commit() throws LoginException { return true; }280public boolean abort() throws LoginException { return true; }281public boolean logout() throws LoginException { return true; }282}283284public static class MyModule6 implements LoginModule {285286public MyModule6() { }287288public void initialize(Subject s, CallbackHandler ch,289Map<String,?> state, Map<String,?> options) { }290291public boolean login() throws LoginException {292// do something security-sensitive293System.out.println(System.getProperty("user.home"));294return true;295}296public boolean commit() throws LoginException { return true; }297public boolean abort() throws LoginException { return true; }298public boolean logout() throws LoginException { return true; }299}300}301302303