J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4703361 |
| 27 | * @summary can not specify Configuration to LoginContext constructor |
| 28 | * |
| 29 | * @run main/othervm/policy=ConfigConstructorNoPerm.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructorNoPerm |
| 30 | */ |
| 31 | |
| 32 | /** |
| 33 | * This test shares the login config with ConfigConstructor. |
| 34 | * This test has no configured permissions |
| 35 | * (ConfigConstructor tests code with perms configured). |
| 36 | */ |
| 37 | |
| 38 | import java.util.Map; |
| 39 | import javax.security.auth.*; |
| 40 | import javax.security.auth.login.*; |
| 41 | import javax.security.auth.spi.*; |
| 42 | import javax.security.auth.callback.*; |
| 43 | |
| 44 | public class ConfigConstructorNoPerm { |
| 45 | |
| 46 | private static Subject s = new Subject(); |
| 47 | private static CallbackHandler ch = |
| 48 | new com.sun.security.auth.callback.TextCallbackHandler(); |
| 49 | private static Configuration c = new MyConfig(); |
| 50 | |
| 51 | public static void main(String[] args) throws Exception { |
| 52 | |
| 53 | // test old constructor with no permission |
| 54 | try { |
| 55 | LoginContext lc1 = new LoginContext |
| 56 | ("module1", |
| 57 | s, |
| 58 | ch); |
| 59 | throw new RuntimeException("Test 1 Failed"); |
| 60 | } catch (SecurityException se) { |
| 61 | // test passed |
| 62 | } |
| 63 | System.out.println("Test 1 Succeeded"); |
| 64 | |
| 65 | // test new constructor (null config) with no permission |
| 66 | try { |
| 67 | LoginContext lc2 = new LoginContext |
| 68 | ("module1", |
| 69 | s, |
| 70 | ch, |
| 71 | null); |
| 72 | throw new RuntimeException("Test 2 Failed"); |
| 73 | } catch (SecurityException se) { |
| 74 | // test passed |
| 75 | } |
| 76 | System.out.println("Test 2 Succeeded"); |
| 77 | |
| 78 | // test new constructor (config) - no permission needed |
| 79 | // (and none configured) |
| 80 | LoginContext lc3 = new LoginContext |
| 81 | ("module1", |
| 82 | s, |
| 83 | ch, |
| 84 | c); |
| 85 | System.out.println("Test 3 Succeeded"); |
| 86 | |
| 87 | // test old constructor with no permission for other |
| 88 | try { |
| 89 | LoginContext lc4 = new LoginContext |
| 90 | ("goToOther", |
| 91 | s, |
| 92 | ch); |
| 93 | throw new RuntimeException("Test 4 Failed"); |
| 94 | } catch (SecurityException se) { |
| 95 | // test passed |
| 96 | } |
| 97 | System.out.println("Test 4 Succeeded"); |
| 98 | |
| 99 | // test new constructor with no permission for other |
| 100 | try { |
| 101 | LoginContext lc5 = new LoginContext |
| 102 | ("goToOther", |
| 103 | s, |
| 104 | ch, |
| 105 | null); |
| 106 | throw new RuntimeException("Test 5 Failed"); |
| 107 | } catch (SecurityException se) { |
| 108 | // test passed |
| 109 | } |
| 110 | System.out.println("Test 5 Succeeded"); |
| 111 | } |
| 112 | |
| 113 | private static class MyConfig extends Configuration { |
| 114 | public MyConfig() { } |
| 115 | public AppConfigurationEntry[] getAppConfigurationEntry(String name) { |
| 116 | java.util.HashMap map = new java.util.HashMap(); |
| 117 | AppConfigurationEntry[] entries = new AppConfigurationEntry[1]; |
| 118 | |
| 119 | if (name.equals("module1")) { |
| 120 | AppConfigurationEntry entry = new AppConfigurationEntry |
| 121 | ("ConfigConstructor$MyModule1", |
| 122 | AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, |
| 123 | map); |
| 124 | entries[0] = entry; |
| 125 | } else { |
| 126 | entries = null; |
| 127 | } |
| 128 | return entries; |
| 129 | } |
| 130 | public void refresh() { } |
| 131 | } |
| 132 | } |