1 /*
2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4703361
27  * @modules jdk.security.auth
28  * @summary can not specify Configuration to LoginContext constructor
29  *
30  * @run main/othervm/policy=ConfigConstructorNoPerm.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructorNoPerm
31  */
32 
33 /**
34  * This test shares the login config with ConfigConstructor.
35  * This test has no configured permissions
36  * (ConfigConstructor tests code with perms configured).
37  */
38 
39 import java.util.Map;
40 import javax.security.auth.Subject;
41 import javax.security.auth.login.AppConfigurationEntry;
42 import javax.security.auth.login.Configuration;
43 import javax.security.auth.login.LoginContext;
44 import javax.security.auth.callback.CallbackHandler;
45 
46 public class ConfigConstructorNoPerm {
47 
48     private static Subject s = new Subject();
49     private static CallbackHandler ch =
50                 new com.sun.security.auth.callback.TextCallbackHandler();
51     private static Configuration c = new MyConfig();
52 
main(String[] args)53     public static void main(String[] args) throws Exception {
54 
55         // test old constructor with no permission
56         try {
57             LoginContext lc1 = new LoginContext
58                         ("module1",
59                         s,
60                         ch);
61             throw new RuntimeException("Test 1 Failed");
62         } catch (SecurityException se) {
63             // test passed
64         }
65         System.out.println("Test 1 Succeeded");
66 
67         // test new constructor (null config) with no permission
68         try {
69             LoginContext lc2 = new LoginContext
70                         ("module1",
71                         s,
72                         ch,
73                         null);
74             throw new RuntimeException("Test 2 Failed");
75         } catch (SecurityException se) {
76             // test passed
77         }
78         System.out.println("Test 2 Succeeded");
79 
80         // test new constructor (config) - no permission needed
81         // (and none configured)
82         LoginContext lc3 = new LoginContext
83                         ("module1",
84                         s,
85                         ch,
86                         c);
87         System.out.println("Test 3 Succeeded");
88 
89         // test old constructor with no permission for other
90         try {
91             LoginContext lc4 = new LoginContext
92                         ("goToOther",
93                         s,
94                         ch);
95             throw new RuntimeException("Test 4 Failed");
96         } catch (SecurityException se) {
97             // test passed
98         }
99         System.out.println("Test 4 Succeeded");
100 
101         // test new constructor with no permission for other
102         try {
103             LoginContext lc5 = new LoginContext
104                         ("goToOther",
105                         s,
106                         ch,
107                         null);
108             throw new RuntimeException("Test 5 Failed");
109         } catch (SecurityException se) {
110             // test passed
111         }
112         System.out.println("Test 5 Succeeded");
113     }
114 
115     private static class MyConfig extends Configuration {
MyConfig()116         public MyConfig() { }
getAppConfigurationEntry(String name)117         public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
118             java.util.HashMap map = new java.util.HashMap();
119             AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
120 
121             if (name.equals("module1")) {
122                 AppConfigurationEntry entry = new AppConfigurationEntry
123                         ("ConfigConstructor$MyModule1",
124                         AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
125                         map);
126                 entries[0] = entry;
127             } else {
128                 entries = null;
129             }
130             return entries;
131         }
refresh()132         public void refresh() { }
133     }
134 }
135