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