001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.dataSourcePermissions
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.tests.jdbcapi;
023:
024: import org.apache.derby.jdbc.EmbeddedDataSource;
025: import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;
026: import org.apache.derby.jdbc.EmbeddedXADataSource;
027:
028: import java.util.Properties;
029:
030: import java.sql.Connection;
031: import java.sql.ResultSet;
032: import java.sql.PreparedStatement;
033: import java.sql.CallableStatement;
034: import java.sql.Statement;
035: import java.sql.SQLException;
036: import java.sql.DriverManager;
037:
038: import javax.sql.DataSource;
039: import javax.sql.XADataSource;
040: import javax.sql.PooledConnection;
041: import javax.sql.XAConnection;
042: import javax.sql.ConnectionPoolDataSource;
043: import javax.transaction.xa.XAResource;
044: import javax.transaction.xa.XAException;
045: import javax.transaction.xa.Xid;
046: import javax.sql.ConnectionEventListener;
047: import javax.sql.ConnectionEvent;
048: import org.apache.derby.tools.JDBCDisplayUtil;
049: import org.apache.derby.tools.ij;
050: import org.apache.derbyTesting.functionTests.util.TestUtil;
051:
052: import java.io.*;
053: import java.util.Hashtable;
054:
055: import javax.naming.*;
056: import javax.naming.directory.*;
057:
058: public class dataSourcePermissions {
059:
060: private static final String zeus = "\u0396\u0395\u03A5\u03A3";
061: private static final String apollo = "\u0391\u09A0\u039F\u039B\u039B\u039A\u0390";
062:
063: public static void main(String[] args) throws Exception {
064:
065: ij.getPropertyArg(args);
066: new dataSourcePermissions().runTest();
067: new dataSourcePermissions().cleanUp();
068: System.out.println("Completed dataSourcePermissions");
069:
070: }
071:
072: public dataSourcePermissions() {
073: }
074:
075: protected void runTest() throws Exception {
076:
077: // Check the returned type of the JDBC Connections.
078: Connection conn = ij.startJBMS();
079:
080: CallableStatement cs = conn
081: .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
082: cs.setString(1, "derby.user.EDWARD");
083: cs.setString(2, "noodle");
084: cs.execute();
085:
086: cs.setString(1, "derby.user.FRANCES");
087: cs.setString(2, "isabella");
088: cs.execute();
089:
090: cs.setString(1, "derby.authentication.provider");
091: cs.setString(2, "BUILTIN");
092: cs.execute();
093:
094: // a greek unicode string
095: cs.setString(1, "derby.user." + zeus);
096: cs.setString(2, apollo);
097: cs.execute();
098:
099: cs.setString(1, "derby.connection.requireAuthentication");
100: cs.setString(2, "true");
101: cs.execute();
102:
103: cs.close();
104:
105: conn.close();
106: shutdown();
107:
108: start();
109:
110: System.out
111: .println("Checking authentication with DriverManager");
112: try {
113: DriverManager.getConnection(getJDBCUrl("wombat", null));
114: System.out
115: .println("FAIL - get DriverManager connection without user");
116: } catch (SQLException sqle) {
117: System.out
118: .println("EXPECTED CONNFAIL " + sqle.getMessage());
119: }
120:
121: try {
122: DriverManager.getConnection(getJDBCUrl("wombat",
123: "user=cynthia;password=sara"));
124: System.out
125: .println("FAIL - get DriverManager connection with invalid user");
126: } catch (SQLException sqle) {
127: System.out
128: .println("EXPECTED CONNFAIL " + sqle.getMessage());
129: }
130:
131: checkConnection(DriverManager.getConnection(getJDBCUrl(
132: "wombat", "user=EDWARD;password=noodle")));
133: checkConnection(DriverManager.getConnection(getJDBCUrl(
134: "wombat", "user=FRANCES;password=isabella")));
135:
136: if (supportsUnicodeNames()) {
137: checkConnection(DriverManager.getConnection(getJDBCUrl(
138: "wombat", "user=" + zeus + ";password=" + apollo)));
139: checkConnection(DriverManager.getConnection(getJDBCUrl(
140: "wombat", null), zeus, apollo));
141: }
142:
143: {
144: System.out.println("Checking connections with DataSource");
145:
146: shutdown();
147:
148: System.out.println("data source with no default user");
149: javax.sql.DataSource ds = getDS("wombat", null, null);
150:
151: try {
152: ds.getConnection().close();
153: System.out
154: .println("FAIL - get DataSource connection with no user");
155: } catch (SQLException sqle) {
156: System.out.println("EXPECTED CONNFAIL "
157: + sqle.getMessage());
158: }
159:
160: try {
161: ds.getConnection("cynthia", "sara").close();
162: System.out
163: .println("FAIL - get DataSource connection with invalid user");
164: } catch (SQLException sqle) {
165: System.out.println("EXPECTED CONNFAIL "
166: + sqle.getMessage());
167: }
168:
169: checkConnection(ds.getConnection("EDWARD", "noodle"));
170: checkConnection(ds.getConnection("FRANCES", "isabella"));
171: if (supportsUnicodeNames()) {
172: checkConnection(ds.getConnection(zeus, apollo));
173: }
174:
175: shutdown();
176:
177: System.out.println("data source with invalid default user");
178: ds = getDS("wombat", "EDWARD", "sara");
179: try {
180: ds.getConnection().close();
181: System.out
182: .println("FAIL - get DataSource connection with no user and invalid defaults");
183: } catch (SQLException sqle) {
184: System.out.println("EXPECTED CONNFAIL "
185: + sqle.getMessage());
186: }
187: checkConnection(ds.getConnection("FRANCES", "isabella"));
188: shutdown();
189:
190: System.out.println("data source with valid default user");
191: ds = getDS("wombat", "EDWARD", "noodle");
192:
193: checkConnection(ds.getConnection());
194: checkConnection(ds.getConnection("FRANCES", "isabella"));
195: shutdown();
196: }
197:
198: if (supportsPooling()) {
199:
200: System.out
201: .println("Checking connections with ConnectionPoolDataSource");
202:
203: System.out
204: .println("ConnectionPoolDataSource with no default user");
205: javax.sql.ConnectionPoolDataSource ds = getCPDS("wombat",
206: null, null);
207:
208: try {
209: ds.getPooledConnection().close();
210: System.out
211: .println("FAIL - get ConnectionPoolDataSource connection with no user");
212: } catch (SQLException sqle) {
213: System.out.println("EXPECTED CONNFAIL "
214: + sqle.getMessage());
215: }
216:
217: try {
218: ds.getPooledConnection("cynthia", "sara").close();
219: System.out
220: .println("FAIL - get ConnectionPoolDataSource connection with invalid user");
221: } catch (SQLException sqle) {
222: System.out.println("EXPECTED CONNFAIL "
223: + sqle.getMessage());
224: }
225:
226: checkConnection(ds.getPooledConnection("EDWARD", "noodle"));
227: checkConnection(ds.getPooledConnection("FRANCES",
228: "isabella"));
229: if (supportsUnicodeNames()) {
230: checkConnection(ds.getPooledConnection(zeus, apollo));
231: }
232: shutdown();
233:
234: System.out
235: .println("ConnectionPoolDataSource with invalid default user");
236: ds = getCPDS("wombat", "EDWARD", "sara");
237: try {
238: ds.getPooledConnection().close();
239: System.out
240: .println("FAIL - get ConnectionPoolDataSource connection with no user and invalid defaults");
241: } catch (SQLException sqle) {
242: System.out.println("EXPECTED CONNFAIL "
243: + sqle.getMessage());
244: }
245: checkConnection(ds.getPooledConnection("FRANCES",
246: "isabella"));
247: shutdown();
248:
249: System.out
250: .println("ConnectionPoolDataSource with valid default user");
251: ds = getCPDS("wombat", "EDWARD", "noodle");
252:
253: checkConnection(ds.getPooledConnection());
254: checkConnection(ds.getPooledConnection("FRANCES",
255: "isabella"));
256: shutdown();
257: }
258:
259: if (supportsXA()) {
260:
261: System.out
262: .println("Checking connections with XADataSource");
263:
264: System.out.println("XADataSource with no default user");
265: EmbeddedXADataSource ds = new EmbeddedXADataSource();
266: ds.setDatabaseName("wombat");
267:
268: try {
269: ds.getXAConnection().close();
270: System.out
271: .println("FAIL - get XADataSource connection with no user");
272: } catch (SQLException sqle) {
273: System.out.println("EXPECTED CONNFAIL "
274: + sqle.getMessage());
275: }
276:
277: try {
278: ds.getXAConnection("cynthia", "sara").close();
279: System.out
280: .println("FAIL - get XADataSource connection with invalid user");
281: } catch (SQLException sqle) {
282: System.out.println("EXPECTED CONNFAIL "
283: + sqle.getMessage());
284: }
285:
286: checkConnection(ds.getXAConnection("EDWARD", "noodle"));
287: checkConnection(ds.getXAConnection("FRANCES", "isabella"));
288: if (supportsUnicodeNames()) {
289: checkConnection(ds.getXAConnection(zeus, apollo));
290: }
291:
292: shutdown();
293:
294: System.out
295: .println("XADataSource with invalid default user");
296: ds = new EmbeddedXADataSource();
297: ds.setDatabaseName("wombat");
298: ds.setUser("edward");
299: ds.setPassword("sara");
300: try {
301: ds.getXAConnection().close();
302: System.out
303: .println("FAIL - get XADataSource connection with no user and invalid defaults");
304: } catch (SQLException sqle) {
305: System.out.println("EXPECTED CONNFAIL "
306: + sqle.getMessage());
307: }
308: checkConnection(ds.getXAConnection("FRANCES", "isabella"));
309: shutdown();
310:
311: System.out.println("XADataSource with valid default user");
312: ds = new EmbeddedXADataSource();
313: ds.setDatabaseName("wombat");
314: ds.setUser("EDWARD");
315: ds.setPassword("noodle");
316:
317: checkConnection(ds.getXAConnection());
318: checkConnection(ds.getXAConnection("FRANCES", "isabella"));
319:
320: shutdown();
321: }
322:
323: }
324:
325: private static void checkConnection(Connection conn)
326: throws SQLException {
327: checkConnection("DS", conn);
328: }
329:
330: private static void checkConnection(String tag, Connection conn)
331: throws SQLException {
332: ResultSet rs = conn.createStatement().executeQuery(
333: "values current_user");
334: rs.next();
335: String who = rs.getString(1);
336: rs.close();
337: conn.close();
338:
339: if (zeus.equals(who))
340: who = "GREEK ZEUS";
341:
342: System.out.println(tag + " connected as " + who);
343: }
344:
345: private static void checkConnection(javax.sql.PooledConnection pc)
346: throws SQLException {
347: checkConnection("CP", pc.getConnection());
348: pc.close();
349:
350: }
351:
352: private static void checkConnection(javax.sql.XAConnection xac)
353: throws SQLException {
354: checkConnection("XA", xac.getConnection());
355: xac.close();
356: }
357:
358: /*
359: ** Allow sub-classes to pick different implementations for (say) the network server.
360: */
361:
362: public String getJDBCUrl(String db, String attrs) {
363: String s = "jdbc:derby:" + db;
364:
365: if (attrs != null)
366: s = s + ";" + attrs;
367:
368: return s;
369:
370: }
371:
372: public javax.sql.DataSource getDS(String database, String user,
373: String password) {
374:
375: EmbeddedDataSource ds = new EmbeddedDataSource();
376: ds.setDatabaseName(database);
377: if (user != null) {
378: ds.setUser(user);
379: ds.setPassword(password);
380: }
381:
382: return ds;
383: }
384:
385: public javax.sql.ConnectionPoolDataSource getCPDS(String database,
386: String user, String password) {
387:
388: EmbeddedConnectionPoolDataSource ds = new EmbeddedConnectionPoolDataSource();
389: ds.setDatabaseName(database);
390: if (user != null) {
391: ds.setUser(user);
392: ds.setPassword(password);
393: }
394:
395: return ds;
396: }
397:
398: public boolean supportsUnicodeNames() {
399: return true;
400: }
401:
402: public boolean supportsPooling() {
403: return true;
404: }
405:
406: public boolean supportsXA() {
407: return true;
408: }
409:
410: public void start() {
411: new org.apache.derby.jdbc.EmbeddedDriver();
412: }
413:
414: public void shutdown() {
415: try {
416: DriverManager.getConnection("jdbc:derby:;shutdown=true");
417: System.out.println("FAIL - Shutdown returned connection");
418: } catch (SQLException sqle) {
419: System.out
420: .println("EXPECTED SHUTDOWN " + sqle.getMessage());
421: }
422: }
423:
424: protected void cleanUp() throws Exception {
425: // clear up in case this test gets run with useprocess=false
426: try {
427: TestUtil.loadDriver();
428: Connection conn = DriverManager.getConnection(getJDBCUrl(
429: "wombat", "user=EDWARD;password=noodle"));
430: CallableStatement cs = conn
431: .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
432: start();
433:
434: cs.setString(1, "derby.connection.requireAuthentication");
435: cs.setString(2, "false");
436: cs.execute();
437: cs.close();
438:
439: shutdown();
440: start();
441:
442: conn = DriverManager.getConnection(getJDBCUrl("wombat",
443: "user=EDWARD;password=noodle"));
444: cs = conn
445: .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
446:
447: cs.setString(1, "derby.user.EDWARD");
448: cs.setNull(2, java.sql.Types.VARCHAR);
449: cs.execute();
450:
451: cs.setString(1, "derby.user.FRANCES");
452: cs.setNull(2, java.sql.Types.VARCHAR);
453: cs.execute();
454:
455: cs.setString(1, "derby.user." + zeus);
456: cs.setNull(2, java.sql.Types.VARCHAR);
457: cs.execute();
458:
459: cs.close();
460:
461: conn.close();
462: shutdown();
463:
464: } catch (SQLException sqle) {
465: System.out.println("UNEXPECTED CONNFAIL "
466: + sqle.getMessage());
467: System.out.println("could not clean up");
468: }
469: }
470:
471: }
|