Path: blob/master/test/jdk/com/sun/net/httpserver/SelCacheTest.java
41152 views
/*1* Copyright (c) 2006, 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 -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest29* @run main/othervm -Djava.net.preferIPv6Addresses=true30-Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest31* @summary Light weight HTTP server32*/3334import com.sun.net.httpserver.*;35import jdk.test.lib.net.SimpleSSLContext;36import jdk.test.lib.net.URIBuilder;3738import java.util.*;39import java.util.concurrent.*;40import java.io.*;41import java.net.*;42import java.security.*;43import java.security.cert.*;44import javax.net.ssl.*;4546/* basic http/s connectivity test47* (based on Test1)48*/4950public class SelCacheTest extends Test {5152static SSLContext ctx;5354public static void main(String[] args) throws Exception {55HttpServer s1 = null;56HttpsServer s2 = null;57ExecutorService executor=null;58InetAddress loopback = InetAddress.getLoopbackAddress();59try {60String root = System.getProperty("test.src")+ "/docs";61System.out.print("Test1: ");62InetSocketAddress addr = new InetSocketAddress(loopback, 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().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", loopback, port, "smallfile.txt", 23);82test(true, "http", root+"/test1", loopback, port, "largefile.txt", 2730088);83test(true, "https", root+"/test1", loopback, httpsport, "smallfile.txt", 23);84test(true, "https", root+"/test1", loopback, httpsport, "largefile.txt", 2730088);85test(false, "http", root+"/test1", loopback, port, "smallfile.txt", 23);86test(false, "http", root+"/test1", loopback, port, "largefile.txt", 2730088);87test(false, "https", root+"/test1", loopback, httpsport, "smallfile.txt", 23);88test(false, "https", root+"/test1", loopback, httpsport, "largefile.txt", 2730088);89System.out.println("OK");90} finally {91delay();92s1.stop(2);93s2.stop(2);94executor.shutdown();95}96}9798static void test(boolean fixedLen, String protocol, String root,99InetAddress address, int port, String f, int size) throws Exception {100Thread.sleep(2000);101URL url = URIBuilder.newBuilder()102.scheme(protocol)103.host(address)104.port(port)105.path("/test1/"+f)106.toURL();107HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);108if (urlc instanceof HttpsURLConnection) {109HttpsURLConnection urlcs = (HttpsURLConnection) urlc;110urlcs.setHostnameVerifier(new HostnameVerifier() {111public boolean verify(String s, SSLSession s1) {112return true;113}114});115urlcs.setSSLSocketFactory(ctx.getSocketFactory());116}117byte [] buf = new byte [4096];118119if (fixedLen) {120urlc.setRequestProperty("XFixed", "yes");121}122InputStream is = urlc.getInputStream();123File temp = File.createTempFile("Test1", null);124temp.deleteOnExit();125OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp));126int c, count = 0;127while ((c=is.read(buf)) != -1) {128count += c;129fout.write(buf, 0, c);130}131is.close();132fout.close();133134if (count != size) {135throw new RuntimeException("wrong amount of data returned");136}137String orig = root + "/" + f;138compare(new File(orig), temp);139temp.delete();140}141142/* compare the contents of the two files */143144static void compare(File f1, File f2) throws IOException {145InputStream i1 = new BufferedInputStream(new FileInputStream(f1));146InputStream i2 = new BufferedInputStream(new FileInputStream(f2));147148int c1,c2;149150try {151while ((c1=i1.read()) != -1) {152c2 = i2.read();153if (c1 != c2) {154throw new RuntimeException("file compare failed 1");155}156}157if (i2.read() != -1) {158throw new RuntimeException("file compare failed 2");159}160} finally {161i1.close();162i2.close();163}164}165}166167168