Path: blob/master/test/jdk/com/sun/net/httpserver/Test12.java
41152 views
/*1* Copyright (c) 2005, 2019, 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* @library /test/lib27* @build jdk.test.lib.net.SimpleSSLContext28* @run main/othervm Test1229* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1230* @summary Light weight HTTP server31*/3233import com.sun.net.httpserver.*;3435import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import javax.net.ssl.*;39import jdk.test.lib.net.SimpleSSLContext;40import jdk.test.lib.net.URIBuilder;4142/* basic http/s connectivity test43* Tests:44* - same as Test1, but in parallel45*/4647public class Test12 extends Test {4849static SSLContext ctx;5051static boolean fail = false;5253public static void main (String[] args) throws Exception {54HttpServer s1 = null;55HttpsServer s2 = null;56ExecutorService executor=null;57try {58String root = System.getProperty ("test.src")+ "/docs";59System.out.print ("Test12: ");60InetAddress loopback = InetAddress.getLoopbackAddress();61InetSocketAddress addr = new InetSocketAddress(loopback, 0);62s1 = HttpServer.create (addr, 0);63s2 = HttpsServer.create (addr, 0);64HttpHandler h = new FileServerHandler (root);65HttpContext c1 = s1.createContext ("/test1", h);66HttpContext c2 = s2.createContext ("/test1", h);67executor = Executors.newCachedThreadPool();68s1.setExecutor (executor);69s2.setExecutor (executor);70ctx = new SimpleSSLContext().get();71s2.setHttpsConfigurator(new HttpsConfigurator (ctx));72s1.start();73s2.start();7475int port = s1.getAddress().getPort();76int httpsport = s2.getAddress().getPort();77Runner r[] = new Runner[8];78r[0] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);79r[1] = new Runner (true, "http", root+"/test1", port, "largefile.txt", 2730088);80r[2] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);81r[3] = new Runner (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);82r[4] = new Runner (false, "http", root+"/test1", port, "smallfile.txt", 23);83r[5] = new Runner (false, "http", root+"/test1", port, "largefile.txt", 2730088);84r[6] = new Runner (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);85r[7] = new Runner (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);86start (r);87join (r);88System.out.println ("OK");89} finally {90delay();91if (s1 != null)92s1.stop(2);93if (s2 != null)94s2.stop(2);95if (executor != null)96executor.shutdown ();97}98}99100static void start (Runner[] x) {101for (int i=0; i<x.length; i++) {102x[i].start();103}104}105106static void join (Runner[] x) {107for (int i=0; i<x.length; i++) {108try {109x[i].join();110} catch (InterruptedException e) {}111}112}113114115static class Runner extends Thread {116117boolean fixedLen;118String protocol;119String root;120int port;121String f;122int size;123124Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) {125this.fixedLen=fixedLen;126this.protocol=protocol;127this.root=root;128this.port=port;129this.f=f;130this.size = size;131}132133public void run () {134try {135URL url = URIBuilder.newBuilder()136.scheme(protocol)137.loopback()138.port(port)139.path("/test1/"+f)140.toURL();141HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);142if (urlc instanceof HttpsURLConnection) {143HttpsURLConnection urlcs = (HttpsURLConnection) urlc;144urlcs.setHostnameVerifier (new HostnameVerifier () {145public boolean verify (String s, SSLSession s1) {146return true;147}148});149urlcs.setSSLSocketFactory (ctx.getSocketFactory());150}151byte [] buf = new byte [4096];152153if (fixedLen) {154urlc.setRequestProperty ("XFixed", "yes");155}156InputStream is = urlc.getInputStream();157File temp = File.createTempFile ("Test1", null);158temp.deleteOnExit();159OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));160int c, count = 0;161while ((c=is.read(buf)) != -1) {162count += c;163fout.write (buf, 0, c);164}165is.close();166fout.close();167168if (count != size) {169throw new RuntimeException ("wrong amount of data returned");170}171String orig = root + "/" + f;172compare (new File(orig), temp);173temp.delete();174} catch (Exception e) {175e.printStackTrace();176fail = true;177}178}179}180181/* compare the contents of the two files */182183static void compare (File f1, File f2) throws IOException {184InputStream i1 = new BufferedInputStream (new FileInputStream(f1));185InputStream i2 = new BufferedInputStream (new FileInputStream(f2));186187int c1,c2;188try {189while ((c1=i1.read()) != -1) {190c2 = i2.read();191if (c1 != c2) {192throw new RuntimeException ("file compare failed 1");193}194}195if (i2.read() != -1) {196throw new RuntimeException ("file compare failed 2");197}198} finally {199i1.close();200i2.close();201}202}203}204205206