jdk/test/javax/security/auth/kerberos/ServicePermissionCollection.java
changeset 31080 00a25f4c4d44
equal deleted inserted replaced
31062:a6b5f444cb68 31080:00a25f4c4d44
       
     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 ServicePermissionCollection subclass
       
    28  */
       
    29 
       
    30 import java.security.Permission;
       
    31 import java.security.PermissionCollection;
       
    32 import java.security.SecurityPermission;
       
    33 import java.util.Enumeration;
       
    34 import javax.security.auth.kerberos.ServicePermission;
       
    35 
       
    36 public class ServicePermissionCollection {
       
    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 BAZ = "host/baz.example.com@EXAMPLE.COM";
       
    41 
       
    42     public static void main(String[] args) throws Exception {
       
    43 
       
    44         int testFail = 0;
       
    45 
       
    46         ServicePermission perm = new ServicePermission(FOO, "accept");
       
    47         PermissionCollection perms = perm.newPermissionCollection();
       
    48 
       
    49         // test 1
       
    50         System.out.println
       
    51             ("test 1: add throws IllegalArgExc for wrong permission 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
       
    67             ("test 3: implies returns true for match on name and action");
       
    68         perms.add(new ServicePermission(FOO, "accept"));
       
    69         if (!perms.implies(new ServicePermission(FOO, "accept"))) {
       
    70             System.err.println("Expected true, returned false");
       
    71             testFail++;
       
    72         }
       
    73 
       
    74         // test 4
       
    75         System.out.println
       
    76             ("test 4: implies returns false for match on name but not action");
       
    77         if (perms.implies(new ServicePermission(FOO, "initiate"))) {
       
    78             System.err.println("Expected false, returned true");
       
    79             testFail++;
       
    80         }
       
    81 
       
    82         // test 5
       
    83         System.out.println("test 5: implies returns true for match on " +
       
    84                            "name and subset of actions");
       
    85         perms.add(new ServicePermission(BAR, "accept, initiate"));
       
    86         if (!perms.implies(new ServicePermission(BAR, "accept"))) {
       
    87             System.err.println("Expected true, returned false");
       
    88             testFail++;
       
    89         }
       
    90 
       
    91         // test 6
       
    92         System.out.println("test 6: implies returns false for aggregate " +
       
    93                            "match on name and action");
       
    94         perms.add(new ServicePermission(BAZ, "accept"));
       
    95         perms.add(new ServicePermission(BAZ, "initiate"));
       
    96         if (!perms.implies(new ServicePermission(BAZ, "initiate"))) {
       
    97             System.err.println("Expected true, returned false");
       
    98             testFail++;
       
    99         }
       
   100         if (!perms.implies(new ServicePermission(BAZ, "initiate, accept"))) {
       
   101             System.err.println("Expected true, returned false");
       
   102             testFail++;
       
   103         }
       
   104 
       
   105         // test 7
       
   106         System.out.println("test 7: implies returns true for wildcard " +
       
   107                            "match on name and action");
       
   108         perms.add(new ServicePermission("*", "initiate"));
       
   109         if (!perms.implies(new ServicePermission("Duke", "initiate"))) {
       
   110             System.err.println("Expected true, returned false");
       
   111             testFail++;
       
   112         }
       
   113 
       
   114         // test 8
       
   115         System.out.println("test 8: elements returns correct number of perms");
       
   116         int numPerms = 0;
       
   117         Enumeration<Permission> e = perms.elements();
       
   118         while (e.hasMoreElements()) {
       
   119             numPerms++;
       
   120             System.out.println(e.nextElement());
       
   121         }
       
   122         // the 2 FOO permissions and the 2 BAZ permisssions
       
   123         // are combined into one
       
   124         if (numPerms != 4) {
       
   125             System.err.println("Expected 4, got " + numPerms);
       
   126             testFail++;
       
   127         }
       
   128 
       
   129         if (testFail > 0) {
       
   130             throw new Exception(testFail + " test(s) failed");
       
   131         }
       
   132     }
       
   133 }