KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > checkSecMgr


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.checkSecMgr
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.derbynet;
23
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.DriverManager JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil;
32 import org.apache.derby.impl.tools.ij.util;
33 import org.apache.derbyTesting.functionTests.util.TestUtil;
34
35 /**
36     This tests to see if the security manager is running.
37 */

38
39 public class checkSecMgr
40 {
41
42     public static void main (String JavaDoc args[])
43     {
44         try
45         {
46             Connection JavaDoc conn = null;
47             util.getPropertyArg(args);
48             conn = util.startJBMS();
49             // bug 6021
50
// testIllegalDBCreate();
51
testIllegalPropertySet(conn);
52             cleanUp(conn);
53         }
54         catch (Exception JavaDoc e)
55         {
56             e.printStackTrace();
57         }
58     }
59
60     public static void testIllegalDBCreate() throws Exception JavaDoc
61     {
62             System.out.println("Security Manager Test Starts");
63             // Initialize JavaCommonClient Driver.
64
Class.forName("com.ibm.db2.jcc.DB2Driver");
65             Connection JavaDoc conn = null;
66
67             // This tries to create a database that is not allowed.
68
// To repro bug 6021 change to some disallowed file system.
69
// There are two problems with this test.
70
// 1) if set to a different file system than the test runs,
71
// (e.g. D:/wombat), a null pointer is thrown.
72
// 2) If just set to a disallowed directory on the same file system.
73
// We seem to be able to create the database.
74
// Ideally this test should attempt to create the database
75
// ../wombat;create=true and get the security exception.
76
String JavaDoc hostName = TestUtil.getHostName();
77             String JavaDoc databaseURL;
78             if (hostName.equals("localhost"))
79             {
80                 databaseURL = TestUtil.getJdbcUrlPrefix() + hostName +
81                 "/\"D:/wombat;create=true\"";
82             }
83             else
84             {
85                 databaseURL = TestUtil.getJdbcUrlPrefix() + hostName + "wombat";
86             }
87             //System.out.println(databaseURL);
88
java.util.Properties JavaDoc properties = new java.util.Properties JavaDoc();
89             properties.put ("user", "cs");
90             properties.put ("password", "cs");
91
92             try {
93                 conn = DriverManager.getConnection(databaseURL, properties);
94                 System.out.println("FAILED: Expected Security Exception");
95             }
96             catch (SQLException JavaDoc se) {
97                 System.out.println("Expected Security Exception");
98                 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se);
99             }
100     }
101
102     
103     /** Try to set a property in a stored procedure for which there is not
104      * adequate permissions in the policy file
105      */

106     public static void testIllegalPropertySet(Connection JavaDoc conn)
107     {
108         System.out.println("testIllegalPropertySet");
109         try {
110             String JavaDoc createproc = "CREATE PROCEDURE setIllegalPropertyProc() DYNAMIC RESULT SETS 0 LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.tests.derbynet.checkSecMgr.setIllegalPropertyProc' PARAMETER STYLE JAVA";
111             PreparedStatement JavaDoc pstmt = conn.prepareStatement(createproc);
112             pstmt.executeUpdate();
113             CallableStatement JavaDoc cstmt = conn.prepareCall("{call setIllegalPropertyProc()}");
114             System.out.println("execute the procedure setting illegal property");
115             cstmt.executeUpdate();
116             System.out.println("FAILED: Should have gotten security Exception");
117         } catch (SQLException JavaDoc se)
118         {
119             System.out.println("Expected Security Exception");
120             JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se);
121         }
122     }
123
124     public static void setIllegalPropertyProc()
125     {
126         System.setProperty("notAllowed", "somevalue");
127     }
128
129     public static void cleanUp(Connection JavaDoc conn) throws SQLException JavaDoc
130     {
131         Statement JavaDoc stmt = conn.createStatement();
132         try {
133             stmt.executeUpdate("drop procedure setIllegalPropertyProc");
134         } catch (SQLException JavaDoc se) {
135             JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se);
136         }
137     }
138
139 }
140
Popular Tags