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 |
* - 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(); |
|
3070
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
89 |
if (s1 != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
90 |
s1.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
91 |
if (s2 != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
92 |
s2.stop(2); |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
93 |
if (executor != null) |
0175bbf9a833
6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents:
2
diff
changeset
|
94 |
executor.shutdown (); |
2 | 95 |
} |
96 |
} |
|
97 |
||
98 |
static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception { |
|
99 |
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); |
|
100 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
|
101 |
if (urlc instanceof HttpsURLConnection) { |
|
102 |
HttpsURLConnection urlcs = (HttpsURLConnection) urlc; |
|
103 |
urlcs.setHostnameVerifier (new HostnameVerifier () { |
|
104 |
public boolean verify (String s, SSLSession s1) { |
|
105 |
return true; |
|
106 |
} |
|
107 |
}); |
|
108 |
urlcs.setSSLSocketFactory (ctx.getSocketFactory()); |
|
109 |
} |
|
110 |
byte [] buf = new byte [4096]; |
|
111 |
||
112 |
if (fixedLen) { |
|
113 |
urlc.setRequestProperty ("XFixed", "yes"); |
|
114 |
} |
|
115 |
InputStream is = urlc.getInputStream(); |
|
116 |
File temp = File.createTempFile ("Test1", null); |
|
117 |
temp.deleteOnExit(); |
|
118 |
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp)); |
|
119 |
int c, count = 0; |
|
120 |
while ((c=is.read(buf)) != -1) { |
|
121 |
count += c; |
|
122 |
fout.write (buf, 0, c); |
|
123 |
} |
|
124 |
is.close(); |
|
125 |
fout.close(); |
|
126 |
||
127 |
if (count != size) { |
|
128 |
throw new RuntimeException ("wrong amount of data returned"); |
|
129 |
} |
|
130 |
String orig = root + "/" + f; |
|
131 |
compare (new File(orig), temp); |
|
132 |
temp.delete(); |
|
133 |
} |
|
134 |
||
135 |
/* compare the contents of the two files */ |
|
136 |
||
137 |
static void compare (File f1, File f2) throws IOException { |
|
138 |
InputStream i1 = new BufferedInputStream (new FileInputStream(f1)); |
|
139 |
InputStream i2 = new BufferedInputStream (new FileInputStream(f2)); |
|
140 |
||
141 |
int c1,c2; |
|
142 |
try { |
|
143 |
while ((c1=i1.read()) != -1) { |
|
144 |
c2 = i2.read(); |
|
145 |
if (c1 != c2) { |
|
146 |
throw new RuntimeException ("file compare failed 1"); |
|
147 |
} |
|
148 |
} |
|
149 |
if (i2.read() != -1) { |
|
150 |
throw new RuntimeException ("file compare failed 2"); |
|
151 |
} |
|
152 |
} finally { |
|
153 |
i1.close(); |
|
154 |
i2.close(); |
|
155 |
} |
|
156 |
} |
|
157 |
} |