Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/SelCacheTest.java
38855 views
/*1* Copyright (c) 2006, 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 -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest27* @summary Light weight HTTP server28*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.io.*;35import java.net.*;36import java.security.*;37import java.security.cert.*;38import javax.net.ssl.*;3940/* basic http/s connectivity test41* (based on Test1)42*/4344public class SelCacheTest extends Test {4546static SSLContext ctx;4748public static void main (String[] args) throws Exception {49HttpServer s1 = null;50HttpsServer s2 = null;51ExecutorService executor=null;52try {53String root = System.getProperty ("test.src")+ "/docs";54System.out.print ("Test1: ");55InetSocketAddress addr = new InetSocketAddress (0);56s1 = HttpServer.create (addr, 0);57if (s1 instanceof HttpsServer) {58throw new RuntimeException ("should not be httpsserver");59}60s2 = HttpsServer.create (addr, 0);61HttpHandler h = new FileServerHandler (root);62HttpContext c1 = s1.createContext ("/test1", h);63HttpContext c2 = s2.createContext ("/test1", h);64executor = Executors.newCachedThreadPool();65s1.setExecutor (executor);66s2.setExecutor (executor);67ctx = new SimpleSSLContext(System.getProperty("test.src")).get();68s2.setHttpsConfigurator(new HttpsConfigurator (ctx));69s1.start();70s2.start();7172int port = s1.getAddress().getPort();73int httpsport = s2.getAddress().getPort();74test (true, "http", root+"/test1", port, "smallfile.txt", 23);75test (true, "http", root+"/test1", port, "largefile.txt", 2730088);76test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);77test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);78test (false, "http", root+"/test1", port, "smallfile.txt", 23);79test (false, "http", root+"/test1", port, "largefile.txt", 2730088);80test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);81test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);82System.out.println ("OK");83} finally {84delay();85s1.stop(2);86s2.stop(2);87executor.shutdown ();88}89}9091static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {92Thread.sleep (2000);93URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);94HttpURLConnection urlc = (HttpURLConnection) url.openConnection();95if (urlc instanceof HttpsURLConnection) {96HttpsURLConnection urlcs = (HttpsURLConnection) urlc;97urlcs.setHostnameVerifier (new HostnameVerifier () {98public boolean verify (String s, SSLSession s1) {99return true;100}101});102urlcs.setSSLSocketFactory (ctx.getSocketFactory());103}104byte [] buf = new byte [4096];105106if (fixedLen) {107urlc.setRequestProperty ("XFixed", "yes");108}109InputStream is = urlc.getInputStream();110File temp = File.createTempFile ("Test1", null);111temp.deleteOnExit();112OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));113int c, count = 0;114while ((c=is.read(buf)) != -1) {115count += c;116fout.write (buf, 0, c);117}118is.close();119fout.close();120121if (count != size) {122throw new RuntimeException ("wrong amount of data returned");123}124String orig = root + "/" + f;125compare (new File(orig), temp);126temp.delete();127}128129/* compare the contents of the two files */130131static void compare (File f1, File f2) throws IOException {132InputStream i1 = new BufferedInputStream (new FileInputStream(f1));133InputStream i2 = new BufferedInputStream (new FileInputStream(f2));134135int c1,c2;136137try {138while ((c1=i1.read()) != -1) {139c2 = i2.read();140if (c1 != c2) {141throw new RuntimeException ("file compare failed 1");142}143}144if (i2.read() != -1) {145throw new RuntimeException ("file compare failed 2");146}147} finally {148i1.close();149i2.close();150}151}152}153154155