Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/Configuration/GetInstance.java
38861 views
1
/*
2
* Copyright (c) 2005, 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 6268315
27
* @bug 6273812
28
* @summary Configuration should be provider-based
29
* @build GetInstanceConfigSpi GetInstanceProvider
30
* @run main/othervm -Djava.security.auth.login.config==${test.src}${/}GetInstance.config GetInstance
31
*/
32
33
import javax.security.auth.login.*;
34
35
import java.security.*;
36
import java.io.File;
37
import java.net.URI;
38
39
public class GetInstance {
40
41
private static final String JAVA_CONFIG = "JavaLoginConfig";
42
43
private static final String MOD0 = "com.foo.Module";
44
private static final String MOD1 = "com.bar.Module";
45
private static final String MOD2 = "com.foobar.Module";
46
private static final String MOD3 = "Module";
47
48
private static class BadParam implements Configuration.Parameters { }
49
50
public static void main(String[] args) throws Exception {
51
52
int testnum = 1;
53
GetInstance gi = new GetInstance();
54
55
testnum = gi.testDefault(testnum);
56
testnum = gi.testStringProvider(testnum);
57
testnum = gi.testProvider(testnum);
58
testnum = gi.testCustomImpl(testnum);
59
testnum = gi.testURIParam(testnum);
60
testnum = gi.testException(testnum);
61
}
62
63
private int testDefault(int testnum) throws Exception {
64
// get an instance of the default ConfigSpiFile
65
Configuration c = Configuration.getInstance(JAVA_CONFIG, null);
66
doTest(c, testnum++);
67
68
// get an instance of FooConfig
69
try {
70
c = Configuration.getInstance("FooConfig", null);
71
throw new SecurityException("test " + testnum++ + " failed");
72
} catch (NoSuchAlgorithmException nsae) {
73
// good
74
System.out.println("test " + testnum++ + " passed");
75
}
76
77
return testnum;
78
}
79
80
private int testStringProvider(int testnum) throws Exception {
81
// get an instance of JavaLoginConfig from SUN
82
Configuration c = Configuration.getInstance(JAVA_CONFIG, null, "SUN");
83
doTest(c, testnum++);
84
85
// get an instance of JavaLoginConfig from SunRsaSign
86
try {
87
c = Configuration.getInstance(JAVA_CONFIG, null, "SunRsaSign");
88
throw new SecurityException("test " + testnum++ + " failed");
89
} catch (NoSuchAlgorithmException nsae) {
90
// good
91
System.out.println("test " + testnum++ + " passed");
92
}
93
94
// get an instance of JavaLoginConfig from FOO
95
try {
96
c = Configuration.getInstance(JAVA_CONFIG, null, "FOO");
97
throw new SecurityException("test " + testnum++ + " failed");
98
} catch (NoSuchProviderException nspe) {
99
// good
100
System.out.println("test " + testnum++ + " passed");
101
}
102
103
return testnum;
104
}
105
106
private int testProvider(int testnum) throws Exception {
107
// get an instance of JavaLoginConfig from SUN
108
Configuration c = Configuration.getInstance(JAVA_CONFIG,
109
null,
110
Security.getProvider("SUN"));
111
doTest(c, testnum++);
112
113
// get an instance of JavaLoginConfig from SunRsaSign
114
try {
115
c = Configuration.getInstance(JAVA_CONFIG,
116
null,
117
Security.getProvider("SunRsaSign"));
118
throw new SecurityException("test " + testnum++ + " failed");
119
} catch (NoSuchAlgorithmException nsae) {
120
// good
121
System.out.println("test " + testnum++ + " passed");
122
}
123
124
return testnum;
125
}
126
127
private int testCustomImpl(int testnum) throws Exception {
128
Provider customProvider = new GetInstanceProvider();
129
Configuration c = Configuration.getInstance("GetInstanceConfigSpi",
130
null,
131
customProvider);
132
doCustomTest(c, testnum++, customProvider);
133
return testnum;
134
}
135
136
private int testURIParam(int testnum) throws Exception {
137
// get an instance of JavaLoginConfig
138
// from SUN and have it read from the URI
139
140
File file = new File(System.getProperty("test.src", "."),
141
"GetInstance.configURI");
142
URI uri = file.toURI();
143
URIParameter uriParam = new URIParameter(uri);
144
Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);
145
doTestURI(c, uriParam, testnum++);
146
147
return testnum;
148
}
149
150
private int testException(int testnum) throws Exception {
151
// get an instance of JavaLoginConfig
152
// from SUN and have it read from the bad URI
153
154
File file = new File(System.getProperty("test.src", "."),
155
"GetInstance.bad.configURI");
156
URI uri = file.toURI();
157
URIParameter uriParam = new URIParameter(uri);
158
159
try {
160
Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);
161
throw new SecurityException("expected IOException");
162
} catch (NoSuchAlgorithmException nsae) {
163
if (nsae.getCause() instanceof java.io.IOException) {
164
System.out.println("exception test passed: " +
165
nsae.getCause().getMessage());
166
} else {
167
throw new SecurityException("expected IOException");
168
}
169
}
170
171
// pass bad param
172
try {
173
Configuration c = Configuration.getInstance(JAVA_CONFIG,
174
new BadParam());
175
throw new SecurityException("test " + testnum++ + " failed");
176
} catch (IllegalArgumentException iae) {
177
// good
178
System.out.println("test " + testnum++ + " passed");
179
}
180
181
try {
182
Configuration c = Configuration.getInstance(JAVA_CONFIG,
183
new BadParam(),
184
"SUN");
185
throw new SecurityException("test " + testnum++ + " failed");
186
} catch (IllegalArgumentException iae) {
187
// good
188
System.out.println("test " + testnum++ + " passed");
189
}
190
191
try {
192
Configuration c = Configuration.getInstance(JAVA_CONFIG,
193
new BadParam(),
194
Security.getProvider("SUN"));
195
throw new SecurityException("test " + testnum++ + " failed");
196
} catch (IllegalArgumentException iae) {
197
// good
198
System.out.println("test " + testnum++ + " passed");
199
}
200
201
return testnum;
202
}
203
204
private int doCommon(Configuration c, int testnum) throws Exception {
205
206
AppConfigurationEntry[] entries = c.getAppConfigurationEntry("EMPTY");
207
if (entries == null) {
208
System.out.println("test " + testnum + ".1 passed");
209
} else {
210
throw new SecurityException("test " + testnum + ".1 failed");
211
}
212
213
entries = c.getAppConfigurationEntry("one");
214
if (entries.length == 1 &&
215
MOD0.equals(entries[0].getLoginModuleName()) &&
216
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==
217
entries[0].getControlFlag()) {
218
System.out.println("test " + testnum + ".2 passed");
219
} else {
220
throw new SecurityException("test " + testnum + ".2 failed");
221
}
222
223
entries = c.getAppConfigurationEntry("two");
224
if (entries.length == 2 &&
225
MOD0.equals(entries[0].getLoginModuleName()) &&
226
AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==
227
entries[0].getControlFlag() &&
228
MOD1.equals(entries[1].getLoginModuleName()) &&
229
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==
230
entries[1].getControlFlag()) {
231
System.out.println("test " + testnum + ".3 passed");
232
} else {
233
throw new SecurityException("test " + testnum + ".3 failed");
234
}
235
236
entries = c.getAppConfigurationEntry("three");
237
if (entries.length == 3 &&
238
MOD0.equals(entries[0].getLoginModuleName()) &&
239
AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==
240
entries[0].getControlFlag() &&
241
MOD1.equals(entries[1].getLoginModuleName()) &&
242
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==
243
entries[1].getControlFlag() &&
244
MOD2.equals(entries[2].getLoginModuleName()) &&
245
AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL ==
246
entries[2].getControlFlag()) {
247
System.out.println("test " + testnum + ".4 passed");
248
} else {
249
throw new SecurityException("test " + testnum + ".4 failed");
250
}
251
252
return testnum;
253
}
254
255
private void doCustomTest(Configuration c,
256
int testnum,
257
Provider custom) throws Exception {
258
259
testnum = doCommon(c, testnum);
260
261
// test getProvider
262
if (custom == c.getProvider() &&
263
"GetInstanceProvider".equals(c.getProvider().getName())) {
264
System.out.println("test " + testnum + " (getProvider) passed");
265
} else {
266
throw new SecurityException
267
("test " + testnum + " (getProvider) failed");
268
}
269
270
// test getType
271
if ("GetInstanceConfigSpi".equals(c.getType())) {
272
System.out.println("test " + testnum + "(getType) passed");
273
} else {
274
throw new SecurityException("test " + testnum +
275
" (getType) failed");
276
}
277
}
278
279
private void doTest(Configuration c, int testnum) throws Exception {
280
testnum = doCommon(c, testnum);
281
282
// test getProvider
283
if ("SUN".equals(c.getProvider().getName())) {
284
System.out.println("test " + testnum + " (getProvider) passed");
285
} else {
286
throw new SecurityException("test " + testnum +
287
" (getProvider) failed");
288
}
289
290
// test getType
291
if (JAVA_CONFIG.equals(c.getType())) {
292
System.out.println("test " + testnum + " (getType) passed");
293
} else {
294
throw new SecurityException("test " + testnum +
295
" (getType) failed");
296
}
297
}
298
299
private void doTestURI(Configuration c,
300
Configuration.Parameters uriParam,
301
int testnum) throws Exception {
302
303
AppConfigurationEntry[] entries = c.getAppConfigurationEntry("four");
304
if (entries.length == 4 &&
305
MOD0.equals(entries[0].getLoginModuleName()) &&
306
AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==
307
entries[0].getControlFlag() &&
308
MOD1.equals(entries[1].getLoginModuleName()) &&
309
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==
310
entries[1].getControlFlag() &&
311
MOD2.equals(entries[2].getLoginModuleName()) &&
312
AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL ==
313
entries[2].getControlFlag() &&
314
MOD3.equals(entries[3].getLoginModuleName()) &&
315
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==
316
entries[3].getControlFlag()) {
317
System.out.println("test " + testnum + ".1 passed");
318
} else {
319
throw new SecurityException("test " + testnum + ".1 failed");
320
}
321
322
// test getProvider
323
if ("SUN".equals(c.getProvider().getName())) {
324
System.out.println("test " + testnum + " (getProvider) passed");
325
} else {
326
throw new SecurityException("test " + testnum +
327
" (getProvider) failed");
328
}
329
330
// test getType
331
if (JAVA_CONFIG.equals(c.getType())) {
332
System.out.println("test " + testnum + " (getType) passed");
333
} else {
334
throw new SecurityException("test " + testnum +
335
" (getType) failed");
336
}
337
338
// test getParameters
339
if (uriParam.equals(c.getParameters())) {
340
System.out.println("test " + testnum + " (getParameters) passed");
341
} else {
342
throw new SecurityException("test " + testnum +
343
" (getParameters) failed");
344
}
345
}
346
}
347
348