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  * @run main/othervm Test1
  28  * @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
  29  * @summary  Light weight HTTP server
  30  */
  31 
  32 import com.sun.net.httpserver.*;
  33 
  34 import java.util.concurrent.*;
  35 import java.io.*;
  36 import java.net.*;
  37 import javax.net.ssl.*;
  38 
  39 /* basic http/s connectivity test
  40  * Tests:
  41  *      - client/server
  42  *      - send/receive large/small file
  43  *      - chunked encoding
  44  *      - via http and https
  45  */
  46 
  47 public class Test1 extends Test {
  48 
  49     static SSLContext ctx;
  50 
  51     public static void main (String[] args) throws Exception {
  52         HttpServer s1 = null;
  53         HttpsServer s2 = null;
  54         ExecutorService executor=null;
  55         try {
  56             String root = System.getProperty ("test.src")+ "/docs";
  57             System.out.print ("Test1: ");
  58             InetSocketAddress addr = new InetSocketAddress (0);
  59             s1 = HttpServer.create (addr, 0);
  60             if (s1 instanceof HttpsServer) {
  61                 throw new RuntimeException ("should not be httpsserver");
  62             }
  63             s2 = HttpsServer.create (addr, 0);
  64             HttpHandler h = new FileServerHandler (root);
  65             HttpContext c1 = s1.createContext ("/test1", h);
  66             HttpContext c2 = s2.createContext ("/test1", h);
  67             executor = Executors.newCachedThreadPool();
  68             s1.setExecutor (executor);
  69             s2.setExecutor (executor);
  70             ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
  71             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
  72             s1.start();
  73             s2.start();
  74 
  75             int port = s1.getAddress().getPort();
  76             int httpsport = s2.getAddress().getPort();
  77             test (true, "http", root+"/test1", port, "smallfile.txt", 23);
  78             test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
  79             test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
  80             test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
  81             test (false, "http", root+"/test1", port, "smallfile.txt", 23);
  82             test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
  83             test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
  84             test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
  85             System.out.println ("OK");
  86         } finally {
  87             delay();
  88             if (s1 != null)
  89                 s1.stop(2);
  90             if (s2 != null)
  91                 s2.stop(2);
  92             if (executor != null)
  93                 executor.shutdown ();
  94         }
  95     }
  96 
  97     static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
  98         URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
  99         HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
 100         if (urlc instanceof HttpsURLConnection) {
 101             HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
 102             urlcs.setHostnameVerifier (new HostnameVerifier () {
 103                 public boolean verify (String s, SSLSession s1) {
 104                     return true;
 105                 }
 106             });
 107             urlcs.setSSLSocketFactory (ctx.getSocketFactory());
 108         }
 109         byte [] buf = new byte [4096];
 110 
 111         if (fixedLen) {
 112             urlc.setRequestProperty ("XFixed", "yes");
 113         }
 114         InputStream is = urlc.getInputStream();
 115         File temp = File.createTempFile ("Test1", null);
 116         temp.deleteOnExit();
 117         OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
 118         int c, count = 0;
 119         while ((c=is.read(buf)) != -1) {
 120             count += c;
 121             fout.write (buf, 0, c);
 122         }
 123         is.close();
 124         fout.close();
 125 
 126         if (count != size) {
 127             throw new RuntimeException ("wrong amount of data returned");
 128         }
 129         String orig = root + "/" + f;
 130         compare (new File(orig), temp);
 131         temp.delete();
 132     }
 133 
 134     /* compare the contents of the two files */
 135 
 136     static void compare (File f1, File f2) throws IOException {
 137         InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
 138         InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
 139 
 140         int c1,c2;
 141         try {
 142             while ((c1=i1.read()) != -1) {
 143                 c2 = i2.read();
 144                 if (c1 != c2) {
 145                     throw new RuntimeException ("file compare failed 1");
 146                 }
 147             }
 148             if (i2.read() != -1) {
 149                 throw new RuntimeException ("file compare failed 2");
 150             }
 151         } finally {
 152             i1.close();
 153             i2.close();
 154         }
 155     }
 156 }