001: /*
002: *
003: * Derby - Class org.apache.derbyTesting.functionTests.lang.SQLAuthorizationPropTest
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to You under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020:
021: package org.apache.derbyTesting.functionTests.tests.lang;
022:
023: import java.io.IOException;
024: import java.sql.CallableStatement;
025: import java.sql.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import java.util.Properties;
030:
031: import junit.framework.Test;
032: import junit.framework.TestSuite;
033:
034: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
035: import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
036: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
037:
038: public class SQLAuthorizationPropTest extends BaseJDBCTestCase {
039:
040: public SQLAuthorizationPropTest(String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: TestSuite suite = new TestSuite();
046: suite.addTestSuite(SQLAuthorizationPropTest.class);
047:
048: // Use DatabasePropertyTestSetup decorator to set the property
049: // required by this test.
050: Properties props = new Properties();
051: props.setProperty("derby.database.sqlAuthorization", "true");
052: Test test = new SQLAuthorizationPropTest(
053: "grantRevokeAfterSettingSQLAuthProperty");
054: suite.addTest(new DatabasePropertyTestSetup(test, props));
055:
056: // This test has to be run after SQL authorization property has been
057: // set to true.
058: suite.addTest(new SQLAuthorizationPropTest(
059: "resetSQLAuthProperty"));
060:
061: return suite;
062: }
063:
064: /**
065: * Create a table to test grant/revoke statements
066: */
067: protected void setUp() throws SQLException {
068: Statement stmt = createStatement();
069: stmt.execute("create table GR_TAB (id int)");
070: stmt.close();
071: }
072:
073: /**
074: * Drop the table created during setup.
075: * @throws Exception
076: */
077: protected void tearDown() throws Exception {
078: Statement stmt = createStatement();
079: stmt.execute("drop table GR_TAB");
080: stmt.close();
081: super .tearDown();
082: }
083:
084: /**
085: * This method tests that grant/revoke is not available if
086: * derby.database.sqlAuthorization property is not set.
087: *
088: * @throws SQLException
089: */
090: public void testGrantRevokeWithoutSQLAuthProperty()
091: throws SQLException {
092: Statement stmt = createStatement();
093:
094: try {
095: stmt.execute("grant select on GR_TAB to some_user");
096: fail("FAIL: Grant statement should have failed when SQL authorization is not set");
097: } catch (SQLException sqle) {
098: assertSQLState(
099: SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS,
100: sqle);
101: }
102:
103: try {
104: stmt.execute("revoke select on GR_TAB from some_user");
105: fail("FAIL: Revoke statement should have failed when SQL authorization is not set");
106: } catch (SQLException sqle) {
107: assertSQLState(
108: SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS,
109: sqle);
110: }
111: stmt.close();
112: }
113:
114: /**
115: * This method tests that grant/revoke is available
116: * once derby.database.sqlAuthorization property is set to true.
117: *
118: * @throws SQLException
119: */
120: public void grantRevokeAfterSettingSQLAuthProperty()
121: throws SQLException {
122: // Shutdown the database for derby.database.sqlAuthorization property
123: // to take effect. This was set by DatabasePropertyTestSetup decorator.
124: try {
125: getDefaultConnection("shutdown=true");
126: fail("FAIL: Failed to shutdown database");
127: } catch (SQLException sqle) {
128: assertSQLState(SQLStateConstants.SHUTDOWN_DATABASE, sqle);
129: }
130:
131: Statement stmt = createStatement();
132: stmt.execute("grant select on GR_TAB to some_user");
133: stmt.execute("revoke select on GR_TAB from some_user");
134: stmt.close();
135: }
136:
137: /**
138: * This method tests that once derby.database.sqlAuthorization property
139: * has been set to true, it cannot be reset to any other value. For the
140: * test to be valid, it must follow the test method which sets
141: * derby.database.sqlAuthorization property to true.
142: *
143: * @throws SQLException
144: */
145: public void resetSQLAuthProperty() throws SQLException {
146: Connection conn = getConnection();
147: conn.setAutoCommit(false);
148:
149: CallableStatement setDBP = conn
150: .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
151: setDBP.setString(1, "derby.database.sqlAuthorization");
152: // Resetting to any value other than true should fail
153: testPropertyReset(setDBP, "false");
154: testPropertyReset(setDBP, null);
155: testPropertyReset(setDBP, "some_value");
156: // This should work
157: testPropertyReset(setDBP, "true");
158: }
159:
160: /**
161: * This method executes a callable statement to set the database property
162: * to a given value. It checks that reset to any value other than "true"
163: * fails.
164: *
165: * @param cs CallableStatement object used to set database property
166: * @param value value of database property
167: * @throws SQLException
168: */
169: public void testPropertyReset(CallableStatement cs, String value)
170: throws SQLException {
171:
172: cs.setString(2, value);
173:
174: try {
175: cs.executeUpdate();
176: if (value.compareToIgnoreCase("true") != 0)
177: fail("FAIL: Should not be possible to reset sql authorization once it has been turned on");
178: } catch (SQLException sqle) {
179: assertSQLState(
180: SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE, sqle);
181: }
182:
183: }
184: }
|