author | schien |
Thu, 25 Aug 2011 17:18:25 -0700 | |
changeset 10304 | df130f34ab4c |
parent 7668 | d4a77089c587 |
child 21834 | 2ed7771ef949 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2005, 2010, 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 |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
27 |
* @run main/othervm Test9a |
2 | 28 |
* @summary Light weight HTTP server |
29 |
*/ |
|
30 |
||
31 |
import com.sun.net.httpserver.*; |
|
32 |
||
33 |
import java.util.concurrent.*; |
|
34 |
import java.io.*; |
|
35 |
import java.net.*; |
|
36 |
import javax.net.ssl.*; |
|
37 |
||
38 |
/* Same as Test1 but requests run in parallel. |
|
39 |
*/ |
|
40 |
||
41 |
public class Test9a extends Test { |
|
42 |
||
43 |
static SSLContext serverCtx, clientCtx; |
|
44 |
static boolean error = false; |
|
45 |
||
46 |
public static void main (String[] args) throws Exception { |
|
47 |
HttpsServer server = null; |
|
48 |
ExecutorService executor=null; |
|
49 |
try { |
|
50 |
String root = System.getProperty ("test.src")+ "/docs"; |
|
51 |
System.out.print ("Test9a: "); |
|
52 |
InetSocketAddress addr = new InetSocketAddress (0); |
|
53 |
server = HttpsServer.create (addr, 0); |
|
54 |
HttpHandler h = new FileServerHandler (root); |
|
55 |
HttpContext c1 = server.createContext ("/test1", h); |
|
56 |
executor = Executors.newCachedThreadPool(); |
|
57 |
server.setExecutor (executor); |
|
58 |
serverCtx = new SimpleSSLContext(System.getProperty("test.src")).get(); |
|
59 |
clientCtx = new SimpleSSLContext(System.getProperty("test.src")).get(); |
|
60 |
server.setHttpsConfigurator(new HttpsConfigurator (serverCtx)); |
|
61 |
server.start(); |
|
62 |
||
63 |
int port = server.getAddress().getPort(); |
|
64 |
error = false; |
|
65 |
Thread[] t = new Thread[100]; |
|
66 |
||
67 |
t[0] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
68 |
t[1] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
69 |
t[2] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
70 |
t[3] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
71 |
t[4] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
72 |
t[5] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
73 |
t[6] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
74 |
t[7] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
75 |
t[8] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
76 |
t[9] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
77 |
t[10] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
78 |
t[11] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
79 |
t[12] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
80 |
t[13] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
81 |
t[14] = test (true, "https", root+"/test1", port, "smallfile.txt", 23); |
|
82 |
t[15] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088); |
|
83 |
for (int i=0; i<16; i++) { |
|
84 |
t[i].join(); |
|
85 |
} |
|
86 |
if (error) { |
|
87 |
throw new RuntimeException ("error"); |
|
88 |
} |
|
89 |
||
90 |
System.out.println ("OK"); |
|
91 |
} finally { |
|
92 |
delay(); |
|
3070
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
93 |
if (server != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
94 |
server.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
95 |
if (executor != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
96 |
executor.shutdown(); |
2 | 97 |
} |
98 |
} |
|
99 |
||
100 |
static int foo = 1; |
|
101 |
||
102 |
static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception { |
|
103 |
ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size); |
|
104 |
t.start(); |
|
105 |
return t; |
|
106 |
} |
|
107 |
||
108 |
static Object fileLock = new Object(); |
|
109 |
||
110 |
static class ClientThread extends Thread { |
|
111 |
||
112 |
boolean fixedLen; |
|
113 |
String protocol; |
|
114 |
String root; |
|
115 |
int port; |
|
116 |
String f; |
|
117 |
int size; |
|
118 |
||
119 |
ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) { |
|
120 |
this.fixedLen = fixedLen; |
|
121 |
this.protocol = protocol; |
|
122 |
this.root = root; |
|
123 |
this.port = port; |
|
124 |
this.f = f; |
|
125 |
this.size = size; |
|
126 |
} |
|
127 |
||
128 |
public void run () { |
|
129 |
try { |
|
130 |
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); |
|
131 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
|
132 |
if (urlc instanceof HttpsURLConnection) { |
|
133 |
HttpsURLConnection urlcs = (HttpsURLConnection) urlc; |
|
134 |
urlcs.setHostnameVerifier (new HostnameVerifier () { |
|
135 |
public boolean verify (String s, SSLSession s1) { |
|
136 |
return true; |
|
137 |
} |
|
138 |
}); |
|
139 |
urlcs.setSSLSocketFactory (clientCtx.getSocketFactory()); |
|
140 |
} |
|
141 |
byte [] buf = new byte [4096]; |
|
142 |
||
143 |
String s = "chunk"; |
|
144 |
if (fixedLen) { |
|
145 |
urlc.setRequestProperty ("XFixed", "yes"); |
|
146 |
s = "fixed"; |
|
147 |
} |
|
148 |
InputStream is = urlc.getInputStream(); |
|
149 |
File temp; |
|
150 |
synchronized (fileLock) { |
|
151 |
temp = File.createTempFile (s, null); |
|
152 |
temp.deleteOnExit(); |
|
153 |
} |
|
154 |
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp)); |
|
155 |
int c, count = 0; |
|
156 |
while ((c=is.read(buf)) != -1) { |
|
157 |
count += c; |
|
158 |
fout.write (buf, 0, c); |
|
159 |
} |
|
160 |
is.close(); |
|
161 |
fout.close(); |
|
162 |
||
163 |
if (count != size) { |
|
164 |
System.out.println ("wrong amount of data returned"); |
|
165 |
System.out.println ("fixedLen = "+fixedLen); |
|
166 |
System.out.println ("protocol = "+protocol); |
|
167 |
System.out.println ("root = "+root); |
|
168 |
System.out.println ("port = "+port); |
|
169 |
System.out.println ("f = "+f); |
|
170 |
System.out.println ("size = "+size); |
|
171 |
System.out.println ("temp = "+temp); |
|
172 |
System.out.println ("count = "+count); |
|
173 |
error = true; |
|
174 |
} |
|
175 |
String orig = root + "/" + f; |
|
176 |
compare (new File(orig), temp); |
|
177 |
temp.delete(); |
|
178 |
} catch (IOException e) { |
|
179 |
error = true; |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
/* compare the contents of the two files */ |
|
185 |
||
186 |
static void compare (File f1, File f2) throws IOException { |
|
187 |
InputStream i1 = new BufferedInputStream (new FileInputStream(f1)); |
|
188 |
InputStream i2 = new BufferedInputStream (new FileInputStream(f2)); |
|
189 |
||
190 |
int c1,c2; |
|
191 |
try { |
|
192 |
while ((c1=i1.read()) != -1) { |
|
193 |
c2 = i2.read(); |
|
194 |
if (c1 != c2) { |
|
195 |
throw new RuntimeException ("file compare failed 1"); |
|
196 |
} |
|
197 |
} |
|
198 |
if (i2.read() != -1) { |
|
199 |
throw new RuntimeException ("file compare failed 2"); |
|
200 |
} |
|
201 |
} finally { |
|
202 |
i1.close(); |
|
203 |
i2.close(); |
|
204 |
} |
|
205 |
} |
|
206 |
} |