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