J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6270015 |
| 27 | * @summary Light weight HTTP server |
| 28 | */ |
| 29 | |
| 30 | import com.sun.net.httpserver.*; |
| 31 | |
| 32 | import java.util.*; |
| 33 | import java.util.concurrent.*; |
| 34 | import java.io.*; |
| 35 | import java.net.*; |
| 36 | import java.security.*; |
| 37 | import java.security.cert.*; |
| 38 | import javax.net.ssl.*; |
| 39 | |
| 40 | /* basic http/s connectivity test |
| 41 | * Tests: |
| 42 | * - client/server |
| 43 | * - send/receive large/small file |
| 44 | * - chunked encoding |
| 45 | * - via http and https |
| 46 | */ |
| 47 | |
| 48 | public class Test1 extends Test { |
| 49 | |
| 50 | static SSLContext ctx; |
| 51 | |
| 52 | public static void main (String[] args) throws Exception { |
| 53 | HttpServer s1 = null; |
| 54 | HttpsServer s2 = null; |
| 55 | ExecutorService executor=null; |
| 56 | try { |
| 57 | String root = System.getProperty ("test.src")+ "/docs"; |
| 58 | System.out.print ("Test1: "); |
| 59 | InetSocketAddress addr = new InetSocketAddress (0); |
| 60 | s1 = HttpServer.create (addr, 0); |
| 61 | if (s1 instanceof HttpsServer) { |
| 62 | throw new RuntimeException ("should not be httpsserver"); |
| 63 | } |
| 64 | s2 = HttpsServer.create (addr, 0); |
| 65 | HttpHandler h = new FileServerHandler (root); |
| 66 | HttpContext c1 = s1.createContext ("/test1", h); |
| 67 | HttpContext c2 = s2.createContext ("/test1", h); |
| 68 | executor = Executors.newCachedThreadPool(); |
| 69 | s1.setExecutor (executor); |
| 70 | s2.setExecutor (executor); |
| 71 | ctx = new SimpleSSLContext(System.getProperty("test.src")).get(); |
| 72 | s2.setHttpsConfigurator(new HttpsConfigurator (ctx)); |
| 73 | s1.start(); |
| 74 | s2.start(); |
| 75 | |
| 76 | int port = s1.getAddress().getPort(); |
| 77 | int httpsport = s2.getAddress().getPort(); |
| 78 | test (true, "http", root+"/test1", port, "smallfile.txt", 23); |
| 79 | test (true, "http", root+"/test1", port, "largefile.txt", 2730088); |
| 80 | test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23); |
| 81 | test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088); |
| 82 | test (false, "http", root+"/test1", port, "smallfile.txt", 23); |
| 83 | test (false, "http", root+"/test1", port, "largefile.txt", 2730088); |
| 84 | test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23); |
| 85 | test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088); |
| 86 | System.out.println ("OK"); |
| 87 | } finally { |
| 88 | delay(); |
| 89 | s1.stop(2); |
| 90 | s2.stop(2); |
| 91 | executor.shutdown (); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception { |
| 96 | URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); |
| 97 | HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
| 98 | if (urlc instanceof HttpsURLConnection) { |
| 99 | HttpsURLConnection urlcs = (HttpsURLConnection) urlc; |
| 100 | urlcs.setHostnameVerifier (new HostnameVerifier () { |
| 101 | public boolean verify (String s, SSLSession s1) { |
| 102 | return true; |
| 103 | } |
| 104 | }); |
| 105 | urlcs.setSSLSocketFactory (ctx.getSocketFactory()); |
| 106 | } |
| 107 | byte [] buf = new byte [4096]; |
| 108 | |
| 109 | if (fixedLen) { |
| 110 | urlc.setRequestProperty ("XFixed", "yes"); |
| 111 | } |
| 112 | InputStream is = urlc.getInputStream(); |
| 113 | File temp = File.createTempFile ("Test1", null); |
| 114 | temp.deleteOnExit(); |
| 115 | OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp)); |
| 116 | int c, count = 0; |
| 117 | while ((c=is.read(buf)) != -1) { |
| 118 | count += c; |
| 119 | fout.write (buf, 0, c); |
| 120 | } |
| 121 | is.close(); |
| 122 | fout.close(); |
| 123 | |
| 124 | if (count != size) { |
| 125 | throw new RuntimeException ("wrong amount of data returned"); |
| 126 | } |
| 127 | String orig = root + "/" + f; |
| 128 | compare (new File(orig), temp); |
| 129 | temp.delete(); |
| 130 | } |
| 131 | |
| 132 | /* compare the contents of the two files */ |
| 133 | |
| 134 | static void compare (File f1, File f2) throws IOException { |
| 135 | InputStream i1 = new BufferedInputStream (new FileInputStream(f1)); |
| 136 | InputStream i2 = new BufferedInputStream (new FileInputStream(f2)); |
| 137 | |
| 138 | int c1,c2; |
| 139 | try { |
| 140 | while ((c1=i1.read()) != -1) { |
| 141 | c2 = i2.read(); |
| 142 | if (c1 != c2) { |
| 143 | throw new RuntimeException ("file compare failed 1"); |
| 144 | } |
| 145 | } |
| 146 | if (i2.read() != -1) { |
| 147 | throw new RuntimeException ("file compare failed 2"); |
| 148 | } |
| 149 | } finally { |
| 150 | i1.close(); |
| 151 | i2.close(); |
| 152 | } |
| 153 | } |
| 154 | } |