Path: blob/master/test/jdk/java/security/PermissionCollection/AddToReadOnlyPermissionCollection.java
41149 views
/*1* Copyright (c) 1999, 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* @author Gary Ellison26* @bug 423269427* @summary PermissionCollection.setReadOnly() does not preclude using add()28*/2930import java.security.*;31import java.net.SocketPermission;32import java.io.FilePermission;33import java.util.PropertyPermission;3435public class AddToReadOnlyPermissionCollection {36public static void main(String args[]) throws Exception {3738try {39if (args.length == 0) {40tryAllPC();41tryBasicPC();42tryFilePC();43tryPropPC();44trySockPC();45} else {46for (int i=0; i <args.length; i++) {47switch (args[i].toLowerCase().charAt(1)) {48case 'a':49tryAllPC();50break;51case 'b':52tryBasicPC();53break;54case 'f':55tryFilePC();56break;57case 'p':58tryPropPC();59break;60case 's':61trySockPC();62break;63default:64throw new Exception("usage: AddToReadOnlyPermissonCollection [-a -b -f -p -s]");65}66}67}68} catch (Exception e) {69throw e;70}71System.out.println("Passed. OKAY");72}7374static void tryPropPC() throws Exception {75try {76PropertyPermission p0 = new PropertyPermission("user.home","read");77PermissionCollection pc = p0.newPermissionCollection();78pc.setReadOnly(); // this should lock out future adds79//80PropertyPermission p1 = new PropertyPermission("java.home","read");81pc.add(p1);82throw new83Exception("Failed...PropertyPermission added to readonly PropertyPermissionCollection.");8485} catch (SecurityException se) {86System.out.println("PropertyPermissionCollection passed");87}88}8990static void trySockPC() throws Exception {91try {92SocketPermission p0= new SocketPermission("example.com","connect");93PermissionCollection pc = p0.newPermissionCollection();94pc.setReadOnly(); // this should lock out future adds95//96SocketPermission p1= new SocketPermission("example.net","connect");97pc.add(p1);98throw new99Exception("Failed...SocketPermission added to readonly SocketPermissionCollection.");100101} catch (SecurityException se) {102System.out.println("SocketPermissionCollection passed");103}104105}106107static void tryFilePC() throws Exception {108try {109FilePermission p0 = new FilePermission("/tmp/foobar","read");110PermissionCollection pc = p0.newPermissionCollection();111pc.setReadOnly(); // this should lock out future adds112//113FilePermission p1 = new FilePermission("/tmp/quux","read");114pc.add(p1);115throw new116Exception("Failed...FilePermission added to readonly FilePermissionCollection.");117118} catch (SecurityException se) {119System.out.println("FilePermissionCollection passed");120}121}122123static void tryBasicPC() throws Exception {124try {125MyBasicPermission p0 = new MyBasicPermission("BasicPermision");126PermissionCollection pc = p0.newPermissionCollection();127pc.setReadOnly(); // this should lock out future adds128//129MyBasicPermission p1 = new MyBasicPermission("EvenMoreBasic");130pc.add(p1);131throw new132Exception("Failed...BasicPermission added to readonly BasicPermissionCollection.");133134} catch (SecurityException se) {135System.out.println("BasicPermissionCollection passed");136}137}138139static void tryAllPC() throws Exception {140try {141AllPermission p0 = new AllPermission("AllOfIt","read");142PermissionCollection pc = p0.newPermissionCollection();143pc.setReadOnly(); // this should lock out future adds144//145AllPermission p1 = new AllPermission("SomeOfIt","read");146pc.add(p1);147throw new148Exception("Failed...AllPermission added to readonly AllPermissionCollection.");149150} catch (SecurityException se) {151System.out.println("AllPermissionCollection passed");152}153}154155}156157class MyBasicPermission extends BasicPermission {158public MyBasicPermission(String name) {159super(name);160}161}162163164