1 /* 2 * Copyright (c) 2005, 2006, 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 Test12 28 * @summary Light weight HTTP server 29 */ 30 31 import com.sun.net.httpserver.*; 32 33 import java.util.concurrent.*; 34 import java.io.*; 35 import java.net.*; 36 import javax.net.ssl.*; 37 38 /* basic http/s connectivity test 39 * Tests: 40 * - same as Test1, but in parallel 41 */ 42 43 public class Test12 extends Test { 44 45 static SSLContext ctx; 46 47 static boolean fail = false; 48 49 public static void main (String[] args) throws Exception { 50 HttpServer s1 = null; 51 HttpsServer s2 = null; 52 ExecutorService executor=null; 53 try { 54 String root = System.getProperty ("test.src")+ "/docs"; 55 System.out.print ("Test12: "); 56 InetSocketAddress addr = new InetSocketAddress (0); 57 s1 = HttpServer.create (addr, 0); 58 s2 = HttpsServer.create (addr, 0); 59 HttpHandler h = new FileServerHandler (root); 60 HttpContext c1 = s1.createContext ("/test1", h); 61 HttpContext c2 = s2.createContext ("/test1", h); 62 executor = Executors.newCachedThreadPool(); 63 s1.setExecutor (executor); 64 s2.setExecutor (executor); 65 ctx = new SimpleSSLContext(System.getProperty("test.src")).get(); 66 s2.setHttpsConfigurator(new HttpsConfigurator (ctx)); 67 s1.start(); 68 s2.start(); 69 70 int port = s1.getAddress().getPort(); 71 int httpsport = s2.getAddress().getPort(); 72 Runner r[] = new Runner[8]; 73 r[0] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23); 74 r[1] = new Runner (true, "http", root+"/test1", port, "largefile.txt", 2730088); 75 r[2] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23); 76 r[3] = new Runner (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088); 77 r[4] = new Runner (false, "http", root+"/test1", port, "smallfile.txt", 23); 78 r[5] = new Runner (false, "http", root+"/test1", port, "largefile.txt", 2730088); 79 r[6] = new Runner (false, "https", root+"/test1", httpsport, "smallfile.txt", 23); 80 r[7] = new Runner (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088); 81 start (r); 82 join (r); 83 System.out.println ("OK"); 84 } finally { 85 delay(); 86 if (s1 != null) 87 s1.stop(2); 88 if (s2 != null) 89 s2.stop(2); 90 if (executor != null) 91 executor.shutdown (); 92 } 93 } 94 95 static void start (Runner[] x) { 96 for (int i=0; i<x.length; i++) { 97 x[i].start(); 98 } 99 } 100 101 static void join (Runner[] x) { 102 for (int i=0; i<x.length; i++) { 103 try { 104 x[i].join(); 105 } catch (InterruptedException e) {} 106 } 107 } 108 109 110 static class Runner extends Thread { 111 112 boolean fixedLen; 113 String protocol; 114 String root; 115 int port; 116 String f; 117 int size; 118 119 Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) { 120 this.fixedLen=fixedLen; 121 this.protocol=protocol; 122 this.root=root; 123 this.port=port; 124 this.f=f; 125 this.size = size; 126 } 127 128 public void run () { 129 try { 130 URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); 131 HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); 132 if (urlc instanceof HttpsURLConnection) { 133 HttpsURLConnection urlcs = (HttpsURLConnection) urlc; 134 urlcs.setHostnameVerifier (new HostnameVerifier () { 135 public boolean verify (String s, SSLSession s1) { 136 return true; 137 } 138 }); 139 urlcs.setSSLSocketFactory (ctx.getSocketFactory()); 140 } 141 byte [] buf = new byte [4096]; 142 143 if (fixedLen) { 144 urlc.setRequestProperty ("XFixed", "yes"); 145 } 146 InputStream is = urlc.getInputStream(); 147 File temp = File.createTempFile ("Test1", null); 148 temp.deleteOnExit(); 149 OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp)); 150 int c, count = 0; 151 while ((c=is.read(buf)) != -1) { 152 count += c; 153 fout.write (buf, 0, c); 154 } 155 is.close(); 156 fout.close(); 157 158 if (count != size) { 159 throw new RuntimeException ("wrong amount of data returned"); 160 } 161 String orig = root + "/" + f; 162 compare (new File(orig), temp); 163 temp.delete(); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 fail = true; 167 } 168 } 169 } 170 171 /* compare the contents of the two files */ 172 173 static void compare (File f1, File f2) throws IOException { 174 InputStream i1 = new BufferedInputStream (new FileInputStream(f1)); 175 InputStream i2 = new BufferedInputStream (new FileInputStream(f2)); 176 177 int c1,c2; 178 try { 179 while ((c1=i1.read()) != -1) { 180 c2 = i2.read(); 181 if (c1 != c2) { 182 throw new RuntimeException ("file compare failed 1"); 183 } 184 } 185 if (i2.read() != -1) { 186 throw new RuntimeException ("file compare failed 2"); 187 } 188 } finally { 189 i1.close(); 190 i2.close(); 191 } 192 } 193 }