Path: blob/master/test/jdk/com/sun/net/httpserver/Test9a.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.SimpleSSLContext jdk.test.lib.net.URIBuilder28* @run main/othervm Test9a29* @run main/othervm -Djava.net.preferIPv6Addresses=true Test9a30* @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/* Same as Test1 but requests run in parallel.43*/4445public class Test9a extends Test {4647static SSLContext serverCtx;48static volatile SSLContext clientCtx = null;49static volatile boolean error = false;5051public static void main (String[] args) throws Exception {52HttpsServer server = null;53ExecutorService executor=null;54try {55String root = System.getProperty ("test.src")+ "/docs";56System.out.print ("Test9a: ");57InetAddress loopback = InetAddress.getLoopbackAddress();58InetSocketAddress addr = new InetSocketAddress(loopback, 0);59server = HttpsServer.create (addr, 0);60HttpHandler h = new FileServerHandler (root);61HttpContext c1 = server.createContext ("/test1", h);62executor = Executors.newCachedThreadPool();63server.setExecutor (executor);64serverCtx = new SimpleSSLContext().get();65clientCtx = new SimpleSSLContext().get();66server.setHttpsConfigurator(new HttpsConfigurator (serverCtx));67server.start();6869int port = server.getAddress().getPort();70error = false;71Thread[] t = new Thread[100];7273t[0] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);74t[1] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);75t[2] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);76t[3] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);77t[4] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);78t[5] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);79t[6] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);80t[7] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);81t[8] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);82t[9] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);83t[10] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);84t[11] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);85t[12] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);86t[13] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);87t[14] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);88t[15] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);89for (int i=0; i<16; i++) {90t[i].join();91}92if (error) {93throw new RuntimeException ("error");94}9596System.out.println ("OK");97} finally {98delay();99if (server != null)100server.stop(2);101if (executor != null)102executor.shutdown();103}104}105106static int foo = 1;107108static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {109ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);110t.start();111return t;112}113114static Object fileLock = new Object();115116static class ClientThread extends Thread {117118boolean fixedLen;119String protocol;120String root;121int port;122String f;123int size;124125ClientThread (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 = URIBuilder.newBuilder()137.scheme(protocol)138.loopback()139.port(port)140.path("/test1/" + f)141.toURL();142143HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);144if (urlc instanceof HttpsURLConnection) {145HttpsURLConnection urlcs = (HttpsURLConnection) urlc;146urlcs.setHostnameVerifier (new HostnameVerifier () {147public boolean verify (String s, SSLSession s1) {148return true;149}150});151urlcs.setSSLSocketFactory (clientCtx.getSocketFactory());152}153byte [] buf = new byte [4096];154155String s = "chunk";156if (fixedLen) {157urlc.setRequestProperty ("XFixed", "yes");158s = "fixed";159}160InputStream is = urlc.getInputStream();161File temp;162synchronized (fileLock) {163temp = File.createTempFile (s, null);164temp.deleteOnExit();165}166OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));167int c, count = 0;168while ((c=is.read(buf)) != -1) {169count += c;170fout.write (buf, 0, c);171}172is.close();173fout.close();174175if (count != size) {176System.out.println ("wrong amount of data returned");177System.out.println ("fixedLen = "+fixedLen);178System.out.println ("protocol = "+protocol);179System.out.println ("root = "+root);180System.out.println ("port = "+port);181System.out.println ("f = "+f);182System.out.println ("size = "+size);183System.out.println ("temp = "+temp);184System.out.println ("count = "+count);185error = true;186}187String orig = root + "/" + f;188compare (new File(orig), temp);189temp.delete();190} catch (Exception e) {191e.printStackTrace();192error = true;193}194}195}196197/* compare the contents of the two files */198199static void compare (File f1, File f2) throws IOException {200InputStream i1 = new BufferedInputStream (new FileInputStream(f1));201InputStream i2 = new BufferedInputStream (new FileInputStream(f2));202203int c1,c2;204try {205while ((c1=i1.read()) != -1) {206c2 = i2.read();207if (c1 != c2) {208throw new RuntimeException ("file compare failed 1");209}210}211if (i2.read() != -1) {212throw new RuntimeException ("file compare failed 2");213}214} finally {215i1.close();216i2.close();217}218}219}220221222