001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.T_Authorize
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, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.ResultSetMetaData;
029: import java.sql.SQLException;
030:
031: /** Utility functions for testing authorization. */
032: public class T_Authorize {
033:
034: public static void verifyAccessRW(int k) throws Exception {
035: verifyAccess(k, false);
036: }
037:
038: public static void verifyAccessRO(int k) throws Exception {
039: verifyAccess(k, true);
040: }
041:
042: /**
043: Verify that the database enforces the expected access mode appropriatly.
044: This function depends on DDL performed by the authorize.jsql test.
045:
046: @param k A key for adding/deleting rows in table t.
047: @param shortReadOnly true -> the connection should be ReadOnly
048: */
049: private static void verifyAccess(int k, boolean shouldBeReadOnly)
050: throws Exception {
051: String qText, sText;
052: int[] args = new int[2];
053: int[] qArgs = new int[2];
054:
055: Connection c = DriverManager
056: .getConnection("jdbc:default:connection");
057:
058: if (c.isReadOnly() != shouldBeReadOnly)
059: throw new Exception(
060: "Connection read-only mode does not match "
061: + shouldBeReadOnly);
062:
063: sText = "create table t2 (a int)";
064: verifyExecute(c, sText, 0, args, shouldBeReadOnly, 0);
065:
066: if (!shouldBeReadOnly) {
067: sText = "drop table t2";
068: verifyExecute(c, sText, 0, args, shouldBeReadOnly, 0);
069: }
070:
071: args[0] = k;
072: sText = "insert into AUTH_TEST.t1 values ?";
073: verifyExecute(c, sText, 1, args, shouldBeReadOnly, 1);
074: qText = "select a from AUTH_TEST.t1 where a = ?";
075: qArgs[0] = k;
076: verifyResult(c, qText, 1, qArgs, !shouldBeReadOnly, Integer
077: .toString(k));
078:
079: args[0] = -k;
080: args[1] = k;
081: sText = "update AUTH_TEST.t1 set a=? where a=?";
082: verifyExecute(c, sText, 2, args, shouldBeReadOnly, 1);
083: qArgs[0] = -k;
084: verifyResult(c, qText, 1, qArgs, !shouldBeReadOnly, Integer
085: .toString(-k));
086:
087: sText = "delete from AUTH_TEST.t1 where a=?";
088: verifyExecute(c, sText, 1, args, shouldBeReadOnly, 1);
089: verifyResult(c, qText, 1, qArgs, false, null);
090:
091: sText = "call sqlj.install_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v1','j1v1.jar', 'extinout/j1v1.jar'), 'APP.J1', 0)";
092: verifyExecute(c, sText, 0, args, shouldBeReadOnly, 0);
093: qText = "select filename from sys.sysfiles where filename = 'J1'";
094: verifyResult(c, qText, 0, qArgs, !shouldBeReadOnly, "J1");
095:
096: if (shouldBeReadOnly)
097: sText = "call sqlj.replace_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v2','j1v2.jar', 'extinout/j1v2.jar'), 'APP.IMMUTABLE')";
098: else
099: sText = "call sqlj.replace_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v2','j1v2.jar', 'extinout/j1v2.jar'), 'APP.J1')";
100: verifyExecute(c, sText, 0, args, shouldBeReadOnly, 0);
101: verifyResult(c, qText, 0, qArgs, !shouldBeReadOnly, "J1"); //RESOLVE: verify jar content
102:
103: if (shouldBeReadOnly)
104: sText = "call sqlj.remove_jar('APP.IMMUTABLE', 0)";
105: else
106: sText = "call sqlj.remove_jar('APP.J1', 0)";
107: verifyExecute(c, sText, 0, args, shouldBeReadOnly, 0);
108: verifyResult(c, qText, 0, qArgs, false, null);
109:
110: c.close();
111: }
112:
113: private static void verifyExecute(Connection c, String sText,
114: int paramCount, int[] args, boolean shouldBeReadOnly,
115: int expectRowCount) throws Exception {
116:
117: PreparedStatement ps = null;
118: try {
119: ps = c.prepareStatement(sText);
120: for (int ix = 0; ix < paramCount; ix++)
121: ps.setInt(ix + 1, args[ix]);
122: int rc = ps.executeUpdate();
123: if (shouldBeReadOnly)
124: throw new Exception(
125: "operation incorrectly allowed for read only connection "
126: + sText);
127: if (rc != expectRowCount) {
128: StringBuffer argSb = new StringBuffer();
129: for (int ix = 0; ix < paramCount; ix++) {
130: if (ix != 0)
131: argSb.append(",");
132: argSb.append(args[ix]);
133: }
134: throw new Exception("Incorrect row count " + rc
135: + " for " + sText + " with args " + argSb);
136:
137: }
138: }
139:
140: catch (SQLException sqle) {
141: String sqlState = sqle.getSQLState();
142: boolean authorizeError = sqlState.equals("25502")
143: || sqlState.equals("25503")
144: || sqlState.equals("25505");
145: if (!(shouldBeReadOnly && authorizeError))
146: throw new Exception("Unexpected exception for " + sText
147: + " (" + sqle + ")");
148: }
149:
150: finally {
151: if (ps != null)
152: ps.close();
153: }
154: }
155:
156: private static void verifyResult(Connection c, String qText,
157: int paramCount, int[] args, boolean expectResult,
158: String expect) throws Exception {
159: PreparedStatement ps = c.prepareStatement(qText);
160: for (int ix = 0; ix < paramCount; ix++)
161: ps.setInt(ix + 1, args[ix]);
162: ResultSet rs = ps.executeQuery();
163: boolean isRow = rs.next();
164: if (expectResult) {
165: if (!isRow)
166: throw new Exception("incorrect row count");
167: ResultSetMetaData rmd = rs.getMetaData();
168: if (rmd.getColumnCount() != 1)
169: new Exception("bad column count");
170: String colVal = rs.getString(1);
171: if (!expect.equals(colVal))
172: throw new Exception("bad return column " + colVal);
173: isRow = rs.next();
174: if (isRow)
175: throw new Exception("incorrect row count");
176: } else {
177: if (isRow)
178: throw new Exception("incorrect row count");
179: }
180: }
181: }
|