0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4671598
0N/A * @summary PermissionCollection is not properly synchronized
0N/A */
0N/A
0N/Aimport java.security.*; // AllPermission, BasicPermission, Permissions
0N/Aimport java.net.NetPermission;
0N/Aimport java.net.SocketPermission;
0N/Aimport java.io.FilePermission;
0N/Aimport java.util.PropertyPermission;
0N/Aimport javax.security.auth.AuthPermission;
0N/Aimport javax.security.auth.kerberos.DelegationPermission;
0N/Aimport javax.security.auth.kerberos.ServicePermission;
0N/Aimport javax.management.MBeanServerPermission;
0N/Aimport com.sun.rmi.rmid.ExecPermission;
0N/Aimport com.sun.rmi.rmid.ExecOptionPermission;
0N/A
0N/Aimport java.util.*;
0N/A
0N/Apublic class Concurrent {
0N/A private static final int LIMIT1 = 2000;
0N/A private static final int LIMIT2 = 1000;
0N/A private static final boolean debug = false;
0N/A private static final Map errors =
0N/A Collections.synchronizedMap(new HashMap());
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A testPc(allp);
0N/A testPc(filep);
0N/A testPc(sockp);
0N/A testPc(propp);
0N/A testPc(basicp);
0N/A testPc(delegatep);
0N/A testPc(servicep);
0N/A testPc(mbeanp);
0N/A testPc(unresp);
0N/A
0N/A testPerms();
0N/A
0N/A if (errors.size() > 0) {
0N/A if (true) {
0N/A Iterator iter = errors.entrySet().iterator();
0N/A while (iter.hasNext()) {
0N/A System.out.println(iter.next());
0N/A }
0N/A };
0N/A throw (Exception) new Exception("Got errors");
0N/A }
0N/A }
0N/A
0N/A private static void testPc (final Permission[] perm) throws Exception {
0N/A
0N/A final PermissionCollection pc = perm[0].newPermissionCollection();
0N/A
0N/A new Thread() {
0N/A {
0N/A setDaemon(true);
0N/A start();
0N/A }
0N/A public void run() {
0N/A try {
0N/A for (int i = 0; i < LIMIT1; i++) {
0N/A for (int j = 0; j < perm.length; j++) {
0N/A pc.add(perm[j]);
0N/A if (debug) {
0N/A System.out.println("added " + perm[j]);
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A errors.put(perm[0].getClass().getName(), e);
0N/A }
0N/A }
0N/A };
0N/A try {
0N/A for (int i = 0; i < LIMIT2; i++) {
0N/A boolean result = pc.implies(perm[perm.length-1]);
0N/A if (debug) {
0N/A System.out.println(perm[perm.length-1] + " implies " + result);
0N/A }
0N/A
0N/A synchronized (pc) {
0N/A Enumeration en = pc.elements();
0N/A while (en.hasMoreElements()) {
0N/A Object obj = en.nextElement();
0N/A if (debug) {
0N/A System.out.println(obj);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A errors.put(perm[0].getClass().getName(), e);
0N/A }
0N/A }
0N/A
0N/A private static void testPerms () throws Exception {
0N/A
0N/A final Permissions pc = new Permissions();
0N/A
0N/A new Thread() {
0N/A {
0N/A setDaemon(true);
0N/A start();
0N/A }
0N/A public void run() {
0N/A try {
0N/A for (int i = 0; i < LIMIT1; i++) {
0N/A for (int j = 0; j < permlist.length; j++) {
0N/A for (int k = 0; k < permlist[j].length; k++) {
0N/A pc.add(permlist[j][k]);
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A errors.put("java.security.Permissions", e);
0N/A }
0N/A }
0N/A };
0N/A try {
0N/A for (int i = 0; i < LIMIT2; i++) {
0N/A for (int j = 0; j < permlist.length; j++) {
0N/A boolean result = pc.implies(permlist[j][0]);
0N/A if (debug) {
0N/A System.out.println(permlist[j][0] + " implies " + result);
0N/A }
0N/A }
0N/A
0N/A synchronized (pc) {
0N/A Enumeration en = pc.elements();
0N/A while (en.hasMoreElements()) {
0N/A Object obj = en.nextElement();
0N/A if (debug) {
0N/A System.out.println(obj);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A errors.put("java.security.Permissions", e);
0N/A }
0N/A }
0N/A
0N/A private static final Permission[] allp = new Permission[]{
0N/A new AllPermission(), new AllPermission()};
0N/A
0N/A private static final Permission[] filep = new Permission[]{
0N/A new FilePermission("/home/foobar", "read"),
0N/A new FilePermission("/home/foo", "write"),
0N/A new FilePermission("/home/foobar", "read,write"),
0N/A };
0N/A
0N/A private static final Permission[] sockp = new Permission[]{
0N/A new SocketPermission("example.net", "connect"),
0N/A new SocketPermission("www.sun.com", "resolve"),
0N/A new SocketPermission("www.test1.com", "accept"),
0N/A new SocketPermission("www.test3.com", "resolve,connect"),
0N/A new SocketPermission("www.test4.com", "listen"),
0N/A };
0N/A
0N/A private static final Permission[] propp = new Permission[]{
0N/A new PropertyPermission("user.home", "read"),
0N/A new PropertyPermission("java.home", "write"),
0N/A new PropertyPermission("test.home", "write"),
0N/A new PropertyPermission("test1.home", "read"),
0N/A new PropertyPermission("test2.home", "read"),
0N/A };
0N/A
0N/A private static final Permission[] basicp = new Permission[] {
0N/A new NetPermission("setDefaultAuthenticator"),
0N/A new NetPermission("requestPasswordAuthentication"),
0N/A new NetPermission("specifyStreamHandler")
0N/A };
0N/A
0N/A private static final Permission[] delegatep = new Permission[] {
0N/A new DelegationPermission(
0N/A "\"host/foo.example.com@EXAMPLE.COM\" \"cn=John,o=imc,c=us\""),
0N/A new DelegationPermission(
0N/A "\"user/rosanna@EXAMPLE.COM\" \"cn=John,o=imc,c=us\""),
0N/A new DelegationPermission(
0N/A "\"host/bar.example.com@EXAMPLE.COM\" \"cn=John,o=imc,c=us\"")
0N/A };
0N/A
0N/A private static final Permission[] servicep = new Permission[]{
0N/A new ServicePermission("krbtgt/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
0N/A new ServicePermission("ldap/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
0N/A new ServicePermission("imap/EXAMPLE.COM@EXAMPLE.COM", "accept"),
0N/A new ServicePermission("acap/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
0N/A new ServicePermission("host/EXAMPLE.COM@EXAMPLE.COM", "initiate"),
0N/A };
0N/A
0N/A private static final Permission[] mbeanp = new Permission[] {
0N/A new MBeanServerPermission("createMBeanServer"),
0N/A new MBeanServerPermission("findMBeanServer"),
0N/A new MBeanServerPermission("newMBeanServer"),
0N/A new MBeanServerPermission("releaseMBeanServer"),
0N/A };
0N/A
0N/A private static final Permission[] unresp = new Permission[] {
0N/A new UnresolvedPermission("com.unknown.TestClass", "UnknownPermission",
0N/A "read,write", null),
0N/A new UnresolvedPermission("com.unknown.TestClass", "APermission",
0N/A "read,write", null),
0N/A new UnresolvedPermission("com.unknown.TestClass", "BPermission",
0N/A "read,write", null),
0N/A new UnresolvedPermission("com.unknown.CClass", "CPermission",
0N/A "read,write", null),
0N/A new UnresolvedPermission("com.unknown.DClass", "DUnknownPermission",
0N/A "read,write", null),
0N/A new UnresolvedPermission("com.unknown.EClass", "EUnknownPermission",
0N/A "read,write", null),
0N/A };
0N/A
0N/A private static final Permission[][] permlist = new Permission[][]{
0N/A allp, filep, sockp, propp, basicp, delegatep, servicep, mbeanp, unresp};
0N/A}