Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
38867 views
1
/*
2
* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
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
*
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.
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.*;
34
import com.sun.net.httpserver.HttpServer;
35
import com.sun.net.httpserver.HttpHandler;
36
import com.sun.net.httpserver.HttpExchange;
37
import static java.lang.System.out;
38
39
public class ChunkedEncodingTest{
40
private static MessageDigest serverDigest, clientDigest;
41
private static volatile byte[] serverMac, clientMac;
42
43
static void client(String u) throws Exception {
44
URL url = new URL(u);
45
out.println("client opening connection to: " + u);
46
URLConnection urlc = url.openConnection();
47
DigestInputStream dis =
48
new DigestInputStream(urlc.getInputStream(), clientDigest);
49
while (dis.read() != -1);
50
clientMac = dis.getMessageDigest().digest();
51
dis.close();
52
}
53
54
public static void test() {
55
HttpServer server = null;
56
try {
57
serverDigest = MessageDigest.getInstance("MD5");
58
clientDigest = MessageDigest.getInstance("MD5");
59
server = startHttpServer();
60
61
int port = server.getAddress().getPort();
62
out.println ("Server listening on port: " + port);
63
client("http://localhost:" + port + "/chunked/");
64
65
if (!MessageDigest.isEqual(clientMac, serverMac)) {
66
throw new RuntimeException(
67
"Data received is NOT equal to the data sent");
68
}
69
} catch (Exception e) {
70
throw new RuntimeException(e);
71
} finally {
72
if (server != null)
73
server.stop(0);
74
}
75
}
76
77
public static void main(String[] args) {
78
test();
79
}
80
81
/**
82
* Http Server
83
*/
84
static HttpServer startHttpServer() throws IOException {
85
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
86
HttpHandler httpHandler = new SimpleHandler();
87
httpServer.createContext("/chunked/", httpHandler);
88
httpServer.start();
89
return httpServer;
90
}
91
92
static class SimpleHandler implements HttpHandler {
93
static byte[] baMessage;
94
final static int CHUNK_SIZE = 8 * 1024;
95
final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE;
96
97
static {
98
baMessage = new byte[MESSAGE_LENGTH];
99
for (int i=0; i<MESSAGE_LENGTH; i++)
100
baMessage[i] = (byte)i;
101
}
102
103
@Override
104
public void handle(HttpExchange t) throws IOException {
105
InputStream is = t.getRequestBody();
106
while (is.read() != -1);
107
is.close();
108
109
t.sendResponseHeaders (200, 0);
110
OutputStream os = t.getResponseBody();
111
DigestOutputStream dos = new DigestOutputStream(os, serverDigest);
112
113
int offset = 0;
114
for (int i=0; i<52; i++) {
115
dos.write(baMessage, offset, CHUNK_SIZE);
116
offset += CHUNK_SIZE;
117
}
118
serverMac = serverDigest.digest();
119
os.close();
120
t.close();
121
}
122
}
123
}
124
125