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 
  24 /**
  25  * @test
  26  * @modules java.sql.rowset/com.sun.rowset
  27  *          java.sql.rowset/com.sun.rowset.internal
  28  *          java.sql.rowset/com.sun.rowset.providers
  29  */
  30 
  31 package test.rowset.spi;
  32 
  33 import java.security.AccessControlException;
  34 import java.security.Policy;
  35 import java.util.logging.Level;
  36 import java.util.logging.Logger;
  37 import javax.naming.Context;
  38 import javax.sql.rowset.spi.SyncFactory;
  39 import javax.sql.rowset.spi.SyncFactoryException;
  40 import org.testng.annotations.AfterClass;
  41 import org.testng.annotations.BeforeClass;
  42 import org.testng.annotations.Test;
  43 import util.BaseTest;
  44 import util.StubContext;
  45 import util.TestPolicy;
  46 
  47 public class SyncFactoryPermissionsTests extends BaseTest {
  48 
  49     Context ctx;
  50     private static Policy policy;
  51     private static SecurityManager sm;
  52     private final Logger alogger = Logger.getLogger(this.getClass().getName());
  53 
  54     /*
  55      * Install a SeeurityManager along with a base Policy to allow testNG to run
  56      */
  57     @BeforeClass
  58     public static void setUpClass() throws Exception {
  59         setPolicy(new TestPolicy());
  60         System.setSecurityManager(new SecurityManager());
  61     }
  62 
  63     /*
  64      * Install the original Policy and SecurityManager
  65      */
  66     @AfterClass
  67     public static void tearDownClass() throws Exception {
  68         System.setSecurityManager(sm);
  69         setPolicy(policy);
  70     }
  71 
  72     /*
  73      * Initialize a Context to be used in our tests.
  74      * Save off the original Policy and SecurityManager
  75      */
  76     public SyncFactoryPermissionsTests() {
  77         policy = Policy.getPolicy();
  78         sm = System.getSecurityManager();
  79         ctx = new StubContext();
  80     }
  81 
  82     /*
  83      * Validate that AccessControlException is thrown if
  84      * SQLPermission("setSyncFactory") has not been granted
  85      */
  86     @Test(expectedExceptions = AccessControlException.class)
  87     public void test() throws Exception {
  88         setPolicy(new TestPolicy());
  89         SyncFactory.setJNDIContext(ctx);
  90     }
  91 
  92     /*
  93      * Validate that a SyncFactoryException is thrown if the Logger is null
  94      */
  95     @Test(expectedExceptions = SyncFactoryException.class)
  96     public void test00() throws SyncFactoryException {
  97         Logger l = SyncFactory.getLogger();
  98     }
  99 
 100     /*
 101      * Validate that setJNDIContext succeeds if SQLPermission("setSyncFactory")
 102      * has been granted
 103      */
 104     @Test
 105     public void test01() throws Exception {
 106         setPolicy(new TestPolicy("setSyncFactory"));
 107         SyncFactory.setJNDIContext(ctx);
 108     }
 109 
 110     /*
 111      * Validate that setJNDIContext succeeds if AllPermissions has been granted
 112      */
 113     @Test
 114     public void test02() throws Exception {
 115         setPolicy(new TestPolicy("all"));
 116         SyncFactory.setJNDIContext(ctx);
 117     }
 118 
 119     /*
 120      * Validate that AccessControlException is thrown if
 121      * SQLPermission("setSyncFactory") has not been granted
 122      */
 123     @Test(expectedExceptions = AccessControlException.class)
 124     public void test03() throws Exception {
 125         setPolicy(new TestPolicy());
 126         SyncFactory.setLogger(alogger);
 127     }
 128 
 129     /*
 130      * Validate that setLogger succeeds if SQLPermission("setSyncFactory")
 131      * has been granted
 132      */
 133     @Test
 134     public void test04() throws Exception {
 135         setPolicy(new TestPolicy("setSyncFactory"));
 136         SyncFactory.setLogger(alogger);
 137     }
 138 
 139     /*
 140      * Validate that setLogger succeeds if AllPermissions has been granted
 141      */
 142     @Test
 143     public void test05() throws Exception {
 144         setPolicy(new TestPolicy("all"));
 145         SyncFactory.setLogger(alogger);
 146     }
 147 
 148     /*
 149      * Validate that AccessControlException is thrown if
 150      * SQLPermission("setSyncFactory") has not been granted
 151      */
 152     @Test(expectedExceptions = AccessControlException.class)
 153     public void test06() throws Exception {
 154         setPolicy(new TestPolicy());
 155         SyncFactory.setLogger(alogger, Level.INFO);
 156     }
 157 
 158     /*
 159      * Validate that AccessControlException is thrown if
 160      * SQLPermission("setSyncFactory")  and LoggingPermission("control", null)
 161      * have not been granted
 162      */
 163     @Test(expectedExceptions = AccessControlException.class)
 164     public void test07() throws Exception {
 165         setPolicy(new TestPolicy("setSyncFactory"));
 166         SyncFactory.setLogger(alogger, Level.INFO);
 167     }
 168 
 169     /*
 170      * Validate that setLogger succeeds if SQLPermission("setSyncFactory")
 171      * has been granted
 172      */
 173     @Test
 174     public void test08() throws Exception {
 175         setPolicy(new TestPolicy("setSyncFactoryLogger"));
 176         SyncFactory.setLogger(alogger, Level.INFO);
 177     }
 178 
 179     /*
 180      * Validate that setLogger succeeds if AllPermissions has been granted
 181      */
 182     @Test
 183     public void test09() throws Exception {
 184         setPolicy(new TestPolicy("all"));
 185         SyncFactory.setLogger(alogger, Level.INFO);
 186     }
 187 }