jdk/test/java/security/BasicPermission/BasicPermissionCollection.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 BasicPermissionCollection subclass
       
    28  */
       
    29 
       
    30 import java.security.BasicPermission;
       
    31 import java.security.Permission;
       
    32 import java.security.PermissionCollection;
       
    33 import java.security.SecurityPermission;
       
    34 import java.util.Enumeration;
       
    35 
       
    36 public class BasicPermissionCollection {
       
    37 
       
    38     public static void main(String[] args) throws Exception {
       
    39 
       
    40         int testFail = 0;
       
    41 
       
    42         TestPermission perm = new TestPermission("foo");
       
    43         PermissionCollection perms = perm.newPermissionCollection();
       
    44 
       
    45         // test 1
       
    46         System.out.println("test 1: add throws IllegalArgumentExc");
       
    47         try {
       
    48             perms.add(new SecurityPermission("createAccessControlContext"));
       
    49             System.err.println("Expected IllegalArgumentException");
       
    50             testFail++;
       
    51         } catch (IllegalArgumentException iae) {}
       
    52 
       
    53         // test 2
       
    54         System.out.println("test 2: implies returns false for wrong class");
       
    55         if (perms.implies(new SecurityPermission("getPolicy"))) {
       
    56             System.err.println("Expected false, returned true");
       
    57             testFail++;
       
    58         }
       
    59 
       
    60         // test 3
       
    61         System.out.println("test 3: implies returns true for match on name");
       
    62         perms.add(new TestPermission("foo"));
       
    63         if (!perms.implies(new TestPermission("foo"))) {
       
    64             System.err.println("Expected true, returned false");
       
    65             testFail++;
       
    66         }
       
    67 
       
    68         // test 4
       
    69         System.out.println("test 4: implies returns true for wildcard match");
       
    70         perms.add(new TestPermission("bar.*"));
       
    71         if (!perms.implies(new TestPermission("bar.foo"))) {
       
    72             System.err.println("Expected true, returned false");
       
    73             testFail++;
       
    74         }
       
    75 
       
    76         // test 5
       
    77         System.out.println
       
    78             ("test 5: implies returns false for invalid wildcard");
       
    79         perms.add(new TestPermission("baz*"));
       
    80         if (perms.implies(new TestPermission("baz.foo"))) {
       
    81             System.err.println("Expected false, returned true");
       
    82             testFail++;
       
    83         }
       
    84 
       
    85         // test 6
       
    86         System.out.println
       
    87             ("test 6: implies returns true for deep wildcard match");
       
    88         if (!perms.implies(new TestPermission("bar.foo.baz"))) {
       
    89             System.err.println("Expected true, returned false");
       
    90             testFail++;
       
    91         }
       
    92 
       
    93         // test 7
       
    94         System.out.println
       
    95             ("test 7: implies returns true for all wildcard match");
       
    96         perms.add(new TestPermission("*"));
       
    97         if (!perms.implies(new TestPermission("yes"))) {
       
    98             System.err.println("Expected true, returned false");
       
    99             testFail++;
       
   100         }
       
   101 
       
   102         // test 8
       
   103         System.out.println("test 8: elements returns correct number of perms");
       
   104         int numPerms = 0;
       
   105         Enumeration<Permission> e = perms.elements();
       
   106         while (e.hasMoreElements()) {
       
   107             numPerms++;
       
   108             System.out.println(e.nextElement());
       
   109         }
       
   110         if (numPerms != 4) {
       
   111             System.err.println("Expected 4, got " + numPerms);
       
   112             testFail++;
       
   113         }
       
   114 
       
   115         if (testFail > 0) {
       
   116             throw new Exception(testFail + " test(s) failed");
       
   117         }
       
   118     }
       
   119 
       
   120     private static class TestPermission extends BasicPermission {
       
   121         TestPermission(String name) {
       
   122             super(name);
       
   123         }
       
   124     }
       
   125 }