jdk/test/java/security/PermissionCollection/AddToReadOnlyPermissionCollection.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 5506 202f599c92aa
child 32932 9250ad475b43
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Gary Ellison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4232694
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary PermissionCollection.setReadOnly() does not preclude using add()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.PropertyPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class AddToReadOnlyPermissionCollection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                tryAllPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                tryBasicPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                tryFilePC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                tryPropPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                trySockPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                for (int i=0; i <args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                    switch (args[i].toLowerCase().charAt(1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                        tryAllPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                    case 'b':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                        tryBasicPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                        tryFilePC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    case 'p':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                        tryPropPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    case 's':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                        trySockPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        throw new Exception("usage: AddToReadOnlyPermissonCollection [-a -b -f -p -s]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("Passed. OKAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void tryPropPC() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            PropertyPermission p0 = new PropertyPermission("user.home","read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            PermissionCollection pc = p0.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            pc.setReadOnly();   // this should lock out future adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            PropertyPermission p1 = new PropertyPermission("java.home","read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            pc.add(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                Exception("Failed...PropertyPermission added to readonly PropertyPermissionCollection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println("PropertyPermissionCollection passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static void trySockPC() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            SocketPermission p0= new SocketPermission("example.com","connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            PermissionCollection pc = p0.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            pc.setReadOnly();   // this should lock out future adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            SocketPermission p1= new SocketPermission("example.net","connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            pc.add(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                Exception("Failed...SocketPermission added to readonly SocketPermissionCollection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("SocketPermissionCollection passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void tryFilePC() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            FilePermission p0 = new FilePermission("/home/foobar","read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            PermissionCollection pc = p0.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            pc.setReadOnly();   // this should lock out future adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            FilePermission p1 = new FilePermission("/home/quux","read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            pc.add(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                Exception("Failed...FilePermission added to readonly FilePermissionCollection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("FilePermissionCollection passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static void tryBasicPC() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            MyBasicPermission p0 = new MyBasicPermission("BasicPermision");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            PermissionCollection pc = p0.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            pc.setReadOnly();   // this should lock out future adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            MyBasicPermission p1 = new MyBasicPermission("EvenMoreBasic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            pc.add(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                Exception("Failed...BasicPermission added to readonly BasicPermissionCollection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.out.println("BasicPermissionCollection passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void tryAllPC() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            AllPermission p0 = new AllPermission("AllOfIt","read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            PermissionCollection pc = p0.newPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            pc.setReadOnly();   // this should lock out future adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            AllPermission p1 = new AllPermission("SomeOfIt","read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            pc.add(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                Exception("Failed...AllPermission added to readonly AllPermissionCollection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("AllPermissionCollection passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
class MyBasicPermission extends BasicPermission {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public MyBasicPermission(String name)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}