import java.io.*; import java.net.*; import java.security.*; import javax.net.ssl.*; import com.sun.ejte.ccl.reporter.*; /** * Test case for covering the https blocking issue * IssueTracker#1710 * @author Jagadesh Munta */ public class WebTest { private static SimpleReporterAdapter stat = new SimpleReporterAdapter("appserver-sqe"); private static final String TEST_NAME = "security-block-ssl-issue1710"; private String host; private String port; private String httpsPort; private String contextRoot; private String trustStorePath; private String urlFile = "/index.html"; private String timeoutSecs = "60"; public WebTest(String[] args) { host = args[0]; port = args[1]; urlFile = args[2]; trustStorePath = args[3]; timeoutSecs = args[4]; } public static void main(String[] args) { stat.addDescription("Test for blockssl, #1710"); WebTest webTest = new WebTest(args); webTest.doTest(); stat.printSummary(TEST_NAME); } public void doTest() { URL url = null; int responseCode; boolean fail = false; try { System.out.println("http-listener secure port: " + port); // Open a socket if (port==null) { port = "8181"; } System.out.println("Starting opensocket thread at " + port); Thread t = new OpenSocketThread(host,Integer.parseInt(port)); t.start(); int tvalue = Integer.parseInt(timeoutSecs)*1000; url = new URL("https://" + host + ":" + port + urlFile); System.out.println("Connecting to: " + url.toString()); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, getTrustManagers(trustStorePath), null); HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection(); httpsConn.setSSLSocketFactory( new CustomSSLFactory(ctx.getSocketFactory(),tvalue)); httpsConn.setHostnameVerifier(new MyHostnameVerifier()); //httpsConn.setConnectTimeout(tvalue); // this is not working and using the custom ssl factory! System.out.println("HttpsURLconnection timeout = "+timeoutSecs+" secs."); httpsConn.connect(); responseCode = httpsConn.getResponseCode(); System.out.println("Response code: " + responseCode); if (responseCode != HttpURLConnection.HTTP_OK) { fail = true; } if (fail) { stat.addStatus(TEST_NAME, stat.FAIL); } else { stat.addStatus(TEST_NAME, stat.PASS); } } catch (Exception ex) { System.out.println(TEST_NAME + " test failed"); stat.addStatus(TEST_NAME, stat.FAIL); ex.printStackTrace(); } } private TrustManager[] getTrustManagers(String path) throws Exception { TrustManager[] tms = null; InputStream istream = null; System.out.println("Trust store path: " + path); try { KeyStore trustStore = KeyStore.getInstance("JKS"); istream = new FileInputStream(path); trustStore.load(istream,null); istream.close(); istream = null; String alg = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(alg); tmf.init(trustStore); tms = tmf.getTrustManagers(); } finally { if (istream != null) { try { istream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } return tms; } private static class MyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } } private class OpenSocketThread extends Thread { String host = "localhost"; int port = 8181; public OpenSocketThread(String host,int port ){ this.host = host; this.port = port; } public void run() { try { Socket s = new Socket(host,port); System.out.println("Is socket connected?"+s.isConnected()); System.out.println("Opened socket thread wait time = "+ (Integer.parseInt(timeoutSecs)+20)+" secs."); Thread.sleep((Integer.parseInt(timeoutSecs)+20)*1000); s.close(); //s.setSoTimeout((Integer.parseInt(timeoutSecs)+10)*1000); System.out.println("Is socket connected?"+s.isConnected()); } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (InterruptedException ex) { ex.printStackTrace(); } } } /* * Create a custom ssl factory to set the timeout. Use the original factory * for default implementation. */ private class CustomSSLFactory extends javax.net.ssl.SSLSocketFactory { SSLSocketFactory ofactory = null; int timeout = 0; public CustomSSLFactory(SSLSocketFactory ofactory, int timeout){ super(); this.ofactory = ofactory; this.timeout = timeout; } public String[] getDefaultCipherSuites() { return ofactory.getDefaultCipherSuites(); } public String[] getSupportedCipherSuites() { return ofactory.getSupportedCipherSuites(); } public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { Socket socket = ofactory.createSocket( s, host, port, autoClose ); socket.setSoTimeout(timeout); return socket; } public Socket createSocket( String host, int port ) throws IOException, UnknownHostException { Socket socket = ofactory.createSocket( host, port ); socket.setSoTimeout(timeout); return socket; } public Socket createSocket(String host, int port, InetAddress localHost, int localPort ) throws IOException, UnknownHostException { Socket socket = ofactory.createSocket( host, port, localHost, localPort ); socket.setSoTimeout( timeout ); return socket; } public Socket createSocket( InetAddress host, int port ) throws IOException { Socket socket = ofactory.createSocket(); socket.setSoTimeout(timeout ); socket.connect(new InetSocketAddress(host, port)); return socket; } public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort ) throws IOException { Socket socket = ofactory.createSocket( address, port, localAddress, localPort ); socket.setSoTimeout( timeout ); return socket; } } }