Path: blob/master/test/jdk/com/sun/net/httpserver/Test1.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 Test129* @run main/othervm -Djava.net.preferIPv6Addresses=true Test130* @run main/othervm -Djdk.net.usePlainSocketImpl Test131* @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test132* @run main/othervm -Dsun.net.httpserver.nodelay=true Test133* @summary Light weight HTTP server34*/3536import com.sun.net.httpserver.*;3738import java.util.concurrent.*;39import java.io.*;40import java.net.*;41import javax.net.ssl.*;42import jdk.test.lib.net.SimpleSSLContext;43import jdk.test.lib.net.URIBuilder;4445/* basic http/s connectivity test46* Tests:47* - client/server48* - send/receive large/small file49* - chunked encoding50* - via http and https51*52* The test is also run with sun.net.httpserver.nodelay simply to exercise53* this option. There is no specific pass or failure related to running with54* this option.55*/5657public class Test1 extends Test {5859static SSLContext ctx;6061public static void main (String[] args) throws Exception {62HttpServer s1 = null;63HttpsServer s2 = null;64ExecutorService executor=null;65try {66String root = System.getProperty ("test.src")+ "/docs";67System.out.print ("Test1: ");68InetAddress loopback = InetAddress.getLoopbackAddress();69InetSocketAddress addr = new InetSocketAddress (loopback, 0);70s1 = HttpServer.create (addr, 0);71if (s1 instanceof HttpsServer) {72throw new RuntimeException ("should not be httpsserver");73}74s2 = HttpsServer.create (addr, 0);75HttpHandler h = new FileServerHandler (root);76HttpContext c1 = s1.createContext ("/test1", h);77HttpContext c2 = s2.createContext ("/test1", h);78executor = Executors.newCachedThreadPool();79s1.setExecutor (executor);80s2.setExecutor (executor);81ctx = new SimpleSSLContext().get();82s2.setHttpsConfigurator(new HttpsConfigurator (ctx));83s1.start();84s2.start();8586int port = s1.getAddress().getPort();87int httpsport = s2.getAddress().getPort();88test (true, "http", root+"/test1", port, "smallfile.txt", 23);89test (true, "http", root+"/test1", port, "largefile.txt", 2730088);90test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);91test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);92test (false, "http", root+"/test1", port, "smallfile.txt", 23);93test (false, "http", root+"/test1", port, "largefile.txt", 2730088);94test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);95test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);96System.out.println ("OK");97} finally {98delay();99if (s1 != null)100s1.stop(2);101if (s2 != null)102s2.stop(2);103if (executor != null)104executor.shutdown ();105}106}107108static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {109URL url = URIBuilder.newBuilder()110.scheme(protocol)111.loopback()112.port(port)113.path("/test1/"+f)114.toURL();115HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);116if (urlc instanceof HttpsURLConnection) {117HttpsURLConnection urlcs = (HttpsURLConnection) urlc;118urlcs.setHostnameVerifier (new HostnameVerifier () {119public boolean verify (String s, SSLSession s1) {120return true;121}122});123urlcs.setSSLSocketFactory (ctx.getSocketFactory());124}125byte [] buf = new byte [4096];126127if (fixedLen) {128urlc.setRequestProperty ("XFixed", "yes");129}130InputStream is = urlc.getInputStream();131File temp = File.createTempFile ("Test1", null);132temp.deleteOnExit();133OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));134int c, count = 0;135while ((c=is.read(buf)) != -1) {136count += c;137fout.write (buf, 0, c);138}139is.close();140fout.close();141142if (count != size) {143throw new RuntimeException ("wrong amount of data returned");144}145String orig = root + "/" + f;146compare (new File(orig), temp);147temp.delete();148}149150/* compare the contents of the two files */151152static void compare (File f1, File f2) throws IOException {153InputStream i1 = new BufferedInputStream (new FileInputStream(f1));154InputStream i2 = new BufferedInputStream (new FileInputStream(f2));155156int c1,c2;157try {158while ((c1=i1.read()) != -1) {159c2 = i2.read();160if (c1 != c2) {161throw new RuntimeException ("file compare failed 1");162}163}164if (i2.read() != -1) {165throw new RuntimeException ("file compare failed 2");166}167} finally {168i1.close();169i2.close();170}171}172}173174175