0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4703361
0N/A * @summary can not specify Configuration to LoginContext constructor
0N/A *
0N/A * @run main/othervm/policy=ConfigConstructor.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructor
0N/A *
0N/A */
0N/A
0N/A/**
0N/A * This test shares the login config with ConfigConstructorNoPerm.
0N/A * This test has all necessary permissions configured in the policy
0N/A * (ConfigConstructorNoPerm has no perms and checks for SecurityExceptions).
0N/A */
0N/A
0N/Aimport java.util.Map;
0N/Aimport javax.security.auth.*;
0N/Aimport javax.security.auth.login.*;
0N/Aimport javax.security.auth.spi.*;
0N/Aimport javax.security.auth.callback.*;
0N/A
0N/Apublic class ConfigConstructor {
0N/A
0N/A private static Subject s = new Subject();
0N/A private static CallbackHandler ch =
0N/A new com.sun.security.auth.callback.TextCallbackHandler();
0N/A private static Configuration c = new MyConfig();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A // test non-null behavior with provided config
0N/A LoginContext lc = new LoginContext
0N/A ("module1",
0N/A s,
0N/A ch,
0N/A c);
0N/A lc.login();
0N/A System.out.println("Test 1 Passed");
0N/A
0N/A // test null behavior with provided config
0N/A LoginContext lc2 = new LoginContext
0N/A ("module2",
0N/A null,
0N/A null,
0N/A c);
0N/A lc2.login();
0N/A System.out.println("Test 2 Passed");
0N/A
0N/A // test null config
0N/A LoginContext lc3 = new LoginContext
0N/A ("module3",
0N/A s,
0N/A ch,
0N/A null);
0N/A lc3.login();
0N/A System.out.println("Test 3 Passed");
0N/A
0N/A // test null config
0N/A LoginContext lc4 = new LoginContext
0N/A ("module4",
0N/A null,
0N/A null,
0N/A null);
0N/A lc4.login();
0N/A System.out.println("Test 4 Passed");
0N/A
0N/A // test security (without permission)
0N/A try {
0N/A LoginContext lc5 = new LoginContext
0N/A ("module5",
0N/A null,
0N/A null,
0N/A c);
0N/A lc5.login();
0N/A throw new SecurityException("test failed - security check failed");
0N/A } catch (LoginException le) {
0N/A if (le.getCause() instanceof SecurityException) {
0N/A // test passed
0N/A } else {
0N/A le.printStackTrace();
0N/A throw new SecurityException("test failed: " +
0N/A "LoginException did not have chained SecurityException");
0N/A }
0N/A }
0N/A System.out.println("Test 5 Passed");
0N/A
0N/A // test security (with permission)
0N/A LoginContext lc6 = new LoginContext
0N/A ("module6",
0N/A null,
0N/A null,
0N/A c);
0N/A lc6.login();
0N/A System.out.println("Test 6 Passed");
0N/A
0N/A // test other
0N/A LoginContext lc7 = new LoginContext
0N/A ("goToOther",
0N/A null,
0N/A null,
0N/A c);
0N/A lc7.login();
0N/A System.out.println("Test 7 Passed");
0N/A
0N/A // test other old constructor
0N/A LoginContext lc8 = new LoginContext
0N/A ("goToOther");
0N/A lc8.login();
0N/A System.out.println("Test 8 Passed");
0N/A }
0N/A
0N/A private static class MyConfig extends Configuration {
0N/A public MyConfig() { }
0N/A public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
0N/A java.util.HashMap map = new java.util.HashMap();
0N/A AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
0N/A
0N/A if (name.equals("module1")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule1",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else if (name.equals("module2")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule2",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else if (name.equals("module3")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule3",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else if (name.equals("module4")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule4",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else if (name.equals("module5")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule5",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else if (name.equals("module6")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule6",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else if (name.equalsIgnoreCase("other")) {
0N/A AppConfigurationEntry entry = new AppConfigurationEntry
0N/A ("ConfigConstructor$MyModule2",
0N/A AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
0N/A map);
0N/A entries[0] = entry;
0N/A } else {
0N/A entries = null;
0N/A }
0N/A return entries;
0N/A }
0N/A public void refresh() { }
0N/A }
0N/A
0N/A public static class MyModule1 implements LoginModule {
0N/A
0N/A public MyModule1() { }
0N/A
0N/A public void initialize(Subject s, CallbackHandler ch,
0N/A Map<String,?> state, Map<String,?> options) {
0N/A if (s != ConfigConstructor.s ||
0N/A ch != ConfigConstructor.ch) {
0N/A throw new SecurityException("Module 1 failed");
0N/A }
0N/A }
0N/A
0N/A public boolean login() throws LoginException { return true; }
0N/A public boolean commit() throws LoginException { return true; }
0N/A public boolean abort() throws LoginException { return true; }
0N/A public boolean logout() throws LoginException { return true; }
0N/A }
0N/A
0N/A public static class MyModule2 implements LoginModule {
0N/A
0N/A public MyModule2() { }
0N/A
0N/A public void initialize(Subject s, CallbackHandler ch,
0N/A Map<String,?> state, Map<String,?> options) {
0N/A if (s == ConfigConstructor.s ||
0N/A ch != null) {
0N/A throw new SecurityException("Module 2 failed");
0N/A }
0N/A }
0N/A
0N/A public boolean login() throws LoginException { return true; }
0N/A public boolean commit() throws LoginException { return true; }
0N/A public boolean abort() throws LoginException { return true; }
0N/A public boolean logout() throws LoginException { return true; }
0N/A }
0N/A
0N/A public static class MyModule3 implements LoginModule {
0N/A
0N/A public MyModule3() { }
0N/A
0N/A public void initialize(Subject s, CallbackHandler ch,
0N/A Map<String,?> state, Map<String,?> options) {
0N/A if (s != ConfigConstructor.s ||
0N/A ch == null ||
0N/A ch == ConfigConstructor.ch) {
0N/A throw new SecurityException("Module 3 failed");
0N/A }
0N/A }
0N/A
0N/A public boolean login() throws LoginException { return true; }
0N/A public boolean commit() throws LoginException { return true; }
0N/A public boolean abort() throws LoginException { return true; }
0N/A public boolean logout() throws LoginException { return true; }
0N/A }
0N/A
0N/A public static class MyModule4 implements LoginModule {
0N/A
0N/A public MyModule4() { }
0N/A
0N/A public void initialize(Subject s, CallbackHandler ch,
0N/A Map<String,?> state, Map<String,?> options) {
0N/A if (s == ConfigConstructor.s ||
0N/A ch != null) {
0N/A throw new SecurityException("Module 4 failed");
0N/A }
0N/A }
0N/A
0N/A public boolean login() throws LoginException { return true; }
0N/A public boolean commit() throws LoginException { return true; }
0N/A public boolean abort() throws LoginException { return true; }
0N/A public boolean logout() throws LoginException { return true; }
0N/A }
0N/A
0N/A public static class MyModule5 implements LoginModule {
0N/A
0N/A public MyModule5() { }
0N/A
0N/A public void initialize(Subject s, CallbackHandler ch,
0N/A Map<String,?> state, Map<String,?> options) { }
0N/A
0N/A public boolean login() throws LoginException {
0N/A // do something security-sensitive
0N/A System.out.println(System.getProperty("user.name"));
0N/A return true;
0N/A }
0N/A public boolean commit() throws LoginException { return true; }
0N/A public boolean abort() throws LoginException { return true; }
0N/A public boolean logout() throws LoginException { return true; }
0N/A }
0N/A
0N/A public static class MyModule6 implements LoginModule {
0N/A
0N/A public MyModule6() { }
0N/A
0N/A public void initialize(Subject s, CallbackHandler ch,
0N/A Map<String,?> state, Map<String,?> options) { }
0N/A
0N/A public boolean login() throws LoginException {
0N/A // do something security-sensitive
0N/A System.out.println(System.getProperty("user.home"));
0N/A return true;
0N/A }
0N/A public boolean commit() throws LoginException { return true; }
0N/A public boolean abort() throws LoginException { return true; }
0N/A public boolean logout() throws LoginException { return true; }
0N/A }
0N/A}