TestPolicy.java revision 10904:126b5b4a9ce4
1/*
2 * Copyright (c) 2014, 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 */
23package util;
24
25import java.io.FilePermission;
26import java.security.AllPermission;
27import java.security.CodeSource;
28import java.security.Permission;
29import java.security.PermissionCollection;
30import java.security.Permissions;
31import java.security.Policy;
32import java.security.ProtectionDomain;
33import java.security.SecurityPermission;
34import java.sql.SQLPermission;
35import java.util.Enumeration;
36import java.util.PropertyPermission;
37import java.util.StringJoiner;
38
39/*
40 * Simple Policy class that supports the required Permissions to validate the
41 * JDBC concrete classes
42 */
43public class TestPolicy extends Policy {
44
45    final PermissionCollection permissions = new Permissions();
46
47    /**
48     * Constructor which sets the minimum permissions allowing testNG to work
49     * with a SecurityManager
50     */
51    public TestPolicy() {
52        setMinimalPermissions();
53    }
54
55    /*
56     * Constructor which determines which permissions are defined for this
57     * Policy used by the JDBC tests Possible values are: all (ALLPermissions),
58     * setLog (SQLPemission("setLog"), deregisterDriver
59     * (SQLPermission("deregisterDriver") (SQLPermission("deregisterDriver"),
60     * and setSyncFactory(SQLPermission(setSyncFactory),
61     *
62     * @param policy Permissions to set
63     */
64    public TestPolicy(String policy) {
65
66        switch (policy) {
67            case "all":
68                permissions.add(new AllPermission());
69                break;
70            case "setLog":
71                setMinimalPermissions();
72                permissions.add(new SQLPermission("setLog"));
73                break;
74            case "deregisterDriver":
75                setMinimalPermissions();
76                permissions.add(new SQLPermission("deregisterDriver"));
77                break;
78            case "setSyncFactory":
79                setMinimalPermissions();
80                permissions.add(new SQLPermission("setSyncFactory"));
81                break;
82            default:
83                setMinimalPermissions();
84        }
85    }
86
87    /*
88     * Defines the minimal permissions required by testNG when running these
89     * tests
90     */
91    private void setMinimalPermissions() {
92        permissions.add(new SecurityPermission("getPolicy"));
93        permissions.add(new SecurityPermission("setPolicy"));
94        permissions.add(new RuntimePermission("getClassLoader"));
95        permissions.add(new RuntimePermission("setSecurityManager"));
96        permissions.add(new RuntimePermission("createSecurityManager"));
97        permissions.add(new PropertyPermission("testng.show.stack.frames",
98                "read"));
99        permissions.add(new PropertyPermission("line.separator", "read"));
100        permissions.add(new PropertyPermission("fileStringBuffer", "read"));
101        permissions.add(new PropertyPermission("dataproviderthreadcount", "read"));
102        permissions.add(new PropertyPermission("java.io.tmpdir", "read"));
103        permissions.add(new FilePermission("<<ALL FILES>>",
104                "read, write, delete"));
105    }
106
107    /*
108     * Overloaded methods from the Policy class
109     */
110    @Override
111    public String toString() {
112        StringJoiner sj = new StringJoiner("\n", "policy: ", "");
113        Enumeration<Permission> perms = permissions.elements();
114        while (perms.hasMoreElements()) {
115            sj.add(perms.nextElement().toString());
116        }
117        return sj.toString();
118
119    }
120
121    @Override
122    public PermissionCollection getPermissions(ProtectionDomain domain) {
123        return permissions;
124    }
125
126    @Override
127    public PermissionCollection getPermissions(CodeSource codesource) {
128        return permissions;
129    }
130
131    @Override
132    public boolean implies(ProtectionDomain domain, Permission perm) {
133        return permissions.implies(perm);
134    }
135}
136