1 /*
   2  * Copyright (c) 2003, 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 4671598
  27  * @summary PermissionCollection is not properly synchronized
  28  */
  29 
  30 import java.security.*; // AllPermission, BasicPermission, Permissions
  31 import java.net.NetPermission;
  32 import java.net.SocketPermission;
  33 import java.io.FilePermission;
  34 import java.util.PropertyPermission;
  35 import javax.security.auth.AuthPermission;
  36 import javax.security.auth.kerberos.DelegationPermission;
  37 import javax.security.auth.kerberos.ServicePermission;
  38 import javax.management.MBeanServerPermission;
  39 import com.sun.rmi.rmid.ExecPermission;
  40 import com.sun.rmi.rmid.ExecOptionPermission;
  41 
  42 import java.util.*;
  43 
  44 public class Concurrent {
  45     private static final int LIMIT1 = 2000;
  46     private static final int LIMIT2 = 1000;
  47     private static final boolean debug = false;
  48     private static final Map errors =
  49         Collections.synchronizedMap(new HashMap());
  50 
  51     public static void main(String args[]) throws Exception {
  52         testPc(allp);
  53         testPc(filep);
  54         testPc(sockp);
  55         testPc(propp);
  56         testPc(basicp);
  57         testPc(delegatep);
  58         testPc(servicep);
  59         testPc(mbeanp);
  60         testPc(unresp);
  61 
  62         testPerms();
  63 
  64         if (errors.size() > 0) {
  65             if (true) {
  66                 Iterator iter = errors.entrySet().iterator();
  67                 while (iter.hasNext()) {
  68                     System.out.println(iter.next());
  69                 }
  70             };
  71             throw (Exception) new Exception("Got errors");
  72         }
  73     }
  74 
  75     private static void testPc (final Permission[] perm) throws Exception {
  76 
  77         final PermissionCollection pc = perm[0].newPermissionCollection();
  78 
  79         new Thread() {
  80             {
  81                 setDaemon(true);
  82                 start();
  83             }
  84             public void run() {
  85                 try {
  86                     for (int i = 0; i < LIMIT1; i++) {
  87                         for (int j = 0; j < perm.length; j++) {
  88                             pc.add(perm[j]);
  89                             if (debug) {
  90                                 System.out.println("added " + perm[j]);
  91                             }
  92                         }
  93                     }
  94                 } catch (Exception e) {
  95                     errors.put(perm[0].getClass().getName(), e);
  96                 }
  97             }
  98         };
  99         try {
 100             for (int i = 0; i < LIMIT2; i++) {
 101                 boolean result = pc.implies(perm[perm.length-1]);
 102                 if (debug) {
 103                     System.out.println(perm[perm.length-1] + " implies " + result);
 104                 }
 105 
 106                 synchronized (pc) {
 107                     Enumeration en = pc.elements();
 108                     while (en.hasMoreElements()) {
 109                         Object obj = en.nextElement();
 110                         if (debug) {
 111                             System.out.println(obj);
 112                         }
 113                     }
 114                 }
 115             }
 116         } catch (Exception e) {
 117             errors.put(perm[0].getClass().getName(), e);
 118         }
 119     }
 120 
 121     private static void testPerms () throws Exception {
 122 
 123         final Permissions pc = new Permissions();
 124 
 125         new Thread() {
 126             {
 127                 setDaemon(true);
 128                 start();
 129             }
 130             public void run() {
 131                 try {
 132                     for (int i = 0; i < LIMIT1; i++) {
 133                         for (int j = 0; j < permlist.length; j++) {
 134                             for (int k = 0; k < permlist[j].length; k++) {
 135                                 pc.add(permlist[j][k]);
 136                             }
 137                         }
 138                     }
 139                 } catch (Exception e) {
 140                     errors.put("java.security.Permissions", e);
 141                 }
 142             }
 143         };
 144         try {
 145             for (int i = 0; i < LIMIT2; i++) {
 146                 for (int j = 0; j < permlist.length; j++) {
 147                     boolean result = pc.implies(permlist[j][0]);
 148                     if (debug) {
 149                         System.out.println(permlist[j][0] + " implies " + result);
 150                     }
 151                 }
 152 
 153                 synchronized (pc) {
 154                     Enumeration en = pc.elements();
 155                     while (en.hasMoreElements()) {
 156                         Object obj = en.nextElement();
 157                         if (debug) {
 158                             System.out.println(obj);
 159                         }
 160                     }
 161                 }
 162             }
 163         } catch (Exception e) {
 164             errors.put("java.security.Permissions", e);
 165         }
 166     }
 167 
 168     private static final Permission[] allp = new Permission[]{
 169         new AllPermission(), new AllPermission()};
 170 
 171     private static final Permission[] filep = new Permission[]{
 172         new FilePermission("/home/foobar", "read"),
 173         new FilePermission("/home/foo", "write"),
 174         new FilePermission("/home/foobar", "read,write"),
 175             };
 176 
 177     private static final Permission[] sockp = new Permission[]{
 178         new SocketPermission("example.net", "connect"),
 179             new SocketPermission("www.sun.com", "resolve"),
 180             new SocketPermission("www.test1.com", "accept"),
 181             new SocketPermission("www.test3.com", "resolve,connect"),
 182             new SocketPermission("www.test4.com", "listen"),
 183             };
 184 
 185     private static final Permission[] propp = new Permission[]{
 186         new PropertyPermission("user.home", "read"),
 187             new PropertyPermission("java.home", "write"),
 188             new PropertyPermission("test.home", "write"),
 189             new PropertyPermission("test1.home", "read"),
 190             new PropertyPermission("test2.home", "read"),
 191             };
 192 
 193     private static final Permission[] basicp = new Permission[] {
 194         new NetPermission("setDefaultAuthenticator"),
 195             new NetPermission("requestPasswordAuthentication"),
 196             new NetPermission("specifyStreamHandler")
 197             };
 198 
 199     private static final Permission[] delegatep = new Permission[] {
 200         new DelegationPermission(
 201             "\"host/foo.example.com@EXAMPLE.COM\" \"cn=John,o=imc,c=us\""),
 202         new DelegationPermission(
 203             "\"user/rosanna@EXAMPLE.COM\" \"cn=John,o=imc,c=us\""),
 204         new DelegationPermission(
 205             "\"host/bar.example.com@EXAMPLE.COM\" \"cn=John,o=imc,c=us\"")
 206             };
 207 
 208     private static final Permission[] servicep = new Permission[]{
 209         new ServicePermission("krbtgt/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
 210         new ServicePermission("ldap/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
 211         new ServicePermission("imap/EXAMPLE.COM@EXAMPLE.COM", "accept"),
 212         new ServicePermission("acap/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
 213         new ServicePermission("host/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
 214             };
 215 
 216     private static final Permission[] mbeanp = new Permission[] {
 217         new MBeanServerPermission("createMBeanServer"),
 218         new MBeanServerPermission("findMBeanServer"),
 219         new MBeanServerPermission("newMBeanServer"),
 220         new MBeanServerPermission("releaseMBeanServer"),
 221             };
 222 
 223     private static final Permission[] unresp = new Permission[] {
 224         new UnresolvedPermission("com.unknown.TestClass", "UnknownPermission",
 225             "read,write", null),
 226         new UnresolvedPermission("com.unknown.TestClass", "APermission",
 227             "read,write", null),
 228         new UnresolvedPermission("com.unknown.TestClass", "BPermission",
 229             "read,write", null),
 230         new UnresolvedPermission("com.unknown.CClass", "CPermission",
 231             "read,write", null),
 232         new UnresolvedPermission("com.unknown.DClass", "DUnknownPermission",
 233             "read,write", null),
 234         new UnresolvedPermission("com.unknown.EClass", "EUnknownPermission",
 235             "read,write", null),
 236             };
 237 
 238     private static final Permission[][] permlist = new Permission[][]{
 239         allp, filep, sockp, propp, basicp, delegatep, servicep, mbeanp, unresp};
 240 }