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/PrivateCredentialPermission/Subset.java
38854 views
1
/*
2
* Copyright (c) 2000, 2007, 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
* @author Ram Marti
27
* @bug 4326852
28
* @summary Retrive a subset of private credentials can be accessed
29
* @run main/othervm/policy=Subset.policy Subset
30
*/
31
32
import java.util.*;
33
import com.sun.security.auth.SolarisPrincipal;
34
import javax.security.auth.Subject;
35
36
/*
37
* Author : Ram Marti
38
* This is a test program to verify the fix for Bug 4326852
39
* (impossible to extract a subset of private credentials)
40
* The policy file used allows read access only to String classes.
41
* grant {
42
* permission javax.security.auth.AuthPermission \
43
* "modifyPrivateCredentials";
44
* permission javax.security.auth.PrivateCredentialPermission \
45
* "java.lang.String com.sun.security.auth.SolarisPrincipal \"user"", "read";
46
* };
47
48
* The test verifies the following:
49
* - String class creds can be retrieved by using
50
* getPrivateCredentials(String.class)
51
* - The above set is not backed internally
52
* - getPrivateCredentials(Boolean or Integer) returns an empty set
53
* - Set is returned by getPrivateCredentials() throws
54
* security exception when trying to access non-String
55
* class credentials
56
* - The above set is internally backed up and any changes in
57
* internal private creds are reflected in the set returned
58
* - When the above set throws security exception the iterator
59
* - is advanced to the next item in the list of creds.
60
* - equals,contains,containsAll,add,remove operations work correctly
61
*/
62
63
public class Subset {
64
public static void main(String[] args) throws Exception {
65
int exceptionCounter =0;
66
Iterator iter1;
67
HashSet creds = new HashSet();
68
Subject emptys =
69
new Subject(false, //readOnly
70
Collections.singleton(new SolarisPrincipal("user")),
71
Collections.EMPTY_SET,
72
creds);
73
/* Test principals */
74
75
Set princ= emptys.getPrincipals();
76
HashSet collp= new HashSet();
77
collp.add(new String("abc"));
78
collp.add(new String("def"));
79
collp.add(new String("Exists"));
80
collp.add(new String("Does not Exist"));
81
try {
82
if (princ.containsAll(collp)) {
83
throw new Exception ("Error: Contains the collection");
84
} else
85
System.out.println ("Does not Contain the collection");
86
} catch (SecurityException e) {
87
throw new Exception ("Error: Exception in containsAll (string coll)!!");
88
}
89
90
91
Set p1 = emptys.getPrivateCredentials();
92
93
if (p1.size() != 0) {
94
throw new Exception("Error:p1 size should have been 6 and was " +
95
p1.size());
96
}
97
98
creds.add("abc");
99
creds.add(new Integer(3));
100
creds.add(Boolean.TRUE);
101
Subject sremove =
102
new Subject(false, //readOnly
103
Collections.singleton(new SolarisPrincipal("user")),
104
Collections.EMPTY_SET,
105
creds);
106
Set p2 = sremove.getPrivateCredentials();
107
108
if (p2.size() !=3){
109
throw new Exception("Error: p2 size should have been 3 and was " +
110
p2.size());
111
}
112
iter1 = p2.iterator();
113
exceptionCounter=0;
114
while (iter1.hasNext()) {
115
try {
116
Object o = iter1.next();
117
System.out.println(" private creds of class " +
118
o.getClass() + "value is " + o.toString());
119
} catch (SecurityException e) {
120
System.out.println("Expected Exception occured");
121
exceptionCounter++;
122
}
123
}
124
if (exceptionCounter != 2) {
125
throw new Exception("Expected number of exceptions was 2 " +
126
"The actual number was " + exceptionCounter);
127
}
128
129
// Verify that remove op was successful
130
131
iter1.remove();
132
if (p2.size() !=2) {
133
throw new RuntimeException("Error: p2 size should have been 2 and was " +
134
p2.size());
135
}
136
System.out.println ("Checking the value after removal");
137
p2 = sremove.getPrivateCredentials();
138
try {
139
if (!p2.add(new String("XYZ"))) {
140
141
throw new RuntimeException("Error in adding string");
142
}
143
if (!p2.add(new Integer(99))) {
144
145
throw new RuntimeException("Error in adding Integer");
146
}
147
HashSet coll1 = new HashSet();
148
coll1.add(new String("RST"));
149
coll1.add(new Integer(1));
150
if (!p2.addAll(coll1)) {
151
152
throw new RuntimeException("Error in addAll");
153
}
154
155
} catch (Exception e){
156
e.printStackTrace();
157
throw new RuntimeException("Unexpected exception in add");
158
159
}
160
iter1 = p2.iterator();
161
162
while (iter1.hasNext()) {
163
try {
164
Object o = iter1.next();
165
System.out.println(" private creds of class " +
166
o.getClass() + "value is " + o.toString());
167
} catch (SecurityException e) {
168
// System.out.println("Exception!!");
169
}
170
}
171
iter1 = p2.iterator();
172
173
System.out.println ("Checked the value after removal");
174
175
HashSet creds1 = new HashSet();
176
creds1.add("abc");
177
creds1.add("def");
178
creds1.add(Boolean.TRUE);
179
creds1.add(new Integer(1));
180
creds1.add(new String("Exists"));
181
Subject scontain =
182
new Subject(false, //readOnly
183
Collections.singleton(new SolarisPrincipal("user")),
184
Collections.EMPTY_SET,
185
creds1);
186
p2 = scontain.getPrivateCredentials();
187
try {
188
Object ObjAr = p2.toArray();
189
} catch (SecurityException e) {
190
System.out.println("Should get an Exception in toArray()");
191
}
192
193
HashSet creds3 = new HashSet();
194
creds3.add (new String("abc"));
195
p2 = scontain.getPrivateCredentials();
196
197
try {
198
Object ObjCred = (Object)creds3.clone();
199
System.out.println ("Size of p2 is " + p2.size() +
200
"Size of ObjCred is " +
201
((HashSet)ObjCred).size()
202
);
203
if (p2.equals(ObjCred))
204
throw new RuntimeException("Error:Equals ObjCred *** ");
205
else
206
System.out.println ("Does not Equal Objcred");
207
} catch (SecurityException e) {
208
throw new RuntimeException("Error:Should not get an Exception in equals of creds3");
209
210
211
}
212
213
try {
214
Object ObjCred = (Object)creds1.clone();
215
System.out.println ("Size of p2 is " + p2.size() +
216
"Size of ObjCred is " +
217
((HashSet)ObjCred).size()
218
);
219
if (p2.equals(ObjCred))
220
throw new RuntimeException ("Error: Equals ObjCred");
221
else
222
throw new RuntimeException ("Error: Does not Equal Objcred");
223
} catch (SecurityException e) {
224
System.out.println("Should get an Exception in equals of creds1");
225
}
226
/* We can store only string types of creds
227
* Let us create a subject with only string type of creds
228
*/
229
230
HashSet creds2 = new HashSet();
231
creds2.add("abc");
232
creds2.add("def");
233
creds2.add("ghi");
234
Subject sstring =
235
new Subject(false, //readOnly
236
Collections.singleton(new SolarisPrincipal("user")),
237
Collections.EMPTY_SET,
238
creds2);
239
p2 = sstring.getPrivateCredentials();
240
try {
241
String[] selectArray = { "exits", "Does not exist"};
242
Object ObjAr = p2.toArray(selectArray);
243
System.out.println(" No Exception in ObjAr- String");
244
245
} catch (SecurityException e) {
246
throw new RuntimeException(" Error: Exception in ObjAr- String!!");
247
}
248
/*
249
* New subject scontain1, set p3, creds4
250
*/
251
252
253
HashSet creds4 = new HashSet();
254
creds4.add("abc");
255
creds4.add("def");
256
creds4.add("ghi");
257
creds4.add(new Integer(1));
258
creds4.add("Exists");
259
Subject scontain1 =
260
new Subject(false, //readOnly
261
Collections.singleton(new SolarisPrincipal("user")),
262
Collections.EMPTY_SET,
263
creds4);
264
Set p3 = scontain1.getPrivateCredentials();
265
try {
266
Object Obj = new String("Exists");
267
if (p3.contains(Obj))
268
System.out.println ("Contains String cred");
269
else
270
throw new RuntimeException ("Error Does not Contain the stringcred exists");
271
} catch (SecurityException e) {
272
throw new RuntimeException("Error:Exception!!");
273
274
}
275
try {
276
Object ObjCred = (Object)creds4.clone();
277
if (p3.equals(ObjCred))
278
throw new RuntimeException ("Error:Equals ObjCred");
279
else
280
throw new RuntimeException ("Error:Does not Equal Objcred");
281
} catch (SecurityException e) {
282
System.out.println("Should get an Exception in equals");
283
}
284
285
try {
286
Object Obj = new Integer(1);
287
if (p3.contains(Obj))
288
throw new RuntimeException ("Error:Contains integer cred");
289
else
290
throw new RuntimeException ("Error:Does not Contain integer cred");
291
} catch (SecurityException e) {
292
System.out.println("Should get an Exception in contains Integer cred");
293
}
294
295
296
297
HashSet coll = new HashSet();
298
coll.add(new String("abc"));
299
coll.add(new String("def"));
300
coll.add(new String("Exists"));
301
coll.add(new String("Does not Exist"));
302
try {
303
if (p3.containsAll(coll))
304
throw new RuntimeException ("Error: Contains the collection");
305
else
306
System.out.println ("Does not Contain the collection");
307
} catch (SecurityException e) {
308
throw new RuntimeException("Error: Exception in containsAll (string coll)!!");
309
310
}
311
coll.remove(new String("Exists"));
312
coll.remove(new String("Does not Exist"));
313
try {
314
if (p3.containsAll(coll))
315
System.out.println ("Contains the collection");
316
else
317
throw new RuntimeException ("Error:Does not Contain the collection");
318
} catch (SecurityException e) {
319
throw new RuntimeException("Error: Exception in containsAll (string coll)!!");
320
}
321
322
Object Obj = new String("Exists");
323
try {
324
if (p3.contains(Obj))
325
System.out.println ("Contains String cred exists");
326
else
327
System.out.println ("Does not Contain String cred exists");
328
} catch (SecurityException e) {
329
System.out.println("Exception in String cred!!");
330
}
331
332
Obj = new String("Does not exist");
333
try {
334
if (p3.contains(Obj))
335
throw new RuntimeException ("Error: Contains the String does not exist");
336
else
337
System.out.println ("Does not Contain the String cred Does not exist");
338
} catch (SecurityException e) {
339
throw new RuntimeException("Error: Exception in Contains!!");
340
}
341
p3.add(new Integer(2));
342
coll.add(new Integer(2));
343
p3.add("XYZ");
344
345
System.out.println ("Testing Retainall ");
346
exceptionCounter =0;
347
iter1 = p3.iterator();
348
while (iter1.hasNext())
349
{
350
try {
351
Object o = iter1.next();
352
System.out.println(" private creds of class " +
353
o.getClass() + "value is " + o.toString());
354
} catch (SecurityException e) {
355
System.out.println(" We should get exception");
356
System.out.println("Exception!!");
357
exceptionCounter++;
358
}
359
}
360
System.out.println(" After the retainall Operation");
361
try {
362
if (p3.retainAll(coll))
363
System.out.println ("Retained the collection");
364
else
365
throw new RuntimeException ("Error: RetainAll did not succeed");
366
} catch (SecurityException e) {
367
e.printStackTrace();
368
throw new RuntimeException("Error: Unexpected Exception in retainAll!");
369
}
370
iter1 = p3.iterator();
371
while (iter1.hasNext())
372
{
373
try {
374
Object o = iter1.next();
375
System.out.println(" private creds of class " +
376
o.getClass() + "value is " + o.toString());
377
} catch (SecurityException e) {
378
exceptionCounter++;
379
}
380
}
381
System.out.println ("Retainall collection");
382
p3.add(new Integer (3));
383
iter1 = p3.iterator();
384
while (iter1.hasNext()) {
385
try {
386
Object o = iter1.next();
387
System.out.println(" private creds of class " +
388
o.getClass() + "value is " + o.toString());
389
} catch (SecurityException e) {
390
System.out.println("Should get Exception ");
391
}
392
}
393
exceptionCounter=0;
394
HashSet coll2 = new HashSet();
395
coll2.add(new String("abc"));
396
coll2.add(new Integer (3));
397
System.out.println(" before removeall");
398
iter1 = p3.iterator();
399
exceptionCounter =0;
400
while (iter1.hasNext()) {
401
try {
402
Object o = iter1.next();
403
System.out.println(" private creds of class " +
404
o.getClass() + "value is " + o.toString());
405
} catch (SecurityException e) {
406
System.out.println("Expected Exception thrown ");
407
exceptionCounter++;
408
}
409
}
410
// We added two integer creds so there must be two exceptions only
411
412
if (exceptionCounter != 2) {
413
throw new RuntimeException("Expected 2 Exceptions; received " +
414
exceptionCounter + "exceptions ");
415
}
416
417
try {
418
p3.removeAll(coll2);
419
System.out.println(" removeall successful! ");
420
} catch (SecurityException e) {
421
throw new RuntimeException(" Error: removeAll Security Exception!!");
422
}
423
424
iter1 = p3.iterator();
425
System.out.println(" After removeall");
426
exceptionCounter = 0;
427
while (iter1.hasNext()) {
428
try {
429
Object o = iter1.next();
430
System.out.println (" private creds of class " +
431
o.getClass() + "value is " + o.toString());
432
} catch (SecurityException e) {
433
System.out.println("Expected Exception thrown ");
434
exceptionCounter++;
435
}
436
}
437
// We had two integer creds; removed one as a part of coll2; so
438
// only one exception must have been thrown
439
if (exceptionCounter != 1) {
440
throw new RuntimeException("Expected 1 Exceptions; received " +
441
exceptionCounter + "exceptions ");
442
}
443
try {
444
p3.clear();
445
System.out.println(" Clear() successful! ");
446
} catch (SecurityException e) {
447
throw new RuntimeException(" Error: Clear Security Exception!!");
448
}
449
450
451
/* New subject s with creds and privCredSet
452
*
453
*/
454
creds.clear();
455
creds.add("abc");
456
creds.add("def");
457
creds.add("ghi");
458
creds.add(new Integer(1));
459
Subject s =
460
new Subject(false, //readOnly
461
Collections.singleton(new SolarisPrincipal("user")),
462
Collections.EMPTY_SET,
463
creds);
464
try {
465
Set privCredSet = s.getPrivateCredentials(char.class);
466
if (privCredSet.size() != 0) {
467
throw new RuntimeException("Error:String Privcred size should have been 0 and was " +
468
privCredSet.size());
469
}
470
471
} catch (Exception e) {
472
throw new RuntimeException ("Error " + e.toString());
473
}
474
475
476
try {
477
Set privCredSet = s.getPrivateCredentials(String.class);
478
if (privCredSet.size() != 3) {
479
throw new RuntimeException("Error:String Privcred size should have been 2 and was " +
480
privCredSet.size());
481
}
482
s.getPrivateCredentials().add("XYZ");
483
/*
484
* Since the privCredSet is not backed by internal private
485
* creds adding to it should not make any difference to
486
* privCredSet and theize should still be 3
487
*/
488
489
if (privCredSet.size() != 3) {
490
throw new RuntimeException("Error:String Privcred size should have been 2 and was " +
491
privCredSet.size());
492
}
493
s.getPrivateCredentials().remove("XYZ");
494
/*
495
* Let us try to get the elements
496
* No exception should occur
497
*/
498
499
Iterator iter = privCredSet.iterator();
500
while (iter.hasNext()) {
501
try {
502
Object o = iter.next();
503
System.out.println(" private creds of class " +
504
o.getClass() + "value is " + o.toString());
505
} catch (SecurityException e) {
506
}
507
}
508
} catch (Exception e) {
509
e.printStackTrace();
510
throw new RuntimeException("Unexcpected Exception");
511
}
512
513
/*
514
* Can we add and remove the creds
515
*/
516
s.getPrivateCredentials().add("XYZ");
517
s.getPrivateCredentials().remove("XYZ");
518
s.getPrivateCredentials().add(new Integer(2));
519
s.getPrivateCredentials().remove(new Integer(2));
520
521
522
// We don't have permission to read Boolean creds
523
// SInce the creds have no boolean creds we should get an empty
524
// set
525
try {
526
Set privCredSet1 = s.getPrivateCredentials(Boolean.class);
527
if (privCredSet1.size() != 0){
528
throw new RuntimeException("Error:String PrivcredSet1 of Boolean size should have been 0 and was " +
529
privCredSet1.size());
530
}
531
} catch (SecurityException e) {
532
e.printStackTrace();
533
throw new RuntimeException("Unexcpected Exception");
534
}
535
System.out.println ("Checked Boolean Creds ");
536
537
/*
538
* We don't have permission to read Integer creds
539
* We should get an empty set even though the private creds
540
* has an integer cred. No security exception either !
541
*/
542
543
try {
544
Set privCredSet1 = s.getPrivateCredentials(Integer.class);
545
if (privCredSet1.size() != 0){
546
throw new RuntimeException("Error:String PrivcredSet1 of Integer size should have been 0 and was " +
547
privCredSet1.size());
548
}
549
} catch (SecurityException e) {
550
System.out.println ("Expected exception");
551
}
552
System.out.println ("Checked Integer Creds ");
553
554
Set privCredSet2 = s.getPrivateCredentials();
555
556
if (privCredSet2.size() != 4){
557
throw new RuntimeException("Error:String PrivcredSet1 size should have been 4 and was " +
558
privCredSet2.size());
559
}
560
561
/*
562
* Since the returned privCredSet2 is internally backed by the
563
* private creds, any additions to it should be reflected in
564
* privcredSet2
565
*/
566
s.getPrivateCredentials().add("XYZ");
567
if (privCredSet2.size() != 5) {
568
throw new RuntimeException("Error:String PrivcredSet1 size should have been 5 and was " +
569
privCredSet2.size());
570
}
571
s.getPrivateCredentials().remove("XYZ");
572
if (privCredSet2.size() != 4) {
573
throw new RuntimeException("String privCredSet2 size should have been 5 and was " +
574
privCredSet2.size());
575
}
576
System.out.println("Checked remove(String) operation");
577
/* Let us add a couple of Boolean creds */
578
s.getPrivateCredentials().add(Boolean.TRUE);
579
s.getPrivateCredentials().add(new Integer(2));
580
581
exceptionCounter =0;
582
iter1 = privCredSet2.iterator();
583
while (iter1.hasNext())
584
{
585
try {
586
Object o = iter1.next();
587
System.out.println(" private creds of class " +
588
o.getClass() + "value is " + o.toString());
589
} catch (SecurityException e) {
590
System.out.println(" We should get exception");
591
System.out.println("Exception!!");
592
exceptionCounter++;
593
}
594
}
595
if (exceptionCounter != 3) {
596
throw new RuntimeException("Expected number of exception was 3 " +
597
"The actual number was " + exceptionCounter);
598
}
599
privCredSet2.add (new Integer(3));
600
try {
601
int hashCode = privCredSet2.hashCode();
602
} catch (SecurityException e) {
603
System.out.println ("hashCode Expected exception");
604
}
605
System.out.println ("Tests completed");
606
}
607
608
}
609
610