1 22 package org.jboss.test.security.test; 23 24 import javax.net.ssl.KeyManagerFactory; 25 import javax.net.ssl.SSLContext; 26 import javax.net.ssl.TrustManager; 27 import javax.net.ssl.TrustManagerFactory; 28 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.net.InetAddress ; 33 import java.net.ServerSocket ; 34 import java.net.Socket ; 35 import java.net.URL ; 36 import java.security.KeyStore ; 37 import java.security.Security ; 38 import java.text.SimpleDateFormat ; 39 import java.util.Date ; 40 import java.util.TimeZone ; 41 import javax.management.ObjectName ; 42 import javax.net.ServerSocketFactory; 43 import javax.net.ssl.SSLServerSocket; 44 import javax.net.ssl.SSLServerSocketFactory; 45 46 import junit.extensions.TestSetup; 47 import junit.framework.Test; 48 import junit.framework.TestSuite; 49 50 import org.jboss.logging.Logger; 51 import org.jboss.test.JBossTestCase; 52 import org.jboss.test.JBossTestSetup; 53 import org.jboss.test.util.SecurityProviderUtil; 54 55 62 public class HttpsUnitTestCase extends JBossTestCase 63 { 64 static final String JAR = "https-service.sar"; 65 static final String KEYSTORE_PASSWORD = "unit-tests"; 66 67 public HttpsUnitTestCase(String name) 68 { 69 super(name); 70 } 71 72 74 public void testJSSE() throws Exception 75 { 76 log.debug("+++ testJSSE"); 77 ServerSocketFactory factory = 78 SSLServerSocketFactory.getDefault(); 79 SSLServerSocket sslSocket = (SSLServerSocket) 80 factory.createServerSocket(0); 81 int port = sslSocket.getLocalPort(); 82 83 String [] cipherSuites = sslSocket.getEnabledCipherSuites(); 84 for(int i = 0; i < cipherSuites.length; i++) 85 { 86 getLog().debug("Cipher Suite " + i + 87 " = " + cipherSuites[i]); 88 } 89 sslSocket.close(); 90 } 91 92 94 public void testHttpsURL() throws Exception 95 { 96 log.debug("+++ testHttpsURL"); 97 String httpsURL = initServer(); 99 log.debug("Setup SSL socket, URL="+httpsURL); 100 ObjectName name = new ObjectName ("jboss.security.tests:service=HttpsClient"); 102 String method = "readURL"; 103 Object [] args = {httpsURL}; 104 String [] sig = {"java.lang.String"}; 105 String reply = (String ) invoke(name, method, args, sig); 106 log.debug("Reply for url="+httpsURL+" is: "+reply); 107 } 108 109 private String initServer() throws Exception 110 { 111 String httpsURL = null; 112 SSLContext sslCtx = null; 113 try 114 { 115 sslCtx = SSLContext.getInstance("TLS"); 116 ClassLoader loader = getClass().getClassLoader(); 117 URL keyStoreURL = loader.getResource("tst.keystore"); 118 if( keyStoreURL == null ) 119 throw new IOException ("Failed to find resource tst.keystore"); 120 log.debug("Opening KeyStore: "+keyStoreURL); 121 KeyStore keyStore = KeyStore.getInstance("JKS"); 122 InputStream is = keyStoreURL.openStream(); 123 keyStore.load(is, KEYSTORE_PASSWORD.toCharArray()); 124 String algorithm = KeyManagerFactory.getDefaultAlgorithm(); 125 KeyManagerFactory keyMgr = KeyManagerFactory.getInstance(algorithm); 126 keyMgr.init(keyStore, KEYSTORE_PASSWORD.toCharArray()); 127 algorithm = TrustManagerFactory.getDefaultAlgorithm(); 128 TrustManagerFactory trustMgr = TrustManagerFactory.getInstance(algorithm); 129 trustMgr.init(keyStore); 130 TrustManager[] trustMgrs = trustMgr.getTrustManagers(); 131 sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null); 132 } 133 catch(Exception e) 134 { 135 log.error("Failed to init SSLContext", e); 136 throw new IOException ("Failed to get SSLContext for TLS algorithm"); 137 } 138 139 ServerSocketFactory factory = sslCtx.getServerSocketFactory(); 140 ServerSocket serverSocket = factory.createServerSocket(0); 141 getLog().debug("Created serverSocket: "+serverSocket); 142 int port = serverSocket.getLocalPort(); 143 InetAddress addr = serverSocket.getInetAddress(); 144 httpsURL = "https://localhost:" + port + '/'; 145 AcceptThread thread = new AcceptThread(serverSocket, getLog(), httpsURL); 146 synchronized( httpsURL ) 147 { 148 log.debug("Starting server socket thread"); 149 thread.start(); 150 log.debug("Waiting for accept thread notify"); 151 httpsURL.wait(); 152 } 153 return httpsURL; 154 } 155 156 159 public static Test suite() throws Exception 160 { 161 TestSuite suite = new TestSuite(); 162 suite.addTest(new TestSuite(HttpsUnitTestCase.class)); 163 164 TestSetup wrapper = new JBossTestSetup(suite) 166 { 167 protected void setUp() throws Exception 168 { 169 super.setUp(); 170 deploy(JAR); 171 Security.addProvider(SecurityProviderUtil.getJSSEProvider()); 172 } 173 protected void tearDown() throws Exception 174 { 175 undeploy(JAR); 176 super.tearDown(); 177 } 178 }; 179 return wrapper; 180 } 181 182 185 static class AcceptThread extends Thread 186 { 187 ServerSocket serverSocket; 188 Logger log; 189 Object lock; 190 AcceptThread(ServerSocket serverSocket, Logger log, Object lock) 191 { 192 super("AcceptThread"); 193 super.setDaemon(true); 194 this.serverSocket = serverSocket; 195 this.log = log; 196 this.lock = lock; 197 } 198 199 public void run() 200 { 201 SimpleDateFormat fmt = new SimpleDateFormat ("E, dd MMM yyyy HH:mm:ss z"); 202 fmt.setTimeZone(TimeZone.getTimeZone("GMT")); 203 Date now = new Date (); 204 String dateString = fmt.format(now); 205 String content = "<html><head><title>HttpsUnitTestCase</title></head>" 206 + "<body>"+dateString+"</body></html>\r\n"; 207 String reply = "HTTP/1.1 200 OK\r\n" 208 + "Date: "+dateString+"\r\n" 209 + "Server: HttpsUnitTestCase/JSSE SSL\r\n" 210 + "Last-Modified: "+dateString+"\r\n" 211 + "Content-Length: "+content.length()+"\r\n" 212 + "Connection: close\r\n" 213 + "Content-Type: text/html\r\n\r\n" 214 + content; 215 216 while( true ) 217 { 218 try 219 { 220 log.debug("Waiting for client connection"); 221 synchronized( lock ) 222 { 223 lock.notify(); 224 } 225 Socket client = serverSocket.accept(); 226 log.debug("Accepted client: "+client); 227 InputStream is = client.getInputStream(); 228 OutputStream os = client.getOutputStream(); 229 byte[] buffer = new byte[4096]; 230 int bytes = is.read(buffer); 231 log.debug("Read: "+bytes); 232 os.write(reply.getBytes()); 233 os.flush(); 234 log.debug("Wrote: "+reply.length()); 235 log.debug("ReplyData: "+reply); 236 os.close(); 237 is.close(); 238 client.close(); 239 log.debug("Closed client"); 240 } 241 catch(Exception e) 242 { 243 log.error("Failed to process request", e); 244 break; 245 } 246 } 247 248 try 249 { 250 serverSocket.close(); 251 } 252 catch(Exception e) 253 { 254 log.error("Failed to close server socket", e); 255 } 256 } 257 } 258 } 259 | Popular Tags |