author | dholmes |
Wed, 06 Nov 2019 21:18:42 -0500 | |
changeset 58956 | 9a0a5e70eeb2 |
parent 55330 | 1fef7d9309a9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, 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 |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
47216
diff
changeset
|
28 |
* @build jdk.test.lib.net.SimpleSSLContext |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
29 |
* @run main/othervm Test13 |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
30 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test13 |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
47216
diff
changeset
|
31 |
* @summary Light weight HTTP server |
2 | 32 |
*/ |
33 |
||
34 |
import com.sun.net.httpserver.*; |
|
35 |
||
36 |
import java.util.concurrent.*; |
|
7271 | 37 |
import java.util.logging.*; |
2 | 38 |
import java.io.*; |
39 |
import java.net.*; |
|
40 |
import javax.net.ssl.*; |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
47216
diff
changeset
|
41 |
import jdk.test.lib.net.SimpleSSLContext; |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
42 |
import jdk.test.lib.net.URIBuilder; |
2 | 43 |
|
44 |
/* basic http/s connectivity test |
|
45 |
* Tests: |
|
46 |
* - same as Test12, but with 64 threads |
|
47 |
*/ |
|
48 |
||
49 |
public class Test13 extends Test { |
|
50 |
||
51 |
static SSLContext ctx; |
|
52 |
||
7271 | 53 |
final static int NUM = 32; // was 32 |
54 |
||
2 | 55 |
static boolean fail = false; |
56 |
||
57 |
public static void main (String[] args) throws Exception { |
|
58 |
HttpServer s1 = null; |
|
59 |
HttpsServer s2 = null; |
|
60 |
ExecutorService executor=null; |
|
7271 | 61 |
Logger l = Logger.getLogger ("com.sun.net.httpserver"); |
62 |
Handler ha = new ConsoleHandler(); |
|
63 |
ha.setLevel(Level.ALL); |
|
64 |
l.setLevel(Level.ALL); |
|
65 |
l.addHandler(ha); |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
66 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
67 |
|
2 | 68 |
try { |
69 |
String root = System.getProperty ("test.src")+ "/docs"; |
|
70 |
System.out.print ("Test13: "); |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
71 |
InetSocketAddress addr = new InetSocketAddress(loopback, 0); |
2 | 72 |
s1 = HttpServer.create (addr, 0); |
73 |
s2 = HttpsServer.create (addr, 0); |
|
74 |
HttpHandler h = new FileServerHandler (root); |
|
75 |
HttpContext c1 = s1.createContext ("/test1", h); |
|
76 |
HttpContext c2 = s2.createContext ("/test1", h); |
|
77 |
executor = Executors.newCachedThreadPool(); |
|
78 |
s1.setExecutor (executor); |
|
79 |
s2.setExecutor (executor); |
|
26206
fb87c4051d65
8055747: Move SimpleSSLContext to jdk/testlibrary
michaelm
parents:
7668
diff
changeset
|
80 |
ctx = new SimpleSSLContext().get(); |
2 | 81 |
s2.setHttpsConfigurator(new HttpsConfigurator (ctx)); |
82 |
s1.start(); |
|
83 |
s2.start(); |
|
84 |
||
85 |
int port = s1.getAddress().getPort(); |
|
86 |
int httpsport = s2.getAddress().getPort(); |
|
7271 | 87 |
Runner r[] = new Runner[NUM*2]; |
88 |
for (int i=0; i<NUM; i++) { |
|
2 | 89 |
r[i] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23); |
7271 | 90 |
r[i+NUM] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23); |
2 | 91 |
} |
92 |
start (r); |
|
93 |
join (r); |
|
94 |
System.out.println ("OK"); |
|
95 |
} finally { |
|
96 |
delay(); |
|
3070
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
97 |
if (s1 != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
98 |
s1.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
99 |
if (s2 != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
100 |
s2.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
101 |
if (executor != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
102 |
executor.shutdown (); |
2 | 103 |
} |
104 |
} |
|
105 |
||
106 |
static void start (Runner[] x) { |
|
107 |
for (int i=0; i<x.length; i++) { |
|
7271 | 108 |
if (x[i] != null) |
2 | 109 |
x[i].start(); |
110 |
} |
|
111 |
} |
|
112 |
||
113 |
static void join (Runner[] x) { |
|
114 |
for (int i=0; i<x.length; i++) { |
|
115 |
try { |
|
7271 | 116 |
if (x[i] != null) |
2 | 117 |
x[i].join(); |
118 |
} catch (InterruptedException e) {} |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
||
123 |
static class Runner extends Thread { |
|
124 |
||
125 |
boolean fixedLen; |
|
126 |
String protocol; |
|
127 |
String root; |
|
128 |
int port; |
|
129 |
String f; |
|
130 |
int size; |
|
131 |
||
132 |
Runner (boolean fixedLen, String protocol, String root, int port, String f, int size) { |
|
133 |
this.fixedLen=fixedLen; |
|
134 |
this.protocol=protocol; |
|
135 |
this.root=root; |
|
136 |
this.port=port; |
|
137 |
this.f=f; |
|
138 |
this.size = size; |
|
139 |
} |
|
140 |
||
141 |
public void run () { |
|
142 |
try { |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
143 |
URL url = URIBuilder.newBuilder() |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
144 |
.scheme(protocol) |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
145 |
.loopback() |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
146 |
.port(port) |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
147 |
.path("/test1/"+f) |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
148 |
.toURL(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
52121
diff
changeset
|
149 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); |
2 | 150 |
if (urlc instanceof HttpsURLConnection) { |
151 |
HttpsURLConnection urlcs = (HttpsURLConnection) urlc; |
|
152 |
urlcs.setHostnameVerifier (new HostnameVerifier () { |
|
153 |
public boolean verify (String s, SSLSession s1) { |
|
154 |
return true; |
|
155 |
} |
|
156 |
}); |
|
157 |
urlcs.setSSLSocketFactory (ctx.getSocketFactory()); |
|
158 |
} |
|
159 |
byte [] buf = new byte [4096]; |
|
160 |
||
161 |
if (fixedLen) { |
|
162 |
urlc.setRequestProperty ("XFixed", "yes"); |
|
163 |
} |
|
164 |
InputStream is = urlc.getInputStream(); |
|
165 |
File temp = File.createTempFile ("Test1", null); |
|
166 |
temp.deleteOnExit(); |
|
167 |
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp)); |
|
168 |
int c, count = 0; |
|
169 |
while ((c=is.read(buf)) != -1) { |
|
170 |
count += c; |
|
171 |
fout.write (buf, 0, c); |
|
172 |
} |
|
173 |
is.close(); |
|
174 |
fout.close(); |
|
175 |
||
176 |
if (count != size) { |
|
177 |
throw new RuntimeException ("wrong amount of data returned"); |
|
178 |
} |
|
179 |
String orig = root + "/" + f; |
|
180 |
compare (new File(orig), temp); |
|
181 |
temp.delete(); |
|
182 |
} catch (Exception e) { |
|
183 |
e.printStackTrace(); |
|
184 |
fail = true; |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
/* compare the contents of the two files */ |
|
190 |
||
191 |
static void compare (File f1, File f2) throws IOException { |
|
192 |
InputStream i1 = new BufferedInputStream (new FileInputStream(f1)); |
|
193 |
InputStream i2 = new BufferedInputStream (new FileInputStream(f2)); |
|
194 |
||
195 |
int c1,c2; |
|
196 |
try { |
|
197 |
while ((c1=i1.read()) != -1) { |
|
198 |
c2 = i2.read(); |
|
199 |
if (c1 != c2) { |
|
200 |
throw new RuntimeException ("file compare failed 1"); |
|
201 |
} |
|
202 |
} |
|
203 |
if (i2.read() != -1) { |
|
204 |
throw new RuntimeException ("file compare failed 2"); |
|
205 |
} |
|
206 |
} finally { |
|
207 |
i1.close(); |
|
208 |
i2.close(); |
|
209 |
} |
|
210 |
} |
|
211 |
} |