1 20 21 package org.apache.derbyTesting.functionTests.tests.lang; 22 23 import java.io.IOException ; 24 import java.sql.CallableStatement ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.sql.Statement ; 29 import java.util.Properties ; 30 31 import junit.framework.Test; 32 import junit.framework.TestSuite; 33 34 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 35 import org.apache.derbyTesting.junit.DatabasePropertyTestSetup; 36 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 37 38 public class SQLAuthorizationPropTest extends BaseJDBCTestCase { 39 40 public SQLAuthorizationPropTest(String name) { 41 super(name); 42 } 43 44 public static Test suite() { 45 TestSuite suite = new TestSuite(); 46 suite.addTestSuite(SQLAuthorizationPropTest.class); 47 48 Properties props = new Properties (); 51 props.setProperty("derby.database.sqlAuthorization", "true"); 52 Test test = new SQLAuthorizationPropTest("grantRevokeAfterSettingSQLAuthProperty"); 53 suite.addTest(new DatabasePropertyTestSetup (test, props)); 54 55 suite.addTest(new SQLAuthorizationPropTest("resetSQLAuthProperty")); 58 59 return suite; 60 } 61 62 65 protected void setUp() throws SQLException { 66 Statement stmt = createStatement(); 67 stmt.execute("create table GR_TAB (id int)"); 68 stmt.close(); 69 } 70 71 75 protected void tearDown() 76 throws Exception { 77 Statement stmt = createStatement(); 78 stmt.execute("drop table GR_TAB"); 79 stmt.close(); 80 super.tearDown(); 81 } 82 83 89 public void testGrantRevokeWithoutSQLAuthProperty() throws SQLException { 90 Statement stmt = createStatement(); 91 92 try { 93 stmt.execute("grant select on GR_TAB to some_user"); 94 fail("FAIL: Grant statement should have failed when SQL authorization is not set"); 95 } catch(SQLException sqle) { 96 assertSQLState(SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS, sqle); 97 } 98 99 try { 100 stmt.execute("revoke select on GR_TAB from some_user"); 101 fail("FAIL: Revoke statement should have failed when SQL authorization is not set"); 102 } catch(SQLException sqle) { 103 assertSQLState(SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS, sqle); 104 } 105 stmt.close(); 106 } 107 108 114 public void grantRevokeAfterSettingSQLAuthProperty() throws SQLException { 115 try{ 118 getDefaultConnection("shutdown=true"); 119 fail("FAIL: Failed to shutdown database"); 120 } catch (SQLException sqle) { 121 assertSQLState(SQLStateConstants.SHUTDOWN_DATABASE, sqle); 122 } 123 124 Statement stmt = createStatement(); 125 stmt.execute("grant select on GR_TAB to some_user"); 126 stmt.execute("revoke select on GR_TAB from some_user"); 127 stmt.close(); 128 } 129 130 138 public void resetSQLAuthProperty() throws SQLException { 139 Connection conn = getConnection(); 140 conn.setAutoCommit(false); 141 142 CallableStatement setDBP = conn.prepareCall( 143 "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 144 setDBP.setString(1, "derby.database.sqlAuthorization"); 145 testPropertyReset(setDBP, "false"); 147 testPropertyReset(setDBP, null); 148 testPropertyReset(setDBP, "some_value"); 149 testPropertyReset(setDBP, "true"); 151 } 152 153 162 public void testPropertyReset(CallableStatement cs, String value) throws SQLException { 163 164 cs.setString(2, value); 165 166 try { 167 cs.executeUpdate(); 168 if(value.compareToIgnoreCase("true") != 0) 169 fail("FAIL: Should not be possible to reset sql authorization once it has been turned on"); 170 } catch (SQLException sqle) { 171 assertSQLState(SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE, sqle); 172 } 173 174 } 175 } 176 | Popular Tags |