author | chegar |
Fri, 10 Sep 2010 15:57:05 +0100 | |
changeset 6536 | 2694e08f83ea |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2008, 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 4333920 |
|
27 |
* @run main ChunkedEncodingTest |
|
28 |
* @summary ChunkedEncodingTest unit test |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
import java.net.*; |
|
33 |
import java.security.*; |
|
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
34 |
import com.sun.net.httpserver.HttpServer; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
35 |
import com.sun.net.httpserver.HttpHandler; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
36 |
import com.sun.net.httpserver.HttpExchange; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
37 |
import static java.lang.System.out; |
2 | 38 |
|
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
39 |
public class ChunkedEncodingTest{ |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
40 |
private static MessageDigest serverDigest, clientDigest; |
6536
2694e08f83ea
6980517: TEST_BUG sun\net\www\http\ChunkedInputStream\ChunkedEncodingTest.java NullPointerException
chegar
parents:
5506
diff
changeset
|
41 |
private static volatile byte[] serverMac, clientMac; |
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
42 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
43 |
static void client(String u) throws Exception { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
44 |
URL url = new URL(u); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
45 |
out.println("client opening connection to: " + u); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
46 |
URLConnection urlc = url.openConnection(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
47 |
DigestInputStream dis = |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
48 |
new DigestInputStream(urlc.getInputStream(), clientDigest); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
49 |
while (dis.read() != -1); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
50 |
clientMac = dis.getMessageDigest().digest(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
51 |
dis.close(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
52 |
} |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
53 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
54 |
public static void test() { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
55 |
HttpServer server = null; |
2 | 56 |
try { |
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
57 |
serverDigest = MessageDigest.getInstance("MD5"); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
58 |
clientDigest = MessageDigest.getInstance("MD5"); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
59 |
server = startHttpServer(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
60 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
61 |
int port = server.getAddress().getPort(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
62 |
out.println ("Server listening on port: " + port); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
63 |
client("http://localhost:" + port + "/chunked/"); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
64 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
65 |
if (!MessageDigest.isEqual(clientMac, serverMac)) { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
66 |
throw new RuntimeException( |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
67 |
"Data received is NOT equal to the data sent"); |
2 | 68 |
} |
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
69 |
} catch (Exception e) { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
70 |
throw new RuntimeException(e); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
71 |
} finally { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
72 |
if (server != null) |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
73 |
server.stop(0); |
2 | 74 |
} |
75 |
} |
|
76 |
||
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
77 |
public static void main(String[] args) { |
2 | 78 |
test(); |
79 |
} |
|
80 |
||
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
81 |
/** |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
82 |
* Http Server |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
83 |
*/ |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
84 |
static HttpServer startHttpServer() throws IOException { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
85 |
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
86 |
HttpHandler httpHandler = new SimpleHandler(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
87 |
httpServer.createContext("/chunked/", httpHandler); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
88 |
httpServer.start(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
89 |
return httpServer; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
90 |
} |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
91 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
92 |
static class SimpleHandler implements HttpHandler { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
93 |
static byte[] baMessage; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
94 |
final static int CHUNK_SIZE = 8 * 1024; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
95 |
final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
96 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
97 |
static { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
98 |
baMessage = new byte[MESSAGE_LENGTH]; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
99 |
for (int i=0; i<MESSAGE_LENGTH; i++) |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
100 |
baMessage[i] = (byte)i; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
101 |
} |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
102 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
103 |
@Override |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
104 |
public void handle(HttpExchange t) throws IOException { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
105 |
InputStream is = t.getRequestBody(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
106 |
while (is.read() != -1); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
107 |
is.close(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
108 |
|
6536
2694e08f83ea
6980517: TEST_BUG sun\net\www\http\ChunkedInputStream\ChunkedEncodingTest.java NullPointerException
chegar
parents:
5506
diff
changeset
|
109 |
t.sendResponseHeaders (200, 0); |
1339
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
110 |
OutputStream os = t.getResponseBody(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
111 |
DigestOutputStream dos = new DigestOutputStream(os, serverDigest); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
112 |
|
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
113 |
int offset = 0; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
114 |
for (int i=0; i<52; i++) { |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
115 |
dos.write(baMessage, offset, CHUNK_SIZE); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
116 |
offset += CHUNK_SIZE; |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
117 |
} |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
118 |
serverMac = serverDigest.digest(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
119 |
os.close(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
120 |
t.close(); |
5fb372f1d51f
6621697: Problem with file test/sun/net/www/http/ChunkedInputStream/test.txt
chegar
parents:
2
diff
changeset
|
121 |
} |
2 | 122 |
} |
123 |
} |