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