author | never |
Mon, 12 Jul 2010 22:27:18 -0700 | |
changeset 5926 | a36f90d986b6 |
parent 5506 | 202f599c92aa |
child 6115 | 7c523cf2bc8a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
* - same as Test1, but in parallel |
|
43 |
*/ |
|
44 |
||
45 |
public class Test12 extends Test { |
|
46 |
||
47 |
static SSLContext ctx; |
|
48 |
||
49 |
static boolean fail = false; |
|
50 |
||
51 |
public static void main (String[] args) throws Exception { |
|
52 |
HttpServer s1 = null; |
|
53 |
HttpsServer s2 = null; |
|
54 |
ExecutorService executor=null; |
|
55 |
try { |
|
56 |
String root = System.getProperty ("test.src")+ "/docs"; |
|
57 |
System.out.print ("Test12: "); |
|
58 |
InetSocketAddress addr = new InetSocketAddress (0); |
|
59 |
s1 = HttpServer.create (addr, 0); |
|
60 |
s2 = HttpsServer.create (addr, 0); |
|
61 |
HttpHandler h = new FileServerHandler (root); |
|
62 |
HttpContext c1 = s1.createContext ("/test1", h); |
|
63 |
HttpContext c2 = s2.createContext ("/test1", h); |
|
64 |
executor = Executors.newCachedThreadPool(); |
|
65 |
s1.setExecutor (executor); |
|
66 |
s2.setExecutor (executor); |
|
67 |
ctx = new SimpleSSLContext(System.getProperty("test.src")).get(); |
|
68 |
s2.setHttpsConfigurator(new HttpsConfigurator (ctx)); |
|
69 |
s1.start(); |
|
70 |
s2.start(); |
|
71 |
||
72 |
int port = s1.getAddress().getPort(); |
|
73 |
int httpsport = s2.getAddress().getPort(); |
|
74 |
Runner r[] = new Runner[8]; |
|
75 |
r[0] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23); |
|
76 |
r[1] = new Runner (true, "http", root+"/test1", port, "largefile.txt", 2730088); |
|
77 |
r[2] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23); |
|
78 |
r[3] = new Runner (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088); |
|
79 |
r[4] = new Runner (false, "http", root+"/test1", port, "smallfile.txt", 23); |
|
80 |
r[5] = new Runner (false, "http", root+"/test1", port, "largefile.txt", 2730088); |
|
81 |
r[6] = new Runner (false, "https", root+"/test1", httpsport, "smallfile.txt", 23); |
|
82 |
r[7] = new Runner (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088); |
|
83 |
start (r); |
|
84 |
join (r); |
|
85 |
System.out.println ("OK"); |
|
86 |
} finally { |
|
87 |
delay(); |
|
3070
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
88 |
if (s1 != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
89 |
s1.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
90 |
if (s2 != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
91 |
s2.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
92 |
if (executor != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
93 |
executor.shutdown (); |
2 | 94 |
} |
95 |
} |
|
96 |
||
97 |
static void start (Runner[] x) { |
|
98 |
for (int i=0; i<x.length; i++) { |
|
99 |
x[i].start(); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
static void join (Runner[] x) { |
|
104 |
for (int i=0; i<x.length; i++) { |
|
105 |
try { |
|
106 |
x[i].join(); |
|
107 |
} catch (InterruptedException e) {} |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
||
112 |
static class Runner extends Thread { |
|
113 |
||
114 |
boolean fixedLen; |
|
115 |
String protocol; |
|
116 |
String root; |
|
117 |
int port; |
|
118 |
String f; |
|
119 |
int size; |
|
120 |
||
121 |
Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) { |
|
122 |
this.fixedLen=fixedLen; |
|
123 |
this.protocol=protocol; |
|
124 |
this.root=root; |
|
125 |
this.port=port; |
|
126 |
this.f=f; |
|
127 |
this.size = size; |
|
128 |
} |
|
129 |
||
130 |
public void run () { |
|
131 |
try { |
|
132 |
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); |
|
133 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
|
134 |
if (urlc instanceof HttpsURLConnection) { |
|
135 |
HttpsURLConnection urlcs = (HttpsURLConnection) urlc; |
|
136 |
urlcs.setHostnameVerifier (new HostnameVerifier () { |
|
137 |
public boolean verify (String s, SSLSession s1) { |
|
138 |
return true; |
|
139 |
} |
|
140 |
}); |
|
141 |
urlcs.setSSLSocketFactory (ctx.getSocketFactory()); |
|
142 |
} |
|
143 |
byte [] buf = new byte [4096]; |
|
144 |
||
145 |
if (fixedLen) { |
|
146 |
urlc.setRequestProperty ("XFixed", "yes"); |
|
147 |
} |
|
148 |
InputStream is = urlc.getInputStream(); |
|
149 |
File temp = File.createTempFile ("Test1", null); |
|
150 |
temp.deleteOnExit(); |
|
151 |
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp)); |
|
152 |
int c, count = 0; |
|
153 |
while ((c=is.read(buf)) != -1) { |
|
154 |
count += c; |
|
155 |
fout.write (buf, 0, c); |
|
156 |
} |
|
157 |
is.close(); |
|
158 |
fout.close(); |
|
159 |
||
160 |
if (count != size) { |
|
161 |
throw new RuntimeException ("wrong amount of data returned"); |
|
162 |
} |
|
163 |
String orig = root + "/" + f; |
|
164 |
compare (new File(orig), temp); |
|
165 |
temp.delete(); |
|
166 |
} catch (Exception e) { |
|
167 |
e.printStackTrace(); |
|
168 |
fail = true; |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
/* compare the contents of the two files */ |
|
174 |
||
175 |
static void compare (File f1, File f2) throws IOException { |
|
176 |
InputStream i1 = new BufferedInputStream (new FileInputStream(f1)); |
|
177 |
InputStream i2 = new BufferedInputStream (new FileInputStream(f2)); |
|
178 |
||
179 |
int c1,c2; |
|
180 |
try { |
|
181 |
while ((c1=i1.read()) != -1) { |
|
182 |
c2 = i2.read(); |
|
183 |
if (c1 != c2) { |
|
184 |
throw new RuntimeException ("file compare failed 1"); |
|
185 |
} |
|
186 |
} |
|
187 |
if (i2.read() != -1) { |
|
188 |
throw new RuntimeException ("file compare failed 2"); |
|
189 |
} |
|
190 |
} finally { |
|
191 |
i1.close(); |
|
192 |
i2.close(); |
|
193 |
} |
|
194 |
} |
|
195 |
} |