Path: blob/master/test/jdk/javax/sql/permissionTests/SyncFactoryPermissionsTests.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*/22import java.security.AccessControlException;23import java.security.Policy;24import java.util.logging.Level;25import java.util.logging.Logger;26import javax.naming.Context;27import javax.sql.rowset.spi.SyncFactory;28import javax.sql.rowset.spi.SyncFactoryException;29import org.testng.annotations.AfterClass;30import org.testng.annotations.BeforeClass;31import org.testng.annotations.Test;32import util.BaseTest;33import util.StubContext;34import util.TestPolicy;35/*36* @test37* @library /java/sql/testng38* @library /javax/sql/testng39* @run testng/othervm -Djava.security.manager=allow SyncFactoryPermissionsTests40* @summary Tests SyncFactory permissions.41*/42public class SyncFactoryPermissionsTests extends BaseTest {4344Context ctx;45private static Policy policy;46private static SecurityManager sm;47private final Logger alogger = Logger.getLogger(this.getClass().getName());4849/*50* Install a SeeurityManager along with a base Policy to allow testNG to run51*/52@BeforeClass53public static void setUpClass() throws Exception {54setPolicy(new TestPolicy());55System.setSecurityManager(new SecurityManager());56}5758/*59* Install the original Policy and SecurityManager60*/61@AfterClass62public static void tearDownClass() throws Exception {63System.setSecurityManager(sm);64setPolicy(policy);65}6667/*68* Initialize a Context to be used in our tests.69* Save off the original Policy and SecurityManager70*/71public SyncFactoryPermissionsTests() {72policy = Policy.getPolicy();73sm = System.getSecurityManager();74ctx = new StubContext();75}7677/*78* Validate that AccessControlException is thrown if79* SQLPermission("setSyncFactory") has not been granted80*/81@Test(expectedExceptions = AccessControlException.class)82public void test() throws Exception {83setPolicy(new TestPolicy());84SyncFactory.setJNDIContext(ctx);85}8687/*88* Validate that a SyncFactoryException is thrown if the Logger is null89*/90@Test(expectedExceptions = SyncFactoryException.class)91public void test00() throws SyncFactoryException {92Logger l = SyncFactory.getLogger();93}9495/*96* Validate that setJNDIContext succeeds if SQLPermission("setSyncFactory")97* has been granted98*/99@Test100public void test01() throws Exception {101setPolicy(new TestPolicy("setSyncFactory"));102SyncFactory.setJNDIContext(ctx);103}104105/*106* Validate that setJNDIContext succeeds if AllPermissions has been granted107*/108@Test109public void test02() throws Exception {110setPolicy(new TestPolicy("all"));111SyncFactory.setJNDIContext(ctx);112}113114/*115* Validate that AccessControlException is thrown if116* SQLPermission("setSyncFactory") has not been granted117*/118@Test(expectedExceptions = AccessControlException.class)119public void test03() throws Exception {120setPolicy(new TestPolicy());121SyncFactory.setLogger(alogger);122}123124/*125* Validate that setLogger succeeds if SQLPermission("setSyncFactory")126* has been granted127*/128@Test129public void test04() throws Exception {130setPolicy(new TestPolicy("setSyncFactory"));131SyncFactory.setLogger(alogger);132}133134/*135* Validate that setLogger succeeds if AllPermissions has been granted136*/137@Test138public void test05() throws Exception {139setPolicy(new TestPolicy("all"));140SyncFactory.setLogger(alogger);141}142143/*144* Validate that AccessControlException is thrown if145* SQLPermission("setSyncFactory") has not been granted146*/147@Test(expectedExceptions = AccessControlException.class)148public void test06() throws Exception {149setPolicy(new TestPolicy());150SyncFactory.setLogger(alogger, Level.INFO);151}152153/*154* Validate that AccessControlException is thrown if155* SQLPermission("setSyncFactory") and LoggingPermission("control", null)156* have not been granted157*/158@Test(expectedExceptions = AccessControlException.class)159public void test07() throws Exception {160setPolicy(new TestPolicy("setSyncFactory"));161SyncFactory.setLogger(alogger, Level.INFO);162}163164/*165* Validate that setLogger succeeds if SQLPermission("setSyncFactory")166* has been granted167*/168@Test169public void test08() throws Exception {170setPolicy(new TestPolicy("setSyncFactoryLogger"));171SyncFactory.setLogger(alogger, Level.INFO);172}173174/*175* Validate that setLogger succeeds if AllPermissions has been granted176*/177@Test178public void test09() throws Exception {179setPolicy(new TestPolicy("all"));180SyncFactory.setLogger(alogger, Level.INFO);181}182}183184185