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