1 21 22 package org.apache.derbyTesting.functionTests.tests.derbynet; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.PreparedStatement ; 27 import java.sql.CallableStatement ; 28 import java.sql.Statement ; 29 import java.sql.SQLException ; 30 import java.sql.DriverManager ; 31 import javax.sql.DataSource ; 32 import javax.sql.ConnectionPoolDataSource ; 33 import javax.sql.PooledConnection ; 34 35 import org.apache.derby.tools.JDBCDisplayUtil; 36 import org.apache.derby.tools.ij; 37 import org.apache.derby.drda.NetworkServerControl; 38 import org.apache.derbyTesting.functionTests.util.TestUtil; 39 import java.io.*; 40 import java.net.InetAddress ; 41 import java.util.Hashtable ; 42 import java.util.Properties ; 43 44 import java.lang.reflect.*; 45 46 133 public class testSecMec extends dataSourcePermissions_net 134 { 135 private static boolean dbNotCreated = true; 138 139 private static String [] derby_drda_securityMechanism = { null, "USER_ONLY_SECURITY", "CLEAR_TEXT_PASSWORD_SECURITY", 142 "ENCRYPTED_USER_AND_PASSWORD_SECURITY", 143 "STRONG_PASSWORD_SUBSTITUTE_SECURITY", "INVALID_VALUE", "" }; 144 145 private static String [] USER_ATTRIBUTE = {"calvin",null}; 148 149 private static String [] PWD_ATTRIBUTE = {"hobbes",null}; 152 153 private static int NETWORKSERVER_PORT; 154 155 private static NetworkServerControl networkServer = null; 156 157 private static FileOutputStream serverOutput; 158 159 public static void main(String [] args) throws Exception { 160 ij.getPropertyArg(args); 162 163 String hostName = TestUtil.getHostName(); 164 if (hostName.equals("localhost")) 165 NETWORKSERVER_PORT = 20000; 166 else 167 NETWORKSERVER_PORT = 1527; 168 169 173 try { 174 TestUtil.loadDriver(); 175 } catch (Exception e) { 176 e.printStackTrace(); 177 } 178 179 String fileName = System.getProperty( "derby.system.home", "") 180 + "serverConsoleOutput.log"; 181 serverOutput = new FileOutputStream(fileName); 182 183 for ( int i = 0; i < derby_drda_securityMechanism.length; i++) 189 { 190 if (derby_drda_securityMechanism[i]!=null) 191 System.setProperty("derby.drda.securityMechanism",derby_drda_securityMechanism[i]); 192 193 System.out.println("----------------------------------------------"); 194 System.out.println("Testing with derby.drda.securityMechanism="+ 195 System.getProperty("derby.drda.securityMechanism")); 196 if (hostName.equals("localhost")) 198 { 199 try 200 { 201 networkServer = new NetworkServerControl(InetAddress.getByName(hostName),NETWORKSERVER_PORT); 202 networkServer.start(new PrintWriter(serverOutput)); 203 }catch(Exception e) 204 { 205 if ( derby_drda_securityMechanism[i].equals("INVALID_VALUE")|| 206 derby_drda_securityMechanism[i].equals("")) 207 { 208 System.out.println("EXPECTED EXCEPTION "+ e.getMessage()); 209 continue; 210 } 211 System.out.println("EXCEPTION " +e.getMessage()); 215 continue; 216 } 217 218 if (!isServerStarted(networkServer, 120)) { 222 System.out.println( 223 "FAIL: Server failed to respond to ping - ending test"); 224 225 break; 226 } 227 } 228 229 try { 231 testSecMec tester = 232 new testSecMec(); 233 tester.runTest(); 237 238 } catch (Exception e) { 239 System.out.println("FAIL - Exiting due to unexpected error: " + 244 e.getMessage()); 245 e.printStackTrace(); 246 } 247 248 if (hostName.equals("localhost")) 250 { 251 252 networkServer.shutdown(); 253 Thread.sleep(5000); 256 257 } 258 259 } 261 System.out.println("Completed testSecMec"); 262 263 serverOutput.close(); 264 } 265 266 static final short SECMEC_EUSRIDPWD = 0x09; 268 269 static final short SECMEC_USRIDONL = 0x04; 271 272 static final short SECMEC_USRENCPWD = 0x07; 274 275 static final short SECMEC_USRIDNWPWD = 0x05; 277 278 static final short SECMEC_USRIDPWD = 0x03; 280 281 static final short SECMEC_USRSSBPWD = 0x08; 283 284 private static short[] SECMEC_ATTRIBUTE = { 286 SECMEC_USRIDONL, 287 SECMEC_USRIDPWD, 288 SECMEC_EUSRIDPWD, 289 SECMEC_USRSSBPWD 290 }; 291 292 313 protected void runTest() 314 { 315 System.out.println("Checking security mechanism authentication with DriverManager"); 320 321 if ( dbNotCreated ) 326 { 327 getConnectionUsingDriverManager(getJDBCUrl("wombat;create=true","user=neelima;password=lee;securityMechanism="+SECMEC_USRIDPWD),"T4:"); 328 dbNotCreated = false; 329 } 330 else 331 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=neelima;password=lee;securityMechanism="+SECMEC_USRIDPWD),"T4:"); 332 getConnectionUsingDriverManager(getJDBCUrl("wombat",null),"T1:"); 333 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=max"),"T2:"); 334 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=neelima;password=lee"),"T3:"); 335 336 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=neelima;password=lee;securityMechanism="+SECMEC_EUSRIDPWD),"T5:"); 343 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=neelima;securityMechanism="+SECMEC_USRIDONL),"T6:"); 344 345 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=neelima;password=lee;securityMechanism="+SECMEC_USRIDONL),"T8:"); 348 getConnectionUsingDriverManager(getJDBCUrl("wombat","user=neelima;password=lee;securityMechanism="+SECMEC_USRSSBPWD),"T9:"); 350 351 getConnectionUsingDataSource(); 352 353 testDerby1080(); 355 356 testAllCombinationsOfUserPasswordSecMecInput(); 358 359 String serverSecurityMechanism = 363 System.getProperty("derby.drda.securityMechanism"); 364 365 if ((serverSecurityMechanism == null) || 366 (serverSecurityMechanism.equals( 367 "STRONG_PASSWORD_SUBSTITUTE_SECURITY"))) 368 { 369 testUSRSSBPWD_with_BUILTIN(); 370 } 371 } 372 373 376 public void getConnectionUsingDataSource() 377 { 378 testSecurityMechanism("john","sarah",new Short (SECMEC_USRIDPWD),"SECMEC_USRIDPWD:"); 381 382 if (!TestUtil.isJCCFramework()) 388 { 389 testSecurityMechanism("john","sarah",new Short (SECMEC_EUSRIDPWD),"SECMEC_EUSRIDPWD:"); 396 testSecurityMechanism("john","sarah",new Short (SECMEC_USRSSBPWD),"SECMEC_USRSSBPWD:"); 398 } 399 } 400 401 public void testSecurityMechanism(String user, String password,Short secmec,String msg) 402 { 403 Connection conn; 404 String securityMechanismProperty = "SecurityMechanism"; 405 Class [] argType = { Short.TYPE }; 406 String methodName = TestUtil.getSetterName(securityMechanismProperty); 407 Object [] args = new Short [1]; 408 args[0] = secmec; 409 410 try { 411 DataSource ds = getDS("wombat", user,password); 412 Method sh = ds.getClass().getMethod(methodName, argType); 413 sh.invoke(ds, args); 414 conn = ds.getConnection(); 415 conn.close(); 416 System.out.println(msg +" OK"); 417 } 418 catch (SQLException sqle) 419 { 420 System.out.println(msg +"EXCEPTION testSecurityMechanism() " + sqle.getMessage()); 424 dumpSQLException(sqle.getNextException()); 425 } 426 catch (Exception e) 427 { 428 System.out.println("UNEXPECTED EXCEPTION!!!" +msg); 429 e.printStackTrace(); 430 } 431 } 432 433 public void getConnectionUsingDriverManager(String dbUrl, String msg) 434 { 435 try 436 { 437 DriverManager.getConnection(dbUrl); 438 System.out.println(msg +" "+dbUrl ); 439 } 440 catch(SQLException sqle) 441 { 442 System.out.println(msg +" "+dbUrl +" - EXCEPTION "+ sqle.getMessage()); 448 dumpSQLException(sqle.getNextException()); 449 } 450 } 451 452 626 public void testAllCombinationsOfUserPasswordSecMecInput() { 627 String urlAttributes = null; 634 635 System.out.println("******testAllCombinationsOfUserPasswordsSecMecInput***"); 636 for (int k = 0; k < USER_ATTRIBUTE.length; k++) { 637 for (int j = 0; j < PWD_ATTRIBUTE.length; j++) { 638 urlAttributes = ""; 639 if (USER_ATTRIBUTE[k] != null) 640 urlAttributes += "user=" + USER_ATTRIBUTE[k] +";"; 641 if (PWD_ATTRIBUTE[j] != null) 642 urlAttributes += "password=" + PWD_ATTRIBUTE[j] +";"; 643 644 if (urlAttributes.length() >= 1) 648 urlAttributes = urlAttributes.substring(0,urlAttributes.length()-1); 649 650 getConnectionUsingDriverManager(getJDBCUrl("wombat", 653 urlAttributes), "Test:"); 654 getDataSourceConnection(USER_ATTRIBUTE[k],PWD_ATTRIBUTE[j], 655 "TEST_DS("+urlAttributes+")"); 656 657 for (int i = 0; i < SECMEC_ATTRIBUTE.length; i++) { 658 getConnectionUsingDriverManager(getJDBCUrl("wombat", 661 urlAttributes + ";securityMechanism=" 662 + SECMEC_ATTRIBUTE[i]), "#"); 663 testSecurityMechanism(USER_ATTRIBUTE[k],PWD_ATTRIBUTE[j], 665 new Short (SECMEC_ATTRIBUTE[i]),"TEST_DS ("+urlAttributes+ 666 ",securityMechanism="+SECMEC_ATTRIBUTE[i]+")"); 667 } 668 } 669 } 670 } 671 672 684 public void getDataSourceConnection(String user, String password,String msg) 685 { 686 Connection conn; 687 try { 688 DataSource ds = getDS("wombat", user, password); 690 conn = ds.getConnection(); 691 conn.close(); 692 System.out.println(msg + " OK"); 693 } 694 catch (SQLException sqle) 695 { 696 System.out.println(msg + "EXCEPTION getDataSourceConnection() " + sqle.getMessage()); 707 dumpSQLException(sqle.getNextException()); 708 } 709 catch (Exception e) 710 { 711 System.out.println("UNEXPECTED EXCEPTION!!!" + msg); 712 e.printStackTrace(); 713 } 714 } 715 716 721 public static void dumpSQLException(SQLException sqle) 722 { 723 while ( sqle != null) 724 { 725 System.out.println("SQLSTATE("+sqle.getSQLState()+"): " + sqle.getMessage()); 726 sqle = sqle.getNextException(); 727 } 728 } 729 730 741 public void testSecMecWithConnPooling(String user, String password, 742 Short secmec) throws Exception 743 { 744 System.out.println("withConnectionPooling"); 745 Connection conn; 746 String securityMechanismProperty = "SecurityMechanism"; 747 Class [] argType = { Short.TYPE }; 748 String methodName = TestUtil.getSetterName(securityMechanismProperty); 749 Object [] args = new Short [1]; 750 args[0] = secmec; 751 752 ConnectionPoolDataSource cpds = getCPDS("wombat", user,password); 753 754 Method sh = cpds.getClass().getMethod(methodName, argType); 756 sh.invoke(cpds, args); 757 758 PooledConnection pc = cpds.getPooledConnection(); 762 conn = pc.getConnection(); 763 conn.close(); 764 conn = pc.getConnection(); 765 test(conn); 766 conn.close(); 767 System.out.println("OK"); 768 } 769 770 775 public void test(Connection conn) 776 throws Exception 777 { 778 779 Statement stmt = null; 780 ResultSet rs = null; 781 try 782 { 783 stmt = conn.createStatement(); 785 rs = stmt.executeQuery("select count(*) from sys.systables"); 786 while(rs.next()) 787 System.out.println(" query ok "); 788 789 } 790 catch(SQLException sqle) 791 { 792 System.out.println("SQLException when querying on the database connection; "+ sqle); 793 throw sqle; 794 } 795 finally 796 { 797 if(rs != null) 798 rs.close(); 799 if(stmt != null) 800 stmt.close(); 801 } 802 } 803 804 846 public void testDerby1080() 847 { 848 try 849 { 850 System.out.println("Test DERBY-1080"); 851 testSecMecWithConnPooling("peter","neelima",new Short (SECMEC_EUSRIDPWD)); 854 } 855 catch (SQLException sqle) 856 { 857 System.out.println("DERBY-1080 EXCEPTION () " + sqle.getMessage()); 864 dumpSQLException(sqle.getNextException()); 865 } 866 catch (Exception e) 867 { 868 System.out.println("UNEXPECTED EXCEPTION!!!" ); 869 e.printStackTrace(); 870 } 871 872 } 873 874 883 public void testUSRSSBPWD_with_BUILTIN() 884 { 885 System.out.println( 888 "Test USRSSBPWD_with_BUILTIN - derby.drda.securityMechanism=" + 889 System.getProperty("derby.drda.securityMechanism")); 890 891 try 892 { 893 System.out.println("Turning ON Derby BUILTIN authentication"); 894 Connection conn = 895 getConnectionWithSecMec("neelima", "lee", 896 new Short (SECMEC_USRSSBPWD)); 897 if (conn == null) 898 return; 900 CallableStatement cs = 902 conn.prepareCall( 903 "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 904 905 cs.setString(1, "derby.user.neelima"); 906 cs.setString(2, "lee"); 907 cs.execute(); 908 909 cs.setString(1, "derby.connection.requireAuthentication"); 910 cs.setString(2, "true"); 911 cs.execute(); 912 913 cs.close(); 914 cs = null; 915 916 conn.close(); 917 918 getConnectionUsingDriverManager(getJDBCUrl( 923 "wombat","user=neelima;password=lee;shutdown=true;securityMechanism=" + 924 SECMEC_USRSSBPWD),"USRSSBPWD (T0):"); 925 926 getConnectionUsingDriverManager(getJDBCUrl( 929 "wombat","user=neelima;password=lee;securityMechanism=" + 930 SECMEC_USRSSBPWD),"USRSSBPWD + BUILTIN (T1):"); 931 testSecurityMechanism("neelima","lee",new Short (SECMEC_USRSSBPWD), 932 "TEST_DS - USRSSBPWD + BUILTIN (T2):"); 933 getConnectionUsingDriverManager(getJDBCUrl( 935 "wombat","user=invalid;password=user;securityMechanism=" + 936 SECMEC_USRSSBPWD),"USRSSBPWD + BUILTIN (T3):"); 937 testSecurityMechanism("invalid","user",new Short (SECMEC_USRSSBPWD), 938 "TEST_DS - USRSSBPWD + BUILTIN (T4):"); 939 940 System.out.println("Turning OFF Derby BUILTIN authentication"); 941 conn = getConnectionWithSecMec("neelima", "lee", 942 new Short (SECMEC_USRSSBPWD)); 943 944 if (conn == null) 945 return; 947 cs = conn.prepareCall( 949 "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 950 951 cs.setString(1, "derby.connection.requireAuthentication"); 952 cs.setString(2, "false"); 953 cs.execute(); 954 955 cs.close(); 956 cs = null; 957 conn.close(); 958 959 getConnectionUsingDriverManager(getJDBCUrl( 962 "wombat","user=neelima;password=lee;shutdown=true;securityMechanism=" + 963 SECMEC_USRSSBPWD),"USRSSBPWD + BUILTIN (T5):"); 964 } 965 catch (Exception e) 966 { 967 System.out.println( 968 "FAIL: testUSRSSBPWD_with_BUILTIN(). Unexpected Exception " + 969 e.getMessage()); 970 e.printStackTrace(); 971 } 972 } 973 974 public Connection getConnectionWithSecMec(String user, 975 String password, 976 Short secMec) 977 { 978 Connection conn = null; 979 String securityMechanismProperty = "SecurityMechanism"; 980 Class [] argType = { Short.TYPE }; 981 String methodName = TestUtil.getSetterName(securityMechanismProperty); 982 Object [] args = new Short [1]; 983 args[0] = secMec; 984 985 try { 986 DataSource ds = getDS("wombat", user, password); 987 Method sh = ds.getClass().getMethod(methodName, argType); 988 sh.invoke(ds, args); 989 conn = ds.getConnection(); 990 } 991 catch (SQLException sqle) 992 { 993 System.out.println("EXCEPTION getConnectionWithSecMec() " + sqle.getMessage()); 997 dumpSQLException(sqle.getNextException()); 998 } 999 catch (Exception e) 1000 { 1001 System.out.println( 1002 "UNEXPECTED EXCEPTION!!! getConnectionWithSecMec() - " + 1003 secMec); 1004 e.printStackTrace(); 1005 } 1006 1007 return conn; 1008 } 1009} 1010 | Popular Tags |