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.rowset.spi;
  24 
  25 import java.security.AccessControlException;
  26 import java.security.Policy;
  27 import java.util.logging.Level;
  28 import java.util.logging.Logger;
  29 import javax.naming.Context;
  30 import javax.naming.InitialContext;
  31 import javax.naming.NamingException;
  32 import javax.sql.rowset.spi.SyncFactory;
  33 import org.testng.annotations.AfterClass;
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.Test;
  36 import util.BaseTest;
  37 import util.TestPolicy;
  38 
  39 public class SyncFactoryPermissionsTests extends BaseTest {
  40 
  41     Context ctx;
  42     private static Policy policy;
  43     private static SecurityManager sm;
  44 
  45     /*
  46      * Install a SeeurityManager along with a base Policy to allow testNG to run
  47      */
  48     @BeforeClass
  49     public static void setUpClass() throws Exception {
  50         setPolicy(new TestPolicy());
  51         System.setSecurityManager(new SecurityManager());
  52     }
  53 
  54     /*
  55      * Install the original Policy and SecurityManager
  56      */
  57     @AfterClass
  58     public static void tearDownClass() throws Exception {
  59         System.setSecurityManager(sm);
  60         setPolicy(policy);
  61     }
  62 
  63     /*
  64      * Initialize a Context to be used in our tests.
  65      * Save off the original Policy and SecurityManager
  66      */
  67     public SyncFactoryPermissionsTests() {
  68         policy = Policy.getPolicy();
  69         sm = System.getSecurityManager();
  70 
  71         try {
  72             ctx = new InitialContext();
  73         } catch (NamingException ex) {
  74             Logger.getLogger(SyncFactoryPermissionsTests.class.getName()).
  75                     log(Level.SEVERE, null, ex);
  76         }
  77     }
  78 
  79     /*
  80      * Validate that AccessControlException is thrown if
  81      * SQLPermission("setSyncFactory") has not been granted
  82      */
  83     @Test(expectedExceptions = AccessControlException.class)
  84     public void test() throws Exception {
  85         setPolicy(new TestPolicy());
  86         SyncFactory.setJNDIContext(ctx);
  87     }
  88 
  89     /*
  90      * Validate that setJNDIContext succeeds if SQLPermission("setSyncFactory")
  91      * has been granted
  92      */
  93     @Test
  94     public void test1() throws Exception {
  95         Policy.setPolicy(new TestPolicy("setSyncFactory"));
  96         SyncFactory.setJNDIContext(ctx);
  97     }
  98 
  99     /*
 100      * Validate that setJNDIContext succeeds if AllPermissions has been granted
 101      */
 102     @Test
 103     public void test2() throws Exception {
 104         setPolicy(new TestPolicy("all"));
 105         SyncFactory.setJNDIContext(ctx);
 106     }
 107 }