Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/PermissionCollection/Concurrent.java
38811 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 467159826* @summary PermissionCollection is not properly synchronized27*/2829import java.security.*; // AllPermission, BasicPermission, Permissions30import java.net.NetPermission;31import java.net.SocketPermission;32import java.io.FilePermission;33import java.util.PropertyPermission;34import javax.security.auth.AuthPermission;35import javax.security.auth.kerberos.DelegationPermission;36import javax.security.auth.kerberos.ServicePermission;37import javax.management.MBeanServerPermission;38import com.sun.rmi.rmid.ExecPermission;39import com.sun.rmi.rmid.ExecOptionPermission;4041import java.util.*;4243public class Concurrent {44private static final int LIMIT1 = 2000;45private static final int LIMIT2 = 1000;46private static final boolean debug = false;47private static final Map errors =48Collections.synchronizedMap(new HashMap());4950public static void main(String args[]) throws Exception {51testPc(allp);52testPc(filep);53testPc(sockp);54testPc(propp);55testPc(basicp);56testPc(delegatep);57testPc(servicep);58testPc(mbeanp);59testPc(unresp);6061testPerms();6263if (errors.size() > 0) {64if (true) {65Iterator iter = errors.entrySet().iterator();66while (iter.hasNext()) {67System.out.println(iter.next());68}69};70throw (Exception) new Exception("Got errors");71}72}7374private static void testPc (final Permission[] perm) throws Exception {7576final PermissionCollection pc = perm[0].newPermissionCollection();7778new Thread() {79{80setDaemon(true);81start();82}83public void run() {84try {85for (int i = 0; i < LIMIT1; i++) {86for (int j = 0; j < perm.length; j++) {87pc.add(perm[j]);88if (debug) {89System.out.println("added " + perm[j]);90}91}92}93} catch (Exception e) {94errors.put(perm[0].getClass().getName(), e);95}96}97};98try {99for (int i = 0; i < LIMIT2; i++) {100boolean result = pc.implies(perm[perm.length-1]);101if (debug) {102System.out.println(perm[perm.length-1] + " implies " + result);103}104105synchronized (pc) {106Enumeration en = pc.elements();107while (en.hasMoreElements()) {108Object obj = en.nextElement();109if (debug) {110System.out.println(obj);111}112}113}114}115} catch (Exception e) {116errors.put(perm[0].getClass().getName(), e);117}118}119120private static void testPerms () throws Exception {121122final Permissions pc = new Permissions();123124new Thread() {125{126setDaemon(true);127start();128}129public void run() {130try {131for (int i = 0; i < LIMIT1; i++) {132for (int j = 0; j < permlist.length; j++) {133for (int k = 0; k < permlist[j].length; k++) {134pc.add(permlist[j][k]);135}136}137}138} catch (Exception e) {139errors.put("java.security.Permissions", e);140}141}142};143try {144for (int i = 0; i < LIMIT2; i++) {145for (int j = 0; j < permlist.length; j++) {146boolean result = pc.implies(permlist[j][0]);147if (debug) {148System.out.println(permlist[j][0] + " implies " + result);149}150}151152synchronized (pc) {153Enumeration en = pc.elements();154while (en.hasMoreElements()) {155Object obj = en.nextElement();156if (debug) {157System.out.println(obj);158}159}160}161}162} catch (Exception e) {163errors.put("java.security.Permissions", e);164}165}166167private static final Permission[] allp = new Permission[]{168new AllPermission(), new AllPermission()};169170private static final Permission[] filep = new Permission[]{171new FilePermission("/home/foobar", "read"),172new FilePermission("/home/foo", "write"),173new FilePermission("/home/foobar", "read,write"),174};175176private static final Permission[] sockp = new Permission[]{177new SocketPermission("example.net", "connect"),178new SocketPermission("www.sun.com", "resolve"),179new SocketPermission("www.test1.com", "accept"),180new SocketPermission("www.test3.com", "resolve,connect"),181new SocketPermission("www.test4.com", "listen"),182};183184private static final Permission[] propp = new Permission[]{185new PropertyPermission("user.home", "read"),186new PropertyPermission("java.home", "write"),187new PropertyPermission("test.home", "write"),188new PropertyPermission("test1.home", "read"),189new PropertyPermission("test2.home", "read"),190};191192private static final Permission[] basicp = new Permission[] {193new NetPermission("setDefaultAuthenticator"),194new NetPermission("requestPasswordAuthentication"),195new NetPermission("specifyStreamHandler")196};197198private static final Permission[] delegatep = new Permission[] {199new DelegationPermission(200"\"host/[email protected]\" \"cn=John,o=imc,c=us\""),201new DelegationPermission(202"\"user/[email protected]\" \"cn=John,o=imc,c=us\""),203new DelegationPermission(204"\"host/[email protected]\" \"cn=John,o=imc,c=us\"")205};206207private static final Permission[] servicep = new Permission[]{208new ServicePermission("krbtgt/[email protected]", "initiate"),209new ServicePermission("ldap/[email protected]", "initiate"),210new ServicePermission("imap/[email protected]", "accept"),211new ServicePermission("acap/[email protected]", "initiate"),212new ServicePermission("host/[email protected]", "initiate"),213};214215private static final Permission[] mbeanp = new Permission[] {216new MBeanServerPermission("createMBeanServer"),217new MBeanServerPermission("findMBeanServer"),218new MBeanServerPermission("newMBeanServer"),219new MBeanServerPermission("releaseMBeanServer"),220};221222private static final Permission[] unresp = new Permission[] {223new UnresolvedPermission("com.unknown.TestClass", "UnknownPermission",224"read,write", null),225new UnresolvedPermission("com.unknown.TestClass", "APermission",226"read,write", null),227new UnresolvedPermission("com.unknown.TestClass", "BPermission",228"read,write", null),229new UnresolvedPermission("com.unknown.CClass", "CPermission",230"read,write", null),231new UnresolvedPermission("com.unknown.DClass", "DUnknownPermission",232"read,write", null),233new UnresolvedPermission("com.unknown.EClass", "EUnknownPermission",234"read,write", null),235};236237private static final Permission[][] permlist = new Permission[][]{238allp, filep, sockp, propp, basicp, delegatep, servicep, mbeanp, unresp};239}240241242