Home | History | Annotate | Download | only in elonen
      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 import java.net.ServerSocket;
     40 
     41 import org.junit.Assert;
     42 import org.junit.Test;
     43 
     44 import fi.iki.elonen.HttpServerTest.TestServer;
     45 import fi.iki.elonen.NanoHTTPD.SecureServerSocketFactory;
     46 
     47 public class ServerSocketFactoryTest extends NanoHTTPD {
     48 
     49     public static final int PORT = 8192;
     50 
     51     public ServerSocketFactoryTest() {
     52         super(PORT);
     53 
     54         this.setServerSocketFactory(new TestFactory());
     55     }
     56 
     57     @Test
     58     public void isCustomServerSocketFactory() {
     59         System.out.println("CustomServerSocketFactory test");
     60         Assert.assertTrue(this.getServerSocketFactory() instanceof TestFactory);
     61     }
     62 
     63     @Test
     64     public void testCreateServerSocket() {
     65         System.out.println("CreateServerSocket test");
     66         ServerSocket ss = null;
     67         try {
     68             ss = this.getServerSocketFactory().create();
     69         } catch (IOException e) {
     70         }
     71         Assert.assertTrue(ss != null);
     72     }
     73 
     74     @Test
     75     public void testSSLServerSocketFail() {
     76         String[] protocols = {
     77             ""
     78         };
     79         System.setProperty("javax.net.ssl.trustStore", new File("src/test/resources/keystore.jks").getAbsolutePath());
     80         ServerSocketFactory ssFactory = new SecureServerSocketFactory(null, protocols);
     81         ServerSocket ss = null;
     82         try {
     83             ss = ssFactory.create();
     84         } catch (Exception e) {
     85         }
     86         Assert.assertTrue(ss == null);
     87 
     88     }
     89 
     90     private class TestFactory implements ServerSocketFactory {
     91 
     92         @Override
     93         public ServerSocket create() {
     94             try {
     95                 return new ServerSocket();
     96             } catch (IOException e) {
     97                 e.printStackTrace();
     98             }
     99             return null;
    100         }
    101     }
    102 }
    103