Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test1.java
38855 views
/*1* Copyright (c) 2005, 2011, 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 Test127* @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test128* @run main/othervm -Dsun.net.httpserver.nodelay=true Test129* @summary Light weight HTTP server30*/3132import com.sun.net.httpserver.*;3334import java.util.concurrent.*;35import java.io.*;36import java.net.*;37import javax.net.ssl.*;3839/* basic http/s connectivity test40* Tests:41* - client/server42* - send/receive large/small file43* - chunked encoding44* - via http and https45*46* The test is also run with sun.net.httpserver.nodelay simply to exercise47* this option. There is no specific pass or failure related to running with48* this option.49*/5051public class Test1 extends Test {5253static SSLContext ctx;5455public static void main (String[] args) throws Exception {56HttpServer s1 = null;57HttpsServer s2 = null;58ExecutorService executor=null;59try {60String root = System.getProperty ("test.src")+ "/docs";61System.out.print ("Test1: ");62InetSocketAddress addr = new InetSocketAddress (0);63s1 = HttpServer.create (addr, 0);64if (s1 instanceof HttpsServer) {65throw new RuntimeException ("should not be httpsserver");66}67s2 = HttpsServer.create (addr, 0);68HttpHandler h = new FileServerHandler (root);69HttpContext c1 = s1.createContext ("/test1", h);70HttpContext c2 = s2.createContext ("/test1", h);71executor = Executors.newCachedThreadPool();72s1.setExecutor (executor);73s2.setExecutor (executor);74ctx = new SimpleSSLContext(System.getProperty("test.src")).get();75s2.setHttpsConfigurator(new HttpsConfigurator (ctx));76s1.start();77s2.start();7879int port = s1.getAddress().getPort();80int httpsport = s2.getAddress().getPort();81test (true, "http", root+"/test1", port, "smallfile.txt", 23);82test (true, "http", root+"/test1", port, "largefile.txt", 2730088);83test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);84test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);85test (false, "http", root+"/test1", port, "smallfile.txt", 23);86test (false, "http", root+"/test1", port, "largefile.txt", 2730088);87test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);88test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);89System.out.println ("OK");90} finally {91delay();92if (s1 != null)93s1.stop(2);94if (s2 != null)95s2.stop(2);96if (executor != null)97executor.shutdown ();98}99}100101static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {102URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);103HttpURLConnection urlc = (HttpURLConnection) url.openConnection();104if (urlc instanceof HttpsURLConnection) {105HttpsURLConnection urlcs = (HttpsURLConnection) urlc;106urlcs.setHostnameVerifier (new HostnameVerifier () {107public boolean verify (String s, SSLSession s1) {108return true;109}110});111urlcs.setSSLSocketFactory (ctx.getSocketFactory());112}113byte [] buf = new byte [4096];114115if (fixedLen) {116urlc.setRequestProperty ("XFixed", "yes");117}118InputStream is = urlc.getInputStream();119File temp = File.createTempFile ("Test1", null);120temp.deleteOnExit();121OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));122int c, count = 0;123while ((c=is.read(buf)) != -1) {124count += c;125fout.write (buf, 0, c);126}127is.close();128fout.close();129130if (count != size) {131throw new RuntimeException ("wrong amount of data returned");132}133String orig = root + "/" + f;134compare (new File(orig), temp);135temp.delete();136}137138/* compare the contents of the two files */139140static void compare (File f1, File f2) throws IOException {141InputStream i1 = new BufferedInputStream (new FileInputStream(f1));142InputStream i2 = new BufferedInputStream (new FileInputStream(f2));143144int c1,c2;145try {146while ((c1=i1.read()) != -1) {147c2 = i2.read();148if (c1 != c2) {149throw new RuntimeException ("file compare failed 1");150}151}152if (i2.read() != -1) {153throw new RuntimeException ("file compare failed 2");154}155} finally {156i1.close();157i2.close();158}159}160}161162163