1 package fi.iki.elonen; 2 3 import java.io.File; 4 5 /* 6 * #%L 7 * NanoHttpd-Core 8 * %% 9 * Copyright (C) 2012 - 2015 nanohttpd 10 * %% 11 * Redistribution and use in source and binary forms, with or without modification, 12 * are permitted provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this 15 * list of conditions and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, 18 * this list of conditions and the following disclaimer in the documentation 19 * and/or other materials provided with the distribution. 20 * 21 * 3. Neither the name of the nanohttpd nor the names of its contributors 22 * may be used to endorse or promote products derived from this software without 23 * specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 33 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 34 * OF THE POSSIBILITY OF SUCH DAMAGE. 35 * #L% 36 */ 37 38 import java.io.IOException; 39 40 import org.apache.http.HttpEntity; 41 import org.apache.http.HttpResponse; 42 import org.apache.http.client.ClientProtocolException; 43 import org.apache.http.client.methods.HttpTrace; 44 import org.apache.http.impl.client.DefaultHttpClient; 45 import org.junit.After; 46 import org.junit.Assert; 47 import org.junit.Before; 48 import org.junit.Test; 49 50 import fi.iki.elonen.NanoHTTPD.SecureServerSocketFactory; 51 52 public class SSLServerSocketFactoryTest extends HttpServerTest { 53 54 @Test 55 public void testSSLConnection() throws ClientProtocolException, IOException { 56 DefaultHttpClient httpclient = new DefaultHttpClient(); 57 HttpTrace httphead = new HttpTrace("https://localhost:9043/index.html"); 58 HttpResponse response = httpclient.execute(httphead); 59 HttpEntity entity = response.getEntity(); 60 Assert.assertEquals(200, response.getStatusLine().getStatusCode()); 61 62 Assert.assertEquals(9043, this.testServer.getListeningPort()); 63 Assert.assertTrue(this.testServer.isAlive()); 64 } 65 66 @Before 67 public void setUp() throws Exception { 68 System.setProperty("javax.net.ssl.trustStore", new File("src/test/resources/keystore.jks").getAbsolutePath()); 69 this.testServer = new TestServer(9043); 70 this.testServer.setServerSocketFactory(new SecureServerSocketFactory(NanoHTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray()), null)); 71 this.tempFileManager = new TestTempFileManager(); 72 this.testServer.start(); 73 try { 74 long start = System.currentTimeMillis(); 75 Thread.sleep(100L); 76 while (!this.testServer.wasStarted()) { 77 Thread.sleep(100L); 78 if (System.currentTimeMillis() - start > 2000) { 79 Assert.fail("could not start server"); 80 } 81 } 82 } catch (InterruptedException e) { 83 } 84 } 85 86 @After 87 public void tearDown() { 88 this.testServer.stop(); 89 } 90 } 91