Path: blob/master/test/jdk/javax/security/auth/login/LoginContext/ConfigConstructorNoPerm.java
51705 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=ConfigConstructorNoPerm.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructorNoPerm30*/3132/**33* This test shares the login config with ConfigConstructor.34* This test has no configured permissions35* (ConfigConstructor tests code with perms configured).36*/3738import java.util.Map;39import javax.security.auth.Subject;40import javax.security.auth.login.AppConfigurationEntry;41import javax.security.auth.login.Configuration;42import javax.security.auth.login.LoginContext;43import javax.security.auth.callback.CallbackHandler;4445public class ConfigConstructorNoPerm {4647private static Subject s = new Subject();48private static CallbackHandler ch =49new com.sun.security.auth.callback.TextCallbackHandler();50private static Configuration c = new MyConfig();5152public static void main(String[] args) throws Exception {5354// test old constructor with no permission55try {56LoginContext lc1 = new LoginContext57("module1",58s,59ch);60throw new RuntimeException("Test 1 Failed");61} catch (SecurityException se) {62// test passed63}64System.out.println("Test 1 Succeeded");6566// test new constructor (null config) with no permission67try {68LoginContext lc2 = new LoginContext69("module1",70s,71ch,72null);73throw new RuntimeException("Test 2 Failed");74} catch (SecurityException se) {75// test passed76}77System.out.println("Test 2 Succeeded");7879// test new constructor (config) - no permission needed80// (and none configured)81LoginContext lc3 = new LoginContext82("module1",83s,84ch,85c);86System.out.println("Test 3 Succeeded");8788// test old constructor with no permission for other89try {90LoginContext lc4 = new LoginContext91("goToOther",92s,93ch);94throw new RuntimeException("Test 4 Failed");95} catch (SecurityException se) {96// test passed97}98System.out.println("Test 4 Succeeded");99100// test new constructor with no permission for other101try {102LoginContext lc5 = new LoginContext103("goToOther",104s,105ch,106null);107throw new RuntimeException("Test 5 Failed");108} catch (SecurityException se) {109// test passed110}111System.out.println("Test 5 Succeeded");112}113114private static class MyConfig extends Configuration {115public MyConfig() { }116public AppConfigurationEntry[] getAppConfigurationEntry(String name) {117java.util.HashMap map = new java.util.HashMap();118AppConfigurationEntry[] entries = new AppConfigurationEntry[1];119120if (name.equals("module1")) {121AppConfigurationEntry entry = new AppConfigurationEntry122("ConfigConstructor$MyModule1",123AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,124map);125entries[0] = entry;126} else {127entries = null;128}129return entries;130}131public void refresh() { }132}133}134135136