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