1/* 2 * Copyright (c) 2015, 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 8056179 27 * @summary Unit test for DelegationPermissionCollection subclass 28 */ 29 30import java.security.Permission; 31import java.security.PermissionCollection; 32import java.security.SecurityPermission; 33import java.util.Enumeration; 34import javax.security.auth.kerberos.DelegationPermission; 35 36public class DelegationPermissionCollection { 37 38 private static final String FOO = "\"host/foo.example.com@EXAMPLE.COM\""; 39 private static final String BAR = "\"host/bar.example.com@EXAMPLE.COM\""; 40 private static final String TGT = "\"krbtgt/EXAMPLE.COM@EXAMPLE.COM\""; 41 42 public static void main(String[] args) throws Exception { 43 44 int testFail = 0; 45 46 DelegationPermission perm = new DelegationPermission(FOO + " " + TGT); 47 PermissionCollection perms = perm.newPermissionCollection(); 48 49 // test 1 50 System.out.println 51 ("test 1: add throws IllegalArgException for wrong perm type"); 52 try { 53 perms.add(new SecurityPermission("createAccessControlContext")); 54 System.err.println("Expected IllegalArgumentException"); 55 testFail++; 56 } catch (IllegalArgumentException iae) {} 57 58 // test 2 59 System.out.println("test 2: implies returns false for wrong perm type"); 60 if (perms.implies(new SecurityPermission("getPolicy"))) { 61 System.err.println("Expected false, returned true"); 62 testFail++; 63 } 64 65 // test 3 66 System.out.println("test 3: implies returns true for match on name"); 67 perms.add(new DelegationPermission(FOO + " " + TGT)); 68 if (!perms.implies(new DelegationPermission(FOO + " " + TGT))) { 69 System.err.println("Expected true, returned false"); 70 testFail++; 71 } 72 73 // test 4 74 System.out.println 75 ("test 4: implies returns false for non-match on name"); 76 if (perms.implies(new DelegationPermission(BAR + " " + TGT))) { 77 System.err.println("Expected false, returned true"); 78 testFail++; 79 } 80 81 // test 5 82 System.out.println("test 5: elements returns correct number of perms"); 83 int numPerms = 0; 84 Enumeration<Permission> e = perms.elements(); 85 while (e.hasMoreElements()) { 86 numPerms++; 87 System.out.println(e.nextElement()); 88 } 89 if (numPerms != 1) { 90 System.err.println("Expected 1, got " + numPerms); 91 testFail++; 92 } 93 94 if (testFail > 0) { 95 throw new Exception(testFail + " test(s) failed"); 96 } 97 } 98} 99