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