Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test13.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 Test1327* @summary Light weight HTTP server28*/2930import com.sun.net.httpserver.*;3132import java.util.concurrent.*;33import java.util.logging.*;34import java.io.*;35import java.net.*;3637import javax.net.ssl.*;3839/* basic http/s connectivity test40* Tests:41* - same as Test12, but with 64 threads42*/4344public class Test13 extends Test {4546static SSLContext ctx;4748final static int NUM = 32; // was 324950static boolean fail = false;5152public static void main (String[] args) throws Exception {53HttpServer s1 = null;54HttpsServer s2 = null;55ExecutorService executor=null;56Logger l = Logger.getLogger ("com.sun.net.httpserver");57Handler ha = new ConsoleHandler();58ha.setLevel(Level.ALL);59l.setLevel(Level.ALL);60l.addHandler(ha);61try {62String root = System.getProperty ("test.src")+ "/docs";63System.out.print ("Test13: ");64InetSocketAddress addr = new InetSocketAddress (0);65s1 = HttpServer.create (addr, 0);66s2 = HttpsServer.create (addr, 0);67HttpHandler h = new FileServerHandler (root);68HttpContext c1 = s1.createContext ("/test1", h);69HttpContext c2 = s2.createContext ("/test1", h);70executor = Executors.newCachedThreadPool();71s1.setExecutor (executor);72s2.setExecutor (executor);73ctx = new SimpleSSLContext(System.getProperty("test.src")).get();74s2.setHttpsConfigurator(new HttpsConfigurator (ctx));75s1.start();76s2.start();7778int port = s1.getAddress().getPort();79int httpsport = s2.getAddress().getPort();80Runner r[] = new Runner[NUM*2];81for (int i=0; i<NUM; i++) {82r[i] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);83r[i+NUM] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);84}85start (r);86join (r);87System.out.println ("OK");88} finally {89delay();90if (s1 != null)91s1.stop(2);92if (s2 != null)93s2.stop(2);94if (executor != null)95executor.shutdown ();96}97}9899static void start (Runner[] x) {100for (int i=0; i<x.length; i++) {101if (x[i] != null)102x[i].start();103}104}105106static void join (Runner[] x) {107for (int i=0; i<x.length; i++) {108try {109if (x[i] != null)110x[i].join();111} catch (InterruptedException e) {}112}113}114115116static class Runner extends Thread {117118boolean fixedLen;119String protocol;120String root;121int port;122String f;123int size;124125Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) {126this.fixedLen=fixedLen;127this.protocol=protocol;128this.root=root;129this.port=port;130this.f=f;131this.size = size;132}133134public void run () {135try {136URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);137HttpURLConnection urlc = (HttpURLConnection) url.openConnection();138if (urlc instanceof HttpsURLConnection) {139HttpsURLConnection urlcs = (HttpsURLConnection) urlc;140urlcs.setHostnameVerifier (new HostnameVerifier () {141public boolean verify (String s, SSLSession s1) {142return true;143}144});145urlcs.setSSLSocketFactory (ctx.getSocketFactory());146}147byte [] buf = new byte [4096];148149if (fixedLen) {150urlc.setRequestProperty ("XFixed", "yes");151}152InputStream is = urlc.getInputStream();153File temp = File.createTempFile ("Test1", null);154temp.deleteOnExit();155OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));156int c, count = 0;157while ((c=is.read(buf)) != -1) {158count += c;159fout.write (buf, 0, c);160}161is.close();162fout.close();163164if (count != size) {165throw new RuntimeException ("wrong amount of data returned");166}167String orig = root + "/" + f;168compare (new File(orig), temp);169temp.delete();170} catch (Exception e) {171e.printStackTrace();172fail = true;173}174}175}176177/* compare the contents of the two files */178179static void compare (File f1, File f2) throws IOException {180InputStream i1 = new BufferedInputStream (new FileInputStream(f1));181InputStream i2 = new BufferedInputStream (new FileInputStream(f2));182183int c1,c2;184try {185while ((c1=i1.read()) != -1) {186c2 = i2.read();187if (c1 != c2) {188throw new RuntimeException ("file compare failed 1");189}190}191if (i2.read() != -1) {192throw new RuntimeException ("file compare failed 2");193}194} finally {195i1.close();196i2.close();197}198}199}200201202