Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/security/auth/login/LoginContext/ConfigConstructor.java
51706 views
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=ConfigConstructor.policy -Djava.security.auth.login.config=file:${test.src}/ConfigConstructor.config ConfigConstructor
31
*
32
*/
33
34
/**
35
* This test shares the login config with ConfigConstructorNoPerm.
36
* This test has all necessary permissions configured in the policy
37
* (ConfigConstructorNoPerm has no perms and checks for SecurityExceptions).
38
*/
39
40
import java.util.Map;
41
import javax.security.auth.Subject;
42
import javax.security.auth.login.AppConfigurationEntry;
43
import javax.security.auth.login.Configuration;
44
import javax.security.auth.login.LoginContext;
45
import javax.security.auth.login.LoginException;
46
import javax.security.auth.spi.LoginModule;
47
import javax.security.auth.callback.CallbackHandler;
48
49
public class ConfigConstructor {
50
51
private static Subject s = new Subject();
52
private static CallbackHandler ch =
53
new com.sun.security.auth.callback.TextCallbackHandler();
54
private static Configuration c = new MyConfig();
55
56
public static void main(String[] args) throws Exception {
57
58
// test non-null behavior with provided config
59
LoginContext lc = new LoginContext
60
("module1",
61
s,
62
ch,
63
c);
64
lc.login();
65
System.out.println("Test 1 Passed");
66
67
// test null behavior with provided config
68
LoginContext lc2 = new LoginContext
69
("module2",
70
null,
71
null,
72
c);
73
lc2.login();
74
System.out.println("Test 2 Passed");
75
76
// test null config
77
LoginContext lc3 = new LoginContext
78
("module3",
79
s,
80
ch,
81
null);
82
lc3.login();
83
System.out.println("Test 3 Passed");
84
85
// test null config
86
LoginContext lc4 = new LoginContext
87
("module4",
88
null,
89
null,
90
null);
91
lc4.login();
92
System.out.println("Test 4 Passed");
93
94
// test security (without permission)
95
try {
96
LoginContext lc5 = new LoginContext
97
("module5",
98
null,
99
null,
100
c);
101
lc5.login();
102
throw new SecurityException("test failed - security check failed");
103
} catch (LoginException le) {
104
if (le.getCause() instanceof SecurityException) {
105
// test passed
106
} else {
107
le.printStackTrace();
108
throw new SecurityException("test failed: " +
109
"LoginException did not have chained SecurityException");
110
}
111
}
112
System.out.println("Test 5 Passed");
113
114
// test security (with permission)
115
LoginContext lc6 = new LoginContext
116
("module6",
117
null,
118
null,
119
c);
120
lc6.login();
121
System.out.println("Test 6 Passed");
122
123
// test other
124
LoginContext lc7 = new LoginContext
125
("goToOther",
126
null,
127
null,
128
c);
129
lc7.login();
130
System.out.println("Test 7 Passed");
131
132
// test other old constructor
133
LoginContext lc8 = new LoginContext
134
("goToOther");
135
lc8.login();
136
System.out.println("Test 8 Passed");
137
}
138
139
private static class MyConfig extends Configuration {
140
public MyConfig() { }
141
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
142
java.util.HashMap map = new java.util.HashMap();
143
AppConfigurationEntry[] entries = new AppConfigurationEntry[1];
144
145
if (name.equals("module1")) {
146
AppConfigurationEntry entry = new AppConfigurationEntry
147
("ConfigConstructor$MyModule1",
148
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
149
map);
150
entries[0] = entry;
151
} else if (name.equals("module2")) {
152
AppConfigurationEntry entry = new AppConfigurationEntry
153
("ConfigConstructor$MyModule2",
154
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
155
map);
156
entries[0] = entry;
157
} else if (name.equals("module3")) {
158
AppConfigurationEntry entry = new AppConfigurationEntry
159
("ConfigConstructor$MyModule3",
160
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
161
map);
162
entries[0] = entry;
163
} else if (name.equals("module4")) {
164
AppConfigurationEntry entry = new AppConfigurationEntry
165
("ConfigConstructor$MyModule4",
166
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
167
map);
168
entries[0] = entry;
169
} else if (name.equals("module5")) {
170
AppConfigurationEntry entry = new AppConfigurationEntry
171
("ConfigConstructor$MyModule5",
172
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
173
map);
174
entries[0] = entry;
175
} else if (name.equals("module6")) {
176
AppConfigurationEntry entry = new AppConfigurationEntry
177
("ConfigConstructor$MyModule6",
178
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
179
map);
180
entries[0] = entry;
181
} else if (name.equalsIgnoreCase("other")) {
182
AppConfigurationEntry entry = new AppConfigurationEntry
183
("ConfigConstructor$MyModule2",
184
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
185
map);
186
entries[0] = entry;
187
} else {
188
entries = null;
189
}
190
return entries;
191
}
192
public void refresh() { }
193
}
194
195
public static class MyModule1 implements LoginModule {
196
197
public MyModule1() { }
198
199
public void initialize(Subject s, CallbackHandler ch,
200
Map<String,?> state, Map<String,?> options) {
201
if (s != ConfigConstructor.s ||
202
ch != ConfigConstructor.ch) {
203
throw new SecurityException("Module 1 failed");
204
}
205
}
206
207
public boolean login() throws LoginException { return true; }
208
public boolean commit() throws LoginException { return true; }
209
public boolean abort() throws LoginException { return true; }
210
public boolean logout() throws LoginException { return true; }
211
}
212
213
public static class MyModule2 implements LoginModule {
214
215
public MyModule2() { }
216
217
public void initialize(Subject s, CallbackHandler ch,
218
Map<String,?> state, Map<String,?> options) {
219
if (s == ConfigConstructor.s ||
220
ch != null) {
221
throw new SecurityException("Module 2 failed");
222
}
223
}
224
225
public boolean login() throws LoginException { return true; }
226
public boolean commit() throws LoginException { return true; }
227
public boolean abort() throws LoginException { return true; }
228
public boolean logout() throws LoginException { return true; }
229
}
230
231
public static class MyModule3 implements LoginModule {
232
233
public MyModule3() { }
234
235
public void initialize(Subject s, CallbackHandler ch,
236
Map<String,?> state, Map<String,?> options) {
237
if (s != ConfigConstructor.s ||
238
ch == null ||
239
ch == ConfigConstructor.ch) {
240
throw new SecurityException("Module 3 failed");
241
}
242
}
243
244
public boolean login() throws LoginException { return true; }
245
public boolean commit() throws LoginException { return true; }
246
public boolean abort() throws LoginException { return true; }
247
public boolean logout() throws LoginException { return true; }
248
}
249
250
public static class MyModule4 implements LoginModule {
251
252
public MyModule4() { }
253
254
public void initialize(Subject s, CallbackHandler ch,
255
Map<String,?> state, Map<String,?> options) {
256
if (s == ConfigConstructor.s ||
257
ch != null) {
258
throw new SecurityException("Module 4 failed");
259
}
260
}
261
262
public boolean login() throws LoginException { return true; }
263
public boolean commit() throws LoginException { return true; }
264
public boolean abort() throws LoginException { return true; }
265
public boolean logout() throws LoginException { return true; }
266
}
267
268
public static class MyModule5 implements LoginModule {
269
270
public MyModule5() { }
271
272
public void initialize(Subject s, CallbackHandler ch,
273
Map<String,?> state, Map<String,?> options) { }
274
275
public boolean login() throws LoginException {
276
// do something security-sensitive
277
System.out.println(System.getProperty("user.name"));
278
return true;
279
}
280
public boolean commit() throws LoginException { return true; }
281
public boolean abort() throws LoginException { return true; }
282
public boolean logout() throws LoginException { return true; }
283
}
284
285
public static class MyModule6 implements LoginModule {
286
287
public MyModule6() { }
288
289
public void initialize(Subject s, CallbackHandler ch,
290
Map<String,?> state, Map<String,?> options) { }
291
292
public boolean login() throws LoginException {
293
// do something security-sensitive
294
System.out.println(System.getProperty("user.home"));
295
return true;
296
}
297
public boolean commit() throws LoginException { return true; }
298
public boolean abort() throws LoginException { return true; }
299
public boolean logout() throws LoginException { return true; }
300
}
301
}
302
303