test/jdk/com/sun/net/httpserver/Test12.java
changeset 47216 71c04702a3d5
parent 26206 fb87c4051d65
child 52121 934969c63223
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 6270015
       
    27  * @library /lib/testlibrary/
       
    28  * @build jdk.testlibrary.SimpleSSLContext
       
    29  * @run main/othervm Test12
       
    30  * @summary  Light weight HTTP server
       
    31  */
       
    32 
       
    33 import com.sun.net.httpserver.*;
       
    34 
       
    35 import java.util.concurrent.*;
       
    36 import java.io.*;
       
    37 import java.net.*;
       
    38 import javax.net.ssl.*;
       
    39 import jdk.testlibrary.SimpleSSLContext;
       
    40 
       
    41 /* basic http/s connectivity test
       
    42  * Tests:
       
    43  *      - same as Test1, but in parallel
       
    44  */
       
    45 
       
    46 public class Test12 extends Test {
       
    47 
       
    48     static SSLContext ctx;
       
    49 
       
    50     static boolean fail = false;
       
    51 
       
    52     public static void main (String[] args) throws Exception {
       
    53         HttpServer s1 = null;
       
    54         HttpsServer s2 = null;
       
    55         ExecutorService executor=null;
       
    56         try {
       
    57             String root = System.getProperty ("test.src")+ "/docs";
       
    58             System.out.print ("Test12: ");
       
    59             InetSocketAddress addr = new InetSocketAddress (0);
       
    60             s1 = HttpServer.create (addr, 0);
       
    61             s2 = HttpsServer.create (addr, 0);
       
    62             HttpHandler h = new FileServerHandler (root);
       
    63             HttpContext c1 = s1.createContext ("/test1", h);
       
    64             HttpContext c2 = s2.createContext ("/test1", h);
       
    65             executor = Executors.newCachedThreadPool();
       
    66             s1.setExecutor (executor);
       
    67             s2.setExecutor (executor);
       
    68             ctx = new SimpleSSLContext().get();
       
    69             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
       
    70             s1.start();
       
    71             s2.start();
       
    72 
       
    73             int port = s1.getAddress().getPort();
       
    74             int httpsport = s2.getAddress().getPort();
       
    75             Runner r[] = new Runner[8];
       
    76             r[0] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);
       
    77             r[1] = new Runner (true, "http", root+"/test1", port, "largefile.txt", 2730088);
       
    78             r[2] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
       
    79             r[3] = new Runner (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
       
    80             r[4] = new Runner (false, "http", root+"/test1", port, "smallfile.txt", 23);
       
    81             r[5] = new Runner (false, "http", root+"/test1", port, "largefile.txt", 2730088);
       
    82             r[6] = new Runner (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
       
    83             r[7] = new Runner (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
       
    84             start (r);
       
    85             join (r);
       
    86             System.out.println ("OK");
       
    87         } finally {
       
    88             delay();
       
    89             if (s1 != null)
       
    90                 s1.stop(2);
       
    91             if (s2 != null)
       
    92                 s2.stop(2);
       
    93             if (executor != null)
       
    94                 executor.shutdown ();
       
    95         }
       
    96     }
       
    97 
       
    98     static void start (Runner[] x) {
       
    99         for (int i=0; i<x.length; i++) {
       
   100             x[i].start();
       
   101         }
       
   102     }
       
   103 
       
   104     static void join (Runner[] x) {
       
   105         for (int i=0; i<x.length; i++) {
       
   106             try {
       
   107                 x[i].join();
       
   108             } catch (InterruptedException e) {}
       
   109         }
       
   110     }
       
   111 
       
   112 
       
   113     static class Runner extends Thread {
       
   114 
       
   115         boolean fixedLen;
       
   116         String protocol;
       
   117         String root;
       
   118         int port;
       
   119         String f;
       
   120         int size;
       
   121 
       
   122         Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) {
       
   123             this.fixedLen=fixedLen;
       
   124             this.protocol=protocol;
       
   125             this.root=root;
       
   126             this.port=port;
       
   127             this.f=f;
       
   128             this.size = size;
       
   129         }
       
   130 
       
   131         public void run () {
       
   132             try {
       
   133                 URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
       
   134                 HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
       
   135                 if (urlc instanceof HttpsURLConnection) {
       
   136                     HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
       
   137                     urlcs.setHostnameVerifier (new HostnameVerifier () {
       
   138                         public boolean verify (String s, SSLSession s1) {
       
   139                             return true;
       
   140                         }
       
   141                     });
       
   142                     urlcs.setSSLSocketFactory (ctx.getSocketFactory());
       
   143                 }
       
   144                 byte [] buf = new byte [4096];
       
   145 
       
   146                 if (fixedLen) {
       
   147                     urlc.setRequestProperty ("XFixed", "yes");
       
   148                 }
       
   149                 InputStream is = urlc.getInputStream();
       
   150                 File temp = File.createTempFile ("Test1", null);
       
   151                 temp.deleteOnExit();
       
   152                 OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
       
   153                 int c, count = 0;
       
   154                 while ((c=is.read(buf)) != -1) {
       
   155                     count += c;
       
   156                     fout.write (buf, 0, c);
       
   157                 }
       
   158                 is.close();
       
   159                 fout.close();
       
   160 
       
   161                 if (count != size) {
       
   162                     throw new RuntimeException ("wrong amount of data returned");
       
   163                 }
       
   164                 String orig = root + "/" + f;
       
   165                 compare (new File(orig), temp);
       
   166                 temp.delete();
       
   167             } catch (Exception e) {
       
   168                 e.printStackTrace();
       
   169                 fail = true;
       
   170             }
       
   171         }
       
   172     }
       
   173 
       
   174     /* compare the contents of the two files */
       
   175 
       
   176     static void compare (File f1, File f2) throws IOException {
       
   177         InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
       
   178         InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
       
   179 
       
   180         int c1,c2;
       
   181         try {
       
   182             while ((c1=i1.read()) != -1) {
       
   183                 c2 = i2.read();
       
   184                 if (c1 != c2) {
       
   185                     throw new RuntimeException ("file compare failed 1");
       
   186                 }
       
   187             }
       
   188             if (i2.read() != -1) {
       
   189                 throw new RuntimeException ("file compare failed 2");
       
   190             }
       
   191         } finally {
       
   192             i1.close();
       
   193             i2.close();
       
   194         }
       
   195     }
       
   196 }