Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/sql/permissionTests/DriverManagerPermissionsTests.java
41149 views
1
/*
2
* Copyright (c) 2014, 2021, 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
24
import org.testng.annotations.AfterClass;
25
import org.testng.annotations.BeforeClass;
26
import org.testng.annotations.Test;
27
import util.BaseTest;
28
import util.StubDriver;
29
import util.TestPolicy;
30
31
import java.security.AccessControlException;
32
import java.security.Policy;
33
import java.sql.DriverManager;
34
import java.sql.SQLException;
35
36
/*
37
* @test
38
* @library /java/sql/testng
39
* @run testng/othervm -Djava.security.manager=allow DriverManagerPermissionsTests
40
* @summary Tests that a JDBC Driver that is a module can be loaded
41
* via the service-provider loading mechanism.
42
*/
43
public class DriverManagerPermissionsTests extends BaseTest {
44
45
private static Policy policy;
46
private static SecurityManager sm;
47
48
/*
49
* Install a SecurityManager along with a base Policy to allow testNG to run
50
*/
51
@BeforeClass
52
public static void setUpClass() throws Exception {
53
setPolicy(new TestPolicy());
54
System.setSecurityManager(new SecurityManager());
55
}
56
57
/*
58
* Install the original Policy and SecurityManager
59
*/
60
@AfterClass
61
public static void tearDownClass() throws Exception {
62
System.setSecurityManager(sm);
63
setPolicy(policy);
64
}
65
66
/*
67
* Save off the original Policy and SecurityManager
68
*/
69
public DriverManagerPermissionsTests() {
70
policy = Policy.getPolicy();
71
sm = System.getSecurityManager();
72
}
73
74
/*
75
* Validate that AccessControlException is thrown if SQLPermission("setLog")
76
* has not been granted
77
*/
78
@Test(expectedExceptions = AccessControlException.class)
79
public void test() {
80
setPolicy(new TestPolicy());
81
DriverManager.setLogStream(null);
82
}
83
84
/*
85
* Validate that setLogStream succeeds if SQLPermission("setLog") has been
86
* granted
87
*/
88
@Test
89
public void test1() {
90
Policy.setPolicy(new TestPolicy("setLog"));
91
DriverManager.setLogStream(null);
92
}
93
94
/*
95
* Validate that setLogStream succeeds if AllPermissions has been granted
96
*/
97
@Test
98
public void test2() {
99
setPolicy(new TestPolicy("all"));
100
DriverManager.setLogStream(null);
101
}
102
103
/*
104
* Validate that AccessControlException is thrown if SQLPermission("setLog")
105
* has not been granted
106
*/
107
@Test(expectedExceptions = AccessControlException.class)
108
public void test4() {
109
setPolicy(new TestPolicy());
110
DriverManager.setLogWriter(null);
111
}
112
113
/*
114
* Validate that setLogWriter succeeds if SQLPermission("setLog") has been
115
* granted
116
*/
117
@Test
118
public void test5() {
119
setPolicy(new TestPolicy("setLog"));
120
DriverManager.setLogWriter(null);
121
}
122
123
/*
124
* Validate that setLogWriter succeeds if AllPermissions has been granted
125
*/
126
@Test
127
public void test6() {
128
setPolicy(new TestPolicy("all"));
129
DriverManager.setLogWriter(null);
130
}
131
132
/*
133
* Validate that AccessControlException is thrown if
134
* SQLPermission("deregisterDriver") has not been granted
135
*/
136
@Test(expectedExceptions = AccessControlException.class)
137
public void test7() throws SQLException {
138
setPolicy(new TestPolicy());
139
DriverManager.deregisterDriver(new StubDriver());
140
}
141
142
/*
143
* Validate that deregisterDriver succeeds if
144
* SQLPermission("deregisterDriver") has been granted
145
*/
146
@Test
147
public void test8() throws SQLException {
148
setPolicy(new TestPolicy("deregisterDriver"));
149
DriverManager.deregisterDriver(new StubDriver());
150
}
151
152
/*
153
* Validate that deregisterDriver succeeds if AllPermissions has been
154
* granted
155
*/
156
@Test
157
public void test9() throws SQLException {
158
setPolicy(new TestPolicy("all"));
159
DriverManager.deregisterDriver(new StubDriver());
160
}
161
}
162
163