Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test12.java
38855 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 627001526* @run main/othervm Test1227* @summary Light weight HTTP server28*/2930import com.sun.net.httpserver.*;3132import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import javax.net.ssl.*;3637/* basic http/s connectivity test38* Tests:39* - same as Test1, but in parallel40*/4142public class Test12 extends Test {4344static SSLContext ctx;4546static boolean fail = false;4748public static void main (String[] args) throws Exception {49HttpServer s1 = null;50HttpsServer s2 = null;51ExecutorService executor=null;52try {53String root = System.getProperty ("test.src")+ "/docs";54System.out.print ("Test12: ");55InetSocketAddress addr = new InetSocketAddress (0);56s1 = HttpServer.create (addr, 0);57s2 = HttpsServer.create (addr, 0);58HttpHandler h = new FileServerHandler (root);59HttpContext c1 = s1.createContext ("/test1", h);60HttpContext c2 = s2.createContext ("/test1", h);61executor = Executors.newCachedThreadPool();62s1.setExecutor (executor);63s2.setExecutor (executor);64ctx = new SimpleSSLContext(System.getProperty("test.src")).get();65s2.setHttpsConfigurator(new HttpsConfigurator (ctx));66s1.start();67s2.start();6869int port = s1.getAddress().getPort();70int httpsport = s2.getAddress().getPort();71Runner r[] = new Runner[8];72r[0] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);73r[1] = new Runner (true, "http", root+"/test1", port, "largefile.txt", 2730088);74r[2] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);75r[3] = new Runner (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);76r[4] = new Runner (false, "http", root+"/test1", port, "smallfile.txt", 23);77r[5] = new Runner (false, "http", root+"/test1", port, "largefile.txt", 2730088);78r[6] = new Runner (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);79r[7] = new Runner (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);80start (r);81join (r);82System.out.println ("OK");83} finally {84delay();85if (s1 != null)86s1.stop(2);87if (s2 != null)88s2.stop(2);89if (executor != null)90executor.shutdown ();91}92}9394static void start (Runner[] x) {95for (int i=0; i<x.length; i++) {96x[i].start();97}98}99100static void join (Runner[] x) {101for (int i=0; i<x.length; i++) {102try {103x[i].join();104} catch (InterruptedException e) {}105}106}107108109static class Runner extends Thread {110111boolean fixedLen;112String protocol;113String root;114int port;115String f;116int size;117118Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) {119this.fixedLen=fixedLen;120this.protocol=protocol;121this.root=root;122this.port=port;123this.f=f;124this.size = size;125}126127public void run () {128try {129URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);130HttpURLConnection urlc = (HttpURLConnection) url.openConnection();131if (urlc instanceof HttpsURLConnection) {132HttpsURLConnection urlcs = (HttpsURLConnection) urlc;133urlcs.setHostnameVerifier (new HostnameVerifier () {134public boolean verify (String s, SSLSession s1) {135return true;136}137});138urlcs.setSSLSocketFactory (ctx.getSocketFactory());139}140byte [] buf = new byte [4096];141142if (fixedLen) {143urlc.setRequestProperty ("XFixed", "yes");144}145InputStream is = urlc.getInputStream();146File temp = File.createTempFile ("Test1", null);147temp.deleteOnExit();148OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));149int c, count = 0;150while ((c=is.read(buf)) != -1) {151count += c;152fout.write (buf, 0, c);153}154is.close();155fout.close();156157if (count != size) {158throw new RuntimeException ("wrong amount of data returned");159}160String orig = root + "/" + f;161compare (new File(orig), temp);162temp.delete();163} catch (Exception e) {164e.printStackTrace();165fail = true;166}167}168}169170/* compare the contents of the two files */171172static void compare (File f1, File f2) throws IOException {173InputStream i1 = new BufferedInputStream (new FileInputStream(f1));174InputStream i2 = new BufferedInputStream (new FileInputStream(f2));175176int c1,c2;177try {178while ((c1=i1.read()) != -1) {179c2 = i2.read();180if (c1 != c2) {181throw new RuntimeException ("file compare failed 1");182}183}184if (i2.read() != -1) {185throw new RuntimeException ("file compare failed 2");186}187} finally {188i1.close();189i2.close();190}191}192}193194195