1 /* 2 * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6270015 27 * @library /test/lib 28 * @build jdk.test.lib.net.SimpleSSLContext 29 * @run main/othervm -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest 30 * @run main/othervm -Djava.net.preferIPv6Addresses=true 31 -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest 32 * @summary Light weight HTTP server 33 */ 34 35 import com.sun.net.httpserver.*; 36 import jdk.test.lib.net.SimpleSSLContext; 37 import jdk.test.lib.net.URIBuilder; 38 39 import java.util.*; 40 import java.util.concurrent.*; 41 import java.io.*; 42 import java.net.*; 43 import java.security.*; 44 import java.security.cert.*; 45 import javax.net.ssl.*; 46 47 /* basic http/s connectivity test 48 * (based on Test1) 49 */ 50 51 public class SelCacheTest extends Test { 52 53 static SSLContext ctx; 54 main(String[] args)55 public static void main(String[] args) throws Exception { 56 HttpServer s1 = null; 57 HttpsServer s2 = null; 58 ExecutorService executor=null; 59 InetAddress loopback = InetAddress.getLoopbackAddress(); 60 try { 61 String root = System.getProperty("test.src")+ "/docs"; 62 System.out.print("Test1: "); 63 InetSocketAddress addr = new InetSocketAddress(loopback, 0); 64 s1 = HttpServer.create(addr, 0); 65 if (s1 instanceof HttpsServer) { 66 throw new RuntimeException("should not be httpsserver"); 67 } 68 s2 = HttpsServer.create(addr, 0); 69 HttpHandler h = new FileServerHandler(root); 70 HttpContext c1 = s1.createContext("/test1", h); 71 HttpContext c2 = s2.createContext("/test1", h); 72 executor = Executors.newCachedThreadPool(); 73 s1.setExecutor(executor); 74 s2.setExecutor(executor); 75 ctx = new SimpleSSLContext().get(); 76 s2.setHttpsConfigurator(new HttpsConfigurator(ctx)); 77 s1.start(); 78 s2.start(); 79 80 int port = s1.getAddress().getPort(); 81 int httpsport = s2.getAddress().getPort(); 82 test(true, "http", root+"/test1", loopback, port, "smallfile.txt", 23); 83 test(true, "http", root+"/test1", loopback, port, "largefile.txt", 2730088); 84 test(true, "https", root+"/test1", loopback, httpsport, "smallfile.txt", 23); 85 test(true, "https", root+"/test1", loopback, httpsport, "largefile.txt", 2730088); 86 test(false, "http", root+"/test1", loopback, port, "smallfile.txt", 23); 87 test(false, "http", root+"/test1", loopback, port, "largefile.txt", 2730088); 88 test(false, "https", root+"/test1", loopback, httpsport, "smallfile.txt", 23); 89 test(false, "https", root+"/test1", loopback, httpsport, "largefile.txt", 2730088); 90 System.out.println("OK"); 91 } finally { 92 delay(); 93 s1.stop(2); 94 s2.stop(2); 95 executor.shutdown(); 96 } 97 } 98 test(boolean fixedLen, String protocol, String root, InetAddress address, int port, String f, int size)99 static void test(boolean fixedLen, String protocol, String root, 100 InetAddress address, int port, String f, int size) throws Exception { 101 Thread.sleep(2000); 102 URL url = URIBuilder.newBuilder() 103 .scheme(protocol) 104 .host(address) 105 .port(port) 106 .path("/test1/"+f) 107 .toURL(); 108 HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); 109 if (urlc instanceof HttpsURLConnection) { 110 HttpsURLConnection urlcs = (HttpsURLConnection) urlc; 111 urlcs.setHostnameVerifier(new HostnameVerifier() { 112 public boolean verify(String s, SSLSession s1) { 113 return true; 114 } 115 }); 116 urlcs.setSSLSocketFactory(ctx.getSocketFactory()); 117 } 118 byte [] buf = new byte [4096]; 119 120 if (fixedLen) { 121 urlc.setRequestProperty("XFixed", "yes"); 122 } 123 InputStream is = urlc.getInputStream(); 124 File temp = File.createTempFile("Test1", null); 125 temp.deleteOnExit(); 126 OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp)); 127 int c, count = 0; 128 while ((c=is.read(buf)) != -1) { 129 count += c; 130 fout.write(buf, 0, c); 131 } 132 is.close(); 133 fout.close(); 134 135 if (count != size) { 136 throw new RuntimeException("wrong amount of data returned"); 137 } 138 String orig = root + "/" + f; 139 compare(new File(orig), temp); 140 temp.delete(); 141 } 142 143 /* compare the contents of the two files */ 144 compare(File f1, File f2)145 static void compare(File f1, File f2) throws IOException { 146 InputStream i1 = new BufferedInputStream(new FileInputStream(f1)); 147 InputStream i2 = new BufferedInputStream(new FileInputStream(f2)); 148 149 int c1,c2; 150 151 try { 152 while ((c1=i1.read()) != -1) { 153 c2 = i2.read(); 154 if (c1 != c2) { 155 throw new RuntimeException("file compare failed 1"); 156 } 157 } 158 if (i2.read() != -1) { 159 throw new RuntimeException("file compare failed 2"); 160 } 161 } finally { 162 i1.close(); 163 i2.close(); 164 } 165 } 166 } 167