Path: blob/master/test/jdk/java/sql/permissionTests/DriverManagerPermissionsTests.java
41149 views
/*1* Copyright (c) 2014, 2021, 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*/2223import org.testng.annotations.AfterClass;24import org.testng.annotations.BeforeClass;25import org.testng.annotations.Test;26import util.BaseTest;27import util.StubDriver;28import util.TestPolicy;2930import java.security.AccessControlException;31import java.security.Policy;32import java.sql.DriverManager;33import java.sql.SQLException;3435/*36* @test37* @library /java/sql/testng38* @run testng/othervm -Djava.security.manager=allow DriverManagerPermissionsTests39* @summary Tests that a JDBC Driver that is a module can be loaded40* via the service-provider loading mechanism.41*/42public class DriverManagerPermissionsTests extends BaseTest {4344private static Policy policy;45private static SecurityManager sm;4647/*48* Install a SecurityManager along with a base Policy to allow testNG to run49*/50@BeforeClass51public static void setUpClass() throws Exception {52setPolicy(new TestPolicy());53System.setSecurityManager(new SecurityManager());54}5556/*57* Install the original Policy and SecurityManager58*/59@AfterClass60public static void tearDownClass() throws Exception {61System.setSecurityManager(sm);62setPolicy(policy);63}6465/*66* Save off the original Policy and SecurityManager67*/68public DriverManagerPermissionsTests() {69policy = Policy.getPolicy();70sm = System.getSecurityManager();71}7273/*74* Validate that AccessControlException is thrown if SQLPermission("setLog")75* has not been granted76*/77@Test(expectedExceptions = AccessControlException.class)78public void test() {79setPolicy(new TestPolicy());80DriverManager.setLogStream(null);81}8283/*84* Validate that setLogStream succeeds if SQLPermission("setLog") has been85* granted86*/87@Test88public void test1() {89Policy.setPolicy(new TestPolicy("setLog"));90DriverManager.setLogStream(null);91}9293/*94* Validate that setLogStream succeeds if AllPermissions has been granted95*/96@Test97public void test2() {98setPolicy(new TestPolicy("all"));99DriverManager.setLogStream(null);100}101102/*103* Validate that AccessControlException is thrown if SQLPermission("setLog")104* has not been granted105*/106@Test(expectedExceptions = AccessControlException.class)107public void test4() {108setPolicy(new TestPolicy());109DriverManager.setLogWriter(null);110}111112/*113* Validate that setLogWriter succeeds if SQLPermission("setLog") has been114* granted115*/116@Test117public void test5() {118setPolicy(new TestPolicy("setLog"));119DriverManager.setLogWriter(null);120}121122/*123* Validate that setLogWriter succeeds if AllPermissions has been granted124*/125@Test126public void test6() {127setPolicy(new TestPolicy("all"));128DriverManager.setLogWriter(null);129}130131/*132* Validate that AccessControlException is thrown if133* SQLPermission("deregisterDriver") has not been granted134*/135@Test(expectedExceptions = AccessControlException.class)136public void test7() throws SQLException {137setPolicy(new TestPolicy());138DriverManager.deregisterDriver(new StubDriver());139}140141/*142* Validate that deregisterDriver succeeds if143* SQLPermission("deregisterDriver") has been granted144*/145@Test146public void test8() throws SQLException {147setPolicy(new TestPolicy("deregisterDriver"));148DriverManager.deregisterDriver(new StubDriver());149}150151/*152* Validate that deregisterDriver succeeds if AllPermissions has been153* granted154*/155@Test156public void test9() throws SQLException {157setPolicy(new TestPolicy("all"));158DriverManager.deregisterDriver(new StubDriver());159}160}161162163