1 21 22 package org.apache.derbyTesting.functionTests.util; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.ResultSetMetaData ; 29 import java.sql.SQLException ; 30 31 32 public class T_Authorize 33 { 34 35 public static void verifyAccessRW(int k) 36 throws Exception  37 { 38 verifyAccess(k, false); 39 } 40 public static void verifyAccessRO(int k) 41 throws Exception  42 { 43 verifyAccess(k, true); 44 } 45 46 53 private static void verifyAccess(int k, boolean shouldBeReadOnly) 54 throws Exception  55 { 56 String qText,sText; 57 int[] args = new int[2]; 58 int[] qArgs = new int[2]; 59 60 Connection c = 61 DriverManager.getConnection("jdbc:default:connection"); 62 63 if (c.isReadOnly() != shouldBeReadOnly) 64 throw new Exception ("Connection read-only mode does not match " + shouldBeReadOnly); 65 66 sText = "create table t2 (a int)"; 67 verifyExecute(c,sText,0,args,shouldBeReadOnly,0); 68 69 if (!shouldBeReadOnly) 70 { 71 sText = "drop table t2"; 72 verifyExecute(c,sText,0,args,shouldBeReadOnly,0); 73 } 74 75 args[0] = k; 76 sText = "insert into AUTH_TEST.t1 values ?"; 77 verifyExecute(c,sText,1,args,shouldBeReadOnly,1); 78 qText = "select a from AUTH_TEST.t1 where a = ?"; 79 qArgs[0] = k; 80 verifyResult(c,qText,1,qArgs,!shouldBeReadOnly,Integer.toString(k)); 81 82 args[0] = -k; 83 args[1] = k; 84 sText = "update AUTH_TEST.t1 set a=? where a=?"; 85 verifyExecute(c,sText,2,args,shouldBeReadOnly,1); 86 qArgs[0] = -k; 87 verifyResult(c,qText,1,qArgs,!shouldBeReadOnly,Integer.toString(-k)); 88 89 sText = "delete from AUTH_TEST.t1 where a=?"; 90 verifyExecute(c,sText,1,args,shouldBeReadOnly,1); 91 verifyResult(c,qText,1,qArgs,false,null); 92 93 sText = "call sqlj.install_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v1','j1v1.jar', 'extinout/j1v1.jar'), 'APP.J1', 0)"; 94 verifyExecute(c,sText,0,args,shouldBeReadOnly,0); 95 qText = "select filename from sys.sysfiles where filename = 'J1'"; 96 verifyResult(c,qText,0,qArgs,!shouldBeReadOnly,"J1"); 97 98 if (shouldBeReadOnly) 99 sText = "call sqlj.replace_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v2','j1v2.jar', 'extinout/j1v2.jar'), 'APP.IMMUTABLE')"; 100 else 101 sText = "call sqlj.replace_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v2','j1v2.jar', 'extinout/j1v2.jar'), 'APP.J1')"; 102 verifyExecute(c,sText,0,args,shouldBeReadOnly,0); 103 verifyResult(c,qText,0,qArgs,!shouldBeReadOnly,"J1"); 105 if (shouldBeReadOnly) 106 sText = "call sqlj.remove_jar('APP.IMMUTABLE', 0)"; 107 else 108 sText = "call sqlj.remove_jar('APP.J1', 0)"; 109 verifyExecute(c,sText,0,args,shouldBeReadOnly,0); 110 verifyResult(c,qText,0,qArgs,false,null); 111 112 c.close(); 113 } 114 115 private static void verifyExecute(Connection c, 116 String sText, 117 int paramCount, 118 int[] args, 119 boolean shouldBeReadOnly, 120 int expectRowCount) 121 throws Exception  122 { 123 124 PreparedStatement ps = null; 125 try { 126 ps = c.prepareStatement(sText); 127 for (int ix=0;ix<paramCount; ix++) 128 ps.setInt(ix+1,args[ix]); 129 int rc = ps.executeUpdate(); 130 if (shouldBeReadOnly) 131 throw new Exception ("operation incorrectly allowed for read only connection "+sText); 132 if (rc != expectRowCount) 133 { 134 StringBuffer argSb = new StringBuffer (); 135 for (int ix=0;ix<paramCount;ix++) 136 { 137 if (ix!=0) argSb.append(","); 138 argSb.append(args[ix]); 139 } 140 throw new Exception ("Incorrect row count "+rc+ 141 " for "+sText+ 142 " with args "+argSb); 143 144 } 145 } 146 147 catch (SQLException sqle) { 148 String sqlState = sqle.getSQLState(); 149 boolean authorizeError = sqlState.equals("25502") || 150 sqlState.equals("25503") || 151 sqlState.equals("25505"); 152 if (!(shouldBeReadOnly && authorizeError)) 153 throw new Exception ("Unexpected exception for "+sText+ 154 " ("+sqle+")"); 155 } 156 157 finally { 158 if (ps != null) 159 ps.close(); 160 } 161 } 162 163 private static void verifyResult(Connection c, 164 String qText, 165 int paramCount, 166 int[] args, 167 boolean expectResult, 168 String expect) 169 throws Exception  170 { 171 PreparedStatement ps = c.prepareStatement(qText); 172 for (int ix=0;ix<paramCount; ix++) 173 ps.setInt(ix+1,args[ix]); 174 ResultSet rs = ps.executeQuery(); 175 boolean isRow = rs.next(); 176 if (expectResult) 177 { 178 if (!isRow) throw new Exception ("incorrect row count"); 179 ResultSetMetaData rmd = rs.getMetaData(); 180 if (rmd.getColumnCount() != 1) new Exception ("bad column count"); 181 String colVal = rs.getString(1); 182 if (!expect.equals(colVal)) 183 throw new Exception ("bad return column "+colVal); 184 isRow = rs.next(); 185 if (isRow) throw new Exception ("incorrect row count"); 186 } 187 else 188 { 189 if (isRow) throw new Exception ("incorrect row count"); 190 } 191 } 192 } 193 | Popular Tags |